2013-12-24 20:40:52 -05:00
|
|
|
package common
|
|
|
|
|
|
|
|
import (
|
2018-01-22 19:03:49 -05:00
|
|
|
"context"
|
2013-12-24 20:40:52 -05:00
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
2020-11-17 19:31:03 -05:00
|
|
|
"github.com/hashicorp/packer/packer-plugin-sdk/multistep"
|
2013-12-24 20:40:52 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestStepCleanVMX_impl(t *testing.T) {
|
|
|
|
var _ multistep.Step = new(StepCleanVMX)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStepCleanVMX(t *testing.T) {
|
|
|
|
state := testState(t)
|
|
|
|
step := new(StepCleanVMX)
|
|
|
|
|
|
|
|
vmxPath := testVMXFile(t)
|
|
|
|
defer os.Remove(vmxPath)
|
|
|
|
state.Put("vmx_path", vmxPath)
|
|
|
|
|
|
|
|
// Test the run
|
2018-01-22 19:03:49 -05:00
|
|
|
if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
|
2013-12-24 20:40:52 -05:00
|
|
|
t.Fatalf("bad action: %#v", action)
|
|
|
|
}
|
|
|
|
if _, ok := state.GetOk("error"); ok {
|
|
|
|
t.Fatal("should NOT have error")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStepCleanVMX_floppyPath(t *testing.T) {
|
|
|
|
state := testState(t)
|
|
|
|
step := new(StepCleanVMX)
|
|
|
|
|
|
|
|
vmxPath := testVMXFile(t)
|
|
|
|
defer os.Remove(vmxPath)
|
|
|
|
if err := ioutil.WriteFile(vmxPath, []byte(testVMXFloppyPath), 0644); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-12-03 19:03:02 -05:00
|
|
|
// Set the path to the temporary vmx
|
2013-12-24 20:40:52 -05:00
|
|
|
state.Put("vmx_path", vmxPath)
|
|
|
|
|
2018-12-03 19:03:02 -05:00
|
|
|
// Add the floppy device to the list of temporary build devices
|
|
|
|
state.Put("temporaryDevices", []string{"floppy0"})
|
|
|
|
|
2013-12-24 20:40:52 -05:00
|
|
|
// Test the run
|
2018-01-22 19:03:49 -05:00
|
|
|
if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
|
2013-12-24 20:40:52 -05:00
|
|
|
t.Fatalf("bad action: %#v", action)
|
|
|
|
}
|
|
|
|
if _, ok := state.GetOk("error"); ok {
|
|
|
|
t.Fatal("should NOT have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test the resulting data
|
|
|
|
vmxContents, err := ioutil.ReadFile(vmxPath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
vmxData := ParseVMX(string(vmxContents))
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
Key string
|
|
|
|
Value string
|
|
|
|
}{
|
|
|
|
{"floppy0.present", "FALSE"},
|
2015-08-10 17:52:34 -04:00
|
|
|
{"floppy0.filetype", ""},
|
|
|
|
{"floppy0.filename", ""},
|
2013-12-24 20:40:52 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
if tc.Value == "" {
|
|
|
|
if _, ok := vmxData[tc.Key]; ok {
|
|
|
|
t.Fatalf("should not have key: %s", tc.Key)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if vmxData[tc.Key] != tc.Value {
|
|
|
|
t.Fatalf("bad: %s %#v", tc.Key, vmxData[tc.Key])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestStepCleanVMX_isoPath(t *testing.T) {
|
|
|
|
state := testState(t)
|
|
|
|
step := new(StepCleanVMX)
|
|
|
|
|
|
|
|
vmxPath := testVMXFile(t)
|
|
|
|
defer os.Remove(vmxPath)
|
|
|
|
if err := ioutil.WriteFile(vmxPath, []byte(testVMXISOPath), 0644); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-12-03 19:03:02 -05:00
|
|
|
// Set the path to the temporary vmx
|
2013-12-24 20:40:52 -05:00
|
|
|
state.Put("vmx_path", vmxPath)
|
|
|
|
|
2018-12-03 19:03:02 -05:00
|
|
|
// Add the cdrom device to the list of temporary build devices
|
|
|
|
state.Put("temporaryDevices", []string{"ide0:0"})
|
|
|
|
|
2013-12-24 20:40:52 -05:00
|
|
|
// Test the run
|
2018-01-22 19:03:49 -05:00
|
|
|
if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
|
2013-12-24 20:40:52 -05:00
|
|
|
t.Fatalf("bad action: %#v", action)
|
|
|
|
}
|
|
|
|
if _, ok := state.GetOk("error"); ok {
|
|
|
|
t.Fatal("should NOT have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test the resulting data
|
|
|
|
vmxContents, err := ioutil.ReadFile(vmxPath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
vmxData := ParseVMX(string(vmxContents))
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
Key string
|
|
|
|
Value string
|
|
|
|
}{
|
2015-08-10 17:52:34 -04:00
|
|
|
{"ide0:0.filename", "auto detect"},
|
|
|
|
{"ide0:0.devicetype", "cdrom-raw"},
|
|
|
|
{"ide0:1.filename", "bar"},
|
2013-12-24 20:40:52 -05:00
|
|
|
{"foo", "bar"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
if tc.Value == "" {
|
|
|
|
if _, ok := vmxData[tc.Key]; ok {
|
|
|
|
t.Fatalf("should not have key: %s", tc.Key)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if vmxData[tc.Key] != tc.Value {
|
|
|
|
t.Fatalf("bad: %s %#v", tc.Key, vmxData[tc.Key])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-05-24 19:44:35 -04:00
|
|
|
func TestStepCleanVMX_ethernet(t *testing.T) {
|
|
|
|
state := testState(t)
|
|
|
|
step := &StepCleanVMX{
|
|
|
|
RemoveEthernetInterfaces: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
vmxPath := testVMXFile(t)
|
|
|
|
defer os.Remove(vmxPath)
|
|
|
|
if err := ioutil.WriteFile(vmxPath, []byte(testVMXEthernet), 0644); err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
|
2018-12-03 19:03:02 -05:00
|
|
|
// Set the path to the temporary vmx
|
2017-05-24 19:44:35 -04:00
|
|
|
state.Put("vmx_path", vmxPath)
|
|
|
|
|
2018-12-03 19:03:02 -05:00
|
|
|
// TODO: Add the ethernet devices to the list of temporary build devices
|
|
|
|
// state.Put("temporaryDevices", []string{"ethernet0", "ethernet1"})
|
|
|
|
|
2017-05-24 19:44:35 -04:00
|
|
|
// Test the run
|
2018-01-22 19:03:49 -05:00
|
|
|
if action := step.Run(context.Background(), state); action != multistep.ActionContinue {
|
2017-05-24 19:44:35 -04:00
|
|
|
t.Fatalf("bad action: %#v", action)
|
|
|
|
}
|
|
|
|
if _, ok := state.GetOk("error"); ok {
|
|
|
|
t.Fatal("should NOT have error")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Test the resulting data
|
|
|
|
vmxContents, err := ioutil.ReadFile(vmxPath)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("err: %s", err)
|
|
|
|
}
|
|
|
|
vmxData := ParseVMX(string(vmxContents))
|
|
|
|
|
|
|
|
cases := []struct {
|
|
|
|
Key string
|
|
|
|
Value string
|
|
|
|
}{
|
|
|
|
{"ethernet0.addresstype", ""},
|
|
|
|
{"ethernet0.bsdname", ""},
|
|
|
|
{"ethernet0.connectiontype", ""},
|
|
|
|
{"ethernet1.addresstype", ""},
|
|
|
|
{"ethernet1.bsdname", ""},
|
|
|
|
{"ethernet1.connectiontype", ""},
|
|
|
|
{"foo", "bar"},
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tc := range cases {
|
|
|
|
if tc.Value == "" {
|
|
|
|
if _, ok := vmxData[tc.Key]; ok {
|
|
|
|
t.Fatalf("should not have key: %s", tc.Key)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if vmxData[tc.Key] != tc.Value {
|
|
|
|
t.Fatalf("bad: %s %#v", tc.Key, vmxData[tc.Key])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-24 20:40:52 -05:00
|
|
|
const testVMXFloppyPath = `
|
|
|
|
floppy0.present = "TRUE"
|
2015-08-10 17:52:34 -04:00
|
|
|
floppy0.filetype = "file"
|
2013-12-24 20:40:52 -05:00
|
|
|
`
|
|
|
|
|
|
|
|
const testVMXISOPath = `
|
2015-08-10 17:52:34 -04:00
|
|
|
ide0:0.devicetype = "cdrom-image"
|
|
|
|
ide0:0.filename = "foo"
|
|
|
|
ide0:1.filename = "bar"
|
2013-12-24 20:40:52 -05:00
|
|
|
foo = "bar"
|
|
|
|
`
|
2017-05-24 19:44:35 -04:00
|
|
|
|
|
|
|
const testVMXEthernet = `
|
|
|
|
ethernet0.addresstype = "generated"
|
|
|
|
ethernet0.bsdname = "en0"
|
|
|
|
ethernet0.connectiontype = "nat"
|
|
|
|
ethernet1.addresstype = "generated"
|
|
|
|
ethernet1.bsdname = "en1"
|
|
|
|
ethernet1.connectiontype = "nat"
|
|
|
|
foo = "bar"
|
|
|
|
`
|