diff --git a/builder/virtualbox/common/driver_mock.go b/builder/virtualbox/common/driver_mock.go index 2a0017120..ad00ae248 100644 --- a/builder/virtualbox/common/driver_mock.go +++ b/builder/virtualbox/common/driver_mock.go @@ -15,9 +15,8 @@ type DriverMock struct { SuppressMessagesCalled bool SuppressMessagesErr error - VBoxManageCalled bool - VBoxManageArgs []string - VBoxManageErr error + VBoxManageCalls [][]string + VBoxManageErrs []error VerifyCalled bool VerifyErr error @@ -49,9 +48,12 @@ func (d *DriverMock) SuppressMessages() error { } func (d *DriverMock) VBoxManage(args ...string) error { - d.VBoxManageCalled = true - d.VBoxManageArgs = args - return d.VBoxManageErr + d.VBoxManageCalls = append(d.VBoxManageCalls, args) + + if len(d.VBoxManageErrs) >= len(d.VBoxManageCalls) { + return d.VBoxManageErrs[len(d.VBoxManageCalls)-1] + } + return nil } func (d *DriverMock) Verify() error { diff --git a/builder/virtualbox/iso/step_attach_floppy.go b/builder/virtualbox/common/step_attach_floppy.go similarity index 86% rename from builder/virtualbox/iso/step_attach_floppy.go rename to builder/virtualbox/common/step_attach_floppy.go index e784a0fc0..5ebcf9b5f 100644 --- a/builder/virtualbox/iso/step_attach_floppy.go +++ b/builder/virtualbox/common/step_attach_floppy.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" "io" "io/ioutil" @@ -15,13 +14,16 @@ import ( // This step attaches the ISO to the virtual machine. // // Uses: +// driver Driver +// ui packer.Ui +// vmName string // // Produces: -type stepAttachFloppy struct { +type StepAttachFloppy struct { floppyPath string } -func (s *stepAttachFloppy) Run(state multistep.StateBag) multistep.StepAction { +func (s *StepAttachFloppy) Run(state multistep.StateBag) multistep.StepAction { // Determine if we even have a floppy disk to attach var floppyPath string if floppyPathRaw, ok := state.GetOk("floppy_path"); ok { @@ -40,7 +42,7 @@ func (s *stepAttachFloppy) Run(state multistep.StateBag) multistep.StepAction { return multistep.ActionHalt } - driver := state.Get("driver").(vboxcommon.Driver) + driver := state.Get("driver").(Driver) ui := state.Get("ui").(packer.Ui) vmName := state.Get("vmName").(string) @@ -77,7 +79,7 @@ func (s *stepAttachFloppy) Run(state multistep.StateBag) multistep.StepAction { return multistep.ActionContinue } -func (s *stepAttachFloppy) Cleanup(state multistep.StateBag) { +func (s *StepAttachFloppy) Cleanup(state multistep.StateBag) { if s.floppyPath == "" { return } @@ -85,7 +87,7 @@ func (s *stepAttachFloppy) Cleanup(state multistep.StateBag) { // Delete the floppy disk defer os.Remove(s.floppyPath) - driver := state.Get("driver").(vboxcommon.Driver) + driver := state.Get("driver").(Driver) vmName := state.Get("vmName").(string) command := []string{ @@ -101,7 +103,7 @@ func (s *stepAttachFloppy) Cleanup(state multistep.StateBag) { } } -func (s *stepAttachFloppy) copyFloppy(path string) (string, error) { +func (s *StepAttachFloppy) copyFloppy(path string) (string, error) { tempdir, err := ioutil.TempDir("", "packer") if err != nil { return "", err diff --git a/builder/virtualbox/common/step_attach_floppy_test.go b/builder/virtualbox/common/step_attach_floppy_test.go new file mode 100644 index 000000000..110ddfe74 --- /dev/null +++ b/builder/virtualbox/common/step_attach_floppy_test.go @@ -0,0 +1,73 @@ +package common + +import ( + "github.com/mitchellh/multistep" + "io/ioutil" + "os" + "testing" +) + +func TestStepAttachFloppy_impl(t *testing.T) { + var _ multistep.Step = new(StepAttachFloppy) +} + +func TestStepAttachFloppy(t *testing.T) { + state := testState(t) + step := new(StepAttachFloppy) + + // Create a temporary file for our floppy file + tf, err := ioutil.TempFile("", "packer") + if err != nil { + t.Fatalf("err: %s", err) + } + tf.Close() + defer os.Remove(tf.Name()) + + state.Put("floppy_path", tf.Name()) + 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") + } + + if len(driver.VBoxManageCalls) != 2 { + t.Fatal("not enough calls to VBoxManage") + } + if driver.VBoxManageCalls[0][0] != "storagectl" { + t.Fatal("bad call") + } + if driver.VBoxManageCalls[1][0] != "storageattach" { + t.Fatal("bad call") + } + + // Test the cleanup + step.Cleanup(state) + if driver.VBoxManageCalls[2][0] != "storageattach" { + t.Fatal("bad call") + } +} + +func TestStepAttachFloppy_noFloppy(t *testing.T) { + state := testState(t) + step := new(StepAttachFloppy) + + 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") + } + + if len(driver.VBoxManageCalls) > 0 { + t.Fatal("should not call vboxmanage") + } +} diff --git a/builder/virtualbox/iso/builder.go b/builder/virtualbox/iso/builder.go index f4948cb04..353cfdcb1 100644 --- a/builder/virtualbox/iso/builder.go +++ b/builder/virtualbox/iso/builder.go @@ -381,7 +381,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe new(stepCreateDisk), new(stepAttachISO), new(stepAttachGuestAdditions), - new(stepAttachFloppy), + new(vboxcommon.StepAttachFloppy), new(stepForwardSSH), new(stepVBoxManage), new(stepRun), diff --git a/builder/virtualbox/ovf/builder.go b/builder/virtualbox/ovf/builder.go index 1d7c088d9..831fb3214 100644 --- a/builder/virtualbox/ovf/builder.go +++ b/builder/virtualbox/ovf/builder.go @@ -52,7 +52,9 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe }, /* new(stepAttachGuestAdditions), - new(stepAttachFloppy), + */ + new(vboxcommon.StepAttachFloppy), + /* new(stepForwardSSH), new(stepVBoxManage), new(stepRun),