respect the destroy flag in content library config (#10165)

* respect the destroy flag in content library config

* Make vsphere-clone respect delete_vm state tag; use a common delete func to prevent future drift
This commit is contained in:
Megan Marsh 2020-10-27 06:07:08 -07:00 committed by GitHub
parent 4bc16455b4
commit c4866504e1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 94 additions and 40 deletions

View File

@ -123,24 +123,5 @@ func (s *StepCloneVM) Run(ctx context.Context, state multistep.StateBag) multist
}
func (s *StepCloneVM) Cleanup(state multistep.StateBag) {
_, cancelled := state.GetOk(multistep.StateCancelled)
_, halted := state.GetOk(multistep.StateHalted)
if !cancelled && !halted {
return
}
ui := state.Get("ui").(packer.Ui)
st := state.Get("vm")
if st == nil {
return
}
vm := st.(*driver.VirtualMachineDriver)
ui.Say("Destroying VM...")
err := vm.Destroy()
if err != nil {
ui.Error(err.Error())
}
common.CleanupVM(state)
}

View File

@ -0,0 +1,30 @@
package common
import (
"github.com/hashicorp/packer/builder/vsphere/driver"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
)
func CleanupVM(state multistep.StateBag) {
_, cancelled := state.GetOk(multistep.StateCancelled)
_, halted := state.GetOk(multistep.StateHalted)
_, destroy := state.GetOk("destroy_vm")
if !cancelled && !halted && !destroy {
return
}
ui := state.Get("ui").(packer.Ui)
st := state.Get("vm")
if st == nil {
return
}
vm := st.(driver.VirtualMachine)
ui.Say("Destroying VM...")
err := vm.Destroy()
if err != nil {
ui.Error(err.Error())
}
}

View File

@ -0,0 +1,62 @@
package common
import (
"bytes"
"testing"
"github.com/hashicorp/packer/builder/vsphere/driver"
"github.com/hashicorp/packer/helper/multistep"
"github.com/hashicorp/packer/packer"
)
func cleanupTestState(mockVM driver.VirtualMachine) multistep.StateBag {
state := new(multistep.BasicStateBag)
state.Put("vm", mockVM)
state.Put("ui", &packer.BasicUi{
Reader: new(bytes.Buffer),
Writer: new(bytes.Buffer),
})
return state
}
func Test_CleanupVM(t *testing.T) {
type testCase struct {
Reason string
ExtraState map[string]interface{}
ExpectDestroy bool
}
testCases := []testCase{
{
"if cancelled, we should destroy the VM",
map[string]interface{}{multistep.StateCancelled: true},
true,
},
{
"if halted, we should destroy the VM",
map[string]interface{}{multistep.StateHalted: true},
true,
},
{
"if destroy flag is set, we should destroy the VM",
map[string]interface{}{"destroy_vm": true},
true,
},
{
"if none of the above flags are set, we should not destroy the VM",
map[string]interface{}{},
false,
},
}
for _, tc := range testCases {
mockVM := &driver.VirtualMachineMock{}
state := cleanupTestState(mockVM)
for k, v := range tc.ExtraState {
state.Put(k, v)
}
CleanupVM(state)
if mockVM.DestroyCalled != tc.ExpectDestroy {
t.Fatalf("Problem with cleanup: %s", tc.Reason)
}
}
}

View File

@ -277,24 +277,5 @@ func (s *StepCreateVM) Run(_ context.Context, state multistep.StateBag) multiste
}
func (s *StepCreateVM) Cleanup(state multistep.StateBag) {
_, cancelled := state.GetOk(multistep.StateCancelled)
_, halted := state.GetOk(multistep.StateHalted)
_, destroy := state.GetOk("destroy_vm")
if !cancelled && !halted && !destroy {
return
}
ui := state.Get("ui").(packer.Ui)
st := state.Get("vm")
if st == nil {
return
}
vm := st.(driver.VirtualMachine)
ui.Say("Destroying VM...")
err := vm.Destroy()
if err != nil {
ui.Error(err.Error())
}
common.CleanupVM(state)
}