Skip to content

Commit

Permalink
Add integration tests for byocluster_controller (#345)
Browse files Browse the repository at this point in the history
* Add integration tests for byocluster_controller

Signed-off-by: chen hui <huchen@vmware.com>
  • Loading branch information
huchen2021 committed Jan 25, 2022
1 parent edba0db commit 3069d0c
Show file tree
Hide file tree
Showing 4 changed files with 168 additions and 5 deletions.
4 changes: 2 additions & 2 deletions controllers/infrastructure/byocluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import (
)

var (
defaultAPIEndpointPort = 6443
DefaultAPIEndpointPort = 6443
clusterControlledType = &infrav1.ByoCluster{}
clusterControlledTypeName = reflect.TypeOf(clusterControlledType).Elem().Name()
clusterControlledTypeGVK = infrav1.GroupVersion.WithKind(clusterControlledTypeName)
Expand Down Expand Up @@ -170,7 +170,7 @@ func (r ByoClusterReconciler) reconcileNormal(ctx context.Context, byoCluster *i
controllerutil.AddFinalizer(byoCluster, infrav1.ClusterFinalizer)

if byoCluster.Spec.ControlPlaneEndpoint.Port == 0 {
byoCluster.Spec.ControlPlaneEndpoint.Port = int32(defaultAPIEndpointPort)
byoCluster.Spec.ControlPlaneEndpoint.Port = int32(DefaultAPIEndpointPort)
}

byoCluster.Status.Ready = true
Expand Down
138 changes: 138 additions & 0 deletions controllers/infrastructure/byocluster_controller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
// Copyright 2021 VMware, Inc. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

package controllers_test

import (
"context"
"fmt"

. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
infrastructurev1beta1 "github.com/vmware-tanzu/cluster-api-provider-bringyourownhost/apis/infrastructure/v1beta1"
controllers "github.com/vmware-tanzu/cluster-api-provider-bringyourownhost/controllers/infrastructure"
"github.com/vmware-tanzu/cluster-api-provider-bringyourownhost/test/builder"
"k8s.io/apimachinery/pkg/types"
"k8s.io/client-go/kubernetes/scheme"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"
"sigs.k8s.io/controller-runtime/pkg/client"
"sigs.k8s.io/controller-runtime/pkg/controller/controllerutil"
"sigs.k8s.io/controller-runtime/pkg/reconcile"
)

var _ = Describe("Controllers/ByoclusterController", func() {

var (
ctx context.Context
k8sClientUncached client.Client
byoCluster *infrastructurev1beta1.ByoCluster
cluster *clusterv1.Cluster
)

BeforeEach(func() {
ctx = context.Background()
var clientErr error

k8sClientUncached, clientErr = client.New(cfg, client.Options{Scheme: scheme.Scheme})
Expect(clientErr).NotTo(HaveOccurred())
})

It("should not throw error when byocluster does not exist", func() {
_, err := byoClusterReconciler.Reconcile(ctx, reconcile.Request{
NamespacedName: types.NamespacedName{
Name: "non-existent-byocluster",
Namespace: "non-existent-namespace"}})
Expect(err).NotTo(HaveOccurred())
})

It("should not throw error when OwnerRef is not set", func() {
byoCluster = builder.ByoCluster(defaultNamespace, "byocluster-not-link-cluster").Build()
Expect(k8sClientUncached.Create(ctx, byoCluster)).Should(Succeed())
WaitForObjectsToBePopulatedInCache(byoCluster)

_, err := byoClusterReconciler.Reconcile(ctx, reconcile.Request{
NamespacedName: types.NamespacedName{
Name: byoCluster.Name,
Namespace: byoCluster.Namespace}})
Expect(err).NotTo(HaveOccurred())
})

It("should not throw error when byocluster is paused", func() {
cluster = builder.Cluster(defaultNamespace, "cluster-paused").
WithPausedField(true).
Build()
Expect(k8sClientUncached.Create(ctx, cluster)).Should(Succeed())
WaitForObjectsToBePopulatedInCache(cluster)

byoCluster = builder.ByoCluster(defaultNamespace, "byocluster-paused").
WithOwnerCluster(cluster).
Build()
Expect(k8sClientUncached.Create(ctx, byoCluster)).Should(Succeed())
WaitForObjectsToBePopulatedInCache(byoCluster)

_, err := byoClusterReconciler.Reconcile(ctx, reconcile.Request{
NamespacedName: types.NamespacedName{
Name: byoCluster.Name,
Namespace: byoCluster.Namespace}})
Expect(err).NotTo(HaveOccurred())
})

It("should be able to delete ByoCluster", func() {
cluster = builder.Cluster(defaultNamespace, "byocluster-deleted").
Build()
Expect(k8sClientUncached.Create(ctx, cluster)).Should(Succeed())
WaitForObjectsToBePopulatedInCache(cluster)

byoCluster = builder.ByoCluster(defaultNamespace, "byocluster-deleted").
WithOwnerCluster(cluster).
Build()
Expect(k8sClientUncached.Create(ctx, byoCluster)).Should(Succeed())
WaitForObjectsToBePopulatedInCache(byoCluster)

byoClusterLookupKey := types.NamespacedName{Name: byoCluster.Name, Namespace: byoCluster.Namespace}
_, err := byoClusterReconciler.Reconcile(ctx, reconcile.Request{
NamespacedName: byoClusterLookupKey})
Expect(err).NotTo(HaveOccurred())

Expect(k8sClientUncached.Delete(ctx, byoCluster)).Should(Succeed())
WaitForObjectToBeUpdatedInCache(byoCluster, func(object client.Object) bool {
return !object.(*infrastructurev1beta1.ByoCluster).ObjectMeta.DeletionTimestamp.IsZero()
})

_, err = byoClusterReconciler.Reconcile(ctx, reconcile.Request{
NamespacedName: byoClusterLookupKey})
Expect(err).NotTo(HaveOccurred())

// assert ByoCluster does not exists
deletedByoCluster := &infrastructurev1beta1.ByoCluster{}
err = k8sClientUncached.Get(ctx, byoClusterLookupKey, deletedByoCluster)
Expect(err).To(MatchError(fmt.Sprintf("byoclusters.infrastructure.cluster.x-k8s.io %q not found", byoClusterLookupKey.Name)))

})

It("should get valid value of fields when ByoClusterController gets a create request", func() {
cluster = builder.Cluster(defaultNamespace, "byocluster-finalizer").
Build()
Expect(k8sClientUncached.Create(ctx, cluster)).Should(Succeed())
WaitForObjectsToBePopulatedInCache(cluster)

byoCluster = builder.ByoCluster(defaultNamespace, "byocluster-finalizer").
WithOwnerCluster(cluster).
Build()
Expect(k8sClientUncached.Create(ctx, byoCluster)).Should(Succeed())
WaitForObjectsToBePopulatedInCache(byoCluster)

byoClusterLookupKey := types.NamespacedName{Name: byoCluster.Name, Namespace: byoCluster.Namespace}
_, err := byoClusterReconciler.Reconcile(ctx, reconcile.Request{
NamespacedName: byoClusterLookupKey})
Expect(err).NotTo(HaveOccurred())

createdByoCluster := &infrastructurev1beta1.ByoCluster{}
err = k8sClientUncached.Get(ctx, byoClusterLookupKey, createdByoCluster)
Expect(err).ToNot(HaveOccurred())
Expect(controllerutil.ContainsFinalizer(createdByoCluster, infrastructurev1beta1.ClusterFinalizer)).To(BeTrue())
Expect(createdByoCluster.Status.Ready).To(BeTrue())
Expect(createdByoCluster.Spec.ControlPlaneEndpoint.Port).To(Equal(int32(controllers.DefaultAPIEndpointPort)))
})

})
7 changes: 7 additions & 0 deletions controllers/infrastructure/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ var (
testEnv *envtest.Environment
clientFake client.Client
reconciler *controllers.ByoMachineReconciler
byoClusterReconciler *controllers.ByoClusterReconciler
recorder *record.FakeRecorder
byoCluster *infrastructurev1beta1.ByoCluster
capiCluster *clusterv1.Cluster
Expand Down Expand Up @@ -121,6 +122,12 @@ var _ = BeforeSuite(func() {
err = reconciler.SetupWithManager(context.TODO(), k8sManager)
Expect(err).NotTo(HaveOccurred())

byoClusterReconciler = &controllers.ByoClusterReconciler{
Client: k8sManager.GetClient(),
}
err = byoClusterReconciler.SetupWithManager(k8sManager)
Expect(err).NotTo(HaveOccurred())

go func() {
err = k8sManager.GetCache().Start(context.TODO())
Expect(err).NotTo(HaveOccurred())
Expand Down
24 changes: 21 additions & 3 deletions test/builder/builders.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ type ByoClusterBuilder struct {
name string
bundleRegistry string
bundleTag string
cluster *clusterv1.Cluster
}

// ByoCluster returns a ByoClusterBuilder with the given name and namespace
Expand All @@ -146,13 +147,19 @@ func ByoCluster(namespace, name string) *ByoClusterBuilder {
}
}

// WithPausedField adds the passed paused value to the ByoClusterBuilder
// WithOwnerCluster adds the passed Owner Cluster to the ByoClusterBuilder
func (c *ByoClusterBuilder) WithOwnerCluster(cluster *clusterv1.Cluster) *ByoClusterBuilder {
c.cluster = cluster
return c
}

// WithBundleBaseRegistry adds the passed registry value to the ByoClusterBuilder
func (c *ByoClusterBuilder) WithBundleBaseRegistry(registry string) *ByoClusterBuilder {
c.bundleRegistry = registry
return c
}

// WithPausedField adds the passed paused value to the ByoClusterBuilder
// WithBundleTag adds the passed bundleTag value to the ByoClusterBuilder
func (c *ByoClusterBuilder) WithBundleTag(tag string) *ByoClusterBuilder {
c.bundleTag = tag
return c
Expand All @@ -172,6 +179,17 @@ func (c *ByoClusterBuilder) Build() *infrastructurev1beta1.ByoCluster {
Spec: infrastructurev1beta1.ByoClusterSpec{},
}

if c.cluster != nil {
cluster.ObjectMeta.OwnerReferences = []metav1.OwnerReference{
{
Kind: "Cluster",
Name: c.cluster.Name,
APIVersion: clusterv1.GroupVersion.String(),
UID: c.cluster.UID,
},
}
}

if c.bundleRegistry != "" {
cluster.Spec.BundleLookupBaseRegistry = c.bundleRegistry
}
Expand Down Expand Up @@ -255,7 +273,7 @@ func (c *ClusterBuilder) WithPausedField(paused bool) *ClusterBuilder {
return c
}

// WithPausedField adds the passed paused value to the ClusterBuilder
// WithInfrastructureRef adds the passed byoCluster value to the ClusterBuilder
func (c *ClusterBuilder) WithInfrastructureRef(byoCluster *infrastructurev1beta1.ByoCluster) *ClusterBuilder {
c.byoCluster = byoCluster
return c
Expand Down

0 comments on commit 3069d0c

Please sign in to comment.