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