From efe6637524a90dfefb3ac5192f7df878d4362df2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patryk=20Ma=C5=82ek?= Date: Wed, 9 Oct 2024 09:45:35 +0200 Subject: [PATCH] tests(konnect): fix flaky KongConsumerCredential tests (#719) * tests(konnect): fix flaky KongConsumerCredential tests * Update test/envtest/kongconsumercredential_jwt_test.go --- test/envtest/assert.go | 30 +++++++++++++++++++ .../kongconsumercredential_acl_test.go | 6 ++++ .../kongconsumercredential_apikey_test.go | 6 ++++ .../kongconsumercredential_basicauth_test.go | 6 ++++ .../kongconsumercredential_hmac_test.go | 6 ++++ .../kongconsumercredential_jwt_test.go | 6 ++++ 6 files changed, 60 insertions(+) create mode 100644 test/envtest/assert.go diff --git a/test/envtest/assert.go b/test/envtest/assert.go new file mode 100644 index 000000000..9e093df75 --- /dev/null +++ b/test/envtest/assert.go @@ -0,0 +1,30 @@ +package envtest + +import ( + "context" + "testing" + + "github.com/stretchr/testify/assert" + "sigs.k8s.io/controller-runtime/pkg/client" +) + +func assertCollectObjectExistsAndHasKonnectID( + t *testing.T, + ctx context.Context, + clientNamespaced client.Client, + obj interface { + client.Object + GetKonnectID() string + }, + konnectID string, +) func(c *assert.CollectT) { + t.Helper() + + return func(c *assert.CollectT) { + nn := client.ObjectKeyFromObject(obj) + if !assert.NoError(c, clientNamespaced.Get(ctx, nn, obj)) { + return + } + assert.Equal(t, konnectID, obj.GetKonnectID()) + } +} diff --git a/test/envtest/kongconsumercredential_acl_test.go b/test/envtest/kongconsumercredential_acl_test.go index 8a5b5474e..43d1dc973 100644 --- a/test/envtest/kongconsumercredential_acl_test.go +++ b/test/envtest/kongconsumercredential_acl_test.go @@ -116,6 +116,12 @@ func TestKongConsumerCredential_ACL(t *testing.T) { StartReconcilers(ctx, t, mgr, logs, reconcilers...) + assert.EventuallyWithT(t, + assertCollectObjectExistsAndHasKonnectID(t, ctx, clientNamespaced, kongCredentialACL, aclID), + waitTime, tickTime, + "KongCredentialACL wasn't created", + ) + assert.EventuallyWithT(t, func(c *assert.CollectT) { assert.True(c, factory.SDK.KongCredentialsACLSDK.AssertExpectations(t)) }, waitTime, tickTime) diff --git a/test/envtest/kongconsumercredential_apikey_test.go b/test/envtest/kongconsumercredential_apikey_test.go index d442147ed..b1c952922 100644 --- a/test/envtest/kongconsumercredential_apikey_test.go +++ b/test/envtest/kongconsumercredential_apikey_test.go @@ -115,6 +115,12 @@ func TestKongConsumerCredential_APIKey(t *testing.T) { StartReconcilers(ctx, t, mgr, logs, reconcilers...) + assert.EventuallyWithT(t, + assertCollectObjectExistsAndHasKonnectID(t, ctx, clientNamespaced, kongCredentialAPIKey, keyID), + waitTime, tickTime, + "KongCredentialAPIKey wasn't created", + ) + assert.EventuallyWithT(t, func(c *assert.CollectT) { assert.True(c, factory.SDK.KongCredentialsAPIKeySDK.AssertExpectations(t)) }, waitTime, tickTime) diff --git a/test/envtest/kongconsumercredential_basicauth_test.go b/test/envtest/kongconsumercredential_basicauth_test.go index 8ac53ae3c..ab1252fa4 100644 --- a/test/envtest/kongconsumercredential_basicauth_test.go +++ b/test/envtest/kongconsumercredential_basicauth_test.go @@ -118,6 +118,12 @@ func TestKongConsumerCredential_BasicAuth(t *testing.T) { StartReconcilers(ctx, t, mgr, logs, reconcilers...) + assert.EventuallyWithT(t, + assertCollectObjectExistsAndHasKonnectID(t, ctx, clientNamespaced, kongCredentialBasicAuth, basicAuthID), + waitTime, tickTime, + "KongCredentialBasicAuth wasn't created", + ) + assert.EventuallyWithT(t, func(c *assert.CollectT) { assert.True(c, factory.SDK.KongCredentialsBasicAuthSDK.AssertExpectations(t)) }, waitTime, tickTime) diff --git a/test/envtest/kongconsumercredential_hmac_test.go b/test/envtest/kongconsumercredential_hmac_test.go index b261922b9..7171ef650 100644 --- a/test/envtest/kongconsumercredential_hmac_test.go +++ b/test/envtest/kongconsumercredential_hmac_test.go @@ -115,6 +115,12 @@ func TestKongConsumerCredential_HMAC(t *testing.T) { StartReconcilers(ctx, t, mgr, logs, reconcilers...) + assert.EventuallyWithT(t, + assertCollectObjectExistsAndHasKonnectID(t, ctx, clientNamespaced, kongCredentialHMAC, hmacID), + waitTime, tickTime, + "KongCredentialHMAC wasn't created", + ) + assert.EventuallyWithT(t, func(c *assert.CollectT) { assert.True(c, factory.SDK.KongCredentialsHMACSDK.AssertExpectations(t)) }, waitTime, tickTime) diff --git a/test/envtest/kongconsumercredential_jwt_test.go b/test/envtest/kongconsumercredential_jwt_test.go index 752320df5..e8fd3f031 100644 --- a/test/envtest/kongconsumercredential_jwt_test.go +++ b/test/envtest/kongconsumercredential_jwt_test.go @@ -116,6 +116,12 @@ func TestKongConsumerCredential_JWT(t *testing.T) { StartReconcilers(ctx, t, mgr, logs, reconcilers...) + assert.EventuallyWithT(t, + assertCollectObjectExistsAndHasKonnectID(t, ctx, clientNamespaced, kongCredentialJWT, jwtID), + waitTime, tickTime, + "KongCredentialJWT wasn't created", + ) + assert.EventuallyWithT(t, func(c *assert.CollectT) { assert.True(c, factory.SDK.KongCredentialsJWTSDK.AssertExpectations(t)) }, waitTime, tickTime)