2013-12-26 10:34:27 -05:00
|
|
|
package vmx
|
|
|
|
|
|
|
|
import (
|
2018-01-22 19:03:49 -05:00
|
|
|
"context"
|
2018-03-30 17:00:21 -04:00
|
|
|
"fmt"
|
2013-12-26 17:44:39 -05:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2013-12-26 10:34:27 -05:00
|
|
|
"testing"
|
|
|
|
|
2017-04-04 16:39:01 -04:00
|
|
|
vmwcommon "github.com/hashicorp/packer/builder/vmware/common"
|
2018-01-19 19:18:44 -05:00
|
|
|
"github.com/hashicorp/packer/helper/multistep"
|
2018-03-30 17:00:21 -04:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
scsiFilename = "scsiDisk.vmdk"
|
|
|
|
sataFilename = "sataDisk.vmdk"
|
|
|
|
nvmeFilename = "nvmeDisk.vmdk"
|
|
|
|
ideFilename = "ideDisk.vmdk"
|
2013-12-26 10:34:27 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestStepCloneVMX_impl(t *testing.T) {
|
|
|
|
var _ multistep.Step = new(StepCloneVMX)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStepCloneVMX(t *testing.T) {
|
2013-12-26 17:44:39 -05:00
|
|
|
// Setup some state
|
|
|
|
td, err := ioutil.TempDir("", "packer")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
defer os.RemoveAll(td)
|
|
|
|
|
2018-03-30 17:00:21 -04:00
|
|
|
// Set up mock vmx file contents
|
|
|
|
var testCloneVMX = fmt.Sprintf("scsi0:0.filename = \"%s\"\n"+
|
|
|
|
"sata0:0.filename = \"%s\"\n"+
|
|
|
|
"nvme0:0.filename = \"%s\"\n"+
|
|
|
|
"ide1:0.filename = \"%s\"\n"+
|
2018-03-30 17:44:14 -04:00
|
|
|
"ide0:0.filename = \"auto detect\"\n"+
|
|
|
|
"ethernet0.connectiontype = \"nat\"\n", scsiFilename,
|
2018-03-30 17:00:21 -04:00
|
|
|
sataFilename, nvmeFilename, ideFilename)
|
|
|
|
|
|
|
|
// Set up expected mock disk file paths
|
|
|
|
diskFilenames := []string{scsiFilename, sataFilename, ideFilename, nvmeFilename}
|
|
|
|
var diskPaths []string
|
|
|
|
for _, diskFilename := range diskFilenames {
|
|
|
|
diskPaths = append(diskPaths, filepath.Join(td, diskFilename))
|
|
|
|
}
|
|
|
|
|
2013-12-26 17:44:39 -05:00
|
|
|
// 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)
|
|
|
|
}
|
|
|
|
|
2013-12-26 10:34:27 -05:00
|
|
|
state := testState(t)
|
|
|
|
step := new(StepCloneVMX)
|
2013-12-26 17:44:39 -05:00
|
|
|
step.OutputDir = td
|
|
|
|
step.Path = sourcePath
|
2013-12-26 10:34:27 -05:00
|
|
|
step.VMName = "foo"
|
|
|
|
|
2013-12-26 16:39:41 -05:00
|
|
|
driver := state.Get("driver").(*vmwcommon.DriverMock)
|
|
|
|
|
2013-12-26 10:34:27 -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-26 10:34:27 -05:00
|
|
|
t.Fatalf("bad action: %#v", action)
|
|
|
|
}
|
|
|
|
if _, ok := state.GetOk("error"); ok {
|
|
|
|
t.Fatal("should NOT have error")
|
|
|
|
}
|
|
|
|
|
2013-12-26 17:44:39 -05:00
|
|
|
// Test we cloned
|
2013-12-26 16:39:41 -05:00
|
|
|
if !driver.CloneCalled {
|
2013-12-26 17:44:39 -05:00
|
|
|
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 {
|
2018-03-30 17:44:14 -04:00
|
|
|
t.Fatalf("bad path to vmx: %#v", vmxPath)
|
2013-12-26 17:44:39 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
if diskPath, ok := state.GetOk("full_disk_path"); !ok {
|
|
|
|
t.Fatal("should set full_disk_path")
|
2018-03-30 17:00:21 -04:00
|
|
|
} else if diskPath != diskPaths[0] {
|
2018-03-30 17:44:14 -04:00
|
|
|
t.Fatalf("bad disk path: %#v", diskPath)
|
2013-12-26 10:34:27 -05:00
|
|
|
}
|
2013-12-26 17:44:39 -05:00
|
|
|
|
2018-03-30 17:00:21 -04:00
|
|
|
if stateDiskPaths, ok := state.GetOk("additional_disk_paths"); !ok {
|
|
|
|
t.Fatal("should set additional_disk_paths")
|
|
|
|
} else {
|
|
|
|
assert.ElementsMatchf(t, stateDiskPaths.([]string), diskPaths[1:],
|
|
|
|
"%s\nshould contain the same elements as:\n%s", stateDiskPaths.([]string), diskPaths[1:])
|
|
|
|
}
|
2018-03-30 17:44:14 -04:00
|
|
|
|
|
|
|
// Test we got the network type
|
|
|
|
if networkType, ok := state.GetOk("vmnetwork"); !ok {
|
|
|
|
t.Fatal("should set vmnetwork")
|
|
|
|
} else if networkType != "nat" {
|
|
|
|
t.Fatalf("bad network type: %#v", networkType)
|
|
|
|
}
|
2018-03-30 17:00:21 -04:00
|
|
|
}
|