github.com/sneal/packer@v0.5.2/builder/googlecompute/step_instance_info_test.go (about) 1 package googlecompute 2 3 import ( 4 "errors" 5 "github.com/mitchellh/multistep" 6 "testing" 7 "time" 8 ) 9 10 func TestStepInstanceInfo_impl(t *testing.T) { 11 var _ multistep.Step = new(StepInstanceInfo) 12 } 13 14 func TestStepInstanceInfo(t *testing.T) { 15 state := testState(t) 16 step := new(StepInstanceInfo) 17 defer step.Cleanup(state) 18 19 state.Put("instance_name", "foo") 20 21 config := state.Get("config").(*Config) 22 driver := state.Get("driver").(*DriverMock) 23 driver.GetNatIPResult = "1.2.3.4" 24 25 // run the step 26 if action := step.Run(state); action != multistep.ActionContinue { 27 t.Fatalf("bad action: %#v", action) 28 } 29 30 // Verify state 31 if driver.WaitForInstanceState != "RUNNING" { 32 t.Fatalf("bad: %#v", driver.WaitForInstanceState) 33 } 34 if driver.WaitForInstanceZone != config.Zone { 35 t.Fatalf("bad: %#v", driver.WaitForInstanceZone) 36 } 37 if driver.WaitForInstanceName != "foo" { 38 t.Fatalf("bad: %#v", driver.WaitForInstanceName) 39 } 40 41 ipRaw, ok := state.GetOk("instance_ip") 42 if !ok { 43 t.Fatal("should have ip") 44 } 45 if ip, ok := ipRaw.(string); !ok { 46 t.Fatal("ip is not a string") 47 } else if ip != "1.2.3.4" { 48 t.Fatalf("bad ip: %s", ip) 49 } 50 } 51 52 func TestStepInstanceInfo_getNatIPError(t *testing.T) { 53 state := testState(t) 54 step := new(StepInstanceInfo) 55 defer step.Cleanup(state) 56 57 state.Put("instance_name", "foo") 58 59 driver := state.Get("driver").(*DriverMock) 60 driver.GetNatIPErr = errors.New("error") 61 62 // run the step 63 if action := step.Run(state); action != multistep.ActionHalt { 64 t.Fatalf("bad action: %#v", action) 65 } 66 67 // Verify state 68 if _, ok := state.GetOk("error"); !ok { 69 t.Fatal("should have error") 70 } 71 if _, ok := state.GetOk("instance_ip"); ok { 72 t.Fatal("should NOT have instance IP") 73 } 74 } 75 76 func TestStepInstanceInfo_waitError(t *testing.T) { 77 state := testState(t) 78 step := new(StepInstanceInfo) 79 defer step.Cleanup(state) 80 81 state.Put("instance_name", "foo") 82 83 errCh := make(chan error, 1) 84 errCh <- errors.New("error") 85 86 driver := state.Get("driver").(*DriverMock) 87 driver.WaitForInstanceErrCh = errCh 88 89 // run the step 90 if action := step.Run(state); action != multistep.ActionHalt { 91 t.Fatalf("bad action: %#v", action) 92 } 93 94 // Verify state 95 if _, ok := state.GetOk("error"); !ok { 96 t.Fatal("should have error") 97 } 98 if _, ok := state.GetOk("instance_ip"); ok { 99 t.Fatal("should NOT have instance IP") 100 } 101 } 102 103 func TestStepInstanceInfo_errorTimeout(t *testing.T) { 104 state := testState(t) 105 step := new(StepInstanceInfo) 106 defer step.Cleanup(state) 107 108 errCh := make(chan error, 1) 109 go func() { 110 <-time.After(10 * time.Millisecond) 111 errCh <- nil 112 }() 113 114 state.Put("instance_name", "foo") 115 116 config := state.Get("config").(*Config) 117 config.stateTimeout = 1 * time.Microsecond 118 119 driver := state.Get("driver").(*DriverMock) 120 driver.WaitForInstanceErrCh = errCh 121 122 // run the step 123 if action := step.Run(state); action != multistep.ActionHalt { 124 t.Fatalf("bad action: %#v", action) 125 } 126 127 // Verify state 128 if _, ok := state.GetOk("error"); !ok { 129 t.Fatal("should have error") 130 } 131 if _, ok := state.GetOk("instance_ip"); ok { 132 t.Fatal("should NOT have instance IP") 133 } 134 }