-
Notifications
You must be signed in to change notification settings - Fork 41
/
config_ext_test.go
892 lines (799 loc) · 25 KB
/
config_ext_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
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
// Copyright (c) 2018 Uber Technologies, Inc.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
package config_test
import (
"fmt"
"strings"
"testing"
. "go.uber.org/config"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
yaml "gopkg.in/yaml.v2"
)
const (
_base = `
nothing: ~
fun:
- maserati
- porsche
practical: &ptr
toyota: camry
honda: accord
antique_scalar: model_t
antique_sequence:
- model_t
antique_mapping_empty: # will override with empty map
ford: model_t
antique_mapping_nil: # will override with nil
ford: model_t
occupants:
honda:
driver: jane
backseat: [nate]
extra_practical:
<<: *ptr
volkswagon: jetta
1: car
two:
"2": two cars
2: 2 cars
~: no car
no: car unavailable
`
_override = `
fun:
- maserati
- lamborghini
practical:
honda: civic
nissan: altima
antique_scalar: ~
antique_sequence: ~
antique_mapping_empty: {}
antique_mapping_nil: ~
occupants:
honda:
passenger: arthur
backseat: [nora]
`
)
type testCase struct {
Value Value
HasValue bool
Interface interface{}
Populate interface{}
}
func run(t testing.TB, tt testCase) {
assert.Equal(t, tt.HasValue, tt.Value.HasValue(), "unexpected result from HasValue")
assert.Equal(t, tt.Interface, tt.Value.Value(), "unexpected result from Value")
require.NoError(t, tt.Value.Populate(tt.Populate), "error populating")
}
func runCommonTests(t *testing.T, provider Provider) {
t.Run("missing", func(t *testing.T) {
var s string
t.Run("top level", func(t *testing.T) {
run(t, testCase{
Value: provider.Get("not_a_key"),
HasValue: false,
Interface: nil,
Populate: &s,
})
assert.Equal(t, "", s, "unexpected result after Populate")
})
t.Run("subkey", func(t *testing.T) {
run(t, testCase{
Value: provider.Get("practical.cadillac"),
HasValue: false,
Interface: nil,
Populate: &s,
})
assert.Equal(t, "", s, "unexpected result after Populate")
})
t.Run("subkey of sequence", func(t *testing.T) {
// Accessing a subkey of a sequence is meaningless.
run(t, testCase{
Value: provider.Get("fun.not_there"),
HasValue: false,
Interface: nil,
Populate: &s,
})
assert.Equal(t, "", s, "unexpected result after Populate")
})
})
t.Run("nil", func(t *testing.T) {
var s string
run(t, testCase{
Value: provider.Get("nothing"),
HasValue: true,
Interface: nil,
Populate: &s,
})
assert.Equal(t, "", s, "unexpected result after Populate")
})
t.Run("scalar", func(t *testing.T) {
var s string
run(t, testCase{
Value: provider.Get("practical.toyota"),
HasValue: true,
Interface: "camry",
Populate: &s,
})
assert.Equal(t, "camry", s, "unexpected result after Populate")
})
t.Run("map", func(t *testing.T) {
var m map[string]string
run(t, testCase{
Value: provider.Get("practical"),
HasValue: true,
Interface: map[interface{}]interface{}{
"toyota": "camry",
"honda": "civic",
"nissan": "altima",
},
Populate: &m,
})
assert.Equal(t, map[string]string{
"toyota": "camry",
"honda": "civic",
"nissan": "altima",
}, m, "unexpected result after populate")
})
t.Run("slice", func(t *testing.T) {
var s []string
run(t, testCase{
Value: provider.Get("fun"),
HasValue: true,
Interface: []interface{}{"maserati", "lamborghini"},
Populate: &s,
})
assert.Equal(t, []string{"maserati", "lamborghini"}, s, "unexpected result after populate")
})
t.Run("struct", func(t *testing.T) {
type Occupants struct {
Driver string
Passenger string
Backseat []string
}
var o Occupants
run(t, testCase{
Value: provider.Get("occupants.honda"),
HasValue: true,
Interface: map[interface{}]interface{}{
"driver": "jane",
"passenger": "arthur",
"backseat": []interface{}{"nora"},
},
Populate: &o,
})
assert.Equal(t, Occupants{
Driver: "jane",
Passenger: "arthur",
Backseat: []string{"nora"},
}, o, "unexpected result after populate")
})
t.Run("nil merged into scalar", func(t *testing.T) {
// Merging replaces scalars, so an explicit nil should replace
// lower-priority values. (This matches gopkg.in/yaml.v2.)
var s string
run(t, testCase{
Value: provider.Get("antique_scalar"),
HasValue: true,
Interface: nil,
Populate: &s,
})
assert.Empty(t, s, "unexpected result after Populate")
})
t.Run("nil merged into sequence", func(t *testing.T) {
// Merging replaces sequences, so an explicit nil should replace
// lower-priority values. (This matches gopkg.in/yaml.v2.)
var s []string
run(t, testCase{
Value: provider.Get("antique_sequence"),
HasValue: true,
Interface: nil,
Populate: &s,
})
assert.Empty(t, s, "unexpected result after Populate")
})
t.Run("nil merged into mapping", func(t *testing.T) {
// Merging deep-merges mappings, but we should honor explicitly-configured
// nils and replace all existing configuration. (This matches
// gopkg.in/yaml.v2.)
var m map[string]string
run(t, testCase{
Value: provider.Get("antique_mapping_nil"),
HasValue: true,
Interface: nil,
Populate: &m,
})
assert.Nil(t, m, "unexpected result after Populate")
})
t.Run("empty map merged into mapping", func(t *testing.T) {
// Merging deep-merges mappings, so merging in an empty map is a no-op.
// (This matches gopkg.in/yaml.v2.)
var m map[string]string
run(t, testCase{
Value: provider.Get("antique_mapping_empty"),
HasValue: true,
Interface: map[interface{}]interface{}{"ford": "model_t"},
Populate: &m,
})
assert.Equal(t, map[string]string{"ford": "model_t"}, m, "unexpected result after Populate")
})
t.Run("anchors and native merge", func(t *testing.T) {
var m map[string]string
run(t, testCase{
Value: provider.Get("extra_practical"),
HasValue: true,
Interface: map[interface{}]interface{}{
"toyota": "camry",
"honda": "accord",
"volkswagon": "jetta",
},
Populate: &m,
})
assert.Equal(t, map[string]string{
"toyota": "camry",
"honda": "accord",
"volkswagon": "jetta",
}, m, "unexpected result after populate")
})
t.Run("multiple gets", func(t *testing.T) {
var s string
run(t, testCase{
Value: provider.Get("occupants").Get("honda").Get("driver"),
HasValue: true,
Interface: "jane",
Populate: &s,
})
assert.Equal(t, "jane", s, "unexpected result after populate")
})
t.Run("default missing value", func(t *testing.T) {
var s string
val, err := provider.Get("not_there").WithDefault("something")
require.NoError(t, err, "couldn't set default")
run(t, testCase{
Value: val,
HasValue: true,
Interface: "something",
Populate: &s,
})
assert.Equal(t, "something", s, "unexpected result after populate")
})
t.Run("default overriden by scalar", func(t *testing.T) {
var s string
val, err := provider.Get("practical.honda").WithDefault("CRV")
require.NoError(t, err, "couldn't set default")
run(t, testCase{
Value: val,
HasValue: true,
Interface: "civic",
Populate: &s,
})
assert.Equal(t, "civic", s, "unexpected result after populate")
})
t.Run("default overriden by nil", func(t *testing.T) {
var s string
val, err := provider.Get("nothing").WithDefault("something")
require.NoError(t, err, "couldn't set default")
run(t, testCase{
Value: val,
HasValue: true,
Interface: nil,
Populate: &s,
})
assert.Equal(t, "", s, "unexpected result after populate")
})
t.Run("default merges maps", func(t *testing.T) {
var m map[string]string
val, err := provider.Get("practical").WithDefault(map[string]string{
"ford": "fiesta", // new key
"toyota": "corolla", // key present and set to "camry"
})
require.NoError(t, err, "couldn't set default")
run(t, testCase{
Value: val,
HasValue: true,
Interface: map[interface{}]interface{}{
"honda": "civic",
"toyota": "camry",
"nissan": "altima",
"ford": "fiesta",
},
Populate: &m,
})
assert.Equal(t, map[string]string{
"honda": "civic",
"toyota": "camry",
"nissan": "altima",
"ford": "fiesta",
}, m, "unexpected result after populate")
})
t.Run("default replaces sequences", func(t *testing.T) {
var s []string
val, err := provider.Get("fun").WithDefault([]string{"delorean"})
require.NoError(t, err, "couldn't set default")
run(t, testCase{
Value: val,
HasValue: true,
Interface: []interface{}{"maserati", "lamborghini"},
Populate: &s,
})
assert.Equal(t, []string{"maserati", "lamborghini"}, s, "unexpected result after populate")
})
t.Run("chained defaults", func(t *testing.T) {
// Each call to WithDefault should merge all existing config into the
// default, then use the result. This means that repeated calls to
// WithDefault should deep-merge all the supplied defaults, with the last
// call to WithDefault having the lowest priority.
// First, set a default.
val, err := provider.Get("top").WithDefault(map[string]string{"middle": "bottom"})
require.NoError(t, err, "couldn't set first default")
// Second, set another default with a different key. First default should
// be deep-merged into this one.
val, err = val.WithDefault(map[string]string{"other_middle": "other_bottom"})
require.NoError(t, err, "couldn't set second default")
// Last, set a default for the new key. The result of merging first and
// second defaults should be merged into this value, overwriting it.
val, err = val.WithDefault(map[string]string{"other_middle": "should be overwritten"})
require.NoError(t, err, "couldn't set third default")
var m map[string]string
run(t, testCase{
Value: val,
HasValue: true,
Interface: map[interface{}]interface{}{
"middle": "bottom",
"other_middle": "other_bottom",
},
Populate: &m,
})
assert.Equal(t, map[string]string{
"middle": "bottom",
"other_middle": "other_bottom",
}, m, "unexpected result after populate")
})
t.Run("deep copy", func(t *testing.T) {
// Regression test for https://github.com/uber-go/config/issues/76.
const key = "foobar"
unmarshal := func() map[interface{}]interface{} {
var m map[interface{}]interface{}
require.NoError(t, provider.Get(Root).Populate(&m), "Populate failed")
return m["practical"].(map[interface{}]interface{})
}
before := unmarshal()
require.NotContains(t, before, key, "precondition failed: key %q already in map", key)
before[key] = "bazbing"
after := unmarshal()
assert.NotContains(t, after, key, "didn't deep copy config")
})
t.Run("named field", func(t *testing.T) {
c := struct {
Toyota string
Honda string
Datsun string `yaml:"nissan"`
}{}
require.NoError(t, provider.Get("practical").Populate(&c))
assert.Equal(t, "camry", c.Toyota, "wrong Toyota")
assert.Equal(t, "altima", c.Datsun, "wrong Datsun (aka Nissan)")
})
t.Run("omitempty field", func(t *testing.T) {
c := struct {
Toyota string
Honda string
Nissan string `yaml:",omitempty"`
}{}
require.NoError(t, provider.Get("practical").Populate(&c))
assert.Equal(t, "altima", c.Nissan, "wrong Nissan")
})
t.Run("flow field", func(t *testing.T) {
c := struct {
Toyota string
Honda string
Nissan string `yaml:",flow"`
}{}
require.NoError(t, provider.Get("practical").Populate(&c))
assert.Equal(t, "altima", c.Nissan, "wrong Nissan")
})
t.Run("inline field", func(t *testing.T) {
c := struct {
Humans struct {
Driver string
Passenger string
Backseat []string
} `yaml:",inline"`
}{}
require.NoError(t, provider.Get("occupants.honda").Populate(&c))
assert.Equal(t, 1, len(c.Humans.Backseat), "wrong number of backseat occupants")
})
t.Run("non-string scalar keys", func(t *testing.T) {
t.Run("numeric key", func(t *testing.T) {
var val string
run(t, testCase{
Value: provider.Get("1"),
HasValue: true,
Interface: "car",
Populate: &val,
})
})
t.Run("nil key", func(t *testing.T) {
var val string
run(t, testCase{
Value: provider.Get("~"),
HasValue: true,
Interface: "no car",
Populate: &val,
})
})
t.Run("boolean key", func(t *testing.T) {
// This lookup will appear to be a string key.
var val string
run(t, testCase{
Value: provider.Get("no"),
HasValue: true,
Interface: "car unavailable",
Populate: &val,
})
// Yet in reality it is a boolean key.
run(t, testCase{
Value: provider.Get("false"),
HasValue: true,
Interface: "car unavailable",
Populate: &val,
})
})
})
t.Run("string scalar keys which look like non-string", func(t *testing.T) {
t.Run("numeric prefers string variant", func(t *testing.T) {
var val string
run(t, testCase{
Value: provider.Get("two.2"),
HasValue: true,
Interface: "two cars",
Populate: &val,
})
})
})
}
func TestPermissiveYAML(t *testing.T) {
provider, err := NewYAML(
Source(strings.NewReader(_base)),
Source(strings.NewReader(_override)),
Permissive(),
)
require.NoError(t, err, "couldn't create provider")
assert.Equal(t, "YAML", provider.Get(Root).Source(), "wrong source")
assert.Equal(t, "<nil>", provider.Get("nothing").String(), "wrong string representation")
runCommonTests(t, provider)
t.Run("ignore type mismatch during merge", func(t *testing.T) {
provider, err := NewYAML(
Source(strings.NewReader("mismatch: foo")), // scalar
Source(strings.NewReader("mismatch: [foo]")), // sequence
Permissive(),
)
require.NoError(t, err, "couldn't create permissive provider with type mismatch")
var s []string
run(t, testCase{
Value: provider.Get("mismatch"),
HasValue: true,
Interface: []interface{}{"foo"},
Populate: &s,
})
assert.Equal(t, []string{"foo"}, s, "unexpected result after populate")
})
t.Run("ignores type mismatches during WithDefault", func(t *testing.T) {
// Since defaults are effectively the lowest-priority config, their types
// should be ignored when necessary.
provider, err := NewYAML(
Source(strings.NewReader("mismatch: foo")), // scalar
Permissive(),
)
require.NoError(t, err, "couldn't create provider")
val, err := provider.Get("mismatch").WithDefault([]string{"foo"}) // sequence
assert.NoError(t, err, "error on type mismatch in permissive mode")
assert.Equal(t, "foo", val.Value(), "wrong merged output")
})
t.Run("ignore duplicate keys", func(t *testing.T) {
provider, err := NewYAML(
Source(strings.NewReader("dupe: foo\ndupe: bar")),
Permissive(),
)
require.NoError(t, err, "couldn't create provider")
var s string
run(t, testCase{
Value: provider.Get("dupe"),
HasValue: true,
Interface: "bar",
Populate: &s,
})
assert.Equal(t, "bar", s, "unexpected result after populate")
})
t.Run("ignores extra data during Populate", func(t *testing.T) {
provider, err := NewYAML(
Source(strings.NewReader("foo: bar\nbaz: quux")),
Permissive(),
)
require.NoError(t, err, "couldn't create provider")
c := struct {
Foo string
}{}
require.NoError(t, provider.Get(Root).Populate(&c), "populate failed")
assert.Equal(t, "bar", c.Foo, "unexpected value")
})
t.Run("may provide ignored field", func(t *testing.T) {
provider, err := NewYAML(
Source(strings.NewReader("toyota: camry")),
Permissive(),
)
require.NoError(t, err, "couldn't create provider")
c := struct {
Toyota string `yaml:"-"`
}{}
require.NoError(t, provider.Get(Root).Populate(&c))
assert.Empty(t, c.Toyota, "should ignore Toyota")
})
}
func TestStrictYAML(t *testing.T) {
provider, err := NewYAML(
Source(strings.NewReader(_base)),
Source(strings.NewReader(_override)),
)
require.NoError(t, err, "couldn't create provider")
assert.Equal(t, "YAML", provider.Get(Root).Source(), "wrong source")
assert.Equal(t, "<nil>", provider.Get("nothing").String(), "wrong string representation")
runCommonTests(t, provider)
t.Run("fail on type mismatch during merge", func(t *testing.T) {
_, err := NewYAML(
Source(strings.NewReader("mismatch: foo")), // scalar
Source(strings.NewReader("mismatch: [foo]")), // sequence
)
require.Error(t, err, "couldn't create permissive provider with type mismatch")
assert.Contains(t, err.Error(), "couldn't merge", "unexpected error message")
})
t.Run("fail on type mismatches during WithDefault", func(t *testing.T) {
provider, err := NewYAML(
Source(strings.NewReader("mismatch: foo")), // scalar
)
require.NoError(t, err, "couldn't create provider")
_, err = provider.Get("mismatch").WithDefault([]string{"foo"}) // sequence
assert.Error(t, err, "success on type mismatch in strict mode")
assert.Contains(t, err.Error(), "can't merge", "unexpected error message")
})
t.Run("fail on duplicate keys", func(t *testing.T) {
_, err := NewYAML(
Source(strings.NewReader("dupe: foo\ndupe: bar")),
)
require.Error(t, err, "created strict provider with type mismatch")
assert.Contains(t, err.Error(), `key "dupe" already set in map`, "unexpected error message")
})
t.Run("allow missing data during Populate", func(t *testing.T) {
provider, err := NewYAML(
Source(strings.NewReader("foo: bar")),
)
require.NoError(t, err, "couldn't create provider")
c := struct {
Foo string
Unset string
}{}
err = provider.Get(Root).Populate(&c)
require.NoError(t, err, "populate failed")
assert.Equal(t, c.Foo, "bar", "unexpected value for set field")
assert.Zero(t, c.Unset, "expected unset field to be zero value")
})
t.Run("fail on extra data during Populate", func(t *testing.T) {
provider, err := NewYAML(
Source(strings.NewReader("foo: bar\nbaz: quux")),
)
require.NoError(t, err, "couldn't create provider")
c := struct {
Foo string
}{}
err = provider.Get(Root).Populate(&c)
require.Error(t, err, "populate succeeded")
assert.Contains(t, err.Error(), "field baz not found in type struct", "unexpected error message")
})
t.Run("must not provide ignored fields", func(t *testing.T) {
provider, err := NewYAML(Source(strings.NewReader("toyota: camry")))
require.NoError(t, err, "couldn't create provider")
c := struct {
Toyota string `yaml:"-"`
}{}
err = provider.Get(Root).Populate(&c)
require.Error(t, err, "expected error")
assert.Contains(t, err.Error(), "field toyota not found in type", "unexpected error message")
})
t.Run("non-unique keys in a map[string] context", func(t *testing.T) {
var m map[string]interface{}
assert.Error(t, provider.Get("two").Populate(&m), "Populate succeeded")
})
}
func TestStaticFromYAML(t *testing.T) {
// Since we have a common test suite, we may as well use it to exercise the
// static provider too.
var base, override interface{}
require.NoError(t, yaml.Unmarshal([]byte(_base), &base), "couldn't unmarshal base YAML")
require.NoError(t, yaml.Unmarshal([]byte(_override), &override), "couldn't unmarshal base YAML")
p, err := NewYAML(Static(base), Static(override))
require.NoError(t, err, "couldn't construct provider")
runCommonTests(t, p)
}
func TestEmptySources(t *testing.T) {
const (
key = "foo"
val = "bar"
)
full := fmt.Sprintf("%s: %s", key, val)
empty := ""
comment := "# just a comment"
runTests := func(t *testing.T, newProvider func([]string) Provider) {
tests := []struct {
desc string
sources []string
expect interface{}
}{
{"no sources", []string{}, nil},
{"empty base", []string{empty, full}, val},
{"empty override", []string{full, empty}, val},
{"empty base and override", []string{empty, empty}, nil},
{"comment-only base", []string{comment, full}, val},
{"comment-only override", []string{full, comment}, val},
{"empty base and comment-only override", []string{empty, comment}, nil},
{"comment-only base and empty override", []string{comment, empty}, nil},
}
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
p := newProvider(tt.sources)
assert.Equal(t, tt.expect, p.Get("foo").Value(), "incorrect merged result")
d, err := p.Get("not_there").WithDefault(42)
require.NoError(t, err, "failed to set default")
assert.Equal(t, 42, d.Value(), "incorrect defaulted value")
})
}
}
t.Run("NewYAML", func(t *testing.T) {
runTests(t, func(sources []string) Provider {
opts := make([]YAMLOption, len(sources))
for i := range sources {
opts[i] = Source(strings.NewReader(sources[i]))
}
p, err := NewYAML(opts...)
require.NoError(t, err, "failed to create provider")
return p
})
})
t.Run("NewProviderGroup", func(t *testing.T) {
runTests(t, func(sources []string) Provider {
providers := make([]Provider, len(sources))
for i := range sources {
var err error
providers[i], err = NewYAML(Source(strings.NewReader(sources[i])))
require.NoError(t, err, "failed to create provider")
}
p, err := NewProviderGroup("foo", providers...)
require.NoError(t, err, "failed to create provider")
return p
})
})
}
func TestNullSources(t *testing.T) {
const (
key = "foo"
val = "bar"
)
full := fmt.Sprintf("%s: %s", key, val)
empty := ""
null := "~"
runTests := func(t *testing.T, newProvider func([]string) Provider) {
tests := []struct {
desc string
sources []string
expect interface{}
}{
{"null base", []string{null, empty, full}, val},
{"null override", []string{full, empty, null}, nil},
}
for _, tt := range tests {
t.Run(tt.desc, func(t *testing.T) {
p := newProvider(tt.sources)
assert.Equal(t, tt.expect, p.Get("foo").Value(), "incorrect merged result")
d, err := p.Get("not_there").WithDefault(42)
require.NoError(t, err, "failed to set default")
// Since there's an explicit nil in the initial sources, calls to
// WithDefault should have no effect.
assert.Equal(t, nil, d.Value(), "default should have no effect")
})
}
}
t.Run("NewYAML", func(t *testing.T) {
runTests(t, func(sources []string) Provider {
opts := make([]YAMLOption, len(sources))
for i := range sources {
opts[i] = Source(strings.NewReader(sources[i]))
}
p, err := NewYAML(opts...)
require.NoError(t, err, "failed to create provider")
return p
})
})
t.Run("NewProviderGroup", func(t *testing.T) {
runTests(t, func(sources []string) Provider {
providers := make([]Provider, len(sources))
for i := range sources {
var err error
providers[i], err = NewYAML(Source(strings.NewReader(sources[i])))
require.NoError(t, err, "failed to create provider")
}
p, err := NewProviderGroup("foo", providers...)
require.NoError(t, err, "failed to create provider")
return p
})
})
}
func environment(vars map[string]string) LookupFunc {
return func(k string) (string, bool) {
v, ok := vars[k]
return v, ok
}
}
func TestRawSources(t *testing.T) {
lookup := environment(map[string]string{
"ZONE": "west1",
})
base := `zone: $ZONE`
secrets := `secret: abc$ZONE`
cfg := struct {
Zone string
Secret string
}{}
provider, err := NewYAML(
Source(strings.NewReader(base)),
RawSource(strings.NewReader(secrets)),
Expand(lookup),
)
require.NoError(t, err, "failed to create provider")
assert.NoError(t, provider.Get(Root).Populate(&cfg), "failed to populate config struct")
assert.Equal(t, "west1", cfg.Zone, "unexpected zone")
assert.Equal(t, "abc$ZONE", cfg.Secret, "unexpected secret")
}
func TestEnvironmentExpansion(t *testing.T) {
const expectZone = "west1"
const expectMeta = "$ZONE"
lookup := environment(map[string]string{
"ZONE": expectZone,
"META": expectMeta,
})
base := `{zone: $ZONE}`
override := `{zone: $ZONE, meta: $META}`
provider, err := NewYAML(
Source(strings.NewReader(base)),
Source(strings.NewReader(override)),
Expand(lookup),
)
require.NoError(t, err, "failed to create provider")
zone := provider.Get("zone")
assert.Equal(t, expectZone, zone.String(), "wrong zone")
meta := provider.Get("meta")
assert.Equal(t, expectMeta, meta.String(), "wrong meta")
defaultedZone, err := zone.WithDefault("default")
require.NoError(t, err, "failed to set default for zone")
defaultedMeta, err := meta.WithDefault("default")
require.NoError(t, err, "failed to set default for meta")
assert.Equal(t, expectZone, defaultedZone.String(), "wrong zone after default")
assert.Equal(t, expectMeta, defaultedMeta.String(), "wrong meta after default")
}