github.com/rothwerx/packer@v0.9.0/builder/docker/step_run_test.go (about)

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