github.phpd.cn/hashicorp/packer@v1.3.2/builder/triton/step_stop_machine_test.go (about) 1 package triton 2 3 import ( 4 "context" 5 "errors" 6 "testing" 7 8 "github.com/hashicorp/packer/helper/multistep" 9 ) 10 11 func TestStepStopMachine(t *testing.T) { 12 state := testState(t) 13 step := new(StepStopMachine) 14 defer step.Cleanup(state) 15 16 driver := state.Get("driver").(*DriverMock) 17 18 machineId := "test-machine-id" 19 state.Put("machine", machineId) 20 21 if action := step.Run(context.Background(), state); action != multistep.ActionContinue { 22 t.Fatalf("bad action: %#v", action) 23 } 24 25 step.Cleanup(state) 26 27 if driver.StopMachineId != machineId { 28 t.Fatalf("should've stopped machine (%s != %s)", driver.StopMachineId, machineId) 29 } 30 } 31 32 func TestStepStopMachine_StopMachineError(t *testing.T) { 33 state := testState(t) 34 step := new(StepStopMachine) 35 defer step.Cleanup(state) 36 37 driver := state.Get("driver").(*DriverMock) 38 39 machineId := "test-machine-id" 40 state.Put("machine", machineId) 41 42 driver.StopMachineErr = errors.New("error") 43 44 if action := step.Run(context.Background(), state); action != multistep.ActionHalt { 45 t.Fatalf("bad action: %#v", action) 46 } 47 48 if _, ok := state.GetOk("error"); !ok { 49 t.Fatalf("should have error") 50 } 51 } 52 53 func TestStepStopMachine_WaitForMachineStoppedError(t *testing.T) { 54 state := testState(t) 55 step := new(StepStopMachine) 56 defer step.Cleanup(state) 57 58 driver := state.Get("driver").(*DriverMock) 59 60 machineId := "test-machine-id" 61 state.Put("machine", machineId) 62 63 driver.WaitForMachineStateErr = errors.New("error") 64 65 if action := step.Run(context.Background(), state); action != multistep.ActionHalt { 66 t.Fatalf("bad action: %#v", action) 67 } 68 69 if _, ok := state.GetOk("error"); !ok { 70 t.Fatalf("should have error") 71 } 72 }