github.com/alouche/packer@v0.3.7/builder/virtualbox/builder_test.go (about) 1 package virtualbox 2 3 import ( 4 "github.com/mitchellh/packer/packer" 5 "io/ioutil" 6 "os" 7 "reflect" 8 "testing" 9 ) 10 11 var testPem = ` 12 -----BEGIN RSA PRIVATE KEY----- 13 MIIEpQIBAAKCAQEAxd4iamvrwRJvtNDGQSIbNvvIQN8imXTRWlRY62EvKov60vqu 14 hh+rDzFYAIIzlmrJopvOe0clqmi3mIP9dtkjPFrYflq52a2CF5q+BdwsJXuRHbJW 15 LmStZUwW1khSz93DhvhmK50nIaczW63u4EO/jJb3xj+wxR1Nkk9bxi3DDsYFt8SN 16 AzYx9kjlEYQ/+sI4/ATfmdV9h78SVotjScupd9KFzzi76gWq9gwyCBLRynTUWlyD 17 2UOfJRkOvhN6/jKzvYfVVwjPSfA9IMuooHdScmC4F6KBKJl/zf/zETM0XyzIDNmH 18 uOPbCiljq2WoRM+rY6ET84EO0kVXbfx8uxUsqQIDAQABAoIBAQCkPj9TF0IagbM3 19 5BSs/CKbAWS4dH/D4bPlxx4IRCNirc8GUg+MRb04Xz0tLuajdQDqeWpr6iLZ0RKV 20 BvreLF+TOdV7DNQ4XE4gSdJyCtCaTHeort/aordL3l0WgfI7mVk0L/yfN1PEG4YG 21 E9q1TYcyrB3/8d5JwIkjabxERLglCcP+geOEJp+QijbvFIaZR/n2irlKW4gSy6ko 22 9B0fgUnhkHysSg49ChHQBPQ+o5BbpuLrPDFMiTPTPhdfsvGGcyCGeqfBA56oHcSF 23 K02Fg8OM+Bd1lb48LAN9nWWY4WbwV+9bkN3Ym8hO4c3a/Dxf2N7LtAQqWZzFjvM3 24 /AaDvAgBAoGBAPLD+Xn1IYQPMB2XXCXfOuJewRY7RzoVWvMffJPDfm16O7wOiW5+ 25 2FmvxUDayk4PZy6wQMzGeGKnhcMMZTyaq2g/QtGfrvy7q1Lw2fB1VFlVblvqhoJa 26 nMJojjC4zgjBkXMHsRLeTmgUKyGs+fdFbfI6uejBnnf+eMVUMIdJ+6I9AoGBANCn 27 kWO9640dttyXURxNJ3lBr2H3dJOkmD6XS+u+LWqCSKQe691Y/fZ/ZL0Oc4Mhy7I6 28 hsy3kDQ5k2V0fkaNODQIFJvUqXw2pMewUk8hHc9403f4fe9cPrL12rQ8WlQw4yoC 29 v2B61vNczCCUDtGxlAaw8jzSRaSI5s6ax3K7enbdAoGBAJB1WYDfA2CoAQO6y9Sl 30 b07A/7kQ8SN5DbPaqrDrBdJziBQxukoMJQXJeGFNUFD/DXFU5Fp2R7C86vXT7HIR 31 v6m66zH+CYzOx/YE6EsUJms6UP9VIVF0Rg/RU7teXQwM01ZV32LQ8mswhTH20o/3 32 uqMHmxUMEhZpUMhrfq0isyApAoGAe1UxGTXfj9AqkIVYylPIq2HqGww7+jFmVEj1 33 9Wi6S6Sq72ffnzzFEPkIQL/UA4TsdHMnzsYKFPSbbXLIWUeMGyVTmTDA5c0e5XIR 34 lPhMOKCAzv8w4VUzMnEkTzkFY5JqFCD/ojW57KvDdNZPVB+VEcdxyAW6aKELXMAc 35 eHLc1nkCgYEApm/motCTPN32nINZ+Vvywbv64ZD+gtpeMNP3CLrbe1X9O+H52AXa 36 1jCoOldWR8i2bs2NVPcKZgdo6fFULqE4dBX7Te/uYEIuuZhYLNzRO1IKU/YaqsXG 37 3bfQ8hKYcSnTfE0gPtLDnqCIxTocaGLSHeG3TH9fTw+dA8FvWpUztI4= 38 -----END RSA PRIVATE KEY----- 39 ` 40 41 func testConfig() map[string]interface{} { 42 return map[string]interface{}{ 43 "iso_checksum": "foo", 44 "iso_checksum_type": "md5", 45 "iso_url": "http://www.google.com/", 46 "ssh_username": "foo", 47 48 packer.BuildNameConfigKey: "foo", 49 } 50 } 51 52 func TestBuilder_ImplementsBuilder(t *testing.T) { 53 var raw interface{} 54 raw = &Builder{} 55 if _, ok := raw.(packer.Builder); !ok { 56 t.Error("Builder must implement builder.") 57 } 58 } 59 60 func TestBuilderPrepare_Defaults(t *testing.T) { 61 var b Builder 62 config := testConfig() 63 err := b.Prepare(config) 64 if err != nil { 65 t.Fatalf("should not have error: %s", err) 66 } 67 68 if b.config.GuestOSType != "Other" { 69 t.Errorf("bad guest OS type: %s", b.config.GuestOSType) 70 } 71 72 if b.config.OutputDir != "output-foo" { 73 t.Errorf("bad output dir: %s", b.config.OutputDir) 74 } 75 76 if b.config.SSHHostPortMin != 2222 { 77 t.Errorf("bad min ssh host port: %d", b.config.SSHHostPortMin) 78 } 79 80 if b.config.SSHHostPortMax != 4444 { 81 t.Errorf("bad max ssh host port: %d", b.config.SSHHostPortMax) 82 } 83 84 if b.config.SSHPort != 22 { 85 t.Errorf("bad ssh port: %d", b.config.SSHPort) 86 } 87 88 if b.config.VMName != "packer-foo" { 89 t.Errorf("bad vm name: %s", b.config.VMName) 90 } 91 92 if b.config.Format != "ovf" { 93 t.Errorf("bad format: %s", b.config.Format) 94 } 95 } 96 97 func TestBuilderPrepare_BootWait(t *testing.T) { 98 var b Builder 99 config := testConfig() 100 101 // Test a default boot_wait 102 delete(config, "boot_wait") 103 err := b.Prepare(config) 104 if err != nil { 105 t.Fatalf("err: %s", err) 106 } 107 108 if b.config.RawBootWait != "10s" { 109 t.Fatalf("bad value: %s", b.config.RawBootWait) 110 } 111 112 // Test with a bad boot_wait 113 config["boot_wait"] = "this is not good" 114 err = b.Prepare(config) 115 if err == nil { 116 t.Fatal("should have error") 117 } 118 119 // Test with a good one 120 config["boot_wait"] = "5s" 121 b = Builder{} 122 err = b.Prepare(config) 123 if err != nil { 124 t.Fatalf("should not have error: %s", err) 125 } 126 } 127 128 func TestBuilderPrepare_DiskSize(t *testing.T) { 129 var b Builder 130 config := testConfig() 131 132 delete(config, "disk_size") 133 err := b.Prepare(config) 134 if err != nil { 135 t.Fatalf("bad err: %s", err) 136 } 137 138 if b.config.DiskSize != 40000 { 139 t.Fatalf("bad size: %d", b.config.DiskSize) 140 } 141 142 config["disk_size"] = 60000 143 b = Builder{} 144 err = b.Prepare(config) 145 if err != nil { 146 t.Fatalf("should not have error: %s", err) 147 } 148 149 if b.config.DiskSize != 60000 { 150 t.Fatalf("bad size: %s", b.config.DiskSize) 151 } 152 } 153 154 func TestBuilderPrepare_FloppyFiles(t *testing.T) { 155 var b Builder 156 config := testConfig() 157 158 delete(config, "floppy_files") 159 err := b.Prepare(config) 160 if err != nil { 161 t.Fatalf("bad err: %s", err) 162 } 163 164 if len(b.config.FloppyFiles) != 0 { 165 t.Fatalf("bad: %#v", b.config.FloppyFiles) 166 } 167 168 config["floppy_files"] = []string{"foo", "bar"} 169 b = Builder{} 170 err = b.Prepare(config) 171 if err != nil { 172 t.Fatalf("should not have error: %s", err) 173 } 174 175 expected := []string{"foo", "bar"} 176 if !reflect.DeepEqual(b.config.FloppyFiles, expected) { 177 t.Fatalf("bad: %#v", b.config.FloppyFiles) 178 } 179 } 180 181 func TestBuilderPrepare_GuestAdditionsPath(t *testing.T) { 182 var b Builder 183 config := testConfig() 184 185 delete(config, "guest_additions_path") 186 err := b.Prepare(config) 187 if err != nil { 188 t.Fatalf("bad err: %s", err) 189 } 190 191 if b.config.GuestAdditionsPath != "VBoxGuestAdditions.iso" { 192 t.Fatalf("bad: %s", b.config.GuestAdditionsPath) 193 } 194 195 config["guest_additions_path"] = "foo" 196 b = Builder{} 197 err = b.Prepare(config) 198 if err != nil { 199 t.Fatalf("should not have error: %s", err) 200 } 201 202 if b.config.GuestAdditionsPath != "foo" { 203 t.Fatalf("bad size: %s", b.config.GuestAdditionsPath) 204 } 205 } 206 207 func TestBuilderPrepare_GuestAdditionsSHA256(t *testing.T) { 208 var b Builder 209 config := testConfig() 210 211 delete(config, "guest_additions_sha256") 212 err := b.Prepare(config) 213 if err != nil { 214 t.Fatalf("bad err: %s", err) 215 } 216 217 if b.config.GuestAdditionsSHA256 != "" { 218 t.Fatalf("bad: %s", b.config.GuestAdditionsSHA256) 219 } 220 221 config["guest_additions_sha256"] = "FOO" 222 b = Builder{} 223 err = b.Prepare(config) 224 if err != nil { 225 t.Fatalf("should not have error: %s", err) 226 } 227 228 if b.config.GuestAdditionsSHA256 != "foo" { 229 t.Fatalf("bad size: %s", b.config.GuestAdditionsSHA256) 230 } 231 } 232 233 func TestBuilderPrepare_GuestAdditionsURL(t *testing.T) { 234 var b Builder 235 config := testConfig() 236 237 config["guest_additions_url"] = "" 238 err := b.Prepare(config) 239 if err != nil { 240 t.Fatalf("err: %s", err) 241 } 242 243 if b.config.GuestAdditionsURL != "" { 244 t.Fatalf("should be empty: %s", b.config.GuestAdditionsURL) 245 } 246 247 config["guest_additions_url"] = "http://www.packer.io" 248 b = Builder{} 249 err = b.Prepare(config) 250 if err != nil { 251 t.Errorf("should not have error: %s", err) 252 } 253 } 254 255 func TestBuilderPrepare_HardDriveInterface(t *testing.T) { 256 var b Builder 257 config := testConfig() 258 259 // Test a default boot_wait 260 delete(config, "hard_drive_interface") 261 err := b.Prepare(config) 262 if err != nil { 263 t.Fatalf("err: %s", err) 264 } 265 266 if b.config.HardDriveInterface != "ide" { 267 t.Fatalf("bad: %s", b.config.HardDriveInterface) 268 } 269 270 // Test with a bad 271 config["hard_drive_interface"] = "fake" 272 b = Builder{} 273 err = b.Prepare(config) 274 if err == nil { 275 t.Fatal("should have error") 276 } 277 278 // Test with a good 279 config["hard_drive_interface"] = "sata" 280 b = Builder{} 281 err = b.Prepare(config) 282 if err != nil { 283 t.Fatalf("should not have error: %s", err) 284 } 285 } 286 287 func TestBuilderPrepare_HTTPPort(t *testing.T) { 288 var b Builder 289 config := testConfig() 290 291 // Bad 292 config["http_port_min"] = 1000 293 config["http_port_max"] = 500 294 err := b.Prepare(config) 295 if err == nil { 296 t.Fatal("should have error") 297 } 298 299 // Bad 300 config["http_port_min"] = -500 301 b = Builder{} 302 err = b.Prepare(config) 303 if err == nil { 304 t.Fatal("should have error") 305 } 306 307 // Good 308 config["http_port_min"] = 500 309 config["http_port_max"] = 1000 310 b = Builder{} 311 err = b.Prepare(config) 312 if err != nil { 313 t.Fatalf("should not have error: %s", err) 314 } 315 } 316 317 func TestBuilderPrepare_Format(t *testing.T) { 318 var b Builder 319 config := testConfig() 320 321 // Bad 322 config["format"] = "illegal value" 323 err := b.Prepare(config) 324 if err == nil { 325 t.Fatal("should have error") 326 } 327 328 // Good 329 config["format"] = "ova" 330 b = Builder{} 331 err = b.Prepare(config) 332 if err != nil { 333 t.Fatalf("should not have error: %s", err) 334 } 335 336 // Good 337 config["format"] = "ovf" 338 b = Builder{} 339 err = b.Prepare(config) 340 if err != nil { 341 t.Fatalf("should not have error: %s", err) 342 } 343 } 344 345 func TestBuilderPrepare_InvalidKey(t *testing.T) { 346 var b Builder 347 config := testConfig() 348 349 // Add a random key 350 config["i_should_not_be_valid"] = true 351 err := b.Prepare(config) 352 if err == nil { 353 t.Fatal("should have error") 354 } 355 } 356 357 func TestBuilderPrepare_ISOChecksum(t *testing.T) { 358 var b Builder 359 config := testConfig() 360 361 // Test bad 362 config["iso_checksum"] = "" 363 err := b.Prepare(config) 364 if err == nil { 365 t.Fatal("should have error") 366 } 367 368 // Test good 369 config["iso_checksum"] = "FOo" 370 b = Builder{} 371 err = b.Prepare(config) 372 if err != nil { 373 t.Fatalf("should not have error: %s", err) 374 } 375 376 if b.config.ISOChecksum != "foo" { 377 t.Fatalf("should've lowercased: %s", b.config.ISOChecksum) 378 } 379 } 380 381 func TestBuilderPrepare_ISOChecksumType(t *testing.T) { 382 var b Builder 383 config := testConfig() 384 385 // Test bad 386 config["iso_checksum_type"] = "" 387 err := b.Prepare(config) 388 if err == nil { 389 t.Fatal("should have error") 390 } 391 392 // Test good 393 config["iso_checksum_type"] = "mD5" 394 b = Builder{} 395 err = b.Prepare(config) 396 if err != nil { 397 t.Fatalf("should not have error: %s", err) 398 } 399 400 if b.config.ISOChecksumType != "md5" { 401 t.Fatalf("should've lowercased: %s", b.config.ISOChecksumType) 402 } 403 404 // Test unknown 405 config["iso_checksum_type"] = "fake" 406 b = Builder{} 407 err = b.Prepare(config) 408 if err == nil { 409 t.Fatal("should have error") 410 } 411 } 412 413 func TestBuilderPrepare_ISOUrl(t *testing.T) { 414 var b Builder 415 config := testConfig() 416 delete(config, "iso_url") 417 delete(config, "iso_urls") 418 419 // Test both epty 420 config["iso_url"] = "" 421 b = Builder{} 422 err := b.Prepare(config) 423 if err == nil { 424 t.Fatal("should have error") 425 } 426 427 // Test iso_url set 428 config["iso_url"] = "http://www.packer.io" 429 b = Builder{} 430 err = b.Prepare(config) 431 if err != nil { 432 t.Errorf("should not have error: %s", err) 433 } 434 435 expected := []string{"http://www.packer.io"} 436 if !reflect.DeepEqual(b.config.ISOUrls, expected) { 437 t.Fatalf("bad: %#v", b.config.ISOUrls) 438 } 439 440 // Test both set 441 config["iso_url"] = "http://www.packer.io" 442 config["iso_urls"] = []string{"http://www.packer.io"} 443 b = Builder{} 444 err = b.Prepare(config) 445 if err == nil { 446 t.Fatal("should have error") 447 } 448 449 // Test just iso_urls set 450 delete(config, "iso_url") 451 config["iso_urls"] = []string{ 452 "http://www.packer.io", 453 "http://www.hashicorp.com", 454 } 455 456 b = Builder{} 457 err = b.Prepare(config) 458 if err != nil { 459 t.Errorf("should not have error: %s", err) 460 } 461 462 expected = []string{ 463 "http://www.packer.io", 464 "http://www.hashicorp.com", 465 } 466 if !reflect.DeepEqual(b.config.ISOUrls, expected) { 467 t.Fatalf("bad: %#v", b.config.ISOUrls) 468 } 469 } 470 471 func TestBuilderPrepare_OutputDir(t *testing.T) { 472 var b Builder 473 config := testConfig() 474 475 // Test with existing dir 476 dir, err := ioutil.TempDir("", "packer") 477 if err != nil { 478 t.Fatalf("err: %s", err) 479 } 480 defer os.RemoveAll(dir) 481 482 config["output_directory"] = dir 483 b = Builder{} 484 err = b.Prepare(config) 485 if err == nil { 486 t.Fatal("should have error") 487 } 488 489 // Test with a good one 490 config["output_directory"] = "i-hope-i-dont-exist" 491 b = Builder{} 492 err = b.Prepare(config) 493 if err != nil { 494 t.Fatalf("should not have error: %s", err) 495 } 496 } 497 498 func TestBuilderPrepare_ShutdownTimeout(t *testing.T) { 499 var b Builder 500 config := testConfig() 501 502 // Test with a bad value 503 config["shutdown_timeout"] = "this is not good" 504 err := b.Prepare(config) 505 if err == nil { 506 t.Fatal("should have error") 507 } 508 509 // Test with a good one 510 config["shutdown_timeout"] = "5s" 511 b = Builder{} 512 err = b.Prepare(config) 513 if err != nil { 514 t.Fatalf("should not have error: %s", err) 515 } 516 } 517 518 func TestBuilderPrepare_SSHHostPort(t *testing.T) { 519 var b Builder 520 config := testConfig() 521 522 // Bad 523 config["ssh_host_port_min"] = 1000 524 config["ssh_host_port_max"] = 500 525 b = Builder{} 526 err := b.Prepare(config) 527 if err == nil { 528 t.Fatal("should have error") 529 } 530 531 // Bad 532 config["ssh_host_port_min"] = -500 533 b = Builder{} 534 err = b.Prepare(config) 535 if err == nil { 536 t.Fatal("should have error") 537 } 538 539 // Good 540 config["ssh_host_port_min"] = 500 541 config["ssh_host_port_max"] = 1000 542 b = Builder{} 543 err = b.Prepare(config) 544 if err != nil { 545 t.Fatalf("should not have error: %s", err) 546 } 547 } 548 549 func TestBuilderPrepare_sshKeyPath(t *testing.T) { 550 var b Builder 551 config := testConfig() 552 553 config["ssh_key_path"] = "" 554 b = Builder{} 555 err := b.Prepare(config) 556 if err != nil { 557 t.Fatalf("should not have error: %s", err) 558 } 559 560 config["ssh_key_path"] = "/i/dont/exist" 561 b = Builder{} 562 err = b.Prepare(config) 563 if err == nil { 564 t.Fatal("should have error") 565 } 566 567 // Test bad contents 568 tf, err := ioutil.TempFile("", "packer") 569 if err != nil { 570 t.Fatalf("err: %s", err) 571 } 572 defer os.Remove(tf.Name()) 573 defer tf.Close() 574 575 if _, err := tf.Write([]byte("HELLO!")); err != nil { 576 t.Fatalf("err: %s", err) 577 } 578 579 config["ssh_key_path"] = tf.Name() 580 b = Builder{} 581 err = b.Prepare(config) 582 if err == nil { 583 t.Fatal("should have error") 584 } 585 586 // Test good contents 587 tf.Seek(0, 0) 588 tf.Truncate(0) 589 tf.Write([]byte(testPem)) 590 config["ssh_key_path"] = tf.Name() 591 b = Builder{} 592 err = b.Prepare(config) 593 if err != nil { 594 t.Fatalf("err: %s", err) 595 } 596 } 597 598 func TestBuilderPrepare_SSHUser(t *testing.T) { 599 var b Builder 600 config := testConfig() 601 602 config["ssh_username"] = "" 603 b = Builder{} 604 err := b.Prepare(config) 605 if err == nil { 606 t.Fatal("should have error") 607 } 608 609 config["ssh_username"] = "exists" 610 b = Builder{} 611 err = b.Prepare(config) 612 if err != nil { 613 t.Fatalf("should not have error: %s", err) 614 } 615 } 616 617 func TestBuilderPrepare_SSHWaitTimeout(t *testing.T) { 618 var b Builder 619 config := testConfig() 620 621 // Test a default boot_wait 622 delete(config, "ssh_wait_timeout") 623 err := b.Prepare(config) 624 if err != nil { 625 t.Fatalf("err: %s", err) 626 } 627 628 if b.config.RawSSHWaitTimeout != "20m" { 629 t.Fatalf("bad value: %s", b.config.RawSSHWaitTimeout) 630 } 631 632 // Test with a bad value 633 config["ssh_wait_timeout"] = "this is not good" 634 b = Builder{} 635 err = b.Prepare(config) 636 if err == nil { 637 t.Fatal("should have error") 638 } 639 640 // Test with a good one 641 config["ssh_wait_timeout"] = "5s" 642 b = Builder{} 643 err = b.Prepare(config) 644 if err != nil { 645 t.Fatalf("should not have error: %s", err) 646 } 647 } 648 649 func TestBuilderPrepare_VBoxManage(t *testing.T) { 650 var b Builder 651 config := testConfig() 652 653 // Test with empty 654 delete(config, "vboxmanage") 655 err := b.Prepare(config) 656 if err != nil { 657 t.Fatalf("err: %s", err) 658 } 659 660 if !reflect.DeepEqual(b.config.VBoxManage, [][]string{}) { 661 t.Fatalf("bad: %#v", b.config.VBoxManage) 662 } 663 664 // Test with a good one 665 config["vboxmanage"] = [][]interface{}{ 666 []interface{}{"foo", "bar", "baz"}, 667 } 668 669 b = Builder{} 670 err = b.Prepare(config) 671 if err != nil { 672 t.Fatalf("should not have error: %s", err) 673 } 674 675 expected := [][]string{ 676 []string{"foo", "bar", "baz"}, 677 } 678 679 if !reflect.DeepEqual(b.config.VBoxManage, expected) { 680 t.Fatalf("bad: %#v", b.config.VBoxManage) 681 } 682 } 683 684 func TestBuilderPrepare_VBoxVersionFile(t *testing.T) { 685 var b Builder 686 config := testConfig() 687 688 // Test empty 689 delete(config, "virtualbox_version_file") 690 err := b.Prepare(config) 691 if err != nil { 692 t.Fatalf("err: %s", err) 693 } 694 695 if b.config.VBoxVersionFile != ".vbox_version" { 696 t.Fatalf("bad value: %s", b.config.VBoxVersionFile) 697 } 698 699 // Test with a good one 700 config["virtualbox_version_file"] = "foo" 701 b = Builder{} 702 err = b.Prepare(config) 703 if err != nil { 704 t.Fatalf("should not have error: %s", err) 705 } 706 707 if b.config.VBoxVersionFile != "foo" { 708 t.Fatalf("bad value: %s", b.config.VBoxVersionFile) 709 } 710 }