github.com/marksheahan/packer@v0.10.2-0.20160613200515-1acb2d6645a0/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.CreateImageFamily != config.ImageFamily {
    35  		t.Fatalf("bad: %#v", driver.CreateImageFamily)
    36  	}
    37  	if driver.CreateImageZone != config.Zone {
    38  		t.Fatalf("bad: %#v", driver.CreateImageZone)
    39  	}
    40  	if driver.CreateImageDisk != config.DiskName {
    41  		t.Fatalf("bad: %#v", driver.CreateImageDisk)
    42  	}
    43  
    44  	nameRaw, ok := state.GetOk("image_name")
    45  	if !ok {
    46  		t.Fatal("should have name")
    47  	}
    48  	if name, ok := nameRaw.(string); !ok {
    49  		t.Fatal("name is not a string")
    50  	} else if name != config.ImageName {
    51  		t.Fatalf("bad name: %s", name)
    52  	}
    53  }
    54  
    55  func TestStepCreateImage_errorOnChannel(t *testing.T) {
    56  	state := testState(t)
    57  	step := new(StepCreateImage)
    58  	defer step.Cleanup(state)
    59  
    60  	errCh := make(chan error, 1)
    61  	errCh <- errors.New("error")
    62  
    63  	driver := state.Get("driver").(*DriverMock)
    64  	driver.CreateImageErrCh = errCh
    65  
    66  	// run the step
    67  	if action := step.Run(state); action != multistep.ActionHalt {
    68  		t.Fatalf("bad action: %#v", action)
    69  	}
    70  
    71  	if _, ok := state.GetOk("error"); !ok {
    72  		t.Fatal("should have error")
    73  	}
    74  	if _, ok := state.GetOk("image_name"); ok {
    75  		t.Fatal("should NOT have image")
    76  	}
    77  }