-
-
Notifications
You must be signed in to change notification settings - Fork 147
Expand file tree
/
Copy pathyaml_func_env_test.go
More file actions
265 lines (238 loc) · 7.5 KB
/
yaml_func_env_test.go
File metadata and controls
265 lines (238 loc) · 7.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
package exec
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
cfg "github.com/cloudposse/atmos/pkg/config"
"github.com/cloudposse/atmos/pkg/schema"
u "github.com/cloudposse/atmos/pkg/utils"
)
// TestProcessTagEnv tests the !env YAML function processing.
func TestProcessTagEnv(t *testing.T) {
tests := []struct {
name string
input string
envVar string
envValue string
expected string
}{
{
name: "simple env var",
input: "!env TEST_VAR_1",
envVar: "TEST_VAR_1",
envValue: "test-value-1",
expected: "test-value-1",
},
{
name: "env var with underscores",
input: "!env TEST_VAR_WITH_UNDERSCORES",
envVar: "TEST_VAR_WITH_UNDERSCORES",
envValue: "value-with-underscores",
expected: "value-with-underscores",
},
{
name: "env var with numeric suffix",
input: "!env MY_VAR_123",
envVar: "MY_VAR_123",
envValue: "numeric-suffix-value",
expected: "numeric-suffix-value",
},
{
name: "env var with empty value",
input: "!env EMPTY_VAR",
envVar: "EMPTY_VAR",
envValue: "",
expected: "",
},
{
name: "env var with special characters in value",
input: "!env SPECIAL_VAR",
envVar: "SPECIAL_VAR",
envValue: "value!@#$%^&*()",
expected: "value!@#$%^&*()",
},
{
name: "env var with spaces in value",
input: "!env SPACES_VAR",
envVar: "SPACES_VAR",
envValue: "value with spaces",
expected: "value with spaces",
},
{
name: "env var with newlines in value",
input: "!env NEWLINES_VAR",
envVar: "NEWLINES_VAR",
envValue: "line1\nline2\nline3",
expected: "line1\nline2\nline3",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
// Set the environment variable.
t.Setenv(tt.envVar, tt.envValue)
// Process the env tag using the utils function.
result, err := u.ProcessTagEnv(tt.input, nil)
require.NoError(t, err)
assert.Equal(t, tt.expected, result)
})
}
}
// TestProcessTagEnvWithDefault tests the !env YAML function with default values.
func TestProcessTagEnvWithDefault(t *testing.T) {
tests := []struct {
name string
input string
envVar string
envValue string
setEnv bool
expected string
}{
{
name: "env var exists, no default used",
input: "!env EXISTING_VAR default-value",
envVar: "EXISTING_VAR",
envValue: "actual-value",
setEnv: true,
expected: "actual-value",
},
{
name: "env var missing, default used",
input: "!env MISSING_VAR_UNIQUE_123456 default-value",
envVar: "MISSING_VAR_UNIQUE_123456",
envValue: "",
setEnv: false,
expected: "default-value",
},
{
name: "env var empty, empty returned",
input: "!env EMPTY_VAR_TEST",
envVar: "EMPTY_VAR_TEST",
envValue: "",
setEnv: true,
expected: "",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.setEnv {
t.Setenv(tt.envVar, tt.envValue)
}
result, err := u.ProcessTagEnv(tt.input, nil)
require.NoError(t, err)
assert.Equal(t, tt.expected, result)
})
}
}
// TestEnvFunctionInYAMLParsing tests the !env function during YAML parsing.
// Note: !env functions are deferred during initial parsing and resolved during stack processing.
func TestEnvFunctionInYAMLParsing(t *testing.T) {
t.Setenv("TEST_YAML_ENV_VAR", "env-value")
t.Setenv("TEST_YAML_ENV_VAR_2", "second-value")
yamlContent := `
components:
terraform:
test-component:
vars:
env_value: !env TEST_YAML_ENV_VAR
env_value_2: !env TEST_YAML_ENV_VAR_2
static_value: "static"
`
atmosConfig := &schema.AtmosConfiguration{}
result, err := u.UnmarshalYAMLFromFile[map[string]interface{}](atmosConfig, yamlContent, "test.yaml")
require.NoError(t, err)
// Navigate to vars.
components := result["components"].(map[string]interface{})
terraform := components["terraform"].(map[string]interface{})
testComponent := terraform["test-component"].(map[string]interface{})
vars := testComponent["vars"].(map[string]interface{})
// During raw YAML parsing, !env functions are stored as strings (deferred).
// They are resolved during stack processing via processCustomYamlTags.
assert.Contains(t, vars["env_value"].(string), "!env", "!env should be deferred during parsing")
assert.Contains(t, vars["env_value_2"].(string), "!env", "!env should be deferred during parsing")
assert.Equal(t, "static", vars["static_value"])
}
// TestEnvFunctionInLists tests the !env function when used in YAML lists.
// Note: !env functions are deferred during initial parsing and resolved during stack processing.
func TestEnvFunctionInLists(t *testing.T) {
t.Setenv("LIST_ENV_1", "value1")
t.Setenv("LIST_ENV_2", "value2")
t.Setenv("LIST_ENV_3", "value3")
yamlContent := `
components:
terraform:
test-component:
vars:
env_list:
- !env LIST_ENV_1
- !env LIST_ENV_2
- !env LIST_ENV_3
mixed_list:
- "static"
- !env LIST_ENV_1
- "another-static"
`
atmosConfig := &schema.AtmosConfiguration{}
result, err := u.UnmarshalYAMLFromFile[map[string]interface{}](atmosConfig, yamlContent, "test.yaml")
require.NoError(t, err)
components := result["components"].(map[string]interface{})
terraform := components["terraform"].(map[string]interface{})
testComponent := terraform["test-component"].(map[string]interface{})
vars := testComponent["vars"].(map[string]interface{})
// During raw YAML parsing, !env functions in lists are stored as strings (deferred).
envList := vars["env_list"].([]interface{})
assert.Equal(t, 3, len(envList), "List should have 3 items")
assert.Contains(t, envList[0].(string), "!env", "List items with !env should be deferred")
assert.Contains(t, envList[1].(string), "!env", "List items with !env should be deferred")
assert.Contains(t, envList[2].(string), "!env", "List items with !env should be deferred")
// Verify mixed list - static values are resolved, !env is deferred.
mixedList := vars["mixed_list"].([]interface{})
assert.Equal(t, 3, len(mixedList), "Mixed list should have 3 items")
assert.Equal(t, "static", mixedList[0], "Static values should be preserved")
assert.Contains(t, mixedList[1].(string), "!env", "!env items should be deferred")
assert.Equal(t, "another-static", mixedList[2], "Static values should be preserved")
}
// TestEnvFunctionErrorCases tests error handling for the !env function.
func TestEnvFunctionErrorCases(t *testing.T) {
tests := []struct {
name string
input string
expectErr bool
}{
{
name: "empty env var name",
input: "!env ",
expectErr: true,
},
{
name: "env tag only with no space",
input: "!env",
expectErr: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
_, err := u.ProcessTagEnv(tt.input, nil)
if tt.expectErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
}
})
}
}
// TestEnvFunctionIntegration tests the !env function in a full stack context.
func TestEnvFunctionIntegration(t *testing.T) {
// Create minimal config for testing.
info := schema.ConfigAndStacksInfo{}
atmosConfig, err := cfg.InitCliConfig(info, false)
if err != nil {
t.Skip("Skipping integration test - cannot initialize config")
}
// Set test environment variables.
t.Setenv("ATMOS_TEST_REGION", "us-west-2")
t.Setenv("ATMOS_TEST_ENVIRONMENT", "production")
t.Run("env vars in config", func(t *testing.T) {
// Verify atmos config loaded.
require.NotNil(t, atmosConfig)
})
}