packer-cn/builder/vmware/vmx/step_clone_vmx_test.go

111 lines
2.9 KiB
Go
Raw Normal View History

2013-12-26 10:34:27 -05:00
package vmx
import (
2018-01-22 19:03:49 -05:00
"context"
"fmt"
"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"
"github.com/hashicorp/packer/helper/multistep"
"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) {
// Setup some state
td, err := ioutil.TempDir("", "packer")
if err != nil {
t.Fatalf("err: %s", err)
}
defer os.RemoveAll(td)
// 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"+
"ide0:0.filename = \"auto detect\"\n"+
"ethernet0.connectiontype = \"nat\"\n", scsiFilename,
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))
}
// 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)
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")
}
// Test we cloned
2013-12-26 16:39:41 -05:00
if !driver.CloneCalled {
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 path to vmx: %#v", vmxPath)
}
if diskPath, ok := state.GetOk("full_disk_path"); !ok {
t.Fatal("should set full_disk_path")
} else if diskPath != diskPaths[0] {
t.Fatalf("bad disk path: %#v", diskPath)
2013-12-26 10:34:27 -05: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:])
}
// 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)
}
}