Commit 378a7320 authored by Mitchell Hashimoto's avatar Mitchell Hashimoto

builder/vmware: A lot more validation, testing

parent 057d656e
package vmware package vmware
import ( import (
"errors"
"fmt" "fmt"
"github.com/mitchellh/mapstructure" "github.com/mitchellh/mapstructure"
"github.com/mitchellh/multistep" "github.com/mitchellh/multistep"
...@@ -25,7 +26,7 @@ type config struct { ...@@ -25,7 +26,7 @@ type config struct {
HTTPDir string `mapstructure:"http_directory"` HTTPDir string `mapstructure:"http_directory"`
BootCommand []string `mapstructure:"boot_command"` BootCommand []string `mapstructure:"boot_command"`
BootWait time.Duration BootWait time.Duration
SSHUser string `mapstructure:"ssh_user"` SSHUser string `mapstructure:"ssh_username"`
SSHPassword string `mapstructure:"ssh_password"` SSHPassword string `mapstructure:"ssh_password"`
SSHWaitTimeout time.Duration SSHWaitTimeout time.Duration
...@@ -51,11 +52,21 @@ func (b *Builder) Prepare(raw interface{}) (err error) { ...@@ -51,11 +52,21 @@ func (b *Builder) Prepare(raw interface{}) (err error) {
b.config.OutputDir = "vmware" 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 != "" { if b.config.RawBootWait != "" {
b.config.BootWait, err = time.ParseDuration(b.config.RawBootWait) b.config.BootWait, err = time.ParseDuration(b.config.RawBootWait)
if err != nil { 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) { ...@@ -65,16 +76,16 @@ func (b *Builder) Prepare(raw interface{}) (err error) {
b.config.SSHWaitTimeout, err = time.ParseDuration(b.config.RawSSHWaitTimeout) b.config.SSHWaitTimeout, err = time.ParseDuration(b.config.RawSSHWaitTimeout)
if err != nil { 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() b.driver, err = b.newDriver()
if err != nil { 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 { if len(errs) > 0 {
return &packer.MultiError{errors} return &packer.MultiError{errs}
} }
return nil return nil
......
...@@ -8,6 +8,8 @@ import ( ...@@ -8,6 +8,8 @@ import (
func testConfig() map[string]interface{} { func testConfig() map[string]interface{} {
return map[string]interface{}{ return map[string]interface{}{
"iso_url": "foo",
"ssh_username": "foo",
} }
} }
...@@ -63,6 +65,41 @@ func TestBuilderPrepare_Defaults(t *testing.T) { ...@@ -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) { func TestBuilderPrepare_SSHWaitTimeout(t *testing.T) {
var b Builder var b Builder
config := testConfig() config := testConfig()
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment