From 8abd1061f666ad8d909d615f33aeb6ff3d05bd1d Mon Sep 17 00:00:00 2001 From: Juliano Martinez Date: Mon, 4 Dec 2023 21:25:36 +0100 Subject: [PATCH] adds new tests --- pkg/aclmanager/aclmanager_test.go | 39 +++++++++++++++++++++++++++++-- 1 file changed, 37 insertions(+), 2 deletions(-) diff --git a/pkg/aclmanager/aclmanager_test.go b/pkg/aclmanager/aclmanager_test.go index 9da9b76..38f5806 100644 --- a/pkg/aclmanager/aclmanager_test.go +++ b/pkg/aclmanager/aclmanager_test.go @@ -78,6 +78,12 @@ func TestFindNodes(t *testing.T) { }, wantErr: false, }, + { + name: "error on replicationInfo", + mockResp: followerOutput, + want: nil, + wantErr: true, + }, } for _, tt := range tests { @@ -85,7 +91,11 @@ func TestFindNodes(t *testing.T) { redisClient, mock := redismock.NewClientMock() // Mocking the response for the Info function - mock.ExpectInfo("replication").SetVal(tt.mockResp) + if tt.wantErr { + mock.ExpectInfo("replication").SetErr(fmt.Errorf("error")) + } else { + mock.ExpectInfo("replication").SetVal(tt.mockResp) + } aclManager := AclManager{RedisClient: redisClient} nodes, err := aclManager.FindNodes() @@ -93,7 +103,6 @@ func TestFindNodes(t *testing.T) { t.Errorf("FindNodes() error = %v, wantErr %v", err, tt.wantErr) return } - assert.Equal(t, tt.want, nodes) }) } @@ -281,6 +290,32 @@ func TestIsItPrimary(t *testing.T) { } } +func TestNewAclManager(t *testing.T) { + tests := []struct { + name string + want *AclManager + }{ + { + name: "create AclManager", + want: &AclManager{ + Addr: "localhost:6379", + Password: "password", + Username: "username", + }, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + got := New(tt.want.Addr, tt.want.Username, tt.want.Password) + assert.Equal(t, tt.want.Addr, got.Addr) + assert.Equal(t, tt.want.Username, got.Username) + assert.Equal(t, tt.want.Password, got.Password) + assert.NotNil(t, got.RedisClient) + }) + } +} + func BenchmarkParseRedisOutputFollower(b *testing.B) { for i := 0; i < b.N; i++ { _, err := parseRedisOutput(followerOutput)