-
-
Notifications
You must be signed in to change notification settings - Fork 146
Expand file tree
/
Copy pathpath_utils_test.go
More file actions
333 lines (310 loc) · 10.6 KB
/
path_utils_test.go
File metadata and controls
333 lines (310 loc) · 10.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
package exec
import (
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
provWorkdir "github.com/cloudposse/atmos/pkg/provisioner/workdir"
"github.com/cloudposse/atmos/pkg/schema"
)
// TestConstructTerraformComponentWorkingDir_WithWorkdirPath tests workdir path resolution.
func TestConstructTerraformComponentWorkingDir_WithWorkdirPath(t *testing.T) {
// Test workdir path takes precedence (returned verbatim from config).
workdirPath := filepath.Join("workdir", "terraform", "dev-vpc")
atmosConfig := schema.AtmosConfiguration{
BasePath: "base",
Components: schema.Components{
Terraform: schema.Terraform{
BasePath: filepath.Join("components", "terraform"),
},
},
}
info := schema.ConfigAndStacksInfo{
ComponentFolderPrefix: "",
FinalComponent: "vpc",
ComponentSection: map[string]any{
provWorkdir.WorkdirPathKey: workdirPath,
},
}
got := constructTerraformComponentWorkingDir(&atmosConfig, &info)
assert.Equal(t, workdirPath, got)
// Test empty workdir path falls back to standard path.
info2 := schema.ConfigAndStacksInfo{
ComponentFolderPrefix: "",
FinalComponent: "vpc",
ComponentSection: map[string]any{
provWorkdir.WorkdirPathKey: "",
},
}
got2 := constructTerraformComponentWorkingDir(&atmosConfig, &info2)
assert.Equal(t, filepath.Join("base", "components", "terraform", "vpc"), got2)
// Test no workdir path uses standard path.
info3 := schema.ConfigAndStacksInfo{
ComponentFolderPrefix: "",
FinalComponent: "vpc",
ComponentSection: map[string]any{},
}
got3 := constructTerraformComponentWorkingDir(&atmosConfig, &info3)
assert.Equal(t, filepath.Join("base", "components", "terraform", "vpc"), got3)
}
// TestConstructHelmfileComponentWorkingDir_WithWorkdirPath tests workdir path resolution for helmfile.
func TestConstructHelmfileComponentWorkingDir_WithWorkdirPath(t *testing.T) {
// Test workdir path takes precedence (returned verbatim from config).
workdirPath := filepath.Join("workdir", "helmfile", "dev-nginx")
atmosConfig := schema.AtmosConfiguration{
BasePath: "base",
Components: schema.Components{
Helmfile: schema.Helmfile{
BasePath: filepath.Join("components", "helmfile"),
},
},
}
info := schema.ConfigAndStacksInfo{
ComponentFolderPrefix: "",
FinalComponent: "nginx",
ComponentSection: map[string]any{
provWorkdir.WorkdirPathKey: workdirPath,
},
}
got := constructHelmfileComponentWorkingDir(&atmosConfig, &info)
assert.Equal(t, workdirPath, got)
// Test standard path when no workdir.
info2 := schema.ConfigAndStacksInfo{
ComponentFolderPrefix: "",
FinalComponent: "nginx",
ComponentSection: map[string]any{},
}
got2 := constructHelmfileComponentWorkingDir(&atmosConfig, &info2)
assert.Equal(t, filepath.Join("base", "components", "helmfile", "nginx"), got2)
}
// TestConstructPackerComponentWorkingDir_WithWorkdirPath tests workdir path resolution for packer.
func TestConstructPackerComponentWorkingDir_WithWorkdirPath(t *testing.T) {
// Test workdir path takes precedence (returned verbatim from config).
workdirPath := filepath.Join("workdir", "packer", "dev-ami")
atmosConfig := schema.AtmosConfiguration{
BasePath: "base",
Components: schema.Components{
Packer: schema.Packer{
BasePath: filepath.Join("components", "packer"),
},
},
}
info := schema.ConfigAndStacksInfo{
ComponentFolderPrefix: "",
FinalComponent: "ami",
ComponentSection: map[string]any{
provWorkdir.WorkdirPathKey: workdirPath,
},
}
got := constructPackerComponentWorkingDir(&atmosConfig, &info)
assert.Equal(t, workdirPath, got)
// Test standard path when no workdir.
info2 := schema.ConfigAndStacksInfo{
ComponentFolderPrefix: "",
FinalComponent: "ami",
ComponentSection: map[string]any{},
}
got2 := constructPackerComponentWorkingDir(&atmosConfig, &info2)
assert.Equal(t, filepath.Join("base", "components", "packer", "ami"), got2)
}
func TestConstructPackerComponentVarfileName(t *testing.T) {
tests := []struct {
name string
info schema.ConfigAndStacksInfo
want string
}{
{
name: "with empty component folder prefix",
info: schema.ConfigAndStacksInfo{
ContextPrefix: "dev",
Component: "example",
},
want: "dev-example.packer.vars.json",
},
{
name: "with component folder prefix",
info: schema.ConfigAndStacksInfo{
ContextPrefix: "prod",
Component: "webapp",
ComponentFolderPrefixReplaced: "infra",
},
want: "prod-infra-webapp.packer.vars.json",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got := constructPackerComponentVarfileName(&tt.info)
assert.Equal(t, tt.want, got)
})
}
}
func TestConstructPackerComponentWorkingDir(t *testing.T) {
// Test basic working dir with folder prefix.
atmosConfig1 := schema.AtmosConfiguration{
BasePath: "base",
Components: schema.Components{
Packer: schema.Packer{
BasePath: "packer",
},
},
}
info1 := schema.ConfigAndStacksInfo{
ComponentFolderPrefix: "infra",
FinalComponent: "app",
}
got1 := constructPackerComponentWorkingDir(&atmosConfig1, &info1)
assert.Equal(t, filepath.Join("base", "packer", "infra", "app"), got1)
// Test empty folder prefix.
atmosConfig2 := schema.AtmosConfiguration{
BasePath: "root",
Components: schema.Components{
Packer: schema.Packer{
BasePath: "packer-templates",
},
},
}
info2 := schema.ConfigAndStacksInfo{
ComponentFolderPrefix: "",
FinalComponent: "base",
}
got2 := constructPackerComponentWorkingDir(&atmosConfig2, &info2)
assert.Equal(t, filepath.Join("root", "packer-templates", "base"), got2)
}
// TestConstructTerraformComponentVarfilePath_WithWorkdirPath tests varfile path with JIT vendored components.
// This test verifies that varfile paths correctly use workdir paths set by JIT provisioning.
func TestConstructTerraformComponentVarfilePath_WithWorkdirPath(t *testing.T) {
// Test varfile path uses workdir path when set (JIT vendored component scenario).
workdirPath := filepath.Join("workdir", "terraform", "dev-vpc")
atmosConfig := schema.AtmosConfiguration{
BasePath: "base",
Components: schema.Components{
Terraform: schema.Terraform{
BasePath: filepath.Join("components", "terraform"),
},
},
}
info := schema.ConfigAndStacksInfo{
ContextPrefix: "tenant1-ue2-dev",
ComponentFolderPrefix: "",
Component: "vpc",
FinalComponent: "vpc",
ComponentSection: map[string]any{
provWorkdir.WorkdirPathKey: workdirPath,
},
}
got := constructTerraformComponentVarfilePath(&atmosConfig, &info)
assert.Equal(t, filepath.Join(workdirPath, "tenant1-ue2-dev-vpc.terraform.tfvars.json"), got)
// Test varfile path uses standard path when no workdir.
info2 := schema.ConfigAndStacksInfo{
ContextPrefix: "tenant1-ue2-dev",
ComponentFolderPrefix: "",
Component: "vpc",
FinalComponent: "vpc",
ComponentSection: map[string]any{},
}
got2 := constructTerraformComponentVarfilePath(&atmosConfig, &info2)
assert.Equal(t, filepath.Join("base", "components", "terraform", "vpc", "tenant1-ue2-dev-vpc.terraform.tfvars.json"), got2)
}
// TestConstructTerraformComponentWorkingDir_JITVendoredComponent tests working dir for JIT vendored components.
// This simulates the scenario where a component is downloaded via JIT provisioning
// and the workdir path is set by the source provisioner.
func TestConstructTerraformComponentWorkingDir_JITVendoredComponent(t *testing.T) {
tests := []struct {
name string
workdirPath string
expectedPath string
componentName string
hasSource bool
sourceConfig map[string]any
}{
{
name: "JIT vendored component with workdir path",
workdirPath: filepath.Join("tmp", "atmos-vendor", "abc123", "modules", "vpc"),
expectedPath: filepath.Join("tmp", "atmos-vendor", "abc123", "modules", "vpc"),
componentName: "vpc",
hasSource: true,
sourceConfig: map[string]any{
"source": map[string]any{
"uri": "git::https://github.com/cloudposse/terraform-aws-vpc.git?ref=v1.0.0",
},
},
},
{
name: "JIT vendored component with string source",
workdirPath: filepath.Join("tmp", "vendor", "my-component"),
expectedPath: filepath.Join("tmp", "vendor", "my-component"),
componentName: "my-component",
hasSource: true,
sourceConfig: map[string]any{
"source": "git::https://github.com/org/repo.git?ref=main",
},
},
{
name: "Regular component without source (no workdir)",
workdirPath: "",
expectedPath: filepath.Join("base", "components", "terraform", "vpc"),
componentName: "vpc",
hasSource: false,
sourceConfig: map[string]any{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
atmosConfig := schema.AtmosConfiguration{
BasePath: "base",
Components: schema.Components{
Terraform: schema.Terraform{
BasePath: filepath.Join("components", "terraform"),
},
},
}
componentSection := tt.sourceConfig
if tt.workdirPath != "" {
componentSection[provWorkdir.WorkdirPathKey] = tt.workdirPath
}
info := schema.ConfigAndStacksInfo{
ComponentFolderPrefix: "",
FinalComponent: tt.componentName,
ComponentSection: componentSection,
}
got := constructTerraformComponentWorkingDir(&atmosConfig, &info)
assert.Equal(t, tt.expectedPath, got)
})
}
}
func TestConstructPackerComponentVarfilePath(t *testing.T) {
// Test complete path construction.
atmosConfig1 := schema.AtmosConfiguration{
BasePath: "base",
Components: schema.Components{
Packer: schema.Packer{
BasePath: "packer",
},
},
}
info1 := schema.ConfigAndStacksInfo{
ContextPrefix: "dev",
ComponentFolderPrefix: "infra",
Component: "app",
FinalComponent: "app",
}
got1 := constructPackerComponentVarfilePath(&atmosConfig1, &info1)
assert.Equal(t, filepath.Join("base", "packer", "infra", "app", "dev-app.packer.vars.json"), got1)
// Test path with replaced folder prefix.
atmosConfig2 := schema.AtmosConfiguration{
BasePath: "root",
Components: schema.Components{
Packer: schema.Packer{
BasePath: "packer-templates",
},
},
}
info2 := schema.ConfigAndStacksInfo{
ContextPrefix: "prod",
ComponentFolderPrefix: "platform",
ComponentFolderPrefixReplaced: "plat",
Component: "base",
FinalComponent: "base",
}
got2 := constructPackerComponentVarfilePath(&atmosConfig2, &info2)
assert.Equal(t, filepath.Join("root", "packer-templates", "platform", "base", "prod-plat-base.packer.vars.json"), got2)
}