github.com/mitchellh/packer@v1.3.2/builder/docker/step_run_test.go (about)

     1  package docker
     2  
     3  import (
     4  	"context"
     5  	"errors"
     6  	"testing"
     7  
     8  	"github.com/hashicorp/packer/helper/multistep"
     9  )
    10  
    11  func testStepRunState(t *testing.T) multistep.StateBag {
    12  	state := testState(t)
    13  	state.Put("temp_dir", "/foo")
    14  	return state
    15  }
    16  
    17  func TestStepRun_impl(t *testing.T) {
    18  	var _ multistep.Step = new(StepRun)
    19  }
    20  
    21  func TestStepRun(t *testing.T) {
    22  	state := testStepRunState(t)
    23  	step := new(StepRun)
    24  	defer step.Cleanup(state)
    25  
    26  	config := state.Get("config").(*Config)
    27  	driver := state.Get("driver").(*MockDriver)
    28  	driver.StartID = "foo"
    29  
    30  	// run the step
    31  	if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
    32  		t.Fatalf("bad action: %#v", action)
    33  	}
    34  
    35  	// verify we did the right thing
    36  	if !driver.StartCalled {
    37  		t.Fatal("should've called")
    38  	}
    39  	if driver.StartConfig.Image != config.Image {
    40  		t.Fatalf("bad: %#v", driver.StartConfig.Image)
    41  	}
    42  
    43  	// verify the ID is saved
    44  	idRaw, ok := state.GetOk("container_id")
    45  	if !ok {
    46  		t.Fatal("should've saved ID")
    47  	}
    48  
    49  	id := idRaw.(string)
    50  	if id != "foo" {
    51  		t.Fatalf("bad: %#v", id)
    52  	}
    53  
    54  	// Verify we haven't called stop yet
    55  	if driver.StopCalled {
    56  		t.Fatal("should not have stopped")
    57  	}
    58  
    59  	// Cleanup
    60  	step.Cleanup(state)
    61  	if !driver.StopCalled {
    62  		t.Fatal("should've stopped")
    63  	}
    64  	if driver.StopID != id {
    65  		t.Fatalf("bad: %#v", driver.StopID)
    66  	}
    67  }
    68  
    69  func TestStepRun_error(t *testing.T) {
    70  	state := testStepRunState(t)
    71  	step := new(StepRun)
    72  	defer step.Cleanup(state)
    73  
    74  	driver := state.Get("driver").(*MockDriver)
    75  	driver.StartError = errors.New("foo")
    76  
    77  	// run the step
    78  	if action := step.Run(context.Background(), state); action != multistep.ActionHalt {
    79  		t.Fatalf("bad action: %#v", action)
    80  	}
    81  
    82  	// verify the ID is not saved
    83  	if _, ok := state.GetOk("container_id"); ok {
    84  		t.Fatal("shouldn't save container ID")
    85  	}
    86  
    87  	// Verify we haven't called stop yet
    88  	if driver.StopCalled {
    89  		t.Fatal("should not have stopped")
    90  	}
    91  
    92  	// Cleanup
    93  	step.Cleanup(state)
    94  	if driver.StopCalled {
    95  		t.Fatal("should not have stopped")
    96  	}
    97  }