-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathconfig_provider_temp_config_test.go
More file actions
948 lines (745 loc) · 28.8 KB
/
config_provider_temp_config_test.go
File metadata and controls
948 lines (745 loc) · 28.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
package modular
import (
"reflect"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func Test_createTempConfig(t *testing.T) {
t.Parallel()
t.Run("with pointer", func(t *testing.T) {
originalCfg := &testCfg{Str: "test", Num: 42}
tempCfg, info, err := createTempConfig(originalCfg)
require.NoError(t, err)
require.NotNil(t, tempCfg)
assert.True(t, info.isPtr)
assert.Equal(t, reflect.ValueOf(originalCfg).Type(), info.tempVal.Type())
})
t.Run("with non-pointer", func(t *testing.T) {
originalCfg := testCfg{Str: "test", Num: 42}
tempCfg, info, err := createTempConfig(originalCfg)
require.NoError(t, err)
require.NotNil(t, tempCfg)
assert.False(t, info.isPtr)
assert.Equal(t, reflect.PointerTo(reflect.ValueOf(originalCfg).Type()), info.tempVal.Type())
})
t.Run("maps and slices are shallow copied (default behavior)", func(t *testing.T) {
type ConfigWithMaps struct {
Name string
Settings map[string]string
Tags []string
}
// Create an original config with initialized maps and slices
originalCfg := &ConfigWithMaps{
Name: "original",
Settings: map[string]string{
"key1": "value1",
"key2": "value2",
},
Tags: []string{"tag1", "tag2"},
}
// Create temp config
tempCfg, info, err := createTempConfig(originalCfg)
require.NoError(t, err)
require.NotNil(t, tempCfg)
tempCfgTyped := tempCfg.(*ConfigWithMaps)
// Verify initial values are copied
assert.Equal(t, "original", tempCfgTyped.Name)
assert.Equal(t, "value1", tempCfgTyped.Settings["key1"])
assert.Equal(t, "tag1", tempCfgTyped.Tags[0])
// Modify the temp config's maps and slices
tempCfgTyped.Settings["key1"] = "MODIFIED"
tempCfgTyped.Settings["newkey"] = "newvalue"
tempCfgTyped.Tags[0] = "MODIFIED"
// IMPORTANT: createTempConfig does a SHALLOW copy using reflect.Value.Set()
// This means maps and slices are shared between original and temp
assert.Equal(t, "MODIFIED", originalCfg.Settings["key1"],
"Original config's map IS affected by temp config modifications (shallow copy)")
assert.Contains(t, originalCfg.Settings, "newkey",
"Original config's map IS affected by temp config modifications (shallow copy)")
assert.Equal(t, "MODIFIED", originalCfg.Tags[0],
"Original config's slice IS affected by temp config modifications (shallow copy)")
// Verify the info struct is correct
assert.True(t, info.isPtr)
})
t.Run("deep copy isolates maps and slices (createTempConfigDeep)", func(t *testing.T) {
type ConfigWithMaps struct {
Name string
Settings map[string]string
Tags []string
}
// Create an original config with initialized maps and slices
originalCfg := &ConfigWithMaps{
Name: "original",
Settings: map[string]string{
"key1": "value1",
"key2": "value2",
},
Tags: []string{"tag1", "tag2"},
}
// Create temp config using DEEP copy
tempCfg, info, err := createTempConfigDeep(originalCfg)
require.NoError(t, err)
require.NotNil(t, tempCfg)
tempCfgTyped := tempCfg.(*ConfigWithMaps)
// Verify initial values are copied
assert.Equal(t, "original", tempCfgTyped.Name)
assert.Equal(t, "value1", tempCfgTyped.Settings["key1"])
assert.Equal(t, "tag1", tempCfgTyped.Tags[0])
// Modify the temp config's maps and slices
tempCfgTyped.Settings["key1"] = "MODIFIED"
tempCfgTyped.Settings["newkey"] = "newvalue"
tempCfgTyped.Tags[0] = "MODIFIED"
// Deep copy ensures isolation - original should NOT be affected
assert.Equal(t, "value1", originalCfg.Settings["key1"],
"Original config's map should NOT be affected (deep copy)")
assert.NotContains(t, originalCfg.Settings, "newkey",
"Original config's map should NOT be affected (deep copy)")
assert.Equal(t, "tag1", originalCfg.Tags[0],
"Original config's slice should NOT be affected (deep copy)")
// Verify the info struct is correct
assert.True(t, info.isPtr)
})
}
func Test_updateConfig(t *testing.T) {
t.Parallel()
t.Run("with pointer config", func(t *testing.T) {
originalCfg := &testCfg{Str: "old", Num: 0}
tempCfg := &testCfg{Str: "new", Num: 42}
mockLogger := new(MockLogger)
app := &StdApplication{logger: mockLogger}
origInfo := configInfo{
originalVal: reflect.ValueOf(originalCfg),
tempVal: reflect.ValueOf(tempCfg),
isPtr: true,
}
updateConfig(app, origInfo)
// Check the original config was updated
assert.Equal(t, "new", originalCfg.Str)
assert.Equal(t, 42, originalCfg.Num)
})
t.Run("with non-pointer config", func(t *testing.T) {
originalCfg := testCfg{Str: "old", Num: 0}
tempCfgPtr, origInfo, err := createTempConfig(originalCfg)
require.NoError(t, err)
tempCfgPtr.(*testCfg).Str = "new"
tempCfgPtr.(*testCfg).Num = 42
mockLogger := new(MockLogger)
mockLogger.On("Debug",
"Creating new provider with updated config (original was non-pointer)",
[]any(nil)).Return()
app := &StdApplication{
logger: mockLogger,
cfgProvider: NewStdConfigProvider(originalCfg),
}
updateConfig(app, origInfo)
// Check the updated provider from the app (not the original provider reference)
updated := app.cfgProvider.GetConfig()
assert.Equal(t, reflect.Struct, reflect.ValueOf(updated).Kind())
assert.Equal(t, "new", updated.(testCfg).Str)
assert.Equal(t, 42, updated.(testCfg).Num)
mockLogger.AssertExpectations(t)
})
}
func Test_updateSectionConfig(t *testing.T) {
t.Parallel()
t.Run("with pointer section config", func(t *testing.T) {
originalCfg := &testSectionCfg{Enabled: false, Name: "old"}
tempCfg := &testSectionCfg{Enabled: true, Name: "new"}
mockLogger := new(MockLogger)
app := &StdApplication{
logger: mockLogger,
cfgSections: make(map[string]ConfigProvider),
}
app.cfgSections["test"] = NewStdConfigProvider(originalCfg)
sectionInfo := configInfo{
originalVal: reflect.ValueOf(originalCfg),
tempVal: reflect.ValueOf(tempCfg),
isPtr: true,
}
updateSectionConfig(app, "test", sectionInfo)
// Check the original config was updated
assert.True(t, originalCfg.Enabled)
assert.Equal(t, "new", originalCfg.Name)
})
t.Run("with non-pointer section config", func(t *testing.T) {
originalCfg := testSectionCfg{Enabled: false, Name: "old"}
tempCfgPtr, sectionInfo, err := createTempConfig(originalCfg)
require.NoError(t, err)
// Cast and update the temp config
tempCfgPtr.(*testSectionCfg).Enabled = true
tempCfgPtr.(*testSectionCfg).Name = "new"
mockLogger := new(MockLogger)
mockLogger.On("Debug", "Creating new provider for section", []any{"section", "test"}).Return()
app := &StdApplication{
logger: mockLogger,
cfgSections: make(map[string]ConfigProvider),
}
app.cfgSections["test"] = NewStdConfigProvider(originalCfg)
updateSectionConfig(app, "test", sectionInfo)
// Check a new provider was created
sectCfg := app.cfgSections["test"].GetConfig()
assert.True(t, sectCfg.(testSectionCfg).Enabled)
assert.Equal(t, "new", sectCfg.(testSectionCfg).Name)
mockLogger.AssertExpectations(t)
})
}
// TestDeepCopyValue_Maps tests deep copying of maps
func TestDeepCopyValue_Maps(t *testing.T) {
t.Parallel()
t.Run("simple map of strings", func(t *testing.T) {
src := map[string]string{
"key1": "value1",
"key2": "value2",
}
dst := reflect.New(reflect.TypeOf(src)).Elem()
deepCopyValue(dst, reflect.ValueOf(src))
dstMap := dst.Interface().(map[string]string)
assert.Equal(t, "value1", dstMap["key1"])
assert.Equal(t, "value2", dstMap["key2"])
// Verify it's a deep copy by modifying the source
src["key1"] = "modified"
assert.Equal(t, "value1", dstMap["key1"], "Destination should not be affected by source modification")
})
t.Run("map with string slice values", func(t *testing.T) {
src := map[string][]string{
"list1": {"a", "b", "c"},
"list2": {"x", "y", "z"},
}
dst := reflect.New(reflect.TypeOf(src)).Elem()
deepCopyValue(dst, reflect.ValueOf(src))
dstMap := dst.Interface().(map[string][]string)
assert.Equal(t, []string{"a", "b", "c"}, dstMap["list1"])
assert.Equal(t, []string{"x", "y", "z"}, dstMap["list2"])
// Verify it's a deep copy by modifying the source slice
src["list1"][0] = "modified"
assert.Equal(t, "a", dstMap["list1"][0], "Destination slice should not be affected")
})
t.Run("nested maps", func(t *testing.T) {
src := map[string]map[string]int{
"group1": {"a": 1, "b": 2},
"group2": {"x": 10, "y": 20},
}
dst := reflect.New(reflect.TypeOf(src)).Elem()
deepCopyValue(dst, reflect.ValueOf(src))
dstMap := dst.Interface().(map[string]map[string]int)
assert.Equal(t, 1, dstMap["group1"]["a"])
assert.Equal(t, 20, dstMap["group2"]["y"])
// Verify it's a deep copy
src["group1"]["a"] = 999
assert.Equal(t, 1, dstMap["group1"]["a"], "Nested map should not be affected")
})
t.Run("nil map", func(t *testing.T) {
var src map[string]string = nil
dst := reflect.New(reflect.TypeFor[map[string]string]()).Elem()
deepCopyValue(dst, reflect.ValueOf(src))
// For nil map, deepCopyValue returns early without modifying dst
// dst remains as zero value which is nil for maps
assert.True(t, !dst.IsValid() || dst.IsNil(), "Destination should remain nil for nil source")
})
}
// TestDeepCopyValue_Slices tests deep copying of slices
func TestDeepCopyValue_Slices(t *testing.T) {
t.Parallel()
t.Run("simple slice of integers", func(t *testing.T) {
src := []int{1, 2, 3, 4, 5}
dst := reflect.New(reflect.TypeFor[[]int]()).Elem()
deepCopyValue(dst, reflect.ValueOf(src))
dstSlice := dst.Interface().([]int)
assert.Equal(t, []int{1, 2, 3, 4, 5}, dstSlice)
// Verify it's a deep copy
src[0] = 999
assert.Equal(t, 1, dstSlice[0], "Destination should not be affected")
})
t.Run("slice of strings", func(t *testing.T) {
src := []string{"hello", "world"}
dst := reflect.New(reflect.TypeFor[[]string]()).Elem()
deepCopyValue(dst, reflect.ValueOf(src))
dstSlice := dst.Interface().([]string)
assert.Equal(t, []string{"hello", "world"}, dstSlice)
src[0] = "modified"
assert.Equal(t, "hello", dstSlice[0])
})
t.Run("slice of maps", func(t *testing.T) {
src := []map[string]int{
{"a": 1, "b": 2},
{"x": 10, "y": 20},
}
dst := reflect.New(reflect.TypeOf(src)).Elem()
deepCopyValue(dst, reflect.ValueOf(src))
dstSlice := dst.Interface().([]map[string]int)
assert.Equal(t, 1, dstSlice[0]["a"])
assert.Equal(t, 20, dstSlice[1]["y"])
// Verify it's a deep copy
src[0]["a"] = 999
assert.Equal(t, 1, dstSlice[0]["a"], "Nested map in slice should not be affected")
})
t.Run("nil slice", func(t *testing.T) {
var src []string = nil
dst := reflect.New(reflect.TypeFor[[]string]()).Elem()
deepCopyValue(dst, reflect.ValueOf(src))
// For nil slice, deepCopyValue returns early without modifying dst
// dst remains as zero value which is nil for slices
assert.True(t, !dst.IsValid() || dst.IsNil(), "Destination should remain nil for nil source")
})
}
// TestDeepCopyValue_Pointers tests deep copying of pointers
func TestDeepCopyValue_Pointers(t *testing.T) {
t.Parallel()
t.Run("pointer to string", func(t *testing.T) {
str := "original"
src := &str
dst := reflect.New(reflect.TypeFor[*string]()).Elem()
deepCopyValue(dst, reflect.ValueOf(src))
dstPtr := dst.Interface().(*string)
assert.Equal(t, "original", *dstPtr)
// Verify it's a deep copy
*src = "modified"
assert.Equal(t, "original", *dstPtr, "Destination should not be affected")
})
t.Run("pointer to struct", func(t *testing.T) {
type TestStruct struct {
Name string
Value int
}
src := &TestStruct{Name: "test", Value: 42}
dst := reflect.New(reflect.TypeFor[*TestStruct]()).Elem()
deepCopyValue(dst, reflect.ValueOf(src))
dstPtr := dst.Interface().(*TestStruct)
assert.Equal(t, "test", dstPtr.Name)
assert.Equal(t, 42, dstPtr.Value)
// Verify it's a deep copy
src.Name = "modified"
assert.Equal(t, "test", dstPtr.Name)
})
t.Run("nil pointer", func(t *testing.T) {
var src *string = nil
dst := reflect.New(reflect.TypeFor[*string]()).Elem()
deepCopyValue(dst, reflect.ValueOf(src))
// For nil pointer, deepCopyValue returns early without modifying dst
// dst remains as zero value which is nil for pointers
assert.True(t, !dst.IsValid() || dst.IsNil(), "Destination should remain nil for nil source")
})
}
// TestDeepCopyValue_Structs tests deep copying of structs
func TestDeepCopyValue_Structs(t *testing.T) {
t.Parallel()
t.Run("simple struct", func(t *testing.T) {
type SimpleStruct struct {
Name string
Age int
}
src := SimpleStruct{Name: "John", Age: 30}
dst := reflect.New(reflect.TypeFor[SimpleStruct]()).Elem()
deepCopyValue(dst, reflect.ValueOf(src))
dstStruct := dst.Interface().(SimpleStruct)
assert.Equal(t, "John", dstStruct.Name)
assert.Equal(t, 30, dstStruct.Age)
})
t.Run("struct with map field", func(t *testing.T) {
type ConfigStruct struct {
Name string
Settings map[string]string
}
src := ConfigStruct{
Name: "config1",
Settings: map[string]string{"key1": "value1", "key2": "value2"},
}
dst := reflect.New(reflect.TypeFor[ConfigStruct]()).Elem()
deepCopyValue(dst, reflect.ValueOf(src))
dstStruct := dst.Interface().(ConfigStruct)
assert.Equal(t, "config1", dstStruct.Name)
assert.Equal(t, "value1", dstStruct.Settings["key1"])
// Verify it's a deep copy - THIS TESTS THE KEY BUG FIX
src.Settings["key1"] = "modified"
assert.Equal(t, "value1", dstStruct.Settings["key1"], "Map in struct should not be affected")
})
t.Run("struct with slice field", func(t *testing.T) {
type ListStruct struct {
Name string
Items []string
}
src := ListStruct{
Name: "list1",
Items: []string{"a", "b", "c"},
}
dst := reflect.New(reflect.TypeFor[ListStruct]()).Elem()
deepCopyValue(dst, reflect.ValueOf(src))
dstStruct := dst.Interface().(ListStruct)
assert.Equal(t, "list1", dstStruct.Name)
assert.Equal(t, []string{"a", "b", "c"}, dstStruct.Items)
// Verify it's a deep copy
src.Items[0] = "modified"
assert.Equal(t, "a", dstStruct.Items[0], "Slice in struct should not be affected")
})
t.Run("nested struct", func(t *testing.T) {
type InnerStruct struct {
Value int
}
type OuterStruct struct {
Name string
Inner InnerStruct
}
src := OuterStruct{
Name: "outer",
Inner: InnerStruct{Value: 42},
}
dst := reflect.New(reflect.TypeFor[OuterStruct]()).Elem()
deepCopyValue(dst, reflect.ValueOf(src))
dstStruct := dst.Interface().(OuterStruct)
assert.Equal(t, "outer", dstStruct.Name)
assert.Equal(t, 42, dstStruct.Inner.Value)
})
t.Run("struct with unexported fields", func(t *testing.T) {
type StructWithPrivate struct {
Public string
private int
}
src := StructWithPrivate{Public: "visible", private: 42}
dst := reflect.New(reflect.TypeOf(src)).Elem()
// Should not panic even with unexported fields
require.NotPanics(t, func() {
deepCopyValue(dst, reflect.ValueOf(src))
})
dstStruct := dst.Interface().(StructWithPrivate)
assert.Equal(t, "visible", dstStruct.Public)
})
}
// TestDeepCopyValue_BasicTypes tests deep copying of basic types
func TestDeepCopyValue_BasicTypes(t *testing.T) {
t.Parallel()
tests := []struct {
name string
value any
}{
{"int", 42},
{"int64", int64(123456789)},
{"float64", 3.14159},
{"string", "hello world"},
{"bool", true},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
src := reflect.ValueOf(tt.value)
dst := reflect.New(src.Type()).Elem()
deepCopyValue(dst, src)
assert.Equal(t, tt.value, dst.Interface())
})
}
}
// TestDeepCopyValue_ComplexStructures tests deep copying of complex nested structures
func TestDeepCopyValue_ComplexStructures(t *testing.T) {
t.Parallel()
type ComplexConfig struct {
Name string
BackendServices map[string]string
Features map[string]bool
AllowedIPs []string
}
src := ComplexConfig{
Name: "tenant1",
BackendServices: map[string]string{
"api": "https://api.example.com",
"legacy": "https://legacy.example.com",
},
Features: map[string]bool{
"feature1": true,
"feature2": false,
},
AllowedIPs: []string{"192.168.1.1", "10.0.0.1"},
}
dst := reflect.New(reflect.TypeFor[ComplexConfig]()).Elem()
deepCopyValue(dst, reflect.ValueOf(src))
dstConfig := dst.Interface().(ComplexConfig)
// Verify all fields copied correctly
assert.Equal(t, "tenant1", dstConfig.Name)
assert.Equal(t, "https://api.example.com", dstConfig.BackendServices["api"])
assert.Equal(t, "https://legacy.example.com", dstConfig.BackendServices["legacy"])
assert.True(t, dstConfig.Features["feature1"])
assert.False(t, dstConfig.Features["feature2"])
assert.Equal(t, []string{"192.168.1.1", "10.0.0.1"}, dstConfig.AllowedIPs)
// Verify deep copy by modifying source
src.BackendServices["api"] = "https://modified.example.com"
src.Features["feature1"] = false
src.AllowedIPs[0] = "1.1.1.1"
// Destination should NOT be affected (isolation is preserved)
assert.Equal(t, "https://api.example.com", dstConfig.BackendServices["api"], "BackendServices map should be deep copied")
assert.True(t, dstConfig.Features["feature1"], "Features map should be deep copied")
assert.Equal(t, "192.168.1.1", dstConfig.AllowedIPs[0], "AllowedIPs slice should be deep copied")
}
// TestDeepCopyValue_Arrays tests deep copying of fixed-size arrays
func TestDeepCopyValue_Arrays(t *testing.T) {
t.Parallel()
t.Run("array of integers", func(t *testing.T) {
src := [5]int{1, 2, 3, 4, 5}
dst := reflect.New(reflect.TypeFor[[5]int]()).Elem()
deepCopyValue(dst, reflect.ValueOf(src))
dstArray := dst.Interface().([5]int)
assert.Equal(t, [5]int{1, 2, 3, 4, 5}, dstArray)
// Arrays are value types in Go, but let's verify the copy works
src[0] = 999
assert.Equal(t, 1, dstArray[0], "Destination array should not be affected")
})
t.Run("array of strings", func(t *testing.T) {
src := [3]string{"foo", "bar", "baz"}
dst := reflect.New(reflect.TypeFor[[3]string]()).Elem()
deepCopyValue(dst, reflect.ValueOf(src))
dstArray := dst.Interface().([3]string)
assert.Equal(t, [3]string{"foo", "bar", "baz"}, dstArray)
src[1] = "modified"
assert.Equal(t, "bar", dstArray[1])
})
t.Run("array of pointers", func(t *testing.T) {
str1, str2 := "value1", "value2"
src := [2]*string{&str1, &str2}
dst := reflect.New(reflect.TypeFor[[2]*string]()).Elem()
deepCopyValue(dst, reflect.ValueOf(src))
dstArray := dst.Interface().([2]*string)
assert.Equal(t, "value1", *dstArray[0])
assert.Equal(t, "value2", *dstArray[1])
// Verify deep copy - modifying source pointer values shouldn't affect destination
*src[0] = "modified"
assert.Equal(t, "value1", *dstArray[0], "Array of pointers should be deep copied")
})
}
// TestDeepCopyValue_Interfaces tests deep copying of interface values
func TestDeepCopyValue_Interfaces(t *testing.T) {
t.Parallel()
t.Run("interface with concrete string", func(t *testing.T) {
var src any = "hello"
dst := reflect.New(reflect.TypeOf(src)).Elem()
deepCopyValue(dst, reflect.ValueOf(src))
dstValue := dst.Interface()
assert.Equal(t, "hello", dstValue)
})
t.Run("interface with concrete map", func(t *testing.T) {
var src any = map[string]int{"a": 1, "b": 2}
dst := reflect.New(reflect.TypeOf(src)).Elem()
deepCopyValue(dst, reflect.ValueOf(src))
dstValue := dst.Interface().(map[string]int)
assert.Equal(t, 1, dstValue["a"])
assert.Equal(t, 2, dstValue["b"])
// Verify it's a deep copy
srcMap := src.(map[string]int)
srcMap["a"] = 999
assert.Equal(t, 1, dstValue["a"], "Interface containing map should be deep copied")
})
t.Run("struct with interface field", func(t *testing.T) {
type ConfigWithInterface struct {
Name string
Data any
}
src := ConfigWithInterface{
Name: "test",
Data: map[string]int{"count": 42},
}
dst := reflect.New(reflect.TypeOf(src)).Elem()
deepCopyValue(dst, reflect.ValueOf(src))
dstValue := dst.Interface().(ConfigWithInterface)
assert.Equal(t, "test", dstValue.Name)
assert.Equal(t, 42, dstValue.Data.(map[string]int)["count"])
// Verify deep copy
srcMap := src.Data.(map[string]int)
srcMap["count"] = 999
assert.Equal(t, 42, dstValue.Data.(map[string]int)["count"], "Interface field containing map should be deep copied")
})
t.Run("struct with nil interface field", func(t *testing.T) {
type ConfigWithInterface struct {
Name string
Data any
}
src := ConfigWithInterface{
Name: "test",
Data: nil,
}
dst := reflect.New(reflect.TypeOf(src)).Elem()
deepCopyValue(dst, reflect.ValueOf(src))
dstValue := dst.Interface().(ConfigWithInterface)
assert.Equal(t, "test", dstValue.Name)
assert.Nil(t, dstValue.Data, "Nil interface field should remain nil")
})
t.Run("interface with struct", func(t *testing.T) {
type TestStruct struct {
Value int
Data map[string]string
}
var src any = TestStruct{
Value: 42,
Data: map[string]string{"key": "value"},
}
dst := reflect.New(reflect.TypeOf(src)).Elem()
deepCopyValue(dst, reflect.ValueOf(src))
dstValue := dst.Interface().(TestStruct)
assert.Equal(t, 42, dstValue.Value)
assert.Equal(t, "value", dstValue.Data["key"])
// Verify deep copy of the map inside the struct
srcStruct := src.(TestStruct)
srcStruct.Data["key"] = "modified"
assert.Equal(t, "value", dstValue.Data["key"], "Interface with struct containing map should be deep copied")
})
}
// TestDeepCopyValue_Channels tests copying of channels (by reference)
func TestDeepCopyValue_Channels(t *testing.T) {
t.Parallel()
t.Run("channel of integers", func(t *testing.T) {
src := make(chan int, 2)
src <- 42
src <- 100
dst := reflect.New(reflect.TypeFor[chan int]()).Elem()
deepCopyValue(dst, reflect.ValueOf(src))
dstChan := dst.Interface().(chan int)
// Channels are copied by reference, so they should be the same channel
assert.Equal(t, 42, <-dstChan, "Channel should be copied by reference")
assert.Equal(t, 100, <-dstChan, "Channel should be copied by reference")
})
t.Run("nil channel", func(t *testing.T) {
var src chan string = nil
dst := reflect.New(reflect.TypeFor[chan string]()).Elem()
deepCopyValue(dst, reflect.ValueOf(src))
dstChan := dst.Interface().(chan string)
assert.Nil(t, dstChan, "Nil channel should remain nil")
})
}
// TestDeepCopyValue_Functions tests copying of functions (by reference)
func TestDeepCopyValue_Functions(t *testing.T) {
t.Parallel()
t.Run("function value", func(t *testing.T) {
callCount := 0
src := func(x int) int {
callCount++
return x * 2
}
dst := reflect.New(reflect.TypeOf(src)).Elem()
deepCopyValue(dst, reflect.ValueOf(src))
dstFunc := dst.Interface().(func(int) int)
// Functions are copied by reference, so calling either increments the same counter
assert.Equal(t, 10, dstFunc(5))
assert.Equal(t, 1, callCount, "Function should be copied by reference")
assert.Equal(t, 20, src(10))
assert.Equal(t, 2, callCount, "Both function references share state")
})
t.Run("nil function", func(t *testing.T) {
var src func(int) int = nil
dst := reflect.New(reflect.TypeOf(src)).Elem()
deepCopyValue(dst, reflect.ValueOf(src))
dstFunc := dst.Interface().(func(int) int)
assert.Nil(t, dstFunc, "Nil function should remain nil")
})
}
// TestDeepCopyValue_Invalid tests handling of invalid reflect values
func TestDeepCopyValue_Invalid(t *testing.T) {
t.Parallel()
t.Run("invalid value", func(t *testing.T) {
var src reflect.Value // Invalid (zero value)
dst := reflect.New(reflect.TypeFor[string]()).Elem()
// Should not panic
require.NotPanics(t, func() {
deepCopyValue(dst, src)
})
})
}
// TestTenantConfigIsolation tests that tenant configurations are properly isolated
// and do not share map references when copied (the bug that was fixed in commit dc66902)
func TestTenantConfigIsolation(t *testing.T) {
t.Parallel()
// This test simulates the actual bug scenario:
// 1. A config struct is created with initialized maps
// 2. That config is copied using createTempConfig (which happens during tenant loading)
// 3. If shallow copy is used (the bug), modifying one copy affects the other
// Create a base config with maps - this simulates a default/template config
baseConfig := &TestTenantConfig{
Name: "BaseConfig",
Environment: "default",
Features: map[string]bool{
"feature1": true,
"feature2": false,
},
}
// Simulate what happens when loading tenant configs:
// Use createTempConfigDeep to make an ISOLATED copy (fixes the bug)
// NOTE: The original bug used createTempConfig which did shallow copy
tenantACfg, _, err := createTempConfigDeep(baseConfig)
require.NoError(t, err)
tenantATyped := tenantACfg.(*TestTenantConfig)
tenantBCfg, _, err := createTempConfigDeep(baseConfig)
require.NoError(t, err)
tenantBTyped := tenantBCfg.(*TestTenantConfig)
// At this point, both tenants have copies of the base config
// Verify initial state
assert.Equal(t, "BaseConfig", tenantATyped.Name)
assert.Equal(t, "BaseConfig", tenantBTyped.Name)
assert.True(t, tenantATyped.Features["feature1"])
assert.True(t, tenantBTyped.Features["feature1"])
// THE CRITICAL TEST: Modify tenant A's map
tenantATyped.Features["feature1"] = false
tenantATyped.Features["new_feature_A"] = true
tenantATyped.Name = "TenantA"
// Modify tenant B's map
tenantBTyped.Features["feature2"] = true
tenantBTyped.Features["new_feature_B"] = true
tenantBTyped.Name = "TenantB"
// If deep copy is working:
// - TenantA and TenantB should have independent maps
// - Changes to one should NOT affect the other
// - The base config should also remain unchanged
// If shallow copy (the bug):
// - All three would share the same map reference
// - Changes to any would affect all
// Verify tenant A has its own changes
assert.False(t, tenantATyped.Features["feature1"], "TenantA should have feature1=false")
assert.True(t, tenantATyped.Features["new_feature_A"], "TenantA should have new_feature_A")
assert.False(t, tenantATyped.Features["new_feature_B"], "TenantA should NOT have TenantB's features")
assert.Equal(t, "TenantA", tenantATyped.Name)
// Verify tenant B has its own changes
assert.True(t, tenantBTyped.Features["feature1"], "TenantB should still have feature1=true (unaffected by TenantA)")
assert.True(t, tenantBTyped.Features["feature2"], "TenantB should have feature2=true")
assert.True(t, tenantBTyped.Features["new_feature_B"], "TenantB should have new_feature_B")
assert.False(t, tenantBTyped.Features["new_feature_A"], "TenantB should NOT have TenantA's features")
assert.Equal(t, "TenantB", tenantBTyped.Name)
// Verify base config remains unchanged
assert.Equal(t, "BaseConfig", baseConfig.Name, "Base config name should be unchanged")
assert.True(t, baseConfig.Features["feature1"], "Base config should still have feature1=true")
assert.False(t, baseConfig.Features["feature2"], "Base config should still have feature2=false")
assert.False(t, baseConfig.Features["new_feature_A"], "Base config should NOT have TenantA's features")
assert.False(t, baseConfig.Features["new_feature_B"], "Base config should NOT have TenantB's features")
t.Log("SUCCESS: Tenant configurations are properly isolated via deep copy - no shared map references")
}
// TestTenantConfigIsolation_ShallowCopyBug demonstrates the bug that occurs with shallow copy
// This test intentionally uses createTempConfig (shallow copy) to show the problematic behavior
func TestTenantConfigIsolation_ShallowCopyBug(t *testing.T) {
t.Parallel()
// This test demonstrates the BUG: when using createTempConfig (shallow copy),
// all configs share the same map reference
baseConfig := &TestTenantConfig{
Name: "BaseConfig",
Environment: "default",
Features: map[string]bool{
"feature1": true,
"feature2": false,
},
}
// Using createTempConfig (shallow copy) - this is the bug
tenantACfg, _, err := createTempConfig(baseConfig)
require.NoError(t, err)
tenantATyped := tenantACfg.(*TestTenantConfig)
tenantBCfg, _, err := createTempConfig(baseConfig)
require.NoError(t, err)
tenantBTyped := tenantBCfg.(*TestTenantConfig)
// Modify tenant A's map
tenantATyped.Features["feature1"] = false
tenantATyped.Features["new_feature_A"] = true
// THE BUG: Because of shallow copy, modifying tenantA's map also affects:
// 1. tenantB's map (they share the same reference)
// 2. baseConfig's map (they all share the same reference)
// Demonstrate the bug: tenantB sees tenantA's changes (WRONG!)
assert.False(t, tenantBTyped.Features["feature1"],
"BUG: TenantB sees TenantA's changes because they share the map reference")
assert.True(t, tenantBTyped.Features["new_feature_A"],
"BUG: TenantB has TenantA's features because they share the map reference")
// Demonstrate the bug: baseConfig sees tenantA's changes (WRONG!)
assert.False(t, baseConfig.Features["feature1"],
"BUG: Base config was modified by TenantA because they share the map reference")
assert.True(t, baseConfig.Features["new_feature_A"],
"BUG: Base config has TenantA's features because they share the map reference")
t.Log("BUG DEMONSTRATED: Shallow copy causes tenant configs to share map references")
}