-
Notifications
You must be signed in to change notification settings - Fork 0
/
configmanager_test.go
590 lines (559 loc) · 15.3 KB
/
configmanager_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
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
package configmanager_test
import (
"context"
"fmt"
"reflect"
"sort"
"testing"
"github.com/dnitsch/configmanager"
"github.com/dnitsch/configmanager/internal/config"
"github.com/dnitsch/configmanager/internal/testutils"
"github.com/dnitsch/configmanager/pkg/generator"
"github.com/go-test/deep"
)
type mockGenerator struct {
generate func(tokens []string) (generator.ParsedMap, error)
}
func (m *mockGenerator) Generate(tokens []string) (generator.ParsedMap, error) {
if m.generate != nil {
return m.generate(tokens)
}
pm := generator.ParsedMap{}
pm["FOO#/test"] = "val1"
pm["ANOTHER://bar/quz"] = "fux"
pm["ZODTHER://bar/quz"] = "xuf"
return pm, nil
}
func Test_Retrieve_from_token_list(t *testing.T) {
tests := map[string]struct {
tokens []string
genvar *mockGenerator
expectKey string
expectVal string
}{
"standard": {
tokens: []string{"FOO#/test", "ANOTHER://bar/quz", "ZODTHER://bar/quz"},
genvar: &mockGenerator{},
expectKey: "FOO#/test",
expectVal: "val1",
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
cm := configmanager.New(context.TODO())
cm.WithGenerator(tt.genvar)
pm, err := cm.Retrieve(tt.tokens)
if err != nil {
t.Errorf(testutils.TestPhrase, err, nil)
}
if val, found := pm[tt.expectKey]; found {
if val != pm[tt.expectKey] {
t.Errorf(testutils.TestPhrase, val, tt.expectVal)
}
} else {
t.Errorf(testutils.TestPhrase, "nil", tt.expectKey)
}
})
}
}
func Test_retrieveWithInputReplaced(t *testing.T) {
tests := map[string]struct {
name string
input string
genvar *mockGenerator
expect string
}{
"strYaml": {
input: `
space: preserved
indents: preserved
arr: [ "FOO#/test" ]
// comments preserved
arr:
- "FOO#/test"
- ANOTHER://bar/quz
`,
genvar: &mockGenerator{},
expect: `
space: preserved
indents: preserved
arr: [ "val1" ]
// comments preserved
arr:
- "val1"
- fux
`,
},
"strToml": {
input: `
// TOML
[[somestuff]]
key = "FOO#/test"
`,
genvar: &mockGenerator{},
expect: `
// TOML
[[somestuff]]
key = "val1"
`,
},
"strTomlWithoutQuotes": {
input: `
// TOML
[[somestuff]]
key = FOO#/test,FOO#/test-FOO#/test
key2 = FOO#/test
key3 = FOO#/test
key4 = FOO#/test
`,
genvar: &mockGenerator{},
expect: `
// TOML
[[somestuff]]
key = val1,val1-val1
key2 = val1
key3 = val1
key4 = val1
`,
},
"strTomlWithoutMultiline": {
input: `
export FOO='FOO#/test'
export FOO1=FOO#/test
export FOO2="FOO#/test"
export FOO3=FOO#/test
export FOO4=FOO#/test
[[section]]
foo23 = FOO#/test
`,
genvar: &mockGenerator{},
expect: `
export FOO='val1'
export FOO1=val1
export FOO2="val1"
export FOO3=val1
export FOO4=val1
[[section]]
foo23 = val1
`,
},
"escaped input": {
input: `"{\"patchPayloadTemplate\":\"{\\\"password\\\":\\\"FOO#/test\\\",\\\"passwordConfirm\\\":\\\"FOO#/test\\\"}\\n\"}"`,
genvar: &mockGenerator{},
expect: `"{\"patchPayloadTemplate\":\"{\\\"password\\\":\\\"val1\\\",\\\"passwordConfirm\\\":\\\"val1\\\"}\\n\"}"`,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
cm := configmanager.New(context.TODO())
cm.WithGenerator(tt.genvar)
got, err := cm.RetrieveWithInputReplaced(tt.input)
if err != nil {
t.Errorf("failed with %v", err)
}
if got != tt.expect {
t.Errorf(testutils.TestPhrase, got, tt.expect)
}
})
}
}
type testSimpleStruct struct {
Foo string `json:"foo" yaml:"foo"`
Bar string `json:"bar" yaml:"bar"`
}
type testAnotherNEst struct {
Number int `json:"number,omitempty" yaml:"number"`
Float float32 `json:"float,omitempty" yaml:"float"`
}
type testLol struct {
Bla string `json:"bla,omitempty" yaml:"bla"`
Another testAnotherNEst `json:"another,omitempty" yaml:"another"`
}
type testNestedStruct struct {
Foo string `json:"foo" yaml:"foo"`
Bar string `json:"bar" yaml:"bar"`
Lol testLol `json:"lol,omitempty" yaml:"lol"`
}
const (
testTokenAWS = "AWSSECRETS:///bar/foo"
)
var marshallTests = map[string]struct {
testType testNestedStruct
expect testNestedStruct
generator func(t *testing.T) *mockGenerator
}{
"happy path complex struct complete": {
testType: testNestedStruct{
Foo: testTokenAWS,
Bar: "quz",
Lol: testLol{
Bla: "booo",
Another: testAnotherNEst{
Number: 1235,
Float: 123.09,
},
},
},
expect: testNestedStruct{
Foo: "baz",
Bar: "quz",
Lol: testLol{
Bla: "booo",
Another: testAnotherNEst{
Number: 1235,
Float: 123.09,
},
},
},
generator: func(t *testing.T) *mockGenerator {
m := &mockGenerator{}
m.generate = func(tokens []string) (generator.ParsedMap, error) {
pm := make(generator.ParsedMap)
pm[testTokenAWS] = "baz"
return pm, nil
}
return m
},
},
"complex struct - missing fields": {
testType: testNestedStruct{
Foo: testTokenAWS,
Bar: "quz",
},
expect: testNestedStruct{
Foo: "baz",
Bar: "quz",
Lol: testLol{},
},
generator: func(t *testing.T) *mockGenerator {
m := &mockGenerator{}
m.generate = func(tokens []string) (generator.ParsedMap, error) {
pm := make(generator.ParsedMap)
pm[testTokenAWS] = "baz"
return pm, nil
}
return m
},
},
}
func Test_RetrieveMarshalledJson(t *testing.T) {
for name, tt := range marshallTests {
t.Run(name, func(t *testing.T) {
c := configmanager.New(context.TODO())
c.Config.WithTokenSeparator("://")
c.WithGenerator(tt.generator(t))
input := &tt.testType
err := c.RetrieveMarshalledJson(input)
MarhsalledHelper(t, err, input, &tt.expect)
})
}
}
func Test_YamlRetrieveMarshalled(t *testing.T) {
for name, tt := range marshallTests {
t.Run(name, func(t *testing.T) {
c := configmanager.New(context.TODO())
c.Config.WithTokenSeparator("://")
c.WithGenerator(tt.generator(t))
input := &tt.testType
err := c.RetrieveMarshalledYaml(input)
MarhsalledHelper(t, err, input, &tt.expect)
})
}
}
func MarhsalledHelper(t *testing.T, err error, input, expectOut any) {
t.Helper()
if err != nil {
t.Errorf(testutils.TestPhrase, err.Error(), nil)
}
if !reflect.DeepEqual(input, expectOut) {
t.Errorf(testutils.TestPhraseWithContext, "returned types do not deep equal", input, expectOut)
}
}
func Test_YamlRetrieveUnmarshalled(t *testing.T) {
ttests := map[string]struct {
input []byte
expect testNestedStruct
generator func(t *testing.T) *mockGenerator
}{
"happy path complex struct complete": {
input: []byte(`foo: AWSSECRETS:///bar/foo
bar: quz
lol:
bla: booo
another:
number: 1235
float: 123.09`),
expect: testNestedStruct{
Foo: "baz",
Bar: "quz",
Lol: testLol{
Bla: "booo",
Another: testAnotherNEst{
Number: 1235,
Float: 123.09,
},
},
},
generator: func(t *testing.T) *mockGenerator {
m := &mockGenerator{}
m.generate = func(tokens []string) (generator.ParsedMap, error) {
pm := make(generator.ParsedMap)
pm[testTokenAWS] = "baz"
return pm, nil
}
return m
},
},
"complex struct - missing fields": {
input: []byte(`foo: AWSSECRETS:///bar/foo
bar: quz`),
expect: testNestedStruct{
Foo: "baz",
Bar: "quz",
Lol: testLol{},
},
generator: func(t *testing.T) *mockGenerator {
m := &mockGenerator{}
m.generate = func(tokens []string) (generator.ParsedMap, error) {
pm := make(generator.ParsedMap)
pm[testTokenAWS] = "baz"
return pm, nil
}
return m
},
},
}
for name, tt := range ttests {
t.Run(name, func(t *testing.T) {
c := configmanager.New(context.TODO())
c.Config.WithTokenSeparator("://")
c.WithGenerator(tt.generator(t))
output := &testNestedStruct{}
err := c.RetrieveUnmarshalledFromYaml(tt.input, output)
MarhsalledHelper(t, err, output, &tt.expect)
})
}
}
func Test_JsonRetrieveUnmarshalled(t *testing.T) {
tests := map[string]struct {
input []byte
expect testNestedStruct
generator func(t *testing.T) *mockGenerator
}{
"happy path complex struct complete": {
input: []byte(`{"foo":"AWSSECRETS:///bar/foo","bar":"quz","lol":{"bla":"booo","another":{"number":1235,"float":123.09}}}`),
expect: testNestedStruct{
Foo: "baz",
Bar: "quz",
Lol: testLol{
Bla: "booo",
Another: testAnotherNEst{
Number: 1235,
Float: 123.09,
},
},
},
generator: func(t *testing.T) *mockGenerator {
m := &mockGenerator{}
m.generate = func(tokens []string) (generator.ParsedMap, error) {
pm := make(generator.ParsedMap)
pm[testTokenAWS] = "baz"
return pm, nil
}
return m
},
},
"complex struct - missing fields": {
input: []byte(`{"foo":"AWSSECRETS:///bar/foo","bar":"quz"}`),
expect: testNestedStruct{
Foo: "baz",
Bar: "quz",
Lol: testLol{},
},
generator: func(t *testing.T) *mockGenerator {
m := &mockGenerator{}
m.generate = func(tokens []string) (generator.ParsedMap, error) {
pm := make(generator.ParsedMap)
pm[testTokenAWS] = "baz"
return pm, nil
}
return m
},
},
}
for name, tt := range tests {
t.Run(name, func(t *testing.T) {
c := configmanager.New(context.TODO())
c.Config.WithTokenSeparator("://")
c.WithGenerator(tt.generator(t))
output := &testNestedStruct{}
err := c.RetrieveUnmarshalledFromJson(tt.input, output)
MarhsalledHelper(t, err, output, &tt.expect)
})
}
}
func TestFindTokens(t *testing.T) {
ttests := map[string]struct {
input string
expect []string
}{
"extract from text correctly": {
`Where does it come from?
Contrary to popular belief,
Lorem Ipsum is AWSPARAMSTR:///djsfsdkjvfjkhfdvibdfinjdsfnjvdsflj <= in middle of sentencenot simply random text.
It has roots in a piece of classical Latin literature from 45
BC, making it over 2000 years old. Richard McClintock, a Latin professor at
Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, c
onsectetur, from a Lorem Ipsum passage , at the end of line => AWSPARAMSTR:///djsfsdkjvfjkhfdvibdfinjdsfnjvdsflj
and going through the cites of the word in c
lassical literature, discovered the undoubtable source. Lorem Ipsum comes from secti
ons in singles =>'AWSPARAMSTR:///djsfsdkjvfjkhfdvibdfinjdsfnjvdsflj'1.10.32 and 1.10.33 of "de Finibus Bonorum et Malorum" (The Extremes of Good and Evil)
in doubles => "AWSPARAMSTR:///djsfsdkjvfjkhfdvibdfinjdsfnjvdsflj"
by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular
during the :=> embedded in text RenaissanceAWSPARAMSTR:///djsfsdkjvfjkhfdvibdfinjdsfnjvdsflj[] embedded in text <=:
The first line of Lorem Ipsum, "Lorem ipsum dolor sit amet..", comes from a line in section 1.10.32.`,
[]string{
"AWSPARAMSTR:///djsfsdkjvfjkhfdvibdfinjdsfnjvdsflj",
"AWSPARAMSTR:///djsfsdkjvfjkhfdvibdfinjdsfnjvdsflj",
"AWSPARAMSTR:///djsfsdkjvfjkhfdvibdfinjdsfnjvdsflj",
"AWSPARAMSTR:///djsfsdkjvfjkhfdvibdfinjdsfnjvdsflj",
"AWSPARAMSTR:///djsfsdkjvfjkhfdvibdfinjdsfnjvdsflj[]"},
},
"unknown implementation not picked up": {
`foo: AWSPARAMSTR:///djsfsdkjvfjkhfdvibdfinjdsfnjvdsflj
bar: AWSPARAMSTR#bar/djsfsdkjvfjkhfdvibdfinjdsfnjvdsflj[version:123]
unknown: GCPPARAMSTR:///djsfsdkjvfjkhfdvibdfinjdsfnjvdsflj`,
[]string{
"AWSPARAMSTR:///djsfsdkjvfjkhfdvibdfinjdsfnjvdsflj",
"AWSPARAMSTR#bar/djsfsdkjvfjkhfdvibdfinjdsfnjvdsflj[version:123]"},
},
"all implementations": {
`param: AWSPARAMSTR:///djsfsdkjvfjkhfdvibdfinjdsfnjvdsflj
secretsmgr: AWSSECRETS#bar/djsfsdkjvfjkhfdvibdfinjdsfnjvdsflj[version:123]
gcp: GCPSECRETS:///djsfsdkjvfjkhfdvibdfinjdsfnjvdsflj
vault: VAULT:///djsfsdkjvfjkhfdvibdfinjdsfnjvdsflj[]
som othere strufsd
azkv: AZKVSECRET:///djsfsdkjvfjkhfdvibdfinjdsfnjvdsflj`,
[]string{
"GCPSECRETS:///djsfsdkjvfjkhfdvibdfinjdsfnjvdsflj",
"AWSPARAMSTR:///djsfsdkjvfjkhfdvibdfinjdsfnjvdsflj",
"AWSSECRETS#bar/djsfsdkjvfjkhfdvibdfinjdsfnjvdsflj[version:123]",
"AZKVSECRET:///djsfsdkjvfjkhfdvibdfinjdsfnjvdsflj",
"VAULT:///djsfsdkjvfjkhfdvibdfinjdsfnjvdsflj[]"},
},
}
for name, tt := range ttests {
t.Run(name, func(t *testing.T) {
got := configmanager.FindTokens(tt.input)
sort.Strings(got)
sort.Strings(tt.expect)
if !reflect.DeepEqual(got, tt.expect) {
t.Errorf("input=(%q)\n\ngot: %v\n\nwant: %v", tt.input, got, tt.expect)
}
})
}
}
func Test_YamlRetrieveMarshalled_errored_in_generator(t *testing.T) {
m := &mockGenerator{}
m.generate = func(tokens []string) (generator.ParsedMap, error) {
return nil, fmt.Errorf("failed to generate a parsedMap")
}
c := configmanager.New(context.TODO())
c.Config.WithTokenSeparator("://")
c.WithGenerator(m)
input := &testNestedStruct{}
err := c.RetrieveMarshalledYaml(input)
if err != nil {
} else {
t.Errorf(testutils.TestPhrase, nil, "err")
}
}
func Test_YamlRetrieveMarshalled_errored_in_marshal(t *testing.T) {
t.Skip()
m := &mockGenerator{}
m.generate = func(tokens []string) (generator.ParsedMap, error) {
return generator.ParsedMap{}, nil
}
c := configmanager.New(context.TODO())
c.Config.WithTokenSeparator("://")
c.WithGenerator(m)
err := c.RetrieveMarshalledYaml(&struct {
A int
B map[string]int `yaml:",inline"`
}{1, map[string]int{"a": 2}})
if err != nil {
} else {
t.Errorf(testutils.TestPhrase, nil, "err")
}
}
func Test_JsonRetrieveMarshalled_errored_in_generator(t *testing.T) {
m := &mockGenerator{}
m.generate = func(tokens []string) (generator.ParsedMap, error) {
return nil, fmt.Errorf("failed to generate a parsedMap")
}
c := configmanager.New(context.TODO())
c.Config.WithTokenSeparator("://")
c.WithGenerator(m)
input := &testNestedStruct{}
err := c.RetrieveMarshalledJson(input)
if err != nil {
} else {
t.Errorf(testutils.TestPhrase, nil, "err")
}
}
func Test_JsonRetrieveMarshalled_errored_in_marshal(t *testing.T) {
m := &mockGenerator{}
m.generate = func(tokens []string) (generator.ParsedMap, error) {
return generator.ParsedMap{}, nil
}
c := configmanager.New(context.TODO())
c.Config.WithTokenSeparator("://")
c.WithGenerator(m)
// input := &testNestedStruct{}
err := c.RetrieveMarshalledJson(nil)
if err != nil {
} else {
t.Errorf(testutils.TestPhrase, nil, "err")
}
}
// config tests
func Test_Generator_Config_(t *testing.T) {
ttests := map[string]struct {
expect config.GenVarsConfig
keySeparator, tokenSep, outputPath string
}{
"default config": {
expect: config.NewConfig().Config(),
// keySeparator: "|", tokenSep: "://",outputPath:"",
},
"outputPath overwritten only": {
expect: (config.NewConfig()).WithOutputPath("baresd").Config(),
// keySeparator: "|", tokenSep: "://",
outputPath: "baresd",
},
"outputPath and keysep overwritten": {
expect: (config.NewConfig()).WithOutputPath("baresd").WithKeySeparator("##").Config(),
keySeparator: "##",
outputPath: "baresd",
// tokenSep: "://",
},
}
for name, tt := range ttests {
t.Run(name, func(t *testing.T) {
cm := configmanager.New(context.TODO())
if tt.keySeparator != "" {
cm.Config.WithKeySeparator(tt.keySeparator)
}
if tt.tokenSep != "" {
cm.Config.WithTokenSeparator(tt.tokenSep)
}
if tt.outputPath != "" {
cm.Config.WithOutputPath(tt.outputPath)
}
got := cm.GeneratorConfig()
if diff := deep.Equal(got, &tt.expect); diff != nil {
t.Errorf(testutils.TestPhraseWithContext, "generator config", fmt.Sprintf("%q", got), fmt.Sprintf("%q", tt.expect))
}
})
}
}