From c4866504e157db4b3524d81a6eded234f5a18dcd Mon Sep 17 00:00:00 2001 From: Megan Marsh Date: Tue, 27 Oct 2020 06:07:08 -0700 Subject: [PATCH] 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 --- builder/vsphere/clone/step_clone.go | 21 +------- builder/vsphere/common/cleanup_vm.go | 30 +++++++++++ builder/vsphere/common/cleanup_vm_test.go | 62 +++++++++++++++++++++++ builder/vsphere/iso/step_create.go | 21 +------- 4 files changed, 94 insertions(+), 40 deletions(-) create mode 100644 builder/vsphere/common/cleanup_vm.go create mode 100644 builder/vsphere/common/cleanup_vm_test.go diff --git a/builder/vsphere/clone/step_clone.go b/builder/vsphere/clone/step_clone.go index 18ba50a30..17da84ad1 100644 --- a/builder/vsphere/clone/step_clone.go +++ b/builder/vsphere/clone/step_clone.go @@ -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) } diff --git a/builder/vsphere/common/cleanup_vm.go b/builder/vsphere/common/cleanup_vm.go new file mode 100644 index 000000000..51c76c571 --- /dev/null +++ b/builder/vsphere/common/cleanup_vm.go @@ -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()) + } +} diff --git a/builder/vsphere/common/cleanup_vm_test.go b/builder/vsphere/common/cleanup_vm_test.go new file mode 100644 index 000000000..cdec04ab9 --- /dev/null +++ b/builder/vsphere/common/cleanup_vm_test.go @@ -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) + } + } + +} diff --git a/builder/vsphere/iso/step_create.go b/builder/vsphere/iso/step_create.go index 864f4a558..fa47f284f 100644 --- a/builder/vsphere/iso/step_create.go +++ b/builder/vsphere/iso/step_create.go @@ -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) }