github.com/mitchellh/packer@v1.3.2/builder/vmware/common/step_clean_vmx_test.go (about)

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