github.com/rahart/packer@v0.12.2-0.20161229105310-282bb6ad370f/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  	"github.com/stretchr/testify/assert"
     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  	c := state.Get("config").(*Config)
    21  	d := state.Get("driver").(*DriverMock)
    22  
    23  	// These are the values of the image the driver will return.
    24  	d.CreateImageResultLicenses = []string{"test-license"}
    25  	d.CreateImageResultProjectId = "test-project"
    26  	d.CreateImageResultSizeGb = 100
    27  
    28  	// run the step
    29  	action := step.Run(state)
    30  	assert.Equal(t, action, multistep.ActionContinue, "Step did not pass.")
    31  
    32  	uncastImage, ok := state.GetOk("image")
    33  	assert.True(t, ok, "State does not have resulting image.")
    34  	image, ok := uncastImage.(*Image)
    35  	assert.True(t, ok, "Image in state is not an Image.")
    36  
    37  	// Verify created Image results.
    38  	assert.Equal(t, image.Licenses, d.CreateImageResultLicenses, "Created image licenses don't match the licenses returned by the driver.")
    39  	assert.Equal(t, image.Name, c.ImageName, "Created image does not match config name.")
    40  	assert.Equal(t, image.ProjectId, d.CreateImageResultProjectId, "Created image project does not match driver project.")
    41  	assert.Equal(t, image.SizeGb, d.CreateImageResultSizeGb, "Created image size does not match the size returned by the driver.")
    42  
    43  	// Verify proper args passed to driver.CreateImage.
    44  	assert.Equal(t, d.CreateImageName, c.ImageName, "Incorrect image name passed to driver.")
    45  	assert.Equal(t, d.CreateImageDesc, c.ImageDescription, "Incorrect image description passed to driver.")
    46  	assert.Equal(t, d.CreateImageFamily, c.ImageFamily, "Incorrect image family passed to driver.")
    47  	assert.Equal(t, d.CreateImageZone, c.Zone, "Incorrect image zone passed to driver.")
    48  	assert.Equal(t, d.CreateImageDisk, c.DiskName, "Incorrect disk passed to driver.")
    49  }
    50  
    51  func TestStepCreateImage_errorOnChannel(t *testing.T) {
    52  	state := testState(t)
    53  	step := new(StepCreateImage)
    54  	defer step.Cleanup(state)
    55  
    56  	errCh := make(chan error, 1)
    57  	errCh <- errors.New("error")
    58  
    59  	driver := state.Get("driver").(*DriverMock)
    60  	driver.CreateImageErrCh = errCh
    61  
    62  	// run the step
    63  	action := step.Run(state)
    64  	assert.Equal(t, action, multistep.ActionHalt, "Step should not have passed.")
    65  	_, ok := state.GetOk("error")
    66  	assert.True(t, ok, "State should have an error.")
    67  	_, ok = state.GetOk("image_name")
    68  	assert.False(t, ok, "State should not have a resulting image.")
    69  }