Skip to content

Commit

Permalink
fix: standardize Eventually() calls in auth tests (#67)
Browse files Browse the repository at this point in the history
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).
  • Loading branch information
cam-garrison authored Aug 29, 2024
1 parent f05c502 commit 8d4d857
Showing 1 changed file with 18 additions and 6 deletions.
24 changes: 18 additions & 6 deletions controllers/authzctrl/controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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) {
Expand All @@ -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,
Expand All @@ -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,
Expand All @@ -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]
Expand Down

0 comments on commit 8d4d857

Please sign in to comment.