github.com/sneal/packer@v0.5.2/builder/vmware/common/step_configure_vmx_test.go (about)

     1  package common
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"testing"
     7  
     8  	"github.com/mitchellh/multistep"
     9  )
    10  
    11  func testVMXFile(t *testing.T) string {
    12  	tf, err := ioutil.TempFile("", "packer")
    13  	if err != nil {
    14  		t.Fatalf("err: %s", err)
    15  	}
    16  	tf.Close()
    17  
    18  	return tf.Name()
    19  }
    20  
    21  func TestStepConfigureVMX_impl(t *testing.T) {
    22  	var _ multistep.Step = new(StepConfigureVMX)
    23  }
    24  
    25  func TestStepConfigureVMX(t *testing.T) {
    26  	state := testState(t)
    27  	step := new(StepConfigureVMX)
    28  	step.CustomData = map[string]string{
    29  		"foo": "bar",
    30  	}
    31  
    32  	vmxPath := testVMXFile(t)
    33  	defer os.Remove(vmxPath)
    34  	state.Put("vmx_path", vmxPath)
    35  
    36  	// Test the run
    37  	if action := step.Run(state); action != multistep.ActionContinue {
    38  		t.Fatalf("bad action: %#v", action)
    39  	}
    40  	if _, ok := state.GetOk("error"); ok {
    41  		t.Fatal("should NOT have error")
    42  	}
    43  
    44  	// Test the resulting data
    45  	vmxContents, err := ioutil.ReadFile(vmxPath)
    46  	if err != nil {
    47  		t.Fatalf("err: %s", err)
    48  	}
    49  	vmxData := ParseVMX(string(vmxContents))
    50  
    51  	cases := []struct {
    52  		Key   string
    53  		Value string
    54  	}{
    55  		// Stuff we set
    56  		{"msg.autoanswer", "true"},
    57  		{"uuid.action", "create"},
    58  
    59  		// Custom data
    60  		{"foo", "bar"},
    61  
    62  		// Stuff that should NOT exist
    63  		{"floppy0.present", ""},
    64  	}
    65  
    66  	for _, tc := range cases {
    67  		if tc.Value == "" {
    68  			if _, ok := vmxData[tc.Key]; ok {
    69  				t.Fatalf("should not have key: %s", tc.Key)
    70  			}
    71  		} else {
    72  			if vmxData[tc.Key] != tc.Value {
    73  				t.Fatalf("bad: %s %#v", tc.Key, vmxData[tc.Key])
    74  			}
    75  		}
    76  	}
    77  }
    78  
    79  func TestStepConfigureVMX_floppyPath(t *testing.T) {
    80  	state := testState(t)
    81  	step := new(StepConfigureVMX)
    82  
    83  	vmxPath := testVMXFile(t)
    84  	defer os.Remove(vmxPath)
    85  
    86  	state.Put("floppy_path", "foo")
    87  	state.Put("vmx_path", vmxPath)
    88  
    89  	// Test the run
    90  	if action := step.Run(state); action != multistep.ActionContinue {
    91  		t.Fatalf("bad action: %#v", action)
    92  	}
    93  	if _, ok := state.GetOk("error"); ok {
    94  		t.Fatal("should NOT have error")
    95  	}
    96  
    97  	// Test the resulting data
    98  	vmxContents, err := ioutil.ReadFile(vmxPath)
    99  	if err != nil {
   100  		t.Fatalf("err: %s", err)
   101  	}
   102  	vmxData := ParseVMX(string(vmxContents))
   103  
   104  	cases := []struct {
   105  		Key   string
   106  		Value string
   107  	}{
   108  		{"floppy0.present", "TRUE"},
   109  		{"floppy0.filetype", "file"},
   110  		{"floppy0.filename", "foo"},
   111  	}
   112  
   113  	for _, tc := range cases {
   114  		if tc.Value == "" {
   115  			if _, ok := vmxData[tc.Key]; ok {
   116  				t.Fatalf("should not have key: %s", tc.Key)
   117  			}
   118  		} else {
   119  			if vmxData[tc.Key] != tc.Value {
   120  				t.Fatalf("bad: %s %#v", tc.Key, vmxData[tc.Key])
   121  			}
   122  		}
   123  	}
   124  
   125  }
   126  
   127  func TestStepConfigureVMX_generatedAddresses(t *testing.T) {
   128  	state := testState(t)
   129  	step := new(StepConfigureVMX)
   130  
   131  	vmxPath := testVMXFile(t)
   132  	defer os.Remove(vmxPath)
   133  
   134  	err := WriteVMX(vmxPath, map[string]string{
   135  		"foo": "bar",
   136  		"ethernet0.generatedAddress":       "foo",
   137  		"ethernet1.generatedAddress":       "foo",
   138  		"ethernet1.generatedAddressOffset": "foo",
   139  	})
   140  	if err != nil {
   141  		t.Fatalf("err: %s", err)
   142  	}
   143  
   144  	state.Put("vmx_path", vmxPath)
   145  
   146  	// Test the run
   147  	if action := step.Run(state); action != multistep.ActionContinue {
   148  		t.Fatalf("bad action: %#v", action)
   149  	}
   150  	if _, ok := state.GetOk("error"); ok {
   151  		t.Fatal("should NOT have error")
   152  	}
   153  
   154  	// Test the resulting data
   155  	vmxContents, err := ioutil.ReadFile(vmxPath)
   156  	if err != nil {
   157  		t.Fatalf("err: %s", err)
   158  	}
   159  	vmxData := ParseVMX(string(vmxContents))
   160  
   161  	cases := []struct {
   162  		Key   string
   163  		Value string
   164  	}{
   165  		{"foo", "bar"},
   166  		{"ethernet0.generatedaddress", ""},
   167  		{"ethernet1.generatedaddress", ""},
   168  		{"ethernet1.generatedaddressoffset", ""},
   169  	}
   170  
   171  	for _, tc := range cases {
   172  		if tc.Value == "" {
   173  			if _, ok := vmxData[tc.Key]; ok {
   174  				t.Fatalf("should not have key: %s", tc.Key)
   175  			}
   176  		} else {
   177  			if vmxData[tc.Key] != tc.Value {
   178  				t.Fatalf("bad: %s %#v", tc.Key, vmxData[tc.Key])
   179  			}
   180  		}
   181  	}
   182  
   183  }