From 4f3dbcb3c25c57589516f07860488b29f3c94867 Mon Sep 17 00:00:00 2001 From: Stefan Bueringer Date: Fri, 2 Aug 2024 13:50:16 +0200 Subject: [PATCH] Improve unit tests MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Stefan Büringer buringerst@vmware.com --- controllers/serviceaccount_controller_intg_test.go | 14 ++++++++++---- .../servicediscovery_controller_intg_test.go | 13 ++++++++++--- 2 files changed, 20 insertions(+), 7 deletions(-) diff --git a/controllers/serviceaccount_controller_intg_test.go b/controllers/serviceaccount_controller_intg_test.go index 5df20d6294..93e82005b9 100644 --- a/controllers/serviceaccount_controller_intg_test.go +++ b/controllers/serviceaccount_controller_intg_test.go @@ -17,7 +17,9 @@ limitations under the License. package controllers import ( + "fmt" "reflect" + "time" "github.com/google/uuid" . "github.com/onsi/ginkgo/v2" @@ -53,15 +55,19 @@ var _ = Describe("ProviderServiceAccount controller integration tests", func() { targetNSObj *corev1.Namespace ) BeforeEach(func() { - By("Creating the Cluster, vSphereCluster and KubeconfigSecret", func() { + By(fmt.Sprintf("Creating the Cluster (%s), vSphereCluster (%s) and KubeconfigSecret", intCtx.Cluster.Name, intCtx.VSphereCluster.Name), func() { helpers.CreateAndWait(ctx, intCtx.Client, intCtx.Cluster) helpers.CreateAndWait(ctx, intCtx.Client, intCtx.VSphereCluster) helpers.CreateAndWait(ctx, intCtx.Client, intCtx.KubeconfigSecret) }) By("Verifying that the guest cluster client works") - guestClient, err := tracker.GetClient(ctx, client.ObjectKeyFromObject(intCtx.Cluster)) - Expect(err).ToNot(HaveOccurred()) + var guestClient client.Client + var err error + Eventually(func() error { + guestClient, err = tracker.GetClient(ctx, client.ObjectKeyFromObject(intCtx.Cluster)) + return err + }, time.Minute, 5*time.Second).Should(Succeed()) // Note: Create a Service informer, so the test later doesn't fail if this doesn't work. Expect(guestClient.List(ctx, &corev1.ServiceList{}, client.InNamespace(metav1.NamespaceDefault))).To(Succeed()) @@ -181,7 +187,7 @@ var _ = Describe("ProviderServiceAccount controller integration tests", func() { var role *rbacv1.Role var roleBinding *rbacv1.RoleBinding BeforeEach(func() { - By("Creating the Cluster, vSphereCluster and KubeconfigSecret", func() { + By(fmt.Sprintf("Creating the Cluster (%s), vSphereCluster (%s) and KubeconfigSecret", intCtx.Cluster.Name, intCtx.VSphereCluster.Name), func() { helpers.CreateAndWait(ctx, intCtx.Client, intCtx.Cluster) helpers.CreateAndWait(ctx, intCtx.Client, intCtx.VSphereCluster) helpers.CreateAndWait(ctx, intCtx.Client, intCtx.KubeconfigSecret) diff --git a/controllers/servicediscovery_controller_intg_test.go b/controllers/servicediscovery_controller_intg_test.go index 519663c4ea..edbb5c835d 100644 --- a/controllers/servicediscovery_controller_intg_test.go +++ b/controllers/servicediscovery_controller_intg_test.go @@ -17,6 +17,9 @@ limitations under the License. package controllers import ( + "fmt" + "time" + . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" corev1 "k8s.io/api/core/v1" @@ -33,15 +36,19 @@ var _ = Describe("Service Discovery controller integration tests", func() { ) BeforeEach(func() { intCtx = helpers.NewIntegrationTestContextWithClusters(ctx, testEnv.Manager.GetClient()) - By("Creating the Cluster, vSphereCluster and KubeconfigSecret", func() { + By(fmt.Sprintf("Creating the Cluster (%s), vSphereCluster (%s) and KubeconfigSecret", intCtx.Cluster.Name, intCtx.VSphereCluster.Name), func() { helpers.CreateAndWait(ctx, intCtx.Client, intCtx.Cluster) helpers.CreateAndWait(ctx, intCtx.Client, intCtx.VSphereCluster) helpers.CreateAndWait(ctx, intCtx.Client, intCtx.KubeconfigSecret) }) By("Verifying that the guest cluster client works") - guestClient, err := tracker.GetClient(ctx, client.ObjectKeyFromObject(intCtx.Cluster)) - Expect(err).ToNot(HaveOccurred()) + var guestClient client.Client + var err error + Eventually(func() error { + guestClient, err = tracker.GetClient(ctx, client.ObjectKeyFromObject(intCtx.Cluster)) + return err + }, time.Minute, 5*time.Second).Should(Succeed()) // Note: Create a Service informer, so the test later doesn't fail if this doesn't work. Expect(guestClient.List(ctx, &corev1.ServiceList{}, client.InNamespace(metav1.NamespaceDefault))).To(Succeed()) })