diff --git a/builder/hyperv/iso/builder_test.go b/builder/hyperv/iso/builder_test.go index 1c655f475..d32ba4f41 100644 --- a/builder/hyperv/iso/builder_test.go +++ b/builder/hyperv/iso/builder_test.go @@ -6,6 +6,7 @@ import ( "testing" "github.com/hashicorp/packer/packer" + "fmt" ) func testConfig() map[string]interface{} { @@ -298,3 +299,110 @@ func TestBuilderPrepare_ISOUrl(t *testing.T) { t.Fatalf("bad: %#v", b.config.ISOUrls) } } + +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{"nonexistent.bat", "nonexistent.ps1"} + b = Builder{} + _, errs := b.Prepare(config) + if errs == nil { + t.Fatalf("Nonexistent floppies should trigger multierror") + } + + if len(errs.(*packer.MultiError).Errors) != 2 { + t.Fatalf("Multierror should work and report 2 errors") + } +} + +func TestBuilderPrepare_CommConfig(t *testing.T) { + // Test Winrm + { + config := testConfig() + config["communicator"] = "winrm" + config["winrm_username"] = "username" + config["winrm_password"] = "password" + config["winrm_host"] = "1.2.3.4" + + var 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) + } + + if b.config.Comm.WinRMUser != "username" { + t.Errorf("bad winrm_username: %s", b.config.Comm.WinRMUser) + } + if b.config.Comm.WinRMPassword != "password" { + t.Errorf("bad winrm_password: %s", b.config.Comm.WinRMPassword) + } + if host := b.config.Comm.Host(); host != "1.2.3.4" { + t.Errorf("bad host: %s", host) + } + } + + // Test SSH + { + config := testConfig() + config["communicator"] = "ssh" + config["ssh_username"] = "username" + config["ssh_password"] = "password" + config["ssh_host"] = "1.2.3.4" + + var 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) + } + + if b.config.Comm.SSHUsername != "username" { + t.Errorf("bad ssh_username: %s", b.config.Comm.SSHUsername) + } + if b.config.Comm.SSHPassword != "password" { + t.Errorf("bad ssh_password: %s", b.config.Comm.SSHPassword) + } + if host := b.config.Comm.Host(); host != "1.2.3.4" { + t.Errorf("bad host: %s", host) + } + } +} diff --git a/builder/hyperv/vmcx/builder.go b/builder/hyperv/vmcx/builder.go index b75667742..3236fa945 100644 --- a/builder/hyperv/vmcx/builder.go +++ b/builder/hyperv/vmcx/builder.go @@ -42,7 +42,7 @@ type Config struct { common.PackerConfig `mapstructure:",squash"` common.HTTPConfig `mapstructure:",squash"` common.ISOConfig `mapstructure:",squash"` - hypervcommon.FloppyConfig `mapstructure:",squash"` + common.FloppyConfig `mapstructure:",squash"` hypervcommon.OutputConfig `mapstructure:",squash"` hypervcommon.SSHConfig `mapstructure:",squash"` hypervcommon.RunConfig `mapstructure:",squash"` diff --git a/builder/hyperv/vmcx/builder_test.go b/builder/hyperv/vmcx/builder_test.go index f0b7736bd..3dd5cc4ab 100644 --- a/builder/hyperv/vmcx/builder_test.go +++ b/builder/hyperv/vmcx/builder_test.go @@ -7,6 +7,7 @@ import ( "github.com/hashicorp/packer/packer" "io/ioutil" "os" + "fmt" ) func testConfig() map[string]interface{} { @@ -259,3 +260,145 @@ func TestBuilderPrepare_ISOUrl(t *testing.T) { t.Fatalf("bad: %#v", b.config.ISOUrls) } } + +func TestBuilderPrepare_FloppyFiles(t *testing.T) { + var b Builder + config := testConfig() + + //Create vmxc folder + td, err := ioutil.TempDir("", "packer") + if err != nil { + t.Fatalf("err: %s", err) + } + defer os.RemoveAll(td) + config["clone_from_vmxc_path"] = td + + 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() + + //Create vmxc folder + td, err := ioutil.TempDir("", "packer") + if err != nil { + t.Fatalf("err: %s", err) + } + defer os.RemoveAll(td) + config["clone_from_vmxc_path"] = td + + config["floppy_files"] = []string{"nonexistent.bat", "nonexistent.ps1"} + b = Builder{} + _, errs := b.Prepare(config) + if errs == nil { + t.Fatalf("Nonexistent floppies should trigger multierror") + } + + if len(errs.(*packer.MultiError).Errors) != 2 { + t.Fatalf("Multierror should work and report 2 errors") + } +} + +func TestBuilderPrepare_CommConfig(t *testing.T) { + // Test Winrm + { + config := testConfig() + + //Create vmxc folder + td, err := ioutil.TempDir("", "packer") + if err != nil { + t.Fatalf("err: %s", err) + } + defer os.RemoveAll(td) + config["clone_from_vmxc_path"] = td + + config["communicator"] = "winrm" + config["winrm_username"] = "username" + config["winrm_password"] = "password" + config["winrm_host"] = "1.2.3.4" + + var 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) + } + + if b.config.Comm.WinRMUser != "username" { + t.Errorf("bad winrm_username: %s", b.config.Comm.WinRMUser) + } + if b.config.Comm.WinRMPassword != "password" { + t.Errorf("bad winrm_password: %s", b.config.Comm.WinRMPassword) + } + if host := b.config.Comm.Host(); host != "1.2.3.4" { + t.Errorf("bad host: %s", host) + } + } + + // Test SSH + { + config := testConfig() + + //Create vmxc folder + td, err := ioutil.TempDir("", "packer") + if err != nil { + t.Fatalf("err: %s", err) + } + defer os.RemoveAll(td) + config["clone_from_vmxc_path"] = td + + config["communicator"] = "ssh" + config["ssh_username"] = "username" + config["ssh_password"] = "password" + config["ssh_host"] = "1.2.3.4" + + var 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) + } + + if b.config.Comm.SSHUsername != "username" { + t.Errorf("bad ssh_username: %s", b.config.Comm.SSHUsername) + } + if b.config.Comm.SSHPassword != "password" { + t.Errorf("bad ssh_password: %s", b.config.Comm.SSHPassword) + } + if host := b.config.Comm.Host(); host != "1.2.3.4" { + t.Errorf("bad host: %s", host) + } + } +}