Skip to content

Commit

Permalink
test: add test cases for multiple import controllers
Browse files Browse the repository at this point in the history
Signed-off-by: Carlos Salas <[email protected]>
  • Loading branch information
salasberryfin committed Jan 16, 2024
1 parent e598205 commit 2025a75
Show file tree
Hide file tree
Showing 4 changed files with 222 additions and 267 deletions.
8 changes: 8 additions & 0 deletions internal/controllers/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,20 @@ import (
"encoding/json"
"errors"
"io"
"strings"

"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime"
yamlDecoder "k8s.io/apimachinery/pkg/util/yaml"
)

func setTemplateParams(template string, params map[string]string) string {
for k, v := range params {
template = strings.ReplaceAll(template, k, v)
}
return template
}

func manifestToObjects(in io.Reader) ([]runtime.Object, error) {
var result []runtime.Object

Expand Down
60 changes: 6 additions & 54 deletions internal/controllers/import_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,10 @@ var _ = Describe("reconcile CAPI Cluster", func() {
r *CAPIImportReconciler
capiCluster *clusterv1.Cluster
rancherCluster *provisioningv1.Cluster
rancherClusterV3 *managementv3.Cluster
clusterRegistrationToken *managementv3.ClusterRegistrationToken
capiKubeconfigSecret *corev1.Secret
clusterName = "generated-rancher-cluster"
testObjectsSuffix = "provisioningv1"
)

BeforeEach(func() {
Expand All @@ -74,13 +74,6 @@ var _ = Describe("reconcile CAPI Cluster", func() {
},
}

rancherClusterV3 = &managementv3.Cluster{
ObjectMeta: metav1.ObjectMeta{
Name: turtlesnaming.Name(capiCluster.Name).ToRancherName(),
Namespace: testNamespace,
},
}

clusterRegistrationToken = &managementv3.ClusterRegistrationToken{
ObjectMeta: metav1.ObjectMeta{
Name: clusterName,
Expand All @@ -100,11 +93,11 @@ var _ = Describe("reconcile CAPI Cluster", func() {
})

AfterEach(func() {
objs, err := manifestToObjects(strings.NewReader(testdata.ImportManifest))
template := setTemplateParams(testdata.ImportManifest, map[string]string{"${TEST_CASE_NAME}": testObjectsSuffix})
objs, err := manifestToObjects(strings.NewReader(template))
clientObjs := []client.Object{
capiCluster,
rancherCluster,
rancherClusterV3,
clusterRegistrationToken,
capiKubeconfigSecret,
}
Expand Down Expand Up @@ -151,29 +144,6 @@ var _ = Describe("reconcile CAPI Cluster", func() {
Expect(cl.Get(ctx, client.ObjectKeyFromObject(rancherCluster), cluster)).ToNot(HaveOccurred())
})

//It("should reconcile a CAPI cluster when rancher cluster doesn't exist using management.cattle.io/v3", func() {

// capiCluster.Labels = map[string]string{
// importLabelName: "true",
// }
// Expect(cl.Create(ctx, capiCluster)).To(Succeed())
// capiCluster.Status.ControlPlaneReady = true
// Expect(cl.Status().Update(ctx, capiCluster)).To(Succeed())

// r.ImportStrategy = ManagementV3ImportStrategy{}
// 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())

// cluster := &managementv3.Cluster{}
// Expect(cl.Get(ctx, client.ObjectKeyFromObject(rancherClusterV3), cluster)).ToNot(HaveOccurred())
//})

It("should reconcile a CAPI cluster when rancher cluster doesn't exist and annotation is set on the namespace", func() {
Expect(cl.Create(ctx, capiCluster)).To(Succeed())
capiCluster.Status.ControlPlaneReady = true
Expand All @@ -192,29 +162,11 @@ var _ = Describe("reconcile CAPI Cluster", func() {
Expect(cl.Get(ctx, client.ObjectKeyFromObject(rancherCluster), cluster)).ToNot(HaveOccurred())
})

//It("should reconcile a CAPI cluster when rancher cluster doesn't exist and annotation is set on the namespace using management.cattle.io/v3", func() {
// Expect(cl.Create(ctx, capiCluster)).To(Succeed())
// capiCluster.Status.ControlPlaneReady = true
// Expect(cl.Status().Update(ctx, capiCluster)).To(Succeed())

// r.ImportStrategy = ManagementV3ImportStrategy{}
// 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())

// cluster := &managementv3.Cluster{}
// Expect(cl.Get(ctx, client.ObjectKeyFromObject(rancherClusterV3), cluster)).ToNot(HaveOccurred())
//})

It("should reconcile a CAPI cluster when rancher cluster exists", func() {
template := setTemplateParams(testdata.ImportManifest, map[string]string{"${TEST_CASE_NAME}": testObjectsSuffix})
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
w.WriteHeader(http.StatusOK)
w.Write([]byte(testdata.ImportManifest))
w.Write([]byte(template))
}))
defer server.Close()

Expand Down Expand Up @@ -244,7 +196,7 @@ var _ = Describe("reconcile CAPI Cluster", func() {
})
Expect(err).ToNot(HaveOccurred())

objs, err := manifestToObjects(strings.NewReader(testdata.ImportManifest))
objs, err := manifestToObjects(strings.NewReader(template))
Expect(err).ToNot(HaveOccurred())

for _, obj := range objs {
Expand Down
Loading

0 comments on commit 2025a75

Please sign in to comment.