From 80471441197e34903d89f7453b14126ffc9af6fd Mon Sep 17 00:00:00 2001 From: Danil Grigorev Date: Thu, 16 Nov 2023 14:43:07 +0100 Subject: [PATCH] Add test case on CRT creation Signed-off-by: Danil Grigorev --- .../controllers/import_controller_test.go | 41 +++++++++++++++++-- internal/controllers/suite_test.go | 13 ++++++ 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/internal/controllers/import_controller_test.go b/internal/controllers/import_controller_test.go index 3e7bb4735..2462d62be 100644 --- a/internal/controllers/import_controller_test.go +++ b/internal/controllers/import_controller_test.go @@ -53,6 +53,7 @@ var _ = Describe("reconcile CAPI Cluster", func() { rancherCluster *provisioningv1.Cluster clusterRegistrationToken *managementv3.ClusterRegistrationToken capiKubeconfigSecret *corev1.Secret + clusterName = "generated-rancher-cluster" ) BeforeEach(func() { @@ -60,6 +61,7 @@ var _ = Describe("reconcile CAPI Cluster", func() { Client: cl, RancherClient: cl, // rancher and rancher-turtles deployed in the same cluster remoteClientGetter: remote.NewClusterClient, + Scheme: testEnv.Scheme, } capiCluster = &clusterv1.Cluster{ @@ -78,7 +80,7 @@ var _ = Describe("reconcile CAPI Cluster", func() { clusterRegistrationToken = &managementv3.ClusterRegistrationToken{ ObjectMeta: metav1.ObjectMeta{ - Name: clusterRegistrationTokenName, + Name: clusterName, Namespace: testNamespace, }, } @@ -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()) @@ -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()) @@ -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 @@ -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()) diff --git a/internal/controllers/suite_test.go b/internal/controllers/suite_test.go index fa9d3d14f..8a72ae9f8 100644 --- a/internal/controllers/suite_test.go +++ b/internal/controllers/suite_test.go @@ -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" @@ -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())