From bfa13bf19e6f2453ce27bcc42ad72e0f199a447e Mon Sep 17 00:00:00 2001 From: oraz Date: Wed, 24 Jan 2024 16:58:55 +0200 Subject: [PATCH] Refactor cleanupFar and check CR deletion event Seek for FAR CR deletion event only when there isn't an API 'is not found' error' --- .../fenceagentsremediation_controller_test.go | 59 ++++++++++--------- 1 file changed, 31 insertions(+), 28 deletions(-) diff --git a/controllers/fenceagentsremediation_controller_test.go b/controllers/fenceagentsremediation_controller_test.go index 01da4860..c1dbb20d 100644 --- a/controllers/fenceagentsremediation_controller_test.go +++ b/controllers/fenceagentsremediation_controller_test.go @@ -149,7 +149,14 @@ var _ = Describe("FAR Controller", func() { DeferCleanup(k8sClient.Delete, context.Background(), node) Expect(k8sClient.Create(context.Background(), underTestFAR)).To(Succeed()) - DeferCleanup(cleanupFar(), context.Background(), underTestFAR) + DeferCleanup(func() { + Expect(cleanupFar(context.Background(), underTestFAR)).To(Succeed()) + deleteErr := k8sClient.Get(ctx, client.ObjectKeyFromObject(underTestFAR), &v1alpha1.FenceAgentsRemediation{}) + if !apierrors.IsNotFound(deleteErr) { + verifyEvent(corev1.EventTypeNormal, utils.EventReasonRemoveRemediationCR, utils.EventMessageRemoveRemediationCR) + } + clearEvents() + }) // Sleep for a second to ensure dummy reconciliation has begun running before the unit tests time.Sleep(1 * time.Second) @@ -564,35 +571,31 @@ func verifyRemediationConditions(far *v1alpha1.FenceAgentsRemediation, nodeName } // cleanupFar deletes the FAR CR and waits until it is deleted. The function ignores if the CR is already deleted. -func cleanupFar() func(ctx context.Context, far *v1alpha1.FenceAgentsRemediation) error { - return func(ctx context.Context, far *v1alpha1.FenceAgentsRemediation) error { - cr := &v1alpha1.FenceAgentsRemediation{} - if err := k8sClient.Get(ctx, client.ObjectKeyFromObject(far), cr); err != nil { - if apierrors.IsNotFound(err) { - return nil - } - return err +func cleanupFar(ctx context.Context, far *v1alpha1.FenceAgentsRemediation) error { + cr := &v1alpha1.FenceAgentsRemediation{} + if err := k8sClient.Get(ctx, client.ObjectKeyFromObject(far), cr); err != nil { + if apierrors.IsNotFound(err) { + return nil } + return err + } - var force client.GracePeriodSeconds = 0 - if err := k8sClient.Delete(ctx, cr, force); err != nil { - if apierrors.IsNotFound(err) { - return nil - } - return err + var force client.GracePeriodSeconds = 0 + if err := k8sClient.Delete(ctx, cr, force); err != nil { + if apierrors.IsNotFound(err) { + return nil } - - ConsistentlyWithOffset(1, func() error { - deleteErr := k8sClient.Get(ctx, client.ObjectKeyFromObject(far), cr) - if apierrors.IsNotFound(deleteErr) { - // when trying to create far CR with invalid name - log.Info("Cleanup: Got error 404", "name", cr.Name) - return nil - } - return deleteErr - }, pollInterval, timeoutPostRemediation).Should(BeNil(), "CR should be deleted") - verifyEvent(corev1.EventTypeNormal, utils.EventReasonRemoveRemediationCR, utils.EventMessageRemoveRemediationCR) - clearEvents() - return nil + return err } + + ConsistentlyWithOffset(1, func() error { + deleteErr := k8sClient.Get(ctx, client.ObjectKeyFromObject(far), cr) + if apierrors.IsNotFound(deleteErr) { + // when trying to create far CR with invalid name + log.Info("Cleanup: Got error 404", "name", cr.Name) + return nil + } + return deleteErr + }, pollInterval, timeoutPostRemediation).Should(BeNil(), "CR should be deleted") + return nil }