github.phpd.cn/hashicorp/packer@v1.3.2/builder/triton/step_create_image_from_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 TestStepCreateImageFromMachine(t *testing.T) {
    12  	state := testState(t)
    13  	step := new(StepCreateImageFromMachine)
    14  	defer step.Cleanup(state)
    15  
    16  	state.Put("machine", "test-machine-id")
    17  
    18  	if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
    19  		t.Fatalf("bad action: %#v", action)
    20  	}
    21  
    22  	_, ok := state.GetOk("image")
    23  	if !ok {
    24  		t.Fatalf("should have image")
    25  	}
    26  
    27  	step.Cleanup(state)
    28  }
    29  
    30  func TestStepCreateImageFromMachine_CreateImageFromMachineError(t *testing.T) {
    31  	state := testState(t)
    32  	step := new(StepCreateImageFromMachine)
    33  	defer step.Cleanup(state)
    34  
    35  	driver := state.Get("driver").(*DriverMock)
    36  	state.Put("machine", "test-machine-id")
    37  
    38  	driver.CreateImageFromMachineErr = errors.New("error")
    39  
    40  	if action := step.Run(context.Background(), state); action != multistep.ActionHalt {
    41  		t.Fatalf("bad action: %#v", action)
    42  	}
    43  
    44  	if _, ok := state.GetOk("error"); !ok {
    45  		t.Fatalf("should have error")
    46  	}
    47  
    48  	if _, ok := state.GetOk("image"); ok {
    49  		t.Fatalf("should NOT have image")
    50  	}
    51  }
    52  
    53  func TestStepCreateImageFromMachine_WaitForImageCreationError(t *testing.T) {
    54  	state := testState(t)
    55  	step := new(StepCreateImageFromMachine)
    56  	defer step.Cleanup(state)
    57  
    58  	driver := state.Get("driver").(*DriverMock)
    59  	state.Put("machine", "test-machine-id")
    60  
    61  	driver.WaitForImageCreationErr = errors.New("error")
    62  
    63  	if action := step.Run(context.Background(), state); action != multistep.ActionHalt {
    64  		t.Fatalf("bad action: %#v", action)
    65  	}
    66  
    67  	if _, ok := state.GetOk("error"); !ok {
    68  		t.Fatalf("should have error")
    69  	}
    70  
    71  	if _, ok := state.GetOk("image"); ok {
    72  		t.Fatalf("should NOT have image")
    73  	}
    74  }