github.com/mitchellh/packer@v1.3.2/builder/vmware/vmx/step_clone_vmx_test.go (about)

     1  package vmx
     2  
     3  import (
     4  	"context"
     5  	"fmt"
     6  	"io/ioutil"
     7  	"os"
     8  	"path/filepath"
     9  	"testing"
    10  
    11  	vmwcommon "github.com/hashicorp/packer/builder/vmware/common"
    12  	"github.com/hashicorp/packer/helper/multistep"
    13  	"github.com/stretchr/testify/assert"
    14  )
    15  
    16  const (
    17  	scsiFilename = "scsiDisk.vmdk"
    18  	sataFilename = "sataDisk.vmdk"
    19  	nvmeFilename = "nvmeDisk.vmdk"
    20  	ideFilename  = "ideDisk.vmdk"
    21  )
    22  
    23  func TestStepCloneVMX_impl(t *testing.T) {
    24  	var _ multistep.Step = new(StepCloneVMX)
    25  }
    26  
    27  func TestStepCloneVMX(t *testing.T) {
    28  	// Setup some state
    29  	td, err := ioutil.TempDir("", "packer")
    30  	if err != nil {
    31  		t.Fatalf("err: %s", err)
    32  	}
    33  	defer os.RemoveAll(td)
    34  
    35  	// Set up mock vmx file contents
    36  	var testCloneVMX = fmt.Sprintf("scsi0:0.filename = \"%s\"\n"+
    37  		"sata0:0.filename = \"%s\"\n"+
    38  		"nvme0:0.filename = \"%s\"\n"+
    39  		"ide1:0.filename = \"%s\"\n"+
    40  		"ide0:0.filename = \"auto detect\"\n"+
    41  		"ethernet0.connectiontype = \"nat\"\n", scsiFilename,
    42  		sataFilename, nvmeFilename, ideFilename)
    43  
    44  	// Set up expected mock disk file paths
    45  	diskFilenames := []string{scsiFilename, sataFilename, ideFilename, nvmeFilename}
    46  	var diskFullPaths []string
    47  	for _, diskFilename := range diskFilenames {
    48  		diskFullPaths = append(diskFullPaths, filepath.Join(td, diskFilename))
    49  	}
    50  
    51  	// Create the source
    52  	sourcePath := filepath.Join(td, "source.vmx")
    53  	if err := ioutil.WriteFile(sourcePath, []byte(testCloneVMX), 0644); err != nil {
    54  		t.Fatalf("err: %s", err)
    55  	}
    56  
    57  	// Create the dest because the mock driver won't
    58  	destPath := filepath.Join(td, "foo.vmx")
    59  	if err := ioutil.WriteFile(destPath, []byte(testCloneVMX), 0644); err != nil {
    60  		t.Fatalf("err: %s", err)
    61  	}
    62  
    63  	state := testState(t)
    64  	step := new(StepCloneVMX)
    65  	step.OutputDir = td
    66  	step.Path = sourcePath
    67  	step.VMName = "foo"
    68  
    69  	driver := state.Get("driver").(*vmwcommon.DriverMock)
    70  
    71  	// Test the run
    72  	if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
    73  		t.Fatalf("bad action: %#v", action)
    74  	}
    75  	if _, ok := state.GetOk("error"); ok {
    76  		t.Fatal("should NOT have error")
    77  	}
    78  
    79  	// Test we cloned
    80  	if !driver.CloneCalled {
    81  		t.Fatal("should call clone")
    82  	}
    83  
    84  	// Test that we have our paths
    85  	if vmxPath, ok := state.GetOk("vmx_path"); !ok {
    86  		t.Fatal("should set vmx_path")
    87  	} else if vmxPath != destPath {
    88  		t.Fatalf("bad path to vmx: %#v", vmxPath)
    89  	}
    90  
    91  	if stateDiskPaths, ok := state.GetOk("disk_full_paths"); !ok {
    92  		t.Fatal("should set disk_full_paths")
    93  	} else {
    94  		assert.ElementsMatchf(t, stateDiskPaths.([]string), diskFullPaths,
    95  			"%s\nshould contain the same elements as:\n%s", stateDiskPaths.([]string), diskFullPaths)
    96  	}
    97  
    98  	// Test we got the network type
    99  	if networkType, ok := state.GetOk("vmnetwork"); !ok {
   100  		t.Fatal("should set vmnetwork")
   101  	} else if networkType != "nat" {
   102  		t.Fatalf("bad network type: %#v", networkType)
   103  	}
   104  }