github.com/amanya/packer@v0.12.1-0.20161117214323-902ac5ab2eb6/builder/virtualbox/common/step_shutdown_test.go (about) 1 package common 2 3 import ( 4 "github.com/mitchellh/multistep" 5 "github.com/mitchellh/packer/packer" 6 "testing" 7 "time" 8 ) 9 10 func TestStepShutdown_impl(t *testing.T) { 11 var _ multistep.Step = new(StepShutdown) 12 } 13 14 func TestStepShutdown_noShutdownCommand(t *testing.T) { 15 state := testState(t) 16 step := new(StepShutdown) 17 18 comm := new(packer.MockCommunicator) 19 state.Put("communicator", comm) 20 state.Put("vmName", "foo") 21 22 driver := state.Get("driver").(*DriverMock) 23 24 // Test the run 25 if action := step.Run(state); action != multistep.ActionContinue { 26 t.Fatalf("bad action: %#v", action) 27 } 28 if _, ok := state.GetOk("error"); ok { 29 t.Fatal("should NOT have error") 30 } 31 32 // Test that Stop was just called 33 if driver.StopName != "foo" { 34 t.Fatal("should call stop") 35 } 36 if comm.StartCalled { 37 t.Fatal("comm start should not be called") 38 } 39 } 40 41 func TestStepShutdown_shutdownCommand(t *testing.T) { 42 state := testState(t) 43 step := new(StepShutdown) 44 step.Command = "poweroff" 45 step.Timeout = 1 * time.Second 46 47 comm := new(packer.MockCommunicator) 48 state.Put("communicator", comm) 49 state.Put("vmName", "foo") 50 51 driver := state.Get("driver").(*DriverMock) 52 driver.IsRunningReturn = true 53 54 go func() { 55 time.Sleep(10 * time.Millisecond) 56 driver.Lock() 57 defer driver.Unlock() 58 driver.IsRunningReturn = false 59 }() 60 61 // Test the run 62 if action := step.Run(state); action != multistep.ActionContinue { 63 t.Fatalf("bad action: %#v", action) 64 } 65 if _, ok := state.GetOk("error"); ok { 66 t.Fatal("should NOT have error") 67 } 68 69 // Test that Stop was just called 70 if driver.StopName != "" { 71 t.Fatal("should not call stop") 72 } 73 if comm.StartCmd.Command != step.Command { 74 t.Fatal("comm start should be called") 75 } 76 } 77 78 func TestStepShutdown_shutdownTimeout(t *testing.T) { 79 state := testState(t) 80 step := new(StepShutdown) 81 step.Command = "poweroff" 82 step.Timeout = 1 * time.Second 83 84 comm := new(packer.MockCommunicator) 85 state.Put("communicator", comm) 86 state.Put("vmName", "foo") 87 88 driver := state.Get("driver").(*DriverMock) 89 driver.IsRunningReturn = true 90 91 go func() { 92 time.Sleep(2 * time.Second) 93 driver.Lock() 94 defer driver.Unlock() 95 driver.IsRunningReturn = false 96 }() 97 98 // Test the run 99 if action := step.Run(state); action != multistep.ActionHalt { 100 t.Fatalf("bad action: %#v", action) 101 } 102 if _, ok := state.GetOk("error"); !ok { 103 t.Fatal("should have error") 104 } 105 } 106 107 func TestStepShutdown_shutdownDelay(t *testing.T) { 108 state := testState(t) 109 step := new(StepShutdown) 110 step.Command = "poweroff" 111 step.Timeout = 5 * time.Second 112 step.Delay = 2 * time.Second 113 114 comm := new(packer.MockCommunicator) 115 state.Put("communicator", comm) 116 state.Put("vmName", "foo") 117 118 driver := state.Get("driver").(*DriverMock) 119 driver.IsRunningReturn = true 120 start := time.Now() 121 122 go func() { 123 time.Sleep(10 * time.Millisecond) 124 driver.Lock() 125 defer driver.Unlock() 126 driver.IsRunningReturn = false 127 }() 128 129 // Test the run 130 131 if action := step.Run(state); action != multistep.ActionContinue { 132 t.Fatalf("bad action: %#v", action) 133 } 134 testDuration := time.Since(start).Seconds() 135 if testDuration < 2.5 || testDuration > 2.6 { 136 t.Fatal("incorrect duration") 137 } 138 139 if _, ok := state.GetOk("error"); ok { 140 t.Fatal("should NOT have error") 141 } 142 143 step.Delay = 0 144 145 driver.IsRunningReturn = true 146 start = time.Now() 147 148 go func() { 149 time.Sleep(10 * time.Millisecond) 150 driver.Lock() 151 defer driver.Unlock() 152 driver.IsRunningReturn = false 153 }() 154 155 // Test the run 156 if action := step.Run(state); action != multistep.ActionContinue { 157 t.Fatalf("bad action: %#v", action) 158 } 159 testDuration = time.Since(start).Seconds() 160 if testDuration > 0.6 { 161 t.Fatal("incorrect duration") 162 } 163 164 if _, ok := state.GetOk("error"); ok { 165 t.Fatal("should NOT have error") 166 } 167 168 }