forked from hashicorp/consul-esm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_test.go
423 lines (361 loc) · 12.7 KB
/
check_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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
package main
import (
"crypto/tls"
"testing"
"time"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/consul/api"
"github.com/hashicorp/consul/sdk/testutil/retry"
"github.com/stretchr/testify/assert"
)
func TestCheck_HTTP(t *testing.T) {
t.Parallel()
s, err := NewTestServer(t)
if err != nil {
t.Fatal(err)
}
defer s.Stop()
client, err := api.NewClient(&api.Config{Address: s.HTTPAddr})
if err != nil {
t.Fatal(err)
}
logger := hclog.New(&hclog.LoggerOptions{
Name: "consul-esm",
Level: hclog.LevelFromString("INFO"),
IncludeLocation: true,
Output: LOGOUT,
})
runner := NewCheckRunner(logger, client, 0, 0, &tls.Config{}, 1, 1)
defer runner.Stop()
// Register an external node with an initially critical http check.
nodeMeta := map[string]string{"external-node": "true"}
nodeRegistration := &api.CatalogRegistration{
Node: "external",
Address: "service.local",
Datacenter: "dc1",
NodeMeta: nodeMeta,
Check: &api.AgentCheck{
Node: "external",
CheckID: "ext-http",
Name: "http-test",
Status: api.HealthCritical,
Definition: api.HealthCheckDefinition{
HTTP: "http://" + s.HTTPAddr + "/v1/status/leader",
IntervalDuration: 50 * time.Millisecond,
},
},
}
_, err = client.Catalog().Register(nodeRegistration, nil)
if err != nil {
t.Fatal(err)
}
checks, _, err := client.Health().State(api.HealthAny, &api.QueryOptions{NodeMeta: nodeMeta})
if err != nil {
t.Fatal(err)
}
runner.UpdateChecks(checks)
// Make sure the health has been updated to passing
retry.Run(t, func(r *retry.R) {
checks, _, err = client.Health().State(api.HealthAny, &api.QueryOptions{NodeMeta: nodeMeta})
if len(checks) != 1 || checks[0].Status != api.HealthPassing {
r.Fatalf("expected: %v, got: %v", api.HealthPassing, checks[0].Status)
}
})
// Re-register the check as critical initially
// The catalog should eventually show the check as passing
nodeRegistration.SkipNodeUpdate = true
nodeRegistration.Check.Status = api.HealthCritical
_, err = client.Catalog().Register(nodeRegistration, nil)
if err != nil {
t.Fatal(err)
}
checks, _, err = client.Health().State(api.HealthAny, &api.QueryOptions{NodeMeta: nodeMeta})
if err != nil {
t.Fatal(err)
}
runner.UpdateChecks(checks)
retry.Run(t, func(r *retry.R) {
checks, _, err = client.Health().State(api.HealthAny, &api.QueryOptions{NodeMeta: nodeMeta})
if len(checks) != 1 || checks[0].Status != api.HealthPassing {
r.Fatalf("expected: %v, got: %v", api.HealthPassing, checks[0].Status)
}
})
// Update the check definition with an invalid http endpoint.
nodeRegistration.Check.Definition.HTTP = "http://" + s.HTTPAddr + "/v1/nope"
nodeRegistration.Check.Status = api.HealthPassing
_, err = client.Catalog().Register(nodeRegistration, nil)
if err != nil {
t.Fatal(err)
}
checks, _, err = client.Health().State(api.HealthAny, &api.QueryOptions{NodeMeta: nodeMeta})
if err != nil {
t.Fatal(err)
}
runner.UpdateChecks(checks)
// Wait for the health check to fail.
retry.Run(t, func(r *retry.R) {
checks, _, err = client.Health().State(api.HealthAny, &api.QueryOptions{NodeMeta: nodeMeta})
if len(checks) != 1 || checks[0].Status != api.HealthCritical {
r.Fatalf("expected: %v, got: %v", api.HealthCritical, checks[0].Status)
}
})
}
func TestCheck_TCP(t *testing.T) {
t.Parallel()
s, err := NewTestServer(t)
if err != nil {
t.Fatal(err)
}
defer s.Stop()
client, err := api.NewClient(&api.Config{Address: s.HTTPAddr})
if err != nil {
t.Fatal(err)
}
logger := hclog.New(&hclog.LoggerOptions{
Name: "consul-esm",
Level: hclog.LevelFromString("INFO"),
IncludeLocation: true,
Output: LOGOUT,
})
runner := NewCheckRunner(logger, client, 0, 0, &tls.Config{}, 1, 1)
defer runner.Stop()
// Register an external node with an initially critical http check
nodeMeta := map[string]string{"external-node": "true"}
nodeRegistration := &api.CatalogRegistration{
Node: "external",
Address: "service.local",
Datacenter: "dc1",
NodeMeta: nodeMeta,
Check: &api.AgentCheck{
Node: "external",
CheckID: "ext-tcp",
Name: "tcp-test",
Status: api.HealthCritical,
Definition: api.HealthCheckDefinition{
TCP: s.HTTPAddr,
IntervalDuration: 50 * time.Millisecond,
},
},
}
_, err = client.Catalog().Register(nodeRegistration, nil)
if err != nil {
t.Fatal(err)
}
checks, _, err := client.Health().State(api.HealthAny, &api.QueryOptions{NodeMeta: nodeMeta})
if err != nil {
t.Fatal(err)
}
runner.UpdateChecks(checks)
// Make sure the health has been updated to passing
retry.Run(t, func(r *retry.R) {
checks, _, err = client.Health().State(api.HealthAny, &api.QueryOptions{NodeMeta: nodeMeta})
if err != nil {
r.Fatal(err)
}
if len(checks) != 1 || checks[0].Status != api.HealthPassing {
r.Fatalf("expected: %v, got: %v", api.HealthPassing, checks[0].Status)
}
})
// Re-register the check as critical initially
// The catalog should eventually show the check as passing
nodeRegistration.SkipNodeUpdate = true
nodeRegistration.Check.Status = api.HealthCritical
_, err = client.Catalog().Register(nodeRegistration, nil)
if err != nil {
t.Fatal(err)
}
checks, _, err = client.Health().State(api.HealthAny, &api.QueryOptions{NodeMeta: nodeMeta})
if err != nil {
t.Fatal(err)
}
runner.UpdateChecks(checks)
// Make sure the health has been updated to passing
retry.Run(t, func(r *retry.R) {
checks, _, err = client.Health().State(api.HealthAny, &api.QueryOptions{NodeMeta: nodeMeta})
if err != nil {
r.Fatal(err)
}
if len(checks) != 1 || checks[0].Status != api.HealthPassing {
r.Fatalf("expected: %v, got: %v", api.HealthPassing, checks[0].Status)
}
})
// Update the check definition with an invalid http endpoint.
nodeRegistration.Check.Definition.TCP = "127.0.0.1:22222"
nodeRegistration.Check.Status = api.HealthPassing
_, err = client.Catalog().Register(nodeRegistration, nil)
if err != nil {
t.Fatal(err)
}
checks, _, err = client.Health().State(api.HealthAny, &api.QueryOptions{NodeMeta: nodeMeta})
if err != nil {
t.Fatal(err)
}
runner.UpdateChecks(checks)
// Wait for the health check to fail.
retry.Run(t, func(r *retry.R) {
checks, _, err = client.Health().State(api.HealthAny, &api.QueryOptions{NodeMeta: nodeMeta})
if len(checks) != 1 || checks[0].Status != api.HealthCritical {
r.Fatalf("expected: %v, got: %v", api.HealthCritical, checks[0].Status)
}
})
}
func TestCheck_MinimumInterval(t *testing.T) {
// Confirm that a check's interval is at least the minimum interval
t.Parallel()
s, err := NewTestServer(t)
if err != nil {
t.Fatal(err)
}
defer s.Stop()
client, err := api.NewClient(&api.Config{Address: s.HTTPAddr})
if err != nil {
t.Fatal(err)
}
logger := hclog.New(&hclog.LoggerOptions{
Name: "consul-esm",
Level: hclog.LevelFromString("INFO"),
IncludeLocation: true,
Output: LOGOUT,
})
minimumInterval := 2 * time.Second
runner := NewCheckRunner(logger, client, 0, minimumInterval, &tls.Config{}, 0, 0)
defer runner.Stop()
// Make a check with an interval that is below the minimum required interval
belowMinimumInterval := 1 * time.Second
check := &api.HealthCheck{
Node: "external",
CheckID: "below-minimum-interval",
Name: "below-minimum-interval-test",
Status: api.HealthCritical,
Definition: api.HealthCheckDefinition{
HTTP: "http://localhost:8080",
IntervalDuration: belowMinimumInterval,
},
}
// run check
checks := api.HealthChecks{check}
runner.UpdateChecks(checks)
// confirm that the original check's interval is unmodified
originalCheck, ok := runner.checks[checkHash(check)]
if !ok {
t.Fatalf("Check was not stored on runner.checks as expected. Checks: %v", runner.checks)
}
if originalCheck.Definition.IntervalDuration != belowMinimumInterval {
t.Fatalf("Unprocessed check's interval was %v but should have remained unchanged at %v", originalCheck.Definition.IntervalDuration, belowMinimumInterval)
}
// confirm that esm's modified version of check's interval is updated
esmCheck, ok := runner.checksHTTP[checkHash(check)]
if !ok {
t.Fatalf("HTTP check was not stored on runner.checksHTTP as expected. Checks: %v", runner.checksHTTP)
}
if esmCheck.Interval != minimumInterval {
t.Fatalf("Processed HTTP check's interval was %v but should have been updated to same as minimum interval %v", esmCheck.Interval, minimumInterval)
}
}
func TestCheck_NoFlapping(t *testing.T) {
// Confirm that the status flapping protections work
s, err := NewTestServer(t)
if err != nil {
t.Fatal(err)
}
defer s.Stop()
client, err := api.NewClient(&api.Config{Address: s.HTTPAddr})
if err != nil {
t.Fatal(err)
}
logger := hclog.New(&hclog.LoggerOptions{
Name: "consul-esm",
Level: hclog.LevelFromString("INFO"),
IncludeLocation: true,
Output: LOGOUT,
})
minimumInterval := 2 * time.Second
runner := NewCheckRunner(logger, client, 0, minimumInterval, &tls.Config{}, 2, 2)
defer runner.Stop()
// Register an external node with an initially critical http check.
nodeMeta := map[string]string{"external-node": "true"}
nodeRegistration := &api.CatalogRegistration{
Node: "external",
Address: "service.local",
Datacenter: "dc1",
NodeMeta: nodeMeta,
Check: &api.AgentCheck{
Node: "external",
CheckID: "ext-http",
Name: "http-test",
Status: api.HealthCritical,
Definition: api.HealthCheckDefinition{
HTTP: "http://" + s.HTTPAddr + "/v1/status/leader",
IntervalDuration: 50 * time.Millisecond,
},
},
}
_, err = client.Catalog().Register(nodeRegistration, nil)
if err != nil {
t.Fatal(err)
}
checks, _, err := client.Health().State(api.HealthAny, &api.QueryOptions{NodeMeta: nodeMeta})
if err != nil {
t.Fatal(err)
}
runner.UpdateChecks(checks)
id := checkHash(checks[0])
originalCheck, ok := runner.checks[id]
if !ok {
t.Fatalf("Check was not stored on runner.checks as expected. Checks: %v", runner.checks)
}
// test consecutive checks: when threshold is met, the status will toggle from
// critical => passing and counters will reset
assert.Equal(t, 0, originalCheck.failureCounter)
assert.Equal(t, 0, originalCheck.successCounter)
assert.Equal(t, api.HealthCritical, originalCheck.Status)
runner.UpdateCheck(id, api.HealthPassing, "")
assert.Equal(t, 0, originalCheck.failureCounter)
assert.Equal(t, 1, originalCheck.successCounter)
assert.Equal(t, api.HealthCritical, originalCheck.Status)
runner.UpdateCheck(id, api.HealthPassing, "")
assert.Equal(t, 0, originalCheck.failureCounter)
assert.Equal(t, 2, originalCheck.successCounter)
assert.Equal(t, api.HealthCritical, originalCheck.Status)
runner.UpdateCheck(id, api.HealthPassing, "")
assert.Equal(t, 0, originalCheck.failureCounter)
assert.Equal(t, 0, originalCheck.successCounter)
assert.Equal(t, api.HealthPassing, originalCheck.Status)
// test non-consecutive checks: non-consecutive will increment and
// decrement accordingly until threshold is crossed
runner.UpdateCheck(id, api.HealthCritical, "")
assert.Equal(t, 1, originalCheck.failureCounter)
assert.Equal(t, 0, originalCheck.successCounter)
assert.Equal(t, api.HealthPassing, originalCheck.Status)
runner.UpdateCheck(id, api.HealthCritical, "")
assert.Equal(t, 2, originalCheck.failureCounter)
assert.Equal(t, 0, originalCheck.successCounter)
assert.Equal(t, api.HealthPassing, originalCheck.Status)
runner.UpdateCheck(id, api.HealthPassing, "")
assert.Equal(t, 1, originalCheck.failureCounter)
assert.Equal(t, 0, originalCheck.successCounter)
assert.Equal(t, api.HealthPassing, originalCheck.Status)
runner.UpdateCheck(id, api.HealthCritical, "")
assert.Equal(t, 2, originalCheck.failureCounter)
assert.Equal(t, 0, originalCheck.successCounter)
assert.Equal(t, api.HealthPassing, originalCheck.Status)
runner.UpdateCheck(id, api.HealthCritical, "")
assert.Equal(t, 0, originalCheck.failureCounter)
assert.Equal(t, 0, originalCheck.successCounter)
assert.Equal(t, api.HealthCritical, originalCheck.Status)
// test if counter values are kept after calling
// UpdateChecks(), which is called everytime there is
// a change in the consul catalog
runner.UpdateCheck(id, api.HealthPassing, "")
assert.Equal(t, 0, originalCheck.failureCounter)
assert.Equal(t, 1, originalCheck.successCounter)
assert.Equal(t, api.HealthCritical, originalCheck.Status)
runner.UpdateChecks(checks)
currentCheck, ok := runner.checks[id]
if !ok {
t.Fatalf("Current check was not stored on runner.checks as expected. Checks: %v", runner.checks)
}
assert.Equal(t, 0, currentCheck.failureCounter)
assert.Equal(t, 1, currentCheck.successCounter)
assert.Equal(t, api.HealthCritical, currentCheck.Status)
}