From c4ce295f6798ca728aaae1d72b4cf71d4b494111 Mon Sep 17 00:00:00 2001 From: Calle Pettersson Date: Thu, 14 Mar 2019 22:28:07 +0100 Subject: [PATCH] Add tests for step_start_vm cleanup --- builder/proxmox/step_start_vm.go | 7 +- builder/proxmox/step_start_vm_test.go | 108 ++++++++++++++++++++++++++ 2 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 builder/proxmox/step_start_vm_test.go diff --git a/builder/proxmox/step_start_vm.go b/builder/proxmox/step_start_vm.go index f3a839c11..c65b8c968 100644 --- a/builder/proxmox/step_start_vm.go +++ b/builder/proxmox/step_start_vm.go @@ -109,6 +109,11 @@ func setDeviceParamIfDefined(dev proxmox.QemuDevice, key, value string) { } } +type startedVMCleaner interface { + StopVm(*proxmox.VmRef) (string, error) + DeleteVm(*proxmox.VmRef) (string, error) +} + func (s *stepStartVM) Cleanup(state multistep.StateBag) { vmRefUntyped, ok := state.GetOk("vmRef") // If not ok, we probably errored out before creating the VM @@ -123,7 +128,7 @@ func (s *stepStartVM) Cleanup(state multistep.StateBag) { return } - client := state.Get("proxmoxClient").(*proxmox.Client) + client := state.Get("proxmoxClient").(startedVMCleaner) ui := state.Get("ui").(packer.Ui) // Destroy the server we just created diff --git a/builder/proxmox/step_start_vm_test.go b/builder/proxmox/step_start_vm_test.go new file mode 100644 index 000000000..70f0aa37b --- /dev/null +++ b/builder/proxmox/step_start_vm_test.go @@ -0,0 +1,108 @@ +package proxmox + +import ( + "fmt" + "testing" + + "github.com/Telmate/proxmox-api-go/proxmox" + "github.com/hashicorp/packer/helper/multistep" + "github.com/hashicorp/packer/packer" +) + +type startedVMCleanerMock struct { + stopVm func() (string, error) + deleteVm func() (string, error) +} + +var _ startedVMCleaner = &startedVMCleanerMock{} + +func (m startedVMCleanerMock) StopVm(*proxmox.VmRef) (string, error) { + return m.stopVm() +} +func (m startedVMCleanerMock) DeleteVm(*proxmox.VmRef) (string, error) { + return m.deleteVm() +} + +func TestCleanupStartVM(t *testing.T) { + cs := []struct { + name string + setVmRef bool + setSuccess bool + stopVMErr error + expectCallStopVM bool + deleteVMErr error + expectCallDeleteVM bool + }{ + { + name: "when vmRef state is not set, nothing should happen", + setVmRef: false, + expectCallStopVM: false, + }, + { + name: "when success state is set, nothing should happen", + setVmRef: true, + setSuccess: true, + expectCallStopVM: false, + }, + { + name: "when not successful, vm should be stopped and deleted", + setVmRef: true, + setSuccess: false, + expectCallStopVM: true, + expectCallDeleteVM: true, + }, + { + name: "if stopping fails, DeleteVm should not be called", + setVmRef: true, + setSuccess: false, + expectCallStopVM: true, + stopVMErr: fmt.Errorf("some error"), + expectCallDeleteVM: false, + }, + } + + for _, c := range cs { + t.Run(c.name, func(t *testing.T) { + var stopWasCalled, deleteWasCalled bool + + cleaner := startedVMCleanerMock{ + stopVm: func() (string, error) { + if !c.expectCallStopVM { + t.Error("Did not expect StopVm to be called") + } + + stopWasCalled = true + return "", c.stopVMErr + }, + deleteVm: func() (string, error) { + if !c.expectCallDeleteVM { + t.Error("Did not expect DeleteVm to be called") + } + + deleteWasCalled = true + return "", c.deleteVMErr + }, + } + + state := new(multistep.BasicStateBag) + state.Put("ui", packer.TestUi(t)) + state.Put("proxmoxClient", cleaner) + if c.setVmRef { + state.Put("vmRef", proxmox.NewVmRef(1)) + } + if c.setSuccess { + state.Put("success", "true") + } + + step := stepStartVM{} + step.Cleanup(state) + + if c.expectCallStopVM && !stopWasCalled { + t.Error("Expected StopVm to be called, but it wasn't") + } + if c.expectCallDeleteVM && !deleteWasCalled { + t.Error("Expected DeleteVm to be called, but it wasn't") + } + }) + } +}