-
Notifications
You must be signed in to change notification settings - Fork 0
/
healthz_test.go
160 lines (139 loc) · 4.23 KB
/
healthz_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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
package healthz_test
import (
"context"
"encoding/json"
"io/ioutil"
"log"
"net"
"net/http"
"testing"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"github.com/chiguirez/healthz"
proto "github.com/chiguirez/healthz/proto/health/v1"
)
//nolint:funlen
func TestHealthCheck(t *testing.T) {
t.Run("Given a service and a HealthCheck GRPC Client", func(t *testing.T) {
healthz.Register()
defer healthz.Unregister()
// Set up a connection to the server.
conn, err := grpc.Dial(":8080", grpc.WithInsecure())
if err != nil {
log.Fatalf("did not connect: %v", err)
}
defer conn.Close()
checkClient := proto.NewHealthClient(conn)
t.Run("When Checked via GRPC", func(t *testing.T) {
check, err := checkClient.Check(context.Background(), &proto.HealthCheckRequest{})
if err != nil {
t.Fatalf("did not connect: %v", err)
}
t.Run("Then is serving", func(t *testing.T) {
if check.Status.String() != "SERVING" {
t.Fatalf("status supposed to be SERVING, got: %v instead", check)
}
})
})
t.Run("When Pinged via GRPC Then Pong is always true", func(t *testing.T) {
pong, err := checkClient.Ping(context.Background(), &proto.PingRequest{})
if err != nil {
t.Fatalf("did not connect: %v", err)
}
if pong.GetPong() != true {
t.Fatalf("Pong expected to be true got: %v instead", err)
}
})
})
t.Run("Given a service and a fault dependency", func(t *testing.T) {
healthz.Register(healthz.WithChecker(faultInMemoryChecker{}))
defer healthz.Unregister()
// Set up a connection to the server.
conn, err := grpc.Dial(":8080", grpc.WithInsecure())
if err != nil {
t.Fatalf("did not connect: %v", err)
}
defer conn.Close()
checkClient := proto.NewHealthClient(conn)
t.Run("When Checked", func(t *testing.T) {
_, err := checkClient.Check(context.Background(), &proto.HealthCheckRequest{})
t.Run("Then is not serving", func(t *testing.T) {
if err != nil {
fromError, ok := status.FromError(err)
if !ok {
t.Fatalf("did not connect: %v", err)
}
if fromError.Code() != codes.Unavailable {
t.Fatalf("expect Unavailable got : %s instead", fromError.Code().String())
}
}
if err == nil {
t.Fatal("health check expected to be not serving")
}
})
})
})
t.Run("Given a GRPCServer", func(t *testing.T) {
lis, err := net.Listen("tcp", "localhost:8080")
if err != nil {
t.Fatalf("failed to listen: %v", err)
}
s := grpc.NewServer()
defer s.GracefulStop()
healthz.Register(healthz.WithGRPCServer(s))
defer healthz.Unregister()
go func() {
_ = s.Serve(lis)
}()
// Set up a connection to the server.
conn, err := grpc.Dial(":8080", grpc.WithInsecure())
if err != nil {
t.Fatalf("did not connect: %v", err)
}
defer conn.Close()
checkClient := proto.NewHealthClient(conn)
t.Run("When Checked", func(t *testing.T) {
check, err := checkClient.Check(context.Background(), &proto.HealthCheckRequest{})
if err != nil {
t.Fatalf("did not connect: %v", err)
}
t.Run("Then is serving", func(t *testing.T) {
if check.Status.String() != "SERVING" {
t.Fatalf("status supposed to be SERVING, got: %v instead", check)
}
})
})
})
t.Run("Given a service and a HealthCheck HTTP Client", func(t *testing.T) {
healthz.Register(healthz.WithHTTPPort(12345))
defer healthz.Unregister()
t.Run("When Pinged via HTTP Then Pong is always true", func(t *testing.T) {
request, err := http.NewRequestWithContext(context.Background(), http.MethodGet, "http://localhost:12345/v1/ping", nil)
if err != nil {
t.Fatalf("did not connect: %v", err)
}
check, err := http.DefaultClient.Do(request)
if err != nil {
t.Fatalf("did not connect: %v", err)
}
defer check.Body.Close()
body, err := ioutil.ReadAll(check.Body)
if err != nil {
t.Fatalf("error fetching data: %v", err)
}
v := make(map[string]interface{}, 1)
err = json.Unmarshal(body, &v)
if err != nil {
t.Fatalf("unmarshalling error : %v", err)
}
if v["pong"] != true {
t.Fatalf("Pong expected to be true got: %v instead", err)
}
})
})
}
type faultInMemoryChecker struct{}
func (i faultInMemoryChecker) HealthCheck(_ context.Context) bool {
return false
}