2013-12-21 18:20:15 -05:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2019-01-11 18:06:36 -05:00
|
|
|
"bytes"
|
2018-01-22 19:03:49 -05:00
|
|
|
"context"
|
2013-12-21 18:20:15 -05:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"testing"
|
2018-01-22 20:21:10 -05:00
|
|
|
|
2018-01-22 18:32:33 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2019-01-11 18:06:36 -05:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2013-12-21 18:20:15 -05:00
|
|
|
)
|
|
|
|
|
2019-01-11 18:06:36 -05:00
|
|
|
func testState(t *testing.T) multistep.StateBag {
|
|
|
|
state := new(multistep.BasicStateBag)
|
|
|
|
state.Put("ui", &packer.BasicUi{
|
|
|
|
Reader: new(bytes.Buffer),
|
|
|
|
Writer: new(bytes.Buffer),
|
|
|
|
})
|
|
|
|
return state
|
|
|
|
}
|
|
|
|
|
2013-12-21 18:20:15 -05:00
|
|
|
func testStepOutputDir(t *testing.T) *StepOutputDir {
|
|
|
|
td, err := ioutil.TempDir("", "packer")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
if err := os.RemoveAll(td); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &StepOutputDir{Force: false, Path: td}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStepOutputDir_impl(t *testing.T) {
|
|
|
|
var _ multistep.Step = new(StepOutputDir)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStepOutputDir(t *testing.T) {
|
|
|
|
state := testState(t)
|
|
|
|
step := testStepOutputDir(t)
|
2018-04-25 10:39:20 -04:00
|
|
|
defer os.RemoveAll(step.Path)
|
2013-12-21 18:20:15 -05:00
|
|
|
|
|
|
|
// Test the run
|
2018-01-22 19:03:49 -05:00
|
|
|
if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
|
2013-12-21 18:20:15 -05:00
|
|
|
t.Fatalf("bad action: %#v", action)
|
|
|
|
}
|
|
|
|
if _, ok := state.GetOk("error"); ok {
|
|
|
|
t.Fatal("should NOT have error")
|
|
|
|
}
|
|
|
|
if _, err := os.Stat(step.Path); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test the cleanup
|
|
|
|
step.Cleanup(state)
|
|
|
|
if _, err := os.Stat(step.Path); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-13 16:58:37 -04:00
|
|
|
func TestStepOutputDir_exists(t *testing.T) {
|
|
|
|
state := testState(t)
|
|
|
|
step := testStepOutputDir(t)
|
|
|
|
|
|
|
|
// Make the dir
|
|
|
|
if err := os.MkdirAll(step.Path, 0755); err != nil {
|
|
|
|
t.Fatalf("bad: %s", err)
|
|
|
|
}
|
2018-04-25 10:39:20 -04:00
|
|
|
defer os.RemoveAll(step.Path)
|
2015-06-13 16:58:37 -04:00
|
|
|
|
|
|
|
// Test the run
|
2018-01-22 19:03:49 -05:00
|
|
|
if action := step.Run(context.Background(), state); action != multistep.ActionHalt {
|
2015-06-13 16:58:37 -04:00
|
|
|
t.Fatalf("bad action: %#v", action)
|
|
|
|
}
|
|
|
|
if _, ok := state.GetOk("error"); !ok {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test the cleanup
|
|
|
|
step.Cleanup(state)
|
|
|
|
if _, err := os.Stat(step.Path); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-21 18:20:15 -05:00
|
|
|
func TestStepOutputDir_cancelled(t *testing.T) {
|
|
|
|
state := testState(t)
|
|
|
|
step := testStepOutputDir(t)
|
|
|
|
|
|
|
|
// Test the run
|
2018-01-22 19:03:49 -05:00
|
|
|
if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
|
2013-12-21 18:20:15 -05:00
|
|
|
t.Fatalf("bad action: %#v", action)
|
|
|
|
}
|
|
|
|
if _, ok := state.GetOk("error"); ok {
|
|
|
|
t.Fatal("should NOT have error")
|
|
|
|
}
|
|
|
|
if _, err := os.Stat(step.Path); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark
|
|
|
|
state.Put(multistep.StateCancelled, true)
|
|
|
|
|
|
|
|
// Test the cleanup
|
|
|
|
step.Cleanup(state)
|
|
|
|
if _, err := os.Stat(step.Path); err == nil {
|
|
|
|
t.Fatal("should not exist")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStepOutputDir_halted(t *testing.T) {
|
|
|
|
state := testState(t)
|
|
|
|
step := testStepOutputDir(t)
|
|
|
|
|
|
|
|
// Test the run
|
2018-01-22 19:03:49 -05:00
|
|
|
if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
|
2013-12-21 18:20:15 -05:00
|
|
|
t.Fatalf("bad action: %#v", action)
|
|
|
|
}
|
|
|
|
if _, ok := state.GetOk("error"); ok {
|
|
|
|
t.Fatal("should NOT have error")
|
|
|
|
}
|
|
|
|
if _, err := os.Stat(step.Path); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Mark
|
|
|
|
state.Put(multistep.StateHalted, true)
|
|
|
|
|
|
|
|
// Test the cleanup
|
|
|
|
step.Cleanup(state)
|
|
|
|
if _, err := os.Stat(step.Path); err == nil {
|
|
|
|
t.Fatal("should not exist")
|
|
|
|
}
|
|
|
|
}
|