-
Notifications
You must be signed in to change notification settings - Fork 1.2k
/
Copy pathconfigmgr_test.go
483 lines (420 loc) · 18.4 KB
/
configmgr_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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
// Unless explicitly stated otherwise all files in this repository are licensed
// under the Apache License Version 2.0.
// This product includes software developed at Datadog (https://www.datadoghq.com/).
// Copyright 2016-present Datadog, Inc.
package autodiscovery
import (
"context"
"fmt"
"math/rand"
"strings"
"testing"
"github.com/mohae/deepcopy"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"github.com/DataDog/datadog-agent/pkg/autodiscovery/integration"
"github.com/DataDog/datadog-agent/pkg/autodiscovery/listeners"
"github.com/DataDog/datadog-agent/pkg/util/testutil"
)
// assertConfigsMatch verifies that the given slice of changes has exactly
// one match to each of the given functions, regardless of order.
func assertConfigsMatch(t *testing.T, configs []integration.Config, matches ...func(integration.Config) bool) {
matchCount := make([]int, len(matches))
for _, config := range configs {
configMatched := false
for i, f := range matches {
if f(config) {
matchCount[i]++
configMatched = true
}
}
if !configMatched {
t.Errorf("Config %#v did not match any of matches", config)
}
}
for i, count := range matchCount {
if count != 1 {
t.Errorf("matches[%d] matched %d times", i, count)
}
}
}
// assertLoadedConfigsMatch asserts that the set of loaded configs on the given
// configManager matches the given functions.
func assertLoadedConfigsMatch(t *testing.T, cm configManager, matches ...func(integration.Config) bool) {
var configs []integration.Config
cm.mapOverLoadedConfigs(func(loaded map[string]integration.Config) {
for _, cfg := range loaded {
configs = append(configs, cfg)
}
})
assertConfigsMatch(t, configs, matches...)
}
// matchAll matches when all of the given functions match
func matchAll(matches ...func(integration.Config) bool) func(integration.Config) bool {
return func(config integration.Config) bool {
for _, f := range matches {
if !f(config) {
return false
}
}
return true
}
}
// matchName matches config.Name
func matchName(name string) func(integration.Config) bool {
return func(config integration.Config) bool {
return config.Name == name
}
}
func matchDigest(digest string) func(integration.Config) bool {
return func(config integration.Config) bool {
return config.Digest() == digest
}
}
// matchLogsConfig matches config.LogsConfig (for verifying templates are applied)
func matchLogsConfig(logsConfig string) func(integration.Config) bool {
return func(config integration.Config) bool {
return string(config.LogsConfig) == logsConfig
}
}
// matchLogsConfig matches config.LogsConfig (for verifying templates are applied)
func matchSvc(serviceID string) func(integration.Config) bool {
return func(config integration.Config) bool {
return config.ServiceID == serviceID
}
}
var (
nonTemplateConfig = integration.Config{Name: "non-template"}
nonTemplateConfigWithSecrets = integration.Config{Name: "non-template-with-secrets", Instances: []integration.Data{integration.Data("foo: ENC[bar]")}}
templateConfig = integration.Config{Name: "template", LogsConfig: []byte("source: %%host%%"), ADIdentifiers: []string{"my-service"}}
myService = &dummyService{ID: "my-service", ADIdentifiers: []string{"my-service"}, Hosts: map[string]string{"main": "myhost"}}
)
type ConfigManagerSuite struct {
suite.Suite
factory func() configManager
cm configManager
}
func (suite *ConfigManagerSuite) SetupTest() {
suite.cm = suite.factory()
}
// A new, non-template config is scheduled immediately and unscheduled when
// deleted
func (suite *ConfigManagerSuite) TestNewNonTemplateScheduled() {
changes := suite.cm.processNewConfig(nonTemplateConfig)
assertConfigsMatch(suite.T(), changes.Schedule, matchName("non-template"))
assertConfigsMatch(suite.T(), changes.Unschedule)
changes = suite.cm.processDelConfigs([]integration.Config{nonTemplateConfig})
assertConfigsMatch(suite.T(), changes.Schedule)
assertConfigsMatch(suite.T(), changes.Unschedule, matchName("non-template"))
}
// A new, non-template config with secrets is scheduled immediately and unscheduled when
// deleted
func (suite *ConfigManagerSuite) TestNewNonTemplateWithSecretsScheduled() {
mockDecrypt := MockSecretDecrypt{suite.T(), []mockSecretScenario{
{
expectedData: []byte("foo: ENC[bar]"),
expectedOrigin: nonTemplateConfigWithSecrets.Name,
returnedData: []byte("foo: barDecoded"),
returnedError: nil,
},
{
expectedData: []byte{},
expectedOrigin: nonTemplateConfigWithSecrets.Name,
returnedData: []byte{},
returnedError: nil,
},
}}
defer mockDecrypt.install()()
inputNewConfig := deepcopy.Copy(nonTemplateConfigWithSecrets).(integration.Config)
changes := suite.cm.processNewConfig(inputNewConfig)
assertConfigsMatch(suite.T(), changes.Schedule, matchName(nonTemplateConfigWithSecrets.Name))
assertConfigsMatch(suite.T(), changes.Unschedule)
// Verify content is actually decoded
require.True(suite.T(), strings.Contains(string(changes.Schedule[0].Instances[0]), "barDecoded"))
newConfigDigest := changes.Schedule[0].Digest()
inputDelConfig := deepcopy.Copy(nonTemplateConfigWithSecrets).(integration.Config)
changes = suite.cm.processDelConfigs([]integration.Config{inputDelConfig})
assertConfigsMatch(suite.T(), changes.Schedule)
assertConfigsMatch(suite.T(), changes.Unschedule, matchName(nonTemplateConfigWithSecrets.Name))
assertConfigsMatch(suite.T(), changes.Unschedule, matchDigest(newConfigDigest))
require.True(suite.T(), strings.Contains(string(changes.Unschedule[0].Instances[0]), "barDecoded"))
}
// A new template config is not scheduled when there is no matching service, and
// not unscheduled when removed
func (suite *ConfigManagerSuite) TestNewTemplateNotScheduled() {
changes := suite.cm.processNewConfig(templateConfig)
assertConfigsMatch(suite.T(), changes.Schedule)
assertConfigsMatch(suite.T(), changes.Unschedule)
changes = suite.cm.processDelConfigs([]integration.Config{templateConfig})
assertConfigsMatch(suite.T(), changes.Schedule)
assertConfigsMatch(suite.T(), changes.Unschedule)
}
// A new template config is not scheduled when there is no matching service, but
// is resolved and scheduled when such a service arrives; deleting the config
// unschedules the resolved configs.
func (suite *ConfigManagerSuite) TestNewTemplateBeforeService_ConfigRemovedFirst() {
changes := suite.cm.processNewConfig(templateConfig)
assertConfigsMatch(suite.T(), changes.Schedule)
assertConfigsMatch(suite.T(), changes.Unschedule)
changes = suite.cm.processNewService(myService.ADIdentifiers, myService)
assertConfigsMatch(suite.T(), changes.Schedule, matchAll(matchName("template"), matchLogsConfig("source: myhost\n")))
assertConfigsMatch(suite.T(), changes.Unschedule)
changes = suite.cm.processDelConfigs([]integration.Config{templateConfig})
assertConfigsMatch(suite.T(), changes.Schedule)
assertConfigsMatch(suite.T(), changes.Unschedule, matchAll(matchName("template"), matchLogsConfig("source: myhost\n")))
changes = suite.cm.processDelService(context.TODO(), myService)
assertConfigsMatch(suite.T(), changes.Schedule)
assertConfigsMatch(suite.T(), changes.Unschedule)
}
// A new template config is not scheduled when there is no matching service, but
// is resolved and scheduled when such a service arrives; deleting the service
// unschedules the resolved configs.
func (suite *ConfigManagerSuite) TestNewTemplateBeforeService_ServiceRemovedFirst() {
changes := suite.cm.processNewConfig(templateConfig)
assertConfigsMatch(suite.T(), changes.Schedule)
assertConfigsMatch(suite.T(), changes.Unschedule)
changes = suite.cm.processNewService(myService.ADIdentifiers, myService)
assertConfigsMatch(suite.T(), changes.Schedule, matchAll(matchName("template"), matchLogsConfig("source: myhost\n")))
assertConfigsMatch(suite.T(), changes.Unschedule)
changes = suite.cm.processDelService(context.TODO(), myService)
assertConfigsMatch(suite.T(), changes.Schedule)
assertConfigsMatch(suite.T(), changes.Unschedule, matchAll(matchName("template"), matchLogsConfig("source: myhost\n")))
changes = suite.cm.processDelConfigs([]integration.Config{templateConfig})
assertConfigsMatch(suite.T(), changes.Schedule)
assertConfigsMatch(suite.T(), changes.Unschedule)
}
// A new service is not scheduled when there is no matching template, but
// is resolved and scheduled when such a template arrives; deleting the template
// unschedules the resolved configs.
func (suite *ConfigManagerSuite) TestNewServiceBeforeTemplate_ConfigRemovedFirst() {
changes := suite.cm.processNewService(myService.ADIdentifiers, myService)
assertConfigsMatch(suite.T(), changes.Schedule)
assertConfigsMatch(suite.T(), changes.Unschedule)
changes = suite.cm.processNewConfig(templateConfig)
assertConfigsMatch(suite.T(), changes.Schedule, matchAll(matchName("template"), matchLogsConfig("source: myhost\n")))
assertConfigsMatch(suite.T(), changes.Unschedule)
changes = suite.cm.processDelConfigs([]integration.Config{templateConfig})
assertConfigsMatch(suite.T(), changes.Schedule)
assertConfigsMatch(suite.T(), changes.Unschedule, matchAll(matchName("template"), matchLogsConfig("source: myhost\n")))
changes = suite.cm.processDelService(context.TODO(), myService)
assertConfigsMatch(suite.T(), changes.Schedule)
assertConfigsMatch(suite.T(), changes.Unschedule)
}
// A new service is not scheduled when there is no matching template, but
// is resolved and scheduled when such a template arrives; deleting the service
// unschedules the resolved configs.
func (suite *ConfigManagerSuite) TestNewServiceBeforeTemplate_ServiceRemovedFirst() {
changes := suite.cm.processNewService(myService.ADIdentifiers, myService)
assertConfigsMatch(suite.T(), changes.Schedule)
assertConfigsMatch(suite.T(), changes.Unschedule)
changes = suite.cm.processNewConfig(templateConfig)
assertConfigsMatch(suite.T(), changes.Schedule, matchAll(matchName("template"), matchLogsConfig("source: myhost\n")))
assertConfigsMatch(suite.T(), changes.Unschedule)
changes = suite.cm.processDelService(context.TODO(), myService)
assertConfigsMatch(suite.T(), changes.Schedule)
assertConfigsMatch(suite.T(), changes.Unschedule, matchAll(matchName("template"), matchLogsConfig("source: myhost\n")))
changes = suite.cm.processDelConfigs([]integration.Config{templateConfig})
assertConfigsMatch(suite.T(), changes.Schedule)
assertConfigsMatch(suite.T(), changes.Unschedule)
}
// Fuzz the config manager to ensure it doesn't "leak" configs -- that schedule
// and unschedule calls are always properly paired.
func (suite *ConfigManagerSuite) TestFuzz() {
testutil.Fuzz(suite.T(), func(seed int64) {
fmt.Printf("==== starting fuzz with random seed %d\n", seed)
cm := suite.factory()
r := rand.New(rand.NewSource(seed))
scheduled := map[string]struct{}{} // currently-scheduled config digests
// apply the given changes, checking for double-schedules and double-unschedules
applyChanges := func(changes integration.ConfigChanges) {
for _, cfg := range changes.Unschedule {
digest := cfg.Digest()
fmt.Printf("unschedule config %s -- Name: %#v, ADIdentifiers: [%s], ServiceID: %#v\n",
digest, cfg.Name, strings.Join(cfg.ADIdentifiers, ", "), cfg.ServiceID)
if _, found := scheduled[digest]; !found {
suite.T().Fatalf("config is not scheduled")
}
delete(scheduled, digest)
}
for _, cfg := range changes.Schedule {
digest := cfg.Digest()
fmt.Printf("schedule config %s -- Name: %#v, ADIdentifiers: [%s], ServiceID: %#v\n",
digest, cfg.Name, strings.Join(cfg.ADIdentifiers, ", "), cfg.ServiceID)
if _, found := scheduled[digest]; found {
suite.T().Fatalf("config is already scheduled")
}
scheduled[digest] = struct{}{}
}
}
// generate a random string with the given prefix, with N possible outcomes
randStr := func(pfx string, n int, r *rand.Rand) string {
return fmt.Sprintf("%s%d", pfx, r.Intn(n)+1)
}
// return an array of AD identifiers
randADIDs := func(r *rand.Rand) []string {
adIdentifiers := make([]string, r.Intn(5)+1)
for i := range adIdentifiers {
adIdentifiers[i] = randStr("ad", 50, r)
}
return adIdentifiers
}
// make a random non-template config
makeNonTemplateConfig := func(r *rand.Rand) integration.Config {
return integration.Config{Name: randStr("cfg", 10, r)}
}
// make a random template config
makeTemplateConfig := func(r *rand.Rand) integration.Config {
return integration.Config{Name: randStr("tpl", 15, r), ADIdentifiers: randADIDs(r)}
}
// make a random service
makeService := func(r *rand.Rand) listeners.Service {
return &dummyService{ID: randStr("svc", 15, r), ADIdentifiers: randADIDs(r)}
}
op := 0
removeAfterOps := 10
configs := map[string]integration.Config{}
services := map[string]listeners.Service{}
for {
p := r.Intn(90)
switch {
case p < 20 && op < removeAfterOps: // add service
svc := makeService(r)
id := svc.GetServiceID()
adIDs, _ := svc.GetADIdentifiers(context.Background())
if _, found := services[id]; !found {
services[id] = svc
fmt.Printf("add service %s with AD idents [%s]\n", id, strings.Join(adIDs, ", "))
applyChanges(cm.processNewService(adIDs, svc))
}
case p < 40 && op < removeAfterOps: // add non-template config
cfg := makeNonTemplateConfig(r)
digest := cfg.Digest()
if _, found := configs[digest]; !found {
configs[digest] = cfg
fmt.Printf("add non-template config %s (digest %s)\n", cfg.Name, digest)
applyChanges(cm.processNewConfig(cfg))
}
case p < 60 && op < removeAfterOps: // add template config
cfg := makeTemplateConfig(r)
digest := cfg.Digest()
if _, found := configs[digest]; !found {
configs[digest] = cfg
fmt.Printf("add template config %s (digest %s) with AD idents [%s]\n",
cfg.Name, digest, strings.Join(cfg.ADIdentifiers, ", "))
applyChanges(cm.processNewConfig(cfg))
}
case p < 70 && len(services) > 0: // remove service
i := rand.Intn(len(services))
for id, svc := range services {
if i == 0 {
delete(services, id)
adIDs, _ := svc.GetADIdentifiers(context.Background())
fmt.Printf("remove service %s with AD idents %s\n", id, strings.Join(adIDs, ", "))
applyChanges(cm.processDelService(context.TODO(), svc))
break
}
i--
}
case p < 90 && len(configs) > 0: // remove config
i := rand.Intn(len(configs))
for digest, cfg := range configs {
if i == 0 {
delete(configs, digest)
if len(cfg.ADIdentifiers) > 0 {
fmt.Printf("remove template config %s (digest %s) with AD idents [%s]\n",
cfg.Name, digest, strings.Join(cfg.ADIdentifiers, ", "))
} else {
fmt.Printf("remove non-template config %s (digest %s)\n", cfg.Name, digest)
}
applyChanges(cm.processDelConfigs([]integration.Config{cfg}))
break
}
i--
}
}
// verify that the loaded configs are correct
cm.mapOverLoadedConfigs(func(loaded map[string]integration.Config) {
failed := false
for digest := range scheduled {
if _, found := loaded[digest]; !found {
fmt.Printf("config with digest %s is not scheduled and should be", digest)
failed = true
}
}
for digest := range loaded {
if _, found := scheduled[digest]; !found {
fmt.Printf("config with digest %s is scheduled and should not be", digest)
failed = true
}
}
if failed {
suite.T().Fatalf("mapOverLoadedConfigs returned unexpected set of configs")
}
})
op++
if op > removeAfterOps && len(services) == 0 && len(configs) == 0 {
break
}
}
require.Empty(suite.T(), scheduled, "configs remain scheduled after everything was removed")
})
}
func TestSimpleConfigManagement(t *testing.T) {
suite.Run(t, &ConfigManagerSuite{factory: newSimpleConfigManager})
}
type ReconcilingConfigManagerSuite struct {
ConfigManagerSuite // include all ConfigManager tests, and more..
}
// A service's filtering determines which templates are resolved and scheduled.
func (suite *ReconcilingConfigManagerSuite) TestServiceTemplateFiltering() {
filterSvc := &dummyService{ID: "filter", ADIdentifiers: []string{"filter"}}
filterSvc.filterTemplates = func(configs map[string]integration.Config) {
for digest, config := range configs {
if !strings.HasSuffix(config.Name, "-keep") {
delete(configs, digest)
}
}
}
// adding service with no templates has no effect
changes := suite.cm.processNewService(myService.ADIdentifiers, myService)
assertConfigsMatch(suite.T(), changes.Schedule)
assertConfigsMatch(suite.T(), changes.Unschedule)
assertLoadedConfigsMatch(suite.T(), suite.cm)
// adding service with no templates has no effect
changes = suite.cm.processNewService(filterSvc.ADIdentifiers, filterSvc)
assertConfigsMatch(suite.T(), changes.Schedule)
assertConfigsMatch(suite.T(), changes.Unschedule)
assertLoadedConfigsMatch(suite.T(), suite.cm)
// adding a template that does not end in -keep only matches my-service
cfg1 := integration.Config{Name: "cfg1", ADIdentifiers: []string{"my-service", "filter"}}
changes = suite.cm.processNewConfig(cfg1)
assertConfigsMatch(suite.T(), changes.Schedule, matchAll(matchName("cfg1"), matchSvc("my-service")))
assertConfigsMatch(suite.T(), changes.Unschedule)
assertLoadedConfigsMatch(suite.T(), suite.cm, matchAll(matchName("cfg1"), matchSvc("my-service")))
// adding a template that ends in -keep matches both services
cfg2 := integration.Config{Name: "cfg2-keep", ADIdentifiers: []string{"my-service", "filter"}}
changes = suite.cm.processNewConfig(cfg2)
assertConfigsMatch(suite.T(), changes.Schedule,
matchAll(matchName("cfg2-keep"), matchSvc("my-service")),
matchAll(matchName("cfg2-keep"), matchSvc("filter")),
)
assertConfigsMatch(suite.T(), changes.Unschedule)
assertLoadedConfigsMatch(suite.T(), suite.cm,
matchAll(matchName("cfg1"), matchSvc("my-service")),
matchAll(matchName("cfg2-keep"), matchSvc("my-service")),
matchAll(matchName("cfg2-keep"), matchSvc("filter")),
)
// removing a service removes only the scheduled configs
changes = suite.cm.processDelService(context.TODO(), filterSvc)
assertConfigsMatch(suite.T(), changes.Schedule)
assertConfigsMatch(suite.T(), changes.Unschedule, matchAll(matchName("cfg2-keep"), matchSvc("filter")))
assertLoadedConfigsMatch(suite.T(), suite.cm,
matchAll(matchName("cfg1"), matchSvc("my-service")),
matchAll(matchName("cfg2-keep"), matchSvc("my-service")),
)
}
func TestReconcilingConfigManagement(t *testing.T) {
suite.Run(t, &ReconcilingConfigManagerSuite{
ConfigManagerSuite{factory: newReconcilingConfigManager},
})
}