github.com/amanya/packer@v0.12.1-0.20161117214323-902ac5ab2eb6/builder/googlecompute/step_instance_info_test.go (about)

     1  package googlecompute
     2  
     3  import (
     4  	"errors"
     5  	"github.com/mitchellh/multistep"
     6  	"testing"
     7  	"time"
     8  )
     9  
    10  func TestStepInstanceInfo_impl(t *testing.T) {
    11  	var _ multistep.Step = new(StepInstanceInfo)
    12  }
    13  
    14  func TestStepInstanceInfo(t *testing.T) {
    15  	state := testState(t)
    16  	step := new(StepInstanceInfo)
    17  	defer step.Cleanup(state)
    18  
    19  	state.Put("instance_name", "foo")
    20  
    21  	config := state.Get("config").(*Config)
    22  	driver := state.Get("driver").(*DriverMock)
    23  	driver.GetNatIPResult = "1.2.3.4"
    24  
    25  	// run the step
    26  	if action := step.Run(state); action != multistep.ActionContinue {
    27  		t.Fatalf("bad action: %#v", action)
    28  	}
    29  
    30  	// Verify state
    31  	if driver.WaitForInstanceState != "RUNNING" {
    32  		t.Fatalf("bad: %#v", driver.WaitForInstanceState)
    33  	}
    34  	if driver.WaitForInstanceZone != config.Zone {
    35  		t.Fatalf("bad: %#v", driver.WaitForInstanceZone)
    36  	}
    37  	if driver.WaitForInstanceName != "foo" {
    38  		t.Fatalf("bad: %#v", driver.WaitForInstanceName)
    39  	}
    40  
    41  	ipRaw, ok := state.GetOk("instance_ip")
    42  	if !ok {
    43  		t.Fatal("should have ip")
    44  	}
    45  	if ip, ok := ipRaw.(string); !ok {
    46  		t.Fatal("ip is not a string")
    47  	} else if ip != "1.2.3.4" {
    48  		t.Fatalf("bad ip: %s", ip)
    49  	}
    50  }
    51  
    52  func TestStepInstanceInfo_InternalIP(t *testing.T) {
    53  	state := testState(t)
    54  	step := new(StepInstanceInfo)
    55  	defer step.Cleanup(state)
    56  
    57  	state.Put("instance_name", "foo")
    58  
    59  	config := state.Get("config").(*Config)
    60  	config.UseInternalIP = true
    61  	driver := state.Get("driver").(*DriverMock)
    62  	driver.GetNatIPResult = "1.2.3.4"
    63  	driver.GetInternalIPResult = "5.6.7.8"
    64  
    65  	// run the step
    66  	if action := step.Run(state); action != multistep.ActionContinue {
    67  		t.Fatalf("bad action: %#v", action)
    68  	}
    69  
    70  	// Verify state
    71  	if driver.WaitForInstanceState != "RUNNING" {
    72  		t.Fatalf("bad: %#v", driver.WaitForInstanceState)
    73  	}
    74  	if driver.WaitForInstanceZone != config.Zone {
    75  		t.Fatalf("bad: %#v", driver.WaitForInstanceZone)
    76  	}
    77  	if driver.WaitForInstanceName != "foo" {
    78  		t.Fatalf("bad: %#v", driver.WaitForInstanceName)
    79  	}
    80  
    81  	ipRaw, ok := state.GetOk("instance_ip")
    82  	if !ok {
    83  		t.Fatal("should have ip")
    84  	}
    85  	if ip, ok := ipRaw.(string); !ok {
    86  		t.Fatal("ip is not a string")
    87  	} else if ip != "5.6.7.8" {
    88  		t.Fatalf("bad ip: %s", ip)
    89  	}
    90  }
    91  
    92  func TestStepInstanceInfo_getNatIPError(t *testing.T) {
    93  	state := testState(t)
    94  	step := new(StepInstanceInfo)
    95  	defer step.Cleanup(state)
    96  
    97  	state.Put("instance_name", "foo")
    98  
    99  	driver := state.Get("driver").(*DriverMock)
   100  	driver.GetNatIPErr = errors.New("error")
   101  
   102  	// run the step
   103  	if action := step.Run(state); action != multistep.ActionHalt {
   104  		t.Fatalf("bad action: %#v", action)
   105  	}
   106  
   107  	// Verify state
   108  	if _, ok := state.GetOk("error"); !ok {
   109  		t.Fatal("should have error")
   110  	}
   111  	if _, ok := state.GetOk("instance_ip"); ok {
   112  		t.Fatal("should NOT have instance IP")
   113  	}
   114  }
   115  
   116  func TestStepInstanceInfo_waitError(t *testing.T) {
   117  	state := testState(t)
   118  	step := new(StepInstanceInfo)
   119  	defer step.Cleanup(state)
   120  
   121  	state.Put("instance_name", "foo")
   122  
   123  	errCh := make(chan error, 1)
   124  	errCh <- errors.New("error")
   125  
   126  	driver := state.Get("driver").(*DriverMock)
   127  	driver.WaitForInstanceErrCh = errCh
   128  
   129  	// run the step
   130  	if action := step.Run(state); action != multistep.ActionHalt {
   131  		t.Fatalf("bad action: %#v", action)
   132  	}
   133  
   134  	// Verify state
   135  	if _, ok := state.GetOk("error"); !ok {
   136  		t.Fatal("should have error")
   137  	}
   138  	if _, ok := state.GetOk("instance_ip"); ok {
   139  		t.Fatal("should NOT have instance IP")
   140  	}
   141  }
   142  
   143  func TestStepInstanceInfo_errorTimeout(t *testing.T) {
   144  	state := testState(t)
   145  	step := new(StepInstanceInfo)
   146  	defer step.Cleanup(state)
   147  
   148  	errCh := make(chan error, 1)
   149  	go func() {
   150  		<-time.After(10 * time.Millisecond)
   151  		errCh <- nil
   152  	}()
   153  
   154  	state.Put("instance_name", "foo")
   155  
   156  	config := state.Get("config").(*Config)
   157  	config.stateTimeout = 1 * time.Microsecond
   158  
   159  	driver := state.Get("driver").(*DriverMock)
   160  	driver.WaitForInstanceErrCh = errCh
   161  
   162  	// run the step
   163  	if action := step.Run(state); action != multistep.ActionHalt {
   164  		t.Fatalf("bad action: %#v", action)
   165  	}
   166  
   167  	// Verify state
   168  	if _, ok := state.GetOk("error"); !ok {
   169  		t.Fatal("should have error")
   170  	}
   171  	if _, ok := state.GetOk("instance_ip"); ok {
   172  		t.Fatal("should NOT have instance IP")
   173  	}
   174  }