-
Notifications
You must be signed in to change notification settings - Fork 2
/
healthcheck_test.go
240 lines (203 loc) · 6.35 KB
/
healthcheck_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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package healthz
import (
"context"
"encoding/json"
"errors"
"fmt"
"net/http"
"net/http/httptest"
"sync"
"testing"
"time"
)
func TestHealthChecks_Default(t *testing.T) {
hc, sc, err := getHealth()
if err != nil {
t.Fatalf("Expected no error, got '%s'", err.Error())
}
if sc != http.StatusOK {
t.Fatalf("Expected status code to equal '%d', got '%d'", http.StatusOK, sc)
}
if ln := len(hc.Tests); ln != 1 {
t.Fatalf("Expected '%d' tests, got '%d'", 1, ln)
}
}
func TestHealthChecks_Custom(t *testing.T) {
t.Run("with multiple tests", func(t *testing.T) {
defer resetTests()
executedTests := 0
var l sync.Mutex
tstFuncFail := func(_ context.Context) (Status, error) {
l.Lock()
defer l.Unlock()
executedTests++
return Unavailable, errors.New("unavailable")
}
tstFuncSuccess := func(_ context.Context) (Status, error) {
l.Lock()
defer l.Unlock()
executedTests++
return Available, nil
}
RegisterTest("custom-failure1", tstFuncFail)
RegisterTest("custom-success1", tstFuncSuccess)
RegisterTest("custom-failure2", tstFuncFail)
RegisterTest("custom-success2", tstFuncSuccess)
hc, _, err := getHealth()
if err != nil {
t.Fatalf("Expected no error, got '%s'", err.Error())
}
// account for default test
if ln := len(hc.Tests); ln != 5 {
t.Fatalf("Expected '%d' tests, got '%d'", 5, ln)
}
if executedTests != 4 {
t.Fatalf("Expected '%d' tests to be executed, got '%d'", 4, executedTests)
}
})
t.Run("with an exceeded deadline", func(t *testing.T) {
defer resetTests()
Timeout = 50 * time.Millisecond
tstFunc := func(_ context.Context) (Status, error) {
time.Sleep(time.Second)
return Available, nil
}
RegisterTest("success", tstFunc)
hc, _, err := getHealth()
if err != nil {
t.Fatalf("Expected no error, got '%s'", err.Error())
}
// our registered test shouldn't be skipped
if ln := len(hc.Tests); ln != 2 {
t.Fatalf("Expected '%d' tests, got '%d'", 1, ln)
}
// our registered test should be marked unavailable despite what the
// outcome is as the response will be too late
if hc.Tests["success"].Status != Unavailable {
t.Fatalf("Expected 'success' test to be Unavailable, got '%s'", hc.Tests["success"].Status)
}
if hc.Tests["success"].Error != ErrTimeout {
t.Fatalf("Expected 'success' test to be timeout")
}
})
t.Run("with a failing test", func(t *testing.T) {
defer resetTests()
tstFunc := func(_ context.Context) (Status, error) {
return Unavailable, errors.New("unavailable")
}
RegisterTest("custom-failure", tstFunc)
hc, sc, err := getHealth()
if err != nil {
t.Fatalf("Expected no error, got '%s'", err.Error())
}
if sc != http.StatusServiceUnavailable {
t.Fatalf("Expected status code to equal '%d', got '%d'", http.StatusServiceUnavailable, sc)
}
if ln := len(hc.Tests); ln != 2 {
t.Fatalf("Expected '%d' tests, got '%d'", 2, ln)
}
if hc.Status != Unavailable {
t.Fatalf("Expected result to equal '%s', got '%s'", Unavailable, hc.Status)
}
if d := string(hc.Tests["custom-failure"].Error); d != string(Unavailable) {
t.Fatalf("Expected custom-failure to equal '%s', got '%s'", string(Unavailable), d)
}
})
t.Run("with a degraded test", func(t *testing.T) {
defer resetTests()
tstFunc := func(_ context.Context) (Status, error) {
return Degraded, errors.New("degraded")
}
RegisterTest("custom-failure", tstFunc)
hc, sc, err := getHealth()
if err != nil {
t.Fatalf("Expected no error, got '%s'", err.Error())
}
if sc != http.StatusOK {
t.Fatalf("Expected status code to equal '%d', got '%d'", http.StatusOK, sc)
}
if ln := len(hc.Tests); ln != 2 {
t.Fatalf("Expected '%d' tests, got '%d'", 2, ln)
}
if hc.Status != Degraded {
t.Fatalf("Expected result to equal '%s', got '%s'", Degraded, hc.Status)
}
if d := string(hc.Tests["custom-failure"].Error); d != string(Degraded) {
t.Fatalf("Expected custom-failure to equal '%s', got '%s'", string(Degraded), d)
}
})
t.Run("with a degraded and unavailable test", func(t *testing.T) {
defer resetTests()
RegisterTest("degraded", func(_ context.Context) (Status, error) {
return Degraded, errors.New("degraded")
})
RegisterTest("unavailable", func(_ context.Context) (Status, error) {
return Unavailable, errors.New("unavailable")
})
hc, sc, err := getHealth()
if err != nil {
t.Fatalf("Expected no error, got '%s'", err.Error())
}
if sc != http.StatusServiceUnavailable {
t.Fatalf("Expected status code to equal '%d', got '%d'", http.StatusServiceUnavailable, sc)
}
if ln := len(hc.Tests); ln != 3 {
t.Fatalf("Expected '%d' tests, got '%d'", 3, ln)
}
if hc.Status != Unavailable {
t.Fatalf("Expected result to equal '%s', got '%s'", Unavailable, hc.Status)
}
if d := string(hc.Tests["degraded"].Error); d != string(Degraded) {
t.Fatalf("Expected degraded test to equal '%s', got '%s'", string(Degraded), d)
}
if d := string(hc.Tests["unavailable"].Error); d != string(Unavailable) {
t.Fatalf("Expected unavailable test to equal '%s', got '%s'", string(Unavailable), d)
}
})
t.Run("with a passing test", func(t *testing.T) {
defer resetTests()
tstFunc := func(_ context.Context) (Status, error) {
return Available, nil
}
RegisterTest("custom-success", tstFunc)
hc, sc, err := getHealth()
if err != nil {
t.Fatalf("Expected no error, got '%s'", err.Error())
}
if sc != http.StatusOK {
t.Fatalf("Expected status code to equal '%d', got '%d'", http.StatusOK, sc)
}
if ln := len(hc.Tests); ln != 2 {
t.Fatalf("Expected '%d' tests, got '%d'", 2, ln)
}
if hc.Status != Available {
t.Fatalf("Expected result to equal '%s', got '%s'", Available, hc.Status)
}
})
}
func getHealth() (HealthCheck, int, error) {
hdlr := NewHandler(http.NewServeMux())
srv := httptest.NewServer(hdlr)
defer srv.Close()
hc := HealthCheck{}
req, err := http.NewRequest(http.MethodGet, srv.URL+"/_healthz", nil)
if err != nil {
return hc, 0, err
}
cl := &http.Client{}
rsp, err := cl.Do(req)
if err != nil {
return hc, 0, err
}
defer rsp.Body.Close()
if rsp.StatusCode == 200 || rsp.StatusCode == 503 {
err := json.NewDecoder(rsp.Body).Decode(&hc)
return hc, rsp.StatusCode, err
}
return hc, 0, fmt.Errorf("Unexpected status code: %d", rsp.StatusCode)
}
func resetTests() {
healthCheckTests = map[string]TestFunc{}
Timeout = 5 * time.Second
RegisterTest("default", defaultCheck)
}