github.phpd.cn/hashicorp/packer@v1.3.2/builder/googlecompute/step_instance_info_test.go (about)

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