From 8d4d8576395260b15422a9ff300e275836642153 Mon Sep 17 00:00:00 2001 From: Cameron Garrison Date: Thu, 29 Aug 2024 09:41:24 -0400 Subject: [PATCH] fix: standardize Eventually() calls in auth tests (#67) Without using `g Gomega` argument for matchers, assertions done in blocks such as `Eventually()` or `Consistently()` can fail on the first run resulting in flaky tests. More details can be found in the [Gomega docs](https://onsi.github.io/gomega/#category-3-making-assertions-eminem-the-function-passed-into-codeeventuallycode). --- controllers/authzctrl/controller_test.go | 24 ++++++++++++++++++------ 1 file changed, 18 insertions(+), 6 deletions(-) diff --git a/controllers/authzctrl/controller_test.go b/controllers/authzctrl/controller_test.go index 49a2e1b..332f860 100644 --- a/controllers/authzctrl/controller_test.go +++ b/controllers/authzctrl/controller_test.go @@ -69,7 +69,7 @@ var _ = Describe("Checking Authorization Resource Creation", test.EnvTest(), fun }) It("should create an anonymous AuthConfig resource by default when a target CR is created", func(ctx context.Context) { - Eventually(func() error { + Eventually(func(g Gomega, ctx context.Context) error { createdAuthConfig := &authorinov1beta2.AuthConfig{} err := envTest.Client.Get(ctx, types.NamespacedName{ Name: resourceName, @@ -88,7 +88,11 @@ var _ = Describe("Checking Authorization Resource Creation", test.EnvTest(), fun Expect(createdAuthConfig).NotTo(HaveKubernetesTokenReview()) return nil - }, test.DefaultTimeout, test.DefaultPolling).Should(Succeed()) + }). + WithContext(ctx). + WithTimeout(test.DefaultTimeout). + WithPolling(test.DefaultPolling). + Should(Succeed()) }) It("should create a non-anonymous AuthConfig resource when annotation is specified", func(ctx context.Context) { @@ -103,7 +107,7 @@ var _ = Describe("Checking Authorization Resource Creation", test.EnvTest(), fun }) Expect(errCreate).ToNot(HaveOccurred()) - Eventually(func() error { + Eventually(func(g Gomega, ctx context.Context) error { createdAuthConfig := &authorinov1beta2.AuthConfig{} err := envTest.Client.Get(ctx, types.NamespacedName{ Name: resourceName, @@ -122,11 +126,15 @@ var _ = Describe("Checking Authorization Resource Creation", test.EnvTest(), fun Expect(createdAuthConfig).To(HaveKubernetesTokenReview()) return nil - }, test.DefaultTimeout, test.DefaultPolling).Should(Succeed()) + }). + WithContext(ctx). + WithTimeout(test.DefaultTimeout). + WithPolling(test.DefaultPolling). + Should(Succeed()) }) It("should create an AuthorizationPolicy when a Component is created", func(ctx context.Context) { - Eventually(func() error { + Eventually(func(g Gomega, ctx context.Context) error { createdAuthPolicy := &istiosecurityv1beta1.AuthorizationPolicy{} err := envTest.Client.Get(ctx, types.NamespacedName{ Name: resourceName, @@ -142,7 +150,11 @@ var _ = Describe("Checking Authorization Resource Creation", test.EnvTest(), fun Expect(createdAuthPolicy.Spec.GetSelector().GetMatchLabels()).To(HaveKeyWithValue("component", resourceName)) return nil - }, test.DefaultTimeout, test.DefaultPolling).Should(Succeed()) + }). + WithContext(ctx). + WithTimeout(test.DefaultTimeout). + WithPolling(test.DefaultPolling). + Should(Succeed()) }) // Using k8s envtest we are not able to test actual garbage collection of resources. [1]