Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
Signed-off-by: Shubham Gupta <[email protected]>
  • Loading branch information
shubham-cmyk committed Nov 11, 2023
1 parent e7392bd commit 2b6ad4b
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 65 deletions.
137 changes: 73 additions & 64 deletions k8sutils/redis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ func TestCreateMultipleLeaderRedisCommand(t *testing.T) {

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
client := mock_utils.CreateFakeClientWithPodIPs(tt.redisCluster)
client := mock_utils.CreateFakeClientWithPodIPs_LeaderPods(tt.redisCluster)
logger := testr.New(t)

cmd := CreateMultipleLeaderRedisCommand(client, logger, tt.redisCluster)
Expand Down Expand Up @@ -277,72 +277,81 @@ func TestGetRedisTLSArgs(t *testing.T) {
}
}

// func TestCreateRedisReplicationCommand(t *testing.T) {
// tests := []struct {
// name string
// redisCluster *redisv1beta2.RedisCluster
// leaderPod RedisDetails
// followerPod RedisDetails
// expectedCommand []string
// }{
// {
// name: "Test case with cluster version v7",
// redisCluster: &redisv1beta2.RedisCluster{
// ObjectMeta: metav1.ObjectMeta{
// Name: "redis-cluster",
// Namespace: "default",
// },
// Spec: redisv1beta2.RedisClusterSpec{
// Size: pointer.Int32(3),
// ClusterVersion: pointer.String("v7"),
// },
// },
// leaderPod: RedisDetails{
// PodName: "redis-cluster-leader-0",
// Namespace: "default",
// },
// followerPod: RedisDetails{
// PodName: "redis-cluster-follower-0",
// Namespace: "default",
// },
// expectedCommand: []string{},
// },
// {
// name: "Test case without cluster version v7",
// redisCluster: &redisv1beta2.RedisCluster{
// ObjectMeta: metav1.ObjectMeta{
// Name: "mycluster",
// Namespace: "default",
// },
// Spec: redisv1beta2.RedisClusterSpec{
// Size: pointer.Int32(3),
// },
// },
// leaderPod: RedisDetails{
// PodName: "redis-cluster-leader-0",
// Namespace: "default",
// },
// followerPod: RedisDetails{
// PodName: "redis-cluster-follower-0",
// Namespace: "default",
// },
// expectedCommand: []string{},
// },
// }
func TestCreateRedisReplicationCommand(t *testing.T) {
tests := []struct {
name string
redisCluster *redisv1beta2.RedisCluster
leaderPod RedisDetails
followerPod RedisDetails
expectedCommand []string
}{
{
name: "Test case with cluster version v7",
redisCluster: &redisv1beta2.RedisCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "redis-cluster",
Namespace: "default",
},
Spec: redisv1beta2.RedisClusterSpec{
Size: pointer.Int32(3),
ClusterVersion: pointer.String("v7"),
},
},
leaderPod: RedisDetails{
PodName: "redis-cluster-leader-0",
Namespace: "default",
},
followerPod: RedisDetails{
PodName: "redis-cluster-follower-0",
Namespace: "default",
},
expectedCommand: []string{
"redis-cli", "--cluster", "add-node",
"redis-cluster-follower-0.redis-cluster-follower-headless.default.svc:6379",
"redis-cluster-leader-0.redis-cluster-leader-headless.default.svc:6379",
"--cluster-slave",
},
},
{
name: "Test case without cluster version v7",
redisCluster: &redisv1beta2.RedisCluster{
ObjectMeta: metav1.ObjectMeta{
Name: "redis-cluster",
Namespace: "default",
},
Spec: redisv1beta2.RedisClusterSpec{
Size: pointer.Int32(3),
},
},
leaderPod: RedisDetails{
PodName: "redis-cluster-leader-0",
Namespace: "default",
},
followerPod: RedisDetails{
PodName: "redis-cluster-follower-0",
Namespace: "default",
},
expectedCommand: []string{
"redis-cli", "--cluster", "add-node",
"192.168.2.1:6379",
"192.168.1.1:6379",
"--cluster-slave",
},
},
}

// for _, tt := range tests {
// t.Run(tt.name, func(t *testing.T) {
// client := mock_utils.CreateFakeClientWithPodIPs(tt.redisCluster)
// logger := testr.New(t)
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
client := mock_utils.CreateFakeClientWithPodIPs(tt.redisCluster)
logger := testr.New(t)

// // Call the function under test
// cmd := createRedisReplicationCommand(client, logger, tt.redisCluster, tt.leaderPod, tt.followerPod)
cmd := createRedisReplicationCommand(client, logger, tt.redisCluster, tt.leaderPod, tt.followerPod)

// // Assert the command is as expected using testify
// assert.Equal(t, tt.expectedCommand, cmd)
// })
// }
// }
// Assert the command is as expected using testify
assert.Equal(t, tt.expectedCommand, cmd)
})
}
}

func TestGetContainerID(t *testing.T) {
tests := []struct {
Expand Down
36 changes: 35 additions & 1 deletion mocks/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import (
"k8s.io/client-go/kubernetes/fake"
)

func CreateFakeClientWithPodIPs(cr *redisv1beta2.RedisCluster) *fake.Clientset {
func CreateFakeClientWithPodIPs_LeaderPods(cr *redisv1beta2.RedisCluster) *fake.Clientset {
replicas := cr.Spec.GetReplicaCounts("leader")
pods := make([]runtime.Object, replicas)

Expand All @@ -29,3 +29,37 @@ func CreateFakeClientWithPodIPs(cr *redisv1beta2.RedisCluster) *fake.Clientset {
}
return fake.NewSimpleClientset(pods...)
}

func CreateFakeClientWithPodIPs(cr *redisv1beta2.RedisCluster) *fake.Clientset {
leaderReplicas := cr.Spec.GetReplicaCounts("leader")
followerReplicas := cr.Spec.GetReplicaCounts("follower")
pods := make([]runtime.Object, leaderReplicas+followerReplicas)

for i := 0; i < int(leaderReplicas); i++ {
podName := cr.ObjectMeta.Name + "-leader-" + strconv.Itoa(i)
pods[i] = &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: podName,
Namespace: cr.Namespace,
},
Status: corev1.PodStatus{
PodIP: fmt.Sprintf("192.168.1.%d", i+1),
},
}
}
for i := 0; i < int(followerReplicas); i++ {
podName := cr.ObjectMeta.Name + "-follower-" + strconv.Itoa(i)
// Adjust the index to place follower pods after leader pods
pods[i+int(leaderReplicas)] = &corev1.Pod{
ObjectMeta: metav1.ObjectMeta{
Name: podName,
Namespace: cr.Namespace,
},
Status: corev1.PodStatus{
PodIP: fmt.Sprintf("192.168.2.%d", i+1),
},
}
}

return fake.NewSimpleClientset(pods...)
}

0 comments on commit 2b6ad4b

Please sign in to comment.