From cb71adbb987574adfa66f8760d5f6c0c5da33770 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Patryk=20Ma=C5=82ek?= Date: Tue, 8 Oct 2024 21:50:45 +0200 Subject: [PATCH] tests(konnect): fix flaky KongConsumerCredential tests --- 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 | 7 +++++ 6 files changed, 61 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..7af774424 100644 --- a/test/envtest/kongconsumercredential_jwt_test.go +++ b/test/envtest/kongconsumercredential_jwt_test.go @@ -116,6 +116,13 @@ func TestKongConsumerCredential_JWT(t *testing.T) { StartReconcilers(ctx, t, mgr, logs, reconcilers...) + kongCredentialJWT.GetKonnectID() + 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)