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

     1  package oci
     2  
     3  import (
     4  	"errors"
     5  	"testing"
     6  
     7  	"github.com/mitchellh/multistep"
     8  )
     9  
    10  func TestStepCreateInstance(t *testing.T) {
    11  	state := testState()
    12  	state.Put("publicKey", "key")
    13  
    14  	step := new(stepCreateInstance)
    15  	defer step.Cleanup(state)
    16  
    17  	driver := state.Get("driver").(*driverMock)
    18  
    19  	if action := step.Run(state); action != multistep.ActionContinue {
    20  		t.Fatalf("bad action: %#v", action)
    21  	}
    22  
    23  	instanceIDRaw, ok := state.GetOk("instance_id")
    24  	if !ok {
    25  		t.Fatalf("should have machine")
    26  	}
    27  
    28  	step.Cleanup(state)
    29  
    30  	if driver.TerminateInstanceID != instanceIDRaw.(string) {
    31  		t.Fatalf(
    32  			"should've deleted instance (%s != %s)",
    33  			driver.TerminateInstanceID, instanceIDRaw.(string))
    34  	}
    35  }
    36  
    37  func TestStepCreateInstance_CreateInstanceErr(t *testing.T) {
    38  	state := testState()
    39  	state.Put("publicKey", "key")
    40  
    41  	step := new(stepCreateInstance)
    42  	defer step.Cleanup(state)
    43  
    44  	driver := state.Get("driver").(*driverMock)
    45  	driver.CreateInstanceErr = errors.New("error")
    46  
    47  	if action := step.Run(state); action != multistep.ActionHalt {
    48  		t.Fatalf("bad action: %#v", action)
    49  	}
    50  
    51  	if _, ok := state.GetOk("error"); !ok {
    52  		t.Fatalf("should have error")
    53  	}
    54  
    55  	if _, ok := state.GetOk("instance_id"); ok {
    56  		t.Fatalf("should NOT have instance_id")
    57  	}
    58  
    59  	step.Cleanup(state)
    60  
    61  	if driver.TerminateInstanceID != "" {
    62  		t.Fatalf("Should not have tried to terminate an instance")
    63  	}
    64  }
    65  
    66  func TestStepCreateInstance_WaitForInstanceStateErr(t *testing.T) {
    67  	state := testState()
    68  	state.Put("publicKey", "key")
    69  
    70  	step := new(stepCreateInstance)
    71  	defer step.Cleanup(state)
    72  
    73  	driver := state.Get("driver").(*driverMock)
    74  	driver.WaitForInstanceStateErr = errors.New("error")
    75  
    76  	if action := step.Run(state); action != multistep.ActionHalt {
    77  		t.Fatalf("bad action: %#v", action)
    78  	}
    79  
    80  	if _, ok := state.GetOk("error"); !ok {
    81  		t.Fatalf("should have error")
    82  	}
    83  }
    84  
    85  func TestStepCreateInstance_TerminateInstanceErr(t *testing.T) {
    86  	state := testState()
    87  	state.Put("publicKey", "key")
    88  
    89  	step := new(stepCreateInstance)
    90  	defer step.Cleanup(state)
    91  
    92  	driver := state.Get("driver").(*driverMock)
    93  
    94  	if action := step.Run(state); action != multistep.ActionContinue {
    95  		t.Fatalf("bad action: %#v", action)
    96  	}
    97  
    98  	_, ok := state.GetOk("instance_id")
    99  	if !ok {
   100  		t.Fatalf("should have machine")
   101  	}
   102  
   103  	driver.TerminateInstanceErr = errors.New("error")
   104  	step.Cleanup(state)
   105  
   106  	if _, ok := state.GetOk("error"); !ok {
   107  		t.Fatalf("should have error")
   108  	}
   109  }
   110  
   111  func TestStepCreateInstanceCleanup_WaitForInstanceStateErr(t *testing.T) {
   112  	state := testState()
   113  	state.Put("publicKey", "key")
   114  
   115  	step := new(stepCreateInstance)
   116  	defer step.Cleanup(state)
   117  
   118  	driver := state.Get("driver").(*driverMock)
   119  
   120  	if action := step.Run(state); action != multistep.ActionContinue {
   121  		t.Fatalf("bad action: %#v", action)
   122  	}
   123  
   124  	driver.WaitForInstanceStateErr = errors.New("error")
   125  	step.Cleanup(state)
   126  
   127  	if _, ok := state.GetOk("error"); !ok {
   128  		t.Fatalf("should have error")
   129  	}
   130  }