github.com/kikitux/packer@v0.10.1-0.20160322154024-6237df566f9f/builder/googlecompute/step_create_image_test.go (about) 1 package googlecompute 2 3 import ( 4 "errors" 5 "testing" 6 7 "github.com/mitchellh/multistep" 8 ) 9 10 func TestStepCreateImage_impl(t *testing.T) { 11 var _ multistep.Step = new(StepCreateImage) 12 } 13 14 func TestStepCreateImage(t *testing.T) { 15 state := testState(t) 16 step := new(StepCreateImage) 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 if driver.CreateImageZone != config.Zone { 35 t.Fatalf("bad: %#v", driver.CreateImageZone) 36 } 37 if driver.CreateImageDisk != config.DiskName { 38 t.Fatalf("bad: %#v", driver.CreateImageDisk) 39 } 40 41 nameRaw, ok := state.GetOk("image_name") 42 if !ok { 43 t.Fatal("should have name") 44 } 45 if name, ok := nameRaw.(string); !ok { 46 t.Fatal("name is not a string") 47 } else if name != config.ImageName { 48 t.Fatalf("bad name: %s", name) 49 } 50 } 51 52 func TestStepCreateImage_errorOnChannel(t *testing.T) { 53 state := testState(t) 54 step := new(StepCreateImage) 55 defer step.Cleanup(state) 56 57 errCh := make(chan error, 1) 58 errCh <- errors.New("error") 59 60 driver := state.Get("driver").(*DriverMock) 61 driver.CreateImageErrCh = errCh 62 63 // run the step 64 if action := step.Run(state); action != multistep.ActionHalt { 65 t.Fatalf("bad action: %#v", action) 66 } 67 68 if _, ok := state.GetOk("error"); !ok { 69 t.Fatal("should have error") 70 } 71 if _, ok := state.GetOk("image_name"); ok { 72 t.Fatal("should NOT have image") 73 } 74 }