From 378a7320a31f2b5f8b147c90d43c61f7a62a5063 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Thu, 6 Jun 2013 14:46:48 -0700 Subject: [PATCH] builder/vmware: A lot more validation, testing --- builder/vmware/builder.go | 25 ++++++++++++++++------- builder/vmware/builder_test.go | 37 ++++++++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 7 deletions(-) diff --git a/builder/vmware/builder.go b/builder/vmware/builder.go index fb8933d1a..d95f1eb33 100644 --- a/builder/vmware/builder.go +++ b/builder/vmware/builder.go @@ -1,6 +1,7 @@ package vmware import ( + "errors" "fmt" "github.com/mitchellh/mapstructure" "github.com/mitchellh/multistep" @@ -25,7 +26,7 @@ type config struct { HTTPDir string `mapstructure:"http_directory"` BootCommand []string `mapstructure:"boot_command"` BootWait time.Duration - SSHUser string `mapstructure:"ssh_user"` + SSHUser string `mapstructure:"ssh_username"` SSHPassword string `mapstructure:"ssh_password"` SSHWaitTimeout time.Duration @@ -51,11 +52,21 @@ func (b *Builder) Prepare(raw interface{}) (err error) { b.config.OutputDir = "vmware" } - errors := make([]error, 0) + // Accumulate any errors + errs := make([]error, 0) + + if b.config.ISOUrl == "" { + errs = append(errs, errors.New("An iso_url must be specified.")) + } + + if b.config.SSHUser == "" { + errs = append(errs, errors.New("An ssh_username must be specified.")) + } + if b.config.RawBootWait != "" { b.config.BootWait, err = time.ParseDuration(b.config.RawBootWait) if err != nil { - errors = append(errors, fmt.Errorf("Failed parsing boot_wait: %s", err)) + errs = append(errs, fmt.Errorf("Failed parsing boot_wait: %s", err)) } } @@ -65,16 +76,16 @@ func (b *Builder) Prepare(raw interface{}) (err error) { b.config.SSHWaitTimeout, err = time.ParseDuration(b.config.RawSSHWaitTimeout) if err != nil { - errors = append(errors, fmt.Errorf("Failed parsing ssh_wait_timeout: %s", err)) + errs = append(errs, fmt.Errorf("Failed parsing ssh_wait_timeout: %s", err)) } b.driver, err = b.newDriver() if err != nil { - errors = append(errors, fmt.Errorf("Failed creating VMware driver: %s", err)) + errs = append(errs, fmt.Errorf("Failed creating VMware driver: %s", err)) } - if len(errors) > 0 { - return &packer.MultiError{errors} + if len(errs) > 0 { + return &packer.MultiError{errs} } return nil diff --git a/builder/vmware/builder_test.go b/builder/vmware/builder_test.go index 052c15d8f..90a522001 100644 --- a/builder/vmware/builder_test.go +++ b/builder/vmware/builder_test.go @@ -8,6 +8,8 @@ import ( func testConfig() map[string]interface{} { return map[string]interface{}{ + "iso_url": "foo", + "ssh_username": "foo", } } @@ -63,6 +65,41 @@ func TestBuilderPrepare_Defaults(t *testing.T) { } } +func TestBuilderPrepare_ISOUrl(t *testing.T) { + var b Builder + config := testConfig() + + // Test iso_url + config["iso_url"] = "" + err := b.Prepare(config) + if err == nil { + t.Fatal("should have error") + } + + config["iso_url"] = "exists" + err = b.Prepare(config) + if err != nil { + t.Fatalf("should not have error: %s", err) + } +} + +func TestBuilderPrepare_SSHUser(t *testing.T) { + var b Builder + config := testConfig() + + config["ssh_username"] = "" + err := b.Prepare(config) + if err == nil { + t.Fatal("should have error") + } + + config["ssh_username"] = "exists" + 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()