-
-
Notifications
You must be signed in to change notification settings - Fork 228
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(client): Add redis mock client (#841)
* 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
1 parent
17abdf4
commit 2ee4324
Showing
4 changed files
with
70 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
}) | ||
} | ||
} |