-
Notifications
You must be signed in to change notification settings - Fork 14
/
health_checker_test.go
348 lines (280 loc) · 10.1 KB
/
health_checker_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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
package executor
import (
"log"
"net/http"
"net/http/httptest"
"net/url"
"os"
"strconv"
"testing"
"time"
mesos "github.com/mesos/mesos-go/api/v1/lib"
"github.com/pkg/errors"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestDoHealthChecksShouldStartHealthCheckig(t *testing.T) {
delay := time.Millisecond.Seconds()
gracePeriod := 0.0
interval := time.Millisecond.Seconds()
// Create always failing health check.
check := mesos.HealthCheck{
GracePeriodSeconds: &gracePeriod,
DelaySeconds: &delay,
IntervalSeconds: &interval,
}
healthStates := make(chan Event)
// Start health checking.
DoHealthChecks(check, healthStates)
// Wait for first health check result.
select {
case <-healthStates:
case <-time.After(time.Second):
t.Error("Health check state should come in configured timeout")
}
// Wait for next health check result.
select {
case <-healthStates:
case <-time.After(time.Second):
t.Error("Health check state should come in configured timeout")
}
}
func TestHandleHealthResultsShouldProxyAllUnhealthyResultsAfterGracePeriod(t *testing.T) {
healthResults := make(chan error)
healthStates := make(chan Event)
gracePeriod := 0.0
go handleHealthResults(mesos.HealthCheck{GracePeriodSeconds: &gracePeriod}, healthResults, healthStates)
err := errors.New("Error")
healthResults <- err
state := <-healthStates
assert.Equal(t, Event{Type: Unhealthy, Message: err.Error()}, state)
healthResults <- err
state = <-healthStates
assert.Equal(t, Event{Type: Unhealthy, Message: err.Error()}, state)
assert.Empty(t, healthStates)
}
func TestHandleHealthResultsShouldSetKillToTrueWhenConsecutiveFailuresExceedsConfiguration(t *testing.T) {
healthResults := make(chan error)
healthStates := make(chan Event)
gracePeriod := 0.0
maxConsecutiveFailures := uint32(1)
check := mesos.HealthCheck{
GracePeriodSeconds: &gracePeriod,
ConsecutiveFailures: &maxConsecutiveFailures,
}
go handleHealthResults(check, healthResults, healthStates)
err := errors.New("Error")
healthResults <- err
state := <-healthStates
assert.Equal(t, Event{Type: FailedDueToUnhealthy, Message: err.Error()}, state)
assert.Empty(t, healthStates)
}
func TestHandleHealthResultsShouldNotNotifyWhenTaskIsHealthy(t *testing.T) {
healthResults := make(chan error)
healthStates := make(chan Event)
gracePeriod := 0.0
maxConsequeltialFailures := uint32(2)
check := mesos.HealthCheck{
GracePeriodSeconds: &gracePeriod,
ConsecutiveFailures: &maxConsequeltialFailures,
}
go handleHealthResults(check, healthResults, healthStates)
err := errors.New("Error")
healthResults <- err
state := <-healthStates
assert.Equal(t, Event{Type: Unhealthy, Message: err.Error()}, state)
healthResults <- nil
state = <-healthStates
assert.Equal(t, Event{Type: Healthy}, state)
healthResults <- nil
assert.Empty(t, healthStates, "Notification should NOT be sent for recurring healthy states")
}
func TestHandleHealthResultsShouldNotNotifyWhenTaskIsInGracePeriod(t *testing.T) {
healthResults := make(chan error)
healthStates := make(chan Event)
gracePeriod := 200.0
maxConsequeltialFailures := uint32(2)
check := mesos.HealthCheck{
GracePeriodSeconds: &gracePeriod,
ConsecutiveFailures: &maxConsequeltialFailures,
}
go handleHealthResults(check, healthResults, healthStates)
err := errors.New("Error")
healthResults <- err
assert.Empty(t, healthStates, "Notification should NOT be sent for failing task during grace period")
}
func TestNewHealthCheckShouldReturnNilWhenCheckPasses(t *testing.T) {
commandType := mesos.HealthCheck_COMMAND
validCommand := "true"
command := mesos.CommandInfo{Value: &validCommand}
check := mesos.HealthCheck{Type: &commandType, Command: &command}
healthCheck := newHealthCheck(check)
err := healthCheck()
assert.NoError(t, err)
}
func TestNewHealthCheckShouldReturnErrorOnUnknownCheck(t *testing.T) {
healthCheck := newHealthCheck(mesos.HealthCheck{})
err := healthCheck()
assert.EqualError(t, err, "unknown health check type: UNKNOWN")
}
func TestNewHealthCheckShouldReturnErrorOnUdefinedCommand(t *testing.T) {
commandType := mesos.HealthCheck_COMMAND
healthCheck := newHealthCheck(mesos.HealthCheck{Type: &commandType})
err := healthCheck()
assert.EqualError(t, err, "unknown health check type: COMMAND")
}
func TestCommandHealthCheckShouldReturnErrorOnEmptyCheck(t *testing.T) {
commandType := mesos.HealthCheck_COMMAND
check := mesos.HealthCheck{Type: &commandType}
err := commandHealthCheck(check)
assert.EqualError(t, err, "command health check not defined")
}
func TestCommandHealthCheckShouldReturnErrorOnInvalidCommand(t *testing.T) {
invalidCommand := "false"
command := mesos.CommandInfo{Value: &invalidCommand}
check := mesos.HealthCheck{Command: &command}
err := commandHealthCheck(check)
assert.EqualError(t, err, "command health check errored: exit status 1")
}
func TestCommandHealthCheckShouldReturnErrorOnInvalidCheck(t *testing.T) {
cmd := "test $X"
env := mesos.Environment{
Variables: []mesos.Environment_Variable{{Name: "X", Value: "1"}},
}
command := mesos.CommandInfo{Value: &cmd, Environment: &env}
check := mesos.HealthCheck{Command: &command}
err := commandHealthCheck(check)
assert.NoError(t, err)
}
func TestCommandHealthCheckShouldReturnErrorOnInvalidCommandForNonShell(t *testing.T) {
commandType := mesos.HealthCheck_COMMAND
invalidCommand := "(true)"
shell := false
command := mesos.CommandInfo{
Value: &invalidCommand,
Shell: &shell,
}
check := mesos.HealthCheck{Type: &commandType, Command: &command}
err := commandHealthCheck(check)
assert.EqualError(t, err, "command health check errored: exec: \"(true)\": executable file not found in $PATH")
}
func TestCommandHealthCheckShouldReturnErrorOnAfterTimeout(t *testing.T) {
command := mesos.CommandInfo{}
sleep := "sleep 1"
command.Value = &sleep
timeout := 0.0
check := mesos.HealthCheck{Command: &command, TimeoutSeconds: &timeout}
err := commandHealthCheck(check)
assert.EqualError(t, err, "command health check timed out after 0s")
}
func TestIfTCPHealthCheckPassesWhenPortIsOpen(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))
defer ts.Close()
check := buildTCPCheckForTestServer(ts, 0.1)
err := tcpHealthCheck(check)
assert.NoError(t, err)
}
func TestIfTCPHealthCheckFailWhenPortIsClosed(t *testing.T) {
check := buildTCPCheck(0, 0.1)
err := tcpHealthCheck(check)
assert.Error(t, err)
}
func TestIfHTTPHealthCheckWithoutPathPassesWhenOKStatusCodeIsReceived(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
}))
defer ts.Close()
check := buildHTTPCheckForTestServer(ts, 0.1, "")
err := httpHealthCheck(check)
assert.NoError(t, err)
}
func TestIfHTTPHealthCheckWithPathPassesWhenOKStatusCodeIsReceived(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
log.Println(r.URL.Path)
if r.URL.Path == "/status/info" {
w.WriteHeader(http.StatusOK)
} else {
w.WriteHeader(http.StatusNotFound)
}
}))
defer ts.Close()
check := buildHTTPCheckForTestServer(ts, 0.1, "/status/info")
err := httpHealthCheck(check)
assert.NoError(t, err)
}
func TestIfHTTPHealthCheckFailsWhenTimeoutOccur(t *testing.T) {
sleep := make(chan bool)
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
<-sleep // wait until timeout occurs
w.WriteHeader(http.StatusOK)
}))
defer ts.Close()
check := buildHTTPCheckForTestServer(ts, time.Millisecond.Seconds(), "")
err := httpHealthCheck(check)
close(sleep) // release the server
require.Error(t, err)
assert.Contains(t, err.Error(), "Client.Timeout exceeded while awaiting headers")
}
func TestIfHTTPHealthCheckFailsWhenRequestIsInvalid(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusBadRequest)
}))
defer ts.Close()
check := buildHTTPCheckForTestServer(ts, 0.1, "")
err := httpHealthCheck(check)
assert.EqualError(t, err, "health check error: received status code 400, but expected codes between 200 and 399")
}
func TestIfHTTPHealthCheckFailsWhenServiceIsUnavailable(t *testing.T) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusServiceUnavailable)
}))
defer ts.Close()
check := buildHTTPCheckForTestServer(ts, 0.1, "")
err := httpHealthCheck(check)
assert.EqualError(t, err, "health check error: received status code 503, but expected codes between 200 and 399")
}
func TestIfHTTPHealthCheckFailsWhenNoServiceIsListeningOnConfiguredPort(t *testing.T) {
check := buildHTTPCheck("http", 1000, "/", 0.1)
err := httpHealthCheck(check)
assert.Error(t, err)
}
func TestIfUsesPublicIPForHealthCheckAddress(t *testing.T) {
os.Setenv("CLOUD_PUBLIC_IP", "6.6.6.6")
defer os.Unsetenv("CLOUD_PUBLIC_IP")
address := HealthCheckAddress(1234)
assert.Equal(t, "6.6.6.6:1234", address)
}
func TestIfFallbacksToLoopbackIfUnableToDeterminePublicIP(t *testing.T) {
address := HealthCheckAddress(1234)
assert.Equal(t, "127.0.0.1:1234", address)
}
func buildHTTPCheck(scheme string, port uint32, path string, timeoutSeconds float64) mesos.HealthCheck {
return mesos.HealthCheck{
HTTP: &mesos.HealthCheck_HTTPCheckInfo{
Path: &path,
Port: port,
Scheme: &scheme,
},
TimeoutSeconds: &timeoutSeconds,
}
}
func buildHTTPCheckForTestServer(ts *httptest.Server, timeoutSeconds float64, path string) mesos.HealthCheck {
testServerURL, _ := url.Parse(ts.URL)
testServerPort, _ := strconv.Atoi(testServerURL.Port())
return buildHTTPCheck(testServerURL.Scheme, uint32(testServerPort), path, timeoutSeconds)
}
func buildTCPCheckForTestServer(ts *httptest.Server, timeoutSeconds float64) mesos.HealthCheck {
testServerURL, _ := url.Parse(ts.URL)
testServerPort, _ := strconv.Atoi(testServerURL.Port())
return buildTCPCheck(uint32(testServerPort), timeoutSeconds)
}
func buildTCPCheck(port uint32, timeoutSeconds float64) mesos.HealthCheck {
return mesos.HealthCheck{
TCP: &mesos.HealthCheck_TCPCheckInfo{
Port: port,
},
TimeoutSeconds: &timeoutSeconds,
}
}