From d8d49be5d9a6e7b1dd8711772b179ada5973f6a9 Mon Sep 17 00:00:00 2001 From: Ryan Richard Date: Mon, 17 Aug 2020 16:28:12 -0700 Subject: [PATCH] Make an integration test more reliable - It would sometimes fail with this error: namespaces is forbidden: User "tanzu-user-authentication@groups.vmware.com" cannot list resource "namespaces" in API group "" at the cluster scope - Seems like it was because the RBAC rule added by the test needs a moment before it starts to take effect, so change the test to retry the API until it succeeds or fail after 3 seconds of trying. --- test/integration/credentialrequest_test.go | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/test/integration/credentialrequest_test.go b/test/integration/credentialrequest_test.go index 196d15c3..af4fe8fb 100644 --- a/test/integration/credentialrequest_test.go +++ b/test/integration/credentialrequest_test.go @@ -13,6 +13,9 @@ import ( "testing" "time" + "github.com/stretchr/testify/assert" + v1 "k8s.io/api/core/v1" + "github.com/stretchr/testify/require" rbacv1 "k8s.io/api/rbac/v1" "k8s.io/apimachinery/pkg/api/errors" @@ -112,8 +115,13 @@ func TestSuccessfulCredentialRequest(t *testing.T) { }) // Use the client which is authenticated as the TMC user to list namespaces - listNamespaceResponse, err := clientWithCertFromCredentialRequest.CoreV1().Namespaces().List(ctx, metav1.ListOptions{}) - require.NoError(t, err) + var listNamespaceResponse *v1.NamespaceList + var canListNamespaces = func() bool { + listNamespaceResponse, err = clientWithCertFromCredentialRequest.CoreV1().Namespaces().List(ctx, metav1.ListOptions{}) + return err == nil + } + assert.Eventually(t, canListNamespaces, 3*time.Second, 250*time.Millisecond) + require.NoError(t, err) // prints out the error in case of failure require.NotEmpty(t, listNamespaceResponse.Items) }) @@ -136,8 +144,13 @@ func TestSuccessfulCredentialRequest(t *testing.T) { }) // Use the client which is authenticated as the TMC group to list namespaces - listNamespaceResponse, err := clientWithCertFromCredentialRequest.CoreV1().Namespaces().List(ctx, metav1.ListOptions{}) - require.NoError(t, err) + var listNamespaceResponse *v1.NamespaceList + var canListNamespaces = func() bool { + listNamespaceResponse, err = clientWithCertFromCredentialRequest.CoreV1().Namespaces().List(ctx, metav1.ListOptions{}) + return err == nil + } + assert.Eventually(t, canListNamespaces, 3*time.Second, 250*time.Millisecond) + require.NoError(t, err) // prints out the error in case of failure require.NotEmpty(t, listNamespaceResponse.Items) }) }