github.com/rothwerx/packer@v0.9.0/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  const testVMXFloppyPath = `
   132  floppy0.present = "TRUE"
   133  floppy0.filetype = "file"
   134  `
   135  
   136  const testVMXISOPath = `
   137  ide0:0.devicetype = "cdrom-image"
   138  ide0:0.filename = "foo"
   139  ide0:1.filename = "bar"
   140  foo = "bar"
   141  `