github.com/kaixiang/packer@v0.5.2-0.20140114230416-1f5786b0d7f1/builder/googlecompute/step_register_image_test.go (about) 1 package googlecompute 2 3 import ( 4 "errors" 5 "github.com/mitchellh/multistep" 6 "testing" 7 "time" 8 ) 9 10 func TestStepRegisterImage_impl(t *testing.T) { 11 var _ multistep.Step = new(StepRegisterImage) 12 } 13 14 func TestStepRegisterImage(t *testing.T) { 15 state := testState(t) 16 step := new(StepRegisterImage) 17 defer step.Cleanup(state) 18 19 config := state.Get("config").(*Config) 20 driver := state.Get("driver").(*DriverMock) 21 22 // run the step 23 if action := step.Run(state); action != multistep.ActionContinue { 24 t.Fatalf("bad action: %#v", action) 25 } 26 27 // Verify state 28 if driver.CreateImageName != config.ImageName { 29 t.Fatalf("bad: %#v", driver.CreateImageName) 30 } 31 if driver.CreateImageDesc != config.ImageDescription { 32 t.Fatalf("bad: %#v", driver.CreateImageDesc) 33 } 34 35 nameRaw, ok := state.GetOk("image_name") 36 if !ok { 37 t.Fatal("should have name") 38 } 39 if name, ok := nameRaw.(string); !ok { 40 t.Fatal("name is not a string") 41 } else if name != config.ImageName { 42 t.Fatalf("bad name: %s", name) 43 } 44 } 45 46 func TestStepRegisterImage_waitError(t *testing.T) { 47 state := testState(t) 48 step := new(StepRegisterImage) 49 defer step.Cleanup(state) 50 51 errCh := make(chan error, 1) 52 errCh <- errors.New("error") 53 54 driver := state.Get("driver").(*DriverMock) 55 driver.CreateImageErrCh = errCh 56 57 // run the step 58 if action := step.Run(state); action != multistep.ActionHalt { 59 t.Fatalf("bad action: %#v", action) 60 } 61 62 // Verify state 63 if _, ok := state.GetOk("error"); !ok { 64 t.Fatal("should have error") 65 } 66 if _, ok := state.GetOk("image_name"); ok { 67 t.Fatal("should NOT have image_name") 68 } 69 } 70 71 func TestStepRegisterImage_errorTimeout(t *testing.T) { 72 state := testState(t) 73 step := new(StepRegisterImage) 74 defer step.Cleanup(state) 75 76 errCh := make(chan error, 1) 77 go func() { 78 <-time.After(10 * time.Millisecond) 79 errCh <- nil 80 }() 81 82 config := state.Get("config").(*Config) 83 config.stateTimeout = 1 * time.Microsecond 84 85 driver := state.Get("driver").(*DriverMock) 86 driver.CreateImageErrCh = errCh 87 88 // run the step 89 if action := step.Run(state); action != multistep.ActionHalt { 90 t.Fatalf("bad action: %#v", action) 91 } 92 93 // Verify state 94 if _, ok := state.GetOk("error"); !ok { 95 t.Fatal("should have error") 96 } 97 if _, ok := state.GetOk("image_name"); ok { 98 t.Fatal("should NOT have image name") 99 } 100 }