-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathbuilder_acc_test.go
More file actions
295 lines (250 loc) · 6.81 KB
/
builder_acc_test.go
File metadata and controls
295 lines (250 loc) · 6.81 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
//go:build integration
package upcloud //nolint:testpackage // not all fields can be exported in Artifact
import (
"errors"
"fmt"
"io"
"os"
"os/exec"
"regexp"
"strings"
"testing"
"time"
"github.com/hashicorp/packer-plugin-sdk/acctest"
"github.com/UpCloudLtd/packer-plugin-upcloud/internal/driver"
_ "embed"
)
// Run tests: PACKER_ACC=1 go test -count 1 -v ./... -timeout=120m
const defaultTestTimeout = 15 * time.Minute
// json
//go:embed test-fixtures/json/basic.json
var testBuildBasic string
//go:embed test-fixtures/json/storage-uuid.json
var testBuilderStorageUUID string
//go:embed test-fixtures/json/storage-name.json
var testBuilderStorageName string
//go:embed test-fixtures/json/networking.json
var testBuilderNetworking string
//go:embed test-fixtures/json/plan_and_tier.json
var testBuilderPlanAndTier string
func TestBuilderAcc_default(t *testing.T) {
t.Parallel()
testAccPreCheck(t)
testCase := &acctest.PluginTestCase{
Name: t.Name(),
Template: testBuildBasic,
Check: checkTestResult(t),
Teardown: teardown(t, t.Name()),
}
acctest.TestPlugin(t, testCase)
}
func TestBuilderAcc_storageUuid(t *testing.T) {
t.Parallel()
testAccPreCheck(t)
testCase := &acctest.PluginTestCase{
Name: t.Name(),
Template: testBuilderStorageUUID,
Check: checkTestResult(t),
Teardown: teardown(t, t.Name()),
}
acctest.TestPlugin(t, testCase)
}
func TestBuilderAcc_storageName(t *testing.T) {
t.Parallel()
testAccPreCheck(t)
testCase := &acctest.PluginTestCase{
Name: t.Name(),
Template: testBuilderStorageName,
Check: checkTestResult(t),
Teardown: teardown(t, t.Name()),
}
acctest.TestPlugin(t, testCase)
}
func TestBuilderAcc_planAndTier(t *testing.T) {
t.Parallel()
testAccPreCheck(t)
testCase := &acctest.PluginTestCase{
Name: t.Name(),
Template: testBuilderPlanAndTier,
Check: checkTestResult(t),
Teardown: teardown(t, t.Name()),
}
acctest.TestPlugin(t, testCase)
}
func TestBuilderAcc_networking(t *testing.T) {
t.Parallel()
testAccPreCheck(t)
testCase := &acctest.PluginTestCase{
Name: t.Name(),
Template: testBuilderNetworking,
Check: checkTestResult(t),
Teardown: teardown(t, t.Name()),
}
acctest.TestPlugin(t, testCase)
}
// pkr.hcl
//go:embed test-fixtures/hcl2/basic.pkr.hcl
var testBuildBasicHcl string
//go:embed test-fixtures/hcl2/plan_and_tier.pkr.hcl
var testBuilderPlanAndTierHcl string
//go:embed test-fixtures/hcl2/storage-uuid.pkr.hcl
var testBuilderStorageUUIDHcl string
//go:embed test-fixtures/hcl2/storage-name.pkr.hcl
var testBuilderStorageNameHcl string
//go:embed test-fixtures/hcl2/network_interfaces.pkr.hcl
var testBuilderNetworkInterfacesHcl string
func TestBuilderAcc_default_hcl(t *testing.T) {
t.Parallel()
testAccPreCheck(t)
testCase := &acctest.PluginTestCase{
Name: t.Name(),
Template: testBuildBasicHcl,
Check: checkTestResult(t),
Teardown: teardown(t, t.Name()),
}
acctest.TestPlugin(t, testCase)
}
func TestBuilderAcc_planAndTier_hcl(t *testing.T) {
t.Parallel()
testAccPreCheck(t)
testCase := &acctest.PluginTestCase{
Name: t.Name(),
Template: testBuilderPlanAndTierHcl,
Check: checkTestResult(t),
Teardown: teardown(t, t.Name()),
}
acctest.TestPlugin(t, testCase)
}
func TestBuilderAcc_storageUuid_hcl(t *testing.T) {
t.Parallel()
testAccPreCheck(t)
testCase := &acctest.PluginTestCase{
Name: t.Name(),
Template: testBuilderStorageUUIDHcl,
Check: checkTestResult(t),
Teardown: teardown(t, t.Name()),
}
acctest.TestPlugin(t, testCase)
}
func TestBuilderAcc_storageName_hcl(t *testing.T) {
t.Parallel()
testAccPreCheck(t)
testCase := &acctest.PluginTestCase{
Name: t.Name(),
Template: testBuilderStorageNameHcl,
Check: checkTestResult(t),
Teardown: teardown(t, t.Name()),
}
acctest.TestPlugin(t, testCase)
}
func TestBuilderAcc_network_interfaces_hcl(t *testing.T) {
t.Parallel()
testAccPreCheck(t)
testCase := &acctest.PluginTestCase{
Name: t.Name(),
Template: testBuilderNetworkInterfacesHcl,
Check: func(buildCommand *exec.Cmd, logfile string) error {
re := regexp.MustCompile(`upcloud.network_interfaces: Selecting default ip '10.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}' as Server IP`)
log, err := readLog(t, logfile)
if err != nil {
return err
}
// Log content is checked via regex, no need to print it
if !re.MatchString(log) {
return fmt.Errorf("Unable find default utility network IP from the log %s", logfile)
}
return nil
},
Teardown: teardown(t, t.Name()),
}
acctest.TestPlugin(t, testCase)
}
func testAccPreCheck(t *testing.T) {
t.Helper()
_, err := driver.CredentialsFromEnv("", "", "")
if err != nil {
t.Skip(err.Error())
}
}
func readLog(t *testing.T, logfile string) (string, error) {
t.Helper()
logs, err := os.Open(logfile) // #nosec G304 -- logfile path is controlled by Packer SDK acctest framework
if err != nil {
return "", fmt.Errorf("Unable find %s", logfile)
}
defer func() {
_ = logs.Close()
}()
logsBytes, err := io.ReadAll(logs)
if err != nil {
return "", fmt.Errorf("Unable to read %s", logfile)
}
return string(logsBytes), nil
}
func checkTestResult(t *testing.T) func(*exec.Cmd, string) error {
t.Helper()
return func(buildCommand *exec.Cmd, logfile string) error {
log, err := readLog(t, logfile)
if err != nil {
return err
}
if buildCommand.ProcessState != nil {
if buildCommand.ProcessState.ExitCode() != 0 {
return fmt.Errorf("Bad exit code. Logfile: %s", logfile)
}
}
_, err = getUuidsFromLog(t, log)
if err != nil {
return err
}
return nil
}
}
var re = regexp.MustCompile(`"Storage template created, UUID: (.*?)"`)
func getUuidsFromLog(t *testing.T, log string) ([]string, error) {
t.Helper()
var match string
ms := re.FindAllStringSubmatch(log, -1)
for _, m := range ms {
match = m[1]
}
if match == "" {
return nil, errors.New("Created template UUIDs not found in the log")
}
uuid := []string{}
for _, item := range strings.Split(match, ",") {
item = strings.TrimSpace(item)
uuid = append(uuid, item)
}
return uuid, nil
}
func teardown(t *testing.T, testName string) func() error {
t.Helper()
logfile := fmt.Sprintf("packer_log_%s.txt", testName)
return func() error {
ctx, cancel := contextWithDefaultTimeout()
defer cancel()
log, err := readLog(t, logfile)
if err != nil {
return err
}
uuids, err := getUuidsFromLog(t, log)
if err != nil {
return err
}
creds, _ := driver.CredentialsFromEnv("", "", "")
drv := driver.NewDriver(&driver.DriverConfig{
Username: creds.Username,
Password: creds.Password,
Token: creds.Token,
Timeout: defaultTestTimeout,
})
for _, u := range uuids {
t.Logf("Cleaning up created templates: %s", u)
if err := drv.DeleteTemplate(ctx, u); err != nil {
return fmt.Errorf("failed to delete template %s during teardown: %w", u, err)
}
}
return nil
}
}