From 078901ad8b98a135fdd1f186294fccdca4442d95 Mon Sep 17 00:00:00 2001 From: Christian Schlotter Date: Mon, 19 Feb 2024 12:00:39 +0100 Subject: [PATCH] controllers: use apireader to not assert on cached information --- .../serviceaccount_controller_suite_test.go | 4 ++-- .../servicediscovery_controller_intg_test.go | 8 ++++---- .../servicediscovery_controller_suite_test.go | 7 ++++--- .../test/helpers/vmware/intg_test_context.go | 18 +++++++++++++----- 4 files changed, 23 insertions(+), 14 deletions(-) diff --git a/controllers/serviceaccount_controller_suite_test.go b/controllers/serviceaccount_controller_suite_test.go index 4dc971f622..14b682435e 100644 --- a/controllers/serviceaccount_controller_suite_test.go +++ b/controllers/serviceaccount_controller_suite_test.go @@ -58,11 +58,11 @@ func createTargetSecretWithInvalidToken(ctx context.Context, guestClient client. Expect(guestClient.Create(ctx, secret)).To(Succeed()) } -func assertEventuallyExistsInNamespace(ctx context.Context, c client.Client, namespace, name string, obj client.Object) { +func assertEventuallyExistsInNamespace(ctx context.Context, c client.Reader, namespace, name string, obj client.Object) { EventuallyWithOffset(2, func() error { key := client.ObjectKey{Namespace: namespace, Name: name} return c.Get(ctx, key, obj) - }).Should(Succeed()) + }, time.Second*3).Should(Succeed()) } func assertNoEntities(ctx context.Context, ctrlClient client.Client, namespace string) { diff --git a/controllers/servicediscovery_controller_intg_test.go b/controllers/servicediscovery_controller_intg_test.go index d9c637a43b..b54b121256 100644 --- a/controllers/servicediscovery_controller_intg_test.go +++ b/controllers/servicediscovery_controller_intg_test.go @@ -51,8 +51,8 @@ var _ = Describe("Service Discovery controller integration tests", func() { It("Should reconcile headless svc", func() { By("creating a service and endpoints using the VIP in the guest cluster") headlessSvc := &corev1.Service{} - assertEventuallyExistsInNamespace(ctx, intCtx.Client, "kube-system", "kube-apiserver-lb-svc", headlessSvc) - assertHeadlessSvcWithVIPEndpoints(ctx, intCtx.GuestClient, supervisorHeadlessSvcNamespace, supervisorHeadlessSvcName) + assertEventuallyExistsInNamespace(ctx, testEnv.Manager.GetAPIReader(), "kube-system", "kube-apiserver-lb-svc", headlessSvc) + assertHeadlessSvcWithVIPEndpoints(ctx, intCtx.GuestAPIReader, supervisorHeadlessSvcNamespace, supervisorHeadlessSvcName) }) }) @@ -67,7 +67,7 @@ var _ = Describe("Service Discovery controller integration tests", func() { }) It("Should reconcile headless svc", func() { By("creating a service and endpoints using the FIP in the guest cluster") - assertHeadlessSvcWithFIPEndpoints(ctx, intCtx.GuestClient, supervisorHeadlessSvcNamespace, supervisorHeadlessSvcName) + assertHeadlessSvcWithFIPEndpoints(ctx, intCtx.GuestAPIReader, supervisorHeadlessSvcNamespace, supervisorHeadlessSvcName) }) }) Context("When headless svc and endpoints already exists", func() { @@ -86,7 +86,7 @@ var _ = Describe("Service Discovery controller integration tests", func() { }) It("Should reconcile headless svc", func() { By("updating the service and endpoints using the VIP in the guest cluster") - assertHeadlessSvcWithUpdatedVIPEndpoints(ctx, intCtx.GuestClient, supervisorHeadlessSvcNamespace, supervisorHeadlessSvcName) + assertHeadlessSvcWithUpdatedVIPEndpoints(ctx, intCtx.GuestAPIReader, supervisorHeadlessSvcNamespace, supervisorHeadlessSvcName) }) }) }) diff --git a/controllers/servicediscovery_controller_suite_test.go b/controllers/servicediscovery_controller_suite_test.go index f50e1d4fa6..3b131c2450 100644 --- a/controllers/servicediscovery_controller_suite_test.go +++ b/controllers/servicediscovery_controller_suite_test.go @@ -20,6 +20,7 @@ import ( "context" "strconv" "strings" + "time" . "github.com/onsi/gomega" corev1 "k8s.io/api/core/v1" @@ -73,10 +74,10 @@ func assertEventuallyDoesNotExistInNamespace(ctx context.Context, guestClient cl func assertHeadlessSvc(ctx context.Context, guestClient client.Client, namespace, name string) { headlessSvc := &corev1.Service{} - EventuallyWithOffset(4, func() error { + Eventually(func() error { key := client.ObjectKey{Namespace: namespace, Name: name} return guestClient.Get(ctx, key, headlessSvc) - }).Should(Succeed()) + }, time.Second*3).Should(Succeed()) Expect(headlessSvc.Spec.Ports[0].Port).To(Equal(int32(supervisorHeadlessSvcPort))) Expect(headlessSvc.Spec.Ports[0].TargetPort.IntVal).To(Equal(int32(supervisorAPIServerPort))) } @@ -137,7 +138,7 @@ func assertHeadlessSvcWithUpdatedVIPEndpoints(ctx context.Context, guestClient c assertHeadlessSvc(ctx, guestClient, namespace, name) headlessEndpoints := &corev1.Endpoints{} assertEventuallyExistsInNamespace(ctx, guestClient, namespace, name, headlessEndpoints) - EventuallyWithOffset(2, func() string { + Eventually(func() string { key := client.ObjectKey{Namespace: namespace, Name: name} Expect(guestClient.Get(ctx, key, headlessEndpoints)).Should(Succeed()) return headlessEndpoints.Subsets[0].Addresses[0].IP diff --git a/internal/test/helpers/vmware/intg_test_context.go b/internal/test/helpers/vmware/intg_test_context.go index 9a89a0a419..e17f4e7a4b 100644 --- a/internal/test/helpers/vmware/intg_test_context.go +++ b/internal/test/helpers/vmware/intg_test_context.go @@ -42,6 +42,7 @@ import ( type IntegrationTestContext struct { Client client.Client GuestClient client.Client + GuestAPIReader client.Client Namespace string VSphereCluster *vmwarev1.VSphereCluster Cluster *clusterv1.Cluster @@ -101,11 +102,6 @@ func NewIntegrationTestContextWithClusters(ctx context.Context, integrationTestC vsphereClusterName := capiutil.RandomString(6) testCtx.Cluster = createCluster(ctx, integrationTestClient, testCtx.Namespace, vsphereClusterName) - By("Create a vsphere cluster and wait for it to exist", func() { - testCtx.VSphereCluster = createVSphereCluster(ctx, integrationTestClient, testCtx.Namespace, vsphereClusterName, testCtx.Cluster.GetName()) - testCtx.VSphereClusterKey = client.ObjectKeyFromObject(testCtx.VSphereCluster) - }) - var config *rest.Config By("Creating guest cluster control plane", func() { @@ -121,6 +117,13 @@ func NewIntegrationTestContextWithClusters(ctx context.Context, integrationTestC Expect(err).ShouldNot(HaveOccurred()) Expect(testCtx.GuestClient).ShouldNot(BeNil()) + // Create the API Reader, a client with no cache. + testCtx.GuestAPIReader, err = client.New(config, client.Options{ + Scheme: testCtx.GuestClient.Scheme(), + Mapper: testCtx.GuestClient.RESTMapper(), + }) + Expect(err).ShouldNot(HaveOccurred()) + testCtx.envTest = envTest }) By("Create the kubeconfig secret for the cluster", func() { @@ -149,6 +152,11 @@ func NewIntegrationTestContextWithClusters(ctx context.Context, integrationTestC }).Should(Succeed()) }) + By("Create a vsphere cluster and wait for it to exist", func() { + testCtx.VSphereCluster = createVSphereCluster(ctx, integrationTestClient, testCtx.Namespace, vsphereClusterName, testCtx.Cluster.GetName()) + testCtx.VSphereClusterKey = client.ObjectKeyFromObject(testCtx.VSphereCluster) + }) + return testCtx }