Skip to content

Commit

Permalink
Merge pull request #7 from ncode/juliano/tests
Browse files Browse the repository at this point in the history
more tests
  • Loading branch information
ncode authored Dec 4, 2023
2 parents bf01086 + a724d9d commit 95f1b2f
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 5 deletions.
11 changes: 6 additions & 5 deletions pkg/aclmanager/aclmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -247,16 +247,17 @@ func (a *AclManager) Loop(ctx context.Context) (err error) {
case <-ctx.Done():
return err
case <-ticker.C:
function, err := a.CurrentFunction()
function, e := a.CurrentFunction()
if err != nil {
slog.Warn("unable to check if it's a primary", "message", err)
slog.Warn("unable to check if it's a primary", "message", e)
err = fmt.Errorf("unable to check if it's a primary: %w", e)
}
if function == Follower {
err = a.SyncAcls()
e = a.SyncAcls()
if err != nil {
slog.Warn("unable to sync acls from primary", "message", err)
return fmt.Errorf("error syncing acls: %v", err)
slog.Warn("unable to sync acls from primary", "message", e)
}
err = fmt.Errorf("unable to check if it's a primary: %w", e)
}
}
}
Expand Down
118 changes: 118 additions & 0 deletions pkg/aclmanager/aclmanager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ package aclmanager
import (
"context"
"fmt"
"github.com/spf13/viper"
"reflect"
"testing"
"time"

"github.com/go-redis/redismock/v9"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -358,6 +360,122 @@ func TestCurrentFunction_Error(t *testing.T) {
assert.Error(t, err)
}

func TestAclManager_Loop(t *testing.T) {
viper.Set("syncInterval", 60)
tests := []struct {
name string
aclManager *AclManager
wantErr bool
expectError error
}{
{
name: "primary node",
aclManager: &AclManager{
Addr: "localhost:6379",
Password: "password",
Username: "username",
},
wantErr: false,
},
{
name: "primary node with error",
aclManager: &AclManager{
Addr: "localhost:6379",
Password: "password",
Username: "username",
},
wantErr: true,
expectError: fmt.Errorf("error"),
},
{
name: "follower node",
aclManager: &AclManager{
Addr: "localhost:6379",
Password: "password",
Username: "username",
},
wantErr: true,
expectError: fmt.Errorf("unable to check if it's a primary: error"),
},
// TODO: refactor to be able to test this case
//{
// name: "follower node",
// aclManager: &AclManager{
// Addr: "localhost:6379",
// Password: "password",
// Username: "username",
// },
// wantErr: false,
// expectError: fmt.Errorf("error syncing acls"),
//},
}

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

if tt.wantErr {
if tt.name == "primary node" {
mock.ExpectInfo("replication").SetErr(fmt.Errorf("error"))
} else {
mock.ExpectInfo("replication").SetVal(followerOutput)
mock.ExpectInfo("replication").SetVal(followerOutput)
mock.ExpectInfo("replication").SetErr(fmt.Errorf("error"))
}
} else {
if tt.name == "primary node" {
mock.ExpectInfo("replication").SetVal(primaryOutput)
} else {
mock.ExpectInfo("replication").SetVal(followerOutput)
mock.ExpectInfo("replication").SetVal(followerOutput)
}
}

// Set up a cancellable context to control the loop
ctx, cancel := context.WithCancel(context.Background())
defer cancel()

// Run Loop in a separate goroutine
done := make(chan error, 1)
go func() {
done <- tt.aclManager.Loop(ctx)
}()

if tt.name == "follower node" {
time.Sleep(time.Second * 10)
}

// Cancel the context to stop the loop
cancel()

// Check for errors
err := <-done
if err != nil {
if !tt.wantErr {
t.Errorf("Expected no error, got: %v", err)
}

if err.Error() != tt.expectError.Error() {
t.Errorf("Expected error: %v, got: %v", tt.expectError, err)
}
}
})
}
}

func TestClose(t *testing.T) {
redisClient, _ := redismock.NewClientMock()
aclManager := AclManager{RedisClient: redisClient}
err := aclManager.Close()
assert.NoError(t, err)
}

func TestClosePanic(t *testing.T) {
aclManager := AclManager{RedisClient: nil}
assert.Panics(t, func() { aclManager.Close() })
}

func BenchmarkParseRedisOutputFollower(b *testing.B) {
for i := 0; i < b.N; i++ {
_, err := parseRedisOutput(followerOutput)
Expand Down

0 comments on commit 95f1b2f

Please sign in to comment.