-
-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathdescribe_stacks_component_processor_test.go
More file actions
1830 lines (1561 loc) · 62.6 KB
/
Copy pathdescribe_stacks_component_processor_test.go
File metadata and controls
1830 lines (1561 loc) · 62.6 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 (
"errors"
"fmt"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
errUtils "github.com/cloudposse/atmos/errors"
cfg "github.com/cloudposse/atmos/pkg/config"
"github.com/cloudposse/atmos/pkg/degradation"
iolib "github.com/cloudposse/atmos/pkg/io"
"github.com/cloudposse/atmos/pkg/schema"
"github.com/cloudposse/atmos/pkg/secrets"
"github.com/cloudposse/atmos/pkg/store"
)
// ---------------------------------------------------------------------------
// extractDescribeComponentSections
// ---------------------------------------------------------------------------
func TestExtractDescribeComponentSections_AllPresent(t *testing.T) {
vars := map[string]any{"key": "val"}
meta := map[string]any{"component": "base"}
settings := map[string]any{"spacelift": true}
env := map[string]any{"TF_VAR_x": "1"}
authMap := map[string]any{"role": "arn:aws"}
providers := map[string]any{"aws": map[string]any{}}
hooks := map[string]any{"pre_plan": "script.sh"}
secrets := map[string]any{"vars": map[string]any{"API_KEY": map[string]any{"store": "app-secrets"}}}
overrides := map[string]any{"env": map[string]any{}}
backend := map[string]any{"bucket": "my-bucket"}
cs := map[string]any{
cfg.VarsSectionName: vars,
cfg.MetadataSectionName: meta,
cfg.SettingsSectionName: settings,
cfg.EnvSectionName: env,
cfg.AuthSectionName: authMap,
cfg.ProvidersSectionName: providers,
cfg.HooksSectionName: hooks,
cfg.SecretsSectionName: secrets,
cfg.OverridesSectionName: overrides,
cfg.BackendSectionName: backend,
cfg.BackendTypeSectionName: "s3",
}
secs := extractDescribeComponentSections(cs)
assert.Equal(t, vars, secs.vars)
assert.Equal(t, meta, secs.metadata)
assert.Equal(t, settings, secs.settings)
assert.Equal(t, env, secs.env)
assert.Equal(t, authMap, secs.auth)
assert.Equal(t, providers, secs.providers)
assert.Equal(t, hooks, secs.hooks)
assert.Equal(t, secrets, secs.secrets)
assert.Equal(t, overrides, secs.overrides)
assert.Equal(t, backend, secs.backend)
assert.Equal(t, "s3", secs.backendType)
}
func TestExtractDescribeComponentSections_Empty(t *testing.T) {
secs := extractDescribeComponentSections(map[string]any{})
assert.Equal(t, map[string]any{}, secs.vars)
assert.Equal(t, map[string]any{}, secs.metadata)
assert.Equal(t, map[string]any{}, secs.settings)
assert.Equal(t, map[string]any{}, secs.env)
assert.Equal(t, map[string]any{}, secs.auth)
assert.Equal(t, map[string]any{}, secs.providers)
assert.Equal(t, map[string]any{}, secs.hooks)
assert.Equal(t, map[string]any{}, secs.overrides)
assert.Equal(t, map[string]any{}, secs.backend)
assert.Equal(t, "", secs.backendType)
}
func TestExtractDescribeComponentSections_WrongTypes(t *testing.T) {
// When a section has the wrong type it should fall back to an empty map.
cs := map[string]any{
cfg.VarsSectionName: "not-a-map",
cfg.MetadataSectionName: 42,
cfg.BackendTypeSectionName: 999, // wrong type → empty string
}
secs := extractDescribeComponentSections(cs)
assert.Equal(t, map[string]any{}, secs.vars)
assert.Equal(t, map[string]any{}, secs.metadata)
assert.Equal(t, "", secs.backendType)
}
// ---------------------------------------------------------------------------
// buildConfigAndStacksInfo
// ---------------------------------------------------------------------------
func TestBuildConfigAndStacksInfo(t *testing.T) {
secs := componentSections{
vars: map[string]any{"region": "us-east-1"},
metadata: map[string]any{"component": "base"},
settings: map[string]any{"spacelift": true},
env: map[string]any{"ENV": "prod"},
auth: map[string]any{},
providers: map[string]any{},
hooks: map[string]any{},
secrets: map[string]any{"vars": map[string]any{}},
overrides: map[string]any{},
backend: map[string]any{"bucket": "tfstate"},
backendType: "s3",
}
info := buildConfigAndStacksInfo("my-component", "stacks/prod.yaml", "prod", secs)
assert.Equal(t, "my-component", info.ComponentFromArg)
assert.Equal(t, "stacks/prod.yaml", info.Stack)
assert.Equal(t, "prod", info.StackManifestName)
assert.Equal(t, secs.vars, info.ComponentVarsSection)
assert.Equal(t, secs.metadata, info.ComponentMetadataSection)
assert.Equal(t, secs.settings, info.ComponentSettingsSection)
assert.Equal(t, secs.env, info.ComponentEnvSection)
assert.Equal(t, secs.auth, info.ComponentAuthSection)
assert.Equal(t, secs.providers, info.ComponentProvidersSection)
assert.Equal(t, secs.hooks, info.ComponentHooksSection)
assert.Equal(t, secs.overrides, info.ComponentOverridesSection)
assert.Equal(t, secs.backend, info.ComponentBackendSection)
assert.Equal(t, "s3", info.ComponentBackendType)
// ComponentSection mirror.
assert.Equal(t, secs.vars, info.ComponentSection[cfg.VarsSectionName])
assert.Equal(t, secs.metadata, info.ComponentSection[cfg.MetadataSectionName])
assert.Equal(t, secs.secrets, info.ComponentSection[cfg.SecretsSectionName])
assert.Equal(t, "s3", info.ComponentSection[cfg.BackendTypeSectionName])
}
// ---------------------------------------------------------------------------
// resolveStackName
// ---------------------------------------------------------------------------
func TestResolveStackName_ManifestName(t *testing.T) {
ac := &schema.AtmosConfiguration{}
info := schema.ConfigAndStacksInfo{}
name, ctx, err := resolveStackName(ac, "stacks/prod.yaml", "my-manifest-name", info, nil)
require.NoError(t, err)
assert.Equal(t, "my-manifest-name", name)
assert.Equal(t, schema.Context{}, ctx) // no context populated for manifest-name path
}
func TestResolveStackName_NoNaming(t *testing.T) {
ac := &schema.AtmosConfiguration{}
info := schema.ConfigAndStacksInfo{}
name, ctx, err := resolveStackName(ac, "stacks/prod.yaml", "", info, nil)
require.NoError(t, err)
assert.Equal(t, "stacks/prod.yaml", name)
assert.Equal(t, schema.Context{}, ctx)
}
func TestResolveStackName_Pattern(t *testing.T) {
ac := &schema.AtmosConfiguration{
Stacks: schema.Stacks{
NamePattern: "{tenant}-{environment}-{stage}",
},
}
info := schema.ConfigAndStacksInfo{
ComponentSection: map[string]any{},
}
vars := map[string]any{
"tenant": "corp",
"environment": "us-east-1",
"stage": "prod",
}
name, ctx, err := resolveStackName(ac, "stacks/corp-us-east-1-prod.yaml", "", info, vars)
require.NoError(t, err)
assert.Equal(t, "corp-us-east-1-prod", name)
// Context should be populated from vars when name_pattern is used.
assert.Equal(t, "corp", ctx.Tenant)
assert.Equal(t, "us-east-1", ctx.Environment)
assert.Equal(t, "prod", ctx.Stage)
}
func TestResolveStackName_NameTemplate(t *testing.T) {
// Use a literal template that returns a fixed string.
ac := &schema.AtmosConfiguration{
Stacks: schema.Stacks{
NameTemplate: "hardcoded-stack-name",
},
}
info := schema.ConfigAndStacksInfo{
ComponentSection: map[string]any{},
}
name, ctx, err := resolveStackName(ac, "stacks/prod.yaml", "", info, nil)
require.NoError(t, err)
assert.Equal(t, "hardcoded-stack-name", name)
assert.Equal(t, schema.Context{}, ctx) // no context populated for name_template path
}
func TestResolveStackName_NameTemplateError(t *testing.T) {
// An invalid Go template should return an error.
ac := &schema.AtmosConfiguration{
Stacks: schema.Stacks{
NameTemplate: "{{.missing_open",
},
}
info := schema.ConfigAndStacksInfo{
ComponentSection: map[string]any{},
}
_, _, err := resolveStackName(ac, "stacks/prod.yaml", "", info, nil)
require.Error(t, err)
}
func TestResolveStackName_PatternValidationFallback(t *testing.T) {
// When the pattern doesn't match the filename, it falls back to the filename.
ac := &schema.AtmosConfiguration{
Stacks: schema.Stacks{
NamePattern: "{tenant}-{environment}-{stage}",
},
}
info := schema.ConfigAndStacksInfo{
ComponentSection: map[string]any{},
}
vars := map[string]any{
"tenant": "corp",
// missing environment and stage → pattern fails
}
name, ctx, err := resolveStackName(ac, "stacks/corp.yaml", "", info, vars)
require.NoError(t, err)
assert.Equal(t, "stacks/corp.yaml", name)
// Context is still populated even on fallback (from cfg.GetContextFromVars).
assert.Equal(t, "corp", ctx.Tenant)
}
// ---------------------------------------------------------------------------
// shouldFilterByStack
// ---------------------------------------------------------------------------
func TestShouldFilterByStack(t *testing.T) {
tests := []struct {
name string
filterByStack string
stackFileName string
stackName string
wantSkip bool
}{
{"no filter", "", "stacks/prod.yaml", "prod", false},
{"matches filename", "stacks/prod.yaml", "stacks/prod.yaml", "prod", false},
{"matches resolved name", "prod", "stacks/prod.yaml", "prod", false},
{"no match", "dev", "stacks/prod.yaml", "prod", true},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.wantSkip, shouldFilterByStack(tc.filterByStack, tc.stackFileName, tc.stackName))
})
}
}
// ---------------------------------------------------------------------------
// ensureComponentEntryInMap
// ---------------------------------------------------------------------------
func TestEnsureComponentEntryInMap_CreatesAllLevels(t *testing.T) {
finalMap := map[string]any{
"prod": map[string]any{},
}
ensureComponentEntryInMap(finalMap, "prod", "terraform", "vpc")
prodEntry, ok := finalMap["prod"].(map[string]any)
require.True(t, ok, "prod entry should be map[string]any")
comps, ok := prodEntry[cfg.ComponentsSectionName].(map[string]any)
require.True(t, ok, "components section should be map[string]any")
tf, ok := comps["terraform"].(map[string]any)
require.True(t, ok, "terraform section should be map[string]any")
assert.NotNil(t, tf["vpc"])
}
func TestEnsureComponentEntryInMap_IdempotentForExistingEntry(t *testing.T) {
finalMap := map[string]any{
"prod": map[string]any{
cfg.ComponentsSectionName: map[string]any{
"terraform": map[string]any{
"vpc": map[string]any{
"vars": map[string]any{"existing": true},
},
},
},
},
}
ensureComponentEntryInMap(finalMap, "prod", "terraform", "vpc")
// Existing content should be preserved.
prodEntry, ok := finalMap["prod"].(map[string]any)
require.True(t, ok)
comps, ok := prodEntry[cfg.ComponentsSectionName].(map[string]any)
require.True(t, ok)
tf, ok := comps["terraform"].(map[string]any)
require.True(t, ok)
vpc, ok := tf["vpc"].(map[string]any)
require.True(t, ok)
assert.Equal(t, map[string]any{"existing": true}, vpc["vars"])
}
// ---------------------------------------------------------------------------
// setAtmosComponentMetadata
// ---------------------------------------------------------------------------
func TestSetAtmosComponentMetadata(t *testing.T) {
section := map[string]any{}
setAtmosComponentMetadata(section, "my-comp", "prod", "stacks/prod.yaml")
assert.Equal(t, "my-comp", section["atmos_component"])
assert.Equal(t, "prod", section["atmos_stack"])
assert.Equal(t, "prod", section["stack"])
assert.Equal(t, "stacks/prod.yaml", section["atmos_stack_file"])
assert.Equal(t, "stacks/prod.yaml", section["atmos_manifest"])
}
// ---------------------------------------------------------------------------
// resolveIncludeEmpty
// ---------------------------------------------------------------------------
func TestResolveIncludeEmpty_DefaultTrue(t *testing.T) {
ac := &schema.AtmosConfiguration{}
assert.True(t, resolveIncludeEmpty(ac, true))
}
func TestResolveIncludeEmpty_ConfigFalse(t *testing.T) {
f := false
ac := &schema.AtmosConfiguration{
Describe: schema.Describe{
Settings: schema.DescribeSettings{IncludeEmpty: &f},
},
}
assert.False(t, resolveIncludeEmpty(ac, true))
}
func TestResolveIncludeEmpty_ConfigTrue(t *testing.T) {
tr := true
ac := &schema.AtmosConfiguration{
Describe: schema.Describe{
Settings: schema.DescribeSettings{IncludeEmpty: &tr},
},
}
assert.True(t, resolveIncludeEmpty(ac, true))
}
func TestResolveIncludeEmpty_CheckFalseAlwaysTrue(t *testing.T) {
// When checkIncludeEmpty is false (non-terraform types), always true regardless of config.
f := false
ac := &schema.AtmosConfiguration{
Describe: schema.Describe{
Settings: schema.DescribeSettings{IncludeEmpty: &f},
},
}
assert.True(t, resolveIncludeEmpty(ac, false))
}
// ---------------------------------------------------------------------------
// addSectionsToComponentEntry
// ---------------------------------------------------------------------------
func TestAddSectionsToComponentEntry_AllSections(t *testing.T) {
dest := map[string]any{}
src := map[string]any{
"vars": map[string]any{"a": 1},
"settings": map[string]any{},
}
addSectionsToComponentEntry(dest, src, nil, true)
assert.Equal(t, map[string]any{"a": 1}, dest["vars"])
assert.Equal(t, map[string]any{}, dest["settings"])
}
func TestAddSectionsToComponentEntry_FilterSections(t *testing.T) {
dest := map[string]any{}
src := map[string]any{
"vars": map[string]any{"a": 1},
"settings": map[string]any{"s": 2},
"env": map[string]any{"E": "v"},
}
addSectionsToComponentEntry(dest, src, []string{"vars", "env"}, true)
assert.Contains(t, dest, "vars")
assert.Contains(t, dest, "env")
assert.NotContains(t, dest, "settings")
}
func TestAddSectionsToComponentEntry_SkipsEmptyMapsWhenIncludeEmptyFalse(t *testing.T) {
dest := map[string]any{}
src := map[string]any{
"vars": map[string]any{}, // empty map → should be skipped
"settings": map[string]any{"key": "val"}, // non-empty → should be included
"label": "not-a-map", // non-map → always included
}
addSectionsToComponentEntry(dest, src, nil, false)
assert.NotContains(t, dest, "vars")
assert.Contains(t, dest, "settings")
assert.Contains(t, dest, "label")
}
func TestAddSectionsToComponentEntry_IncludesEmptyMapsWhenIncludeEmptyTrue(t *testing.T) {
dest := map[string]any{}
src := map[string]any{
"vars": map[string]any{},
}
addSectionsToComponentEntry(dest, src, nil, true)
assert.Contains(t, dest, "vars")
}
// ---------------------------------------------------------------------------
// hasStackExplicitComponents
// ---------------------------------------------------------------------------
func TestHasStackExplicitComponents(t *testing.T) {
tests := []struct {
name string
stack map[string]any
want bool
}{
{"with terraform", map[string]any{cfg.ComponentsSectionName: map[string]any{cfg.TerraformSectionName: map[string]any{"vpc": map[string]any{}}}}, true},
{"empty terraform", map[string]any{cfg.ComponentsSectionName: map[string]any{cfg.TerraformSectionName: map[string]any{}}}, false},
{"with helmfile", map[string]any{cfg.ComponentsSectionName: map[string]any{cfg.HelmfileSectionName: map[string]any{"chart": map[string]any{}}}}, true},
{"with packer", map[string]any{cfg.ComponentsSectionName: map[string]any{cfg.PackerSectionName: map[string]any{"ami": map[string]any{}}}}, true},
{"with ansible", map[string]any{cfg.ComponentsSectionName: map[string]any{cfg.AnsibleSectionName: map[string]any{"playbook": map[string]any{}}}}, true},
{"no components", map[string]any{}, false},
{"nil components", map[string]any{cfg.ComponentsSectionName: nil}, false},
{"wrong type", map[string]any{cfg.ComponentsSectionName: "not-a-map"}, false},
}
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
assert.Equal(t, tc.want, hasStackExplicitComponents(tc.stack))
})
}
}
// ---------------------------------------------------------------------------
// hasStackImports
// ---------------------------------------------------------------------------
func TestHasStackImports_WithImports(t *testing.T) {
stack := map[string]any{
"import": []any{"stacks/catalog/vpc.yaml"},
}
assert.True(t, hasStackImports(stack))
}
func TestHasStackImports_EmptyImports(t *testing.T) {
stack := map[string]any{
"import": []any{},
}
assert.False(t, hasStackImports(stack))
}
func TestHasStackImports_NoImports(t *testing.T) {
assert.False(t, hasStackImports(map[string]any{}))
}
// ---------------------------------------------------------------------------
// stackHasNonEmptyComponents
// ---------------------------------------------------------------------------
func TestStackHasNonEmptyComponents_WithVars(t *testing.T) {
comps := map[string]any{
"terraform": map[string]any{
"vpc": map[string]any{
"vars": map[string]any{"cidr": "10.0.0.0/8"},
},
},
}
assert.True(t, stackHasNonEmptyComponents(comps))
}
func TestStackHasNonEmptyComponents_WithWorkspace(t *testing.T) {
comps := map[string]any{
"terraform": map[string]any{
"vpc": map[string]any{
"workspace": "prod",
},
},
}
assert.True(t, stackHasNonEmptyComponents(comps))
}
func TestStackHasNonEmptyComponents_WithMetadata(t *testing.T) {
comps := map[string]any{
"terraform": map[string]any{
"vpc": map[string]any{
"metadata": map[string]any{"component": "base"},
},
},
}
assert.True(t, stackHasNonEmptyComponents(comps))
}
func TestStackHasNonEmptyComponents_WithSettings(t *testing.T) {
comps := map[string]any{
"terraform": map[string]any{
"vpc": map[string]any{
"settings": map[string]any{"spacelift": true},
},
},
}
assert.True(t, stackHasNonEmptyComponents(comps))
}
func TestStackHasNonEmptyComponents_WithEnv(t *testing.T) {
comps := map[string]any{
"helmfile": map[string]any{
"chart": map[string]any{
"env": map[string]any{"KEY": "val"},
},
},
}
assert.True(t, stackHasNonEmptyComponents(comps))
}
// TestStackHasNonEmptyComponents_NonStandardSections verifies that components with
// non-standard sections (backend, providers, hooks, etc.) are recognized as non-empty.
// Previously, only 5 whitelisted sections were checked; now any non-empty content counts.
func TestStackHasNonEmptyComponents_NonStandardSections(t *testing.T) {
comps := map[string]any{
"terraform": map[string]any{
"vpc": map[string]any{
"backend": map[string]any{"bucket": "my-bucket"},
},
},
}
assert.True(t, stackHasNonEmptyComponents(comps))
}
// TestStackHasNonEmptyComponents_EmptyComponentContent verifies that components
// with an empty content map are treated as empty.
func TestStackHasNonEmptyComponents_EmptyComponentContent(t *testing.T) {
comps := map[string]any{
"terraform": map[string]any{
"vpc": map[string]any{},
},
}
assert.False(t, stackHasNonEmptyComponents(comps))
}
func TestStackHasNonEmptyComponents_EmptyTypes(t *testing.T) {
comps := map[string]any{
"terraform": map[string]any{},
}
assert.False(t, stackHasNonEmptyComponents(comps))
}
func TestStackHasNonEmptyComponents_ComponentEntryNotMap(t *testing.T) {
// When an individual component entry is not a map, it should be skipped.
comps := map[string]any{
"terraform": map[string]any{
"vpc": "not-a-map", // non-map component entry
},
}
assert.False(t, stackHasNonEmptyComponents(comps))
}
func TestStackHasNonEmptyComponents_WrongTypes(t *testing.T) {
comps := map[string]any{
"terraform": "not-a-map",
}
assert.False(t, stackHasNonEmptyComponents(comps))
}
// ---------------------------------------------------------------------------
// filterEmptyFinalStacks
// ---------------------------------------------------------------------------
func TestFilterEmptyFinalStacks_RemovesEmpty(t *testing.T) {
finalMap := map[string]any{
"prod": map[string]any{
cfg.ComponentsSectionName: map[string]any{
"terraform": map[string]any{
"vpc": map[string]any{
"vars": map[string]any{"cidr": "10.0.0.0/8"},
},
},
},
},
"dev": map[string]any{
cfg.ComponentsSectionName: map[string]any{
"terraform": map[string]any{
"vpc": map[string]any{}, // empty component content.
},
},
},
}
err := filterEmptyFinalStacks(finalMap, false)
require.NoError(t, err)
assert.Contains(t, finalMap, "prod")
assert.NotContains(t, finalMap, "dev")
}
func TestFilterEmptyFinalStacks_RemovesEmptyStackName(t *testing.T) {
finalMap := map[string]any{
"": map[string]any{},
}
err := filterEmptyFinalStacks(finalMap, false)
require.NoError(t, err)
assert.NotContains(t, finalMap, "")
}
func TestFilterEmptyFinalStacks_RemovesNoComponentsSection(t *testing.T) {
finalMap := map[string]any{
"prod": map[string]any{
"other_key": "value",
},
}
err := filterEmptyFinalStacks(finalMap, false)
require.NoError(t, err)
assert.NotContains(t, finalMap, "prod")
}
func TestFilterEmptyFinalStacks_IncludeEmptySkipsFiltering(t *testing.T) {
finalMap := map[string]any{
"prod": map[string]any{},
"dev": map[string]any{},
}
err := filterEmptyFinalStacks(finalMap, true)
require.NoError(t, err)
// Both stacks should remain because includeEmptyStacks=true.
assert.Contains(t, finalMap, "prod")
assert.Contains(t, finalMap, "dev")
}
func TestFilterEmptyFinalStacks_InvalidStackEntry(t *testing.T) {
finalMap := map[string]any{
"prod": "not-a-map",
}
err := filterEmptyFinalStacks(finalMap, false)
require.Error(t, err)
assert.Contains(t, err.Error(), "invalid stack entry type")
}
// ---------------------------------------------------------------------------
// applyTerraformMetadataInheritance – no inheritance (trivial path)
// ---------------------------------------------------------------------------
func TestApplyTerraformMetadataInheritance_NoInheritance(t *testing.T) {
ac := &schema.AtmosConfiguration{}
metadata := map[string]any{"component": "vpc"}
result, err := applyTerraformMetadataInheritance(ac, map[string]any{}, "vpc", "stack.yaml", metadata)
require.NoError(t, err)
assert.Equal(t, metadata, result)
}
func TestApplyTerraformMetadataInheritance_EmptyInheritList(t *testing.T) {
ac := &schema.AtmosConfiguration{}
metadata := map[string]any{
cfg.InheritsSectionName: []any{},
}
result, err := applyTerraformMetadataInheritance(ac, map[string]any{}, "vpc", "stack.yaml", metadata)
require.NoError(t, err)
assert.Equal(t, metadata, result)
}
func TestApplyTerraformMetadataInheritance_SkipsNonStringInherit(t *testing.T) {
ac := &schema.AtmosConfiguration{}
metadata := map[string]any{
cfg.InheritsSectionName: []any{42, nil}, // non-string entries should be skipped
}
// allTerraformComponents has no "42" or nil component, so ProcessBaseComponentConfig would be called 0 times.
allComponents := map[string]any{}
result, err := applyTerraformMetadataInheritance(ac, allComponents, "vpc", "stack.yaml", metadata)
require.NoError(t, err)
// No merge because baseComponentMetadata is empty.
assert.Equal(t, metadata, result)
}
// ---------------------------------------------------------------------------
// applyTerraformMetadataInheritance – with real base component
// ---------------------------------------------------------------------------
func TestApplyTerraformMetadataInheritance_WithBaseComponent(t *testing.T) {
// Default AtmosConfiguration has metadata inheritance enabled (default=true).
ac := &schema.AtmosConfiguration{}
allComponents := map[string]any{
"base-foo-comp": map[string]any{
"metadata": map[string]any{
"foo": "bar",
"baz": "qux",
},
},
}
metadata := map[string]any{
cfg.InheritsSectionName: []any{"base-foo-comp"},
"own-key": "own-value",
}
result, err := applyTerraformMetadataInheritance(
ac, allComponents, "foo-comp", "foo-stack.yaml", metadata,
)
require.NoError(t, err)
// The result should be the merged metadata (base overridden by component).
assert.Equal(t, "bar", result["foo"])
assert.Equal(t, "qux", result["baz"])
assert.Equal(t, "own-value", result["own-key"])
}
func TestApplyTerraformMetadataInheritance_WorkspaceOverride(t *testing.T) {
// When the component has an explicit terraform_workspace in merged metadata,
// the workspace_pattern and workspace_template are deleted.
ac := &schema.AtmosConfiguration{}
allComponents := map[string]any{
"base-ws-comp": map[string]any{
"metadata": map[string]any{
"terraform_workspace_pattern": "{env}-{stage}",
"terraform_workspace_template": "{{ .env }}-{{ .stage }}",
},
},
}
metadata := map[string]any{
cfg.InheritsSectionName: []any{"base-ws-comp"},
"terraform_workspace": "my-custom-workspace",
}
result, err := applyTerraformMetadataInheritance(
ac, allComponents, "ws-comp", "ws-stack.yaml", metadata,
)
require.NoError(t, err)
assert.Equal(t, "my-custom-workspace", result["terraform_workspace"])
// Pattern and template should be removed when explicit workspace is set.
assert.NotContains(t, result, "terraform_workspace_pattern")
assert.NotContains(t, result, "terraform_workspace_template")
}
// ---------------------------------------------------------------------------
// describeStacksProcessor method tests
// ---------------------------------------------------------------------------
// newMinimalProcessor creates a processor with default config and empty result map.
func newMinimalProcessor() *describeStacksProcessor {
return newDescribeStacksProcessor(
&schema.AtmosConfiguration{},
"", // filterByStack
nil, // components
nil, // componentTypes
nil, // sections
false, // processTemplates
false, // processYamlFunctions
false, // includeEmptyStacks
nil, // skip
nil, // authManager
)
}
func TestProcessStackFile_EmptyMap(t *testing.T) {
// When stackMap has no components section, processStackFile should return nil without error.
// (The old "not-a-map" test is superseded: the signature now takes map[string]any so
// the compiler enforces type safety at the call site.)
p := newMinimalProcessor()
err := p.processStackFile("test.yaml", map[string]any{})
require.NoError(t, err)
}
func TestProcessStackFile_NoComponentsSection(t *testing.T) {
// When the stack has no "components" key, processStackFile should return nil.
p := newMinimalProcessor()
stackMap := map[string]any{"other_key": "value"}
err := p.processStackFile("test.yaml", stackMap)
require.NoError(t, err)
}
func TestProcessStackFile_ComponentTypeFilterSkipsNonMatching(t *testing.T) {
// When componentTypes filter is set, types not in the filter should be skipped.
p := newDescribeStacksProcessor(
&schema.AtmosConfiguration{},
"",
nil,
[]string{"helmfile"}, // only helmfile, not terraform
nil,
false, false, false, nil, nil,
)
stackMap := map[string]any{
cfg.ComponentsSectionName: map[string]any{
cfg.TerraformSectionName: map[string]any{
"vpc": map[string]any{"vars": map[string]any{}},
},
// No helmfile components
},
}
err := p.processStackFile("test.yaml", stackMap)
require.NoError(t, err)
// Terraform was filtered out, no components should be in finalStacksMap.
}
func TestProcessStackFile_TypeSectionNotAMap(t *testing.T) {
// When a component type section is not a map, the type should be skipped (continue).
p := newMinimalProcessor()
stackMap := map[string]any{
cfg.ComponentsSectionName: map[string]any{
cfg.TerraformSectionName: "not-a-map", // invalid type → continue
},
}
err := p.processStackFile("test.yaml", stackMap)
require.NoError(t, err) // should not fail, just skip
}
func TestProcessStackFile_ProcessComponentTypeSectionError(t *testing.T) {
// When a component type section contains a non-map component entry, processComponentTypeSection
// should return an error which processStackFile propagates.
p := newMinimalProcessor()
stackMap := map[string]any{
cfg.ComponentsSectionName: map[string]any{
cfg.TerraformSectionName: map[string]any{
"vpc": "not-a-map", // invalid component entry → error
},
},
}
err := p.processStackFile("test.yaml", stackMap)
require.Error(t, err)
assert.Contains(t, err.Error(), "invalid")
}
func TestProcessComponentTypeSection_ComponentSectionNotMap(t *testing.T) {
// When a component entry in the type section is not a map, an error is returned.
p := newMinimalProcessor()
typeSection := map[string]any{
"vpc": "not-a-map",
}
err := p.processComponentTypeSection(
"test.yaml", "", cfg.TerraformSectionName, typeSection,
processComponentTypeOpts{},
)
require.Error(t, err)
assert.Contains(t, err.Error(), "invalid")
}
func TestProcessComponentEntry_ComponentFilterExcluded(t *testing.T) {
// When p.components filters out this component, processComponentEntry returns nil
// and must NOT create any entry in finalStacksMap (no ghost entries).
p := newDescribeStacksProcessor(
&schema.AtmosConfiguration{},
"",
[]string{"other-component"}, // filter: only "other-component"
nil, nil, false, false, false, nil, nil,
)
componentSection := map[string]any{
cfg.ComponentSectionName: "vpc",
"vars": map[string]any{},
}
allTypeComponents := map[string]any{
"vpc": componentSection,
}
err := p.processComponentEntry(
"test.yaml", "", cfg.TerraformSectionName,
"vpc", componentSection, allTypeComponents,
processComponentTypeOpts{},
)
require.NoError(t, err)
// No entries should be created when the component is filtered out.
assert.Empty(t, p.finalStacksMap, "finalStacksMap must be empty when all components are filtered")
// Verify that componentSection itself was not mutated (no atmos_* keys added).
assert.NotContains(t, componentSection, "atmos_component", "componentSection must not be mutated for filtered components")
}
func TestProcessComponentEntry_EmptyStackName(t *testing.T) {
// When stackFileName is "" and no name template/pattern, stackName falls back to "".
// Line 223-225: stackName = stackFileName (both "").
p := newMinimalProcessor()
componentSection := map[string]any{
cfg.ComponentSectionName: "vpc",
"vars": map[string]any{"region": "us-east-1"},
}
allTypeComponents := map[string]any{
"vpc": componentSection,
}
err := p.processComponentEntry(
"", // empty stackFileName
"", cfg.TerraformSectionName,
"vpc", componentSection, allTypeComponents,
processComponentTypeOpts{},
)
require.NoError(t, err)
// Stack entry for "" should exist.
assert.Contains(t, p.finalStacksMap, "")
}
func TestProcessComponentEntry_ResolveStackNameError(t *testing.T) {
// When NameTemplate is invalid, resolveStackName returns an error.
p := newDescribeStacksProcessor(
&schema.AtmosConfiguration{
Stacks: schema.Stacks{
NameTemplate: "{{.invalid_open", // invalid Go template
},
},
"", nil, nil, nil, false, false, false, nil, nil,
)
componentSection := map[string]any{
cfg.ComponentSectionName: "vpc",
}
allTypeComponents := map[string]any{"vpc": componentSection}
err := p.processComponentEntry(
"test.yaml", "", cfg.TerraformSectionName,
"vpc", componentSection, allTypeComponents,
processComponentTypeOpts{},
)
require.Error(t, err)
}
// ---------------------------------------------------------------------------
// processComponentTypeSection – additional coverage
// ---------------------------------------------------------------------------
func TestProcessComponentTypeSection_DefaultsComponentKey(t *testing.T) {
// When a component section has no "component" key, it should be defaulted to the component name.
// Lines 161-163 in processComponentTypeSection.
p := newMinimalProcessor()
typeSection := map[string]any{
"vpc": map[string]any{
"vars": map[string]any{"region": "us-east-1"},
// No "component" key – should be set to "vpc" by processComponentTypeSection.
},
}
err := p.processComponentTypeSection(
"test.yaml", "", cfg.TerraformSectionName, typeSection,
processComponentTypeOpts{},
)
require.NoError(t, err)
// The original typeSection must NOT be mutated (clone protects the cache).
vpcSection, ok := typeSection["vpc"].(map[string]any)
require.True(t, ok, "vpc section should be map[string]any")
_, hasComponent := vpcSection[cfg.ComponentSectionName]
assert.False(t, hasComponent, "original map must not be mutated — component key should not be set on the input")
}
func TestProcessComponentTypeSection_ProcessComponentEntryError(t *testing.T) {
// When processComponentEntry returns an error (e.g., invalid name template),
// processComponentTypeSection should propagate the error (lines 168-170).
p := newDescribeStacksProcessor(
&schema.AtmosConfiguration{
Stacks: schema.Stacks{
NameTemplate: "{{.bad", // invalid Go template → resolveStackName fails
},
},
"", nil, nil, nil, false, false, false, nil, nil,
)