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.
This commit is contained in:
Ryan Richard 2020-08-17 16:28:12 -07:00
parent 769ef71db7
commit d8d49be5d9

View File

@ -13,6 +13,9 @@ import (
"testing" "testing"
"time" "time"
"github.com/stretchr/testify/assert"
v1 "k8s.io/api/core/v1"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
rbacv1 "k8s.io/api/rbac/v1" rbacv1 "k8s.io/api/rbac/v1"
"k8s.io/apimachinery/pkg/api/errors" "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 // Use the client which is authenticated as the TMC user to list namespaces
listNamespaceResponse, err := clientWithCertFromCredentialRequest.CoreV1().Namespaces().List(ctx, metav1.ListOptions{}) var listNamespaceResponse *v1.NamespaceList
require.NoError(t, err) 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) 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 // Use the client which is authenticated as the TMC group to list namespaces
listNamespaceResponse, err := clientWithCertFromCredentialRequest.CoreV1().Namespaces().List(ctx, metav1.ListOptions{}) var listNamespaceResponse *v1.NamespaceList
require.NoError(t, err) 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) require.NotEmpty(t, listNamespaceResponse.Items)
}) })
} }