forked from kubevirt/kubevirt
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add functional test to cover vm finalizer flow
Signed-off-by: fossedihelm <[email protected]>
- Loading branch information
1 parent
3b45988
commit 199f012
Showing
1 changed file
with
76 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2246,6 +2246,82 @@ var _ = Describe("[rfe_id:1177][crit:medium][vendor:[email protected]][level:com | |
}, 2*time.Minute, 5*time.Second).Should(BeNil()) | ||
}) | ||
}) | ||
|
||
Context("VirtualMachineControllerFinalizer", func() { | ||
const customFinalizer = "customFinalizer" | ||
|
||
var ( | ||
vmi *v1.VirtualMachineInstance | ||
vm *v1.VirtualMachine | ||
) | ||
|
||
BeforeEach(func() { | ||
vmi = tests.NewRandomVMI() | ||
|
||
By("Creating VirtualMachine") | ||
vm = tests.NewRandomVirtualMachine(vmi, true) | ||
Expect(vm.Finalizers).To(HaveLen(0)) | ||
|
||
}) | ||
|
||
AfterEach(func() { | ||
if controller.HasFinalizer(vm, customFinalizer) { | ||
vm, err = virtClient.VirtualMachine(vm.Namespace).Get(vm.Name, &k8smetav1.GetOptions{}) | ||
Expect(err).ToNot(HaveOccurred()) | ||
var ops []string | ||
oldFinalizers, err := json.Marshal(vm.GetFinalizers()) | ||
Expect(err).ToNot(HaveOccurred()) | ||
newVm := vm.DeepCopy() | ||
controller.RemoveFinalizer(newVm, customFinalizer) | ||
newFinalizers, err := json.Marshal(newVm.GetFinalizers()) | ||
Expect(err).ToNot(HaveOccurred()) | ||
ops = append(ops, fmt.Sprintf(`{ "op": "test", "path": "/metadata/finalizers", "value": %s }`, string(oldFinalizers))) | ||
ops = append(ops, fmt.Sprintf(`{ "op": "replace", "path": "/metadata/finalizers", "value": %s }`, string(newFinalizers))) | ||
vm, err = virtClient.VirtualMachine(vm.Namespace).Patch(vm.Name, types.JSONPatchType, controller.GeneratePatchBytes(ops), &metav1.PatchOptions{}) | ||
Expect(err).ToNot(HaveOccurred()) | ||
} | ||
|
||
err = virtClient.VirtualMachine(testsuite.GetTestNamespace(vm)).Delete(vm.Name, &metav1.DeleteOptions{}) | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
By("Ensure the vm has disappeared") | ||
Eventually(func() bool { | ||
vm, err = virtClient.VirtualMachine(vm.Namespace).Get(vm.Name, &k8smetav1.GetOptions{}) | ||
return errors.IsNotFound(err) | ||
}, 2*time.Minute, 1*time.Second).Should(BeTrue(), fmt.Sprintf("vm %s is not deleted", vm.Name)) | ||
}) | ||
|
||
It("should be added when the vm is created", func() { | ||
vm, err = virtClient.VirtualMachine(testsuite.GetTestNamespace(vm)).Create(vm) | ||
Expect(err).ToNot(HaveOccurred()) | ||
Eventually(func(g Gomega) { | ||
vm, err = virtClient.VirtualMachine(vm.Namespace).Get(vm.Name, &k8smetav1.GetOptions{}) | ||
g.Expect(err).ToNot(HaveOccurred()) | ||
g.Expect(controller.HasFinalizer(vm, v1.VirtualMachineControllerFinalizer)).To(BeTrue()) | ||
}, 2*time.Minute, 1*time.Second) | ||
}) | ||
|
||
It("should be removed when the vm is being deleted", func() { | ||
vm.Finalizers = append(vm.Finalizers, customFinalizer) | ||
vm, err = virtClient.VirtualMachine(testsuite.GetTestNamespace(vm)).Create(vm) | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
Eventually(func(g Gomega) { | ||
vm, err = virtClient.VirtualMachine(vm.Namespace).Get(vm.Name, &k8smetav1.GetOptions{}) | ||
g.Expect(err).ToNot(HaveOccurred()) | ||
g.Expect(controller.HasFinalizer(vm, v1.VirtualMachineControllerFinalizer)).To(BeTrue()) | ||
}, 2*time.Minute, 1*time.Second) | ||
|
||
err = virtClient.VirtualMachine(testsuite.GetTestNamespace(vm)).Delete(vm.Name, &metav1.DeleteOptions{}) | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
Eventually(func(g Gomega) { | ||
vm, err = virtClient.VirtualMachine(vm.Namespace).Get(vm.Name, &k8smetav1.GetOptions{}) | ||
g.Expect(err).ToNot(HaveOccurred()) | ||
g.Expect(controller.HasFinalizer(vm, v1.VirtualMachineControllerFinalizer)).To(BeFalse()) | ||
}, 2*time.Minute, 1*time.Second) | ||
}) | ||
}) | ||
}) | ||
|
||
func getExpectedPodName(vm *v1.VirtualMachine) string { | ||
|