github.phpd.cn/hashicorp/packer@v1.3.2/builder/triton/step_create_source_machine_test.go (about)

     1  package triton
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/packer/helper/multistep"
     9  )
    10  
    11  func TestStepCreateSourceMachine(t *testing.T) {
    12  	state := testState(t)
    13  	step := new(StepCreateSourceMachine)
    14  	defer step.Cleanup(state)
    15  
    16  	driver := state.Get("driver").(*DriverMock)
    17  
    18  	if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
    19  		t.Fatalf("bad action: %#v", action)
    20  	}
    21  
    22  	machineIdRaw, ok := state.GetOk("machine")
    23  	if !ok {
    24  		t.Fatalf("should have machine")
    25  	}
    26  
    27  	step.Cleanup(state)
    28  
    29  	if driver.DeleteMachineId != machineIdRaw.(string) {
    30  		t.Fatalf("should've deleted machine (%s != %s)", driver.DeleteMachineId, machineIdRaw.(string))
    31  	}
    32  }
    33  
    34  func TestStepCreateSourceMachine_CreateMachineError(t *testing.T) {
    35  	state := testState(t)
    36  	step := new(StepCreateSourceMachine)
    37  	defer step.Cleanup(state)
    38  
    39  	driver := state.Get("driver").(*DriverMock)
    40  
    41  	driver.CreateMachineErr = errors.New("error")
    42  
    43  	if action := step.Run(context.Background(), state); action != multistep.ActionHalt {
    44  		t.Fatalf("bad action: %#v", action)
    45  	}
    46  
    47  	if _, ok := state.GetOk("error"); !ok {
    48  		t.Fatalf("should have error")
    49  	}
    50  
    51  	if _, ok := state.GetOk("machine"); ok {
    52  		t.Fatalf("should NOT have machine")
    53  	}
    54  }
    55  
    56  func TestStepCreateSourceMachine_WaitForMachineStateError(t *testing.T) {
    57  	state := testState(t)
    58  	step := new(StepCreateSourceMachine)
    59  	defer step.Cleanup(state)
    60  
    61  	driver := state.Get("driver").(*DriverMock)
    62  
    63  	driver.WaitForMachineStateErr = errors.New("error")
    64  
    65  	if action := step.Run(context.Background(), state); action != multistep.ActionHalt {
    66  		t.Fatalf("bad action: %#v", action)
    67  	}
    68  
    69  	if _, ok := state.GetOk("error"); !ok {
    70  		t.Fatalf("should have error")
    71  	}
    72  
    73  	if _, ok := state.GetOk("machine"); ok {
    74  		t.Fatalf("should NOT have machine")
    75  	}
    76  }
    77  
    78  func TestStepCreateSourceMachine_StopMachineError(t *testing.T) {
    79  	state := testState(t)
    80  	step := new(StepCreateSourceMachine)
    81  	defer step.Cleanup(state)
    82  
    83  	driver := state.Get("driver").(*DriverMock)
    84  
    85  	if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
    86  		t.Fatalf("bad action: %#v", action)
    87  	}
    88  
    89  	_, ok := state.GetOk("machine")
    90  	if !ok {
    91  		t.Fatalf("should have machine")
    92  	}
    93  
    94  	driver.StopMachineErr = errors.New("error")
    95  	step.Cleanup(state)
    96  
    97  	if _, ok := state.GetOk("error"); !ok {
    98  		t.Fatalf("should have error")
    99  	}
   100  
   101  	if _, ok := state.GetOk("machine"); !ok {
   102  		t.Fatalf("should have machine")
   103  	}
   104  }
   105  
   106  func TestStepCreateSourceMachine_WaitForMachineStoppedError(t *testing.T) {
   107  	state := testState(t)
   108  	step := new(StepCreateSourceMachine)
   109  	defer step.Cleanup(state)
   110  
   111  	driver := state.Get("driver").(*DriverMock)
   112  
   113  	if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
   114  		t.Fatalf("bad action: %#v", action)
   115  	}
   116  
   117  	_, ok := state.GetOk("machine")
   118  	if !ok {
   119  		t.Fatalf("should have machine")
   120  	}
   121  
   122  	driver.WaitForMachineStateErr = errors.New("error")
   123  	step.Cleanup(state)
   124  
   125  	if _, ok := state.GetOk("error"); !ok {
   126  		t.Fatalf("should have error")
   127  	}
   128  
   129  	if _, ok := state.GetOk("machine"); !ok {
   130  		t.Fatalf("should have machine")
   131  	}
   132  }
   133  
   134  func TestStepCreateSourceMachine_DeleteMachineError(t *testing.T) {
   135  	state := testState(t)
   136  	step := new(StepCreateSourceMachine)
   137  	defer step.Cleanup(state)
   138  
   139  	driver := state.Get("driver").(*DriverMock)
   140  
   141  	if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
   142  		t.Fatalf("bad action: %#v", action)
   143  	}
   144  
   145  	_, ok := state.GetOk("machine")
   146  	if !ok {
   147  		t.Fatalf("should have machine")
   148  	}
   149  
   150  	driver.DeleteMachineErr = errors.New("error")
   151  	step.Cleanup(state)
   152  
   153  	if _, ok := state.GetOk("error"); !ok {
   154  		t.Fatalf("should have error")
   155  	}
   156  
   157  	if _, ok := state.GetOk("machine"); !ok {
   158  		t.Fatalf("should have machine")
   159  	}
   160  }