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