-
-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathstack_processor_merge_test.go
More file actions
1676 lines (1539 loc) · 64.5 KB
/
Copy pathstack_processor_merge_test.go
File metadata and controls
1676 lines (1539 loc) · 64.5 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 exec
import (
"encoding/json"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
errUtils "github.com/cloudposse/atmos/errors"
cfg "github.com/cloudposse/atmos/pkg/config"
m "github.com/cloudposse/atmos/pkg/merge"
"github.com/cloudposse/atmos/pkg/schema"
)
// Compile-time sentinels: if these fields are renamed the build breaks, preventing
// tests that rely on specific field names from silently passing with zero values.
var _ = schema.AtmosSettings{ListMergeStrategy: ""}
// TestMergeComponentConfigurations verifies that mergeComponentConfigurations
// correctly assembles the final component configuration from layered inputs
// (global, base-component, component, and overrides) for both Terraform and
// Helmfile component types.
func TestMergeComponentConfigurations(t *testing.T) {
tests := []struct {
name string
opts ComponentProcessorOptions
result *ComponentProcessorResult
expectedVars map[string]any
expectedSettings map[string]any
expectedEnv map[string]any
expectedAuth map[string]any
expectedCommand string
expectedProviders map[string]any
expectedHooks map[string]any
checkBaseComponent bool
expectedBaseComponent string
}{
{
name: "terraform component with all fields",
opts: ComponentProcessorOptions{
ComponentType: cfg.TerraformComponentType,
Component: "vpc",
GlobalVars: map[string]any{
"global_var": "global",
},
GlobalSettings: map[string]any{
"global_setting": true,
},
GlobalEnv: map[string]any{
"GLOBAL_ENV": "value",
},
GlobalAuth: map[string]any{
"aws": map[string]any{
"profile": "global-profile",
},
},
GlobalCommand: "terraform",
TerraformProviders: map[string]any{
"aws": map[string]any{
"region": "us-east-1",
},
},
GlobalAndTerraformHooks: map[string]any{
"before": []string{"global hook"},
},
GlobalBackendType: "s3",
GlobalBackendSection: map[string]any{
"s3": map[string]any{
"bucket": "test-bucket",
},
},
AtmosConfig: &schema.AtmosConfiguration{},
},
result: &ComponentProcessorResult{
ComponentVars: map[string]any{
"component_var": "component",
},
ComponentSettings: map[string]any{
"component_setting": false,
},
ComponentEnv: map[string]any{
"COMPONENT_ENV": "value",
},
ComponentCommand: "tofu",
ComponentMetadata: map[string]any{
"type": "real",
},
ComponentOverrides: map[string]any{},
ComponentOverridesVars: map[string]any{},
ComponentOverridesSettings: map[string]any{},
ComponentOverridesEnv: map[string]any{},
ComponentOverridesAuth: map[string]any{},
BaseComponentVars: map[string]any{},
BaseComponentSettings: map[string]any{},
BaseComponentEnv: map[string]any{},
BaseComponentAuth: map[string]any{},
ComponentProviders: map[string]any{
"aws": map[string]any{
"profile": "test",
},
},
ComponentHooks: map[string]any{
"after": []string{"component hook"},
},
ComponentAuth: map[string]any{},
ComponentBackendType: "",
ComponentBackendSection: map[string]any{},
ComponentRemoteStateBackendType: "",
ComponentRemoteStateBackendSection: map[string]any{},
ComponentOverridesProviders: map[string]any{},
ComponentOverridesHooks: map[string]any{},
BaseComponentProviders: map[string]any{},
BaseComponentHooks: map[string]any{},
BaseComponentBackendType: "",
BaseComponentBackendSection: map[string]any{},
BaseComponentRemoteStateBackendType: "",
BaseComponentRemoteStateBackendSection: map[string]any{},
},
expectedVars: map[string]any{
"global_var": "global",
"component_var": "component",
},
expectedSettings: map[string]any{
"global_setting": true,
"component_setting": false,
},
expectedEnv: map[string]any{
"GLOBAL_ENV": "value",
"COMPONENT_ENV": "value",
},
expectedAuth: map[string]any{
"aws": map[string]any{
"profile": "global-profile",
},
},
expectedCommand: "tofu",
},
{
name: "helmfile component",
opts: ComponentProcessorOptions{
ComponentType: cfg.HelmfileComponentType,
Component: "app",
GlobalVars: map[string]any{
"namespace": "kube-system",
},
GlobalSettings: map[string]any{
"enabled": true,
},
GlobalEnv: map[string]any{},
GlobalAuth: map[string]any{},
AtmosConfig: &schema.AtmosConfiguration{
Components: schema.Components{
Helmfile: schema.Helmfile{
Command: "helmfile",
},
},
},
},
result: &ComponentProcessorResult{
ComponentVars: map[string]any{
"namespace": "default",
},
ComponentSettings: map[string]any{},
ComponentEnv: map[string]any{},
ComponentMetadata: map[string]any{},
ComponentOverrides: map[string]any{},
ComponentOverridesVars: map[string]any{},
ComponentOverridesSettings: map[string]any{},
ComponentOverridesEnv: map[string]any{},
ComponentOverridesAuth: map[string]any{},
BaseComponentVars: map[string]any{},
BaseComponentSettings: map[string]any{},
BaseComponentEnv: map[string]any{},
BaseComponentAuth: map[string]any{},
},
expectedVars: map[string]any{
"namespace": "default",
},
expectedCommand: "helmfile",
},
{
name: "packer component",
opts: ComponentProcessorOptions{
ComponentType: cfg.PackerComponentType,
Component: "ami",
GlobalVars: map[string]any{
"region": "us-east-1",
},
GlobalSettings: map[string]any{},
GlobalEnv: map[string]any{},
GlobalAuth: map[string]any{},
AtmosConfig: &schema.AtmosConfiguration{},
},
result: &ComponentProcessorResult{
ComponentVars: map[string]any{
"ami_name": "test-ami",
},
ComponentSettings: map[string]any{},
ComponentEnv: map[string]any{},
ComponentMetadata: map[string]any{},
ComponentOverrides: map[string]any{},
ComponentOverridesVars: map[string]any{},
ComponentOverridesSettings: map[string]any{},
ComponentOverridesEnv: map[string]any{},
BaseComponentVars: map[string]any{},
BaseComponentSettings: map[string]any{},
BaseComponentEnv: map[string]any{},
},
expectedVars: map[string]any{
"region": "us-east-1",
"ami_name": "test-ami",
},
expectedCommand: cfg.PackerComponentType,
},
{
name: "component with base component name",
opts: ComponentProcessorOptions{
ComponentType: cfg.TerraformComponentType,
Component: "derived-vpc",
GlobalVars: map[string]any{},
GlobalSettings: map[string]any{},
GlobalEnv: map[string]any{},
AtmosConfig: &schema.AtmosConfiguration{},
},
result: &ComponentProcessorResult{
ComponentVars: map[string]any{},
ComponentSettings: map[string]any{},
ComponentEnv: map[string]any{},
ComponentMetadata: map[string]any{},
ComponentOverrides: map[string]any{},
ComponentOverridesVars: map[string]any{},
ComponentOverridesSettings: map[string]any{},
ComponentOverridesEnv: map[string]any{},
BaseComponentName: "base-vpc",
BaseComponentVars: map[string]any{},
BaseComponentSettings: map[string]any{},
BaseComponentEnv: map[string]any{},
ComponentProviders: map[string]any{},
ComponentHooks: map[string]any{},
ComponentAuth: map[string]any{},
ComponentBackendType: "",
ComponentBackendSection: map[string]any{},
ComponentRemoteStateBackendType: "",
ComponentRemoteStateBackendSection: map[string]any{},
ComponentOverridesProviders: map[string]any{},
ComponentOverridesHooks: map[string]any{},
BaseComponentProviders: map[string]any{},
BaseComponentHooks: map[string]any{},
BaseComponentBackendType: "",
BaseComponentBackendSection: map[string]any{},
BaseComponentRemoteStateBackendType: "",
BaseComponentRemoteStateBackendSection: map[string]any{},
},
checkBaseComponent: true,
expectedBaseComponent: "base-vpc",
},
{
name: "terraform abstract component removes spacelift workspace_enabled",
opts: ComponentProcessorOptions{
ComponentType: cfg.TerraformComponentType,
Component: "abstract-vpc",
GlobalVars: map[string]any{},
GlobalSettings: map[string]any{},
GlobalEnv: map[string]any{},
GlobalBackendType: "s3",
GlobalBackendSection: map[string]any{
"s3": map[string]any{
"bucket": "test",
},
},
TerraformProviders: map[string]any{},
GlobalAndTerraformHooks: map[string]any{},
AtmosConfig: &schema.AtmosConfiguration{},
},
result: &ComponentProcessorResult{
ComponentVars: map[string]any{},
ComponentSettings: map[string]any{
"spacelift": map[string]any{
"workspace_enabled": true,
},
},
ComponentEnv: map[string]any{},
ComponentMetadata: map[string]any{
"type": cfg.AbstractSectionName,
},
ComponentOverrides: map[string]any{},
ComponentOverridesVars: map[string]any{},
ComponentOverridesSettings: map[string]any{},
ComponentOverridesEnv: map[string]any{},
BaseComponentVars: map[string]any{},
BaseComponentSettings: map[string]any{},
BaseComponentEnv: map[string]any{},
ComponentProviders: map[string]any{},
ComponentHooks: map[string]any{},
ComponentAuth: map[string]any{},
ComponentBackendType: "",
ComponentBackendSection: map[string]any{},
ComponentRemoteStateBackendType: "",
ComponentRemoteStateBackendSection: map[string]any{},
ComponentOverridesProviders: map[string]any{},
ComponentOverridesHooks: map[string]any{},
BaseComponentProviders: map[string]any{},
BaseComponentHooks: map[string]any{},
BaseComponentBackendType: "",
BaseComponentBackendSection: map[string]any{},
BaseComponentRemoteStateBackendType: "",
BaseComponentRemoteStateBackendSection: map[string]any{},
},
expectedSettings: map[string]any{
"spacelift": map[string]any{
// workspace_enabled should be removed
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
comp, err := mergeComponentConfigurations(tt.opts.AtmosConfig, &tt.opts, tt.result)
require.NoError(t, err)
require.NotNil(t, comp)
if tt.expectedVars != nil {
actualVars := comp[cfg.VarsSectionName].(map[string]any)
assert.Equal(t, tt.expectedVars, actualVars)
}
if tt.expectedSettings != nil {
actualSettings := comp[cfg.SettingsSectionName].(map[string]any)
for key, expectedValue := range tt.expectedSettings {
assert.Equal(t, expectedValue, actualSettings[key])
}
}
if tt.expectedEnv != nil {
actualEnv := comp[cfg.EnvSectionName].(map[string]any)
assert.Equal(t, tt.expectedEnv, actualEnv)
}
if tt.expectedCommand != "" {
assert.Equal(t, tt.expectedCommand, comp[cfg.CommandSectionName])
}
if tt.expectedAuth != nil {
actualAuth := comp[cfg.AuthSectionName].(map[string]any)
for key, expectedValue := range tt.expectedAuth {
assert.Equal(t, expectedValue, actualAuth[key])
}
}
if tt.expectedProviders != nil {
actualProviders := comp[cfg.ProvidersSectionName].(map[string]any)
for key, expectedValue := range tt.expectedProviders {
assert.Equal(t, expectedValue, actualProviders[key])
}
}
if tt.expectedHooks != nil {
actualHooks := comp[cfg.HooksSectionName].(map[string]any)
for key, expectedValue := range tt.expectedHooks {
assert.Equal(t, expectedValue, actualHooks[key])
}
}
if tt.checkBaseComponent {
assert.Equal(t, tt.expectedBaseComponent, comp[cfg.ComponentSectionName])
}
})
}
}
// minimalComponentResult returns a ComponentProcessorResult with all map fields
// initialized to empty maps — enough to satisfy mergeComponentConfigurations' nil-safety
// expectations so a retry-focused test doesn't have to repeat the boilerplate.
func minimalComponentResult() *ComponentProcessorResult {
return &ComponentProcessorResult{
ComponentVars: map[string]any{},
ComponentSettings: map[string]any{},
ComponentEnv: map[string]any{},
ComponentAuth: map[string]any{},
ComponentMetadata: map[string]any{},
ComponentOverrides: map[string]any{},
ComponentOverridesVars: map[string]any{},
ComponentOverridesSettings: map[string]any{},
ComponentOverridesEnv: map[string]any{},
ComponentOverridesAuth: map[string]any{},
BaseComponentVars: map[string]any{},
BaseComponentSettings: map[string]any{},
BaseComponentEnv: map[string]any{},
BaseComponentAuth: map[string]any{},
ComponentProviders: map[string]any{},
ComponentHooks: map[string]any{},
ComponentTest: map[string]any{},
ComponentMocks: map[string]any{},
ComponentBackendType: "",
ComponentBackendSection: map[string]any{},
ComponentRemoteStateBackendType: "",
ComponentRemoteStateBackendSection: map[string]any{},
ComponentOverridesProviders: map[string]any{},
ComponentOverridesHooks: map[string]any{},
BaseComponentProviders: map[string]any{},
BaseComponentHooks: map[string]any{},
BaseComponentTest: map[string]any{},
BaseComponentMocks: map[string]any{},
BaseComponentBackendType: "",
BaseComponentBackendSection: map[string]any{},
BaseComponentRemoteStateBackendType: "",
BaseComponentRemoteStateBackendSection: map[string]any{},
}
}
// TestMergeComponentConfigurations_Plugins covers the Helm CLI plugins list merge:
// it is omitted when unset, flows through from base-only and component-only, and the
// concrete component's list replaces the inherited one under the default strategy.
func TestMergeComponentConfigurations_Plugins(t *testing.T) {
atmosCfg := &schema.AtmosConfiguration{}
t.Run("absent-omits-section", func(t *testing.T) {
opts := ComponentProcessorOptions{ComponentType: cfg.HelmfileComponentType, Component: "app", AtmosConfig: atmosCfg}
comp, err := mergeComponentConfigurations(atmosCfg, &opts, minimalComponentResult())
require.NoError(t, err)
_, present := comp[cfg.PluginsSectionName]
assert.False(t, present, "plugins must be absent when neither base nor component set it")
})
t.Run("component-only-flows-through", func(t *testing.T) {
opts := ComponentProcessorOptions{ComponentType: cfg.HelmfileComponentType, Component: "app", AtmosConfig: atmosCfg}
res := minimalComponentResult()
res.ComponentPlugins = []any{"diff@v3.9.4", "secrets"}
comp, err := mergeComponentConfigurations(atmosCfg, &opts, res)
require.NoError(t, err)
got, ok := comp[cfg.PluginsSectionName].([]any)
require.True(t, ok, "plugins must be present and a list")
require.Len(t, got, 2)
assert.Equal(t, "diff@v3.9.4", got[0])
assert.Equal(t, "secrets", got[1])
})
t.Run("base-only-flows-through", func(t *testing.T) {
opts := ComponentProcessorOptions{ComponentType: cfg.HelmComponentType, Component: "app", AtmosConfig: atmosCfg}
res := minimalComponentResult()
res.BaseComponentPlugins = []any{"diff@v3.9.4"}
comp, err := mergeComponentConfigurations(atmosCfg, &opts, res)
require.NoError(t, err)
got, ok := comp[cfg.PluginsSectionName].([]any)
require.True(t, ok)
require.Len(t, got, 1)
assert.Equal(t, "diff@v3.9.4", got[0])
})
t.Run("component-replaces-base-by-default", func(t *testing.T) {
opts := ComponentProcessorOptions{ComponentType: cfg.HelmfileComponentType, Component: "app", AtmosConfig: atmosCfg}
res := minimalComponentResult()
res.BaseComponentPlugins = []any{"diff@v3.8.0"}
res.ComponentPlugins = []any{"diff@v3.9.4", "secrets"}
comp, err := mergeComponentConfigurations(atmosCfg, &opts, res)
require.NoError(t, err)
got := comp[cfg.PluginsSectionName].([]any)
require.Len(t, got, 2, "default replace strategy keeps the concrete component's list")
assert.Equal(t, "diff@v3.9.4", got[0])
assert.Equal(t, "secrets", got[1])
})
t.Run("terraform-ignores-plugins", func(t *testing.T) {
opts := ComponentProcessorOptions{ComponentType: cfg.TerraformComponentType, Component: "vpc", AtmosConfig: atmosCfg}
res := minimalComponentResult()
res.ComponentPlugins = []any{"diff@v3.9.4"}
comp, err := mergeComponentConfigurations(atmosCfg, &opts, res)
require.NoError(t, err)
_, present := comp[cfg.PluginsSectionName]
assert.False(t, present, "terraform components must not emit a plugins section")
})
}
func TestMergeComponentConfigurations_TerraformTestSection(t *testing.T) {
atmosCfg := &schema.AtmosConfiguration{}
opts := ComponentProcessorOptions{
ComponentType: cfg.TerraformComponentType,
Component: "app",
AtmosConfig: atmosCfg,
}
res := minimalComponentResult()
res.BaseComponentTest = map[string]any{
cfg.VarsSectionName: map[string]any{
"fixture_vpc_id": "vpc-from-base",
"base_only": "base",
},
}
res.ComponentTest = map[string]any{
cfg.VarsSectionName: map[string]any{
"fixture_vpc_id": "vpc-from-component",
},
}
comp, err := mergeComponentConfigurations(atmosCfg, &opts, res)
require.NoError(t, err)
testSection, ok := comp[cfg.TestSectionName].(map[string]any)
require.True(t, ok)
testVars, ok := testSection[cfg.VarsSectionName].(map[string]any)
require.True(t, ok)
assert.Equal(t, "vpc-from-component", testVars["fixture_vpc_id"])
assert.Equal(t, "base", testVars["base_only"])
testVars["fixture_vpc_id"] = "merged-mutated"
assert.Equal(t, "vpc-from-component", res.ComponentTest[cfg.VarsSectionName].(map[string]any)["fixture_vpc_id"],
"mutating merged test vars must not mutate the component source map")
res.BaseComponentTest[cfg.VarsSectionName].(map[string]any)["base_only"] = "source-mutated"
res.ComponentTest[cfg.VarsSectionName].(map[string]any)["fixture_vpc_id"] = "source-mutated"
assert.Equal(t, "base", testVars["base_only"], "mutating source maps after merge must not mutate merged test vars")
assert.Equal(t, "merged-mutated", testVars["fixture_vpc_id"], "mutating source maps after merge must not mutate merged test vars")
}
func TestMergeComponentConfigurations_TerraformTestSectionOmittedWhenEmpty(t *testing.T) {
atmosCfg := &schema.AtmosConfiguration{}
opts := ComponentProcessorOptions{
ComponentType: cfg.TerraformComponentType,
Component: "app",
AtmosConfig: atmosCfg,
}
res := minimalComponentResult()
comp, err := mergeComponentConfigurations(atmosCfg, &opts, res)
require.NoError(t, err)
assert.NotContains(t, comp, cfg.TestSectionName)
}
func TestMergeComponentConfigurations_TerraformMocks(t *testing.T) {
atmosCfg := &schema.AtmosConfiguration{}
opts := ComponentProcessorOptions{
ComponentType: cfg.TerraformComponentType,
Component: "app",
AtmosConfig: atmosCfg,
}
res := minimalComponentResult()
res.BaseComponentMocks = map[string]any{
"inherited": "from-base",
"network": map[string]any{
"cidr": "10.0.0.0/16",
"region": "us-east-2",
},
}
res.ComponentMocks = map[string]any{
"vpc_id": "vpc-local",
"network": map[string]any{
"cidr": "10.1.0.0/16",
},
}
comp, err := mergeComponentConfigurations(atmosCfg, &opts, res)
require.NoError(t, err)
mocks, ok := comp[cfg.MocksSectionName].(map[string]any)
require.True(t, ok)
assert.Equal(t, "from-base", mocks["inherited"])
assert.Equal(t, "vpc-local", mocks["vpc_id"])
network, ok := mocks["network"].(map[string]any)
require.True(t, ok)
assert.Equal(t, "10.1.0.0/16", network["cidr"])
assert.Equal(t, "us-east-2", network["region"])
network["cidr"] = "mutated"
assert.Equal(t, "10.1.0.0/16", res.ComponentMocks["network"].(map[string]any)["cidr"])
}
// TestMergeComponentConfigurations_GlobalKubernetesDefaults verifies stack-global
// Kubernetes provider/paths/manifests/render defaults are the lowest-precedence layer:
// they flow through when the component sets nothing, and the component overrides them.
func TestMergeComponentConfigurations_GlobalKubernetesDefaults(t *testing.T) {
atmosCfg := &schema.AtmosConfiguration{}
t.Run("global-defaults-flow-through", func(t *testing.T) {
opts := ComponentProcessorOptions{
ComponentType: cfg.KubernetesComponentType,
Component: "api",
AtmosConfig: atmosCfg,
GlobalKubernetesProvider: "kustomize",
GlobalKubernetesPaths: []any{"base"},
GlobalKubernetesManifests: []any{"global.yaml"},
GlobalKubernetesRender: map[string]any{"output": map[string]any{"split": true}},
}
comp, err := mergeComponentConfigurations(atmosCfg, &opts, minimalComponentResult())
require.NoError(t, err)
assert.Equal(t, "kustomize", comp[cfg.ProviderSectionName])
assert.Equal(t, []any{"base"}, comp[cfg.PathsSectionName])
assert.Equal(t, []any{"global.yaml"}, comp[cfg.ManifestsSectionName])
render, ok := comp[cfg.RenderSectionName].(map[string]any)
require.True(t, ok, "render section must be present")
out := render["output"].(map[string]any)
assert.Equal(t, true, out["split"])
})
t.Run("component-overrides-global", func(t *testing.T) {
opts := ComponentProcessorOptions{
ComponentType: cfg.KubernetesComponentType,
Component: "api",
AtmosConfig: atmosCfg,
GlobalKubernetesProvider: "kustomize",
}
res := minimalComponentResult()
res.ComponentProvider = "kubectl"
comp, err := mergeComponentConfigurations(atmosCfg, &opts, res)
require.NoError(t, err)
assert.Equal(t, "kubectl", comp[cfg.ProviderSectionName], "component provider must override the global default")
})
}
// TestMergeComponentConfigurations_Kubernetes verifies the full three-level precedence
// (stack-global Kubernetes defaults → base component → component instance) for the
// kubernetes-native sections (provider/paths/manifests/render), and that hooks, generate,
// source, and provision flow through for the kubernetes component type.
func TestMergeComponentConfigurations_Kubernetes(t *testing.T) {
atmosCfg := &schema.AtmosConfiguration{}
t.Run("three-level-precedence-provider-component-wins", func(t *testing.T) {
opts := ComponentProcessorOptions{
ComponentType: cfg.KubernetesComponentType,
Component: "api",
AtmosConfig: atmosCfg,
GlobalKubernetesProvider: "kustomize",
}
res := minimalComponentResult()
res.BaseComponentProvider = "kubectl"
res.ComponentProvider = "kustomize-component"
comp, err := mergeComponentConfigurations(atmosCfg, &opts, res)
require.NoError(t, err)
assert.Equal(t, "kustomize-component", comp[cfg.ProviderSectionName],
"component provider must win over base and global")
})
t.Run("provider-base-wins-over-global", func(t *testing.T) {
opts := ComponentProcessorOptions{
ComponentType: cfg.KubernetesComponentType,
Component: "api",
AtmosConfig: atmosCfg,
GlobalKubernetesProvider: "kustomize",
}
res := minimalComponentResult()
res.BaseComponentProvider = "kubectl"
comp, err := mergeComponentConfigurations(atmosCfg, &opts, res)
require.NoError(t, err)
assert.Equal(t, "kubectl", comp[cfg.ProviderSectionName],
"base provider must win over the global default when the component sets nothing")
})
t.Run("paths-and-manifests-merge-across-three-levels", func(t *testing.T) {
opts := ComponentProcessorOptions{
ComponentType: cfg.KubernetesComponentType,
Component: "api",
AtmosConfig: atmosCfg,
GlobalKubernetesPaths: map[string]any{"global": "g.yaml"},
GlobalKubernetesManifests: map[string]any{"global": "gm.yaml"},
}
res := minimalComponentResult()
res.BaseComponentPaths = map[string]any{"base": "b.yaml"}
res.ComponentPaths = map[string]any{"component": "c.yaml"}
res.BaseComponentManifests = map[string]any{"base": "bm.yaml"}
res.ComponentManifests = map[string]any{"component": "cm.yaml"}
comp, err := mergeComponentConfigurations(atmosCfg, &opts, res)
require.NoError(t, err)
paths, ok := comp[cfg.PathsSectionName].(map[string]any)
require.True(t, ok, "paths section must be a merged map")
assert.Equal(t, "g.yaml", paths["global"])
assert.Equal(t, "b.yaml", paths["base"])
assert.Equal(t, "c.yaml", paths["component"])
manifests, ok := comp[cfg.ManifestsSectionName].(map[string]any)
require.True(t, ok, "manifests section must be a merged map")
assert.Equal(t, "gm.yaml", manifests["global"])
assert.Equal(t, "bm.yaml", manifests["base"])
assert.Equal(t, "cm.yaml", manifests["component"])
})
t.Run("render-merges-with-component-winning-on-conflict", func(t *testing.T) {
opts := ComponentProcessorOptions{
ComponentType: cfg.KubernetesComponentType,
Component: "api",
AtmosConfig: atmosCfg,
GlobalKubernetesRender: map[string]any{"engine": "global", "from_global": true},
}
res := minimalComponentResult()
res.BaseComponentRender = map[string]any{"engine": "base", "from_base": true}
res.ComponentRender = map[string]any{"engine": "component", "from_component": true}
comp, err := mergeComponentConfigurations(atmosCfg, &opts, res)
require.NoError(t, err)
render, ok := comp[cfg.RenderSectionName].(map[string]any)
require.True(t, ok, "render section must be a merged map")
assert.Equal(t, "component", render["engine"], "component render must win on conflicting keys")
assert.Equal(t, true, render["from_global"])
assert.Equal(t, true, render["from_base"])
assert.Equal(t, true, render["from_component"])
})
t.Run("hooks-generate-source-provision-flow-through", func(t *testing.T) {
opts := ComponentProcessorOptions{
ComponentType: cfg.KubernetesComponentType,
Component: "api",
AtmosConfig: atmosCfg,
GlobalAndTerraformHooks: map[string]any{"before": map[string]any{"a": "global-hook"}},
GlobalAndTerraformGenerate: map[string]any{"file.yaml": map[string]any{"from": "global"}},
GlobalSourceSection: map[string]any{"uri": "global-uri"},
GlobalProvisionSection: map[string]any{"workdir": "global-wd"},
}
res := minimalComponentResult()
res.ComponentHooks = map[string]any{"after": map[string]any{"b": "component-hook"}}
res.ComponentGenerate = map[string]any{"comp.yaml": map[string]any{"from": "component"}}
res.ComponentSourceSection = map[string]any{"version": "1.2.3"}
res.ComponentProvision = map[string]any{"timeout": "5m"}
comp, err := mergeComponentConfigurations(atmosCfg, &opts, res)
require.NoError(t, err)
hooks, ok := comp[cfg.HooksSectionName].(map[string]any)
require.True(t, ok, "hooks section must be present for kubernetes")
assert.Contains(t, hooks, "before")
assert.Contains(t, hooks, "after")
generate, ok := comp[cfg.GenerateSectionName].(map[string]any)
require.True(t, ok, "generate section must be present for kubernetes")
assert.Contains(t, generate, "file.yaml")
assert.Contains(t, generate, "comp.yaml")
source, ok := comp[cfg.SourceSectionName].(map[string]any)
require.True(t, ok, "source section must be present for kubernetes")
assert.Equal(t, "global-uri", source["uri"])
assert.Equal(t, "1.2.3", source["version"])
provision, ok := comp[cfg.ProvisionSectionName].(map[string]any)
require.True(t, ok, "provision section must be present for kubernetes")
assert.Equal(t, "global-wd", provision["workdir"])
assert.Equal(t, "5m", provision["timeout"])
})
t.Run("validate-component-instance-false-overrides-base-true", func(t *testing.T) {
opts := ComponentProcessorOptions{
ComponentType: cfg.KubernetesComponentType,
Component: "api",
AtmosConfig: atmosCfg,
}
res := minimalComponentResult()
res.BaseComponentValidate = true
res.ComponentValidate = false
comp, err := mergeComponentConfigurations(atmosCfg, &opts, res)
require.NoError(t, err)
assert.Equal(t, false, comp[cfg.ValidateSectionName],
"an explicit component-instance validate:false must override a base-component validate:true")
})
t.Run("validate-base-true-flows-through-when-component-unset", func(t *testing.T) {
opts := ComponentProcessorOptions{
ComponentType: cfg.KubernetesComponentType,
Component: "api",
AtmosConfig: atmosCfg,
}
res := minimalComponentResult()
res.BaseComponentValidate = true
comp, err := mergeComponentConfigurations(atmosCfg, &opts, res)
require.NoError(t, err)
assert.Equal(t, true, comp[cfg.ValidateSectionName],
"base-component validate:true must flow through when the component instance sets nothing")
})
t.Run("validate-unset-everywhere-is-absent-from-comp", func(t *testing.T) {
opts := ComponentProcessorOptions{
ComponentType: cfg.KubernetesComponentType,
Component: "api",
AtmosConfig: atmosCfg,
}
res := minimalComponentResult()
comp, err := mergeComponentConfigurations(atmosCfg, &opts, res)
require.NoError(t, err)
_, ok := comp[cfg.ValidateSectionName]
assert.False(t, ok, "validate must be absent (not defaulted to any value) when unset at every layer")
})
}
// TestMergeComponentConfigurations_Retry covers the per-component retry merge added by
// the component-retry feature: base → component → overrides precedence on scalars, and
// list-append on the `conditions:` slice (the existing deep-merge semantic). It also
// asserts that the retry section is omitted entirely when none of base/component/overrides
// provide one (avoids leaking empty `retry: {}` into rendered component output).
func TestMergeComponentConfigurations_Retry(t *testing.T) {
atmosCfg := &schema.AtmosConfiguration{}
t.Run("no-retry-anywhere-omits-section", func(t *testing.T) {
opts := ComponentProcessorOptions{
ComponentType: cfg.TerraformComponentType,
Component: "vpc",
AtmosConfig: atmosCfg,
}
comp, err := mergeComponentConfigurations(atmosCfg, &opts, minimalComponentResult())
require.NoError(t, err)
_, present := comp[cfg.RetrySectionName]
assert.False(t, present, "retry must be absent when neither base, component, nor overrides set it")
})
t.Run("base-only-flows-through", func(t *testing.T) {
opts := ComponentProcessorOptions{
ComponentType: cfg.TerraformComponentType,
Component: "vpc",
AtmosConfig: atmosCfg,
}
res := minimalComponentResult()
res.BaseComponentRetry = map[string]any{
"max_attempts": 5,
"conditions": []any{"/Bad Gateway/"},
}
comp, err := mergeComponentConfigurations(atmosCfg, &opts, res)
require.NoError(t, err)
got, ok := comp[cfg.RetrySectionName].(map[string]any)
require.True(t, ok, "retry section must be present and a map")
assert.EqualValues(t, 5, got["max_attempts"])
assert.Equal(t, []any{"/Bad Gateway/"}, got["conditions"])
})
t.Run("component-overrides-base-scalar", func(t *testing.T) {
opts := ComponentProcessorOptions{
ComponentType: cfg.TerraformComponentType,
Component: "vpc",
AtmosConfig: atmosCfg,
}
res := minimalComponentResult()
res.BaseComponentRetry = map[string]any{"max_attempts": 3}
res.ComponentRetry = map[string]any{"max_attempts": 7}
comp, err := mergeComponentConfigurations(atmosCfg, &opts, res)
require.NoError(t, err)
got := comp[cfg.RetrySectionName].(map[string]any)
assert.EqualValues(t, 7, got["max_attempts"], "concrete component must override base scalar")
})
t.Run("overrides-wins-over-component-and-base", func(t *testing.T) {
opts := ComponentProcessorOptions{
ComponentType: cfg.TerraformComponentType,
Component: "vpc",
AtmosConfig: atmosCfg,
}
res := minimalComponentResult()
res.BaseComponentRetry = map[string]any{"max_attempts": 1, "backoff_strategy": "constant"}
res.ComponentRetry = map[string]any{"max_attempts": 2}
res.ComponentOverridesRetry = map[string]any{"max_attempts": 9, "backoff_strategy": "exponential"}
comp, err := mergeComponentConfigurations(atmosCfg, &opts, res)
require.NoError(t, err)
got := comp[cfg.RetrySectionName].(map[string]any)
assert.EqualValues(t, 9, got["max_attempts"], "overrides must win")
assert.Equal(t, "exponential", got["backoff_strategy"])
})
t.Run("conditions-list-replaces-by-default", func(t *testing.T) {
// Default list_merge_strategy is "replace", so the last non-empty conditions
// list wins. This documents the default behaviour — users who want additive
// conditions across inheritance layers must opt in with list_merge_strategy: append.
opts := ComponentProcessorOptions{
ComponentType: cfg.TerraformComponentType,
Component: "vpc",
AtmosConfig: atmosCfg,
}
res := minimalComponentResult()
res.BaseComponentRetry = map[string]any{"conditions": []any{"/base-only/"}}
res.ComponentRetry = map[string]any{"conditions": []any{"/component-only/"}}
res.ComponentOverridesRetry = map[string]any{"conditions": []any{"/override-only/"}}
comp, err := mergeComponentConfigurations(atmosCfg, &opts, res)
require.NoError(t, err)
got := comp[cfg.RetrySectionName].(map[string]any)
conds, ok := got["conditions"].([]any)
require.True(t, ok, "conditions must be a slice after merge")
require.Len(t, conds, 1, "default replace strategy keeps only the last layer's conditions")
assert.Equal(t, "/override-only/", conds[0], "overrides win under replace strategy")
})
t.Run("conditions-list-appends-when-strategy-is-append", func(t *testing.T) {
// Opt-in: with list_merge_strategy: append, conditions accumulate base →
// component → overrides so the iteration order in retry.MatchesAny matches
// the inheritance order.
appendCfg := &schema.AtmosConfiguration{
Settings: schema.AtmosSettings{ListMergeStrategy: "append"},
}
opts := ComponentProcessorOptions{
ComponentType: cfg.TerraformComponentType,
Component: "vpc",
AtmosConfig: appendCfg,
}
res := minimalComponentResult()
res.BaseComponentRetry = map[string]any{"conditions": []any{"/base-only/"}}
res.ComponentRetry = map[string]any{"conditions": []any{"/component-only/"}}
res.ComponentOverridesRetry = map[string]any{"conditions": []any{"/override-only/"}}
comp, err := mergeComponentConfigurations(appendCfg, &opts, res)
require.NoError(t, err)
got := comp[cfg.RetrySectionName].(map[string]any)
conds, ok := got["conditions"].([]any)
require.True(t, ok, "conditions must be a slice after merge")
require.Len(t, conds, 3, "append strategy must accumulate each layer's conditions")
assert.Equal(t, "/base-only/", conds[0], "base first")
assert.Equal(t, "/override-only/", conds[2], "overrides last")
})
t.Run("result-mutation-does-not-leak-into-source-maps", func(t *testing.T) {
// Aliasing-isolation check (per CLAUDE.md): mutating the merged result must
// not touch the original base/component/overrides input maps.
opts := ComponentProcessorOptions{
ComponentType: cfg.TerraformComponentType,
Component: "vpc",
AtmosConfig: atmosCfg,
}
baseRetry := map[string]any{"max_attempts": 2}
compRetry := map[string]any{"max_attempts": 4}
res := minimalComponentResult()
res.BaseComponentRetry = baseRetry
res.ComponentRetry = compRetry
comp, err := mergeComponentConfigurations(atmosCfg, &opts, res)
require.NoError(t, err)
got := comp[cfg.RetrySectionName].(map[string]any)
got["max_attempts"] = 999
assert.EqualValues(t, 2, baseRetry["max_attempts"], "base map must stay intact")
assert.EqualValues(t, 4, compRetry["max_attempts"], "component map must stay intact")
})
t.Run("source-mutation-does-not-leak-into-merged-result", func(t *testing.T) {
// src→result isolation (per CLAUDE.md): mutating the original base/component
// maps after the merge must not affect the already-merged output.
opts := ComponentProcessorOptions{
ComponentType: cfg.TerraformComponentType,
Component: "vpc",
AtmosConfig: atmosCfg,
}
baseRetry := map[string]any{"max_attempts": 2}
compRetry := map[string]any{"max_attempts": 4}
res := minimalComponentResult()
res.BaseComponentRetry = baseRetry
res.ComponentRetry = compRetry
comp, err := mergeComponentConfigurations(atmosCfg, &opts, res)
require.NoError(t, err)
got := comp[cfg.RetrySectionName].(map[string]any)
// Pre-condition: merged result reflects the component-wins-over-base precedence.
require.EqualValues(t, 4, got["max_attempts"])
// Mutate the source maps after merge; the merged result must be unaffected.
baseRetry["max_attempts"] = 111
compRetry["max_attempts"] = 222
assert.EqualValues(t, 4, got["max_attempts"], "mutating source maps after merge must not affect the merged result")
})
}
func TestMergeComponentConfigurations_Dependencies(t *testing.T) {
atmosCfg := &schema.AtmosConfiguration{}
t.Run("deep-merges-distinct-dependency-keys", func(t *testing.T) {
opts := ComponentProcessorOptions{
ComponentType: cfg.TerraformComponentType,
Component: "lambda",
AtmosConfig: atmosCfg,
GlobalDependencies: map[string]any{
"tools": map[string]any{"terraform": "1.9.8"},
},
}
res := minimalComponentResult()
res.BaseComponentDependencies = map[string]any{
"components": []any{map[string]any{"component": "vpc"}},
"files": []any{"configs/base.json"},
}
res.ComponentDependencies = map[string]any{
"folders": []any{"src/lambda"},
"tools": map[string]any{"tflint": "0.54.2"},
}
comp, err := mergeComponentConfigurations(atmosCfg, &opts, res)
require.NoError(t, err)
deps, ok := comp[cfg.DependenciesSectionName].(map[string]any)
require.True(t, ok, "dependencies section must be present and a map")
assert.Equal(t, []any{map[string]any{"component": "vpc"}}, deps["components"])
assert.Equal(t, []any{"configs/base.json"}, deps["files"])
assert.Equal(t, []any{"src/lambda"}, deps["folders"])
assert.Equal(t, map[string]any{
"terraform": "1.9.8",
"tflint": "0.54.2",
}, deps["tools"])
})
t.Run("same-list-keys-replace-by-default", func(t *testing.T) {
opts := ComponentProcessorOptions{
ComponentType: cfg.TerraformComponentType,
Component: "lambda",
AtmosConfig: atmosCfg,
}
res := minimalComponentResult()
res.BaseComponentDependencies = map[string]any{
"components": []any{map[string]any{"component": "base-vpc"}},
"files": []any{"configs/base.json"},
"folders": []any{"src/base"},
}
res.ComponentDependencies = map[string]any{
"components": []any{map[string]any{"component": "component-vpc"}},
"files": []any{"configs/component.json"},
"folders": []any{"src/component"},
}