github.com/kaixiang/packer@v0.5.2-0.20140114230416-1f5786b0d7f1/builder/googlecompute/step_create_instance_test.go (about) 1 package googlecompute 2 3 import ( 4 "errors" 5 "github.com/mitchellh/multistep" 6 "testing" 7 "time" 8 ) 9 10 func TestStepCreateInstance_impl(t *testing.T) { 11 var _ multistep.Step = new(StepCreateInstance) 12 } 13 14 func TestStepCreateInstance(t *testing.T) { 15 state := testState(t) 16 step := new(StepCreateInstance) 17 defer step.Cleanup(state) 18 19 state.Put("ssh_public_key", "key") 20 21 config := state.Get("config").(*Config) 22 driver := state.Get("driver").(*DriverMock) 23 24 // run the step 25 if action := step.Run(state); action != multistep.ActionContinue { 26 t.Fatalf("bad action: %#v", action) 27 } 28 29 // Verify state 30 nameRaw, ok := state.GetOk("instance_name") 31 if !ok { 32 t.Fatal("should have instance name") 33 } 34 35 // cleanup 36 step.Cleanup(state) 37 38 if driver.DeleteInstanceName != nameRaw.(string) { 39 t.Fatal("should've deleted instance") 40 } 41 if driver.DeleteInstanceZone != config.Zone { 42 t.Fatal("bad zone: %#v", driver.DeleteInstanceZone) 43 } 44 } 45 46 func TestStepCreateInstance_error(t *testing.T) { 47 state := testState(t) 48 step := new(StepCreateInstance) 49 defer step.Cleanup(state) 50 51 state.Put("ssh_public_key", "key") 52 53 driver := state.Get("driver").(*DriverMock) 54 driver.RunInstanceErr = errors.New("error") 55 56 // run the step 57 if action := step.Run(state); action != multistep.ActionHalt { 58 t.Fatalf("bad action: %#v", action) 59 } 60 61 // Verify state 62 if _, ok := state.GetOk("error"); !ok { 63 t.Fatal("should have error") 64 } 65 if _, ok := state.GetOk("instance_name"); ok { 66 t.Fatal("should NOT have instance name") 67 } 68 } 69 70 func TestStepCreateInstance_errorOnChannel(t *testing.T) { 71 state := testState(t) 72 step := new(StepCreateInstance) 73 defer step.Cleanup(state) 74 75 errCh := make(chan error, 1) 76 errCh <- errors.New("error") 77 78 state.Put("ssh_public_key", "key") 79 80 driver := state.Get("driver").(*DriverMock) 81 driver.RunInstanceErrCh = errCh 82 83 // run the step 84 if action := step.Run(state); action != multistep.ActionHalt { 85 t.Fatalf("bad action: %#v", action) 86 } 87 88 // Verify state 89 if _, ok := state.GetOk("error"); !ok { 90 t.Fatal("should have error") 91 } 92 if _, ok := state.GetOk("instance_name"); ok { 93 t.Fatal("should NOT have instance name") 94 } 95 } 96 97 func TestStepCreateInstance_errorTimeout(t *testing.T) { 98 state := testState(t) 99 step := new(StepCreateInstance) 100 defer step.Cleanup(state) 101 102 errCh := make(chan error, 1) 103 go func() { 104 <-time.After(10 * time.Millisecond) 105 errCh <- nil 106 }() 107 108 state.Put("ssh_public_key", "key") 109 110 config := state.Get("config").(*Config) 111 config.stateTimeout = 1 * time.Microsecond 112 113 driver := state.Get("driver").(*DriverMock) 114 driver.RunInstanceErrCh = errCh 115 116 // run the step 117 if action := step.Run(state); action != multistep.ActionHalt { 118 t.Fatalf("bad action: %#v", action) 119 } 120 121 // Verify state 122 if _, ok := state.GetOk("error"); !ok { 123 t.Fatal("should have error") 124 } 125 if _, ok := state.GetOk("instance_name"); ok { 126 t.Fatal("should NOT have instance name") 127 } 128 }