From f6f0b9ed1fa3bc820aee54d10022f5736be5dd7d Mon Sep 17 00:00:00 2001 From: Stefan Bueringer Date: Tue, 30 Jul 2024 17:12:33 +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.go | 2 +- controllers/servicediscovery_controller_intg_test.go | 10 ++++++++++ controllers/vspherecluster_reconciler_test.go | 6 +++++- internal/test/helpers/envtest.go | 4 ++-- 4 files changed, 18 insertions(+), 4 deletions(-) diff --git a/controllers/serviceaccount_controller.go b/controllers/serviceaccount_controller.go index 674af61d02..96ebec67c0 100644 --- a/controllers/serviceaccount_controller.go +++ b/controllers/serviceaccount_controller.go @@ -247,7 +247,7 @@ func (r *ServiceAccountReconciler) ensureProviderServiceAccounts(ctx context.Con for _, pSvcAccount := range pSvcAccounts { pSvcAccountNames = append(pSvcAccountNames, pSvcAccount.Name) } - log.V(4).Info(fmt.Sprintf("Reconcile ProviderServiceAccounts: %v", strings.Join(pSvcAccountNames, ","))) + log.V(5).Info(fmt.Sprintf("Reconcile ProviderServiceAccounts: %v", strings.Join(pSvcAccountNames, ","))) for i, pSvcAccount := range pSvcAccounts { // Note: We have to use := here to not overwrite log & ctx outside the for loop. diff --git a/controllers/servicediscovery_controller_intg_test.go b/controllers/servicediscovery_controller_intg_test.go index 51dd09c3c7..519663c4ea 100644 --- a/controllers/servicediscovery_controller_intg_test.go +++ b/controllers/servicediscovery_controller_intg_test.go @@ -20,6 +20,7 @@ import ( . "github.com/onsi/ginkgo/v2" . "github.com/onsi/gomega" corev1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "sigs.k8s.io/controller-runtime/pkg/client" helpers "sigs.k8s.io/cluster-api-provider-vsphere/internal/test/helpers/vmware" @@ -37,8 +38,17 @@ var _ = Describe("Service Discovery controller integration tests", func() { 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()) + // 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()) }) AfterEach(func() { + deleteTestResource(ctx, intCtx.Client, intCtx.VSphereCluster) + deleteTestResource(ctx, intCtx.Client, intCtx.Cluster) + deleteTestResource(ctx, intCtx.Client, intCtx.KubeconfigSecret) intCtx.AfterEach() }) diff --git a/controllers/vspherecluster_reconciler_test.go b/controllers/vspherecluster_reconciler_test.go index ca7a245d35..8b886fd93f 100644 --- a/controllers/vspherecluster_reconciler_test.go +++ b/controllers/vspherecluster_reconciler_test.go @@ -358,7 +358,11 @@ var _ = Describe("VIM based VSphere ClusterReconciler", func() { }) AfterEach(func() { - Expect(testEnv.CleanupAndWait(ctx, instance, zoneOne, capiCluster, namespace)).To(Succeed()) + // Note: Make sure VSphereCluster is deleted before the Cluster is deleted. + // Otherwise reconcileDelete in VSphereCluster reconciler will fail because the Cluster cannot be found. + Expect(testEnv.CleanupAndWait(ctx, instance, zoneOne)).To(Succeed()) + Expect(testEnv.CleanupAndWait(ctx, capiCluster)).To(Succeed()) + Expect(testEnv.CleanupAndWait(ctx, namespace)).To(Succeed()) }) It("should reconcile a cluster", func() { diff --git a/internal/test/helpers/envtest.go b/internal/test/helpers/envtest.go index 09fad30771..beb019ac1a 100644 --- a/internal/test/helpers/envtest.go +++ b/internal/test/helpers/envtest.go @@ -62,9 +62,9 @@ import ( ) func init() { - // Set log level 4 as default for testing (default for prod is 2). + // Set log level 5 as default for testing (default for prod is 2). logOptions := logs.NewOptions() - logOptions.Verbosity = 4 + logOptions.Verbosity = 5 if err := logsv1.ValidateAndApply(logOptions, nil); err != nil { panic(err) os.Exit(1)