github.com/sneal/packer@v0.5.2/builder/googlecompute/step_create_image_test.go (about) 1 package googlecompute 2 3 import ( 4 "strings" 5 "testing" 6 7 "github.com/mitchellh/multistep" 8 "github.com/mitchellh/packer/packer" 9 ) 10 11 func TestStepCreateImage_impl(t *testing.T) { 12 var _ multistep.Step = new(StepCreateImage) 13 } 14 15 func TestStepCreateImage(t *testing.T) { 16 state := testState(t) 17 step := new(StepCreateImage) 18 defer step.Cleanup(state) 19 20 comm := new(packer.MockCommunicator) 21 state.Put("communicator", comm) 22 23 // run the step 24 if action := step.Run(state); action != multistep.ActionContinue { 25 t.Fatalf("bad action: %#v", action) 26 } 27 28 // Verify 29 if !comm.StartCalled { 30 t.Fatal("start should be called") 31 } 32 if strings.HasPrefix(comm.StartCmd.Command, "sudo") { 33 t.Fatal("should not sudo") 34 } 35 if !strings.Contains(comm.StartCmd.Command, "gcimagebundle") { 36 t.Fatalf("bad command: %#v", comm.StartCmd.Command) 37 } 38 39 if _, ok := state.GetOk("image_file_name"); !ok { 40 t.Fatal("should have image") 41 } 42 } 43 44 func TestStepCreateImage_badExitStatus(t *testing.T) { 45 state := testState(t) 46 step := new(StepCreateImage) 47 defer step.Cleanup(state) 48 49 comm := new(packer.MockCommunicator) 50 comm.StartExitStatus = 12 51 state.Put("communicator", comm) 52 53 // run the step 54 if action := step.Run(state); action != multistep.ActionHalt { 55 t.Fatalf("bad action: %#v", action) 56 } 57 58 if _, ok := state.GetOk("error"); !ok { 59 t.Fatal("should have error") 60 } 61 if _, ok := state.GetOk("image_file_name"); ok { 62 t.Fatal("should NOT have image") 63 } 64 } 65 66 func TestStepCreateImage_nonRoot(t *testing.T) { 67 state := testState(t) 68 step := new(StepCreateImage) 69 defer step.Cleanup(state) 70 71 comm := new(packer.MockCommunicator) 72 state.Put("communicator", comm) 73 74 config := state.Get("config").(*Config) 75 config.SSHUsername = "bob" 76 77 // run the step 78 if action := step.Run(state); action != multistep.ActionContinue { 79 t.Fatalf("bad action: %#v", action) 80 } 81 82 // Verify 83 if !comm.StartCalled { 84 t.Fatal("start should be called") 85 } 86 if !strings.HasPrefix(comm.StartCmd.Command, "sudo") { 87 t.Fatal("should sudo") 88 } 89 if !strings.Contains(comm.StartCmd.Command, "gcimagebundle") { 90 t.Fatalf("bad command: %#v", comm.StartCmd.Command) 91 } 92 93 if _, ok := state.GetOk("image_file_name"); !ok { 94 t.Fatal("should have image") 95 } 96 }