2013-12-22 12:47:44 -05:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2018-01-22 19:03:49 -05:00
|
|
|
"context"
|
2013-12-22 12:47:44 -05:00
|
|
|
"testing"
|
|
|
|
"time"
|
2018-01-22 20:21:10 -05:00
|
|
|
|
2018-01-22 18:32:33 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2018-01-22 20:21:10 -05:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2013-12-22 12:47:44 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
2018-01-22 19:03:49 -05:00
|
|
|
if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
|
2013-12-22 12:47:44 -05:00
|
|
|
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
|
2018-01-22 19:03:49 -05:00
|
|
|
if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
|
2013-12-22 12:47:44 -05:00
|
|
|
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
|
2018-01-22 19:03:49 -05:00
|
|
|
if action := step.Run(context.Background(), state); action != multistep.ActionHalt {
|
2013-12-22 12:47:44 -05:00
|
|
|
t.Fatalf("bad action: %#v", action)
|
|
|
|
}
|
|
|
|
if _, ok := state.GetOk("error"); !ok {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
}
|
2016-10-02 13:08:32 -04:00
|
|
|
|
|
|
|
func TestStepShutdown_shutdownDelay(t *testing.T) {
|
|
|
|
state := testState(t)
|
|
|
|
step := new(StepShutdown)
|
|
|
|
step.Command = "poweroff"
|
|
|
|
step.Timeout = 5 * time.Second
|
|
|
|
step.Delay = 2 * time.Second
|
|
|
|
|
|
|
|
comm := new(packer.MockCommunicator)
|
|
|
|
state.Put("communicator", comm)
|
|
|
|
state.Put("vmName", "foo")
|
|
|
|
|
|
|
|
driver := state.Get("driver").(*DriverMock)
|
|
|
|
driver.IsRunningReturn = true
|
|
|
|
start := time.Now()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
driver.Lock()
|
|
|
|
defer driver.Unlock()
|
|
|
|
driver.IsRunningReturn = false
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Test the run
|
|
|
|
|
2018-01-22 19:03:49 -05:00
|
|
|
if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
|
2016-10-02 13:08:32 -04:00
|
|
|
t.Fatalf("bad action: %#v", action)
|
|
|
|
}
|
2018-10-17 05:19:57 -04:00
|
|
|
testDuration := time.Since(start)
|
2018-10-17 06:23:01 -04:00
|
|
|
if testDuration < 2500*time.Millisecond || testDuration > 2700*time.Millisecond {
|
2018-10-17 05:19:57 -04:00
|
|
|
t.Fatalf("incorrect duration %s", testDuration)
|
2016-10-02 13:08:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := state.GetOk("error"); ok {
|
|
|
|
t.Fatal("should NOT have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
step.Delay = 0
|
|
|
|
|
|
|
|
driver.IsRunningReturn = true
|
|
|
|
start = time.Now()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
time.Sleep(10 * time.Millisecond)
|
|
|
|
driver.Lock()
|
|
|
|
defer driver.Unlock()
|
|
|
|
driver.IsRunningReturn = false
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Test the run
|
2018-01-22 19:03:49 -05:00
|
|
|
if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
|
2016-10-02 13:08:32 -04:00
|
|
|
t.Fatalf("bad action: %#v", action)
|
|
|
|
}
|
2018-10-17 05:19:57 -04:00
|
|
|
testDuration = time.Since(start)
|
|
|
|
if testDuration > 600*time.Millisecond {
|
|
|
|
t.Fatalf("incorrect duration %s", testDuration)
|
2016-10-02 13:08:32 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := state.GetOk("error"); ok {
|
|
|
|
t.Fatal("should NOT have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|