Add tests for step_start_vm cleanup
This commit is contained in:
parent
4c1fbfdd61
commit
c4ce295f67
|
@ -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) {
|
func (s *stepStartVM) Cleanup(state multistep.StateBag) {
|
||||||
vmRefUntyped, ok := state.GetOk("vmRef")
|
vmRefUntyped, ok := state.GetOk("vmRef")
|
||||||
// If not ok, we probably errored out before creating the VM
|
// If not ok, we probably errored out before creating the VM
|
||||||
|
@ -123,7 +128,7 @@ func (s *stepStartVM) Cleanup(state multistep.StateBag) {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
client := state.Get("proxmoxClient").(*proxmox.Client)
|
client := state.Get("proxmoxClient").(startedVMCleaner)
|
||||||
ui := state.Get("ui").(packer.Ui)
|
ui := state.Get("ui").(packer.Ui)
|
||||||
|
|
||||||
// Destroy the server we just created
|
// Destroy the server we just created
|
||||||
|
|
|
@ -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")
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue