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 25174d5 commit 14bdbb0
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 3 deletions.
3 changes: 2 additions & 1 deletion controllers/controllers_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ func TestControllers(t *testing.T) {

var (
testEnv *helpers.TestEnvironment
tracker *remote.ClusterCacheTracker
ctx = ctrl.SetupSignalHandler()
)

Expand Down Expand Up @@ -80,7 +81,7 @@ func setup() {
panic("unable to create secret caching client")
}

tracker, err := remote.NewClusterCacheTracker(
tracker, err = remote.NewClusterCacheTracker(
testEnv.Manager,
remote.ClusterCacheTrackerOptions{
SecretCachingClient: secretCachingClient,
Expand Down
7 changes: 7 additions & 0 deletions controllers/serviceaccount_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"context"
"fmt"
"reflect"
"strings"
"time"

"github.com/pkg/errors"
Expand Down Expand Up @@ -242,6 +243,12 @@ func (r *ServiceAccountReconciler) reconcileNormal(ctx context.Context, guestClu
func (r *ServiceAccountReconciler) ensureProviderServiceAccounts(ctx context.Context, guestClusterCtx *vmwarecontext.GuestClusterContext, pSvcAccounts []vmwarev1.ProviderServiceAccount) error {
log := ctrl.LoggerFrom(ctx)

var pSvcAccountNames []string
for _, pSvcAccount := range pSvcAccounts {
pSvcAccountNames = append(pSvcAccountNames, pSvcAccount.Name)
}
log.V(4).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.
log := log.WithValues("ProviderServiceAccount", klog.KRef(pSvcAccount.Namespace, pSvcAccount.Name))
Expand Down
11 changes: 9 additions & 2 deletions controllers/serviceaccount_controller_intg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,21 @@ var _ = Describe("ProviderServiceAccount controller integration tests", func() {
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())

pSvcAccount = getTestProviderServiceAccount(intCtx.Namespace, intCtx.VSphereCluster)
createTestResource(ctx, intCtx.Client, pSvcAccount)
assertEventuallyExistsInNamespace(ctx, intCtx.Client, intCtx.Namespace, pSvcAccount.GetName(), pSvcAccount)
})
AfterEach(func() {
// Deleting the provider service account is not strictly required as the context itself
// gets teared down but keeping it for clarity.
deleteTestResource(ctx, intCtx.Client, pSvcAccount)
deleteTestResource(ctx, intCtx.Client, intCtx.VSphereCluster)
deleteTestResource(ctx, intCtx.Client, intCtx.Cluster)
deleteTestResource(ctx, intCtx.Client, intCtx.KubeconfigSecret)
})

Context("When serviceaccount secret is created", func() {
Expand Down
10 changes: 10 additions & 0 deletions internal/test/helpers/envtest.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ import (
"k8s.io/apimachinery/pkg/util/wait"
clientgoscheme "k8s.io/client-go/kubernetes/scheme"
"k8s.io/client-go/rest"
"k8s.io/component-base/logs"
logsv1 "k8s.io/component-base/logs/api/v1"
"k8s.io/klog/v2"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/cluster-api/util/kubeconfig"
Expand All @@ -60,7 +62,15 @@ import (
)

func init() {
// Set log level 4 as default for testing (default for prod is 2).
logOptions := logs.NewOptions()
logOptions.Verbosity = 4
if err := logsv1.ValidateAndApply(logOptions, nil); err != nil {
panic(err)
os.Exit(1)
}
ctrl.SetLogger(klog.Background())

// add logger for ginkgo
klog.SetOutput(ginkgo.GinkgoWriter)
}
Expand Down

0 comments on commit 14bdbb0

Please sign in to comment.