From acededfc6ec9819143e17c5436420d5cab1cac0b Mon Sep 17 00:00:00 2001 From: Ricard Clau Date: Tue, 26 Jul 2016 20:42:04 +0100 Subject: [PATCH] tests actually test the floppies --- .../parallels/common/floppy_config_test.go | 18 ----- builder/parallels/iso/builder.go | 2 +- builder/parallels/iso/builder_test.go | 54 +++++++++++++- builder/parallels/pvm/config.go | 2 +- builder/parallels/pvm/config_test.go | 27 +++++++ builder/qemu/builder.go | 11 ++- builder/qemu/builder_test.go | 53 +++++++++++++- builder/virtualbox/common/floppy_config.go | 19 ----- .../virtualbox/common/floppy_config_test.go | 18 ----- builder/virtualbox/iso/builder.go | 2 +- builder/virtualbox/iso/builder_test.go | 54 +++++++++++++- builder/virtualbox/ovf/config.go | 2 +- builder/virtualbox/ovf/config_test.go | 27 +++++++ builder/vmware/iso/builder.go | 7 +- builder/vmware/iso/builder_test.go | 21 +++++- builder/vmware/vmx/builder_test.go | 23 +++++- builder/vmware/vmx/config.go | 3 +- builder/vmware/vmx/config_test.go | 1 + .../common => common}/floppy_config.go | 15 +++- common/floppy_config_test.go | 70 +++++++++++++++++++ common/test-fixtures/floppies/bar.bat | 1 + common/test-fixtures/floppies/foo.ps1 | 1 + 22 files changed, 350 insertions(+), 81 deletions(-) delete mode 100644 builder/parallels/common/floppy_config_test.go delete mode 100644 builder/virtualbox/common/floppy_config.go delete mode 100644 builder/virtualbox/common/floppy_config_test.go rename {builder/parallels/common => common}/floppy_config.go (58%) create mode 100644 common/floppy_config_test.go create mode 100644 common/test-fixtures/floppies/bar.bat create mode 100644 common/test-fixtures/floppies/foo.ps1 diff --git a/builder/parallels/common/floppy_config_test.go b/builder/parallels/common/floppy_config_test.go deleted file mode 100644 index 3e4fc55db..000000000 --- a/builder/parallels/common/floppy_config_test.go +++ /dev/null @@ -1,18 +0,0 @@ -package common - -import ( - "testing" -) - -func TestFloppyConfigPrepare(t *testing.T) { - c := new(FloppyConfig) - - errs := c.Prepare(testConfigTemplate(t)) - if len(errs) > 0 { - t.Fatalf("err: %#v", errs) - } - - if len(c.FloppyFiles) > 0 { - t.Fatal("should not have floppy files") - } -} diff --git a/builder/parallels/iso/builder.go b/builder/parallels/iso/builder.go index 546ff8890..134f980f3 100644 --- a/builder/parallels/iso/builder.go +++ b/builder/parallels/iso/builder.go @@ -25,7 +25,7 @@ type Config struct { common.PackerConfig `mapstructure:",squash"` common.HTTPConfig `mapstructure:",squash"` common.ISOConfig `mapstructure:",squash"` - parallelscommon.FloppyConfig `mapstructure:",squash"` + common.FloppyConfig `mapstructure:",squash"` parallelscommon.OutputConfig `mapstructure:",squash"` parallelscommon.PrlctlConfig `mapstructure:",squash"` parallelscommon.PrlctlPostConfig `mapstructure:",squash"` diff --git a/builder/parallels/iso/builder_test.go b/builder/parallels/iso/builder_test.go index 43e5b091d..7be21bd59 100644 --- a/builder/parallels/iso/builder_test.go +++ b/builder/parallels/iso/builder_test.go @@ -1,8 +1,11 @@ package iso import ( - "github.com/mitchellh/packer/packer" + "fmt" + "reflect" "testing" + + "github.com/mitchellh/packer/packer" ) func testConfig() map[string]interface{} { @@ -46,6 +49,55 @@ func TestBuilderPrepare_Defaults(t *testing.T) { } } +func TestBuilderPrepare_FloppyFiles(t *testing.T) { + var b Builder + config := testConfig() + + delete(config, "floppy_files") + warns, err := b.Prepare(config) + if len(warns) > 0 { + t.Fatalf("bad: %#v", warns) + } + if err != nil { + t.Fatalf("bad err: %s", err) + } + + if len(b.config.FloppyFiles) != 0 { + t.Fatalf("bad: %#v", b.config.FloppyFiles) + } + + floppies_path := "../../../common/test-fixtures/floppies" + config["floppy_files"] = []string{fmt.Sprintf("%s/bar.bat", floppies_path), fmt.Sprintf("%s/foo.ps1", floppies_path)} + b = Builder{} + warns, err = b.Prepare(config) + if len(warns) > 0 { + t.Fatalf("bad: %#v", warns) + } + if err != nil { + t.Fatalf("should not have error: %s", err) + } + + expected := []string{fmt.Sprintf("%s/bar.bat", floppies_path), fmt.Sprintf("%s/foo.ps1", floppies_path)} + if !reflect.DeepEqual(b.config.FloppyFiles, expected) { + t.Fatalf("bad: %#v", b.config.FloppyFiles) + } +} + +func TestBuilderPrepare_InvalidFloppies(t *testing.T) { + var b Builder + config := testConfig() + config["floppy_files"] = []string{"nonexistant.bat", "nonexistant.ps1"} + b = Builder{} + _, errs := b.Prepare(config) + if errs == nil { + t.Fatalf("Non existant floppies should trigger multierror") + } + + if len(errs.(*packer.MultiError).Errors) != 2 { + t.Fatalf("Multierror should work and report 2 errors") + } +} + func TestBuilderPrepare_DiskSize(t *testing.T) { var b Builder config := testConfig() diff --git a/builder/parallels/pvm/config.go b/builder/parallels/pvm/config.go index f3f27d314..47e90d7a6 100644 --- a/builder/parallels/pvm/config.go +++ b/builder/parallels/pvm/config.go @@ -14,7 +14,7 @@ import ( // Config is the configuration structure for the builder. type Config struct { common.PackerConfig `mapstructure:",squash"` - parallelscommon.FloppyConfig `mapstructure:",squash"` + common.FloppyConfig `mapstructure:",squash"` parallelscommon.OutputConfig `mapstructure:",squash"` parallelscommon.PrlctlConfig `mapstructure:",squash"` parallelscommon.PrlctlPostConfig `mapstructure:",squash"` diff --git a/builder/parallels/pvm/config_test.go b/builder/parallels/pvm/config_test.go index c4270c366..9334e9136 100644 --- a/builder/parallels/pvm/config_test.go +++ b/builder/parallels/pvm/config_test.go @@ -1,9 +1,12 @@ package pvm import ( + "fmt" "io/ioutil" "os" "testing" + + "github.com/mitchellh/packer/packer" ) func testConfig(t *testing.T) map[string]interface{} { @@ -11,6 +14,7 @@ func testConfig(t *testing.T) map[string]interface{} { "ssh_username": "foo", "shutdown_command": "foo", "parallels_tools_flavor": "lin", + "source_path": "config_test.go", } } @@ -68,6 +72,29 @@ func TestNewConfig_sourcePath(t *testing.T) { testConfigOk(t, warns, errs) } +func TestNewConfig_FloppyFiles(t *testing.T) { + c := testConfig(t) + floppies_path := "../../../common/test-fixtures/floppies" + c["floppy_files"] = []string{fmt.Sprintf("%s/bar.bat", floppies_path), fmt.Sprintf("%s/foo.ps1", floppies_path)} + _, _, err := NewConfig(c) + if err != nil { + t.Fatalf("should not have error: %s", err) + } +} + +func TestNewConfig_InvalidFloppies(t *testing.T) { + c := testConfig(t) + c["floppy_files"] = []string{"nonexistant.bat", "nonexistant.ps1"} + _, _, errs := NewConfig(c) + if errs == nil { + t.Fatalf("Non existant floppies should trigger multierror") + } + + if len(errs.(*packer.MultiError).Errors) != 2 { + t.Fatalf("Multierror should work and report 2 errors") + } +} + func TestNewConfig_shutdown_timeout(t *testing.T) { c := testConfig(t) tf := getTempFile(t) diff --git a/builder/qemu/builder.go b/builder/qemu/builder.go index f151bf68c..588313d06 100644 --- a/builder/qemu/builder.go +++ b/builder/qemu/builder.go @@ -82,6 +82,7 @@ type Config struct { common.HTTPConfig `mapstructure:",squash"` common.ISOConfig `mapstructure:",squash"` Comm communicator.Config `mapstructure:",squash"` + common.FloppyConfig `mapstructure:",squash"` ISOSkipCache bool `mapstructure:"iso_skip_cache"` Accelerator string `mapstructure:"accelerator"` @@ -139,6 +140,9 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { return nil, err } + var errs *packer.MultiError + warnings := make([]string, 0) + if b.config.DiskSize == 0 { b.config.DiskSize = 40000 } @@ -215,9 +219,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { b.config.Format = "qcow2" } - if b.config.FloppyFiles == nil { - b.config.FloppyFiles = make([]string, 0) - } + errs = packer.MultiErrorAppend(errs, b.config.FloppyConfig.Prepare(&b.config.ctx)...) if b.config.NetDevice == "" { b.config.NetDevice = "virtio-net" @@ -232,9 +234,6 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { b.config.Comm.SSHTimeout = b.config.SSHWaitTimeout } - var errs *packer.MultiError - warnings := make([]string, 0) - if b.config.ISOSkipCache { b.config.ISOChecksumType = "none" } diff --git a/builder/qemu/builder_test.go b/builder/qemu/builder_test.go index 5c68a9316..1f28bbac6 100644 --- a/builder/qemu/builder_test.go +++ b/builder/qemu/builder_test.go @@ -1,11 +1,13 @@ package qemu import ( - "github.com/mitchellh/packer/packer" + "fmt" "io/ioutil" "os" "reflect" "testing" + + "github.com/mitchellh/packer/packer" ) var testPem = ` @@ -262,6 +264,55 @@ func TestBuilderPrepare_Format(t *testing.T) { } } +func TestBuilderPrepare_FloppyFiles(t *testing.T) { + var b Builder + config := testConfig() + + delete(config, "floppy_files") + warns, err := b.Prepare(config) + if len(warns) > 0 { + t.Fatalf("bad: %#v", warns) + } + if err != nil { + t.Fatalf("bad err: %s", err) + } + + if len(b.config.FloppyFiles) != 0 { + t.Fatalf("bad: %#v", b.config.FloppyFiles) + } + + floppies_path := "../../common/test-fixtures/floppies" + config["floppy_files"] = []string{fmt.Sprintf("%s/bar.bat", floppies_path), fmt.Sprintf("%s/foo.ps1", floppies_path)} + b = Builder{} + warns, err = b.Prepare(config) + if len(warns) > 0 { + t.Fatalf("bad: %#v", warns) + } + if err != nil { + t.Fatalf("should not have error: %s", err) + } + + expected := []string{fmt.Sprintf("%s/bar.bat", floppies_path), fmt.Sprintf("%s/foo.ps1", floppies_path)} + if !reflect.DeepEqual(b.config.FloppyFiles, expected) { + t.Fatalf("bad: %#v", b.config.FloppyFiles) + } +} + +func TestBuilderPrepare_InvalidFloppies(t *testing.T) { + var b Builder + config := testConfig() + config["floppy_files"] = []string{"nonexistant.bat", "nonexistant.ps1"} + b = Builder{} + _, errs := b.Prepare(config) + if errs == nil { + t.Fatalf("Non existant floppies should trigger multierror") + } + + if len(errs.(*packer.MultiError).Errors) != 2 { + t.Fatalf("Multierror should work and report 2 errors") + } +} + func TestBuilderPrepare_InvalidKey(t *testing.T) { var b Builder config := testConfig() diff --git a/builder/virtualbox/common/floppy_config.go b/builder/virtualbox/common/floppy_config.go deleted file mode 100644 index 0e3f5e669..000000000 --- a/builder/virtualbox/common/floppy_config.go +++ /dev/null @@ -1,19 +0,0 @@ -package common - -import ( - "github.com/mitchellh/packer/template/interpolate" -) - -// FloppyConfig is configuration related to created floppy disks and attaching -// them to a VirtualBox machine. -type FloppyConfig struct { - FloppyFiles []string `mapstructure:"floppy_files"` -} - -func (c *FloppyConfig) Prepare(ctx *interpolate.Context) []error { - if c.FloppyFiles == nil { - c.FloppyFiles = make([]string, 0) - } - - return nil -} diff --git a/builder/virtualbox/common/floppy_config_test.go b/builder/virtualbox/common/floppy_config_test.go deleted file mode 100644 index 3e4fc55db..000000000 --- a/builder/virtualbox/common/floppy_config_test.go +++ /dev/null @@ -1,18 +0,0 @@ -package common - -import ( - "testing" -) - -func TestFloppyConfigPrepare(t *testing.T) { - c := new(FloppyConfig) - - errs := c.Prepare(testConfigTemplate(t)) - if len(errs) > 0 { - t.Fatalf("err: %#v", errs) - } - - if len(c.FloppyFiles) > 0 { - t.Fatal("should not have floppy files") - } -} diff --git a/builder/virtualbox/iso/builder.go b/builder/virtualbox/iso/builder.go index 99dff6263..fd8b98f13 100644 --- a/builder/virtualbox/iso/builder.go +++ b/builder/virtualbox/iso/builder.go @@ -26,9 +26,9 @@ type Config struct { common.PackerConfig `mapstructure:",squash"` common.HTTPConfig `mapstructure:",squash"` common.ISOConfig `mapstructure:",squash"` + common.FloppyConfig `mapstructure:",squash"` vboxcommon.ExportConfig `mapstructure:",squash"` vboxcommon.ExportOpts `mapstructure:",squash"` - vboxcommon.FloppyConfig `mapstructure:",squash"` vboxcommon.OutputConfig `mapstructure:",squash"` vboxcommon.RunConfig `mapstructure:",squash"` vboxcommon.ShutdownConfig `mapstructure:",squash"` diff --git a/builder/virtualbox/iso/builder_test.go b/builder/virtualbox/iso/builder_test.go index e4e3e6a53..cc15072de 100644 --- a/builder/virtualbox/iso/builder_test.go +++ b/builder/virtualbox/iso/builder_test.go @@ -1,9 +1,12 @@ package iso import ( + "fmt" + "reflect" + "testing" + "github.com/mitchellh/packer/builder/virtualbox/common" "github.com/mitchellh/packer/packer" - "testing" ) func testConfig() map[string]interface{} { @@ -86,6 +89,55 @@ func TestBuilderPrepare_DiskSize(t *testing.T) { } } +func TestBuilderPrepare_FloppyFiles(t *testing.T) { + var b Builder + config := testConfig() + + delete(config, "floppy_files") + warns, err := b.Prepare(config) + if len(warns) > 0 { + t.Fatalf("bad: %#v", warns) + } + if err != nil { + t.Fatalf("bad err: %s", err) + } + + if len(b.config.FloppyFiles) != 0 { + t.Fatalf("bad: %#v", b.config.FloppyFiles) + } + + floppies_path := "../../../common/test-fixtures/floppies" + config["floppy_files"] = []string{fmt.Sprintf("%s/bar.bat", floppies_path), fmt.Sprintf("%s/foo.ps1", floppies_path)} + b = Builder{} + warns, err = b.Prepare(config) + if len(warns) > 0 { + t.Fatalf("bad: %#v", warns) + } + if err != nil { + t.Fatalf("should not have error: %s", err) + } + + expected := []string{fmt.Sprintf("%s/bar.bat", floppies_path), fmt.Sprintf("%s/foo.ps1", floppies_path)} + if !reflect.DeepEqual(b.config.FloppyFiles, expected) { + t.Fatalf("bad: %#v", b.config.FloppyFiles) + } +} + +func TestBuilderPrepare_InvalidFloppies(t *testing.T) { + var b Builder + config := testConfig() + config["floppy_files"] = []string{"nonexistant.bat", "nonexistant.ps1"} + b = Builder{} + _, errs := b.Prepare(config) + if errs == nil { + t.Fatalf("Non existant floppies should trigger multierror") + } + + if len(errs.(*packer.MultiError).Errors) != 2 { + t.Fatalf("Multierror should work and report 2 errors") + } +} + func TestBuilderPrepare_GuestAdditionsMode(t *testing.T) { var b Builder config := testConfig() diff --git a/builder/virtualbox/ovf/config.go b/builder/virtualbox/ovf/config.go index 0aab0871d..0866a6205 100644 --- a/builder/virtualbox/ovf/config.go +++ b/builder/virtualbox/ovf/config.go @@ -16,9 +16,9 @@ import ( type Config struct { common.PackerConfig `mapstructure:",squash"` common.HTTPConfig `mapstructure:",squash"` + common.FloppyConfig `mapstructure:",squash"` vboxcommon.ExportConfig `mapstructure:",squash"` vboxcommon.ExportOpts `mapstructure:",squash"` - vboxcommon.FloppyConfig `mapstructure:",squash"` vboxcommon.OutputConfig `mapstructure:",squash"` vboxcommon.RunConfig `mapstructure:",squash"` vboxcommon.SSHConfig `mapstructure:",squash"` diff --git a/builder/virtualbox/ovf/config_test.go b/builder/virtualbox/ovf/config_test.go index a94c26bf9..04c5c71d4 100644 --- a/builder/virtualbox/ovf/config_test.go +++ b/builder/virtualbox/ovf/config_test.go @@ -1,15 +1,19 @@ package ovf import ( + "fmt" "io/ioutil" "os" "testing" + + "github.com/mitchellh/packer/packer" ) func testConfig(t *testing.T) map[string]interface{} { return map[string]interface{}{ "ssh_username": "foo", "shutdown_command": "foo", + "source_path": "config_test.go", } } @@ -44,6 +48,29 @@ func testConfigOk(t *testing.T, warns []string, err error) { } } +func TestNewConfig_FloppyFiles(t *testing.T) { + c := testConfig(t) + floppies_path := "../../../common/test-fixtures/floppies" + c["floppy_files"] = []string{fmt.Sprintf("%s/bar.bat", floppies_path), fmt.Sprintf("%s/foo.ps1", floppies_path)} + _, _, err := NewConfig(c) + if err != nil { + t.Fatalf("should not have error: %s", err) + } +} + +func TestNewConfig_InvalidFloppies(t *testing.T) { + c := testConfig(t) + c["floppy_files"] = []string{"nonexistant.bat", "nonexistant.ps1"} + _, _, errs := NewConfig(c) + if errs == nil { + t.Fatalf("Non existant floppies should trigger multierror") + } + + if len(errs.(*packer.MultiError).Errors) != 2 { + t.Fatalf("Multierror should work and report 2 errors") + } +} + func TestNewConfig_sourcePath(t *testing.T) { // Bad c := testConfig(t) diff --git a/builder/vmware/iso/builder.go b/builder/vmware/iso/builder.go index 2cfab3e79..f16a5fdfa 100755 --- a/builder/vmware/iso/builder.go +++ b/builder/vmware/iso/builder.go @@ -28,6 +28,7 @@ type Config struct { common.PackerConfig `mapstructure:",squash"` common.HTTPConfig `mapstructure:",squash"` common.ISOConfig `mapstructure:",squash"` + common.FloppyConfig `mapstructure:",squash"` vmwcommon.DriverConfig `mapstructure:",squash"` vmwcommon.OutputConfig `mapstructure:",squash"` vmwcommon.RunConfig `mapstructure:",squash"` @@ -40,7 +41,6 @@ type Config struct { DiskName string `mapstructure:"vmdk_name"` DiskSize uint `mapstructure:"disk_size"` DiskTypeId string `mapstructure:"disk_type_id"` - FloppyFiles []string `mapstructure:"floppy_files"` Format string `mapstructure:"format"` GuestOSType string `mapstructure:"guest_os_type"` Version string `mapstructure:"version"` @@ -95,6 +95,7 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { errs = packer.MultiErrorAppend(errs, b.config.SSHConfig.Prepare(&b.config.ctx)...) errs = packer.MultiErrorAppend(errs, b.config.ToolsConfig.Prepare(&b.config.ctx)...) errs = packer.MultiErrorAppend(errs, b.config.VMXConfig.Prepare(&b.config.ctx)...) + errs = packer.MultiErrorAppend(errs, b.config.FloppyConfig.Prepare(&b.config.ctx)...) if b.config.DiskName == "" { b.config.DiskName = "disk" @@ -113,10 +114,6 @@ func (b *Builder) Prepare(raws ...interface{}) ([]string, error) { } } - if b.config.FloppyFiles == nil { - b.config.FloppyFiles = make([]string, 0) - } - if b.config.GuestOSType == "" { b.config.GuestOSType = "other" } diff --git a/builder/vmware/iso/builder_test.go b/builder/vmware/iso/builder_test.go index bb2149506..368ec65e8 100644 --- a/builder/vmware/iso/builder_test.go +++ b/builder/vmware/iso/builder_test.go @@ -1,6 +1,7 @@ package iso import ( + "fmt" "io/ioutil" "os" "reflect" @@ -106,7 +107,8 @@ func TestBuilderPrepare_FloppyFiles(t *testing.T) { t.Fatalf("bad: %#v", b.config.FloppyFiles) } - config["floppy_files"] = []string{"foo", "bar"} + floppies_path := "../../../common/test-fixtures/floppies" + config["floppy_files"] = []string{fmt.Sprintf("%s/bar.bat", floppies_path), fmt.Sprintf("%s/foo.ps1", floppies_path)} b = Builder{} warns, err = b.Prepare(config) if len(warns) > 0 { @@ -116,12 +118,27 @@ func TestBuilderPrepare_FloppyFiles(t *testing.T) { t.Fatalf("should not have error: %s", err) } - expected := []string{"foo", "bar"} + expected := []string{fmt.Sprintf("%s/bar.bat", floppies_path), fmt.Sprintf("%s/foo.ps1", floppies_path)} if !reflect.DeepEqual(b.config.FloppyFiles, expected) { t.Fatalf("bad: %#v", b.config.FloppyFiles) } } +func TestBuilderPrepare_InvalidFloppies(t *testing.T) { + var b Builder + config := testConfig() + config["floppy_files"] = []string{"nonexistant.bat", "nonexistant.ps1"} + b = Builder{} + _, errs := b.Prepare(config) + if errs == nil { + t.Fatalf("Non existant floppies should trigger multierror") + } + + if len(errs.(*packer.MultiError).Errors) != 2 { + t.Fatalf("Multierror should work and report 2 errors") + } +} + func TestBuilderPrepare_Format(t *testing.T) { var b Builder config := testConfig() diff --git a/builder/vmware/vmx/builder_test.go b/builder/vmware/vmx/builder_test.go index f23f2f2e5..1c7f795f0 100644 --- a/builder/vmware/vmx/builder_test.go +++ b/builder/vmware/vmx/builder_test.go @@ -1,10 +1,13 @@ package vmx import ( + "fmt" "io/ioutil" "os" "reflect" "testing" + + "github.com/mitchellh/packer/packer" ) func TestBuilderPrepare_FloppyFiles(t *testing.T) { @@ -33,7 +36,8 @@ func TestBuilderPrepare_FloppyFiles(t *testing.T) { t.Fatalf("bad: %#v", b.config.FloppyFiles) } - config["floppy_files"] = []string{"foo", "bar"} + floppies_path := "../../../common/test-fixtures/floppies" + config["floppy_files"] = []string{fmt.Sprintf("%s/bar.bat", floppies_path), fmt.Sprintf("%s/foo.ps1", floppies_path)} b = Builder{} warns, err = b.Prepare(config) if len(warns) > 0 { @@ -43,8 +47,23 @@ func TestBuilderPrepare_FloppyFiles(t *testing.T) { t.Fatalf("should not have error: %s", err) } - expected := []string{"foo", "bar"} + expected := []string{fmt.Sprintf("%s/bar.bat", floppies_path), fmt.Sprintf("%s/foo.ps1", floppies_path)} if !reflect.DeepEqual(b.config.FloppyFiles, expected) { t.Fatalf("bad: %#v", b.config.FloppyFiles) } } + +func TestBuilderPrepare_InvalidFloppies(t *testing.T) { + var b Builder + config := testConfig(t) + config["floppy_files"] = []string{"nonexistant.bat", "nonexistant.ps1"} + b = Builder{} + _, errs := b.Prepare(config) + if errs == nil { + t.Fatalf("Non existant floppies should trigger multierror") + } + + if len(errs.(*packer.MultiError).Errors) != 2 { + t.Fatalf("Multierror should work and report 2 errors") + } +} diff --git a/builder/vmware/vmx/config.go b/builder/vmware/vmx/config.go index 4f93db9f3..52ba61582 100644 --- a/builder/vmware/vmx/config.go +++ b/builder/vmware/vmx/config.go @@ -15,6 +15,7 @@ import ( type Config struct { common.PackerConfig `mapstructure:",squash"` common.HTTPConfig `mapstructure:",squash"` + common.FloppyConfig `mapstructure:",squash"` vmwcommon.DriverConfig `mapstructure:",squash"` vmwcommon.OutputConfig `mapstructure:",squash"` vmwcommon.RunConfig `mapstructure:",squash"` @@ -24,7 +25,6 @@ type Config struct { vmwcommon.VMXConfig `mapstructure:",squash"` BootCommand []string `mapstructure:"boot_command"` - FloppyFiles []string `mapstructure:"floppy_files"` RemoteType string `mapstructure:"remote_type"` SkipCompaction bool `mapstructure:"skip_compaction"` SourcePath string `mapstructure:"source_path"` @@ -64,6 +64,7 @@ func NewConfig(raws ...interface{}) (*Config, []string, error) { errs = packer.MultiErrorAppend(errs, c.SSHConfig.Prepare(&c.ctx)...) errs = packer.MultiErrorAppend(errs, c.ToolsConfig.Prepare(&c.ctx)...) errs = packer.MultiErrorAppend(errs, c.VMXConfig.Prepare(&c.ctx)...) + errs = packer.MultiErrorAppend(errs, c.FloppyConfig.Prepare(&c.ctx)...) if c.SourcePath == "" { errs = packer.MultiErrorAppend(errs, fmt.Errorf("source_path is blank, but is required")) diff --git a/builder/vmware/vmx/config_test.go b/builder/vmware/vmx/config_test.go index ac49957e3..4ee0b9426 100644 --- a/builder/vmware/vmx/config_test.go +++ b/builder/vmware/vmx/config_test.go @@ -10,6 +10,7 @@ func testConfig(t *testing.T) map[string]interface{} { return map[string]interface{}{ "ssh_username": "foo", "shutdown_command": "foo", + "source_path": "config_test.go", } } diff --git a/builder/parallels/common/floppy_config.go b/common/floppy_config.go similarity index 58% rename from builder/parallels/common/floppy_config.go rename to common/floppy_config.go index d656e103a..bd5c5f8c7 100644 --- a/builder/parallels/common/floppy_config.go +++ b/common/floppy_config.go @@ -1,19 +1,28 @@ package common import ( + "fmt" + "os" + "github.com/mitchellh/packer/template/interpolate" ) -// FloppyConfig is configuration related to created floppy disks and attaching -// them to a Parallels virtual machine. type FloppyConfig struct { FloppyFiles []string `mapstructure:"floppy_files"` } func (c *FloppyConfig) Prepare(ctx *interpolate.Context) []error { + var errs []error + if c.FloppyFiles == nil { c.FloppyFiles = make([]string, 0) } - return nil + for _, path := range c.FloppyFiles { + if _, err := os.Stat(path); err != nil { + errs = append(errs, fmt.Errorf("Bad Floppy disk file '%s': %s", path, err)) + } + } + + return errs } diff --git a/common/floppy_config_test.go b/common/floppy_config_test.go new file mode 100644 index 000000000..ee9c742ff --- /dev/null +++ b/common/floppy_config_test.go @@ -0,0 +1,70 @@ +package common + +import ( + "testing" +) + +func TestNilFloppies(t *testing.T) { + c := FloppyConfig{} + errs := c.Prepare(nil) + if len(errs) != 0 { + t.Fatal("nil floppies array should not fail") + } + + if len(c.FloppyFiles) > 0 { + t.Fatal("struct should not have floppy files") + } +} + +func TestEmptyArrayFloppies(t *testing.T) { + c := FloppyConfig{ + FloppyFiles: make([]string, 0), + } + + errs := c.Prepare(nil) + if len(errs) != 0 { + t.Fatal("empty floppies array should never fail") + } + + if len(c.FloppyFiles) > 0 { + t.Fatal("struct should not have floppy files") + } +} + +func TestExistingFloppyFile(t *testing.T) { + c := FloppyConfig{ + FloppyFiles: []string{"floppy_config.go"}, + } + + errs := c.Prepare(nil) + if len(errs) != 0 { + t.Fatal("array with existing floppies should not fail") + } +} + +func TestNonExistingFloppyFile(t *testing.T) { + c := FloppyConfig{ + FloppyFiles: []string{"floppy_config.foo"}, + } + + errs := c.Prepare(nil) + if len(errs) == 0 { + t.Fatal("array with non existing floppies should return errors") + } +} + +func TestMultiErrorFloppyFiles(t *testing.T) { + c := FloppyConfig{ + FloppyFiles: []string{"floppy_config.foo", "floppy_config.go", "floppy_config.bar", "floppy_config_test.go", "floppy_config.baz"}, + } + + errs := c.Prepare(nil) + if len(errs) == 0 { + t.Fatal("array with non existing floppies should return errors") + } + + expectedErrors := 3 + if count := len(errs); count != expectedErrors { + t.Fatalf("array with %v non existing floppy should return %v errors but it is returning %v", expectedErrors, expectedErrors, count) + } +} diff --git a/common/test-fixtures/floppies/bar.bat b/common/test-fixtures/floppies/bar.bat new file mode 100644 index 000000000..0d6a4f32d --- /dev/null +++ b/common/test-fixtures/floppies/bar.bat @@ -0,0 +1 @@ +Echo I am a floppy with a batch file \ No newline at end of file diff --git a/common/test-fixtures/floppies/foo.ps1 b/common/test-fixtures/floppies/foo.ps1 new file mode 100644 index 000000000..672daeb89 --- /dev/null +++ b/common/test-fixtures/floppies/foo.ps1 @@ -0,0 +1 @@ +Write-Host "I am a floppy with some Powershell" \ No newline at end of file