-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgrpc_test.go
98 lines (89 loc) · 2.27 KB
/
grpc_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
package grpc_test
import (
"context"
"log/slog"
"net"
"testing"
"github.com/stretchr/testify/assert"
"google.golang.org/grpc"
"google.golang.org/grpc/health"
"google.golang.org/grpc/health/grpc_health_v1"
ph "github.com/isometry/platform-health/pkg/platform_health"
provider_grpc "github.com/isometry/platform-health/pkg/provider/grpc"
)
func init() {
slog.SetLogLoggerLevel(slog.LevelError)
}
func TestGetHealth(t *testing.T) {
listener, err := net.Listen("tcp", "localhost:0")
if err != nil {
t.Fatalf("Failed to set up test server: %v", err)
}
listenPort := listener.Addr().(*net.TCPAddr).Port
defer listener.Close()
// Start a gRPC server that implements the Health service
server := grpc.NewServer()
healthServer := health.NewServer()
grpc_health_v1.RegisterHealthServer(server, healthServer)
go func() { _ = server.Serve(listener) }()
defer server.Stop()
tests := []struct {
name string
grpc *provider_grpc.GRPC
status grpc_health_v1.HealthCheckResponse_ServingStatus
expected ph.Status
}{
{
name: "HealthyService",
grpc: &provider_grpc.GRPC{
Name: "test",
Host: "localhost",
Port: listenPort,
Service: "",
},
status: grpc_health_v1.HealthCheckResponse_SERVING,
expected: ph.Status_HEALTHY,
},
{
name: "UnhealthyService",
grpc: &provider_grpc.GRPC{
Name: "test",
Host: "localhost",
Port: listenPort,
Service: "",
},
status: grpc_health_v1.HealthCheckResponse_NOT_SERVING,
expected: ph.Status_UNHEALTHY,
},
{
name: "UnknownService",
grpc: &provider_grpc.GRPC{
Name: "test",
Host: "localhost",
Port: listenPort,
Service: "unknown",
},
status: grpc_health_v1.HealthCheckResponse_UNKNOWN,
expected: ph.Status_UNHEALTHY,
},
{
name: "InvalidTarget",
grpc: &provider_grpc.GRPC{
Name: "test",
Host: "localhost",
Port: 1,
Service: "",
},
status: grpc_health_v1.HealthCheckResponse_UNKNOWN,
expected: ph.Status_UNHEALTHY,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
healthServer.SetServingStatus(tt.grpc.Service, tt.status)
tt.grpc.SetDefaults()
service := tt.grpc.GetHealth(context.Background())
assert.Equal(t, tt.expected, service.Status)
})
}
}