github.com/sneal/packer@v0.5.2/builder/googlecompute/step_upload_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 TestStepUploadImage_impl(t *testing.T) {
    12  	var _ multistep.Step = new(StepUploadImage)
    13  }
    14  
    15  func TestStepUploadImage(t *testing.T) {
    16  	state := testState(t)
    17  	step := new(StepUploadImage)
    18  	defer step.Cleanup(state)
    19  
    20  	comm := new(packer.MockCommunicator)
    21  	state.Put("communicator", comm)
    22  	state.Put("image_file_name", "foo")
    23  
    24  	// run the step
    25  	if action := step.Run(state); action != multistep.ActionContinue {
    26  		t.Fatalf("bad action: %#v", action)
    27  	}
    28  
    29  	// Verify
    30  	if !comm.StartCalled {
    31  		t.Fatal("start should be called")
    32  	}
    33  	if strings.HasPrefix(comm.StartCmd.Command, "sudo") {
    34  		t.Fatal("should not sudo")
    35  	}
    36  	if !strings.Contains(comm.StartCmd.Command, "gsutil cp") {
    37  		t.Fatalf("bad command: %#v", comm.StartCmd.Command)
    38  	}
    39  }
    40  
    41  func TestStepUploadImage_badExitStatus(t *testing.T) {
    42  	state := testState(t)
    43  	step := new(StepUploadImage)
    44  	defer step.Cleanup(state)
    45  
    46  	comm := new(packer.MockCommunicator)
    47  	comm.StartExitStatus = 12
    48  	state.Put("communicator", comm)
    49  	state.Put("image_file_name", "foo")
    50  
    51  	// run the step
    52  	if action := step.Run(state); action != multistep.ActionHalt {
    53  		t.Fatalf("bad action: %#v", action)
    54  	}
    55  
    56  	if _, ok := state.GetOk("error"); !ok {
    57  		t.Fatal("should have error")
    58  	}
    59  }
    60  
    61  func TestStepUploadImage_nonRoot(t *testing.T) {
    62  	state := testState(t)
    63  	step := new(StepUploadImage)
    64  	defer step.Cleanup(state)
    65  
    66  	comm := new(packer.MockCommunicator)
    67  	state.Put("communicator", comm)
    68  	state.Put("image_file_name", "foo")
    69  
    70  	config := state.Get("config").(*Config)
    71  	config.SSHUsername = "bob"
    72  
    73  	// run the step
    74  	if action := step.Run(state); action != multistep.ActionContinue {
    75  		t.Fatalf("bad action: %#v", action)
    76  	}
    77  
    78  	// Verify
    79  	if !comm.StartCalled {
    80  		t.Fatal("start should be called")
    81  	}
    82  	if !strings.HasPrefix(comm.StartCmd.Command, "sudo") {
    83  		t.Fatal("should sudo")
    84  	}
    85  	if !strings.Contains(comm.StartCmd.Command, "gsutil cp") {
    86  		t.Fatalf("bad command: %#v", comm.StartCmd.Command)
    87  	}
    88  }