File: step_run_test.go

package info (click to toggle)
packer 0.10.2%2Bdfsg-6
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 4,728 kB
  • ctags: 4,626
  • sloc: sh: 321; makefile: 73
file content (95 lines) | stat: -rw-r--r-- 1,936 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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
package docker

import (
	"errors"
	"github.com/mitchellh/multistep"
	"testing"
)

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

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

func TestStepRun(t *testing.T) {
	state := testStepRunState(t)
	step := new(StepRun)
	defer step.Cleanup(state)

	config := state.Get("config").(*Config)
	driver := state.Get("driver").(*MockDriver)
	driver.StartID = "foo"

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

	// verify we did the right thing
	if !driver.StartCalled {
		t.Fatal("should've called")
	}
	if driver.StartConfig.Image != config.Image {
		t.Fatalf("bad: %#v", driver.StartConfig.Image)
	}

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

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

	// Verify we haven't called stop yet
	if driver.StopCalled {
		t.Fatal("should not have stopped")
	}

	// Cleanup
	step.Cleanup(state)
	if !driver.StopCalled {
		t.Fatal("should've stopped")
	}
	if driver.StopID != id {
		t.Fatalf("bad: %#v", driver.StopID)
	}
}

func TestStepRun_error(t *testing.T) {
	state := testStepRunState(t)
	step := new(StepRun)
	defer step.Cleanup(state)

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

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

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

	// Verify we haven't called stop yet
	if driver.StopCalled {
		t.Fatal("should not have stopped")
	}

	// Cleanup
	step.Cleanup(state)
	if driver.StopCalled {
		t.Fatal("should not have stopped")
	}
}