github.com/rothwerx/packer@v0.9.0/builder/googlecompute/step_create_instance_test.go (about)

     1  package googlecompute
     2  
     3  import (
     4  	"errors"
     5  	"github.com/mitchellh/multistep"
     6  	"testing"
     7  	"time"
     8  )
     9  
    10  func TestStepCreateInstance_impl(t *testing.T) {
    11  	var _ multistep.Step = new(StepCreateInstance)
    12  }
    13  
    14  func TestStepCreateInstance(t *testing.T) {
    15  	state := testState(t)
    16  	step := new(StepCreateInstance)
    17  	defer step.Cleanup(state)
    18  
    19  	state.Put("ssh_public_key", "key")
    20  
    21  	config := state.Get("config").(*Config)
    22  	driver := state.Get("driver").(*DriverMock)
    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 state
    30  	nameRaw, ok := state.GetOk("instance_name")
    31  	if !ok {
    32  		t.Fatal("should have instance name")
    33  	}
    34  
    35  	// cleanup
    36  	step.Cleanup(state)
    37  
    38  	if driver.DeleteInstanceName != nameRaw.(string) {
    39  		t.Fatal("should've deleted instance")
    40  	}
    41  	if driver.DeleteInstanceZone != config.Zone {
    42  		t.Fatalf("bad instance zone: %#v", driver.DeleteInstanceZone)
    43  	}
    44  
    45  	if driver.DeleteDiskName != config.InstanceName {
    46  		t.Fatal("should've deleted disk")
    47  	}
    48  	if driver.DeleteDiskZone != config.Zone {
    49  		t.Fatalf("bad disk zone: %#v", driver.DeleteDiskZone)
    50  	}
    51  }
    52  
    53  func TestStepCreateInstance_error(t *testing.T) {
    54  	state := testState(t)
    55  	step := new(StepCreateInstance)
    56  	defer step.Cleanup(state)
    57  
    58  	state.Put("ssh_public_key", "key")
    59  
    60  	driver := state.Get("driver").(*DriverMock)
    61  	driver.RunInstanceErr = errors.New("error")
    62  
    63  	// run the step
    64  	if action := step.Run(state); action != multistep.ActionHalt {
    65  		t.Fatalf("bad action: %#v", action)
    66  	}
    67  
    68  	// Verify state
    69  	if _, ok := state.GetOk("error"); !ok {
    70  		t.Fatal("should have error")
    71  	}
    72  	if _, ok := state.GetOk("instance_name"); ok {
    73  		t.Fatal("should NOT have instance name")
    74  	}
    75  }
    76  
    77  func TestStepCreateInstance_errorOnChannel(t *testing.T) {
    78  	state := testState(t)
    79  	step := new(StepCreateInstance)
    80  	defer step.Cleanup(state)
    81  
    82  	errCh := make(chan error, 1)
    83  	errCh <- errors.New("error")
    84  
    85  	state.Put("ssh_public_key", "key")
    86  
    87  	driver := state.Get("driver").(*DriverMock)
    88  	driver.RunInstanceErrCh = errCh
    89  
    90  	// run the step
    91  	if action := step.Run(state); action != multistep.ActionHalt {
    92  		t.Fatalf("bad action: %#v", action)
    93  	}
    94  
    95  	// Verify state
    96  	if _, ok := state.GetOk("error"); !ok {
    97  		t.Fatal("should have error")
    98  	}
    99  	if _, ok := state.GetOk("instance_name"); ok {
   100  		t.Fatal("should NOT have instance name")
   101  	}
   102  }
   103  
   104  func TestStepCreateInstance_errorTimeout(t *testing.T) {
   105  	state := testState(t)
   106  	step := new(StepCreateInstance)
   107  	defer step.Cleanup(state)
   108  
   109  	errCh := make(chan error, 1)
   110  	go func() {
   111  		<-time.After(10 * time.Millisecond)
   112  		errCh <- nil
   113  	}()
   114  
   115  	state.Put("ssh_public_key", "key")
   116  
   117  	config := state.Get("config").(*Config)
   118  	config.stateTimeout = 1 * time.Microsecond
   119  
   120  	driver := state.Get("driver").(*DriverMock)
   121  	driver.RunInstanceErrCh = errCh
   122  
   123  	// run the step
   124  	if action := step.Run(state); action != multistep.ActionHalt {
   125  		t.Fatalf("bad action: %#v", action)
   126  	}
   127  
   128  	// Verify state
   129  	if _, ok := state.GetOk("error"); !ok {
   130  		t.Fatal("should have error")
   131  	}
   132  	if _, ok := state.GetOk("instance_name"); ok {
   133  		t.Fatal("should NOT have instance name")
   134  	}
   135  }