Skip to content

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ncode committed Oct 12, 2024
1 parent 53b1368 commit 919fa57
Showing 1 changed file with 53 additions and 18 deletions.
71 changes: 53 additions & 18 deletions pkg/aclmanager/aclmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,11 @@ var (

func TestFindNodes(t *testing.T) {
tests := []struct {
name string
mockRoleResp interface{}
expectedNodes map[string]int
wantErr bool
name string
mockRoleResp interface{}
expectedNodes map[string]int
wantErr bool
expectedErrMsg string
}{
{
name: "parse primary role output",
Expand All @@ -55,52 +56,86 @@ func TestFindNodes(t *testing.T) {
wantErr: false,
},
{
name: "error on ROLE command",
mockRoleResp: nil,
wantErr: true,
name: "ROLE command returns empty result",
mockRoleResp: []interface{}{},
expectedNodes: nil,
wantErr: true,
expectedErrMsg: "findNodes: ROLE command returned empty result",
},
{
name: "ROLE command first element not a string",
mockRoleResp: []interface{}{
int64(12345), // Non-string type explicitly set to int64
"some other data",
},
expectedNodes: nil,
wantErr: true,
expectedErrMsg: "findNodes: unexpected type for role: int64",
},
{
name: "error on ROLE command",
mockRoleResp: nil, // Simulate Redis error
expectedNodes: nil,
wantErr: true,
expectedErrMsg: "findNodes: ROLE command failed",
},
{
name: "unknown role type",
mockRoleResp: []interface{}{
"sentinel",
},
wantErr: true,
expectedNodes: nil,
wantErr: true,
expectedErrMsg: "findNodes: unknown role type: sentinel",
},
{
name: "unexpected type for roleInfo",
mockRoleResp: "invalid_type",
wantErr: true,
name: "unexpected type for roleInfo",
mockRoleResp: "invalid_type", // Not a slice
expectedNodes: nil,
wantErr: true,
expectedErrMsg: "findNodes: unexpected type for roleInfo: string",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
redisClient, mock := redismock.NewClientMock()

if tt.wantErr {
mock.ExpectDo("ROLE").SetErr(fmt.Errorf("error"))
// Setup the expected ROLE command response
if tt.wantErr && tt.mockRoleResp == nil {
// Simulate an error from Redis
mock.ExpectDo("ROLE").SetErr(fmt.Errorf("findNodes: ROLE command failed"))
} else {
// Simulate a successful ROLE command with the provided response
mock.ExpectDo("ROLE").SetVal(tt.mockRoleResp)
}

// Initialize AclManager with the mocked Redis client
aclManager := AclManager{
RedisClient: redisClient,
nodes: make(map[string]int),
mu: sync.Mutex{},
}
ctx := context.Background()

// Execute the findNodes function
err := aclManager.findNodes(ctx)
if (err != nil) != tt.wantErr {
t.Errorf("findNodes() error = %v, wantErr %v", err, tt.wantErr)
return
}

if !tt.wantErr {
// Assert whether an error was expected
if tt.wantErr {
assert.Error(t, err)
if tt.expectedErrMsg != "" {
assert.Contains(t, err.Error(), tt.expectedErrMsg)
}
} else {
assert.NoError(t, err)
aclManager.mu.Lock()
defer aclManager.mu.Unlock()
assert.Equal(t, tt.expectedNodes, aclManager.nodes)
}

// Ensure all expectations were met
assert.NoError(t, mock.ExpectationsWereMet())
})
}
}
Expand Down

0 comments on commit 919fa57

Please sign in to comment.