2013-12-24 13:21:02 -05:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2018-01-22 19:03:49 -05:00
|
|
|
"context"
|
2013-12-24 13:21:02 -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"
|
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)
|
2018-04-25 12:07:38 -04:00
|
|
|
// Delete the test output directory when done
|
|
|
|
defer os.RemoveAll(dir.dir)
|
2013-12-24 13:21:02 -05:00
|
|
|
state.Put("dir", dir)
|
|
|
|
|
|
|
|
// Test the run
|
2018-01-22 19:03:49 -05:00
|
|
|
if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
|
2013-12-24 13:21:02 -05:00
|
|
|
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)
|
|
|
|
}
|
2018-04-25 12:07:38 -04:00
|
|
|
defer os.RemoveAll(dir.dir)
|
2013-12-24 13:21:02 -05:00
|
|
|
|
|
|
|
// Test the run
|
2018-01-22 19:03:49 -05:00
|
|
|
if action := step.Run(context.Background(), state); action != multistep.ActionHalt {
|
2013-12-24 13:21:02 -05:00
|
|
|
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)
|
|
|
|
}
|
2018-04-25 12:07:38 -04:00
|
|
|
defer os.RemoveAll(dir.dir)
|
2013-12-24 13:21:02 -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-24 13:21:02 -05:00
|
|
|
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
|
2018-01-22 19:03:49 -05:00
|
|
|
if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
|
2013-12-24 13:21:02 -05:00
|
|
|
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
|
2018-01-22 19:03:49 -05:00
|
|
|
if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
|
2013-12-24 13:21:02 -05:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|