From 348e216c8522b8662c5ca5adb3da20d7e1454a06 Mon Sep 17 00:00:00 2001 From: Christian Coffield Date: Fri, 18 Oct 2024 16:34:19 -0400 Subject: [PATCH] Added unit test + fixed namespace --- pkg/modules/replication.go | 5 ++- pkg/modules/replication_test.go | 73 +++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 2 deletions(-) diff --git a/pkg/modules/replication.go b/pkg/modules/replication.go index a184217aa..91fbf1770 100644 --- a/pkg/modules/replication.go +++ b/pkg/modules/replication.go @@ -15,11 +15,12 @@ package modules import ( "context" "fmt" + "strings" + corev1 "k8s.io/api/core/v1" k8serrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" t1 "k8s.io/apimachinery/pkg/types" - "strings" csmv1 "github.com/dell/csm-operator/api/v1" @@ -466,7 +467,7 @@ func DeleteReplicationConfigmap(cr csmv1.ContainerStorageModule, ctrlClient clie configMap := &corev1.ConfigMap{ ObjectMeta: metav1.ObjectMeta{ Name: "dell-replication-controller-config", - Namespace: cr.Namespace, + Namespace: "dell-replication-controller", }, } diff --git a/pkg/modules/replication_test.go b/pkg/modules/replication_test.go index cf4f9a3c8..67fd59ce8 100644 --- a/pkg/modules/replication_test.go +++ b/pkg/modules/replication_test.go @@ -18,8 +18,12 @@ import ( utils "github.com/dell/csm-operator/pkg/utils" "github.com/dell/csm-operator/tests/shared" "github.com/stretchr/testify/assert" + corev1 "k8s.io/api/core/v1" rbacv1 "k8s.io/api/rbac/v1" + k8serrors "k8s.io/apimachinery/pkg/api/errors" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/runtime" + t1 "k8s.io/apimachinery/pkg/types" applyv1 "k8s.io/client-go/applyconfigurations/apps/v1" "k8s.io/client-go/kubernetes" "k8s.io/client-go/kubernetes/fake" @@ -406,3 +410,72 @@ func TestReplicationManagerController(t *testing.T) { }) } } + +func TestReplicationConfigmap(t *testing.T) { + // Create a fake client to use in the test + scheme := runtime.NewScheme() + _ = corev1.AddToScheme(scheme) + fakeClient := ctrlClientFake.NewClientBuilder().WithScheme(scheme).Build() + + // Create a test ContainerStorageModule + cr, err := getCustomResource("./testdata/cr_powerscale_replica.yaml") + if err != nil { + panic(err) + } + + // Call the function we want to test + // we can't use test config, as it doesn't have versionvalues + var realConfig = utils.OperatorConfig{ + ConfigDirectory: "../../operatorconfig", + } + objs, err := CreateReplicationConfigmap(context.Background(), cr, realConfig, fakeClient) + + // Check that the function returned the expected results + if err != nil { + t.Errorf("CreateReplicationConfigmap returned an unexpected error: %v", err) + } + + if len(objs) != 1 { + t.Errorf("CreateReplicationConfigmap returned the wrong number of objects: %d", len(objs)) + } + + cm, ok := objs[0].(*corev1.ConfigMap) + if !ok { + t.Errorf("CreateReplicationConfigmap returned the wrong type of object: %T", objs[0]) + } + + if cm.Name != "dell-replication-controller-config" { + t.Errorf("CreateReplicationConfigmap returned the wrong ConfigMap name: %s", cm.Name) + } + + if cm.Namespace != "dell-replication-controller" { + t.Errorf("CreateReplicationConfigmap returned the wrong ConfigMap namespace: %s", cm.Namespace) + } + + // Check that the ConfigMap was created in the fake client + foundConfigMap := &corev1.ConfigMap{} + err = fakeClient.Get(context.Background(), t1.NamespacedName{Name: "dell-replication-controller-config", Namespace: "dell-replication-controller"}, foundConfigMap) + if err != nil { + t.Errorf("ConfigMap was not created in the fake client: %v", err) + } + + // Now verify that the ConfigMap can be deleted properly + // Call the function we want to test + if err := DeleteReplicationConfigmap(cr, fakeClient); err != nil { + t.Errorf("DeleteReplicationConfigmap returned an unexpected error: %v", err) + } + + // Check that the ConfigMap was deleted from the fake client + configMap := &corev1.ConfigMap{ + ObjectMeta: metav1.ObjectMeta{ + Name: "dell-replication-controller-config", + Namespace: "dell-replication-controller", + }, + } + err = fakeClient.Get(context.Background(), t1.NamespacedName{Name: cm.Name, Namespace: cm.Namespace}, configMap) + if err == nil { + t.Errorf("ConfigMap was not deleted from the fake client") + } else if !k8serrors.IsNotFound(err) { + t.Errorf("ConfigMap was not deleted from the fake client: %v", err) + } +}