Skip to content

Commit

Permalink
test(client): Add redis mock client (#841)
Browse files Browse the repository at this point in the history
* test(Client): Add redis mock client

Signed-off-by: Shubham Gupta <[email protected]>

* fake commit

Signed-off-by: Shubham Gupta <[email protected]>

* fake commit

Signed-off-by: Shubham Gupta <[email protected]>

---------

Signed-off-by: Shubham Gupta <[email protected]>
  • Loading branch information
shubham-cmyk authored Mar 24, 2024
1 parent 17abdf4 commit 2ee4324
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
4 changes: 4 additions & 0 deletions k8sutils/cluster-scaling.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
63 changes: 63 additions & 0 deletions k8sutils/cluster-scaling_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
})
}
}

0 comments on commit 2ee4324

Please sign in to comment.