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,
},
&common.StepCreateFloppy{
Files: b.config.FloppyFiles,
Files: b.config.FloppyConfig.FloppyFiles,
Directories: b.config.FloppyConfig.FloppyDirectories,
},
&common.StepHTTPServer{
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,
},
&common.StepCreateFloppy{
Files: b.config.FloppyFiles,
Files: b.config.FloppyConfig.FloppyFiles,
Directories: b.config.FloppyConfig.FloppyDirectories,
},
&StepImport{
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),
&common.StepCreateFloppy{
Files: b.config.FloppyConfig.FloppyFiles,
Contents: b.config.FloppyConfig.FloppyContents,
Directories: b.config.FloppyConfig.FloppyDirectories,
},
new(stepCreateDisk),
new(stepCopyDisk),

View File

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

View File

@ -9,7 +9,7 @@ import (
type FloppyConfig struct {
FloppyFiles []string `mapstructure:"floppy_files"`
FloppyContents []string `mapstructure:"floppy_dirs"`
FloppyDirectories []string `mapstructure:"floppy_dirs"`
}
func (c *FloppyConfig) Prepare(ctx *interpolate.Context) []error {
@ -25,11 +25,11 @@ func (c *FloppyConfig) Prepare(ctx *interpolate.Context) []error {
}
}
if c.FloppyContents == nil {
c.FloppyContents = make([]string, 0)
if c.FloppyDirectories == nil {
c.FloppyDirectories = make([]string, 0)
}
for _, path := range c.FloppyContents {
for _, path := range c.FloppyDirectories {
if _, err := os.Stat(path); err != nil {
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.
// The floppy disk doesn't support sub-directories. Only files at the
// root level are supported.
type StepCreateFloppy struct {
Files []string
Contents []string
Directories []string
floppyPath string
@ -28,7 +26,7 @@ type StepCreateFloppy struct {
}
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.")
return multistep.ActionContinue
}
@ -167,7 +165,7 @@ func (s *StepCreateFloppy) Run(state multistep.StateBag) multistep.StepAction {
// Collect all paths (expanding wildcards) into pathqueue
ui.Message("Collecting paths from floppy_dirs")
var pathqueue []string
for _,filename := range s.Contents {
for _,filename := range s.Directories {
if strings.IndexAny(filename, "*?[") >= 0 {
matches,err := filepath.Glob(filename)
if err != nil {

View File

@ -15,6 +15,8 @@ import (
"fmt"
)
const TestFixtures = "test-fixtures"
// utility function for returning a directory structure as a list of strings
func getDirectory(path string) []string {
var result []string
@ -29,31 +31,6 @@ func getDirectory(path string) []string {
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) {
var raw interface{}
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
hierarchies := [][]string{
[]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"},
}
var basePath = filepath.Join(".", TestFixtures, TestName)
type contentsTest struct {
contents []string
dirs []string
result []string
}
// 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{contents:[]string{"file1","file2","file3"},result:[]string{"file1","file2","file3"}},
contentsTest{contents:[]string{"file?"},result:[]string{"file1","file2","file3"}},
contentsTest{contents:[]string{"*"},result:[]string{"file1","file2","file3"}},
contentsTest{dirs:[]string{"file1","file2","file3"},result:[]string{"file1","file2","file3"}},
contentsTest{dirs:[]string{"file?"},result:[]string{"file1","file2","file3"}},
contentsTest{dirs:[]string{"*"},result:[]string{"file1","file2","file3"}},
},
[]contentsTest{
contentsTest{contents:[]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{contents:[]string{"*"},result:[]string{"dir1/file1","dir1/file2","dir1/file3"}},
contentsTest{contents:[]string{"*/*"},result:[]string{"dir1/file1","dir1/file2","dir1/file3"}},
contentsTest{dirs:[]string{"dir1"},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{dirs:[]string{"*"},result:[]string{"dir1/file1","dir1/file2","dir1/file3"}},
contentsTest{dirs:[]string{"*/*"},result:[]string{"dir1/file1","dir1/file2","dir1/file3"}},
},
[]contentsTest{
contentsTest{contents:[]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{contents:[]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{"dir1"},result:[]string{"dir1/file1","dir1/subdir1/file1","dir1/subdir1/file2"}},
contentsTest{dirs:[]string{"dir2/*"},result:[]string{"dir2/subdir1/file1","dir2/subdir1/file2"}},
contentsTest{dirs:[]string{"dir2/subdir1"},result:[]string{"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
for i,hier := range hierarchies {
t.Logf("Trying with hierarchy : %v",hier)
for i := 0; i < 2; i++ {
dir := filepath.Join(basePath, fmt.Sprintf("test-%d", i))
// create the temp directory
dir, err := ioutil.TempDir("", "packer")
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
for _,test := range directories[i] {
// create a new state and step
state := testStepCreateFloppyState(t)
step := new(StepCreateFloppy)
// modify step.Contents with ones from testcase
step.Contents = []string{}
for _,c := range test.contents {
step.Contents = append(step.Contents, filepath.Join(dir,filepath.FromSlash(c)))
// modify step.Directories with ones from testcase
step.Directories = []string{}
for _,c := range test.dirs {
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
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 {
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)
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
for _,rpath := range test.result {
fpath := filepath.Join(dir, filepath.FromSlash(rpath))
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)
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
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
resulting virtual machine will be created. This may be relative or absolute.
If relative, the path is relative to the working directory when `packer`