github.com/mmcquillan/packer@v1.1.1-0.20171009221028-c85cf0483a5d/builder/googlecompute/step_create_instance_test.go (about)

     1  package googlecompute
     2  
     3  import (
     4  	"errors"
     5  	"strings"
     6  	"testing"
     7  	"time"
     8  
     9  	"github.com/mitchellh/multistep"
    10  	"github.com/stretchr/testify/assert"
    11  )
    12  
    13  func TestStepCreateInstance_impl(t *testing.T) {
    14  	var _ multistep.Step = new(StepCreateInstance)
    15  }
    16  
    17  func TestStepCreateInstance(t *testing.T) {
    18  	state := testState(t)
    19  	step := new(StepCreateInstance)
    20  	defer step.Cleanup(state)
    21  
    22  	state.Put("ssh_public_key", "key")
    23  
    24  	c := state.Get("config").(*Config)
    25  	d := state.Get("driver").(*DriverMock)
    26  	d.GetImageResult = StubImage("test-image", "test-project", []string{}, 100)
    27  
    28  	// run the step
    29  	assert.Equal(t, step.Run(state), multistep.ActionContinue, "Step should have passed and continued.")
    30  
    31  	// Verify state
    32  	nameRaw, ok := state.GetOk("instance_name")
    33  	assert.True(t, ok, "State should have an instance name.")
    34  
    35  	// cleanup
    36  	step.Cleanup(state)
    37  
    38  	// Check args passed to the driver.
    39  	assert.Equal(t, d.DeleteInstanceName, nameRaw.(string), "Incorrect instance name passed to driver.")
    40  	assert.Equal(t, d.DeleteInstanceZone, c.Zone, "Incorrect instance zone passed to driver.")
    41  	assert.Equal(t, d.DeleteDiskName, c.InstanceName, "Incorrect disk name passed to driver.")
    42  	assert.Equal(t, d.DeleteDiskZone, c.Zone, "Incorrect disk zone passed to driver.")
    43  }
    44  
    45  func TestStepCreateInstance_fromFamily(t *testing.T) {
    46  	cases := []struct {
    47  		Name   string
    48  		Family string
    49  		Expect bool
    50  	}{
    51  		{"test-image", "", false},
    52  		{"test-image", "test-family", false}, // name trumps family
    53  		{"", "test-family", true},
    54  	}
    55  
    56  	for _, tc := range cases {
    57  		state := testState(t)
    58  		step := new(StepCreateInstance)
    59  		defer step.Cleanup(state)
    60  
    61  		state.Put("ssh_public_key", "key")
    62  
    63  		c := state.Get("config").(*Config)
    64  		c.SourceImage = tc.Name
    65  		c.SourceImageFamily = tc.Family
    66  		d := state.Get("driver").(*DriverMock)
    67  		d.GetImageResult = StubImage("test-image", "test-project", []string{}, 100)
    68  
    69  		// run the step
    70  		assert.Equal(t, step.Run(state), multistep.ActionContinue, "Step should have passed and continued.")
    71  
    72  		// cleanup
    73  		step.Cleanup(state)
    74  
    75  		// Check args passed to the driver.
    76  		if tc.Expect {
    77  			assert.True(t, d.GetImageFromFamily, "Driver wasn't instructed to use an image family")
    78  		} else {
    79  			assert.False(t, d.GetImageFromFamily, "Driver was unexpectedly instructed to use an image family")
    80  		}
    81  	}
    82  }
    83  
    84  func TestStepCreateInstance_windowsNeedsPassword(t *testing.T) {
    85  
    86  	state := testState(t)
    87  	step := new(StepCreateInstance)
    88  	defer step.Cleanup(state)
    89  
    90  	state.Put("ssh_public_key", "key")
    91  	c := state.Get("config").(*Config)
    92  	d := state.Get("driver").(*DriverMock)
    93  	d.GetImageResult = StubImage("test-image", "test-project", []string{"windows"}, 100)
    94  	c.Comm.Type = "winrm"
    95  	// run the step
    96  	if action := step.Run(state); action != multistep.ActionContinue {
    97  		t.Fatalf("bad action: %#v", action)
    98  	}
    99  
   100  	// Verify state
   101  	nameRaw, ok := state.GetOk("instance_name")
   102  	if !ok {
   103  		t.Fatal("should have instance name")
   104  	}
   105  
   106  	createPassword, ok := state.GetOk("create_windows_password")
   107  
   108  	if !ok || !createPassword.(bool) {
   109  		t.Fatal("should need to create a windows password")
   110  	}
   111  
   112  	// cleanup
   113  	step.Cleanup(state)
   114  
   115  	if d.DeleteInstanceName != nameRaw.(string) {
   116  		t.Fatal("should've deleted instance")
   117  	}
   118  	if d.DeleteInstanceZone != c.Zone {
   119  		t.Fatalf("bad instance zone: %#v", d.DeleteInstanceZone)
   120  	}
   121  
   122  	if d.DeleteDiskName != c.InstanceName {
   123  		t.Fatal("should've deleted disk")
   124  	}
   125  	if d.DeleteDiskZone != c.Zone {
   126  		t.Fatalf("bad disk zone: %#v", d.DeleteDiskZone)
   127  	}
   128  }
   129  
   130  func TestStepCreateInstance_windowsPasswordSet(t *testing.T) {
   131  
   132  	state := testState(t)
   133  	step := new(StepCreateInstance)
   134  	defer step.Cleanup(state)
   135  
   136  	state.Put("ssh_public_key", "key")
   137  
   138  	config := state.Get("config").(*Config)
   139  	driver := state.Get("driver").(*DriverMock)
   140  	driver.GetImageResult = StubImage("test-image", "test-project", []string{"windows"}, 100)
   141  	config.Comm.Type = "winrm"
   142  	config.Comm.WinRMPassword = "password"
   143  
   144  	// run the step
   145  	if action := step.Run(state); action != multistep.ActionContinue {
   146  		t.Fatalf("bad action: %#v", action)
   147  	}
   148  
   149  	// Verify state
   150  	nameRaw, ok := state.GetOk("instance_name")
   151  	if !ok {
   152  		t.Fatal("should have instance name")
   153  	}
   154  
   155  	_, ok = state.GetOk("create_windows_password")
   156  
   157  	if ok {
   158  		t.Fatal("should not need to create windows password")
   159  	}
   160  
   161  	// cleanup
   162  	step.Cleanup(state)
   163  
   164  	if driver.DeleteInstanceName != nameRaw.(string) {
   165  		t.Fatal("should've deleted instance")
   166  	}
   167  	if driver.DeleteInstanceZone != config.Zone {
   168  		t.Fatalf("bad instance zone: %#v", driver.DeleteInstanceZone)
   169  	}
   170  
   171  	if driver.DeleteDiskName != config.InstanceName {
   172  		t.Fatal("should've deleted disk")
   173  	}
   174  	if driver.DeleteDiskZone != config.Zone {
   175  		t.Fatalf("bad disk zone: %#v", driver.DeleteDiskZone)
   176  	}
   177  }
   178  
   179  func TestStepCreateInstance_error(t *testing.T) {
   180  	state := testState(t)
   181  	step := new(StepCreateInstance)
   182  	defer step.Cleanup(state)
   183  
   184  	state.Put("ssh_public_key", "key")
   185  
   186  	d := state.Get("driver").(*DriverMock)
   187  	d.RunInstanceErr = errors.New("error")
   188  	d.GetImageResult = StubImage("test-image", "test-project", []string{}, 100)
   189  
   190  	// run the step
   191  	assert.Equal(t, step.Run(state), multistep.ActionHalt, "Step should have failed and halted.")
   192  
   193  	// Verify state
   194  	_, ok := state.GetOk("error")
   195  	assert.True(t, ok, "State should have an error.")
   196  	_, ok = state.GetOk("instance_name")
   197  	assert.False(t, ok, "State should not have an instance name.")
   198  }
   199  
   200  func TestStepCreateInstance_errorOnChannel(t *testing.T) {
   201  	state := testState(t)
   202  	step := new(StepCreateInstance)
   203  	defer step.Cleanup(state)
   204  
   205  	state.Put("ssh_public_key", "key")
   206  
   207  	errCh := make(chan error, 1)
   208  	errCh <- errors.New("error")
   209  
   210  	d := state.Get("driver").(*DriverMock)
   211  	d.RunInstanceErrCh = errCh
   212  	d.GetImageResult = StubImage("test-image", "test-project", []string{}, 100)
   213  
   214  	// run the step
   215  	assert.Equal(t, step.Run(state), multistep.ActionHalt, "Step should have failed and halted.")
   216  
   217  	// Verify state
   218  	_, ok := state.GetOk("error")
   219  	assert.True(t, ok, "State should have an error.")
   220  	_, ok = state.GetOk("instance_name")
   221  	assert.False(t, ok, "State should not have an instance name.")
   222  }
   223  
   224  func TestStepCreateInstance_errorTimeout(t *testing.T) {
   225  	state := testState(t)
   226  	step := new(StepCreateInstance)
   227  	defer step.Cleanup(state)
   228  
   229  	state.Put("ssh_public_key", "key")
   230  
   231  	errCh := make(chan error, 1)
   232  
   233  	config := state.Get("config").(*Config)
   234  	config.stateTimeout = 1 * time.Microsecond
   235  
   236  	d := state.Get("driver").(*DriverMock)
   237  	d.RunInstanceErrCh = errCh
   238  	d.GetImageResult = StubImage("test-image", "test-project", []string{}, 100)
   239  
   240  	// run the step
   241  	assert.Equal(t, step.Run(state), multistep.ActionHalt, "Step should have failed and halted.")
   242  
   243  	// Verify state
   244  	_, ok := state.GetOk("error")
   245  	assert.True(t, ok, "State should have an error.")
   246  	_, ok = state.GetOk("instance_name")
   247  	assert.False(t, ok, "State should not have an instance name.")
   248  }
   249  
   250  func TestCreateInstanceMetadata(t *testing.T) {
   251  	state := testState(t)
   252  	c := state.Get("config").(*Config)
   253  	image := StubImage("test-image", "test-project", []string{}, 100)
   254  	key := "abcdefgh12345678"
   255  
   256  	// create our metadata
   257  	metadata, err := c.createInstanceMetadata(image, key)
   258  
   259  	assert.True(t, err == nil, "Metadata creation should have succeeded.")
   260  
   261  	// ensure our key is listed
   262  	assert.True(t, strings.Contains(metadata["sshKeys"], key), "Instance metadata should contain provided key")
   263  }
   264  
   265  func TestCreateInstanceMetadata_noPublicKey(t *testing.T) {
   266  	state := testState(t)
   267  	c := state.Get("config").(*Config)
   268  	image := StubImage("test-image", "test-project", []string{}, 100)
   269  	sshKeys := c.Metadata["sshKeys"]
   270  
   271  	// create our metadata
   272  	metadata, err := c.createInstanceMetadata(image, "")
   273  
   274  	assert.True(t, err == nil, "Metadata creation should have succeeded.")
   275  
   276  	// ensure the ssh metadata hasn't changed
   277  	assert.Equal(t, metadata["sshKeys"], sshKeys, "Instance metadata should not have been modified")
   278  }