From fbd20dffcbd1e34db5901806e7bfce1d6517e2d0 Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 22 Dec 2013 09:54:00 -0800 Subject: [PATCH] builder/virtualbox/common: StepRemoveDevices --- .../{iso => common}/step_remove_devices.go | 14 ++-- .../common/step_remove_devices_test.go | 64 +++++++++++++++++++ builder/virtualbox/iso/builder.go | 2 +- builder/virtualbox/ovf/builder.go | 2 +- 4 files changed, 74 insertions(+), 8 deletions(-) rename builder/virtualbox/{iso => common}/step_remove_devices.go (80%) create mode 100644 builder/virtualbox/common/step_remove_devices_test.go diff --git a/builder/virtualbox/iso/step_remove_devices.go b/builder/virtualbox/common/step_remove_devices.go similarity index 80% rename from builder/virtualbox/iso/step_remove_devices.go rename to builder/virtualbox/common/step_remove_devices.go index 763e22670..176de50a4 100644 --- a/builder/virtualbox/iso/step_remove_devices.go +++ b/builder/virtualbox/common/step_remove_devices.go @@ -1,9 +1,8 @@ -package iso +package common import ( "fmt" "github.com/mitchellh/multistep" - vboxcommon "github.com/mitchellh/packer/builder/virtualbox/common" "github.com/mitchellh/packer/packer" ) @@ -11,12 +10,15 @@ import ( // machine that we may have added. // // Uses: +// driver Driver +// ui packer.Ui +// vmName string // // Produces: -type stepRemoveDevices struct{} +type StepRemoveDevices struct{} -func (s *stepRemoveDevices) Run(state multistep.StateBag) multistep.StepAction { - driver := state.Get("driver").(vboxcommon.Driver) +func (s *StepRemoveDevices) Run(state multistep.StateBag) multistep.StepAction { + driver := state.Get("driver").(Driver) ui := state.Get("ui").(packer.Ui) vmName := state.Get("vmName").(string) @@ -56,5 +58,5 @@ func (s *stepRemoveDevices) Run(state multistep.StateBag) multistep.StepAction { return multistep.ActionContinue } -func (s *stepRemoveDevices) Cleanup(state multistep.StateBag) { +func (s *StepRemoveDevices) Cleanup(state multistep.StateBag) { } diff --git a/builder/virtualbox/common/step_remove_devices_test.go b/builder/virtualbox/common/step_remove_devices_test.go new file mode 100644 index 000000000..af1bccc1f --- /dev/null +++ b/builder/virtualbox/common/step_remove_devices_test.go @@ -0,0 +1,64 @@ +package common + +import ( + "github.com/mitchellh/multistep" + "testing" +) + +func TestStepRemoveDevices_impl(t *testing.T) { + var _ multistep.Step = new(StepRemoveDevices) +} + +func TestStepRemoveDevices(t *testing.T) { + state := testState(t) + step := new(StepRemoveDevices) + + state.Put("vmName", "foo") + + driver := state.Get("driver").(*DriverMock) + + // Test the run + if action := step.Run(state); action != multistep.ActionContinue { + t.Fatalf("bad action: %#v", action) + } + if _, ok := state.GetOk("error"); ok { + t.Fatal("should NOT have error") + } + + // Test that ISO was removed + if len(driver.VBoxManageCalls) != 1 { + t.Fatalf("bad: %#v", driver.VBoxManageCalls) + } + if driver.VBoxManageCalls[0][3] != "IDE Controller" { + t.Fatalf("bad: %#v", driver.VBoxManageCalls) + } +} + +func TestStepRemoveDevices_floppyPath(t *testing.T) { + state := testState(t) + step := new(StepRemoveDevices) + + state.Put("floppy_path", "foo") + state.Put("vmName", "foo") + + driver := state.Get("driver").(*DriverMock) + + // Test the run + if action := step.Run(state); action != multistep.ActionContinue { + t.Fatalf("bad action: %#v", action) + } + if _, ok := state.GetOk("error"); ok { + t.Fatal("should NOT have error") + } + + // Test that both were removed + if len(driver.VBoxManageCalls) != 2 { + t.Fatalf("bad: %#v", driver.VBoxManageCalls) + } + if driver.VBoxManageCalls[0][3] != "Floppy Controller" { + t.Fatalf("bad: %#v", driver.VBoxManageCalls) + } + if driver.VBoxManageCalls[1][3] != "IDE Controller" { + t.Fatalf("bad: %#v", driver.VBoxManageCalls) + } +} diff --git a/builder/virtualbox/iso/builder.go b/builder/virtualbox/iso/builder.go index 25c4aee05..ab5ae993d 100644 --- a/builder/virtualbox/iso/builder.go +++ b/builder/virtualbox/iso/builder.go @@ -326,7 +326,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe Command: b.config.ShutdownCommand, Timeout: b.config.ShutdownTimeout, }, - new(stepRemoveDevices), + new(vboxcommon.StepRemoveDevices), new(stepExport), } diff --git a/builder/virtualbox/ovf/builder.go b/builder/virtualbox/ovf/builder.go index 765da1c94..4cd7d2e52 100644 --- a/builder/virtualbox/ovf/builder.go +++ b/builder/virtualbox/ovf/builder.go @@ -79,8 +79,8 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe Command: b.config.ShutdownCommand, Timeout: b.config.ShutdownTimeout, }, + new(vboxcommon.StepRemoveDevices), /* - new(stepRemoveDevices), new(stepExport), */ }