Skip to content

Commit 4947c01

Browse files
committed
test: add basic pkg/server tests
1 parent b7fd92d commit 4947c01

File tree

1 file changed

+182
-0
lines changed

1 file changed

+182
-0
lines changed

pkg/server/server_test.go

Lines changed: 182 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,182 @@
1+
package server
2+
3+
import (
4+
"context"
5+
"testing"
6+
7+
"github.com/stretchr/testify/assert"
8+
9+
ph "github.com/isometry/platform-health/pkg/platform_health"
10+
"github.com/isometry/platform-health/pkg/platform_health/details"
11+
"github.com/isometry/platform-health/pkg/provider"
12+
"github.com/isometry/platform-health/pkg/provider/mock"
13+
)
14+
15+
type mockConfig []provider.Instance
16+
17+
func (c mockConfig) GetInstances() []provider.Instance {
18+
return c
19+
}
20+
21+
func TestNewPlatformHealthServer(t *testing.T) {
22+
tests := []struct {
23+
name string
24+
options []Option
25+
expectedServices []string
26+
}{
27+
{
28+
name: "No Options",
29+
options: []Option{},
30+
expectedServices: []string{
31+
"platform_health.v1.Health",
32+
},
33+
},
34+
{
35+
name: "With HealthService",
36+
options: []Option{WithHealthService()},
37+
expectedServices: []string{
38+
"grpc.health.v1.Health",
39+
"platform_health.v1.Health",
40+
},
41+
},
42+
{
43+
name: "With Reflection",
44+
options: []Option{WithReflection()},
45+
expectedServices: []string{
46+
"grpc.reflection.v1.ServerReflection",
47+
"grpc.reflection.v1alpha.ServerReflection",
48+
"platform_health.v1.Health",
49+
},
50+
},
51+
{
52+
name: "With HealthService and Reflection",
53+
options: []Option{WithHealthService(), WithReflection()},
54+
expectedServices: []string{
55+
"grpc.health.v1.Health",
56+
"grpc.reflection.v1.ServerReflection",
57+
"grpc.reflection.v1alpha.ServerReflection",
58+
"platform_health.v1.Health",
59+
},
60+
},
61+
}
62+
63+
for _, tt := range tests {
64+
t.Run(tt.name, func(t *testing.T) {
65+
serverId := "test-server"
66+
conf := mockConfig{}
67+
phs, err := NewPlatformHealthServer(&serverId, conf, tt.options...)
68+
if err != nil {
69+
t.Fatalf("NewPlatformHealthServer() error = %v", err)
70+
}
71+
services := phs.grpcServer.GetServiceInfo()
72+
assert.Equal(t, len(tt.expectedServices), len(services), "expected number of services to be registered")
73+
for _, service := range tt.expectedServices {
74+
if _, ok := services[service]; !ok {
75+
t.Errorf("expected %s to be registered, but it was not", service)
76+
}
77+
}
78+
})
79+
}
80+
}
81+
82+
func TestPlatformHealthServer_Check(t *testing.T) {
83+
tests := []struct {
84+
name string
85+
serverId string
86+
hops []string
87+
providerConfig mockConfig
88+
expectedStatus ph.Status
89+
expectedComponents int
90+
expectedDetailLoop *details.Detail_Loop
91+
}{
92+
{
93+
name: "No Providers",
94+
serverId: "server-1",
95+
expectedStatus: ph.Status_HEALTHY,
96+
expectedComponents: 0,
97+
},
98+
{
99+
name: "All Healthy Providers",
100+
serverId: "server-1",
101+
providerConfig: mockConfig{
102+
&mock.Mock{Name: "m1", Health: ph.Status_HEALTHY},
103+
&mock.Mock{Name: "m2", Health: ph.Status_HEALTHY},
104+
},
105+
expectedStatus: ph.Status_HEALTHY,
106+
expectedComponents: 2,
107+
},
108+
{
109+
name: "Unhealthy Provider First",
110+
serverId: "server-1",
111+
providerConfig: mockConfig{
112+
&mock.Mock{Name: "m1", Health: ph.Status_UNHEALTHY},
113+
&mock.Mock{Name: "m2", Health: ph.Status_HEALTHY},
114+
},
115+
expectedStatus: ph.Status_UNHEALTHY,
116+
expectedComponents: 2,
117+
},
118+
{
119+
name: "Unhealthy Provider Last",
120+
serverId: "server-1",
121+
providerConfig: mockConfig{
122+
&mock.Mock{Name: "m1", Health: ph.Status_HEALTHY},
123+
&mock.Mock{Name: "m2", Health: ph.Status_UNHEALTHY},
124+
},
125+
expectedStatus: ph.Status_UNHEALTHY,
126+
expectedComponents: 2,
127+
},
128+
{
129+
name: "Simple Loop",
130+
serverId: "server-1",
131+
hops: []string{"server-1"},
132+
providerConfig: mockConfig{
133+
&mock.Mock{Name: "m1", Health: ph.Status_HEALTHY},
134+
},
135+
expectedStatus: ph.Status_LOOP_DETECTED,
136+
expectedComponents: 0,
137+
expectedDetailLoop: &details.Detail_Loop{ServerIds: []string{"server-1", "server-1"}},
138+
},
139+
{
140+
name: "Complex Loop",
141+
serverId: "server-1",
142+
hops: []string{"server-1", "server-2", "server-3"},
143+
providerConfig: mockConfig{
144+
&mock.Mock{Name: "m1", Health: ph.Status_HEALTHY},
145+
},
146+
expectedStatus: ph.Status_LOOP_DETECTED,
147+
expectedComponents: 0,
148+
expectedDetailLoop: &details.Detail_Loop{ServerIds: []string{"server-1", "server-2", "server-3", "server-1"}},
149+
},
150+
}
151+
152+
for _, tt := range tests {
153+
t.Run(tt.name, func(t *testing.T) {
154+
serverId := tt.serverId
155+
156+
phs, err := NewPlatformHealthServer(&serverId, tt.providerConfig)
157+
if err != nil {
158+
t.Fatalf("NewPlatformHealthServer() error = %v", err)
159+
}
160+
161+
req := &ph.HealthCheckRequest{Hops: tt.hops}
162+
resp, err := phs.Check(context.Background(), req)
163+
if err != nil {
164+
t.Fatalf("Check() error = %v", err)
165+
}
166+
167+
assert.Equal(t, tt.expectedStatus, resp.Status, "expected status to match")
168+
169+
assert.Equal(t, tt.expectedComponents, len(resp.Components), "expected number of components to match")
170+
171+
if tt.expectedDetailLoop != nil {
172+
var detail details.Detail_Loop
173+
assert.Equal(t, 1, len(resp.Details), "expected exactly one detail")
174+
err := resp.Details[0].UnmarshalTo(&detail)
175+
if err != nil {
176+
t.Fatalf("UnmarshalTo() error = %v", err)
177+
}
178+
assert.Equal(t, tt.expectedDetailLoop.ServerIds, detail.ServerIds, "expected detail to match")
179+
}
180+
})
181+
}
182+
}

0 commit comments

Comments
 (0)