Skip to content

Commit

Permalink
adds missing tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ncode committed Oct 19, 2024
1 parent 0ef59f0 commit 13bb4cf
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 5 deletions.
17 changes: 12 additions & 5 deletions pkg/aclmanager/aclmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ type AclManager struct {
nodes map[string]int
mu sync.Mutex // Mutex to protect nodes map
aclFile bool
batchSize int
}

// New creates a new AclManager
Expand All @@ -47,9 +48,17 @@ func New(addr string, username string, password string, aclfile bool) *AclManage
RedisClient: redisClient,
nodes: make(map[string]int),
aclFile: aclfile,
batchSize: 100,
}
}

// SetBatchSize sets the batch size for syncing ACLs
func (a *AclManager) SetBatchSize(size int) {
a.mu.Lock()
defer a.mu.Unlock()
a.batchSize = size
}

// findNodes returns a list of nodes in the cluster based on the redis ROLE command
func (a *AclManager) findNodes(ctx context.Context) error {
slog.Debug("Entering findNodes")
Expand Down Expand Up @@ -285,8 +294,6 @@ func (a *AclManager) SyncAcls(ctx context.Context, primary *AclManager) ([]strin
return nil, nil, err
}

const batchSize = 100

// Get source ACLs
sourceAclHashMap, sourceAclStrMap, err := listAndMapAcls(ctx, primary.RedisClient)
if err != nil {
Expand All @@ -302,7 +309,7 @@ func (a *AclManager) SyncAcls(ctx context.Context, primary *AclManager) ([]strin
var updated, deleted []string

// Batch commands
cmds := make([]redis.Cmder, 0, batchSize)
cmds := make([]redis.Cmder, 0, a.batchSize)
pipe := a.RedisClient.Pipeline()

// Delete ACLs that are not in the source
Expand All @@ -312,7 +319,7 @@ func (a *AclManager) SyncAcls(ctx context.Context, primary *AclManager) ([]strin
cmd := pipe.Do(ctx, "ACL", "DELUSER", username)
cmds = append(cmds, cmd)
deleted = append(deleted, username)
if len(cmds) >= batchSize {
if len(cmds) >= a.batchSize {
// Execute pipeline
if _, err = pipe.Exec(ctx); err != nil {
slog.Error("Failed to execute pipeline", "error", err)
Expand Down Expand Up @@ -346,7 +353,7 @@ func (a *AclManager) SyncAcls(ctx context.Context, primary *AclManager) ([]strin
cmds = append(cmds, cmd)
updated = append(updated, username)

if len(cmds) >= batchSize {
if len(cmds) >= a.batchSize {
// Execute pipeline
if _, err = pipe.Exec(ctx); err != nil {
slog.Error("Failed to execute pipeline", "error", err)
Expand Down
53 changes: 53 additions & 0 deletions pkg/aclmanager/aclmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -994,3 +994,56 @@ func TestLoadAclFile(t *testing.T) {
})
}
}

func TestHashString(t *testing.T) {
tests := []struct {
name string
input string
expected string
}{
{
name: "empty string",
input: "",
expected: "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855",
},
{
name: "non-empty string",
input: "hello world",
expected: "b94d27b9934d3e08a52e52d7da7dabfac484efe37a5380ee9088f7ace2efcde9",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
hash := hashString(tt.input)
assert.Equal(t, tt.expected, hash)
})
}
}

func TestSetBatchSize(t *testing.T) {
tests := []struct {
name string
batchSize int
expectedSize int
}{
{
name: "default batch size",
batchSize: 0,
expectedSize: 0,
},
{
name: "custom batch size",
batchSize: 10,
expectedSize: 10,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
aclManager := &AclManager{}
aclManager.SetBatchSize(tt.batchSize)
assert.Equal(t, tt.expectedSize, aclManager.batchSize)
})
}
}

0 comments on commit 13bb4cf

Please sign in to comment.