2013-06-11 18:12:45 -04:00
|
|
|
package virtualbox
|
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/mitchellh/packer/packer"
|
2013-06-11 23:00:30 -04:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2013-06-23 23:58:22 -04:00
|
|
|
"reflect"
|
2013-06-11 18:12:45 -04:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2013-08-28 01:54:56 -04:00
|
|
|
var testPem = `
|
|
|
|
-----BEGIN RSA PRIVATE KEY-----
|
|
|
|
MIIEpQIBAAKCAQEAxd4iamvrwRJvtNDGQSIbNvvIQN8imXTRWlRY62EvKov60vqu
|
|
|
|
hh+rDzFYAIIzlmrJopvOe0clqmi3mIP9dtkjPFrYflq52a2CF5q+BdwsJXuRHbJW
|
|
|
|
LmStZUwW1khSz93DhvhmK50nIaczW63u4EO/jJb3xj+wxR1Nkk9bxi3DDsYFt8SN
|
|
|
|
AzYx9kjlEYQ/+sI4/ATfmdV9h78SVotjScupd9KFzzi76gWq9gwyCBLRynTUWlyD
|
|
|
|
2UOfJRkOvhN6/jKzvYfVVwjPSfA9IMuooHdScmC4F6KBKJl/zf/zETM0XyzIDNmH
|
|
|
|
uOPbCiljq2WoRM+rY6ET84EO0kVXbfx8uxUsqQIDAQABAoIBAQCkPj9TF0IagbM3
|
|
|
|
5BSs/CKbAWS4dH/D4bPlxx4IRCNirc8GUg+MRb04Xz0tLuajdQDqeWpr6iLZ0RKV
|
|
|
|
BvreLF+TOdV7DNQ4XE4gSdJyCtCaTHeort/aordL3l0WgfI7mVk0L/yfN1PEG4YG
|
|
|
|
E9q1TYcyrB3/8d5JwIkjabxERLglCcP+geOEJp+QijbvFIaZR/n2irlKW4gSy6ko
|
|
|
|
9B0fgUnhkHysSg49ChHQBPQ+o5BbpuLrPDFMiTPTPhdfsvGGcyCGeqfBA56oHcSF
|
|
|
|
K02Fg8OM+Bd1lb48LAN9nWWY4WbwV+9bkN3Ym8hO4c3a/Dxf2N7LtAQqWZzFjvM3
|
|
|
|
/AaDvAgBAoGBAPLD+Xn1IYQPMB2XXCXfOuJewRY7RzoVWvMffJPDfm16O7wOiW5+
|
|
|
|
2FmvxUDayk4PZy6wQMzGeGKnhcMMZTyaq2g/QtGfrvy7q1Lw2fB1VFlVblvqhoJa
|
|
|
|
nMJojjC4zgjBkXMHsRLeTmgUKyGs+fdFbfI6uejBnnf+eMVUMIdJ+6I9AoGBANCn
|
|
|
|
kWO9640dttyXURxNJ3lBr2H3dJOkmD6XS+u+LWqCSKQe691Y/fZ/ZL0Oc4Mhy7I6
|
|
|
|
hsy3kDQ5k2V0fkaNODQIFJvUqXw2pMewUk8hHc9403f4fe9cPrL12rQ8WlQw4yoC
|
|
|
|
v2B61vNczCCUDtGxlAaw8jzSRaSI5s6ax3K7enbdAoGBAJB1WYDfA2CoAQO6y9Sl
|
|
|
|
b07A/7kQ8SN5DbPaqrDrBdJziBQxukoMJQXJeGFNUFD/DXFU5Fp2R7C86vXT7HIR
|
|
|
|
v6m66zH+CYzOx/YE6EsUJms6UP9VIVF0Rg/RU7teXQwM01ZV32LQ8mswhTH20o/3
|
|
|
|
uqMHmxUMEhZpUMhrfq0isyApAoGAe1UxGTXfj9AqkIVYylPIq2HqGww7+jFmVEj1
|
|
|
|
9Wi6S6Sq72ffnzzFEPkIQL/UA4TsdHMnzsYKFPSbbXLIWUeMGyVTmTDA5c0e5XIR
|
|
|
|
lPhMOKCAzv8w4VUzMnEkTzkFY5JqFCD/ojW57KvDdNZPVB+VEcdxyAW6aKELXMAc
|
|
|
|
eHLc1nkCgYEApm/motCTPN32nINZ+Vvywbv64ZD+gtpeMNP3CLrbe1X9O+H52AXa
|
|
|
|
1jCoOldWR8i2bs2NVPcKZgdo6fFULqE4dBX7Te/uYEIuuZhYLNzRO1IKU/YaqsXG
|
|
|
|
3bfQ8hKYcSnTfE0gPtLDnqCIxTocaGLSHeG3TH9fTw+dA8FvWpUztI4=
|
|
|
|
-----END RSA PRIVATE KEY-----
|
|
|
|
`
|
|
|
|
|
2013-06-11 18:12:45 -04:00
|
|
|
func testConfig() map[string]interface{} {
|
2013-06-11 23:00:30 -04:00
|
|
|
return map[string]interface{}{
|
2013-07-14 02:50:34 -04:00
|
|
|
"iso_checksum": "foo",
|
|
|
|
"iso_checksum_type": "md5",
|
|
|
|
"iso_url": "http://www.google.com/",
|
|
|
|
"ssh_username": "foo",
|
2013-07-01 14:11:31 -04:00
|
|
|
|
|
|
|
packer.BuildNameConfigKey: "foo",
|
2013-06-11 23:00:30 -04:00
|
|
|
}
|
2013-06-11 18:12:45 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilder_ImplementsBuilder(t *testing.T) {
|
|
|
|
var raw interface{}
|
|
|
|
raw = &Builder{}
|
|
|
|
if _, ok := raw.(packer.Builder); !ok {
|
|
|
|
t.Error("Builder must implement builder.")
|
|
|
|
}
|
|
|
|
}
|
2013-06-11 18:57:20 -04:00
|
|
|
|
|
|
|
func TestBuilderPrepare_Defaults(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
err := b.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.GuestOSType != "Other" {
|
|
|
|
t.Errorf("bad guest OS type: %s", b.config.GuestOSType)
|
|
|
|
}
|
|
|
|
|
2013-07-01 14:11:31 -04:00
|
|
|
if b.config.OutputDir != "output-foo" {
|
2013-06-11 18:57:20 -04:00
|
|
|
t.Errorf("bad output dir: %s", b.config.OutputDir)
|
|
|
|
}
|
2013-06-11 19:12:19 -04:00
|
|
|
|
2013-06-11 23:29:39 -04:00
|
|
|
if b.config.SSHHostPortMin != 2222 {
|
|
|
|
t.Errorf("bad min ssh host port: %d", b.config.SSHHostPortMin)
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.SSHHostPortMax != 4444 {
|
|
|
|
t.Errorf("bad max ssh host port: %d", b.config.SSHHostPortMax)
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.SSHPort != 22 {
|
|
|
|
t.Errorf("bad ssh port: %d", b.config.SSHPort)
|
|
|
|
}
|
|
|
|
|
2013-07-01 14:11:31 -04:00
|
|
|
if b.config.VMName != "packer-foo" {
|
2013-06-11 19:12:19 -04:00
|
|
|
t.Errorf("bad vm name: %s", b.config.VMName)
|
|
|
|
}
|
2013-08-19 14:21:36 -04:00
|
|
|
|
|
|
|
if b.config.Format != "ovf" {
|
|
|
|
t.Errorf("bad format: %s", b.config.Format)
|
|
|
|
}
|
2013-06-11 18:57:20 -04:00
|
|
|
}
|
2013-06-11 23:00:30 -04:00
|
|
|
|
2013-06-11 23:56:44 -04:00
|
|
|
func TestBuilderPrepare_BootWait(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
2013-06-28 15:58:32 -04:00
|
|
|
// Test a default boot_wait
|
|
|
|
delete(config, "boot_wait")
|
|
|
|
err := b.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2013-06-28 16:00:42 -04:00
|
|
|
if b.config.RawBootWait != "10s" {
|
|
|
|
t.Fatalf("bad value: %s", b.config.RawBootWait)
|
|
|
|
}
|
|
|
|
|
2013-06-11 23:56:44 -04:00
|
|
|
// Test with a bad boot_wait
|
|
|
|
config["boot_wait"] = "this is not good"
|
2013-06-28 15:58:32 -04:00
|
|
|
err = b.Prepare(config)
|
2013-06-11 23:56:44 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test with a good one
|
|
|
|
config["boot_wait"] = "5s"
|
2013-08-15 14:57:29 -04:00
|
|
|
b = Builder{}
|
2013-06-11 23:56:44 -04:00
|
|
|
err = b.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-23 23:43:40 -04:00
|
|
|
func TestBuilderPrepare_DiskSize(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
delete(config, "disk_size")
|
|
|
|
err := b.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.DiskSize != 40000 {
|
|
|
|
t.Fatalf("bad size: %d", b.config.DiskSize)
|
|
|
|
}
|
|
|
|
|
|
|
|
config["disk_size"] = 60000
|
|
|
|
b = Builder{}
|
|
|
|
err = b.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.DiskSize != 60000 {
|
|
|
|
t.Fatalf("bad size: %s", b.config.DiskSize)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-09 15:29:40 -04:00
|
|
|
func TestBuilderPrepare_FloppyFiles(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
delete(config, "floppy_files")
|
|
|
|
err := b.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(b.config.FloppyFiles) != 0 {
|
|
|
|
t.Fatalf("bad: %#v", b.config.FloppyFiles)
|
|
|
|
}
|
|
|
|
|
|
|
|
config["floppy_files"] = []string{"foo", "bar"}
|
|
|
|
b = Builder{}
|
|
|
|
err = b.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := []string{"foo", "bar"}
|
|
|
|
if !reflect.DeepEqual(b.config.FloppyFiles, expected) {
|
|
|
|
t.Fatalf("bad: %#v", b.config.FloppyFiles)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-24 02:05:32 -04:00
|
|
|
func TestBuilderPrepare_GuestAdditionsPath(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
2013-07-07 12:09:22 -04:00
|
|
|
delete(config, "guest_additions_path")
|
2013-06-24 02:05:32 -04:00
|
|
|
err := b.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.GuestAdditionsPath != "VBoxGuestAdditions.iso" {
|
|
|
|
t.Fatalf("bad: %s", b.config.GuestAdditionsPath)
|
|
|
|
}
|
|
|
|
|
|
|
|
config["guest_additions_path"] = "foo"
|
|
|
|
b = Builder{}
|
|
|
|
err = b.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.GuestAdditionsPath != "foo" {
|
|
|
|
t.Fatalf("bad size: %s", b.config.GuestAdditionsPath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-07 12:14:16 -04:00
|
|
|
func TestBuilderPrepare_GuestAdditionsSHA256(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
delete(config, "guest_additions_sha256")
|
|
|
|
err := b.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("bad err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.GuestAdditionsSHA256 != "" {
|
|
|
|
t.Fatalf("bad: %s", b.config.GuestAdditionsSHA256)
|
|
|
|
}
|
|
|
|
|
|
|
|
config["guest_additions_sha256"] = "FOO"
|
|
|
|
b = Builder{}
|
|
|
|
err = b.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.GuestAdditionsSHA256 != "foo" {
|
|
|
|
t.Fatalf("bad size: %s", b.config.GuestAdditionsSHA256)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilderPrepare_GuestAdditionsURL(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
config["guest_additions_url"] = ""
|
|
|
|
err := b.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.GuestAdditionsURL != "" {
|
|
|
|
t.Fatalf("should be empty: %s", b.config.GuestAdditionsURL)
|
|
|
|
}
|
|
|
|
|
|
|
|
config["guest_additions_url"] = "http://www.packer.io"
|
2013-08-15 14:57:29 -04:00
|
|
|
b = Builder{}
|
2013-07-07 12:14:16 -04:00
|
|
|
err = b.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-05 15:07:58 -04:00
|
|
|
func TestBuilderPrepare_HardDriveInterface(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
// Test a default boot_wait
|
|
|
|
delete(config, "hard_drive_interface")
|
|
|
|
err := b.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.HardDriveInterface != "ide" {
|
|
|
|
t.Fatalf("bad: %s", b.config.HardDriveInterface)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test with a bad
|
|
|
|
config["hard_drive_interface"] = "fake"
|
|
|
|
b = Builder{}
|
|
|
|
err = b.Prepare(config)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test with a good
|
|
|
|
config["hard_drive_interface"] = "sata"
|
|
|
|
b = Builder{}
|
|
|
|
err = b.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-12 02:44:40 -04:00
|
|
|
func TestBuilderPrepare_HTTPPort(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
// Bad
|
|
|
|
config["http_port_min"] = 1000
|
|
|
|
config["http_port_max"] = 500
|
|
|
|
err := b.Prepare(config)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bad
|
|
|
|
config["http_port_min"] = -500
|
2013-08-15 14:57:29 -04:00
|
|
|
b = Builder{}
|
2013-06-12 02:44:40 -04:00
|
|
|
err = b.Prepare(config)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Good
|
|
|
|
config["http_port_min"] = 500
|
|
|
|
config["http_port_max"] = 1000
|
2013-08-15 14:57:29 -04:00
|
|
|
b = Builder{}
|
2013-06-12 02:44:40 -04:00
|
|
|
err = b.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
2013-07-13 20:28:56 -04:00
|
|
|
}
|
|
|
|
|
2013-08-19 14:21:36 -04:00
|
|
|
func TestBuilderPrepare_Format(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
// Bad
|
|
|
|
config["format"] = "illegal value"
|
|
|
|
err := b.Prepare(config)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Good
|
|
|
|
config["format"] = "ova"
|
|
|
|
b = Builder{}
|
|
|
|
err = b.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Good
|
|
|
|
config["format"] = "ovf"
|
|
|
|
b = Builder{}
|
|
|
|
err = b.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-13 20:28:56 -04:00
|
|
|
func TestBuilderPrepare_InvalidKey(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
// Add a random key
|
|
|
|
config["i_should_not_be_valid"] = true
|
|
|
|
err := b.Prepare(config)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
2013-06-12 02:44:40 -04:00
|
|
|
}
|
|
|
|
|
2013-07-14 02:50:34 -04:00
|
|
|
func TestBuilderPrepare_ISOChecksum(t *testing.T) {
|
2013-06-11 23:00:30 -04:00
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
// Test bad
|
2013-07-14 02:50:34 -04:00
|
|
|
config["iso_checksum"] = ""
|
2013-06-11 23:00:30 -04:00
|
|
|
err := b.Prepare(config)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test good
|
2013-07-14 02:50:34 -04:00
|
|
|
config["iso_checksum"] = "FOo"
|
2013-08-15 14:57:29 -04:00
|
|
|
b = Builder{}
|
2013-06-11 23:00:30 -04:00
|
|
|
err = b.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
2013-07-14 02:50:34 -04:00
|
|
|
if b.config.ISOChecksum != "foo" {
|
|
|
|
t.Fatalf("should've lowercased: %s", b.config.ISOChecksum)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilderPrepare_ISOChecksumType(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
// Test bad
|
|
|
|
config["iso_checksum_type"] = ""
|
|
|
|
err := b.Prepare(config)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test good
|
|
|
|
config["iso_checksum_type"] = "mD5"
|
2013-08-15 14:57:29 -04:00
|
|
|
b = Builder{}
|
2013-07-14 02:50:34 -04:00
|
|
|
err = b.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.ISOChecksumType != "md5" {
|
|
|
|
t.Fatalf("should've lowercased: %s", b.config.ISOChecksumType)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test unknown
|
|
|
|
config["iso_checksum_type"] = "fake"
|
2013-08-15 14:57:29 -04:00
|
|
|
b = Builder{}
|
2013-07-14 02:50:34 -04:00
|
|
|
err = b.Prepare(config)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
2013-06-11 23:00:30 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilderPrepare_ISOUrl(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
2013-08-15 14:57:29 -04:00
|
|
|
delete(config, "iso_url")
|
|
|
|
delete(config, "iso_urls")
|
2013-06-11 23:00:30 -04:00
|
|
|
|
2013-08-15 14:57:29 -04:00
|
|
|
// Test both epty
|
2013-06-11 23:00:30 -04:00
|
|
|
config["iso_url"] = ""
|
2013-08-15 14:57:29 -04:00
|
|
|
b = Builder{}
|
2013-06-11 23:00:30 -04:00
|
|
|
err := b.Prepare(config)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
|
2013-08-15 14:57:29 -04:00
|
|
|
// Test iso_url set
|
2013-06-11 23:00:30 -04:00
|
|
|
config["iso_url"] = "http://www.packer.io"
|
2013-08-15 14:57:29 -04:00
|
|
|
b = Builder{}
|
2013-06-11 23:00:30 -04:00
|
|
|
err = b.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("should not have error: %s", err)
|
|
|
|
}
|
2013-08-15 14:57:29 -04:00
|
|
|
|
|
|
|
expected := []string{"http://www.packer.io"}
|
|
|
|
if !reflect.DeepEqual(b.config.ISOUrls, expected) {
|
|
|
|
t.Fatalf("bad: %#v", b.config.ISOUrls)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test both set
|
|
|
|
config["iso_url"] = "http://www.packer.io"
|
|
|
|
config["iso_urls"] = []string{"http://www.packer.io"}
|
|
|
|
b = Builder{}
|
|
|
|
err = b.Prepare(config)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test just iso_urls set
|
|
|
|
delete(config, "iso_url")
|
|
|
|
config["iso_urls"] = []string{
|
|
|
|
"http://www.packer.io",
|
|
|
|
"http://www.hashicorp.com",
|
|
|
|
}
|
|
|
|
|
|
|
|
b = Builder{}
|
|
|
|
err = b.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Errorf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected = []string{
|
|
|
|
"http://www.packer.io",
|
|
|
|
"http://www.hashicorp.com",
|
|
|
|
}
|
|
|
|
if !reflect.DeepEqual(b.config.ISOUrls, expected) {
|
|
|
|
t.Fatalf("bad: %#v", b.config.ISOUrls)
|
|
|
|
}
|
2013-06-11 23:00:30 -04:00
|
|
|
}
|
2013-06-11 23:51:58 -04:00
|
|
|
|
2013-06-27 22:14:23 -04:00
|
|
|
func TestBuilderPrepare_OutputDir(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
// Test with existing dir
|
|
|
|
dir, err := ioutil.TempDir("", "packer")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
|
|
config["output_directory"] = dir
|
2013-08-15 14:57:29 -04:00
|
|
|
b = Builder{}
|
2013-06-27 22:14:23 -04:00
|
|
|
err = b.Prepare(config)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test with a good one
|
|
|
|
config["output_directory"] = "i-hope-i-dont-exist"
|
2013-08-15 14:57:29 -04:00
|
|
|
b = Builder{}
|
2013-06-27 22:14:23 -04:00
|
|
|
err = b.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-12 21:02:42 -04:00
|
|
|
func TestBuilderPrepare_ShutdownTimeout(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
// Test with a bad value
|
|
|
|
config["shutdown_timeout"] = "this is not good"
|
|
|
|
err := b.Prepare(config)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test with a good one
|
|
|
|
config["shutdown_timeout"] = "5s"
|
2013-08-15 14:57:29 -04:00
|
|
|
b = Builder{}
|
2013-06-12 21:02:42 -04:00
|
|
|
err = b.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-11 23:51:58 -04:00
|
|
|
func TestBuilderPrepare_SSHHostPort(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
// Bad
|
|
|
|
config["ssh_host_port_min"] = 1000
|
|
|
|
config["ssh_host_port_max"] = 500
|
2013-08-15 14:57:29 -04:00
|
|
|
b = Builder{}
|
2013-06-11 23:51:58 -04:00
|
|
|
err := b.Prepare(config)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Bad
|
|
|
|
config["ssh_host_port_min"] = -500
|
2013-08-15 14:57:29 -04:00
|
|
|
b = Builder{}
|
2013-06-11 23:51:58 -04:00
|
|
|
err = b.Prepare(config)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Good
|
|
|
|
config["ssh_host_port_min"] = 500
|
|
|
|
config["ssh_host_port_max"] = 1000
|
2013-08-15 14:57:29 -04:00
|
|
|
b = Builder{}
|
2013-06-11 23:51:58 -04:00
|
|
|
err = b.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
}
|
2013-06-12 03:41:58 -04:00
|
|
|
|
2013-08-28 01:54:56 -04:00
|
|
|
func TestBuilderPrepare_sshKeyPath(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
config["ssh_key_path"] = ""
|
|
|
|
b = Builder{}
|
|
|
|
err := b.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
config["ssh_key_path"] = "/i/dont/exist"
|
|
|
|
b = Builder{}
|
|
|
|
err = b.Prepare(config)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test bad contents
|
|
|
|
tf, err := ioutil.TempFile("", "packer")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.Remove(tf.Name())
|
|
|
|
defer tf.Close()
|
|
|
|
|
|
|
|
if _, err := tf.Write([]byte("HELLO!")); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
config["ssh_key_path"] = tf.Name()
|
|
|
|
b = Builder{}
|
|
|
|
err = b.Prepare(config)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test good contents
|
|
|
|
tf.Seek(0, 0)
|
|
|
|
tf.Truncate(0)
|
|
|
|
tf.Write([]byte(testPem))
|
|
|
|
config["ssh_key_path"] = tf.Name()
|
|
|
|
b = Builder{}
|
|
|
|
err = b.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-06-12 03:41:58 -04:00
|
|
|
func TestBuilderPrepare_SSHUser(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
config["ssh_username"] = ""
|
2013-08-15 14:57:29 -04:00
|
|
|
b = Builder{}
|
2013-06-12 03:41:58 -04:00
|
|
|
err := b.Prepare(config)
|
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
config["ssh_username"] = "exists"
|
2013-08-15 14:57:29 -04:00
|
|
|
b = Builder{}
|
2013-06-12 03:41:58 -04:00
|
|
|
err = b.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestBuilderPrepare_SSHWaitTimeout(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
2013-06-28 16:00:42 -04:00
|
|
|
// Test a default boot_wait
|
|
|
|
delete(config, "ssh_wait_timeout")
|
|
|
|
err := b.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.RawSSHWaitTimeout != "20m" {
|
|
|
|
t.Fatalf("bad value: %s", b.config.RawSSHWaitTimeout)
|
|
|
|
}
|
|
|
|
|
2013-06-12 03:41:58 -04:00
|
|
|
// Test with a bad value
|
|
|
|
config["ssh_wait_timeout"] = "this is not good"
|
2013-08-15 14:57:29 -04:00
|
|
|
b = Builder{}
|
2013-06-28 16:00:42 -04:00
|
|
|
err = b.Prepare(config)
|
2013-06-12 03:41:58 -04:00
|
|
|
if err == nil {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test with a good one
|
|
|
|
config["ssh_wait_timeout"] = "5s"
|
2013-08-15 14:57:29 -04:00
|
|
|
b = Builder{}
|
2013-06-12 03:41:58 -04:00
|
|
|
err = b.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
}
|
2013-06-23 23:58:22 -04:00
|
|
|
|
|
|
|
func TestBuilderPrepare_VBoxManage(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
// Test with empty
|
|
|
|
delete(config, "vboxmanage")
|
|
|
|
err := b.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(b.config.VBoxManage, [][]string{}) {
|
|
|
|
t.Fatalf("bad: %#v", b.config.VBoxManage)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test with a good one
|
|
|
|
config["vboxmanage"] = [][]interface{}{
|
|
|
|
[]interface{}{"foo", "bar", "baz"},
|
|
|
|
}
|
|
|
|
|
|
|
|
b = Builder{}
|
|
|
|
err = b.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
expected := [][]string{
|
|
|
|
[]string{"foo", "bar", "baz"},
|
|
|
|
}
|
|
|
|
|
|
|
|
if !reflect.DeepEqual(b.config.VBoxManage, expected) {
|
|
|
|
t.Fatalf("bad: %#v", b.config.VBoxManage)
|
|
|
|
}
|
|
|
|
}
|
2013-06-24 01:44:58 -04:00
|
|
|
|
|
|
|
func TestBuilderPrepare_VBoxVersionFile(t *testing.T) {
|
|
|
|
var b Builder
|
|
|
|
config := testConfig()
|
|
|
|
|
|
|
|
// Test empty
|
|
|
|
delete(config, "virtualbox_version_file")
|
|
|
|
err := b.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.VBoxVersionFile != ".vbox_version" {
|
|
|
|
t.Fatalf("bad value: %s", b.config.VBoxVersionFile)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test with a good one
|
|
|
|
config["virtualbox_version_file"] = "foo"
|
|
|
|
b = Builder{}
|
|
|
|
err = b.Prepare(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("should not have error: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if b.config.VBoxVersionFile != "foo" {
|
|
|
|
t.Fatalf("bad value: %s", b.config.VBoxVersionFile)
|
|
|
|
}
|
|
|
|
}
|