forked from hashicorp/consul-esm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
agent_test.go
429 lines (382 loc) · 11.1 KB
/
agent_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
424
425
426
427
428
429
package main
import (
"fmt"
"net/http"
"net/http/httptest"
"reflect"
"strings"
"testing"
"time"
"github.com/hashicorp/go-hclog"
"github.com/hashicorp/consul/sdk/testutil/retry"
)
func testAgent(t *testing.T, cb func(*Config)) *Agent {
logger := hclog.New(&hclog.LoggerOptions{
Name: "consul-esm",
Level: hclog.LevelFromString("INFO"),
IncludeLocation: true,
Output: LOGOUT,
})
conf, err := DefaultConfig()
if err != nil {
t.Fatal(err)
}
conf.CoordinateUpdateInterval = 200 * time.Millisecond
if cb != nil {
cb(conf)
}
agent, err := NewAgent(conf, logger)
if err != nil {
t.Fatal(err)
}
go func() {
if err := agent.Run(); err != nil {
panic(err)
}
}()
return agent
}
func TestAgent_registerServiceAndCheck(t *testing.T) {
t.Parallel()
s, err := NewTestServer(t)
if err != nil {
t.Fatal(err)
}
defer s.Stop()
agent := testAgent(t, func(c *Config) {
c.HTTPAddr = s.HTTPAddr
c.Tag = "test"
})
defer agent.Shutdown()
// Lower these retry intervals
serviceID := fmt.Sprintf("%s:%s", agent.config.Service, agent.id)
// Make sure the ESM service and TTL check are registered
ensureRegistered := func(r *retry.R) {
services, _, err := agent.client.Catalog().Service(agent.config.Service, "", nil)
if err != nil {
r.Fatal(err)
}
if len(services) != 1 {
r.Fatalf("bad: %v", services)
}
if got, want := services[0].ServiceID, serviceID; got != want {
r.Fatalf("got %q, want %q", got, want)
}
if got, want := services[0].ServiceName, agent.config.Service; got != want {
r.Fatalf("got %q, want %q", got, want)
}
if got, want := services[0].ServiceTags, []string{"test"}; !reflect.DeepEqual(got, want) {
r.Fatalf("got %q, want %q", got, want)
}
if got, want := services[0].ServiceMeta, map[string]string{"external-source": "consul-esm"}; !reflect.DeepEqual(got, want) {
r.Fatalf("got %q, want %q", got, want)
}
checks, _, err := agent.client.Health().Checks(agent.config.Service, nil)
if err != nil {
r.Fatal(err)
}
if len(checks) != 1 {
r.Fatalf("bad: %v", checks)
}
if got, want := checks[0].CheckID, fmt.Sprintf("%s:%s:agent-ttl", agent.config.Service, agent.id); got != want {
r.Fatalf("got %q, want %q", got, want)
}
if got, want := checks[0].Name, "Consul External Service Monitor Alive"; got != want {
r.Fatalf("got %q, want %q", got, want)
}
if got, want := checks[0].Status, "passing"; got != want {
r.Fatalf("got %q, want %q", got, want)
}
if got, want := checks[0].ServiceID, fmt.Sprintf("%s:%s", agent.config.Service, agent.id); got != want {
r.Fatalf("got %q, want %q", got, want)
}
if got, want := checks[0].ServiceName, agent.config.Service; got != want {
r.Fatalf("got %q, want %q", got, want)
}
}
retry.Run(t, ensureRegistered)
// Deregister the service
go func() {
time.Sleep(time.Second)
if err := agent.client.Agent().ServiceDeregister(serviceID); err != nil {
t.Fatal(err)
}
}()
// Make sure the service and check are deregistered
ensureDeregistered := func(r *retry.R) {
services, _, err := agent.client.Catalog().Service(agent.config.Service, "", nil)
if err != nil {
r.Fatal(err)
}
if len(services) != 0 {
r.Fatalf("bad: %v", services[0])
}
checks, _, err := agent.client.Health().Checks(agent.config.Service, nil)
if err != nil {
r.Fatal(err)
}
if len(checks) != 0 {
r.Fatalf("bad: %v", checks)
}
}
retry.RunWith(&retry.Timer{Timeout: 10 * time.Second, Wait: 20 * time.Millisecond}, t, ensureDeregistered)
// Wait for the agent to re-register the service and TTL check
retry.Run(t, ensureRegistered)
// Stop the ESM agent
agent.Shutdown()
// Make sure the service and check are gone
retry.RunWith(&retry.Timer{Timeout: 15 * time.Second, Wait: 50 * time.Millisecond}, t, ensureDeregistered)
}
func TestAgent_shouldUpdateNodeStatus(t *testing.T) {
t.Parallel()
cases := []struct {
scenario string
node string
status string
nodeHealthRefreshInterval time.Duration
expected bool
}{
{
scenario: "Existing node, not expired status, same status: should not need to update",
node: "existing",
status: "healthy",
nodeHealthRefreshInterval: 1 * time.Hour,
expected: false,
},
{
scenario: "Existing node, expired status, same status: should need to update",
node: "existing",
status: "healthy",
nodeHealthRefreshInterval: 0 * time.Millisecond,
expected: true,
},
{
scenario: "Existing node, not expired status, different status: should need to update",
node: "existing",
status: "critical",
nodeHealthRefreshInterval: 1 * time.Hour,
expected: true,
},
{
scenario: "New node: should need to update",
node: "new node",
status: "critical",
nodeHealthRefreshInterval: 0 * time.Hour,
expected: true,
},
}
conf, err := DefaultConfig()
if err != nil {
t.Fatal(err)
}
for _, tc := range cases {
agent := Agent{
config: conf,
knownNodeStatuses: make(map[string]lastKnownStatus),
}
// set up an existing node and configs for testing
agent.updateLastKnownNodeStatus("existing", "healthy")
agent.config.NodeHealthRefreshInterval = tc.nodeHealthRefreshInterval
actual := agent.shouldUpdateNodeStatus(tc.node, tc.status)
if actual != tc.expected {
t.Fatalf("%s - expected need to update '%t', got '%t'", tc.scenario, tc.expected, actual)
}
}
}
func TestAgent_LastKnownStatusIsExpired(t *testing.T) {
t.Parallel()
cases := []struct {
scenario string
statusAge time.Duration
ttl time.Duration
expected bool
}{
{
scenario: "Last known time is within TTL",
statusAge: 5 * time.Minute,
ttl: 1 * time.Hour,
expected: false,
},
{
scenario: "Last known time is beyond of TTL",
statusAge: 1 * time.Hour,
ttl: 5 * time.Minute,
expected: true,
},
}
for _, tc := range cases {
lastKnown := lastKnownStatus{
status: "healthy",
time: time.Now().Add(-tc.statusAge),
}
actual := lastKnown.isExpired(tc.ttl, time.Now())
if actual != tc.expected {
t.Fatalf("%s - expected expired '%t', got '%t'", tc.scenario, tc.expected, actual)
}
}
}
func TestAgent_VerifyConsulCompatibility(t *testing.T) {
type testCase struct {
name, agentJSON, serviceJSON string
shouldPass bool
}
testVersion := func(t *testing.T, tc testCase) {
ts := httptest.NewServer(http.HandlerFunc(
func(w http.ResponseWriter, r *http.Request) {
uri := r.RequestURI
switch { // ignore anything that doesn't require a return body
case strings.Contains(uri, "status/leader"):
fmt.Fprint(w, `"127.0.0.1"`)
case strings.Contains(uri, "agent/self"):
fmt.Fprint(w, tc.agentJSON)
case strings.Contains(uri, "catalog/service"):
fmt.Fprint(w, tc.serviceJSON)
}
}))
defer ts.Close()
agent := testAgent(t, func(c *Config) {
c.HTTPAddr = ts.URL
c.InstanceID = "test-agent"
})
time.Sleep(time.Millisecond) // race with Run's go routines
defer agent.Shutdown()
err := agent.VerifyConsulCompatibility()
switch {
case tc.shouldPass && err != nil:
t.Fatalf("unexpected error: %s", err)
case !tc.shouldPass && err == nil:
t.Fatalf("should be an error and wasn't: %#v", tc)
}
}
testCases := []testCase{
{
name: "good",
agentJSON: `{"Config": {"Version": "1.10.0"}}`,
serviceJSON: `[{"ServiceMeta" : {"version": "1.10.0"}}]`,
shouldPass: true,
},
{
name: "bad-agent",
agentJSON: `{"Config": {"Version": "1.0.0"}}`,
serviceJSON: `[{"ServiceMeta" : {"version": "1.10.0"}}]`,
},
{
name: "bad-server",
agentJSON: `{"Config": {"Version": "1.10.0"}}`,
serviceJSON: `[{"ServiceMeta" : {"version": "1.0.0"}}]`,
},
{
name: "no-server-version-meta",
agentJSON: `{"Config": {"Version": "1.10.0"}}`,
serviceJSON: `[{"ServiceMeta" : {}}]`,
shouldPass: true, // logs a warning
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
testVersion(t, tc)
})
}
}
func TestAgent_uniqueInstanceID(t *testing.T) {
t.Parallel()
s, err := NewTestServer(t)
if err != nil {
t.Fatal(err)
}
defer s.Stop()
// Register first ESM instance
agent1 := testAgent(t, func(c *Config) {
c.HTTPAddr = s.HTTPAddr
c.InstanceID = "unique-instance-id-1"
})
defer agent1.Shutdown()
// Make sure the first ESM service is registered
retry.Run(t, func(r *retry.R) {
services, _, err := agent1.client.Catalog().Service(agent1.config.Service, "", nil)
if err != nil {
r.Fatal(err)
}
if len(services) != 1 {
r.Fatalf("1 service should be registered: %v", services)
}
if got, want := services[0].ServiceID, agent1.serviceID(); got != want {
r.Fatalf("got service id %q, want %q", got, want)
}
})
// Register second ESM instance
agent2 := testAgent(t, func(c *Config) {
c.HTTPAddr = s.HTTPAddr
c.InstanceID = "unique-instance-id-2"
})
defer agent2.Shutdown()
// Make sure second ESM service is registered
retry.Run(t, func(r *retry.R) {
services, _, err := agent2.client.Catalog().Service(agent2.config.Service, "", nil)
if err != nil {
r.Fatal(err)
}
if len(services) != 2 {
r.Fatalf("2 service should be registered, got: %v", services)
}
if got, want := services[1].ServiceID, agent2.serviceID(); got != want {
r.Fatalf("got service id %q, want %q", got, want)
}
})
}
func TestAgent_notUniqueInstanceIDFails(t *testing.T) {
t.Parallel()
notUniqueInstanceID := "not-unique-instance-id"
s, err := NewTestServer(t)
if err != nil {
t.Fatal(err)
}
defer s.Stop()
// Register first ESM instance
agent := testAgent(t, func(c *Config) {
c.HTTPAddr = s.HTTPAddr
c.InstanceID = notUniqueInstanceID
})
defer agent.Shutdown()
// Make sure the ESM service is registered
ensureRegistered := func(r *retry.R) {
services, _, err := agent.client.Catalog().Service(agent.config.Service, "", nil)
if err != nil {
r.Fatal(err)
}
if len(services) != 1 {
r.Fatalf("1 service should be registered: %v", services)
}
if got, want := services[0].ServiceID, agent.serviceID(); got != want {
r.Fatalf("got service id %q, want %q", got, want)
}
}
retry.Run(t, ensureRegistered)
// Create second ESM service with same instance ID
logger := hclog.New(&hclog.LoggerOptions{
Name: "consul-esm",
Level: hclog.LevelFromString("INFO"),
IncludeLocation: true,
Output: LOGOUT,
})
conf, err := DefaultConfig()
if err != nil {
t.Fatal(err)
}
conf.InstanceID = notUniqueInstanceID
conf.HTTPAddr = s.HTTPAddr
duplicateAgent, err := NewAgent(conf, logger)
if err != nil {
t.Fatal(err)
}
err = duplicateAgent.Run()
defer duplicateAgent.Shutdown()
if err == nil {
t.Fatal("Failed to error when registering ESM service with same instance ID")
}
switch e := err.(type) {
case *alreadyExistsError:
default:
t.Fatalf("Unexpected error type. Wanted an alreadyExistsError type. Error: '%v'", e)
}
}