From 75d649e0519ddf714d6a63aed942b5096eeca053 Mon Sep 17 00:00:00 2001 From: sylviamoss Date: Wed, 23 Sep 2020 12:09:33 +0200 Subject: [PATCH] add step_remove_cdrom tests --- .../vsphere/common/step_remove_cdrom_test.go | 111 ++++++++++++++++++ builder/vsphere/driver/vm_mock.go | 12 +- 2 files changed, 121 insertions(+), 2 deletions(-) create mode 100644 builder/vsphere/common/step_remove_cdrom_test.go diff --git a/builder/vsphere/common/step_remove_cdrom_test.go b/builder/vsphere/common/step_remove_cdrom_test.go new file mode 100644 index 000000000..1017e48b0 --- /dev/null +++ b/builder/vsphere/common/step_remove_cdrom_test.go @@ -0,0 +1,111 @@ +package common + +import ( + "context" + "fmt" + "testing" + + "github.com/google/go-cmp/cmp" + "github.com/google/go-cmp/cmp/cmpopts" + "github.com/hashicorp/packer/builder/vsphere/driver" + "github.com/hashicorp/packer/helper/multistep" +) + +func TestStepRemoveCDRom_Run(t *testing.T) { + tc := []struct { + name string + step *StepRemoveCDRom + expectedAction multistep.StepAction + vmMock *driver.VirtualMachineMock + expectedVmMock *driver.VirtualMachineMock + fail bool + errMessage string + }{ + { + name: "Eject CD-ROM drives", + step: &StepRemoveCDRom{ + Config: &RemoveCDRomConfig{}, + }, + expectedAction: multistep.ActionContinue, + vmMock: new(driver.VirtualMachineMock), + expectedVmMock: &driver.VirtualMachineMock{ + EjectCdromsCalled: true, + }, + fail: false, + }, + { + name: "Failed to eject CD-ROM drives", + step: &StepRemoveCDRom{ + Config: &RemoveCDRomConfig{}, + }, + expectedAction: multistep.ActionHalt, + vmMock: &driver.VirtualMachineMock{ + EjectCdromsErr: fmt.Errorf("failed to eject cd-rom drives"), + }, + expectedVmMock: &driver.VirtualMachineMock{ + EjectCdromsCalled: true, + }, + fail: true, + errMessage: "failed to eject cd-rom drives", + }, + { + name: "Eject and delete CD-ROM drives", + step: &StepRemoveCDRom{ + Config: &RemoveCDRomConfig{ + RemoveCdrom: true, + }, + }, + expectedAction: multistep.ActionContinue, + vmMock: new(driver.VirtualMachineMock), + expectedVmMock: &driver.VirtualMachineMock{ + EjectCdromsCalled: true, + RemoveCdromsCalled: true, + }, + fail: false, + }, + { + name: "Fail to delete CD-ROM drives", + step: &StepRemoveCDRom{ + Config: &RemoveCDRomConfig{ + RemoveCdrom: true, + }, + }, + expectedAction: multistep.ActionHalt, + vmMock: &driver.VirtualMachineMock{ + RemoveCdromsErr: fmt.Errorf("failed to delete cd-rom devices"), + }, + expectedVmMock: &driver.VirtualMachineMock{ + EjectCdromsCalled: true, + RemoveCdromsCalled: true, + }, + fail: true, + errMessage: "failed to delete cd-rom devices", + }, + } + + for _, c := range tc { + t.Run(c.name, func(t *testing.T) { + state := basicStateBag(nil) + state.Put("vm", c.vmMock) + + if action := c.step.Run(context.TODO(), state); action != c.expectedAction { + t.Fatalf("unexpected action %v", action) + } + err, ok := state.Get("error").(error) + if ok { + if err.Error() != c.errMessage { + t.Fatalf("unexpected error %s", err.Error()) + } + } else { + if c.fail { + t.Fatalf("expected to fail but it didn't") + } + } + + if diff := cmp.Diff(c.vmMock, c.expectedVmMock, + cmpopts.IgnoreInterfaces(struct{ error }{})); diff != "" { + t.Fatalf("unexpected VirtualMachine calls: %s", diff) + } + }) + } +} diff --git a/builder/vsphere/driver/vm_mock.go b/builder/vsphere/driver/vm_mock.go index ea16f8e57..5251b2030 100644 --- a/builder/vsphere/driver/vm_mock.go +++ b/builder/vsphere/driver/vm_mock.go @@ -49,6 +49,12 @@ type VirtualMachineMock struct { RemoveDeviceCalled bool RemoveDeviceKeepFiles bool RemoveDeviceDevices []types.BaseVirtualDevice + + EjectCdromsCalled bool + EjectCdromsErr error + + RemoveCdromsCalled bool + RemoveCdromsErr error } func (vm *VirtualMachineMock) Info(params ...string) (*mo.VirtualMachine, error) { @@ -214,9 +220,11 @@ func (vm *VirtualMachineMock) CreateCdrom(c *types.VirtualController) (*types.Vi } func (vm *VirtualMachineMock) RemoveCdroms() error { - return nil + vm.RemoveCdromsCalled = true + return vm.RemoveCdromsErr } func (vm *VirtualMachineMock) EjectCdroms() error { - return nil + vm.EjectCdromsCalled = true + return vm.EjectCdromsErr }