packer-cn/builder/docker/step_run_test.go

98 lines
2.0 KiB
Go
Raw Permalink Normal View History

2013-11-09 16:03:01 -05:00
package docker
import (
"context"
2013-11-09 16:03:01 -05:00
"errors"
"testing"
2018-01-22 20:21:10 -05:00
2020-11-17 19:31:03 -05:00
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
2013-11-09 16:03:01 -05:00
)
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)
}
2018-01-22 19:03:49 -05:00
func TestStepRun(t *testing.T) {
2013-11-09 16:03:01 -05:00
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
2018-01-22 19:03:49 -05:00
if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
2013-11-09 16:03:01 -05:00
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
2019-03-29 14:37:23 -04:00
if driver.KillCalled {
2013-11-09 16:03:01 -05:00
t.Fatal("should not have stopped")
}
// Cleanup
step.Cleanup(state)
2019-03-29 14:37:23 -04:00
if !driver.KillCalled {
2013-11-09 16:03:01 -05:00
t.Fatal("should've stopped")
}
2019-03-29 14:37:23 -04:00
if driver.KillID != id {
2013-11-09 16:03:01 -05:00
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
2018-01-22 19:03:49 -05:00
if action := step.Run(context.Background(), state); action != multistep.ActionHalt {
2013-11-09 16:03:01 -05:00
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
2019-03-29 14:37:23 -04:00
if driver.KillCalled {
2013-11-09 16:03:01 -05:00
t.Fatal("should not have stopped")
}
// Cleanup
step.Cleanup(state)
2019-03-29 14:37:23 -04:00
if driver.KillCalled {
2013-11-09 16:03:01 -05:00
t.Fatal("should not have stopped")
}
}