Skip to content

Commit

Permalink
Add unit tests to vm foreground finalizer
Browse files Browse the repository at this point in the history
Signed-off-by: fossedihelm <[email protected]>
  • Loading branch information
fossedihelm committed Feb 1, 2023
1 parent 43a705b commit 3b45988
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions pkg/virt-controller/watch/vm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,12 @@ var _ = Describe("VirtualMachine", func() {
vmiInterface.EXPECT().Patch(context.Background(), vmi.Name, types.JSONPatchType, []byte(patch), &metav1.PatchOptions{}).Return(vmi, nil)
}

shouldExpectVMFinalizerAddition := func(vm *virtv1.VirtualMachine) {
patch := fmt.Sprintf(`[{ "op": "test", "path": "/metadata/finalizers", "value": null }, { "op": "replace", "path": "/metadata/finalizers", "value": ["%s"] }]`, virtv1.VirtualMachineControllerFinalizer)

vmInterface.EXPECT().Patch(vm.Name, types.JSONPatchType, []byte(patch), &metav1.PatchOptions{}).Return(vm, nil)
}

shouldExpectVMFinalizerRemoval := func(vm *virtv1.VirtualMachine) {
patch := fmt.Sprintf(`[{ "op": "test", "path": "/metadata/finalizers", "value": ["%s"] }, { "op": "replace", "path": "/metadata/finalizers", "value": [] }]`, virtv1.VirtualMachineControllerFinalizer)

Expand Down Expand Up @@ -1386,6 +1392,60 @@ var _ = Describe("VirtualMachine", func() {
testutils.ExpectEvent(recorder, SuccessfulDeleteVirtualMachineReason)
})

It("should add controller finalizer if VirtualMachine does not have it", func() {
vm, _ := DefaultVirtualMachine(false)
vm.Finalizers = nil

addVirtualMachine(vm)

shouldExpectVMFinalizerAddition(vm)
vmInterface.EXPECT().UpdateStatus(gomock.Any()).Times(1).Return(vm, nil)

controller.Execute()
})

It("should add controller finalizer only once", func() {
//DefaultVirtualMachine already set finalizer
vm, _ := DefaultVirtualMachine(false)
Expect(vm.Finalizers).To(HaveLen(1))
Expect(vm.Finalizers[0]).To(BeEquivalentTo(virtv1.VirtualMachineControllerFinalizer))
addVirtualMachine(vm)

//Expect only update status, not Patch on vmInterface
vmInterface.EXPECT().UpdateStatus(gomock.Any()).Times(1).Return(vm, nil)

controller.Execute()
})

It("should delete VirtualMachineInstance when VirtualMachine marked for deletion", func() {
vm, vmi := DefaultVirtualMachine(true)
vm.DeletionTimestamp = now()

addVirtualMachine(vm)
vmiFeeder.Add(vmi)

vmiInterface.EXPECT().Delete(context.Background(), gomock.Any(), gomock.Any()).Return(nil)

vmInterface.EXPECT().UpdateStatus(gomock.Any()).Times(1).Return(vm, nil)

controller.Execute()

testutils.ExpectEvent(recorder, SuccessfulDeleteVirtualMachineReason)
})

It("should remove controller finalizer once VirtualMachineInstance is gone", func() {
//DefaultVirtualMachine already set finalizer
vm, _ := DefaultVirtualMachine(true)
vm.DeletionTimestamp = now()

addVirtualMachine(vm)

shouldExpectVMFinalizerRemoval(vm)
vmInterface.EXPECT().UpdateStatus(gomock.Any()).Times(1).Return(vm, nil)

controller.Execute()
})

DescribeTable("should not delete VirtualMachineInstance when vmi failed", func(runStrategy virtv1.VirtualMachineRunStrategy) {
vm, vmi := DefaultVirtualMachine(true)

Expand Down

0 comments on commit 3b45988

Please sign in to comment.