-
-
Notifications
You must be signed in to change notification settings - Fork 153
Expand file tree
/
Copy pathtemplate_utils_env_test.go
More file actions
512 lines (442 loc) · 13.2 KB
/
template_utils_env_test.go
File metadata and controls
512 lines (442 loc) · 13.2 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
package exec
import (
"os"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/cloudposse/atmos/pkg/schema"
)
// TestExtractEnvFromRawMap tests extracting env vars from raw map[string]any.
func TestExtractEnvFromRawMap(t *testing.T) {
t.Run("nil map returns nil", func(t *testing.T) {
result := extractEnvFromRawMap(nil)
assert.Nil(t, result)
})
t.Run("empty map returns nil", func(t *testing.T) {
result := extractEnvFromRawMap(map[string]any{})
assert.Nil(t, result)
})
t.Run("missing templates key returns nil", func(t *testing.T) {
result := extractEnvFromRawMap(map[string]any{
"other": "value",
})
assert.Nil(t, result)
})
t.Run("missing settings key returns nil", func(t *testing.T) {
result := extractEnvFromRawMap(map[string]any{
"templates": map[string]any{
"other": "value",
},
})
assert.Nil(t, result)
})
t.Run("missing env key returns nil", func(t *testing.T) {
result := extractEnvFromRawMap(map[string]any{
"templates": map[string]any{
"settings": map[string]any{
"enabled": true,
},
},
})
assert.Nil(t, result)
})
t.Run("extracts env from map[string]any", func(t *testing.T) {
result := extractEnvFromRawMap(map[string]any{
"templates": map[string]any{
"settings": map[string]any{
"env": map[string]any{
"AWS_PROFILE": "production",
"AWS_REGION": "us-east-1",
},
},
},
})
require.NotNil(t, result)
assert.Equal(t, "production", result["AWS_PROFILE"])
assert.Equal(t, "us-east-1", result["AWS_REGION"])
})
t.Run("extracts env from map[string]string", func(t *testing.T) {
result := extractEnvFromRawMap(map[string]any{
"templates": map[string]any{
"settings": map[string]any{
"env": map[string]string{
"AWS_PROFILE": "staging",
},
},
},
})
require.NotNil(t, result)
assert.Equal(t, "staging", result["AWS_PROFILE"])
})
t.Run("skips non-string values in map[string]any", func(t *testing.T) {
result := extractEnvFromRawMap(map[string]any{
"templates": map[string]any{
"settings": map[string]any{
"env": map[string]any{
"VALID_KEY": "valid",
"INVALID_KEY": 123,
},
},
},
})
require.NotNil(t, result)
assert.Equal(t, "valid", result["VALID_KEY"])
assert.NotContains(t, result, "INVALID_KEY")
})
t.Run("unsupported env type returns nil", func(t *testing.T) {
result := extractEnvFromRawMap(map[string]any{
"templates": map[string]any{
"settings": map[string]any{
"env": []string{"not", "a", "map"},
},
},
})
assert.Nil(t, result, "unsupported type should return nil")
})
t.Run("empty env map returns nil", func(t *testing.T) {
result := extractEnvFromRawMap(map[string]any{
"templates": map[string]any{
"settings": map[string]any{
"env": map[string]any{},
},
},
})
assert.Nil(t, result, "empty env map should return nil")
})
t.Run("all non-string values returns nil", func(t *testing.T) {
result := extractEnvFromRawMap(map[string]any{
"templates": map[string]any{
"settings": map[string]any{
"env": map[string]any{
"KEY1": 42,
"KEY2": true,
},
},
},
})
assert.Nil(t, result, "env map with only non-string values should return nil")
})
}
// TestSetEnvVarsWithRestore tests that env vars are set and restored correctly.
func TestSetEnvVarsWithRestore(t *testing.T) {
t.Run("sets env vars and restores on cleanup", func(t *testing.T) {
// Set an existing var that will be overwritten.
t.Setenv("TEST_EXISTING_VAR", "original_value")
// Ensure a var that doesn't exist (register for cleanup first).
t.Setenv("TEST_NEW_VAR", "")
os.Unsetenv("TEST_NEW_VAR")
envVars := map[string]string{
"TEST_EXISTING_VAR": "new_value",
"TEST_NEW_VAR": "created_value",
}
cleanup, err := setEnvVarsWithRestore(envVars)
require.NoError(t, err)
// Verify vars are set.
assert.Equal(t, "new_value", os.Getenv("TEST_EXISTING_VAR"))
assert.Equal(t, "created_value", os.Getenv("TEST_NEW_VAR"))
// Run cleanup.
cleanup()
// Verify original var is restored.
assert.Equal(t, "original_value", os.Getenv("TEST_EXISTING_VAR"))
// Verify new var is removed.
_, exists := os.LookupEnv("TEST_NEW_VAR")
assert.False(t, exists, "TEST_NEW_VAR should be unset after cleanup")
})
t.Run("empty map produces no-op cleanup", func(t *testing.T) {
cleanup, err := setEnvVarsWithRestore(map[string]string{})
require.NoError(t, err)
cleanup() // Should not panic.
})
}
// TestProcessTmplWithDatasources_EnvVarsFromConfig tests that env vars configured
// in atmosConfig.Templates.Settings.Env are properly set during template processing.
func TestProcessTmplWithDatasources_EnvVarsFromConfig(t *testing.T) {
// Use t.Setenv for automatic restore on test cleanup, then unset for clean state.
t.Setenv("TEST_GOMPLATE_AWS_PROFILE", "")
os.Unsetenv("TEST_GOMPLATE_AWS_PROFILE")
atmosConfig := &schema.AtmosConfiguration{
Templates: schema.Templates{
Settings: schema.TemplatesSettings{
Enabled: true,
Sprig: schema.TemplatesSettingsSprig{
Enabled: true,
},
Gomplate: schema.TemplatesSettingsGomplate{
Enabled: true,
},
Env: map[string]string{
"TEST_GOMPLATE_AWS_PROFILE": "my-profile",
},
},
},
}
configAndStacksInfo := &schema.ConfigAndStacksInfo{}
settingsSection := schema.Settings{}
// Template that reads the env var using the env function (provided via getEnvFuncMap).
tmplValue := `
config:
profile: '{{ env "TEST_GOMPLATE_AWS_PROFILE" }}'
`
tmplData := map[string]any{}
result, err := ProcessTmplWithDatasources(
atmosConfig,
configAndStacksInfo,
settingsSection,
"test-env-from-config",
tmplValue,
tmplData,
true,
)
require.NoError(t, err)
assert.Contains(t, result, "my-profile", "env var from atmosConfig should be available in template")
// Verify cleanup: the env var should be restored to its original state (unset).
_, exists := os.LookupEnv("TEST_GOMPLATE_AWS_PROFILE")
assert.False(t, exists, "TEST_GOMPLATE_AWS_PROFILE should be unset after template processing")
}
// TestProcessTmplWithDatasources_EnvVarsFromStackManifest tests that env vars from
// stack manifest settings override those from CLI config.
func TestProcessTmplWithDatasources_EnvVarsFromStackManifest(t *testing.T) {
t.Setenv("TEST_GOMPLATE_REGION", "")
os.Unsetenv("TEST_GOMPLATE_REGION")
atmosConfig := &schema.AtmosConfiguration{
Templates: schema.Templates{
Settings: schema.TemplatesSettings{
Enabled: true,
Sprig: schema.TemplatesSettingsSprig{
Enabled: true,
},
Gomplate: schema.TemplatesSettingsGomplate{
Enabled: true,
},
Env: map[string]string{
"TEST_GOMPLATE_REGION": "us-east-1",
},
},
},
}
configAndStacksInfo := &schema.ConfigAndStacksInfo{}
// Stack manifest overrides the CLI config env var.
settingsSection := schema.Settings{
Templates: schema.Templates{
Settings: schema.TemplatesSettings{
Env: map[string]string{
"TEST_GOMPLATE_REGION": "eu-west-1",
},
},
},
}
tmplValue := `
config:
region: '{{ env "TEST_GOMPLATE_REGION" }}'
`
tmplData := map[string]any{}
result, err := ProcessTmplWithDatasources(
atmosConfig,
configAndStacksInfo,
settingsSection,
"test-env-stack-override",
tmplValue,
tmplData,
true,
)
require.NoError(t, err)
assert.Contains(t, result, "eu-west-1", "stack manifest env should override CLI config env")
assert.NotContains(t, result, "us-east-1", "CLI config env should be overridden by stack manifest")
}
// TestProcessTmplWithDatasources_EnvVarsCleanedUp tests that env vars are properly
// restored after template processing, preventing pollution across components.
func TestProcessTmplWithDatasources_EnvVarsCleanedUp(t *testing.T) {
// Set an env var that will be overridden during template processing.
t.Setenv("TEST_GOMPLATE_CLEANUP", "original")
atmosConfig := &schema.AtmosConfiguration{
Templates: schema.Templates{
Settings: schema.TemplatesSettings{
Enabled: true,
Sprig: schema.TemplatesSettingsSprig{
Enabled: true,
},
Gomplate: schema.TemplatesSettingsGomplate{
Enabled: true,
},
Env: map[string]string{
"TEST_GOMPLATE_CLEANUP": "overridden",
},
},
},
}
configAndStacksInfo := &schema.ConfigAndStacksInfo{}
settingsSection := schema.Settings{}
tmplValue := `
config:
value: '{{ env "TEST_GOMPLATE_CLEANUP" }}'
`
result, err := ProcessTmplWithDatasources(
atmosConfig,
configAndStacksInfo,
settingsSection,
"test-env-cleanup",
tmplValue,
map[string]any{},
true,
)
require.NoError(t, err)
assert.Contains(t, result, "overridden", "env var should be overridden during processing")
// After processing, the original value should be restored.
assert.Equal(t, "original", os.Getenv("TEST_GOMPLATE_CLEANUP"),
"env var should be restored to original value after processing")
}
// TestProcessTmplWithDatasources_DisabledTemplating tests that when templating is
// disabled, env vars are not set and the template is returned unchanged.
func TestProcessTmplWithDatasources_DisabledTemplating(t *testing.T) {
t.Setenv("TEST_GOMPLATE_DISABLED", "")
os.Unsetenv("TEST_GOMPLATE_DISABLED")
atmosConfig := &schema.AtmosConfiguration{
Templates: schema.Templates{
Settings: schema.TemplatesSettings{
Enabled: false, // Templating disabled.
Env: map[string]string{
"TEST_GOMPLATE_DISABLED": "should-not-be-set",
},
},
},
}
configAndStacksInfo := &schema.ConfigAndStacksInfo{}
settingsSection := schema.Settings{}
tmplValue := `config: '{{ env "TEST_GOMPLATE_DISABLED" }}'`
result, err := ProcessTmplWithDatasources(
atmosConfig,
configAndStacksInfo,
settingsSection,
"test-disabled",
tmplValue,
map[string]any{},
true,
)
require.NoError(t, err)
// Template should be returned unchanged.
assert.Equal(t, tmplValue, result)
// Env var should NOT have been set.
_, exists := os.LookupEnv("TEST_GOMPLATE_DISABLED")
assert.False(t, exists, "env var should not be set when templating is disabled")
}
// TestProcessTmplWithDatasources_NoEnvVars tests that processing works correctly
// when no env vars are configured.
func TestProcessTmplWithDatasources_NoEnvVars(t *testing.T) {
atmosConfig := &schema.AtmosConfiguration{
Templates: schema.Templates{
Settings: schema.TemplatesSettings{
Enabled: true,
Sprig: schema.TemplatesSettingsSprig{
Enabled: true,
},
Gomplate: schema.TemplatesSettingsGomplate{
Enabled: true,
},
// No Env field set.
},
},
}
configAndStacksInfo := &schema.ConfigAndStacksInfo{}
settingsSection := schema.Settings{}
tmplValue := `
config:
value: '{{ .name }}'
`
tmplData := map[string]any{"name": "test"}
result, err := ProcessTmplWithDatasources(
atmosConfig,
configAndStacksInfo,
settingsSection,
"test-no-env",
tmplValue,
tmplData,
true,
)
require.NoError(t, err)
assert.Contains(t, result, "test")
}
// TestProcessTmplWithDatasources_EnvVarsInEvaluationLoop tests that env vars are
// properly re-extracted when template settings are re-decoded in the evaluation loop.
func TestProcessTmplWithDatasources_EnvVarsInEvaluationLoop(t *testing.T) {
t.Setenv("TEST_EVAL_LOOP_VAR", "")
os.Unsetenv("TEST_EVAL_LOOP_VAR")
atmosConfig := &schema.AtmosConfiguration{
Templates: schema.Templates{
Settings: schema.TemplatesSettings{
Enabled: true,
Evaluations: 2, // Multiple evaluations.
Sprig: schema.TemplatesSettingsSprig{
Enabled: true,
},
Gomplate: schema.TemplatesSettingsGomplate{
Enabled: true,
},
Env: map[string]string{
"TEST_EVAL_LOOP_VAR": "eval-value",
},
},
},
}
configAndStacksInfo := &schema.ConfigAndStacksInfo{}
settingsSection := schema.Settings{}
// Template with settings section that will be re-decoded in the evaluation loop.
tmplValue := `
settings:
templates:
settings:
env:
TEST_EVAL_LOOP_VAR: "eval-value"
config:
value: '{{ env "TEST_EVAL_LOOP_VAR" }}'
`
result, err := ProcessTmplWithDatasources(
atmosConfig,
configAndStacksInfo,
settingsSection,
"test-eval-loop",
tmplValue,
map[string]any{},
true,
)
require.NoError(t, err)
assert.Contains(t, result, "eval-value", "env var should be available across evaluation passes")
}
// TestProcessTmplWithDatasources_EnvVarsCaseSensitive tests that env var keys
// preserve their original case (e.g., AWS_PROFILE stays uppercase).
func TestProcessTmplWithDatasources_EnvVarsCaseSensitive(t *testing.T) {
t.Setenv("AWS_TEST_CASE_KEY", "")
os.Unsetenv("AWS_TEST_CASE_KEY")
atmosConfig := &schema.AtmosConfiguration{
Templates: schema.Templates{
Settings: schema.TemplatesSettings{
Enabled: true,
Sprig: schema.TemplatesSettingsSprig{
Enabled: true,
},
Gomplate: schema.TemplatesSettingsGomplate{
Enabled: true,
},
Env: map[string]string{
"AWS_TEST_CASE_KEY": "test-value",
},
},
},
}
configAndStacksInfo := &schema.ConfigAndStacksInfo{}
settingsSection := schema.Settings{}
tmplValue := `
config:
key: '{{ env "AWS_TEST_CASE_KEY" }}'
`
result, err := ProcessTmplWithDatasources(
atmosConfig,
configAndStacksInfo,
settingsSection,
"test-env-case-sensitive",
tmplValue,
map[string]any{},
true,
)
require.NoError(t, err)
assert.Contains(t, result, "test-value", "case-sensitive env var key should work correctly")
}