github.com/mmcquillan/packer@v1.1.1-0.20171009221028-c85cf0483a5d/builder/vmware/common/step_clean_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 TestStepCleanVMX_impl(t *testing.T) {
    12  	var _ multistep.Step = new(StepCleanVMX)
    13  }
    14  
    15  func TestStepCleanVMX(t *testing.T) {
    16  	state := testState(t)
    17  	step := new(StepCleanVMX)
    18  
    19  	vmxPath := testVMXFile(t)
    20  	defer os.Remove(vmxPath)
    21  	state.Put("vmx_path", vmxPath)
    22  
    23  	// Test the run
    24  	if action := step.Run(state); action != multistep.ActionContinue {
    25  		t.Fatalf("bad action: %#v", action)
    26  	}
    27  	if _, ok := state.GetOk("error"); ok {
    28  		t.Fatal("should NOT have error")
    29  	}
    30  }
    31  
    32  func TestStepCleanVMX_floppyPath(t *testing.T) {
    33  	state := testState(t)
    34  	step := new(StepCleanVMX)
    35  
    36  	vmxPath := testVMXFile(t)
    37  	defer os.Remove(vmxPath)
    38  	if err := ioutil.WriteFile(vmxPath, []byte(testVMXFloppyPath), 0644); err != nil {
    39  		t.Fatalf("err: %s", err)
    40  	}
    41  
    42  	state.Put("vmx_path", vmxPath)
    43  
    44  	// Test the run
    45  	if action := step.Run(state); action != multistep.ActionContinue {
    46  		t.Fatalf("bad action: %#v", action)
    47  	}
    48  	if _, ok := state.GetOk("error"); ok {
    49  		t.Fatal("should NOT have error")
    50  	}
    51  
    52  	// Test the resulting data
    53  	vmxContents, err := ioutil.ReadFile(vmxPath)
    54  	if err != nil {
    55  		t.Fatalf("err: %s", err)
    56  	}
    57  	vmxData := ParseVMX(string(vmxContents))
    58  
    59  	cases := []struct {
    60  		Key   string
    61  		Value string
    62  	}{
    63  		{"floppy0.present", "FALSE"},
    64  		{"floppy0.filetype", ""},
    65  		{"floppy0.filename", ""},
    66  	}
    67  
    68  	for _, tc := range cases {
    69  		if tc.Value == "" {
    70  			if _, ok := vmxData[tc.Key]; ok {
    71  				t.Fatalf("should not have key: %s", tc.Key)
    72  			}
    73  		} else {
    74  			if vmxData[tc.Key] != tc.Value {
    75  				t.Fatalf("bad: %s %#v", tc.Key, vmxData[tc.Key])
    76  			}
    77  		}
    78  	}
    79  }
    80  
    81  func TestStepCleanVMX_isoPath(t *testing.T) {
    82  	state := testState(t)
    83  	step := new(StepCleanVMX)
    84  
    85  	vmxPath := testVMXFile(t)
    86  	defer os.Remove(vmxPath)
    87  	if err := ioutil.WriteFile(vmxPath, []byte(testVMXISOPath), 0644); err != nil {
    88  		t.Fatalf("err: %s", err)
    89  	}
    90  
    91  	state.Put("vmx_path", vmxPath)
    92  
    93  	// Test the run
    94  	if action := step.Run(state); action != multistep.ActionContinue {
    95  		t.Fatalf("bad action: %#v", action)
    96  	}
    97  	if _, ok := state.GetOk("error"); ok {
    98  		t.Fatal("should NOT have error")
    99  	}
   100  
   101  	// Test the resulting data
   102  	vmxContents, err := ioutil.ReadFile(vmxPath)
   103  	if err != nil {
   104  		t.Fatalf("err: %s", err)
   105  	}
   106  	vmxData := ParseVMX(string(vmxContents))
   107  
   108  	cases := []struct {
   109  		Key   string
   110  		Value string
   111  	}{
   112  		{"ide0:0.filename", "auto detect"},
   113  		{"ide0:0.devicetype", "cdrom-raw"},
   114  		{"ide0:1.filename", "bar"},
   115  		{"foo", "bar"},
   116  	}
   117  
   118  	for _, tc := range cases {
   119  		if tc.Value == "" {
   120  			if _, ok := vmxData[tc.Key]; ok {
   121  				t.Fatalf("should not have key: %s", tc.Key)
   122  			}
   123  		} else {
   124  			if vmxData[tc.Key] != tc.Value {
   125  				t.Fatalf("bad: %s %#v", tc.Key, vmxData[tc.Key])
   126  			}
   127  		}
   128  	}
   129  }
   130  
   131  func TestStepCleanVMX_ethernet(t *testing.T) {
   132  	state := testState(t)
   133  	step := &StepCleanVMX{
   134  		RemoveEthernetInterfaces: true,
   135  	}
   136  
   137  	vmxPath := testVMXFile(t)
   138  	defer os.Remove(vmxPath)
   139  	if err := ioutil.WriteFile(vmxPath, []byte(testVMXEthernet), 0644); err != nil {
   140  		t.Fatalf("err: %s", err)
   141  	}
   142  
   143  	state.Put("vmx_path", vmxPath)
   144  
   145  	// Test the run
   146  	if action := step.Run(state); action != multistep.ActionContinue {
   147  		t.Fatalf("bad action: %#v", action)
   148  	}
   149  	if _, ok := state.GetOk("error"); ok {
   150  		t.Fatal("should NOT have error")
   151  	}
   152  
   153  	// Test the resulting data
   154  	vmxContents, err := ioutil.ReadFile(vmxPath)
   155  	if err != nil {
   156  		t.Fatalf("err: %s", err)
   157  	}
   158  	vmxData := ParseVMX(string(vmxContents))
   159  
   160  	cases := []struct {
   161  		Key   string
   162  		Value string
   163  	}{
   164  		{"ethernet0.addresstype", ""},
   165  		{"ethernet0.bsdname", ""},
   166  		{"ethernet0.connectiontype", ""},
   167  		{"ethernet1.addresstype", ""},
   168  		{"ethernet1.bsdname", ""},
   169  		{"ethernet1.connectiontype", ""},
   170  		{"foo", "bar"},
   171  	}
   172  
   173  	for _, tc := range cases {
   174  		if tc.Value == "" {
   175  			if _, ok := vmxData[tc.Key]; ok {
   176  				t.Fatalf("should not have key: %s", tc.Key)
   177  			}
   178  		} else {
   179  			if vmxData[tc.Key] != tc.Value {
   180  				t.Fatalf("bad: %s %#v", tc.Key, vmxData[tc.Key])
   181  			}
   182  		}
   183  	}
   184  }
   185  
   186  const testVMXFloppyPath = `
   187  floppy0.present = "TRUE"
   188  floppy0.filetype = "file"
   189  `
   190  
   191  const testVMXISOPath = `
   192  ide0:0.devicetype = "cdrom-image"
   193  ide0:0.filename = "foo"
   194  ide0:1.filename = "bar"
   195  foo = "bar"
   196  `
   197  
   198  const testVMXEthernet = `
   199  ethernet0.addresstype = "generated"
   200  ethernet0.bsdname = "en0"
   201  ethernet0.connectiontype = "nat"
   202  ethernet1.addresstype = "generated"
   203  ethernet1.bsdname = "en1"
   204  ethernet1.connectiontype = "nat"
   205  foo = "bar"
   206  `