diff --git a/k8sutils/redis_test.go b/k8sutils/redis_test.go index 9b5adfecf..884acdce9 100644 --- a/k8sutils/redis_test.go +++ b/k8sutils/redis_test.go @@ -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" @@ -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" ) @@ -349,6 +352,7 @@ func TestGetRedisTLSArgs(t *testing.T) { } func TestCreateRedisReplicationCommand(t *testing.T) { + logger := logr.Discard() tests := []struct { name string redisCluster *redisv1beta2.RedisCluster @@ -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), }, @@ -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", }, }, { @@ -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 diff --git a/k8sutils/secrets.go b/k8sutils/secrets.go index 40f3853fe..ab86a5960 100644 --- a/k8sutils/secrets.go +++ b/k8sutils/secrets.go @@ -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 } diff --git a/mocks/utils/utils.go b/mocks/utils/utils.go index 9c58952fc..b48dad6a2 100644 --- a/mocks/utils/utils.go +++ b/mocks/utils/utils.go @@ -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) @@ -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 {