-
-
Notifications
You must be signed in to change notification settings - Fork 174
Expand file tree
/
Copy pathdescribe_workflows_test.go
More file actions
390 lines (337 loc) · 11.1 KB
/
Copy pathdescribe_workflows_test.go
File metadata and controls
390 lines (337 loc) · 11.1 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
package exec
import (
"os"
"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"
"github.com/cloudposse/atmos/pkg/schema"
)
// setupTestWorkflowEnvironment creates a temporary test environment with the necessary directory structure and configuration files.
// It returns the temporary directory path.
func setupTestWorkflowEnvironment(t *testing.T) string {
tmpDir := t.TempDir()
workflowsDir := filepath.Join(tmpDir, "stacks", "workflows")
err := os.MkdirAll(workflowsDir, 0o755)
require.NoError(t, err)
atmosConfig := `
base_path: ""
stacks:
base_path: "stacks"
included_paths:
- "**/*"
workflows:
base_path: "stacks/workflows"
`
err = os.WriteFile(filepath.Join(tmpDir, "atmos.yaml"), []byte(atmosConfig), 0o644)
require.NoError(t, err)
return tmpDir
}
// createTestWorkflowFile creates a workflow file in the specified directory with the given content.
func createTestWorkflowFile(t *testing.T, dir string, filename string, content string) {
workflowPath := filepath.Join(dir, filename)
err := os.WriteFile(workflowPath, []byte(content), 0o644)
require.NoError(t, err)
}
// initTestConfig initializes the Atmos configuration for testing.
func initTestConfig(t *testing.T) schema.AtmosConfiguration {
config, err := cfg.InitCliConfig(schema.ConfigAndStacksInfo{}, false)
require.NoError(t, err)
return config
}
func TestExecuteDescribeWorkflows(t *testing.T) {
// Setup test environment
tmpDir := setupTestWorkflowEnvironment(t)
t.Setenv("ATMOS_CLI_CONFIG_PATH", tmpDir)
workflowsDir := filepath.Join(tmpDir, "stacks", "workflows")
// Create test workflow files
workflow1Content := `
workflows:
test-workflow-1:
description: "Test workflow 1"
steps:
- name: "step1"
type: "shell"
command: "echo 'Step 1'"
`
createTestWorkflowFile(t, workflowsDir, "workflow1.yaml", workflow1Content)
workflow2Content := `
workflows:
test-workflow-2:
description: "Test workflow 2"
steps:
- name: "step1"
type: "shell"
command: "echo 'Step 1'"
`
createTestWorkflowFile(t, workflowsDir, "workflow2.yaml", workflow2Content)
// Initialize Atmos config
config := initTestConfig(t)
// Update config with the correct base path
config.BasePath = tmpDir
config.Workflows.BasePath = "stacks/workflows"
// Refresh derived absolute paths (e.g. WorkflowsDirAbsolutePath) after mutating BasePath
// directly -- real callers only ever get a fresh AtmosConfiguration from InitCliConfig,
// which computes these together; a manual post-load field mutation must recompute them
// too or ExecuteDescribeWorkflows would resolve against the stale pre-mutation paths.
require.NoError(t, cfg.AtmosConfigAbsolutePaths(&config))
tests := []struct {
name string
config schema.AtmosConfiguration
wantErr bool
wantSentinel error
errContains string
wantWorkflows int
}{
{
name: "valid workflows",
config: config,
wantErr: false,
wantWorkflows: 2,
},
{
name: "missing workflows base path",
config: schema.AtmosConfiguration{
Workflows: schema.Workflows{
BasePath: "",
},
},
wantErr: true,
wantSentinel: errUtils.ErrWorkflowBasePathNotConfigured,
},
{
name: "nonexistent workflows directory",
config: schema.AtmosConfiguration{
Workflows: schema.Workflows{
BasePath: "nonexistent",
},
},
wantErr: true,
errContains: "workflow directory does not exist: 'nonexistent'",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
listResult, mapResult, allResult, err := ExecuteDescribeWorkflows(tt.config)
if tt.wantErr {
assert.Error(t, err)
if tt.wantSentinel != nil {
assert.ErrorIs(t, err, tt.wantSentinel)
}
if tt.errContains != "" {
assert.Contains(t, err.Error(), tt.errContains)
}
} else {
assert.NoError(t, err)
assert.Len(t, listResult, tt.wantWorkflows)
assert.Len(t, mapResult, tt.wantWorkflows)
assert.Len(t, allResult, tt.wantWorkflows)
}
})
}
}
func TestFindWorkflowAcrossFiles(t *testing.T) {
tmpDir := setupTestWorkflowEnvironment(t)
t.Setenv("ATMOS_CLI_CONFIG_PATH", tmpDir)
workflowsDir := filepath.Join(tmpDir, "stacks", "workflows")
// Create workflow files with duplicate workflow names.
workflow1Content := `
workflows:
deploy:
description: "Deploy infrastructure from file 1"
steps:
- name: "step1"
type: "shell"
command: "echo 'Deploying from file 1'"
test:
description: "Run tests"
steps:
- name: "test"
type: "shell"
command: "echo 'Testing'"
`
createTestWorkflowFile(t, workflowsDir, "infrastructure.yaml", workflow1Content)
workflow2Content := `
workflows:
deploy:
description: "Deploy infrastructure from file 2"
steps:
- name: "step1"
type: "shell"
command: "echo 'Deploying from file 2'"
cleanup:
description: "Cleanup resources"
steps:
- name: "cleanup"
type: "shell"
command: "echo 'Cleaning up'"
`
createTestWorkflowFile(t, workflowsDir, "maintenance.yaml", workflow2Content)
config := initTestConfig(t)
config.BasePath = tmpDir
config.Workflows.BasePath = "stacks/workflows"
require.NoError(t, cfg.AtmosConfigAbsolutePaths(&config))
tests := []struct {
name string
workflowName string
wantErr bool
expectedMatches int
checkDescriptions bool
}{
{
name: "find workflow with multiple matches",
workflowName: "deploy",
wantErr: false,
expectedMatches: 2,
checkDescriptions: true,
},
{
name: "find workflow with single match",
workflowName: "test",
wantErr: false,
expectedMatches: 1,
},
{
name: "workflow not found",
workflowName: "nonexistent",
wantErr: false,
expectedMatches: 0,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
matches, err := findWorkflowAcrossFiles(tt.workflowName, &config)
if tt.wantErr {
assert.Error(t, err)
} else {
assert.NoError(t, err)
assert.Len(t, matches, tt.expectedMatches)
if tt.checkDescriptions && len(matches) > 0 {
// Verify descriptions are populated.
for _, match := range matches {
assert.NotEmpty(t, match.Description)
assert.Contains(t, match.Description, "Deploy infrastructure")
}
}
// Verify all matches have the correct workflow name.
for _, match := range matches {
assert.Equal(t, tt.workflowName, match.Name)
assert.NotEmpty(t, match.File)
}
}
})
}
}
func TestFindWorkflowAcrossFiles_ExecuteDescribeWorkflowsError(t *testing.T) {
// Config with invalid workflows base path.
config := schema.AtmosConfiguration{
Workflows: schema.Workflows{
BasePath: "",
},
}
matches, err := findWorkflowAcrossFiles("deploy", &config)
assert.Error(t, err)
assert.Nil(t, matches)
assert.ErrorIs(t, err, errUtils.ErrWorkflowBasePathNotConfigured)
}
func TestExecuteDescribeWorkflows_InvalidYAMLFile(t *testing.T) {
tmpDir := setupTestWorkflowEnvironment(t)
t.Setenv("ATMOS_CLI_CONFIG_PATH", tmpDir)
workflowsDir := filepath.Join(tmpDir, "stacks", "workflows")
// Create a valid workflow file.
validWorkflow := `
workflows:
deploy:
description: "Valid workflow"
steps:
- name: "step1"
type: "shell"
command: "echo 'valid'"
`
createTestWorkflowFile(t, workflowsDir, "valid.yaml", validWorkflow)
// Create an invalid YAML file.
invalidYAML := `this is not valid yaml: [[[`
createTestWorkflowFile(t, workflowsDir, "invalid.yaml", invalidYAML)
config := initTestConfig(t)
config.BasePath = tmpDir
config.Workflows.BasePath = "stacks/workflows"
require.NoError(t, cfg.AtmosConfigAbsolutePaths(&config))
// Should continue processing and return valid workflows despite invalid file.
listResult, _, _, err := ExecuteDescribeWorkflows(config)
// Should not error - invalid files are logged and skipped.
assert.NoError(t, err)
// Should still find the valid workflow.
assert.Len(t, listResult, 1)
assert.Equal(t, "deploy", listResult[0].Workflow)
}
func TestExecuteDescribeWorkflows_FileWithoutWorkflowsKey(t *testing.T) {
tmpDir := setupTestWorkflowEnvironment(t)
t.Setenv("ATMOS_CLI_CONFIG_PATH", tmpDir)
workflowsDir := filepath.Join(tmpDir, "stacks", "workflows")
// Create a valid workflow file.
validWorkflow := `
workflows:
deploy:
description: "Valid workflow"
steps:
- name: "step1"
type: "shell"
command: "echo 'valid'"
`
createTestWorkflowFile(t, workflowsDir, "valid.yaml", validWorkflow)
// Create a file without workflows key.
noWorkflowsKey := `
some_other_key:
value: "not a workflow file"
`
createTestWorkflowFile(t, workflowsDir, "not-workflows.yaml", noWorkflowsKey)
config := initTestConfig(t)
config.BasePath = tmpDir
config.Workflows.BasePath = "stacks/workflows"
require.NoError(t, cfg.AtmosConfigAbsolutePaths(&config))
// Should continue processing and return valid workflows.
listResult, _, _, err := ExecuteDescribeWorkflows(config)
// Should not error - files without workflows key are logged and skipped.
assert.NoError(t, err)
// Should still find the valid workflow.
assert.Len(t, listResult, 1)
assert.Equal(t, "deploy", listResult[0].Workflow)
}
func TestExecuteDescribeWorkflows_EmptyWorkflowsDirectory(t *testing.T) {
tmpDir := setupTestWorkflowEnvironment(t)
t.Setenv("ATMOS_CLI_CONFIG_PATH", tmpDir)
config := initTestConfig(t)
config.BasePath = tmpDir
config.Workflows.BasePath = "stacks/workflows"
require.NoError(t, cfg.AtmosConfigAbsolutePaths(&config))
// Empty workflows directory.
listResult, mapResult, allResult, err := ExecuteDescribeWorkflows(config)
assert.NoError(t, err)
assert.Len(t, listResult, 0)
assert.Len(t, mapResult, 0)
assert.Len(t, allResult, 0)
}
// TestExecuteDescribeWorkflows_PathLeak guards against a bug found during a field-test pass on
// cloudposse/atmos#2867/#2868: "the workflow directory '%s' does not exist" interpolated the
// resolved absolute workflowsDir directly, and the sibling "error reading the directory" message
// interpolated the raw, unresolved atmosConfig.Workflows.BasePath instead of workflowsDir (the
// directory actually searched) -- a copy/paste inconsistency with the sibling branch one line up.
// The pre-existing "nonexistent workflows directory" test above doesn't catch either bug: it
// leaves BasePath/WorkflowsDirAbsolutePath unset, so getWorkflowsDirToUse falls back to a bare
// relative path that was never absolute to begin with.
func TestExecuteDescribeWorkflows_PathLeak(t *testing.T) {
resolvedDir := resolvedTempDir(t)
config := schema.AtmosConfiguration{
BasePath: resolvedDir,
Workflows: schema.Workflows{
BasePath: "nonexistent-workflows-dir",
},
}
require.NoError(t, cfg.AtmosConfigAbsolutePaths(&config))
_, _, _, err := ExecuteDescribeWorkflows(config)
require.Error(t, err)
assert.NotContains(t, err.Error(), resolvedDir, "error must not leak the machine-specific absolute directory")
assert.Contains(t, err.Error(), "nonexistent-workflows-dir")
}