-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsatellite_test.go
119 lines (103 loc) · 2.5 KB
/
satellite_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package satellite_test
import (
"context"
"log/slog"
"net"
"testing"
"time"
"github.com/stretchr/testify/assert"
ph "github.com/isometry/platform-health/pkg/platform_health"
"github.com/isometry/platform-health/pkg/provider"
"github.com/isometry/platform-health/pkg/provider/mock"
"github.com/isometry/platform-health/pkg/provider/satellite"
"github.com/isometry/platform-health/pkg/server"
)
func init() {
slog.SetLogLoggerLevel(slog.LevelError)
}
type testConfig []provider.Instance
func (c *testConfig) GetInstances() []provider.Instance {
return *c
}
func TestSatelliteGetHealth(t *testing.T) {
// Start listener for the main server
listener, err := net.Listen("tcp", "localhost:0")
if err != nil {
t.Fatalf("Failed to set up test listener: %v", err)
}
port := listener.Addr().(*net.TCPAddr).Port
serverId := "root"
config := &testConfig{}
testServer, err := server.NewPlatformHealthServer(&serverId, config)
if err != nil {
t.Fatalf("Failed to set up test server: %v", err)
}
go testServer.Serve(listener)
defer testServer.Stop()
tests := []struct {
name string
port int
hops []string
config testConfig
expected ph.Status
}{
{
name: "EmptyConfig",
port: port,
config: []provider.Instance{},
expected: ph.Status_HEALTHY,
},
{
name: "HealthyComponent",
port: port,
config: []provider.Instance{
&mock.Mock{
Name: "Test",
Health: ph.Status_HEALTHY,
},
},
expected: ph.Status_HEALTHY,
},
{
name: "UnhealthyComponent",
port: port,
config: []provider.Instance{
&mock.Mock{
Name: "Test",
Health: ph.Status_UNHEALTHY,
},
},
expected: ph.Status_UNHEALTHY,
},
{
name: "SatelliteDown",
port: 1,
expected: ph.Status_UNHEALTHY,
},
{
name: "SatelliteLoop",
port: port,
hops: []string{serverId},
config: []provider.Instance{},
expected: ph.Status_LOOP_DETECTED,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
component := &satellite.Satellite{
Name: "TestSatellite",
Host: "localhost",
Port: tt.port,
Timeout: time.Second,
}
component.SetDefaults()
*config = tt.config
ctx := server.ContextWithHops(context.Background(), tt.hops)
result := component.GetHealth(ctx)
assert.NotNil(t, result)
assert.Equal(t, satellite.TypeSatellite, result.GetType())
assert.Equal(t, component.Name, result.GetName())
assert.Equal(t, tt.expected, result.GetStatus())
})
}
}