-
Notifications
You must be signed in to change notification settings - Fork 128
Expand file tree
/
Copy pathrun_config_test.go
More file actions
656 lines (586 loc) · 17.8 KB
/
run_config_test.go
File metadata and controls
656 lines (586 loc) · 17.8 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
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
// Copyright IBM Corp. 2013, 2025
// SPDX-License-Identifier: MPL-2.0
package common
import (
"os"
"regexp"
"testing"
"github.com/hashicorp/packer-plugin-sdk/communicator"
"github.com/hashicorp/packer-plugin-sdk/template/config"
)
func init() {
// Clear out the AWS access key env vars so they don't
// affect our tests.
os.Setenv("AWS_ACCESS_KEY_ID", "")
os.Setenv("AWS_ACCESS_KEY", "")
os.Setenv("AWS_SECRET_ACCESS_KEY", "")
os.Setenv("AWS_SECRET_KEY", "")
}
func testConfig() *RunConfig {
return &RunConfig{
SourceAmi: "abcd",
InstanceType: "m1.small",
Comm: communicator.Config{
SSH: communicator.SSH{
SSHUsername: "foo",
},
},
}
}
func testConfigFilter() *RunConfig {
config := testConfig()
config.SourceAmi = ""
config.SourceAmiFilter = AmiFilterOptions{}
return config
}
func TestRunConfigPrepare(t *testing.T) {
c := testConfig()
err := c.Prepare(nil)
if len(err) > 0 {
t.Fatalf("err: %s", err)
}
}
func TestRunConfigPrepare_InstanceType(t *testing.T) {
c := testConfig()
c.InstanceType = ""
if err := c.Prepare(nil); len(err) != 1 {
t.Fatalf("Should error if an instance_type is not specified")
}
}
func TestRunConfigPrepare_SourceAmi(t *testing.T) {
c := testConfig()
c.SourceAmi = ""
if err := c.Prepare(nil); len(err) != 2 {
t.Fatalf("Should error if a source_ami (or source_ami_filter) is not specified")
}
}
func TestRunConfigPrepare_SourceAmiFilterBlank(t *testing.T) {
c := testConfigFilter()
if err := c.Prepare(nil); len(err) != 2 {
t.Fatalf("Should error if source_ami_filter is empty or not specified (and source_ami is not specified)")
}
}
func TestRunConfigPrepare_SourceAmiFilterOwnersBlank(t *testing.T) {
c := testConfigFilter()
filter_key := "name"
filter_value := "foo"
c.SourceAmiFilter.Filters = map[string]string{filter_key: filter_value}
if err := c.Prepare(nil); len(err) != 1 {
t.Fatalf("Should error if Owners is not specified)")
}
}
func TestRunConfigPrepare_SourceAmiFilterGood(t *testing.T) {
c := testConfigFilter()
owner := "123"
filter_key := "name"
filter_value := "foo"
goodFilter := AmiFilterOptions{
Owners: []string{owner},
Filters: map[string]string{filter_key: filter_value},
}
c.SourceAmiFilter = goodFilter
if err := c.Prepare(nil); len(err) != 0 {
t.Fatalf("err: %s", err)
}
}
func TestRunConfigPrepare_EnableT2UnlimitedGood(t *testing.T) {
c := testConfig()
// Must have a T2 instance type if T2 Unlimited is enabled
c.InstanceType = "t2.micro"
c.EnableT2Unlimited = true
err := c.Prepare(nil)
if len(err) > 0 {
t.Fatalf("err: %s", err)
}
}
func TestRunConfigPrepare_EnableT2UnlimitedBadInstanceType(t *testing.T) {
c := testConfig()
// T2 Unlimited cannot be used with instance types other than T2
c.InstanceType = "m5.large"
c.EnableT2Unlimited = true
err := c.Prepare(nil)
if len(err) != 1 {
t.Fatalf("Should error if T2 Unlimited is enabled with non-T2 instance_type")
}
}
func TestRunConfigPrepare_EnableT2UnlimitedBadWithSpotInstanceRequest(t *testing.T) {
c := testConfig()
// T2 Unlimited cannot be used with Spot Instances
c.InstanceType = "t2.micro"
c.EnableT2Unlimited = true
c.SpotPrice = "auto"
err := c.Prepare(nil)
if len(err) != 1 {
t.Fatalf("Should error if T2 Unlimited has been used in conjuntion with a Spot Price request")
}
}
func TestRunConfigPrepare_EnableT2UnlimitedDeprecation(t *testing.T) {
c := testConfig()
// Must have a T2 instance type if T2 Unlimited is enabled
c.InstanceType = "t2.micro"
c.EnableT2Unlimited = true
err := c.Prepare(nil)
if c.EnableUnlimitedCredits != true {
t.Errorf("expected EnableUnlimitedCredits to be true when the deprecated EnableT2Unlimited is true, but got %T", c.EnableUnlimitedCredits)
}
if len(err) > 0 {
t.Fatalf("err: %s", err)
}
}
func TestRunConfigPrepare_EnableUnlimitedCredits(t *testing.T) {
tc := []struct {
name string
instanceType string
enableCredits bool
errorCount int
}{
{"T2 instance", "t2.micro", true, 0},
{"T3 instance", "t3.micro", true, 0},
{"T3a instance", "t3a.xlarge", true, 0},
{"T4g instance", "t4g.micro", true, 0},
{"M5 instance", "m5.micro", true, 1},
{"bogus t4 instance", "t4.micro", true, 1},
{"bogus t23 instance", "t23.micro", true, 1},
}
for _, tt := range tc {
tt := tt
t.Run(tt.name, func(t *testing.T) {
c := testConfig()
c.InstanceType = tt.instanceType
c.EnableUnlimitedCredits = tt.enableCredits
err := c.Prepare(nil)
if len(err) != tt.errorCount {
t.Errorf("err: %s", err)
}
})
}
}
func TestRunConfigPrepare_SpotAuto(t *testing.T) {
c := testConfig()
c.SpotPrice = "auto"
if err := c.Prepare(nil); len(err) != 0 {
t.Fatalf("err: %s", err)
}
// Shouldn't error (YET) even though SpotPriceAutoProduct is deprecated
c.SpotPriceAutoProduct = "Linux/Unix"
if err := c.Prepare(nil); len(err) != 0 {
t.Fatalf("err: %s", err)
}
}
func TestRunConfigPrepare_SSHPort(t *testing.T) {
c := testConfig()
c.Comm.SSHPort = 0
if err := c.Prepare(nil); len(err) != 0 {
t.Fatalf("err: %s", err)
}
if c.Comm.SSHPort != 22 {
t.Fatalf("invalid value: %d", c.Comm.SSHPort)
}
c.Comm.SSHPort = 44
if err := c.Prepare(nil); len(err) != 0 {
t.Fatalf("err: %s", err)
}
if c.Comm.SSHPort != 44 {
t.Fatalf("invalid value: %d", c.Comm.SSHPort)
}
}
func TestRunConfigPrepare_UserData(t *testing.T) {
c := testConfig()
tf, err := os.CreateTemp("", "packer")
if err != nil {
t.Fatalf("err: %s", err)
}
defer os.Remove(tf.Name())
defer tf.Close()
c.UserData = "foo"
c.UserDataFile = tf.Name()
if err := c.Prepare(nil); len(err) != 1 {
t.Fatalf("Should error if user_data string and user_data_file have both been specified")
}
}
func TestRunConfigPrepare_UserDataFile(t *testing.T) {
c := testConfig()
if err := c.Prepare(nil); len(err) != 0 {
t.Fatalf("err: %s", err)
}
c.UserDataFile = "idontexistidontthink"
if err := c.Prepare(nil); len(err) != 1 {
t.Fatalf("Should error if the file specified by user_data_file does not exist")
}
tf, err := os.CreateTemp("", "packer")
if err != nil {
t.Fatalf("err: %s", err)
}
defer os.Remove(tf.Name())
defer tf.Close()
c.UserDataFile = tf.Name()
if err := c.Prepare(nil); len(err) != 0 {
t.Fatalf("err: %s", err)
}
}
func TestRunConfigPrepare_TemporaryKeyPairName(t *testing.T) {
c := testConfig()
c.Comm.SSHTemporaryKeyPairName = ""
if err := c.Prepare(nil); len(err) != 0 {
t.Fatalf("err: %s", err)
}
if c.Comm.SSHTemporaryKeyPairName == "" {
t.Fatal("keypair name is empty")
}
// Match prefix and UUID, e.g. "packer_5790d491-a0b8-c84c-c9d2-2aea55086550".
r := regexp.MustCompile(`\Apacker_(?:(?i)[a-f\d]{8}(?:-[a-f\d]{4}){3}-[a-f\d]{12}?)\z`)
if !r.MatchString(c.Comm.SSHTemporaryKeyPairName) {
t.Fatal("keypair name is not valid")
}
c.Comm.SSHTemporaryKeyPairName = "ssh-key-123"
if err := c.Prepare(nil); len(err) != 0 {
t.Fatalf("err: %s", err)
}
if c.Comm.SSHTemporaryKeyPairName != "ssh-key-123" {
t.Fatal("keypair name does not match")
}
}
func TestRunConfigPrepare_TemporaryKeyPairTypeDefault(t *testing.T) {
c := testConfig()
tc := []struct {
desc string
keyPairName, keyPairType string
expectedKeyType string
}{
{desc: "no temporary_key_pair_* config settings should use defaults", expectedKeyType: "rsa"},
{desc: "setting a temporary_key_pair_name should set default key pair type", keyPairName: "local.name", expectedKeyType: "rsa"},
}
for _, tt := range tc {
tt := tt
t.Run(tt.desc, func(t *testing.T) {
c.Comm.SSHTemporaryKeyPairName = tt.keyPairName
c.Comm.SSHTemporaryKeyPairType = tt.keyPairType
if err := c.Prepare(nil); len(err) != 0 {
t.Fatalf("err: %s", err)
}
if c.Comm.SSHTemporaryKeyPairType != tt.expectedKeyType {
t.Fatal("keypair type should have defaulted to rsa")
}
})
}
}
func TestRunConfigPrepare_TemporaryKeyPairTypeRSA(t *testing.T) {
c := testConfig()
c.Comm.SSHTemporaryKeyPairType = "rsa"
if err := c.Prepare(nil); len(err) != 0 {
t.Fatalf("err: %s", err)
}
if c.Comm.SSHTemporaryKeyPairType != "rsa" {
t.Fatal("keypair type should have been rsa")
}
}
func TestRunConfigPrepare_TemporaryKeyPairTypeED25519(t *testing.T) {
c := testConfig()
c.Comm.SSHTemporaryKeyPairType = "ed25519"
if err := c.Prepare(nil); len(err) != 0 {
t.Fatalf("err: %s", err)
}
if c.Comm.SSHTemporaryKeyPairType != "ed25519" {
t.Fatal("keypair type should have been ed25519")
}
}
func TestRunConfigPrepare_TemporaryKeyPairTypeBad(t *testing.T) {
c := testConfig()
c.Comm.SSHTemporaryKeyPairType = "invalid"
if err := c.Prepare(nil); len(err) == 0 {
t.Fatalf("should error if temporary_key_pair_type is set to an invalid type")
}
}
func TestRunConfigPrepare_TenancyBad(t *testing.T) {
c := testConfig()
c.Tenancy = "not_real"
if err := c.Prepare(nil); len(err) != 1 {
t.Fatal("Should error if tenancy is set to an invalid type")
}
}
func TestRunConfigPrepare_TenancyGood(t *testing.T) {
validTenancy := []string{"", "default", "dedicated", "host"}
for _, vt := range validTenancy {
c := testConfig()
c.Tenancy = vt
if err := c.Prepare(nil); len(err) != 0 {
t.Fatalf("Should not error if tenancy is set to %s", vt)
}
}
}
func TestRunConfigPrepare_EnableNitroEnclaveBadWithSpotInstanceRequest(t *testing.T) {
c := testConfig()
// Nitro Enclaves cannot be used with Spot Instances
c.InstanceType = "c5.xlarge"
c.EnableNitroEnclave = true
c.SpotPrice = "auto"
err := c.Prepare(nil)
if len(err) != 1 {
t.Fatalf("Should error if Nitro Enclaves has been used in conjuntion with a Spot Price request")
}
}
func TestRunConfigPrepare_EnableNitroEnclaveBadWithBurstableInstanceType(t *testing.T) {
c := testConfig()
// Nitro Enclaves cannot be used with burstable instances
c.InstanceType = "t2.micro"
c.EnableNitroEnclave = true
err := c.Prepare(nil)
if len(err) != 1 {
t.Fatalf("Should error if Nitro Enclaves has been used in conjuntion with a burstable instance type")
}
}
func TestRunConfigPrepare_EnableNitroEnclaveGood(t *testing.T) {
c := testConfig()
// Nitro Enclaves cannot be used with burstable instances
c.InstanceType = "c5.xlarge"
c.EnableNitroEnclave = true
err := c.Prepare(nil)
if len(err) != 0 {
t.Fatalf("Should not error with valid Nitro Enclave config")
}
}
func TestRunConfigPrepare_FailIfBothHostIDAndGroupSpecified(t *testing.T) {
c := testConfig()
c.Placement.HostId = "host"
c.Placement.HostResourceGroupArn = "group"
err := c.Prepare(nil)
if len(err) != 1 {
t.Fatalf("Should error if both host_id and host_resource_group_arn are set")
}
}
func TestRunConfigPrepare_InvalidTenantForHost(t *testing.T) {
tests := []struct {
name string
setHost string
setGroup string
setTenant string
expectErrors int
}{
{
"no host_id, no host_resource_group_arn, with valid tenant",
"",
"",
"",
0,
},
{
"host_id set, tenant host",
"host",
"",
"host",
0,
},
{
"no host_id, host_resource_group_arn set, with tenant host",
"",
"group",
"host",
0,
},
{
"host_id set, invalid tenant",
"host",
"",
"dedicated",
1,
},
{
"no host_id, host_resource_group_arn set, invalid tenant",
"",
"group",
"dedicated",
1,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := testConfig()
c.Placement.HostId = tt.setHost
c.Placement.HostResourceGroupArn = tt.setGroup
c.Placement.Tenancy = tt.setTenant
errs := c.Prepare(nil)
if len(errs) != tt.expectErrors {
t.Errorf("expected %d errors, got %d", tt.expectErrors, len(errs))
}
})
}
}
func TestRunConfigPrepare_WithCapacityReservations(t *testing.T) {
tests := []struct {
name string
reservationID string
reservationPreference string
reservationARN string
expectedReservationID string
expectedReservationPreference string
expectedReservationARN string
expectError bool
}{
{
name: "None set, preference should be set to none, no error",
reservationID: "",
reservationPreference: "",
reservationARN: "",
expectedReservationID: "",
expectedReservationPreference: "none",
expectedReservationARN: "",
expectError: false,
},
{
name: "Preference set to none, preference should be set to none, no error",
reservationID: "",
reservationPreference: "none",
reservationARN: "",
expectedReservationID: "",
expectedReservationPreference: "none",
expectedReservationARN: "",
expectError: false,
},
{
name: "Preference set to open, preference should be set to open, no error",
reservationID: "",
reservationPreference: "open",
reservationARN: "",
expectedReservationID: "",
expectedReservationPreference: "open",
expectedReservationARN: "",
expectError: false,
},
{
name: "Preference set to and invalid value, expect an error",
reservationID: "",
reservationPreference: "invalid",
reservationARN: "",
expectedReservationID: "",
expectedReservationPreference: "invalid",
expectedReservationARN: "",
expectError: true,
},
{
name: "ID set to something, no preference, no error, no changes to config",
reservationID: "cr-123456",
reservationPreference: "",
reservationARN: "",
expectedReservationID: "cr-123456",
expectedReservationPreference: "",
expectedReservationARN: "",
expectError: false,
},
{
name: "ARN set to something, no preference, no error, no changes to config",
reservationID: "",
reservationPreference: "",
reservationARN: "arn-asduilovgf",
expectedReservationID: "",
expectedReservationPreference: "",
expectedReservationARN: "arn-asduilovgf",
expectError: false,
},
{
name: "Preference set to none, ID not empty, should error as both are incompatible",
reservationID: "cr-123456",
reservationPreference: "none",
reservationARN: "",
expectedReservationID: "cr-123456",
expectedReservationPreference: "none",
expectedReservationARN: "",
expectError: true,
},
{
name: "ID and ARN not empty, should error as both are incompatible",
reservationID: "cr-123456",
reservationPreference: "",
reservationARN: "arn-aseldigubh",
expectedReservationID: "cr-123456",
expectedReservationPreference: "",
expectedReservationARN: "arn-aseldigubh",
expectError: true,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := testConfig()
c.CapacityReservationGroupArn = tt.reservationARN
c.CapacityReservationId = tt.reservationID
c.CapacityReservationPreference = tt.reservationPreference
errs := c.Prepare(nil)
if (len(errs) != 0) != tt.expectError {
t.Errorf("expected %t errors, got %t", tt.expectError, len(errs) != 0)
t.Logf("errors: %v", errs)
}
if c.CapacityReservationGroupArn != tt.expectedReservationARN {
t.Errorf("expected Reservation ARN %q, got %q", tt.expectedReservationARN, c.CapacityReservationGroupArn)
}
if c.CapacityReservationId != tt.expectedReservationID {
t.Errorf("expected Reservation ID %q, got %q", tt.expectedReservationARN, c.CapacityReservationId)
}
if c.CapacityReservationPreference != tt.expectedReservationPreference {
t.Errorf("expected Reservation Preference %q, got %q", tt.expectedReservationPreference, c.CapacityReservationPreference)
}
})
}
}
func TestRunConfigPrepare_EnableSpotInstanceBadSpotAllocationStrategy(t *testing.T) {
c := testConfig()
// There should be some error when Spot Allocation Strategy is invalid.
c.SpotAllocationStrategy = "very-expensive-one"
err := c.Prepare(nil)
if len(err) != 1 {
t.Fatalf("Should error if spot_allocation_strategy is invalid. Expected 1 error, got %d", len(err))
}
}
func TestRunConfigPrepare_SSHInterfaceIPv6_DefaultCIDR(t *testing.T) {
c := testConfig()
c.SSHInterface = "ipv6"
c.TemporarySGSourceCidrs = []string{}
c.TemporarySGSourcePublicIp = false
errs := c.Prepare(nil)
if len(errs) != 0 {
t.Fatalf("expected no errors, got: %v", errs)
}
if len(c.TemporarySGSourceCidrs) != 1 {
t.Fatalf("expected 1 default CIDR, got: %v", c.TemporarySGSourceCidrs)
}
if c.TemporarySGSourceCidrs[0] != "::/0" {
t.Errorf("expected default CIDR to be '::/0', got: %s", c.TemporarySGSourceCidrs[0])
}
}
func TestRunConfigPrepare_ElasticIpAllocationId_Valid(t *testing.T) {
c := testConfig()
c.ElasticIpAllocationId = "eipalloc-0123456789abcdef0"
errs := c.Prepare(nil)
if len(errs) != 0 {
t.Fatalf("expected no errors, got: %v", errs)
}
}
func TestRunConfigPrepare_ElasticIpAllocationId_InvalidFormat(t *testing.T) {
cases := []string{
"eipalloc-UPPER",
"alloc-123",
"eipalloc-",
}
for _, id := range cases {
c := testConfig()
c.ElasticIpAllocationId = id
errs := c.Prepare(nil)
if len(errs) != 1 {
t.Errorf("expected 1 error for %q, got %d: %v", id, len(errs), errs)
}
}
}
func TestRunConfigPrepare_ElasticIpAllocationId_ConflictsWithAssociatePublicIp(t *testing.T) {
c := testConfig()
c.ElasticIpAllocationId = "eipalloc-0123456789abcdef0"
c.AssociatePublicIpAddress = config.TriTrue
errs := c.Prepare(nil)
found := false
for _, err := range errs {
if err.Error() == "associate_public_ip_address must not be set when elastic_ip_allocation_id is specified" {
found = true
break
}
}
if !found {
t.Fatalf("expected conflict error, got: %v", errs)
}
}