github.phpd.cn/hashicorp/packer@v1.3.2/builder/oracle/oci/step_create_instance_test.go (about) 1 package oci 2 3 import ( 4 "context" 5 "errors" 6 "testing" 7 8 "github.com/hashicorp/packer/helper/multistep" 9 ) 10 11 func TestStepCreateInstance(t *testing.T) { 12 state := testState() 13 state.Put("publicKey", "key") 14 15 step := new(stepCreateInstance) 16 defer step.Cleanup(state) 17 18 driver := state.Get("driver").(*driverMock) 19 20 if action := step.Run(context.Background(), state); action != multistep.ActionContinue { 21 t.Fatalf("bad action: %#v", action) 22 } 23 24 instanceIDRaw, ok := state.GetOk("instance_id") 25 if !ok { 26 t.Fatalf("should have machine") 27 } 28 29 step.Cleanup(state) 30 31 if driver.TerminateInstanceID != instanceIDRaw.(string) { 32 t.Fatalf( 33 "should've deleted instance (%s != %s)", 34 driver.TerminateInstanceID, instanceIDRaw.(string)) 35 } 36 } 37 38 func TestStepCreateInstance_CreateInstanceErr(t *testing.T) { 39 state := testState() 40 state.Put("publicKey", "key") 41 42 step := new(stepCreateInstance) 43 defer step.Cleanup(state) 44 45 driver := state.Get("driver").(*driverMock) 46 driver.CreateInstanceErr = errors.New("error") 47 48 if action := step.Run(context.Background(), state); action != multistep.ActionHalt { 49 t.Fatalf("bad action: %#v", action) 50 } 51 52 if _, ok := state.GetOk("error"); !ok { 53 t.Fatalf("should have error") 54 } 55 56 if _, ok := state.GetOk("instance_id"); ok { 57 t.Fatalf("should NOT have instance_id") 58 } 59 60 step.Cleanup(state) 61 62 if driver.TerminateInstanceID != "" { 63 t.Fatalf("Should not have tried to terminate an instance") 64 } 65 } 66 67 func TestStepCreateInstance_WaitForInstanceStateErr(t *testing.T) { 68 state := testState() 69 state.Put("publicKey", "key") 70 71 step := new(stepCreateInstance) 72 defer step.Cleanup(state) 73 74 driver := state.Get("driver").(*driverMock) 75 driver.WaitForInstanceStateErr = errors.New("error") 76 77 if action := step.Run(context.Background(), state); action != multistep.ActionHalt { 78 t.Fatalf("bad action: %#v", action) 79 } 80 81 if _, ok := state.GetOk("error"); !ok { 82 t.Fatalf("should have error") 83 } 84 } 85 86 func TestStepCreateInstance_TerminateInstanceErr(t *testing.T) { 87 state := testState() 88 state.Put("publicKey", "key") 89 90 step := new(stepCreateInstance) 91 defer step.Cleanup(state) 92 93 driver := state.Get("driver").(*driverMock) 94 95 if action := step.Run(context.Background(), state); action != multistep.ActionContinue { 96 t.Fatalf("bad action: %#v", action) 97 } 98 99 _, ok := state.GetOk("instance_id") 100 if !ok { 101 t.Fatalf("should have machine") 102 } 103 104 driver.TerminateInstanceErr = errors.New("error") 105 step.Cleanup(state) 106 107 if _, ok := state.GetOk("error"); !ok { 108 t.Fatalf("should have error") 109 } 110 } 111 112 func TestStepCreateInstanceCleanup_WaitForInstanceStateErr(t *testing.T) { 113 state := testState() 114 state.Put("publicKey", "key") 115 116 step := new(stepCreateInstance) 117 defer step.Cleanup(state) 118 119 driver := state.Get("driver").(*driverMock) 120 121 if action := step.Run(context.Background(), state); action != multistep.ActionContinue { 122 t.Fatalf("bad action: %#v", action) 123 } 124 125 driver.WaitForInstanceStateErr = errors.New("error") 126 step.Cleanup(state) 127 128 if _, ok := state.GetOk("error"); !ok { 129 t.Fatalf("should have error") 130 } 131 }