Skip to content

Commit

Permalink
test: Add unit test
Browse files Browse the repository at this point in the history
Signed-off-by: Shubham Gupta <iamshubhamgupta2001@gmail.com>
  • Loading branch information
shubham-cmyk committed Mar 30, 2024
1 parent eb1cc0d commit e58ab50
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 5 deletions.
27 changes: 24 additions & 3 deletions k8sutils/redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"strings"
"testing"

"github.com/OT-CONTAINER-KIT/redis-operator/api"
redisv1beta2 "github.com/OT-CONTAINER-KIT/redis-operator/api/v1beta2"
mock_utils "github.com/OT-CONTAINER-KIT/redis-operator/mocks/utils"
"github.com/go-logr/logr"
Expand All @@ -16,6 +17,8 @@ import (
"github.com/stretchr/testify/assert"
corev1 "k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/client-go/kubernetes/fake"
k8sClientFake "k8s.io/client-go/kubernetes/fake"
"k8s.io/utils/ptr"
)
Expand Down Expand Up @@ -349,6 +352,7 @@ func TestGetRedisTLSArgs(t *testing.T) {
}

func TestCreateRedisReplicationCommand(t *testing.T) {
logger := logr.Discard()
tests := []struct {
name string
redisCluster *redisv1beta2.RedisCluster
Expand All @@ -364,7 +368,15 @@ func TestCreateRedisReplicationCommand(t *testing.T) {
Namespace: "default",
},
Spec: redisv1beta2.RedisClusterSpec{
Size: ptr.To(int32(3)),
Size: ptr.To(int32(3)),
KubernetesConfig: redisv1beta2.KubernetesConfig{
KubernetesConfig: api.KubernetesConfig{
ExistingPasswordSecret: &api.ExistingPasswordSecret{
Name: ptr.To("redis-password-secret"),
Key: ptr.To("password"),
},
},
},
ClusterVersion: ptr.To("v7"),
Port: ptr.To(6379),
},
Expand All @@ -382,6 +394,7 @@ func TestCreateRedisReplicationCommand(t *testing.T) {
"redis-cluster-follower-0.redis-cluster-follower-headless.default.svc:6379",
"redis-cluster-leader-0.redis-cluster-leader-headless.default.svc:6379",
"--cluster-slave",
"-a", "password",
},
},
{
Expand Down Expand Up @@ -415,9 +428,17 @@ func TestCreateRedisReplicationCommand(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
client := mock_utils.CreateFakeClientWithPodIPs(tt.redisCluster)
logger := testr.New(t)
pods := mock_utils.CreateFakeObjectWithPodIPs(tt.redisCluster)
var secret []runtime.Object
if tt.redisCluster.Spec.KubernetesConfig.ExistingPasswordSecret != nil {
secret = mock_utils.CreateFakeObjectWithSecret(*tt.redisCluster.Spec.KubernetesConfig.ExistingPasswordSecret.Name, tt.redisCluster.GetNamespace(), *tt.redisCluster.Spec.KubernetesConfig.ExistingPasswordSecret.Key)
}

var objects []runtime.Object
objects = append(objects, pods...)
objects = append(objects, secret...)

client := fake.NewSimpleClientset(objects...)
cmd := createRedisReplicationCommand(client, logger, tt.redisCluster, tt.leaderPod, tt.followerPod)

// Assert the command is as expected using testify
Expand Down
2 changes: 2 additions & 0 deletions k8sutils/secrets.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,11 @@ func getRedisPassword(client kubernetes.Interface, logger logr.Logger, namespace
}
for key, value := range secretName.Data {
if key == secretKey {
logger.V(1).Info("Secret key found in the secret", "secretKey", secretKey)
return strings.TrimSpace(string(value)), nil
}
}
logger.Error(errors.New("secret key not found"), "Secret key not found in the secret")
return "", nil
}

Expand Down
17 changes: 15 additions & 2 deletions mocks/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ func CreateFakeClientWithPodIPs_LeaderPods(cr *redisv1beta2.RedisCluster) *fake.
return fake.NewSimpleClientset(pods...)
}

func CreateFakeClientWithPodIPs(cr *redisv1beta2.RedisCluster) *fake.Clientset {
func CreateFakeObjectWithPodIPs(cr *redisv1beta2.RedisCluster) []runtime.Object {
leaderReplicas := cr.Spec.GetReplicaCounts("leader")
followerReplicas := cr.Spec.GetReplicaCounts("follower")
pods := make([]runtime.Object, leaderReplicas+followerReplicas)
Expand Down Expand Up @@ -60,7 +60,20 @@ func CreateFakeClientWithPodIPs(cr *redisv1beta2.RedisCluster) *fake.Clientset {
}
}

return fake.NewSimpleClientset(pods...)
return pods
}

func CreateFakeObjectWithSecret(name, namespace, key string) []runtime.Object {
secret := &corev1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
Data: map[string][]byte{
key: []byte("password"),
},
}
return []runtime.Object{secret}
}

func CreateFakeClientWithSecrets(cr *redisv1beta2.RedisCluster, secretName, secretKey, secretValue string) *fake.Clientset {
Expand Down

0 comments on commit e58ab50

Please sign in to comment.