From 93a664c737a1441f6ef024aef3fd3c152abdbe8e Mon Sep 17 00:00:00 2001 From: Juliano Martinez Date: Thu, 18 Apr 2024 19:55:51 +0200 Subject: [PATCH] adds the missing tests --- pkg/aclmanager/aclmanager_test.go | 66 +++++++++++++++++++++++++++++-- 1 file changed, 62 insertions(+), 4 deletions(-) diff --git a/pkg/aclmanager/aclmanager_test.go b/pkg/aclmanager/aclmanager_test.go index 334e560..9a49497 100644 --- a/pkg/aclmanager/aclmanager_test.go +++ b/pkg/aclmanager/aclmanager_test.go @@ -221,8 +221,11 @@ func TestSyncAcls(t *testing.T) { expectedUpdated []string listAclsError error redisDoError error + saveAclError error + loadAclError error wantSourceErr bool wantDestinationErr bool + aclFile bool }{ { name: "ACLs synced with deletions", @@ -274,6 +277,37 @@ func TestSyncAcls(t *testing.T) { name: "Invalid aclManagerPrimary", listAclsError: fmt.Errorf("error listing destination ACLs"), }, + { + name: "ACLs synced with deletions, aclFile", + sourceAcls: []interface{}{"user acl1", "user acl2"}, + destinationAcls: []interface{}{"user acl1", "user acl3"}, + expectedDeleted: []string{"acl3"}, + expectedUpdated: []string{"acl2"}, + aclFile: true, + }, + { + name: "Error on save ACL file on primary, aclFile", + sourceAcls: []interface{}{"user acl1", "user acl2"}, + saveAclError: fmt.Errorf("failed to save ACL on primary"), + aclFile: true, + wantSourceErr: true, + }, + { + name: "Error on save ACL file on destination, aclFile", + sourceAcls: []interface{}{"user acl1", "user acl2"}, + destinationAcls: []interface{}{"user acl1", "user acl3"}, + saveAclError: fmt.Errorf("failed to save ACL on destination"), + aclFile: true, + wantDestinationErr: true, + }, + { + name: "Error on load ACL file, aclFile", + sourceAcls: []interface{}{"user acl1", "user acl2"}, + destinationAcls: []interface{}{"user acl1", "user acl3"}, + loadAclError: fmt.Errorf("failed to load ACL"), + aclFile: true, + wantDestinationErr: true, + }, } for _, tt := range tests { @@ -322,6 +356,30 @@ func TestSyncAcls(t *testing.T) { } } + if tt.listAclsError != nil && tt.wantDestinationErr { + destMock.ExpectDo("ACL", "LIST").SetErr(tt.listAclsError) + } else { + destMock.ExpectDo("ACL", "LIST").SetVal(tt.destinationAcls) + } + + if tt.aclFile { + if tt.saveAclError != nil { + sourceMock.ExpectDo("ACL", "SAVE").SetErr(tt.saveAclError) + if !tt.wantSourceErr { + destMock.ExpectDo("ACL", "SAVE").SetErr(tt.saveAclError) + } + } else { + sourceMock.ExpectDo("ACL", "SAVE").SetVal("OK") + destMock.ExpectDo("ACL", "SAVE").SetVal("OK") + } + + if tt.loadAclError != nil && !tt.wantSourceErr { + destMock.ExpectDo("ACL", "LOAD").SetErr(tt.loadAclError) + } else { + destMock.ExpectDo("ACL", "LOAD").SetVal("OK") + } + } + added, deleted, err := aclManagerFollower.SyncAcls(context.Background(), aclManagerPrimary) if err != nil { if tt.wantSourceErr { @@ -531,7 +589,7 @@ func TestAclManager_Loop(t *testing.T) { Addr: "localhost:6379", Password: "password", Username: "username", - aclfile: false, + aclFile: false, nodes: make(map[string]int), }, wantErr: false, @@ -542,7 +600,7 @@ func TestAclManager_Loop(t *testing.T) { Addr: "localhost:6379", Password: "password", Username: "username", - aclfile: false, + aclFile: false, nodes: make(map[string]int), }, wantErr: true, @@ -554,7 +612,7 @@ func TestAclManager_Loop(t *testing.T) { Addr: "localhost:6379", Password: "password", Username: "username", - aclfile: false, + aclFile: false, nodes: make(map[string]int), }, wantErr: true, @@ -566,7 +624,7 @@ func TestAclManager_Loop(t *testing.T) { Addr: "localhost:6379", Password: "password", Username: "username", - aclfile: false, + aclFile: false, nodes: make(map[string]int), }, wantErr: false,