github.com/sneal/packer@v0.5.2/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("floppy_path", "foo") 43 state.Put("vmx_path", vmxPath) 44 45 // Test the run 46 if action := step.Run(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("iso_path", "foo") 93 state.Put("vmx_path", vmxPath) 94 95 // Test the run 96 if action := step.Run(state); action != multistep.ActionContinue { 97 t.Fatalf("bad action: %#v", action) 98 } 99 if _, ok := state.GetOk("error"); ok { 100 t.Fatal("should NOT have error") 101 } 102 103 // Test the resulting data 104 vmxContents, err := ioutil.ReadFile(vmxPath) 105 if err != nil { 106 t.Fatalf("err: %s", err) 107 } 108 vmxData := ParseVMX(string(vmxContents)) 109 110 cases := []struct { 111 Key string 112 Value string 113 }{ 114 {"ide0:0.filename", "auto detect"}, 115 {"ide0:0.devicetype", "cdrom-raw"}, 116 {"ide0:1.filename", "bar"}, 117 {"foo", "bar"}, 118 } 119 120 for _, tc := range cases { 121 if tc.Value == "" { 122 if _, ok := vmxData[tc.Key]; ok { 123 t.Fatalf("should not have key: %s", tc.Key) 124 } 125 } else { 126 if vmxData[tc.Key] != tc.Value { 127 t.Fatalf("bad: %s %#v", tc.Key, vmxData[tc.Key]) 128 } 129 } 130 } 131 } 132 133 const testVMXFloppyPath = ` 134 floppy0.present = "TRUE" 135 floppy0.filetype = "file" 136 ` 137 138 const testVMXISOPath = ` 139 ide0:0.filename = "foo" 140 ide0:1.filename = "bar" 141 foo = "bar" 142 `