2013-12-24 13:21:02 -05:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2013-12-24 13:21:02 -05:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"testing"
|
2018-01-22 20:21:10 -05:00
|
|
|
|
|
|
|
"github.com/mitchellh/multistep"
|
2013-12-24 13:21:02 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func testOutputDir(t *testing.T) *LocalOutputDir {
|
|
|
|
td, err := ioutil.TempDir("", "packer")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
os.RemoveAll(td)
|
2013-12-24 13:22:22 -05:00
|
|
|
|
|
|
|
result := new(LocalOutputDir)
|
|
|
|
result.SetOutputDir(td)
|
|
|
|
return result
|
2013-12-24 13:21:02 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
func TestStepOutputDir_impl(t *testing.T) {
|
|
|
|
var _ multistep.Step = new(StepOutputDir)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStepOutputDir(t *testing.T) {
|
|
|
|
state := testState(t)
|
|
|
|
step := new(StepOutputDir)
|
|
|
|
|
|
|
|
dir := testOutputDir(t)
|
|
|
|
state.Put("dir", dir)
|
|
|
|
|
|
|
|
// Test the run
|
|
|
|
if action := step.Run(state); action != multistep.ActionContinue {
|
|
|
|
t.Fatalf("bad action: %#v", action)
|
|
|
|
}
|
|
|
|
if _, ok := state.GetOk("error"); ok {
|
|
|
|
t.Fatal("should NOT have error")
|
|
|
|
}
|
2013-12-24 13:22:22 -05:00
|
|
|
if _, err := os.Stat(dir.dir); err != nil {
|
2013-12-24 13:21:02 -05:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test the cleanup
|
|
|
|
step.Cleanup(state)
|
2013-12-24 13:22:22 -05:00
|
|
|
if _, err := os.Stat(dir.dir); err != nil {
|
2013-12-24 13:21:02 -05:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStepOutputDir_existsNoForce(t *testing.T) {
|
|
|
|
state := testState(t)
|
|
|
|
step := new(StepOutputDir)
|
|
|
|
|
|
|
|
dir := testOutputDir(t)
|
|
|
|
state.Put("dir", dir)
|
|
|
|
|
|
|
|
// Make sure the dir exists
|
2013-12-24 13:22:22 -05:00
|
|
|
if err := os.MkdirAll(dir.dir, 0755); err != nil {
|
2013-12-24 13:21:02 -05:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test the run
|
|
|
|
if action := step.Run(state); action != multistep.ActionHalt {
|
|
|
|
t.Fatalf("bad action: %#v", action)
|
|
|
|
}
|
|
|
|
if _, ok := state.GetOk("error"); !ok {
|
|
|
|
t.Fatal("should have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test the cleanup
|
|
|
|
step.Cleanup(state)
|
2013-12-24 13:22:22 -05:00
|
|
|
if _, err := os.Stat(dir.dir); err != nil {
|
2013-12-24 13:21:02 -05:00
|
|
|
t.Fatal("should not delete dir")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStepOutputDir_existsForce(t *testing.T) {
|
|
|
|
state := testState(t)
|
|
|
|
step := new(StepOutputDir)
|
|
|
|
step.Force = true
|
|
|
|
|
|
|
|
dir := testOutputDir(t)
|
|
|
|
state.Put("dir", dir)
|
|
|
|
|
|
|
|
// Make sure the dir exists
|
2013-12-24 13:22:22 -05:00
|
|
|
if err := os.MkdirAll(dir.dir, 0755); err != nil {
|
2013-12-24 13:21:02 -05:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test the run
|
|
|
|
if action := step.Run(state); action != multistep.ActionContinue {
|
|
|
|
t.Fatalf("bad action: %#v", action)
|
|
|
|
}
|
|
|
|
if _, ok := state.GetOk("error"); ok {
|
|
|
|
t.Fatal("should NOT have error")
|
|
|
|
}
|
2013-12-24 13:22:22 -05:00
|
|
|
if _, err := os.Stat(dir.dir); err != nil {
|
2013-12-24 13:21:02 -05:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStepOutputDir_cancel(t *testing.T) {
|
|
|
|
state := testState(t)
|
|
|
|
step := new(StepOutputDir)
|
|
|
|
|
|
|
|
dir := testOutputDir(t)
|
|
|
|
state.Put("dir", dir)
|
|
|
|
|
|
|
|
// Test the run
|
|
|
|
if action := step.Run(state); action != multistep.ActionContinue {
|
|
|
|
t.Fatalf("bad action: %#v", action)
|
|
|
|
}
|
|
|
|
if _, ok := state.GetOk("error"); ok {
|
|
|
|
t.Fatal("should NOT have error")
|
|
|
|
}
|
2013-12-24 13:22:22 -05:00
|
|
|
if _, err := os.Stat(dir.dir); err != nil {
|
2013-12-24 13:21:02 -05:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test cancel/halt
|
|
|
|
state.Put(multistep.StateCancelled, true)
|
|
|
|
step.Cleanup(state)
|
2013-12-24 13:22:22 -05:00
|
|
|
if _, err := os.Stat(dir.dir); err == nil {
|
2013-12-24 13:21:02 -05:00
|
|
|
t.Fatal("directory should not exist")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStepOutputDir_halt(t *testing.T) {
|
|
|
|
state := testState(t)
|
|
|
|
step := new(StepOutputDir)
|
|
|
|
|
|
|
|
dir := testOutputDir(t)
|
|
|
|
state.Put("dir", dir)
|
|
|
|
|
|
|
|
// Test the run
|
|
|
|
if action := step.Run(state); action != multistep.ActionContinue {
|
|
|
|
t.Fatalf("bad action: %#v", action)
|
|
|
|
}
|
|
|
|
if _, ok := state.GetOk("error"); ok {
|
|
|
|
t.Fatal("should NOT have error")
|
|
|
|
}
|
2013-12-24 13:22:22 -05:00
|
|
|
if _, err := os.Stat(dir.dir); err != nil {
|
2013-12-24 13:21:02 -05:00
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test cancel/halt
|
|
|
|
state.Put(multistep.StateHalted, true)
|
|
|
|
step.Cleanup(state)
|
2013-12-24 13:22:22 -05:00
|
|
|
if _, err := os.Stat(dir.dir); err == nil {
|
2013-12-24 13:21:02 -05:00
|
|
|
t.Fatal("directory should not exist")
|
|
|
|
}
|
|
|
|
}
|