diff --git a/builder/vmware/vmx/step_clone_vmx.go b/builder/vmware/vmx/step_clone_vmx.go index 075df0d63..c1edcff15 100644 --- a/builder/vmware/vmx/step_clone_vmx.go +++ b/builder/vmware/vmx/step_clone_vmx.go @@ -1,6 +1,7 @@ package vmx import ( + "fmt" "log" "path/filepath" @@ -25,12 +26,25 @@ func (s *StepCloneVMX) Run(state multistep.StateBag) multistep.StepAction { ui.Say("Cloning source VM...") log.Printf("Cloning from: %s", s.Path) log.Printf("Cloning to: %s", vmxPath) - if err := driver.Clone(vmxPath, s.Path); err != nil { state.Put("error", err) return multistep.ActionHalt } + vmxData, err := vmwcommon.ReadVMX(vmxPath) + if err != nil { + state.Put("error", err) + return multistep.ActionHalt + } + + diskName, ok := vmxData["scsi0:0.filename"] + if !ok { + err := fmt.Errorf("Root disk filename could not be found!") + state.Put("error", err) + return multistep.ActionHalt + } + + state.Put("full_disk_path", filepath.Join(s.OutputDir, diskName)) state.Put("vmx_path", vmxPath) return multistep.ActionContinue } diff --git a/builder/vmware/vmx/step_clone_vmx_test.go b/builder/vmware/vmx/step_clone_vmx_test.go index 4d6532724..8b8b5a056 100644 --- a/builder/vmware/vmx/step_clone_vmx_test.go +++ b/builder/vmware/vmx/step_clone_vmx_test.go @@ -1,6 +1,9 @@ package vmx import ( + "io/ioutil" + "os" + "path/filepath" "testing" "github.com/mitchellh/multistep" @@ -12,10 +15,29 @@ func TestStepCloneVMX_impl(t *testing.T) { } func TestStepCloneVMX(t *testing.T) { + // Setup some state + td, err := ioutil.TempDir("", "packer") + if err != nil { + t.Fatalf("err: %s", err) + } + defer os.RemoveAll(td) + + // Create the source + sourcePath := filepath.Join(td, "source.vmx") + if err := ioutil.WriteFile(sourcePath, []byte(testCloneVMX), 0644); err != nil { + t.Fatalf("err: %s", err) + } + + // Create the dest because the mock driver won't + destPath := filepath.Join(td, "foo.vmx") + if err := ioutil.WriteFile(destPath, []byte(testCloneVMX), 0644); err != nil { + t.Fatalf("err: %s", err) + } + state := testState(t) step := new(StepCloneVMX) - step.OutputDir = "/foo" - step.Path = "/bar/bar.vmx" + step.OutputDir = td + step.Path = sourcePath step.VMName = "foo" driver := state.Get("driver").(*vmwcommon.DriverMock) @@ -28,7 +50,25 @@ func TestStepCloneVMX(t *testing.T) { t.Fatal("should NOT have error") } + // Test we cloned if !driver.CloneCalled { - t.Fatal("clone should be called") + t.Fatal("should call clone") + } + + // Test that we have our paths + if vmxPath, ok := state.GetOk("vmx_path"); !ok { + t.Fatal("should set vmx_path") + } else if vmxPath != destPath { + t.Fatalf("bad: %#v", vmxPath) + } + + if diskPath, ok := state.GetOk("full_disk_path"); !ok { + t.Fatal("should set full_disk_path") + } else if diskPath != filepath.Join(td, "foo") { + t.Fatalf("bad: %#v", diskPath) } } + +const testCloneVMX = ` +scsi0:0.fileName = "foo" +`