Renamed any and all instances of the word "FloppyContents" to "FloppyDirectories".

Ensure that all builders include FloppyDirectories in the StepCreateFloppy options.
Changed the way the unit-tests in common/step_create_floppy_test work to use the static test-fixtures directory instead of creating the paths dynamically.
Removed a duplicate line of documentation from parallels-pvm.html.md that occurred during rebasing.
This commit is contained in:
Ali Rizvi-Santiago 2016-09-27 23:31:42 -05:00
parent fbe305cf4e
commit 86c00490e9
20 changed files with 47 additions and 89 deletions

View File

@ -149,7 +149,8 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
Path: b.config.OutputDir, Path: b.config.OutputDir,
}, },
&common.StepCreateFloppy{ &common.StepCreateFloppy{
Files: b.config.FloppyFiles, Files: b.config.FloppyConfig.FloppyFiles,
Directories: b.config.FloppyConfig.FloppyDirectories,
}, },
&common.StepHTTPServer{ &common.StepHTTPServer{
HTTPDir: b.config.HTTPDir, HTTPDir: b.config.HTTPDir,

View File

@ -59,7 +59,8 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
Path: b.config.OutputDir, Path: b.config.OutputDir,
}, },
&common.StepCreateFloppy{ &common.StepCreateFloppy{
Files: b.config.FloppyFiles, Files: b.config.FloppyConfig.FloppyFiles,
Directories: b.config.FloppyConfig.FloppyDirectories,
}, },
&StepImport{ &StepImport{
Name: b.config.VMName, Name: b.config.VMName,

View File

@ -365,7 +365,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
steps = append(steps, new(stepPrepareOutputDir), steps = append(steps, new(stepPrepareOutputDir),
&common.StepCreateFloppy{ &common.StepCreateFloppy{
Files: b.config.FloppyConfig.FloppyFiles, Files: b.config.FloppyConfig.FloppyFiles,
Contents: b.config.FloppyConfig.FloppyContents, Directories: b.config.FloppyConfig.FloppyDirectories,
}, },
new(stepCreateDisk), new(stepCreateDisk),
new(stepCopyDisk), new(stepCopyDisk),

View File

@ -232,7 +232,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
}, },
&common.StepCreateFloppy{ &common.StepCreateFloppy{
Files: b.config.FloppyConfig.FloppyFiles, Files: b.config.FloppyConfig.FloppyFiles,
Contents: b.config.FloppyConfig.FloppyContents, Directories: b.config.FloppyConfig.FloppyDirectories,
}, },
&stepRemoteUpload{ &stepRemoteUpload{
Key: "floppy_path", Key: "floppy_path",

View File

@ -63,7 +63,7 @@ func (b *Builder) Run(ui packer.Ui, hook packer.Hook, cache packer.Cache) (packe
}, },
&common.StepCreateFloppy{ &common.StepCreateFloppy{
Files: b.config.FloppyConfig.FloppyFiles, Files: b.config.FloppyConfig.FloppyFiles,
Contents: b.config.FloppyConfig.FloppyContents, Directories: b.config.FloppyConfig.FloppyDirectories,
}, },
&StepCloneVMX{ &StepCloneVMX{
OutputDir: b.config.OutputDir, OutputDir: b.config.OutputDir,

View File

@ -9,7 +9,7 @@ import (
type FloppyConfig struct { type FloppyConfig struct {
FloppyFiles []string `mapstructure:"floppy_files"` FloppyFiles []string `mapstructure:"floppy_files"`
FloppyContents []string `mapstructure:"floppy_dirs"` FloppyDirectories []string `mapstructure:"floppy_dirs"`
} }
func (c *FloppyConfig) Prepare(ctx *interpolate.Context) []error { func (c *FloppyConfig) Prepare(ctx *interpolate.Context) []error {
@ -25,11 +25,11 @@ func (c *FloppyConfig) Prepare(ctx *interpolate.Context) []error {
} }
} }
if c.FloppyContents == nil { if c.FloppyDirectories == nil {
c.FloppyContents = make([]string, 0) c.FloppyDirectories = make([]string, 0)
} }
for _, path := range c.FloppyContents { for _, path := range c.FloppyDirectories {
if _, err := os.Stat(path); err != nil { if _, err := os.Stat(path); err != nil {
errs = append(errs, fmt.Errorf("Bad Floppy disk directory '%s': %s", path, err)) errs = append(errs, fmt.Errorf("Bad Floppy disk directory '%s': %s", path, err))
} }

View File

@ -16,11 +16,9 @@ import (
) )
// StepCreateFloppy will create a floppy disk with the given files. // StepCreateFloppy will create a floppy disk with the given files.
// The floppy disk doesn't support sub-directories. Only files at the
// root level are supported.
type StepCreateFloppy struct { type StepCreateFloppy struct {
Files []string Files []string
Contents []string Directories []string
floppyPath string floppyPath string
@ -28,7 +26,7 @@ type StepCreateFloppy struct {
} }
func (s *StepCreateFloppy) Run(state multistep.StateBag) multistep.StepAction { func (s *StepCreateFloppy) Run(state multistep.StateBag) multistep.StepAction {
if len(s.Files) == 0 && len(s.Contents) == 0 { if len(s.Files) == 0 && len(s.Directories) == 0 {
log.Println("No floppy files specified. Floppy disk will not be made.") log.Println("No floppy files specified. Floppy disk will not be made.")
return multistep.ActionContinue return multistep.ActionContinue
} }
@ -167,7 +165,7 @@ func (s *StepCreateFloppy) Run(state multistep.StateBag) multistep.StepAction {
// Collect all paths (expanding wildcards) into pathqueue // Collect all paths (expanding wildcards) into pathqueue
ui.Message("Collecting paths from floppy_dirs") ui.Message("Collecting paths from floppy_dirs")
var pathqueue []string var pathqueue []string
for _,filename := range s.Contents { for _,filename := range s.Directories {
if strings.IndexAny(filename, "*?[") >= 0 { if strings.IndexAny(filename, "*?[") >= 0 {
matches,err := filepath.Glob(filename) matches,err := filepath.Glob(filename)
if err != nil { if err != nil {

View File

@ -15,6 +15,8 @@ import (
"fmt" "fmt"
) )
const TestFixtures = "test-fixtures"
// utility function for returning a directory structure as a list of strings // utility function for returning a directory structure as a list of strings
func getDirectory(path string) []string { func getDirectory(path string) []string {
var result []string var result []string
@ -29,31 +31,6 @@ func getDirectory(path string) []string {
return result return result
} }
// utility function for creating a directory structure
type createFileContents func(string) []byte
func createDirectory(path string, hier []string, fileContents createFileContents) error {
if fileContents == nil {
fileContents = func(string) []byte {
return []byte{}
}
}
for _,filename := range hier {
p := filepath.Join(path, filename)
if strings.HasSuffix(filename, "/") {
err := os.MkdirAll(p, 0)
if err != nil { return err }
continue
}
f,err := os.Create(p)
if err != nil { return err }
_,err = f.Write(fileContents(filename))
if err != nil { return err }
err = f.Close()
if err != nil { return err }
}
return nil
}
func TestStepCreateFloppy_Impl(t *testing.T) { func TestStepCreateFloppy_Impl(t *testing.T) {
var raw interface{} var raw interface{}
raw = new(StepCreateFloppy) raw = new(StepCreateFloppy)
@ -235,86 +212,73 @@ func xxxTestStepCreateFloppy_notfound(t *testing.T) {
} }
} }
func TestStepCreateFloppyContents(t *testing.T) { func TestStepCreateFloppyDirectories(t *testing.T) {
const TestName = "floppy-hier"
// file-system hierarchies // file-system hierarchies
hierarchies := [][]string{ var basePath = filepath.Join(".", TestFixtures, TestName)
[]string{"file1", "file2", "file3"},
[]string{"dir1/", "dir1/file1", "dir1/file2", "dir1/file3"},
[]string{"dir1/", "dir1/file1", "dir1/subdir1/", "dir1/subdir1/file1", "dir1/subdir1/file2", "dir2/", "dir2/subdir1/", "dir2/subdir1/file1","dir2/subdir1/file2"},
}
type contentsTest struct { type contentsTest struct {
contents []string dirs []string
result []string result []string
} }
// keep in mind that .FilesAdded doesn't keep track of the target filename or directories, but rather the source filename. // keep in mind that .FilesAdded doesn't keep track of the target filename or directories, but rather the source filename.
contents := [][]contentsTest{ directories := [][]contentsTest{
[]contentsTest{ []contentsTest{
contentsTest{contents:[]string{"file1","file2","file3"},result:[]string{"file1","file2","file3"}}, contentsTest{dirs:[]string{"file1","file2","file3"},result:[]string{"file1","file2","file3"}},
contentsTest{contents:[]string{"file?"},result:[]string{"file1","file2","file3"}}, contentsTest{dirs:[]string{"file?"},result:[]string{"file1","file2","file3"}},
contentsTest{contents:[]string{"*"},result:[]string{"file1","file2","file3"}}, contentsTest{dirs:[]string{"*"},result:[]string{"file1","file2","file3"}},
}, },
[]contentsTest{ []contentsTest{
contentsTest{contents:[]string{"dir1"},result:[]string{"dir1/file1","dir1/file2","dir1/file3"}}, contentsTest{dirs:[]string{"dir1"},result:[]string{"dir1/file1","dir1/file2","dir1/file3"}},
contentsTest{contents:[]string{"dir1/file1","dir1/file2","dir1/file3"},result:[]string{"dir1/file1","dir1/file2","dir1/file3"}}, contentsTest{dirs:[]string{"dir1/file1","dir1/file2","dir1/file3"},result:[]string{"dir1/file1","dir1/file2","dir1/file3"}},
contentsTest{contents:[]string{"*"},result:[]string{"dir1/file1","dir1/file2","dir1/file3"}}, contentsTest{dirs:[]string{"*"},result:[]string{"dir1/file1","dir1/file2","dir1/file3"}},
contentsTest{contents:[]string{"*/*"},result:[]string{"dir1/file1","dir1/file2","dir1/file3"}}, contentsTest{dirs:[]string{"*/*"},result:[]string{"dir1/file1","dir1/file2","dir1/file3"}},
}, },
[]contentsTest{ []contentsTest{
contentsTest{contents:[]string{"dir1"},result:[]string{"dir1/file1","dir1/subdir1/file1","dir1/subdir1/file2"}}, contentsTest{dirs:[]string{"dir1"},result:[]string{"dir1/file1","dir1/subdir1/file1","dir1/subdir1/file2"}},
contentsTest{contents:[]string{"dir2/*"},result:[]string{"dir2/subdir1/file1","dir2/subdir1/file2"}}, contentsTest{dirs:[]string{"dir2/*"},result:[]string{"dir2/subdir1/file1","dir2/subdir1/file2"}},
contentsTest{contents:[]string{"dir2/subdir1"},result:[]string{"dir2/subdir1/file1","dir2/subdir1/file2"}}, contentsTest{dirs:[]string{"dir2/subdir1"},result:[]string{"dir2/subdir1/file1","dir2/subdir1/file2"}},
contentsTest{contents:[]string{"dir?"},result:[]string{"dir1/file1","dir1/subdir1/file1","dir1/subdir1/file2","dir2/subdir1/file1","dir2/subdir1/file2"}}, contentsTest{dirs:[]string{"dir?"},result:[]string{"dir1/file1","dir1/subdir1/file1","dir1/subdir1/file2","dir2/subdir1/file1","dir2/subdir1/file2"}},
}, },
} }
// create the hierarchy for each file // create the hierarchy for each file
for i,hier := range hierarchies { for i := 0; i < 2; i++ {
t.Logf("Trying with hierarchy : %v",hier) dir := filepath.Join(basePath, fmt.Sprintf("test-%d", i))
// create the temp directory for _,test := range directories[i] {
dir, err := ioutil.TempDir("", "packer") // create a new state and step
if err != nil {
t.Fatalf("err: %s", err)
}
// create the file contents
err = createDirectory(dir, hier, nil)
if err != nil { t.Fatalf("err: %s", err) }
t.Logf("Making %v", hier)
for _,test := range contents[i] {
// createa new state and step
state := testStepCreateFloppyState(t) state := testStepCreateFloppyState(t)
step := new(StepCreateFloppy) step := new(StepCreateFloppy)
// modify step.Contents with ones from testcase // modify step.Directories with ones from testcase
step.Contents = []string{} step.Directories = []string{}
for _,c := range test.contents { for _,c := range test.dirs {
step.Contents = append(step.Contents, filepath.Join(dir,filepath.FromSlash(c))) step.Directories = append(step.Directories, filepath.Join(dir,filepath.FromSlash(c)))
} }
log.Println(fmt.Sprintf("Trying against floppy_dirs : %v",step.Contents)) log.Println(fmt.Sprintf("Trying against floppy_dirs : %v",step.Directories))
// run the step // run the step
if action := step.Run(state); action != multistep.ActionContinue { if action := step.Run(state); action != multistep.ActionContinue {
t.Fatalf("bad action: %#v for %v : %v", action, step.Contents, state.Get("error")) t.Fatalf("bad action: %#v for %v : %v", action, step.Directories, state.Get("error"))
} }
if _, ok := state.GetOk("error"); ok { if _, ok := state.GetOk("error"); ok {
t.Fatalf("state should be ok for %v : %v", step.Contents, state.Get("error")) t.Fatalf("state should be ok for %v : %v", step.Directories, state.Get("error"))
} }
floppy_path := state.Get("floppy_path").(string) floppy_path := state.Get("floppy_path").(string)
if _, err := os.Stat(floppy_path); err != nil { if _, err := os.Stat(floppy_path); err != nil {
t.Fatalf("file not found: %s for %v : %v", floppy_path, step.Contents, err) t.Fatalf("file not found: %s for %v : %v", floppy_path, step.Directories, err)
} }
// check the FilesAdded array to see if it matches // check the FilesAdded array to see if it matches
for _,rpath := range test.result { for _,rpath := range test.result {
fpath := filepath.Join(dir, filepath.FromSlash(rpath)) fpath := filepath.Join(dir, filepath.FromSlash(rpath))
if !step.FilesAdded[fpath] { if !step.FilesAdded[fpath] {
t.Fatalf("unable to find file: %s for %v", fpath, step.Contents) t.Fatalf("unable to find file: %s for %v", fpath, step.Directories)
} }
} }
@ -322,10 +286,8 @@ func TestStepCreateFloppyContents(t *testing.T) {
step.Cleanup(state) step.Cleanup(state)
if _, err := os.Stat(floppy_path); err == nil { if _, err := os.Stat(floppy_path); err == nil {
t.Fatalf("file found: %s for %v", floppy_path, step.Contents) t.Fatalf("file found: %s for %v", floppy_path, step.Directories)
} }
} }
// remove the temp directory
os.RemoveAll(dir)
} }
} }

View File

@ -92,10 +92,6 @@ builder.
your floppy disk includes drivers or if you just want to organize it's your floppy disk includes drivers or if you just want to organize it's
contents as a hierarchy. Wildcard characters (\*, ?, and \[\]) are allowed. contents as a hierarchy. Wildcard characters (\*, ?, and \[\]) are allowed.
- `reassign_mac` (boolean) - If this is "false" the MAC address of the first
NIC will reused when imported else a new MAC address will be generated
by Parallels. Defaults to "false".
- `output_directory` (string) - This is the path to the directory where the - `output_directory` (string) - This is the path to the directory where the
resulting virtual machine will be created. This may be relative or absolute. resulting virtual machine will be created. This may be relative or absolute.
If relative, the path is relative to the working directory when `packer` If relative, the path is relative to the working directory when `packer`