diff --git a/go.mod b/go.mod index ece74a259..245bbcb6d 100644 --- a/go.mod +++ b/go.mod @@ -31,6 +31,7 @@ require ( github.com/go-openapi/jsonpointer v0.19.6 // indirect github.com/go-openapi/jsonreference v0.20.2 // indirect github.com/go-openapi/swag v0.22.3 // indirect + github.com/go-redis/redismock/v9 v9.2.0 // indirect github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 // indirect github.com/gogo/protobuf v1.3.2 // indirect github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect diff --git a/go.sum b/go.sum index 71caed038..0ba4a0e12 100644 --- a/go.sum +++ b/go.sum @@ -67,6 +67,8 @@ github.com/go-openapi/spec v0.0.0-20160808142527-6aced65f8501/go.mod h1:J8+jY1nA github.com/go-openapi/swag v0.0.0-20160704191624-1d0bd113de87/go.mod h1:DXUve3Dpr1UfpPtxFw+EFuQ41HhCWZfha5jSVRG7C7I= github.com/go-openapi/swag v0.22.3 h1:yMBqmnQ0gyZvEb/+KzuWZOXgllrXT4SADYbvDaXHv/g= github.com/go-openapi/swag v0.22.3/go.mod h1:UzaqsxGiab7freDnrUUra0MwWfN/q7tE4j+VcZ0yl14= +github.com/go-redis/redismock/v9 v9.2.0 h1:ZrMYQeKPECZPjOj5u9eyOjg8Nnb0BS9lkVIZ6IpsKLw= +github.com/go-redis/redismock/v9 v9.2.0/go.mod h1:18KHfGDK4Y6c2R0H38EUGWAdc7ZQS9gfYxc94k7rWT0= github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572 h1:tfuBGBXKqDEevZMzYi5KSi8KkcZtzBcTgAUUtapy0OI= github.com/go-task/slim-sprig v0.0.0-20230315185526-52ccab3ef572/go.mod h1:9Pwr4B2jHnOSGXyyzV8ROjYa2ojvAY6HCGYYfMoC3Ls= github.com/gogo/protobuf v1.3.1/go.mod h1:SlYgWuQ5SjCEi6WLHjHCa1yvBfUnHcTbrrZtXPKa29o= diff --git a/k8sutils/cluster-scaling.go b/k8sutils/cluster-scaling.go index 6699a5a75..e5e86726c 100644 --- a/k8sutils/cluster-scaling.go +++ b/k8sutils/cluster-scaling.go @@ -386,6 +386,10 @@ func VerifyLeaderPod(ctx context.Context, client kubernetes.Interface, logger lo redisClient := configureRedisClient(client, logger, cr, podName) defer redisClient.Close() + return verifyLeaderPodInfo(ctx, redisClient, logger, podName) +} + +func verifyLeaderPodInfo(ctx context.Context, redisClient *redis.Client, logger logr.Logger, podName string) bool { info, err := redisClient.Info(ctx, "replication").Result() if err != nil { logger.Error(err, "Failed to Get the role Info of the", "redis pod", podName) diff --git a/k8sutils/cluster-scaling_test.go b/k8sutils/cluster-scaling_test.go new file mode 100644 index 000000000..ca8107712 --- /dev/null +++ b/k8sutils/cluster-scaling_test.go @@ -0,0 +1,63 @@ +package k8sutils + +import ( + "context" + "testing" + + "github.com/go-logr/logr" + "github.com/go-redis/redismock/v9" + redis "github.com/redis/go-redis/v9" + "github.com/stretchr/testify/assert" +) + +func Test_verifyLeaderPodInfo(t *testing.T) { + logger := logr.Discard() + + tests := []struct { + name string + section string + response string + err error + expectedBool bool + }{ + { + name: "is master", + section: "replication", + response: "role:master\r\n", + expectedBool: true, + }, + { + name: "is replica", + section: "replication", + response: "role:slave\r\n", + expectedBool: false, + }, + { + name: "redis info error", + section: "replication", + err: redis.ErrClosed, + expectedBool: false, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + ctx := context.Background() + client, mock := redismock.NewClientMock() + + if tt.err != nil { + mock.ExpectInfo(tt.section).SetErr(tt.err) + } else { + mock.ExpectInfo(tt.section).SetVal(tt.response) + } + + result := verifyLeaderPodInfo(ctx, client, logger, "test-pod") + + assert.Equal(t, tt.expectedBool, result, "Test case: "+tt.name) + + if err := mock.ExpectationsWereMet(); err != nil { + t.Errorf("there were unmet expectations: %s", err) + } + }) + } +}