Skip to content

Commit

Permalink
Added unit test + fixed namespace
Browse files Browse the repository at this point in the history
  • Loading branch information
ChristianAtDell authored and nitesh3108 committed Oct 21, 2024
1 parent 94e46ac commit 348e216
Show file tree
Hide file tree
Showing 2 changed files with 76 additions and 2 deletions.
5 changes: 3 additions & 2 deletions pkg/modules/replication.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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",
},
}

Expand Down
73 changes: 73 additions & 0 deletions pkg/modules/replication_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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)
}
}

0 comments on commit 348e216

Please sign in to comment.