From b65559d888e0ff0ab9d25575914ce57bfee2ee7d Mon Sep 17 00:00:00 2001 From: Mitchell Hashimoto Date: Sun, 22 Dec 2013 09:47:44 -0800 Subject: [PATCH] builder/virtualbox: StepShutdown tests --- builder/virtualbox/common/driver_mock.go | 7 ++ builder/virtualbox/common/step_shutdown.go | 2 +- .../virtualbox/common/step_shutdown_test.go | 105 ++++++++++++++++++ 3 files changed, 113 insertions(+), 1 deletion(-) create mode 100644 builder/virtualbox/common/step_shutdown_test.go diff --git a/builder/virtualbox/common/driver_mock.go b/builder/virtualbox/common/driver_mock.go index ad00ae248..91b587070 100644 --- a/builder/virtualbox/common/driver_mock.go +++ b/builder/virtualbox/common/driver_mock.go @@ -1,6 +1,10 @@ package common +import "sync" + type DriverMock struct { + sync.Mutex + CreateSATAControllerVM string CreateSATAControllerController string CreateSATAControllerErr error @@ -33,6 +37,9 @@ func (d *DriverMock) CreateSATAController(vm string, controller string) error { } func (d *DriverMock) IsRunning(name string) (bool, error) { + d.Lock() + defer d.Unlock() + d.IsRunningName = name return d.IsRunningReturn, d.IsRunningErr } diff --git a/builder/virtualbox/common/step_shutdown.go b/builder/virtualbox/common/step_shutdown.go index 3469dc7da..0f33692ce 100644 --- a/builder/virtualbox/common/step_shutdown.go +++ b/builder/virtualbox/common/step_shutdown.go @@ -58,7 +58,7 @@ func (s *StepShutdown) Run(state multistep.StateBag) multistep.StepAction { ui.Error(err.Error()) return multistep.ActionHalt default: - time.Sleep(1 * time.Second) + time.Sleep(500 * time.Millisecond) } } } else { diff --git a/builder/virtualbox/common/step_shutdown_test.go b/builder/virtualbox/common/step_shutdown_test.go new file mode 100644 index 000000000..215eefd30 --- /dev/null +++ b/builder/virtualbox/common/step_shutdown_test.go @@ -0,0 +1,105 @@ +package common + +import ( + "github.com/mitchellh/multistep" + "github.com/mitchellh/packer/packer" + "testing" + "time" +) + +func TestStepShutdown_impl(t *testing.T) { + var _ multistep.Step = new(StepShutdown) +} + +func TestStepShutdown_noShutdownCommand(t *testing.T) { + state := testState(t) + step := new(StepShutdown) + + comm := new(packer.MockCommunicator) + state.Put("communicator", comm) + 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 Stop was just called + if driver.StopName != "foo" { + t.Fatal("should call stop") + } + if comm.StartCalled { + t.Fatal("comm start should not be called") + } +} + +func TestStepShutdown_shutdownCommand(t *testing.T) { + state := testState(t) + step := new(StepShutdown) + step.Command = "poweroff" + step.Timeout = 1 * time.Second + + comm := new(packer.MockCommunicator) + state.Put("communicator", comm) + state.Put("vmName", "foo") + + driver := state.Get("driver").(*DriverMock) + driver.IsRunningReturn = true + + go func() { + time.Sleep(10 * time.Millisecond) + driver.Lock() + defer driver.Unlock() + driver.IsRunningReturn = false + }() + + // 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 Stop was just called + if driver.StopName != "" { + t.Fatal("should not call stop") + } + if comm.StartCmd.Command != step.Command { + t.Fatal("comm start should be called") + } +} + +func TestStepShutdown_shutdownTimeout(t *testing.T) { + state := testState(t) + step := new(StepShutdown) + step.Command = "poweroff" + step.Timeout = 1 * time.Second + + comm := new(packer.MockCommunicator) + state.Put("communicator", comm) + state.Put("vmName", "foo") + + driver := state.Get("driver").(*DriverMock) + driver.IsRunningReturn = true + + go func() { + time.Sleep(2 * time.Second) + driver.Lock() + defer driver.Unlock() + driver.IsRunningReturn = false + }() + + // Test the run + if action := step.Run(state); action != multistep.ActionHalt { + t.Fatalf("bad action: %#v", action) + } + if _, ok := state.GetOk("error"); !ok { + t.Fatal("should have error") + } +}