From 13bb4cfa8c60f3c0ac7be1ba33c05c4e45b989fe Mon Sep 17 00:00:00 2001 From: Juliano Martinez Date: Sun, 20 Oct 2024 00:28:26 +0200 Subject: [PATCH] adds missing tests --- pkg/aclmanager/aclmanager.go | 17 +++++++--- pkg/aclmanager/aclmanager_test.go | 53 +++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 5 deletions(-) diff --git a/pkg/aclmanager/aclmanager.go b/pkg/aclmanager/aclmanager.go index 344a9ad..ea973b2 100644 --- a/pkg/aclmanager/aclmanager.go +++ b/pkg/aclmanager/aclmanager.go @@ -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 @@ -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") @@ -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 { @@ -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 @@ -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) @@ -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) diff --git a/pkg/aclmanager/aclmanager_test.go b/pkg/aclmanager/aclmanager_test.go index 8365e51..5806d4f 100644 --- a/pkg/aclmanager/aclmanager_test.go +++ b/pkg/aclmanager/aclmanager_test.go @@ -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) + }) + } +}