Skip to content

Commit

Permalink
Improve unit tests
Browse files Browse the repository at this point in the history
Signed-off-by: Stefan Büringer [email protected]
  • Loading branch information
sbueringer committed Jul 30, 2024
1 parent 14bdbb0 commit f6f0b9e
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 4 deletions.
2 changes: 1 addition & 1 deletion controllers/serviceaccount_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
10 changes: 10 additions & 0 deletions controllers/servicediscovery_controller_intg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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()
})

Expand Down
6 changes: 5 additions & 1 deletion controllers/vspherecluster_reconciler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down
4 changes: 2 additions & 2 deletions internal/test/helpers/envtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Check failure on line 70 in internal/test/helpers/envtest.go

View workflow job for this annotation

GitHub Actions / lint

unreachable: unreachable code (govet)
Expand Down

0 comments on commit f6f0b9e

Please sign in to comment.