2014-04-29 15:27:34 -04:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-01-22 19:03:49 -05:00
|
|
|
"context"
|
2016-10-11 17:43:50 -04:00
|
|
|
"fmt"
|
2014-04-29 15:27:34 -04:00
|
|
|
"io/ioutil"
|
2016-10-11 17:43:50 -04:00
|
|
|
"log"
|
2014-04-29 15:27:34 -04:00
|
|
|
"os"
|
|
|
|
"path"
|
2016-03-05 02:40:16 -05:00
|
|
|
"path/filepath"
|
2014-04-29 15:27:34 -04:00
|
|
|
"strconv"
|
2016-10-11 17:43:50 -04:00
|
|
|
"testing"
|
2018-01-22 20:21:10 -05:00
|
|
|
|
2018-01-22 18:32:33 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2018-01-22 20:21:10 -05:00
|
|
|
"github.com/hashicorp/packer/packer"
|
2014-04-29 15:27:34 -04:00
|
|
|
)
|
|
|
|
|
2016-09-28 00:31:42 -04:00
|
|
|
const TestFixtures = "test-fixtures"
|
|
|
|
|
2014-04-29 15:27:34 -04:00
|
|
|
func TestStepCreateFloppy_Impl(t *testing.T) {
|
|
|
|
var raw interface{}
|
|
|
|
raw = new(StepCreateFloppy)
|
|
|
|
if _, ok := raw.(multistep.Step); !ok {
|
|
|
|
t.Fatalf("StepCreateFloppy should be a step")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func testStepCreateFloppyState(t *testing.T) multistep.StateBag {
|
|
|
|
state := new(multistep.BasicStateBag)
|
|
|
|
state.Put("ui", &packer.BasicUi{
|
|
|
|
Reader: new(bytes.Buffer),
|
|
|
|
Writer: new(bytes.Buffer),
|
|
|
|
})
|
|
|
|
return state
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStepCreateFloppy(t *testing.T) {
|
|
|
|
state := testStepCreateFloppyState(t)
|
|
|
|
step := new(StepCreateFloppy)
|
|
|
|
|
|
|
|
dir, err := ioutil.TempDir("", "packer")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
|
|
count := 10
|
|
|
|
expected := count
|
|
|
|
files := make([]string, count)
|
|
|
|
|
|
|
|
prefix := "exists"
|
|
|
|
ext := ".tmp"
|
|
|
|
|
|
|
|
for i := 0; i < expected; i++ {
|
2014-04-29 15:29:15 -04:00
|
|
|
files[i] = path.Join(dir, prefix+strconv.Itoa(i)+ext)
|
2014-04-29 15:27:34 -04:00
|
|
|
|
|
|
|
_, err := os.Create(files[i])
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
lists := [][]string{
|
|
|
|
files,
|
2016-11-01 17:08:04 -04:00
|
|
|
{dir + string(os.PathSeparator) + prefix + "*" + ext},
|
|
|
|
{dir + string(os.PathSeparator) + prefix + "?" + ext},
|
|
|
|
{dir + string(os.PathSeparator) + prefix + "[0123456789]" + ext},
|
|
|
|
{dir + string(os.PathSeparator) + prefix + "[0-9]" + ext},
|
|
|
|
{dir + string(os.PathSeparator)},
|
|
|
|
{dir},
|
2014-04-29 15:27:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, step.Files = range lists {
|
2018-01-22 19:03:49 -05:00
|
|
|
if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
|
2014-04-29 15:27:34 -04:00
|
|
|
t.Fatalf("bad action: %#v for %v", action, step.Files)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := state.GetOk("error"); ok {
|
|
|
|
t.Fatalf("state should be ok for %v", step.Files)
|
|
|
|
}
|
|
|
|
|
|
|
|
floppy_path := state.Get("floppy_path").(string)
|
|
|
|
|
|
|
|
if _, err := os.Stat(floppy_path); err != nil {
|
2014-12-15 13:51:55 -05:00
|
|
|
t.Fatalf("file not found: %s for %v", floppy_path, step.Files)
|
2014-04-29 15:27:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(step.FilesAdded) != expected {
|
|
|
|
t.Fatalf("expected %d, found %d for %v", expected, len(step.FilesAdded), step.Files)
|
|
|
|
}
|
|
|
|
|
|
|
|
step.Cleanup(state)
|
|
|
|
|
|
|
|
if _, err := os.Stat(floppy_path); err == nil {
|
2014-12-15 13:51:55 -05:00
|
|
|
t.Fatalf("file found: %s for %v", floppy_path, step.Files)
|
2014-04-29 15:27:34 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-02 13:12:31 -04:00
|
|
|
func TestStepCreateFloppy_missing(t *testing.T) {
|
2014-04-29 15:27:34 -04:00
|
|
|
state := testStepCreateFloppyState(t)
|
|
|
|
step := new(StepCreateFloppy)
|
|
|
|
|
|
|
|
dir, err := ioutil.TempDir("", "packer")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
|
|
count := 2
|
|
|
|
expected := 0
|
|
|
|
files := make([]string, count)
|
|
|
|
|
|
|
|
prefix := "missing"
|
|
|
|
|
|
|
|
for i := 0; i < count; i++ {
|
2014-04-29 15:29:15 -04:00
|
|
|
files[i] = path.Join(dir, prefix+strconv.Itoa(i))
|
2014-04-29 15:27:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
lists := [][]string{
|
|
|
|
files,
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, step.Files = range lists {
|
2018-01-22 19:03:49 -05:00
|
|
|
if action := step.Run(context.Background(), state); action != multistep.ActionHalt {
|
2014-04-29 15:27:34 -04:00
|
|
|
t.Fatalf("bad action: %#v for %v", action, step.Files)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := state.GetOk("error"); !ok {
|
|
|
|
t.Fatalf("state should not be ok for %v", step.Files)
|
|
|
|
}
|
|
|
|
|
|
|
|
floppy_path := state.Get("floppy_path")
|
|
|
|
|
|
|
|
if floppy_path != nil {
|
|
|
|
t.Fatalf("floppy_path is not nil for %v", step.Files)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(step.FilesAdded) != expected {
|
|
|
|
t.Fatalf("expected %d, found %d for %v", expected, len(step.FilesAdded), step.Files)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-02 13:10:36 -04:00
|
|
|
func TestStepCreateFloppy_notfound(t *testing.T) {
|
2014-04-29 15:27:34 -04:00
|
|
|
state := testStepCreateFloppyState(t)
|
|
|
|
step := new(StepCreateFloppy)
|
|
|
|
|
|
|
|
dir, err := ioutil.TempDir("", "packer")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(dir)
|
|
|
|
|
|
|
|
count := 2
|
|
|
|
expected := 0
|
|
|
|
files := make([]string, count)
|
|
|
|
|
|
|
|
prefix := "notfound"
|
|
|
|
|
|
|
|
for i := 0; i < count; i++ {
|
2014-04-29 15:29:15 -04:00
|
|
|
files[i] = path.Join(dir, prefix+strconv.Itoa(i))
|
2014-04-29 15:27:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
lists := [][]string{
|
2016-11-01 17:08:04 -04:00
|
|
|
{dir + string(os.PathSeparator) + prefix + "*"},
|
|
|
|
{dir + string(os.PathSeparator) + prefix + "?"},
|
|
|
|
{dir + string(os.PathSeparator) + prefix + "[0123456789]"},
|
|
|
|
{dir + string(os.PathSeparator) + prefix + "[0-9]"},
|
|
|
|
{dir + string(os.PathSeparator)},
|
|
|
|
{dir},
|
2014-04-29 15:27:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, step.Files = range lists {
|
2018-01-22 19:03:49 -05:00
|
|
|
if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
|
2014-04-29 15:27:34 -04:00
|
|
|
t.Fatalf("bad action: %#v for %v", action, step.Files)
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := state.GetOk("error"); ok {
|
|
|
|
t.Fatalf("state should be ok for %v", step.Files)
|
|
|
|
}
|
|
|
|
|
|
|
|
floppy_path := state.Get("floppy_path").(string)
|
|
|
|
|
|
|
|
if _, err := os.Stat(floppy_path); err != nil {
|
2014-12-15 13:51:55 -05:00
|
|
|
t.Fatalf("file not found: %s for %v", floppy_path, step.Files)
|
2014-04-29 15:27:34 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if len(step.FilesAdded) != expected {
|
|
|
|
t.Fatalf("expected %d, found %d for %v", expected, len(step.FilesAdded), step.Files)
|
|
|
|
}
|
|
|
|
|
|
|
|
step.Cleanup(state)
|
|
|
|
|
|
|
|
if _, err := os.Stat(floppy_path); err == nil {
|
2014-12-15 13:51:55 -05:00
|
|
|
t.Fatalf("file found: %s for %v", floppy_path, step.Files)
|
2014-04-29 15:27:34 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2016-03-05 02:40:16 -05:00
|
|
|
|
2016-09-28 00:31:42 -04:00
|
|
|
func TestStepCreateFloppyDirectories(t *testing.T) {
|
|
|
|
const TestName = "floppy-hier"
|
|
|
|
|
2016-03-05 02:40:16 -05:00
|
|
|
// file-system hierarchies
|
2016-09-28 00:31:42 -04:00
|
|
|
var basePath = filepath.Join(".", TestFixtures, TestName)
|
2016-03-05 02:40:16 -05:00
|
|
|
|
|
|
|
type contentsTest struct {
|
2016-10-11 17:43:50 -04:00
|
|
|
dirs []string
|
2016-03-05 02:40:16 -05:00
|
|
|
result []string
|
|
|
|
}
|
|
|
|
|
|
|
|
// keep in mind that .FilesAdded doesn't keep track of the target filename or directories, but rather the source filename.
|
2016-09-28 00:31:42 -04:00
|
|
|
directories := [][]contentsTest{
|
2016-11-01 17:08:04 -04:00
|
|
|
{
|
|
|
|
{dirs: []string{"file1", "file2", "file3"}, result: []string{"file1", "file2", "file3"}},
|
|
|
|
{dirs: []string{"file?"}, result: []string{"file1", "file2", "file3"}},
|
|
|
|
{dirs: []string{"*"}, result: []string{"file1", "file2", "file3"}},
|
2016-03-05 02:40:16 -05:00
|
|
|
},
|
2016-11-01 17:08:04 -04:00
|
|
|
{
|
|
|
|
{dirs: []string{"dir1"}, result: []string{"dir1/file1", "dir1/file2", "dir1/file3"}},
|
|
|
|
{dirs: []string{"dir1/file1", "dir1/file2", "dir1/file3"}, result: []string{"dir1/file1", "dir1/file2", "dir1/file3"}},
|
|
|
|
{dirs: []string{"*"}, result: []string{"dir1/file1", "dir1/file2", "dir1/file3"}},
|
|
|
|
{dirs: []string{"*/*"}, result: []string{"dir1/file1", "dir1/file2", "dir1/file3"}},
|
2016-03-05 02:40:16 -05:00
|
|
|
},
|
2016-11-01 17:08:04 -04:00
|
|
|
{
|
|
|
|
{dirs: []string{"dir1"}, result: []string{"dir1/file1", "dir1/subdir1/file1", "dir1/subdir1/file2"}},
|
|
|
|
{dirs: []string{"dir2/*"}, result: []string{"dir2/subdir1/file1", "dir2/subdir1/file2"}},
|
|
|
|
{dirs: []string{"dir2/subdir1"}, result: []string{"dir2/subdir1/file1", "dir2/subdir1/file2"}},
|
|
|
|
{dirs: []string{"dir?"}, result: []string{"dir1/file1", "dir1/subdir1/file1", "dir1/subdir1/file2", "dir2/subdir1/file1", "dir2/subdir1/file2"}},
|
2016-03-05 02:40:16 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
// create the hierarchy for each file
|
2016-09-28 00:31:42 -04:00
|
|
|
for i := 0; i < 2; i++ {
|
|
|
|
dir := filepath.Join(basePath, fmt.Sprintf("test-%d", i))
|
2016-03-05 02:40:16 -05:00
|
|
|
|
2016-10-11 17:43:50 -04:00
|
|
|
for _, test := range directories[i] {
|
2016-09-28 00:31:42 -04:00
|
|
|
// create a new state and step
|
2016-03-05 02:40:16 -05:00
|
|
|
state := testStepCreateFloppyState(t)
|
|
|
|
step := new(StepCreateFloppy)
|
|
|
|
|
2016-09-28 00:31:42 -04:00
|
|
|
// modify step.Directories with ones from testcase
|
|
|
|
step.Directories = []string{}
|
2016-10-11 17:43:50 -04:00
|
|
|
for _, c := range test.dirs {
|
|
|
|
step.Directories = append(step.Directories, filepath.Join(dir, filepath.FromSlash(c)))
|
2016-03-05 02:40:16 -05:00
|
|
|
}
|
2016-10-11 17:43:50 -04:00
|
|
|
log.Println(fmt.Sprintf("Trying against floppy_dirs : %v", step.Directories))
|
2016-03-05 02:40:16 -05:00
|
|
|
|
|
|
|
// run the step
|
2018-01-22 19:03:49 -05:00
|
|
|
if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
|
2016-09-28 00:31:42 -04:00
|
|
|
t.Fatalf("bad action: %#v for %v : %v", action, step.Directories, state.Get("error"))
|
2016-03-05 02:40:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if _, ok := state.GetOk("error"); ok {
|
2016-09-28 00:31:42 -04:00
|
|
|
t.Fatalf("state should be ok for %v : %v", step.Directories, state.Get("error"))
|
2016-03-05 02:40:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
floppy_path := state.Get("floppy_path").(string)
|
|
|
|
if _, err := os.Stat(floppy_path); err != nil {
|
2016-09-28 00:31:42 -04:00
|
|
|
t.Fatalf("file not found: %s for %v : %v", floppy_path, step.Directories, err)
|
2016-03-05 02:40:16 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// check the FilesAdded array to see if it matches
|
2016-10-11 17:43:50 -04:00
|
|
|
for _, rpath := range test.result {
|
2016-03-05 02:40:16 -05:00
|
|
|
fpath := filepath.Join(dir, filepath.FromSlash(rpath))
|
|
|
|
if !step.FilesAdded[fpath] {
|
2016-09-28 00:31:42 -04:00
|
|
|
t.Fatalf("unable to find file: %s for %v", fpath, step.Directories)
|
2016-03-05 02:40:16 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// cleanup the step
|
|
|
|
step.Cleanup(state)
|
|
|
|
|
|
|
|
if _, err := os.Stat(floppy_path); err == nil {
|
2016-09-28 00:31:42 -04:00
|
|
|
t.Fatalf("file found: %s for %v", floppy_path, step.Directories)
|
2016-03-05 02:40:16 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|