-
-
Notifications
You must be signed in to change notification settings - Fork 151
Expand file tree
/
Copy pathpacker_build_test.go
More file actions
231 lines (183 loc) · 6.66 KB
/
packer_build_test.go
File metadata and controls
231 lines (183 loc) · 6.66 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
package cmd
import (
"bytes"
"os"
"strings"
"testing"
"github.com/stretchr/testify/assert"
log "github.com/cloudposse/atmos/pkg/logger"
)
// TestPackerBuildCmd tests the packer build command execution.
// This test verifies that packer build executes correctly.
// The actual packer execution may fail due to missing AWS credentials.
// The test verifies command arguments are parsed correctly, component and stack are resolved,
// the variable file is generated, and packer is invoked with correct arguments.
func TestPackerBuildCmd(t *testing.T) {
_ = NewTestKit(t)
skipIfPackerNotInstalled(t)
workDir := "../tests/fixtures/scenarios/packer"
t.Setenv("ATMOS_CLI_CONFIG_PATH", workDir)
t.Setenv("ATMOS_LOGS_LEVEL", "Warning")
log.SetLevel(log.WarnLevel)
// Ensure plugins are installed so build errors are about credentials, not init.
RootCmd.SetArgs([]string{"packer", "init", "aws/bastion", "-s", "nonprod"})
if initErr := Execute(); initErr != nil {
t.Skipf("Skipping test: packer init failed (may require network access): %v", initErr)
}
// Reset for actual test.
_ = NewTestKit(t)
oldStdout := os.Stdout
oldStderr := os.Stderr
r, w, err := os.Pipe()
if err != nil {
t.Fatalf("failed to create pipe for stdout capture: %v", err)
}
os.Stdout = w
os.Stderr = w
log.SetOutput(w)
// Ensure cleanup happens before any reads.
defer func() {
os.Stdout = oldStdout
os.Stderr = oldStderr
log.SetOutput(os.Stderr)
}()
// Run packer build. It will fail due to missing AWS credentials.
// We verify that Atmos correctly processes the command.
RootCmd.SetArgs([]string{"packer", "build", "aws/bastion", "-s", "nonprod"})
err = Execute()
// Close write end after Execute.
_ = w.Close()
// Read the captured output.
var buf bytes.Buffer
_, _ = buf.ReadFrom(r)
output := buf.String()
// The command may fail due to AWS credentials.
// The output should contain packer-specific content, indicating that Atmos invoked packer correctly.
if err == nil {
t.Logf("TestPackerBuildCmd completed successfully (unexpected in test environment)")
return
}
// Skip if plugins are still missing despite init attempt.
if strings.Contains(output, "Missing plugins") {
t.Skipf("Skipping test: packer plugins missing (run packer init): %v", err)
}
// If packer ran and failed due to credentials, that's expected.
// Check that packer actually ran (output contains packer-specific content).
packerRan := strings.Contains(output, "amazon-ebs") ||
strings.Contains(output, "Build") ||
strings.Contains(output, "credential") ||
strings.Contains(output, "Packer")
if packerRan {
t.Logf("Packer build executed but failed (likely due to missing credentials): %v", err)
// Test passes - packer was correctly invoked.
return
}
// If the error is from Atmos (not packer), that's a real failure.
t.Logf("TestPackerBuildCmd output: %s", output)
t.Errorf("Packer build failed unexpectedly: %v", err)
}
func TestPackerBuildCmdInvalidComponent(t *testing.T) {
_ = NewTestKit(t)
skipIfPackerNotInstalled(t)
workDir := "../tests/fixtures/scenarios/packer"
t.Setenv("ATMOS_CLI_CONFIG_PATH", workDir)
t.Setenv("ATMOS_LOGS_LEVEL", "Warning")
log.SetLevel(log.WarnLevel)
// Capture stderr for error messages.
oldStderr := os.Stderr
r, w, err := os.Pipe()
if err != nil {
t.Fatalf("failed to create pipe for stderr capture: %v", err)
}
os.Stderr = w
log.SetOutput(w)
defer func() {
os.Stderr = oldStderr
log.SetOutput(os.Stderr)
}()
RootCmd.SetArgs([]string{"packer", "build", "invalid/component", "-s", "nonprod"})
err = Execute()
// Close write end after Execute.
_ = w.Close()
// Read the captured output.
var buf bytes.Buffer
_, _ = buf.ReadFrom(r)
output := buf.String()
// Should fail with invalid component error.
assert.Error(t, err, "'TestPackerBuildCmdInvalidComponent' should fail for invalid component")
// Log the error for debugging.
t.Logf("TestPackerBuildCmdInvalidComponent error: %v", err)
t.Logf("TestPackerBuildCmdInvalidComponent output: %s", output)
}
func TestPackerBuildCmdMissingStack(t *testing.T) {
_ = NewTestKit(t)
skipIfPackerNotInstalled(t)
workDir := "../tests/fixtures/scenarios/packer"
t.Setenv("ATMOS_CLI_CONFIG_PATH", workDir)
t.Setenv("ATMOS_LOGS_LEVEL", "Warning")
log.SetLevel(log.WarnLevel)
RootCmd.SetArgs([]string{"packer", "build", "aws/bastion"})
err := Execute()
// The command should fail either with "stack is required" or with a packer execution error.
// Both indicate the command was processed.
assert.Error(t, err, "'TestPackerBuildCmdMissingStack' should fail when stack is not specified")
t.Logf("TestPackerBuildCmdMissingStack error: %v", err)
}
func TestPackerBuildCmdWithDirectoryTemplate(t *testing.T) {
_ = NewTestKit(t)
skipIfPackerNotInstalled(t)
workDir := "../tests/fixtures/scenarios/packer"
t.Setenv("ATMOS_CLI_CONFIG_PATH", workDir)
t.Setenv("ATMOS_LOGS_LEVEL", "Warning")
log.SetLevel(log.WarnLevel)
// Ensure plugins are installed so build errors are about credentials, not init.
RootCmd.SetArgs([]string{"packer", "init", "aws/multi-file", "-s", "nonprod"})
if initErr := Execute(); initErr != nil {
t.Skipf("Skipping test: packer init failed (may require network access): %v", initErr)
}
// Reset for actual test.
_ = NewTestKit(t)
oldStdout := os.Stdout
oldStderr := os.Stderr
r, w, err := os.Pipe()
if err != nil {
t.Fatalf("failed to create pipe for stdout capture: %v", err)
}
os.Stdout = w
os.Stderr = w
log.SetOutput(w)
defer func() {
os.Stdout = oldStdout
os.Stderr = oldStderr
log.SetOutput(os.Stderr)
}()
// Test with explicit directory template flag (directory mode).
// This uses "." to load all *.pkr.hcl files from the component directory.
RootCmd.SetArgs([]string{"packer", "build", "aws/multi-file", "-s", "nonprod", "--template", "."})
err = Execute()
// Close write end after Execute.
_ = w.Close()
// Read the captured output.
var buf bytes.Buffer
_, _ = buf.ReadFrom(r)
output := buf.String()
// The command may fail due to AWS credentials, but verify packer was invoked.
if err == nil {
return
}
// Skip if plugins are still missing despite init attempt.
if strings.Contains(output, "Missing plugins") {
t.Skipf("Skipping test: packer plugins missing (run packer init): %v", err)
}
packerRan := strings.Contains(output, "amazon-ebs") ||
strings.Contains(output, "Build") ||
strings.Contains(output, "credential") ||
strings.Contains(output, "Packer")
if packerRan {
t.Logf("Packer build with directory template executed (failed due to credentials): %v", err)
// Test passes.
return
}
t.Logf("TestPackerBuildCmdWithDirectoryTemplate output: %s", output)
t.Errorf("Packer build with directory template failed unexpectedly: %v", err)
}