Skip to content

Commit

Permalink
Add test case on CRT creation
Browse files Browse the repository at this point in the history
Signed-off-by: Danil Grigorev <danil.grigorev@suse.com>
  • Loading branch information
Danil-Grigorev committed Nov 16, 2023
1 parent 0146b57 commit 88f6f85
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
41 changes: 37 additions & 4 deletions internal/controllers/import_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,15 @@ var _ = Describe("reconcile CAPI Cluster", func() {
rancherCluster *provisioningv1.Cluster
clusterRegistrationToken *managementv3.ClusterRegistrationToken
capiKubeconfigSecret *corev1.Secret
clusterName = "generated-rancher-cluster"
)

BeforeEach(func() {
r = &CAPIImportReconciler{
Client: cl,
RancherClient: cl, // rancher and rancher-turtles deployed in the same cluster
remoteClientGetter: remote.NewClusterClient,
Scheme: testEnv.Scheme,
}

capiCluster = &clusterv1.Cluster{
Expand All @@ -78,7 +80,7 @@ var _ = Describe("reconcile CAPI Cluster", func() {

clusterRegistrationToken = &managementv3.ClusterRegistrationToken{
ObjectMeta: metav1.ObjectMeta{
Name: clusterRegistrationTokenName,
Name: clusterName,
Namespace: testNamespace,
},
}
Expand Down Expand Up @@ -179,7 +181,7 @@ var _ = Describe("reconcile CAPI Cluster", func() {
Expect(cl.Create(ctx, rancherCluster)).To(Succeed())
cluster := &provisioningv1.Cluster{}
Expect(cl.Get(ctx, client.ObjectKeyFromObject(rancherCluster), cluster)).To(Succeed())
cluster.Status.ClusterName = testNamespace
cluster.Status.ClusterName = clusterName
Expect(cl.Status().Update(ctx, cluster)).To(Succeed())

Expect(cl.Create(ctx, clusterRegistrationToken)).To(Succeed())
Expand Down Expand Up @@ -266,7 +268,7 @@ var _ = Describe("reconcile CAPI Cluster", func() {
Expect(cl.Create(ctx, rancherCluster)).To(Succeed())
cluster := &provisioningv1.Cluster{}
Expect(cl.Get(ctx, client.ObjectKeyFromObject(rancherCluster), cluster)).ToNot(HaveOccurred())
cluster.Status.ClusterName = testNamespace
cluster.Status.ClusterName = clusterName
Expect(cl.Status().Update(ctx, cluster)).To(Succeed())

Expect(cl.Create(ctx, clusterRegistrationToken)).To(Succeed())
Expand All @@ -285,6 +287,37 @@ var _ = Describe("reconcile CAPI Cluster", func() {
Expect(res.Requeue).To(BeTrue())
})

It("should reconcile a CAPI cluster when rancher cluster exists and a cluster registration token does not exist", func() {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte(""))
}))
defer server.Close()

Expect(cl.Create(ctx, capiCluster)).To(Succeed())
capiCluster.Status.ControlPlaneReady = true
Expect(cl.Status().Update(ctx, capiCluster)).To(Succeed())

Expect(cl.Create(ctx, capiKubeconfigSecret)).To(Succeed())

Expect(cl.Create(ctx, rancherCluster)).To(Succeed())
cluster := &provisioningv1.Cluster{}
Expect(cl.Get(ctx, client.ObjectKeyFromObject(rancherCluster), cluster)).ToNot(HaveOccurred())
cluster.Status.ClusterName = clusterName
Expect(cl.Status().Update(ctx, cluster)).To(Succeed())

res, err := r.Reconcile(ctx, reconcile.Request{
NamespacedName: types.NamespacedName{
Namespace: capiCluster.Namespace,
Name: capiCluster.Name,
},
})
Expect(err).ToNot(HaveOccurred())
Expect(res.Requeue).To(BeTrue())

Expect(cl.Get(ctx, client.ObjectKeyFromObject(clusterRegistrationToken), clusterRegistrationToken)).ToNot(HaveOccurred())
})

It("should reconcile a CAPI cluster when rancher cluster exists and registration manifests url is empty", func() {
Expect(cl.Create(ctx, capiCluster)).To(Succeed())
capiCluster.Status.ControlPlaneReady = true
Expand All @@ -295,7 +328,7 @@ var _ = Describe("reconcile CAPI Cluster", func() {
Expect(cl.Create(ctx, rancherCluster)).To(Succeed())
cluster := &provisioningv1.Cluster{}
Expect(cl.Get(ctx, client.ObjectKeyFromObject(rancherCluster), cluster)).ToNot(HaveOccurred())
cluster.Status.ClusterName = testNamespace
cluster.Status.ClusterName = clusterName
Expect(cl.Status().Update(ctx, cluster)).To(Succeed())

Expect(cl.Create(ctx, clusterRegistrationToken)).To(Succeed())
Expand Down
13 changes: 13 additions & 0 deletions internal/controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ import (
"github.com/rancher-sandbox/rancher-turtles/internal/test"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

managementv3 "github.com/rancher-sandbox/rancher-turtles/internal/rancher/management/v3"
provisioningv1 "github.com/rancher-sandbox/rancher-turtles/internal/rancher/provisioning/v1"
clusterv1 "sigs.k8s.io/cluster-api/api/v1beta1"

"k8s.io/apimachinery/pkg/runtime"
utilruntime "k8s.io/apimachinery/pkg/util/runtime"
"k8s.io/client-go/rest"
"k8s.io/client-go/tools/clientcmd"
"k8s.io/client-go/tools/clientcmd/api"
Expand Down Expand Up @@ -59,7 +66,13 @@ var _ = BeforeSuite(func() {
filepath.Join("..", "..", "hack", "crd", "bases"),
},
ErrorIfCRDPathMissing: true,
Scheme: runtime.NewScheme(),
}

utilruntime.Must(clusterv1.AddToScheme(testEnv.Scheme))
utilruntime.Must(provisioningv1.AddToScheme(testEnv.Scheme))
utilruntime.Must(managementv3.AddToScheme(testEnv.Scheme))

cfg, cl, err = test.StartEnvTest(testEnv)
Expect(err).NotTo(HaveOccurred())
Expect(cfg).NotTo(BeNil())
Expand Down

0 comments on commit 88f6f85

Please sign in to comment.