-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathconfig_test.go
More file actions
589 lines (537 loc) · 14.6 KB
/
config_test.go
File metadata and controls
589 lines (537 loc) · 14.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
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
//go:build !integration
package upcloud_test
import (
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/UpCloudLtd/packer-plugin-upcloud/builder/upcloud"
"github.com/UpCloudLtd/packer-plugin-upcloud/internal/driver"
)
func TestConfig_Prepare_ValidUsernamePassword(t *testing.T) {
t.Parallel()
c := &upcloud.Config{}
raws := []interface{}{
map[string]interface{}{
"username": "testuser",
"password": "testpass",
"zone": "fi-hel1",
"storage_uuid": "01000000-0000-4000-8000-000030060200",
},
}
warns, err := c.Prepare(raws...)
assert.NoError(t, err)
assert.Empty(t, warns)
assert.Equal(t, "testuser", c.Username)
assert.Equal(t, "testpass", c.Password)
assert.Empty(t, c.Token)
}
func TestConfig_Prepare_ValidAPIToken(t *testing.T) {
t.Parallel()
c := &upcloud.Config{}
raws := []interface{}{
map[string]interface{}{
"token": "test-api-token",
"zone": "fi-hel1",
"storage_uuid": "01000000-0000-4000-8000-000030060200",
},
}
warns, err := c.Prepare(raws...)
assert.NoError(t, err)
assert.Empty(t, warns)
assert.Equal(t, "test-api-token", c.Token)
assert.Empty(t, c.Username)
assert.Empty(t, c.Password)
}
func TestConfig_Prepare_BothAuthMethods(t *testing.T) {
t.Parallel()
c := &upcloud.Config{}
raws := []interface{}{
map[string]interface{}{
"username": "testuser",
"password": "testpass",
"token": "test-api-token",
"zone": "fi-hel1",
"storage_uuid": "01000000-0000-4000-8000-000030060200",
},
}
warns, err := c.Prepare(raws...)
assert.NoError(t, err)
assert.Equal(t, "test-api-token", c.Token)
assert.Empty(t, c.Username)
assert.Empty(t, c.Password)
assert.Empty(t, warns)
}
func TestConfig_Prepare_NoAuthMethods(t *testing.T) {
t.Parallel()
c := &upcloud.Config{}
raws := []interface{}{
map[string]interface{}{
"zone": "fi-hel1",
"storage_uuid": "01000000-0000-4000-8000-000030060200",
},
}
warns, err := c.Prepare(raws...)
require.Error(t, err)
assert.Contains(t, err.Error(), "credentials not found")
assert.Empty(t, warns)
}
func TestConfig_Prepare_OnlyUsername(t *testing.T) {
t.Parallel()
c := &upcloud.Config{}
raws := []interface{}{
map[string]interface{}{
"username": "testuser",
"zone": "fi-hel1",
"storage_uuid": "01000000-0000-4000-8000-000030060200",
},
}
warns, err := c.Prepare(raws...)
require.Error(t, err)
assert.Contains(t, err.Error(), "credentials not found")
assert.Empty(t, warns)
}
func TestConfig_Prepare_OnlyPassword(t *testing.T) {
t.Parallel()
c := &upcloud.Config{}
raws := []interface{}{
map[string]interface{}{
"password": "testpass",
"zone": "fi-hel1",
"storage_uuid": "01000000-0000-4000-8000-000030060200",
},
}
warns, err := c.Prepare(raws...)
require.Error(t, err)
assert.Contains(t, err.Error(), "credentials not found")
assert.Empty(t, warns)
}
func TestConfig_Prepare_MissingZone(t *testing.T) {
t.Parallel()
c := &upcloud.Config{}
raws := []interface{}{
map[string]interface{}{
"username": "testuser",
"password": "testpass",
"storage_uuid": "01000000-0000-4000-8000-000030060200",
},
}
warns, err := c.Prepare(raws...)
assert.Error(t, err)
assert.Contains(t, err.Error(), "'zone' must be specified")
assert.Empty(t, warns)
}
func TestConfig_Prepare_MissingStorage(t *testing.T) {
t.Parallel()
c := &upcloud.Config{}
raws := []interface{}{
map[string]interface{}{
"username": "testuser",
"password": "testpass",
"zone": "fi-hel1",
},
}
warns, err := c.Prepare(raws...)
assert.Error(t, err)
assert.Contains(t, err.Error(), "'storage_uuid' or 'storage_name' must be specified")
assert.Empty(t, warns)
}
func TestConfig_Prepare_TemplatePrefix(t *testing.T) {
t.Parallel()
c := &upcloud.Config{}
raws := []interface{}{
map[string]interface{}{
"username": "testuser",
"password": "testpass",
"zone": "fi-hel1",
"storage_uuid": "01000000-0000-4000-8000-000030060200",
"template_prefix": "custom-prefix",
},
}
warns, err := c.Prepare(raws...)
assert.NoError(t, err)
assert.Empty(t, warns)
assert.Equal(t, "custom-prefix", c.TemplatePrefix)
assert.Empty(t, c.TemplateName)
}
func TestConfig_Prepare_TemplateName(t *testing.T) {
t.Parallel()
c := &upcloud.Config{}
raws := []interface{}{
map[string]interface{}{
"username": "testuser",
"password": "testpass",
"zone": "fi-hel1",
"storage_uuid": "01000000-0000-4000-8000-000030060200",
"template_name": "my-custom-template",
},
}
warns, err := c.Prepare(raws...)
assert.NoError(t, err)
assert.Empty(t, warns)
assert.Equal(t, "my-custom-template", c.TemplateName)
assert.Empty(t, c.TemplatePrefix)
}
func TestConfig_Prepare_BothTemplateFields(t *testing.T) {
t.Parallel()
c := &upcloud.Config{}
raws := []interface{}{
map[string]interface{}{
"username": "testuser",
"password": "testpass",
"zone": "fi-hel1",
"storage_uuid": "01000000-0000-4000-8000-000030060200",
"template_prefix": "custom-prefix",
"template_name": "my-custom-template",
},
}
warns, err := c.Prepare(raws...)
assert.Error(t, err)
assert.Contains(t, err.Error(), "you can either use 'template_prefix' or 'template_name' in your configuration")
assert.Empty(t, warns)
}
func TestConfig_Prepare_LongTemplatePrefix(t *testing.T) {
t.Parallel()
c := &upcloud.Config{}
longPrefix := "this-is-a-very-long-template-prefix-that-exceeds-the-limit"
raws := []interface{}{
map[string]interface{}{
"username": "testuser",
"password": "testpass",
"zone": "fi-hel1",
"storage_uuid": "01000000-0000-4000-8000-000030060200",
"template_prefix": longPrefix,
},
}
warns, err := c.Prepare(raws...)
assert.Error(t, err)
assert.Contains(t, err.Error(), "'template_prefix' must be 0-40 characters")
assert.Empty(t, warns)
}
func TestConfig_Prepare_LongTemplateName(t *testing.T) {
t.Parallel()
c := &upcloud.Config{}
longName := "this-is-a-very-long-template-name-that-exceeds-the-limit"
raws := []interface{}{
map[string]interface{}{
"username": "testuser",
"password": "testpass",
"zone": "fi-hel1",
"storage_uuid": "01000000-0000-4000-8000-000030060200",
"template_name": longName,
},
}
warns, err := c.Prepare(raws...)
assert.Error(t, err)
assert.Contains(t, err.Error(), "'template_name' is limited to 40 characters")
assert.Empty(t, warns)
}
func TestConfig_Prepare_Defaults(t *testing.T) {
t.Parallel()
c := &upcloud.Config{}
raws := []interface{}{
map[string]interface{}{
"username": "testuser",
"password": "testpass",
"zone": "fi-hel1",
"storage_uuid": "01000000-0000-4000-8000-000030060200",
},
}
warns, err := c.Prepare(raws...)
assert.NoError(t, err)
assert.Empty(t, warns)
// Check defaults
assert.Equal(t, "custom-image", c.TemplatePrefix)
assert.Equal(t, 25, c.StorageSize)
assert.Equal(t, "maxiops", c.StorageTier)
assert.Equal(t, upcloud.DefaultTimeout, c.Timeout)
assert.Equal(t, "ssh", c.Comm.Type)
assert.Equal(t, "root", c.Comm.SSHUsername)
}
func TestConfig_setEnv_APIToken(t *testing.T) {
t.Setenv(driver.EnvConfigAPIToken, "test-token")
c := &upcloud.Config{}
err := c.SetEnv()
assert.NoError(t, err)
assert.Equal(t, "test-token", c.Token)
}
func TestConfig_setEnv_DoesNotOverrideExisting_basic(t *testing.T) {
t.Setenv(driver.EnvConfigUsername, "env-user")
t.Setenv(driver.EnvConfigPassword, "env-pass")
t.Setenv(driver.EnvConfigAPIToken, "env-token")
c := &upcloud.Config{
Username: "existing-user",
Password: "existing-pass",
}
err := c.SetEnv()
assert.NoError(t, err)
// Should not override existing values
assert.Equal(t, "existing-user", c.Username)
assert.Equal(t, "existing-pass", c.Password)
assert.Empty(t, c.Token)
}
func TestConfig_setEnv_DoesNotOverrideExisting_token(t *testing.T) {
t.Setenv(driver.EnvConfigUsername, "env-user")
t.Setenv(driver.EnvConfigPassword, "env-pass")
t.Setenv(driver.EnvConfigAPIToken, "env-token")
c := &upcloud.Config{
Username: "existing-user",
Password: "existing-pass",
Token: "existing-token",
}
err := c.SetEnv()
assert.NoError(t, err)
// Should not override existing values
assert.Empty(t, c.Username)
assert.Empty(t, c.Password)
assert.Equal(t, "existing-token", c.Token)
}
func TestConfig_DefaultIPaddress(t *testing.T) {
t.Parallel()
c := &upcloud.Config{
NetworkInterfaces: []upcloud.NetworkInterface{
{
Type: upcloud.InterfaceTypePublic,
IPAddresses: []upcloud.IPAddress{
{
Family: "IPv4",
Default: false,
},
{
Family: "IPv4",
Default: true,
},
},
},
{
Type: upcloud.InterfaceTypePrivate,
IPAddresses: []upcloud.IPAddress{
{
Family: "IPv4",
Default: false,
},
},
},
},
}
ip, ifaceType := c.DefaultIPaddress()
assert.NotNil(t, ip)
assert.Equal(t, "IPv4", ip.Family)
assert.True(t, ip.Default)
assert.Equal(t, upcloud.InterfaceTypePublic, ifaceType)
}
func TestConfig_StorageName(t *testing.T) {
t.Parallel()
c := &upcloud.Config{}
raws := []interface{}{
map[string]interface{}{
"username": "testuser",
"password": "testpass",
"zone": "fi-hel1",
"storage_name": "Ubuntu Server 20.04",
},
}
warns, err := c.Prepare(raws...)
assert.NoError(t, err)
assert.Empty(t, warns)
assert.Equal(t, "Ubuntu Server 20.04", c.StorageName)
assert.Empty(t, c.StorageUUID)
}
func TestConfig_Prepare_CustomValues(t *testing.T) {
t.Parallel()
c := &upcloud.Config{}
raws := []interface{}{
map[string]interface{}{
"token": "test-token",
"zone": "de-fra1",
"storage_uuid": "01000000-0000-4000-8000-000030060200",
"template_name": "my-custom-template",
"storage_size": 50,
"storage_tier": "standard",
"state_timeout_duration": "10m",
"boot_wait": "30s",
"clone_zones": []string{"nl-ams1", "us-nyc1"},
},
}
warns, err := c.Prepare(raws...)
assert.NoError(t, err)
assert.Empty(t, warns)
assert.Equal(t, "test-token", c.Token)
assert.Equal(t, "de-fra1", c.Zone)
assert.Equal(t, "my-custom-template", c.TemplateName)
assert.Equal(t, 50, c.StorageSize)
assert.Equal(t, "standard", c.StorageTier)
assert.Equal(t, 10*time.Minute, c.Timeout)
assert.Equal(t, 30*time.Second, c.BootWait)
assert.Equal(t, []string{"nl-ams1", "us-nyc1"}, c.CloneZones)
}
func TestConfig_validateNetworkInterfaces(t *testing.T) {
t.Parallel()
tests := []struct {
name string
interfaces []upcloud.NetworkInterface
expectError bool
errorSubstring string
}{
{
name: "valid public interface",
interfaces: []upcloud.NetworkInterface{
{
Type: upcloud.InterfaceTypePublic,
IPAddresses: []upcloud.IPAddress{
{Family: "IPv4", Default: true},
},
},
},
expectError: false,
},
{
name: "valid private interface with UUID",
interfaces: []upcloud.NetworkInterface{
{
Type: upcloud.InterfaceTypePrivate,
Network: "01234567-89ab-cdef-0123-456789abcdef",
IPAddresses: []upcloud.IPAddress{
{Family: "IPv4", Address: "192.168.1.100"},
},
},
},
expectError: false,
},
{
name: "invalid interface type",
interfaces: []upcloud.NetworkInterface{
{Type: upcloud.InterfaceType("invalid")},
},
expectError: true,
errorSubstring: "has invalid type: invalid",
},
{
name: "private interface without UUID",
interfaces: []upcloud.NetworkInterface{
{Type: upcloud.InterfaceTypePrivate},
},
expectError: true,
errorSubstring: "private network requires network UUID",
},
{
name: "private interface with invalid UUID",
interfaces: []upcloud.NetworkInterface{
{
Type: upcloud.InterfaceTypePrivate,
Network: "not-a-uuid",
},
},
expectError: true,
errorSubstring: "invalid network UUID",
},
{
name: "invalid IP family",
interfaces: []upcloud.NetworkInterface{
{
Type: upcloud.InterfaceTypePublic,
IPAddresses: []upcloud.IPAddress{
{Family: "IPv5"},
},
},
},
expectError: true,
errorSubstring: "invalid IP family 'IPv5'",
},
{
name: "invalid IP address",
interfaces: []upcloud.NetworkInterface{
{
Type: upcloud.InterfaceTypePublic,
IPAddresses: []upcloud.IPAddress{
{Family: "IPv4", Address: "invalid-ip"},
},
},
},
expectError: true,
errorSubstring: "invalid IP address 'invalid-ip'",
},
{
name: "IP family mismatch - IPv4 family with IPv6 address",
interfaces: []upcloud.NetworkInterface{
{
Type: upcloud.InterfaceTypePublic,
IPAddresses: []upcloud.IPAddress{
{Family: "IPv4", Address: "2001:db8::1"},
},
},
},
expectError: true,
errorSubstring: "IP family is IPv4 but address is IPv6",
},
{
name: "IP family mismatch - IPv6 family with IPv4 address",
interfaces: []upcloud.NetworkInterface{
{
Type: upcloud.InterfaceTypePublic,
IPAddresses: []upcloud.IPAddress{
{Family: "IPv6", Address: "192.168.1.1"},
},
},
},
expectError: true,
errorSubstring: "IP family is IPv6 but address is IPv4",
},
{
name: "multiple interfaces with mixed errors",
interfaces: []upcloud.NetworkInterface{
{
Type: upcloud.InterfaceTypePublic,
IPAddresses: []upcloud.IPAddress{
{Family: "IPv4", Default: true},
},
},
{
Type: upcloud.InterfaceType("invalid"),
},
{
Type: upcloud.InterfaceTypePrivate,
Network: "bad-uuid",
},
},
expectError: true,
errorSubstring: "has invalid type",
},
{
name: "empty interfaces slice",
interfaces: []upcloud.NetworkInterface{},
expectError: false,
},
{
name: "interface with empty IP addresses",
interfaces: []upcloud.NetworkInterface{
{
Type: upcloud.InterfaceTypeUtility,
IPAddresses: []upcloud.IPAddress{},
},
},
expectError: false,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
t.Parallel()
c := &upcloud.Config{
Username: "testuser",
Password: "testpass",
Zone: "fi-hel1",
StorageUUID: "01000000-0000-4000-8000-000030060200",
NetworkInterfaces: tt.interfaces,
}
_, err := c.Prepare(map[string]interface{}{})
if tt.expectError {
assert.Error(t, err, "expected validation errors")
if tt.errorSubstring != "" {
assert.Contains(t, err.Error(), tt.errorSubstring)
}
} else {
assert.NoError(t, err, "unexpected validation errors")
}
})
}
}