File: step_commit_test.go

package info (click to toggle)
packer 1.3.4%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 8,324 kB
  • sloc: python: 619; sh: 557; makefile: 111
file content (68 lines) | stat: -rw-r--r-- 1,441 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package docker

import (
	"context"
	"errors"
	"testing"

	"github.com/hashicorp/packer/helper/multistep"
)

func testStepCommitState(t *testing.T) multistep.StateBag {
	state := testState(t)
	state.Put("container_id", "foo")
	return state
}

func TestStepCommit_impl(t *testing.T) {
	var _ multistep.Step = new(StepCommit)
}

func TestStepCommit(t *testing.T) {
	state := testStepCommitState(t)
	step := new(StepCommit)
	defer step.Cleanup(state)

	driver := state.Get("driver").(*MockDriver)
	driver.CommitImageId = "bar"

	// run the step
	if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
		t.Fatalf("bad action: %#v", action)
	}

	// verify we did the right thing
	if !driver.CommitCalled {
		t.Fatal("should've called")
	}

	// verify the ID is saved
	idRaw, ok := state.GetOk("image_id")
	if !ok {
		t.Fatal("should've saved ID")
	}

	id := idRaw.(string)
	if id != driver.CommitImageId {
		t.Fatalf("bad: %#v", id)
	}
}

func TestStepCommit_error(t *testing.T) {
	state := testStepCommitState(t)
	step := new(StepCommit)
	defer step.Cleanup(state)

	driver := state.Get("driver").(*MockDriver)
	driver.CommitErr = errors.New("foo")

	// run the step
	if action := step.Run(context.Background(), state); action != multistep.ActionHalt {
		t.Fatalf("bad action: %#v", action)
	}

	// verify the ID is not saved
	if _, ok := state.GetOk("image_id"); ok {
		t.Fatal("shouldn't save image ID")
	}
}