diff --git a/builder/vmware/iso/builder.go b/builder/vmware/iso/builder.go index 351a8de51..361048c90 100644 --- a/builder/vmware/iso/builder.go +++ b/builder/vmware/iso/builder.go @@ -395,6 +395,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe &vmwcommon.StepSuppressMessages{}, &stepHTTPServer{}, &stepConfigureVNC{}, + &StepRegister{}, &stepRun{}, &stepTypeBootCommand{}, &common.StepConnectSSH{ diff --git a/builder/vmware/iso/remote_driver_mock.go b/builder/vmware/iso/remote_driver_mock.go new file mode 100644 index 000000000..fe494fd86 --- /dev/null +++ b/builder/vmware/iso/remote_driver_mock.go @@ -0,0 +1,40 @@ +package iso + +import ( + vmwcommon "github.com/mitchellh/packer/builder/vmware/common" +) + +type RemoteDriverMock struct { + vmwcommon.DriverMock + + UploadISOCalled bool + UploadISOPath string + UploadISOResult string + UploadISOErr error + + RegisterCalled bool + RegisterPath string + RegisterErr error + + UnregisterCalled bool + UnregisterPath string + UnregisterErr error +} + +func (d *RemoteDriverMock) UploadISO(path string) (string, error) { + d.UploadISOCalled = true + d.UploadISOPath = path + return d.UploadISOResult, d.UploadISOErr +} + +func (d *RemoteDriverMock) Register(path string) error { + d.RegisterCalled = true + d.RegisterPath = path + return d.RegisterErr +} + +func (d *RemoteDriverMock) Unregister(path string) error { + d.UnregisterCalled = true + d.UnregisterPath = path + return d.UnregisterErr +} diff --git a/builder/vmware/iso/remote_driver_mock_test.go b/builder/vmware/iso/remote_driver_mock_test.go new file mode 100644 index 000000000..86779a847 --- /dev/null +++ b/builder/vmware/iso/remote_driver_mock_test.go @@ -0,0 +1,12 @@ +package iso + +import ( + "testing" + + vmwcommon "github.com/mitchellh/packer/builder/vmware/common" +) + +func TestRemoteDriverMock_impl(t *testing.T) { + var _ vmwcommon.Driver = new(RemoteDriverMock) + var _ RemoteDriver = new(RemoteDriverMock) +} diff --git a/builder/vmware/iso/step_register.go b/builder/vmware/iso/step_register.go new file mode 100644 index 000000000..6710cbd74 --- /dev/null +++ b/builder/vmware/iso/step_register.go @@ -0,0 +1,52 @@ +package iso + +import ( + "fmt" + + "github.com/mitchellh/multistep" + vmwcommon "github.com/mitchellh/packer/builder/vmware/common" + "github.com/mitchellh/packer/packer" +) + +type StepRegister struct { + registeredPath string +} + +func (s *StepRegister) Run(state multistep.StateBag) multistep.StepAction { + driver := state.Get("driver").(vmwcommon.Driver) + ui := state.Get("ui").(packer.Ui) + vmxPath := state.Get("vmx_path").(string) + + if remoteDriver, ok := driver.(RemoteDriver); ok { + ui.Say("Registering remote VM...") + if err := remoteDriver.Register(vmxPath); err != nil { + err := fmt.Errorf("Error registering VM: %s", err) + state.Put("error", err) + ui.Error(err.Error()) + return multistep.ActionHalt + } + + s.registeredPath = vmxPath + } + + return multistep.ActionContinue +} + +func (s *StepRegister) Cleanup(state multistep.StateBag) { + if s.registeredPath == "" { + return + } + + driver := state.Get("driver").(vmwcommon.Driver) + ui := state.Get("ui").(packer.Ui) + + if remoteDriver, ok := driver.(RemoteDriver); ok { + ui.Say("Unregistering virtual machine...") + if err := remoteDriver.Unregister(s.registeredPath); err != nil { + ui.Error(fmt.Sprintf("Error unregistering VM: %s", err)) + } + + s.registeredPath = "" + } + +} diff --git a/builder/vmware/iso/step_register_test.go b/builder/vmware/iso/step_register_test.go new file mode 100644 index 000000000..c654f836d --- /dev/null +++ b/builder/vmware/iso/step_register_test.go @@ -0,0 +1,65 @@ +package iso + +import ( + "github.com/mitchellh/multistep" + "testing" +) + +func TestStepRegister_impl(t *testing.T) { + var _ multistep.Step = new(StepRegister) +} + +func TestStepRegister_regularDriver(t *testing.T) { + state := testState(t) + step := new(StepRegister) + + state.Put("vmx_path", "foo") + + // 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") + } + + // Cleanup + step.Cleanup(state) +} + +func TestStepRegister_remoteDriver(t *testing.T) { + state := testState(t) + step := new(StepRegister) + + driver := new(RemoteDriverMock) + state.Put("driver", driver) + state.Put("vmx_path", "foo") + + // 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") + } + + // verify + if !driver.RegisterCalled { + t.Fatal("register should be called") + } + if driver.RegisterPath != "foo" { + t.Fatal("should call with correct path") + } + if driver.UnregisterCalled { + t.Fatal("unregister should not be called") + } + + // cleanup + step.Cleanup(state) + if !driver.UnregisterCalled { + t.Fatal("unregister should be called") + } + if driver.UnregisterPath != "foo" { + t.Fatal("should unregister proper path") + } +} diff --git a/builder/vmware/iso/step_run.go b/builder/vmware/iso/step_run.go index 835ff06ab..6cbd6a171 100644 --- a/builder/vmware/iso/step_run.go +++ b/builder/vmware/iso/step_run.go @@ -21,8 +21,6 @@ import ( type stepRun struct { bootTime time.Time vmxPath string - - registered bool } func (s *stepRun) Run(state multistep.StateBag) multistep.StepAction { @@ -45,17 +43,6 @@ func (s *stepRun) Run(state multistep.StateBag) multistep.StepAction { "%s:%d", vncIp, vncPort)) } - if remoteDriver, ok := driver.(RemoteDriver); ok { - if err := remoteDriver.Register(vmxPath); err != nil { - err := fmt.Errorf("Error registering VM: %s", err) - state.Put("error", err) - ui.Error(err.Error()) - return multistep.ActionHalt - } - - s.registered = true - } - if err := driver.Start(vmxPath, config.Headless); err != nil { err := fmt.Errorf("Error starting VM: %s", err) state.Put("error", err) @@ -107,14 +94,5 @@ func (s *stepRun) Cleanup(state multistep.StateBag) { ui.Error(fmt.Sprintf("Error stopping VM: %s", err)) } } - - if remoteDriver, ok := driver.(RemoteDriver); ok && s.registered { - ui.Say("Unregistering virtual machine...") - if err := remoteDriver.Unregister(s.vmxPath); err != nil { - ui.Error(fmt.Sprintf("Error unregistering VM: %s", err)) - } - - s.registered = false - } } } diff --git a/builder/vmware/iso/step_test.go b/builder/vmware/iso/step_test.go index 04490cb29..aad099af5 100644 --- a/builder/vmware/iso/step_test.go +++ b/builder/vmware/iso/step_test.go @@ -3,12 +3,14 @@ package iso import ( "bytes" "github.com/mitchellh/multistep" + vmwcommon "github.com/mitchellh/packer/builder/vmware/common" "github.com/mitchellh/packer/packer" "testing" ) func testState(t *testing.T) multistep.StateBag { state := new(multistep.BasicStateBag) + state.Put("driver", new(vmwcommon.DriverMock)) state.Put("ui", &packer.BasicUi{ Reader: new(bytes.Buffer), Writer: new(bytes.Buffer),