github.com/askholme/packer@v0.7.2-0.20140924152349-70d9566a6852/builder/googlecompute/step_update_gcloud_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 TestStepUpdateGcloud_impl(t *testing.T) { 12 var _ multistep.Step = new(StepUpdateGcloud) 13 } 14 15 func TestStepUpdateGcloud(t *testing.T) { 16 state := testState(t) 17 step := new(StepUpdateGcloud) 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, "gcloud -q components update") { 36 t.Fatalf("bad command: %#v", comm.StartCmd.Command) 37 } 38 } 39 40 func TestStepUpdateGcloud_badExitStatus(t *testing.T) { 41 state := testState(t) 42 step := new(StepUpdateGcloud) 43 defer step.Cleanup(state) 44 45 comm := new(packer.MockCommunicator) 46 comm.StartExitStatus = 12 47 state.Put("communicator", comm) 48 49 // run the step 50 if action := step.Run(state); action != multistep.ActionHalt { 51 t.Fatalf("bad action: %#v", action) 52 } 53 54 if _, ok := state.GetOk("error"); !ok { 55 t.Fatal("should have error") 56 } 57 } 58 59 func TestStepUpdateGcloud_nonRoot(t *testing.T) { 60 state := testState(t) 61 step := new(StepUpdateGcloud) 62 defer step.Cleanup(state) 63 64 comm := new(packer.MockCommunicator) 65 state.Put("communicator", comm) 66 67 config := state.Get("config").(*Config) 68 config.SSHUsername = "bob" 69 70 // run the step 71 if action := step.Run(state); action != multistep.ActionContinue { 72 t.Fatalf("bad action: %#v", action) 73 } 74 75 // Verify 76 if !comm.StartCalled { 77 t.Fatal("start should be called") 78 } 79 if !strings.HasPrefix(comm.StartCmd.Command, "sudo") { 80 t.Fatal("should sudo") 81 } 82 if !strings.Contains(comm.StartCmd.Command, "gcloud -q components update") { 83 t.Fatalf("bad command: %#v", comm.StartCmd.Command) 84 } 85 }