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

     1  package docker
     2  
     3  import (
     4  	"errors"
     5  	"github.com/mitchellh/multistep"
     6  	"testing"
     7  )
     8  
     9  func testStepCommitState(t *testing.T) multistep.StateBag {
    10  	state := testState(t)
    11  	state.Put("container_id", "foo")
    12  	return state
    13  }
    14  
    15  func TestStepCommit_impl(t *testing.T) {
    16  	var _ multistep.Step = new(StepCommit)
    17  }
    18  
    19  func TestStepCommit(t *testing.T) {
    20  	state := testStepCommitState(t)
    21  	step := new(StepCommit)
    22  	defer step.Cleanup(state)
    23  
    24  	driver := state.Get("driver").(*MockDriver)
    25  	driver.CommitImageId = "bar"
    26  
    27  	// run the step
    28  	if action := step.Run(state); action != multistep.ActionContinue {
    29  		t.Fatalf("bad action: %#v", action)
    30  	}
    31  
    32  	// verify we did the right thing
    33  	if !driver.CommitCalled {
    34  		t.Fatal("should've called")
    35  	}
    36  
    37  	// verify the ID is saved
    38  	idRaw, ok := state.GetOk("image_id")
    39  	if !ok {
    40  		t.Fatal("should've saved ID")
    41  	}
    42  
    43  	id := idRaw.(string)
    44  	if id != driver.CommitImageId {
    45  		t.Fatalf("bad: %#v", id)
    46  	}
    47  }
    48  
    49  func TestStepCommit_error(t *testing.T) {
    50  	state := testStepCommitState(t)
    51  	step := new(StepCommit)
    52  	defer step.Cleanup(state)
    53  
    54  	driver := state.Get("driver").(*MockDriver)
    55  	driver.CommitErr = errors.New("foo")
    56  
    57  	// run the step
    58  	if action := step.Run(state); action != multistep.ActionHalt {
    59  		t.Fatalf("bad action: %#v", action)
    60  	}
    61  
    62  	// verify the ID is not saved
    63  	if _, ok := state.GetOk("image_id"); ok {
    64  		t.Fatal("shouldn't save image ID")
    65  	}
    66  }