Repository navigation
Expand file tree
/
Copy pathlayered_extra_test.go
More file actions
1068 lines (863 loc) · 33.8 KB
/
Copy pathlayered_extra_test.go
File metadata and controls
1068 lines (863 loc) · 33.8 KB
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
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package config_test
import (
"context"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tarantool/go-config/v2"
"github.com/tarantool/go-config/v2/collectors"
"github.com/tarantool/go-config/v2/meta"
)
// ---------------------------------------------------------------------------
// Cross-loader MergeReplace map-recursion through Effective
// ---------------------------------------------------------------------------.
// TestLayered_CrossLoader_NonConflictingSubkeysCoexist is the headline
// invariant of accumulateLayerResult: a higher-priority loader that only sets
// one sub-key of a map must not wipe out sibling sub-keys contributed by a
// lower-priority loader. (file loader sets replication/failover, env loader
// sets replication/timeout — both must survive in the effective view.)
func TestLayered_CrossLoader_NonConflictingSubkeysCoexist(t *testing.T) {
t.Parallel()
fileLoader := collectors.NewMap(map[string]any{
"replication": map[string]any{"failover": "election"},
"groups": map[string]any{
"storages": map[string]any{
"replicasets": map[string]any{
"s-001": map[string]any{
"instances": map[string]any{"s-001-a": map[string]any{}},
},
},
},
},
}).WithName("file").WithSourceType(config.FileSource)
envLoader := collectors.NewMap(map[string]any{
"replication": map[string]any{"timeout": 30},
}).WithName("env").WithSourceType(config.EnvSource)
builder := config.NewBuilder()
builder = builder.AddCollector(fileLoader) // lower priority.
builder = builder.AddCollector(envLoader) // higher priority.
builder = builder.WithInheritance(
config.Levels(config.Global, "groups", "replicasets", "instances"),
)
cfg, errs := builder.Build(t.Context())
require.Empty(t, errs)
eff, err := cfg.Effective(
config.NewKeyPath("groups/storages/replicasets/s-001/instances/s-001-a"))
require.NoError(t, err)
var failover string
_, err = eff.Get(config.NewKeyPath("replication/failover"), &failover)
require.NoError(t, err)
assert.Equal(t, "election", failover, "lower-priority sub-key must survive")
var timeout int
_, err = eff.Get(config.NewKeyPath("replication/timeout"), &timeout)
require.NoError(t, err)
assert.Equal(t, 30, timeout, "higher-priority sub-key must be present")
}
// TestLayered_CrossLoader_ConflictingSubkeyHigherPriorityWins verifies that
// when both loaders set the same sub-key, the higher-priority loader wins while
// non-conflicting sub-keys from the lower-priority loader still coexist.
func TestLayered_CrossLoader_ConflictingSubkeyHigherPriorityWins(t *testing.T) {
t.Parallel()
fileLoader := collectors.NewMap(map[string]any{
"replication": map[string]any{"failover": "manual", "timeout": 10},
"groups": map[string]any{
"storages": map[string]any{
"replicasets": map[string]any{
"s-001": map[string]any{
"instances": map[string]any{"s-001-a": map[string]any{}},
},
},
},
},
}).WithName("file").WithSourceType(config.FileSource)
envLoader := collectors.NewMap(map[string]any{
"replication": map[string]any{"failover": "election"},
}).WithName("env").WithSourceType(config.EnvSource)
builder := config.NewBuilder()
builder = builder.AddCollector(fileLoader)
builder = builder.AddCollector(envLoader)
builder = builder.WithInheritance(
config.Levels(config.Global, "groups", "replicasets", "instances"),
)
cfg, errs := builder.Build(t.Context())
require.Empty(t, errs)
eff, err := cfg.Effective(
config.NewKeyPath("groups/storages/replicasets/s-001/instances/s-001-a"))
require.NoError(t, err)
var failover string
fMeta, err := eff.Get(config.NewKeyPath("replication/failover"), &failover)
require.NoError(t, err)
assert.Equal(t, "election", failover)
assert.Equal(t, "env", fMeta.Source.Name)
var timeout int
tMeta, err := eff.Get(config.NewKeyPath("replication/timeout"), &timeout)
require.NoError(t, err)
assert.Equal(t, 10, timeout)
assert.Equal(t, "file", tMeta.Source.Name)
}
// TestLayered_CrossLoader_TypeMismatchHigherPriorityReplaces verifies that when
// a higher-priority loader provides a value of a different shape (scalar vs
// map), the higher-priority loader replaces the lower-priority contribution
// wholesale (no attempt to merge incompatible nodes).
func TestLayered_CrossLoader_TypeMismatchHigherPriorityReplaces(t *testing.T) {
t.Parallel()
fileLoader := collectors.NewMap(map[string]any{
"thing": map[string]any{"a": 1, "b": 2},
"groups": map[string]any{
"storages": map[string]any{
"replicasets": map[string]any{
"s-001": map[string]any{
"instances": map[string]any{"s-001-a": map[string]any{}},
},
},
},
},
}).WithName("file").WithSourceType(config.FileSource)
envLoader := collectors.NewMap(map[string]any{
"thing": "scalar",
}).WithName("env").WithSourceType(config.EnvSource)
builder := config.NewBuilder()
builder = builder.AddCollector(fileLoader)
builder = builder.AddCollector(envLoader)
builder = builder.WithInheritance(
config.Levels(config.Global, "groups", "replicasets", "instances"),
)
cfg, errs := builder.Build(t.Context())
require.Empty(t, errs)
eff, err := cfg.Effective(
config.NewKeyPath("groups/storages/replicasets/s-001/instances/s-001-a"))
require.NoError(t, err)
var thing string
_, err = eff.Get(config.NewKeyPath("thing"), &thing)
require.NoError(t, err)
assert.Equal(t, "scalar", thing)
// The lower-priority map sub-keys must be gone — they were replaced, not merged.
_, err = eff.Get(config.NewKeyPath("thing/a"), new(int))
assert.Error(t, err)
}
// ---------------------------------------------------------------------------
// Tombstone suppression at an intermediate scope level
// ---------------------------------------------------------------------------.
// TestMutableConfig_Layered_Delete_IntermediateScope_FallsBackToGlobal deletes
// a key that was contributed at a group-level scope. The tombstone must be
// resolved against the group scope prefix (not the global one), suppressing
// only that level's contribution, so the global-scope value shines through.
func TestMutableConfig_Layered_Delete_IntermediateScope_FallsBackToGlobal(t *testing.T) {
t.Parallel()
builder := config.NewBuilder()
builder = builder.AddCollector(collectors.NewMap(map[string]any{
"replication": map[string]any{"failover": "manual"}, // global scope.
"groups": map[string]any{
"storages": map[string]any{
"replication": map[string]any{"failover": "election"}, // group scope.
"replicasets": map[string]any{
"s-001": map[string]any{
"instances": map[string]any{"s-001-a": map[string]any{}},
},
},
},
},
}).WithName("file"))
builder = builder.WithInheritance(
config.Levels(config.Global, "groups", "replicasets", "instances"),
)
cfg, errs := builder.BuildMutable(t.Context())
require.Empty(t, errs)
leafPath := config.NewKeyPath("groups/storages/replicasets/s-001/instances/s-001-a")
// Baseline: group-scope value wins over global-scope value within the loader.
eff0, err := cfg.Effective(leafPath)
require.NoError(t, err)
var base string
_, err = eff0.Get(config.NewKeyPath("replication/failover"), &base)
require.NoError(t, err)
assert.Equal(t, "election", base)
// Delete the group-scoped contribution.
require.True(t, cfg.Delete(config.NewKeyPath("groups/storages/replication/failover")))
eff, err := cfg.Effective(leafPath)
require.NoError(t, err)
var failover string
_, err = eff.Get(config.NewKeyPath("replication/failover"), &failover)
require.NoError(t, err)
assert.Equal(t, "manual", failover,
"group-scope contribution suppressed; global-scope value must shine through")
}
// ---------------------------------------------------------------------------
// MutableConfig.Merge / Update in layered mode
// ---------------------------------------------------------------------------.
func mergeSourceConfig(t *testing.T, data map[string]any) config.Config {
t.Helper()
b := config.NewBuilder()
b = b.AddCollector(collectors.NewMap(data))
cfg, errs := b.Build(t.Context())
require.Empty(t, errs)
return cfg
}
func TestMutableConfig_Layered_Merge_EffectiveReflectsModified(t *testing.T) {
t.Parallel()
cfg := buildLayeredMutable(t)
leafPath := config.NewKeyPath("groups/storages/replicasets/s-001/instances/s-001-a")
other := mergeSourceConfig(t, map[string]any{
"replication": map[string]any{"failover": "supervised"},
})
require.NoError(t, cfg.Merge(&other))
eff, err := cfg.Effective(leafPath)
require.NoError(t, err)
var failover string
m, err := eff.Get(config.NewKeyPath("replication/failover"), &failover)
require.NoError(t, err)
assert.Equal(t, "supervised", failover, "merged value must win over every loader")
assert.Equal(t, meta.ModifiedSourceName, m.Source.Name)
}
func TestMutableConfig_Layered_Update_EffectiveReflectsModified(t *testing.T) {
t.Parallel()
cfg := buildLayeredMutable(t)
leafPath := config.NewKeyPath("groups/storages/replicasets/s-001/instances/s-001-a")
// Update only touches keys that already exist in the root. The merged root
// already carries replication/failover from the env loader.
other := mergeSourceConfig(t, map[string]any{
"replication": map[string]any{"failover": "supervised"},
})
require.NoError(t, cfg.Update(&other))
eff, err := cfg.Effective(leafPath)
require.NoError(t, err)
var failover string
m, err := eff.Get(config.NewKeyPath("replication/failover"), &failover)
require.NoError(t, err)
assert.Equal(t, "supervised", failover)
assert.Equal(t, meta.ModifiedSourceName, m.Source.Name)
}
// ---------------------------------------------------------------------------
// Ancestor-scope deletion -> ErrPathNotFound (entityTombstoned prefix match)
// ---------------------------------------------------------------------------.
func TestMutableConfig_Layered_Delete_AncestorScope_ErrPathNotFound(t *testing.T) {
t.Parallel()
cfg := buildLayeredMutable(t)
leafPath := config.NewKeyPath("groups/storages/replicasets/s-001/instances/s-001-a")
_, err := cfg.Effective(leafPath)
require.NoError(t, err)
// Delete a strict ancestor of the entity (the replicaset node).
require.True(t, cfg.Delete(config.NewKeyPath("groups/storages/replicasets/s-001")))
_, err = cfg.Effective(leafPath)
require.Error(t, err)
assert.ErrorIs(t, err, config.ErrPathNotFound,
"deleting an ancestor scope must make the entity unresolvable, got: %v", err)
}
// ---------------------------------------------------------------------------
// Delete validation failure (layered): no tombstone, state stays consistent
// ---------------------------------------------------------------------------.
func TestMutableConfig_Layered_Delete_ValidationFailure_NoTombstone(t *testing.T) {
t.Parallel()
builder := config.NewBuilder()
// Root will carry exactly 3 top-level keys; any delete drops below the minimum.
builder = builder.WithValidator(&minKeysValidator{minKeys: 3})
builder = builder.AddCollector(collectors.NewMap(map[string]any{
"replication": map[string]any{"failover": "manual"},
"alpha": 1,
"groups": map[string]any{
"storages": map[string]any{
"replicasets": map[string]any{
"s-001": map[string]any{
"instances": map[string]any{"s-001-a": map[string]any{}},
},
},
},
},
}).WithName("file"))
builder = builder.WithInheritance(
config.Levels(config.Global, "groups", "replicasets", "instances"),
)
cfg, errs := builder.BuildMutable(t.Context())
require.Empty(t, errs)
leafPath := config.NewKeyPath("groups/storages/replicasets/s-001/instances/s-001-a")
// Deleting any top-level key would drop below the minimum and must be rejected.
require.False(t, cfg.Delete(config.NewKeyPath("alpha")))
require.False(t, cfg.Delete(config.NewKeyPath("replication/failover")))
// The rejected deletes left no tombstone behind and no state change:
// alpha is still resolvable and the entity still resolves.
var alpha int
_, err := cfg.Get(config.NewKeyPath("alpha"), &alpha)
require.NoError(t, err)
assert.Equal(t, 1, alpha)
eff, err := cfg.Effective(leafPath)
require.NoError(t, err)
var failover string
_, err = eff.Get(config.NewKeyPath("replication/failover"), &failover)
require.NoError(t, err)
assert.Equal(t, "manual", failover)
}
// ---------------------------------------------------------------------------
// WithNoInherit combined with multi-loader layering
// ---------------------------------------------------------------------------.
// TestLayered_NoInherit_MultiLoader verifies that WithNoInherit interacts
// correctly with cross-loader layering: a non-leaf-scope contribution of a
// no-inherit key is dropped even from a higher-priority loader, while a
// leaf-scope contribution from a lower-priority loader is kept.
func TestLayered_NoInherit_MultiLoader(t *testing.T) {
t.Parallel()
// Loader 1 (lower priority): leader set at the instance (leaf) scope.
loader1 := collectors.NewMap(map[string]any{
"groups": map[string]any{
"storages": map[string]any{
"replicasets": map[string]any{
"s-001": map[string]any{
"instances": map[string]any{
"s-001-a": map[string]any{"leader": "from-instance"},
},
},
},
},
},
}).WithName("file").WithSourceType(config.FileSource)
// Loader 2 (higher priority): leader set at the replicaset scope.
loader2 := collectors.NewMap(map[string]any{
"groups": map[string]any{
"storages": map[string]any{
"replicasets": map[string]any{
"s-001": map[string]any{"leader": "from-replicaset"},
},
},
},
}).WithName("env").WithSourceType(config.EnvSource)
builder := config.NewBuilder()
builder = builder.AddCollector(loader1)
builder = builder.AddCollector(loader2)
builder = builder.WithInheritance(
config.Levels(config.Global, "groups", "replicasets", "instances"),
config.WithNoInherit("leader"),
)
cfg, errs := builder.Build(t.Context())
require.Empty(t, errs)
eff, err := cfg.Effective(
config.NewKeyPath("groups/storages/replicasets/s-001/instances/s-001-a"))
require.NoError(t, err)
var leader string
_, err = eff.Get(config.NewKeyPath("leader"), &leader)
require.NoError(t, err)
assert.Equal(t, "from-instance", leader,
"non-leaf-scope no-inherit key from the higher-priority loader must be dropped; "+
"leaf-scope contribution from the lower-priority loader survives")
}
// ---------------------------------------------------------------------------
// MultiCollector counts as a single layer
// ---------------------------------------------------------------------------.
// multiMapCollector wraps a base Map (so it satisfies config.Collector) and a
// fixed list of sub-collectors, implementing config.MultiCollector.
type multiMapCollector struct {
*collectors.Map
subs []config.Collector
}
func (m *multiMapCollector) Collectors(_ context.Context) ([]config.Collector, error) {
return m.subs, nil
}
// TestLayered_MultiCollector_CountsAsOneLayer verifies that a MultiCollector is
// folded into one layer tree: a higher-priority top-level collector outranks the
// whole MultiCollector regardless of which sub-collector or scope a value came from.
func TestLayered_MultiCollector_CountsAsOneLayer(t *testing.T) {
t.Parallel()
sub1 := collectors.NewMap(map[string]any{
"groups": map[string]any{
"storages": map[string]any{
"replicasets": map[string]any{
"s-001": map[string]any{
"instances": map[string]any{
"s-001-a": map[string]any{"mode": "from-sub1-instance"},
},
},
},
},
},
}).WithName("sub1").WithSourceType(config.FileSource)
sub2 := collectors.NewMap(map[string]any{
"mode": "from-sub2-global",
}).WithName("sub2").WithSourceType(config.FileSource)
multi := &multiMapCollector{
Map: collectors.NewMap(nil).WithName("multi").WithSourceType(config.FileSource),
subs: []config.Collector{sub1, sub2},
}
// Higher-priority top-level collector, global scope.
top := collectors.NewMap(map[string]any{
"mode": "from-top-global",
}).WithName("top").WithSourceType(config.EnvSource)
builder := config.NewBuilder()
builder = builder.AddCollector(multi) // lower priority (one layer).
builder = builder.AddCollector(top) // higher priority.
builder = builder.WithInheritance(
config.Levels(config.Global, "groups", "replicasets", "instances"),
)
cfg, errs := builder.Build(t.Context())
require.Empty(t, errs)
eff, err := cfg.Effective(
config.NewKeyPath("groups/storages/replicasets/s-001/instances/s-001-a"))
require.NoError(t, err)
var mode string
m, err := eff.Get(config.NewKeyPath("mode"), &mode)
require.NoError(t, err)
assert.Equal(t, "from-top-global", mode,
"higher-priority top-level collector must beat the whole MultiCollector layer")
assert.Equal(t, "top", m.Source.Name)
}
// ---------------------------------------------------------------------------
// Cross-loader: arrays are opaque to map-merge (mergeTreeInto path)
// ---------------------------------------------------------------------------.
// TestLayered_CrossLoader_NestedArrayWholesaleReplace is the cross-loader
// twin of TestWithInheritance_CrossScope_NestedArrayShapes: arrays nested
// inside a deep-merged map must be replaced wholesale by the higher-priority
// loader, with no index-by-index merge that would leak orphan elements.
func TestLayered_CrossLoader_NestedArrayWholesaleReplace(t *testing.T) {
t.Parallel()
fileLoader := collectors.NewMap(map[string]any{
"iproto": map[string]any{
"listen": []any{
map[string]any{"uri": "file-1"},
map[string]any{"uri": "file-2"},
map[string]any{"uri": "file-3"},
},
},
"groups": map[string]any{
"storages": map[string]any{
"replicasets": map[string]any{
"s-001": map[string]any{
"instances": map[string]any{"s-001-a": map[string]any{}},
},
},
},
},
}).WithName("file").WithSourceType(config.FileSource)
envLoader := collectors.NewMap(map[string]any{
"iproto": map[string]any{
"listen": []any{
map[string]any{"uri": "env-1"},
},
},
}).WithName("env").WithSourceType(config.EnvSource)
builder := config.NewBuilder()
builder = builder.AddCollector(fileLoader) // lower priority.
builder = builder.AddCollector(envLoader) // higher priority.
builder = builder.WithInheritance(
config.Levels(config.Global, "groups", "replicasets", "instances"),
)
cfg, errs := builder.Build(t.Context())
require.Empty(t, errs)
eff, err := cfg.Effective(
config.NewKeyPath("groups/storages/replicasets/s-001/instances/s-001-a"))
require.NoError(t, err)
var listen []map[string]string
_, err = eff.Get(config.NewKeyPath("iproto/listen"), &listen)
require.NoError(t, err)
require.Len(t, listen, 1, "env loader's iproto.listen must replace file loader's wholesale")
assert.Equal(t, "env-1", listen[0]["uri"])
}
// TestLayered_CrossLoader_MapSiblingsPreservedWithArrayReplace verifies the
// combined invariant for cross-loader merging: sibling map sub-keys still
// deep-merge across loaders while sibling arrays still wholesale-replace.
func TestLayered_CrossLoader_MapSiblingsPreservedWithArrayReplace(t *testing.T) {
t.Parallel()
fileLoader := collectors.NewMap(map[string]any{
"iproto": map[string]any{
"advertise": map[string]any{
"peer": map[string]any{"login": "replicator"},
"sharding": map[string]any{"login": "storage"},
},
"listen": []any{
map[string]any{"uri": "file-1"},
map[string]any{"uri": "file-2"},
},
},
"groups": map[string]any{
"storages": map[string]any{
"replicasets": map[string]any{
"s-001": map[string]any{
"instances": map[string]any{"s-001-a": map[string]any{}},
},
},
},
},
}).WithName("file").WithSourceType(config.FileSource)
envLoader := collectors.NewMap(map[string]any{
"iproto": map[string]any{
"advertise": map[string]any{
"peer": map[string]any{"login": "replicator-env"},
},
"listen": []any{
map[string]any{"uri": "env-1"},
},
},
}).WithName("env").WithSourceType(config.EnvSource)
builder := config.NewBuilder()
builder = builder.AddCollector(fileLoader)
builder = builder.AddCollector(envLoader)
builder = builder.WithInheritance(
config.Levels(config.Global, "groups", "replicasets", "instances"),
)
cfg, errs := builder.Build(t.Context())
require.Empty(t, errs)
eff, err := cfg.Effective(
config.NewKeyPath("groups/storages/replicasets/s-001/instances/s-001-a"))
require.NoError(t, err)
// Map merge: env wins on the conflicting leaf, sibling sub-key from
// file loader survives.
var peerLogin string
_, err = eff.Get(config.NewKeyPath("iproto/advertise/peer/login"), &peerLogin)
require.NoError(t, err)
assert.Equal(t, "replicator-env", peerLogin)
var shardingLogin string
_, err = eff.Get(config.NewKeyPath("iproto/advertise/sharding/login"), &shardingLogin)
require.NoError(t, err)
assert.Equal(t, "storage", shardingLogin)
// Array replace: env fully replaces file, no leaked file[1].
var listen []map[string]string
_, err = eff.Get(config.NewKeyPath("iproto/listen"), &listen)
require.NoError(t, err)
require.Len(t, listen, 1)
assert.Equal(t, "env-1", listen[0]["uri"])
}
// TestLayered_CrossLoader_ArrayMapTypeMismatch exercises the type-boundary
// invariant across loaders: when one loader has an array and another has
// a map at the same path, the higher-priority loader wins wholesale.
func TestLayered_CrossLoader_ArrayMapTypeMismatch(t *testing.T) {
t.Parallel()
t.Run("ArrayLow_MapHigh", func(t *testing.T) {
t.Parallel()
fileLoader := collectors.NewMap(map[string]any{
"thing": []any{"f1", "f2"},
"groups": map[string]any{
"storages": map[string]any{
"replicasets": map[string]any{
"s-001": map[string]any{
"instances": map[string]any{"s-001-a": map[string]any{}},
},
},
},
},
}).WithName("file").WithSourceType(config.FileSource)
envLoader := collectors.NewMap(map[string]any{
"thing": map[string]any{"key": "value"},
}).WithName("env").WithSourceType(config.EnvSource)
builder := config.NewBuilder()
builder = builder.AddCollector(fileLoader)
builder = builder.AddCollector(envLoader)
builder = builder.WithInheritance(
config.Levels(config.Global, "groups", "replicasets", "instances"),
)
cfg, errs := builder.Build(t.Context())
require.Empty(t, errs)
eff, err := cfg.Effective(
config.NewKeyPath("groups/storages/replicasets/s-001/instances/s-001-a"))
require.NoError(t, err)
var got map[string]string
_, err = eff.Get(config.NewKeyPath("thing"), &got)
require.NoError(t, err)
assert.Equal(t, map[string]string{"key": "value"}, got)
})
t.Run("MapLow_ArrayHigh", func(t *testing.T) {
t.Parallel()
fileLoader := collectors.NewMap(map[string]any{
"thing": map[string]any{"a": 1, "b": 2},
"groups": map[string]any{
"storages": map[string]any{
"replicasets": map[string]any{
"s-001": map[string]any{
"instances": map[string]any{"s-001-a": map[string]any{}},
},
},
},
},
}).WithName("file").WithSourceType(config.FileSource)
envLoader := collectors.NewMap(map[string]any{
"thing": []any{"e1", "e2"},
}).WithName("env").WithSourceType(config.EnvSource)
builder := config.NewBuilder()
builder = builder.AddCollector(fileLoader)
builder = builder.AddCollector(envLoader)
builder = builder.WithInheritance(
config.Levels(config.Global, "groups", "replicasets", "instances"),
)
cfg, errs := builder.Build(t.Context())
require.Empty(t, errs)
eff, err := cfg.Effective(
config.NewKeyPath("groups/storages/replicasets/s-001/instances/s-001-a"))
require.NoError(t, err)
var got []any
_, err = eff.Get(config.NewKeyPath("thing"), &got)
require.NoError(t, err)
assert.Equal(t, []any{"e1", "e2"}, got)
_, err = eff.Get(config.NewKeyPath("thing/a"), new(int))
require.Error(t, err)
})
}
// ---------------------------------------------------------------------------
// Cross-loader: explicit MergeReplace means wholesale replace, both axes
// ---------------------------------------------------------------------------.
// TestLayered_CrossLoader_ExplicitMergeReplaceMap_WholesaleReplaces verifies
// that a user who explicitly opts into MergeReplace for a map key gets
// wholesale replace across loaders — exactly as they would within a single
// loader's scope chain. The previous accumulateLayerResult special-case
// silently recursed map merges for MergeReplace, contradicting both the
// constant's docstring and the matching scope-chain test
// TestWithInheritance_CrossScope_ExplicitMergeReplaceMap.
func TestLayered_CrossLoader_ExplicitMergeReplaceMap_WholesaleReplaces(t *testing.T) {
t.Parallel()
fileLoader := collectors.NewMap(map[string]any{
"credentials": map[string]any{
"users": map[string]any{
"admin": map[string]any{"password": "admin-file"},
"monitor": map[string]any{"password": "monitor-file"},
},
},
"groups": map[string]any{
"storages": map[string]any{
"replicasets": map[string]any{
"s-001": map[string]any{
"instances": map[string]any{"s-001-a": map[string]any{}},
},
},
},
},
}).WithName("file").WithSourceType(config.FileSource)
envLoader := collectors.NewMap(map[string]any{
"credentials": map[string]any{
"users": map[string]any{
"replicator": map[string]any{"password": "rep-env"},
},
},
}).WithName("env").WithSourceType(config.EnvSource)
builder := config.NewBuilder()
builder = builder.AddCollector(fileLoader) // lower priority.
builder = builder.AddCollector(envLoader) // higher priority.
builder = builder.WithInheritance(
config.Levels(config.Global, "groups", "replicasets", "instances"),
config.WithInheritMerge("credentials/users", config.MergeReplace),
)
cfg, errs := builder.Build(t.Context())
require.Empty(t, errs)
eff, err := cfg.Effective(
config.NewKeyPath("groups/storages/replicasets/s-001/instances/s-001-a"))
require.NoError(t, err)
var users map[string]any
_, err = eff.Get(config.NewKeyPath("credentials/users"), &users)
require.NoError(t, err)
require.Len(t, users, 1,
"explicit cross-loader MergeReplace must wholesale-replace the map, not recurse")
assert.Contains(t, users, "replicator")
assert.NotContains(t, users, "admin")
assert.NotContains(t, users, "monitor")
}
// TestLayered_CrossLoader_ExplicitMergeReplaceMatchesScopeChain pins the
// stronger invariant: the same explicit MergeReplace strategy produces an
// identical effective view whether the conflicting maps come from two
// scopes of one loader or two separate loaders. Catches regressions where
// the two paths drift apart again.
func TestLayered_CrossLoader_ExplicitMergeReplaceMatchesScopeChain(t *testing.T) {
t.Parallel()
build := func(crossLoader bool) config.Config {
t.Helper()
lowVal := map[string]any{
"users": map[string]any{
"admin": map[string]any{"password": "admin-low"},
"monitor": map[string]any{"password": "monitor-low"},
},
}
highVal := map[string]any{
"users": map[string]any{
"replicator": map[string]any{"password": "rep-high"},
},
}
builder := config.NewBuilder()
if crossLoader {
builder = builder.AddCollector(collectors.NewMap(map[string]any{
"credentials": lowVal,
"groups": map[string]any{
"storages": map[string]any{
"replicasets": map[string]any{
"s-001": map[string]any{
"instances": map[string]any{"s-001-a": map[string]any{}},
},
},
},
},
}).WithName("low").WithSourceType(config.FileSource))
builder = builder.AddCollector(collectors.NewMap(map[string]any{
"credentials": highVal,
}).WithName("high").WithSourceType(config.EnvSource))
} else {
builder = builder.AddCollector(collectors.NewMap(map[string]any{
"credentials": lowVal,
"groups": map[string]any{
"storages": map[string]any{
"replicasets": map[string]any{
"s-001": map[string]any{
"instances": map[string]any{
"s-001-a": map[string]any{
"credentials": highVal,
},
},
},
},
},
},
}).WithName("only").WithSourceType(config.FileSource))
}
builder = builder.WithInheritance(
config.Levels(config.Global, "groups", "replicasets", "instances"),
config.WithInheritMerge("credentials/users", config.MergeReplace),
)
cfg, errs := builder.Build(t.Context())
require.Empty(t, errs)
return cfg
}
for _, label := range []string{"single-loader-scope-chain", "cross-loader"} {
cfg := build(label == "cross-loader")
eff, err := cfg.Effective(
config.NewKeyPath("groups/storages/replicasets/s-001/instances/s-001-a"))
require.NoError(t, err, label)
var users map[string]any
_, err = eff.Get(config.NewKeyPath("credentials/users"), &users)
require.NoError(t, err, label)
require.Len(t, users, 1, label)
assert.Contains(t, users, "replicator", label)
assert.NotContains(t, users, "admin", label)
}
}
// TestLayered_CrossLoader_ExplicitReplaceLeavesDefaultsAlone verifies that
// using explicit MergeReplace on one specific key path does not bleed into
// sibling keys — those still use the default deep-merge across loaders.
// Catches a regression where removing the cross-loader MergeReplace
// special-case might accidentally degrade default behavior elsewhere.
func TestLayered_CrossLoader_ExplicitReplaceLeavesDefaultsAlone(t *testing.T) {
t.Parallel()
fileLoader := collectors.NewMap(map[string]any{
"credentials": map[string]any{
"users": map[string]any{
"admin": map[string]any{"password": "admin-file"},
},
"settings": map[string]any{
"timeout": 30,
"retries": 3,
},
},
"groups": map[string]any{
"storages": map[string]any{
"replicasets": map[string]any{
"s-001": map[string]any{
"instances": map[string]any{"s-001-a": map[string]any{}},
},
},
},
},
}).WithName("file").WithSourceType(config.FileSource)
envLoader := collectors.NewMap(map[string]any{
"credentials": map[string]any{
"users": map[string]any{
"replicator": map[string]any{"password": "rep-env"},
},
"settings": map[string]any{
"timeout": 60,
},
},
}).WithName("env").WithSourceType(config.EnvSource)
builder := config.NewBuilder()
builder = builder.AddCollector(fileLoader)
builder = builder.AddCollector(envLoader)
builder = builder.WithInheritance(
config.Levels(config.Global, "groups", "replicasets", "instances"),
config.WithInheritMerge("credentials/users", config.MergeReplace),
)
cfg, errs := builder.Build(t.Context())
require.Empty(t, errs)
eff, err := cfg.Effective(
config.NewKeyPath("groups/storages/replicasets/s-001/instances/s-001-a"))
require.NoError(t, err)
// credentials/users — explicit MergeReplace: only env's contribution.
var users map[string]any
_, err = eff.Get(config.NewKeyPath("credentials/users"), &users)
require.NoError(t, err)
require.Len(t, users, 1)
assert.Contains(t, users, "replicator")
assert.NotContains(t, users, "admin")
// credentials/settings — default (MergeDeep): both timeout (env override)
// and retries (file only) must survive.
var timeout, retries int
_, err = eff.Get(config.NewKeyPath("credentials/settings/timeout"), &timeout)
require.NoError(t, err)
assert.Equal(t, 60, timeout, "env wins on conflict for default-strategy leaf")
_, err = eff.Get(config.NewKeyPath("credentials/settings/retries"), &retries)
require.NoError(t, err, "file's retries sibling must survive default deep merge")
assert.Equal(t, 3, retries)
}
// TestLayered_CrossLoader_ExplicitMergeReplaceTypeMismatch exercises the
// explicit-MergeReplace path with incompatible shapes (map ↔ array, map ↔
// scalar) across loaders. The higher-priority loader always wins
// wholesale, with no attempt to merge incompatible nodes.
func TestLayered_CrossLoader_ExplicitMergeReplaceTypeMismatch(t *testing.T) {
t.Parallel()
t.Run("MapLow_ArrayHigh", func(t *testing.T) {
t.Parallel()
fileLoader := collectors.NewMap(map[string]any{
"thing": map[string]any{"a": 1, "b": 2},
"groups": map[string]any{
"storages": map[string]any{
"replicasets": map[string]any{
"s-001": map[string]any{
"instances": map[string]any{"s-001-a": map[string]any{}},
},
},
},
},
}).WithName("file").WithSourceType(config.FileSource)
envLoader := collectors.NewMap(map[string]any{
"thing": []any{"e1", "e2"},
}).WithName("env").WithSourceType(config.EnvSource)
builder := config.NewBuilder()