From 872590cc11c57d25d774847e885a7a6aefd297ea Mon Sep 17 00:00:00 2001 From: dinesh-murugiah Date: Wed, 7 Dec 2022 16:29:56 +0530 Subject: [PATCH] added tes cases --- operator/redisfailover/checker_test.go | 81 ++++++++++++++++----- operator/redisfailover/service/heal_test.go | 9 +-- 2 files changed, 67 insertions(+), 23 deletions(-) diff --git a/operator/redisfailover/checker_test.go b/operator/redisfailover/checker_test.go index c8ae26970..2e83a47b4 100644 --- a/operator/redisfailover/checker_test.go +++ b/operator/redisfailover/checker_test.go @@ -7,7 +7,6 @@ import ( "time" "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/mock" appsv1 "k8s.io/api/apps/v1" corev1 "k8s.io/api/core/v1" @@ -25,7 +24,9 @@ func TestCheckAndHeal(t *testing.T) { name string nMasters int nRedis int - forceNewMaster bool + forceNewMasterNoQrm bool + forceNewMasterFirstBoot bool + singleMasterTest bool slavesOK bool sentinelMonitorOK bool sentinelNumberInMemoryOK bool @@ -39,7 +40,9 @@ func TestCheckAndHeal(t *testing.T) { name: "Everything ok, no need to heal", nMasters: 1, nRedis: 3, - forceNewMaster: false, + singleMasterTest: false, + forceNewMasterNoQrm: false, + forceNewMasterFirstBoot: false, slavesOK: true, sentinelMonitorOK: true, sentinelNumberInMemoryOK: true, @@ -53,7 +56,9 @@ func TestCheckAndHeal(t *testing.T) { name: "Multiple masters", nMasters: 2, nRedis: 3, - forceNewMaster: false, + singleMasterTest: false, + forceNewMasterNoQrm: false, + forceNewMasterFirstBoot: false, slavesOK: true, sentinelMonitorOK: true, sentinelNumberInMemoryOK: true, @@ -67,7 +72,9 @@ func TestCheckAndHeal(t *testing.T) { name: "No masters but wait", nMasters: 0, nRedis: 3, - forceNewMaster: false, + singleMasterTest: false, + forceNewMasterNoQrm: false, + forceNewMasterFirstBoot: false, slavesOK: true, sentinelMonitorOK: true, sentinelNumberInMemoryOK: true, @@ -81,7 +88,9 @@ func TestCheckAndHeal(t *testing.T) { name: "No masters, only one redis available, make master", nMasters: 0, nRedis: 1, - forceNewMaster: false, + singleMasterTest: true, + forceNewMasterNoQrm: false, + forceNewMasterFirstBoot: false, slavesOK: true, sentinelMonitorOK: true, sentinelNumberInMemoryOK: true, @@ -92,10 +101,27 @@ func TestCheckAndHeal(t *testing.T) { allowSentinels: false, }, { - name: "No masters, set random", + name: "No masters,No sentinel quorum set random", nMasters: 0, nRedis: 3, - forceNewMaster: true, + singleMasterTest: false, + forceNewMasterNoQrm: true, + forceNewMasterFirstBoot: false, + slavesOK: true, + sentinelMonitorOK: true, + sentinelNumberInMemoryOK: true, + redisCheckNumberOK: true, + redisSetMasterOnAllOK: true, + sentinelSlavesNumberInMemoryOK: true, + allowSentinels: false, + }, + { + name: "No masters,Sentinel Quorum but slave of local host set random", + nMasters: 0, + nRedis: 3, + singleMasterTest: false, + forceNewMasterNoQrm: false, + forceNewMasterFirstBoot: true, slavesOK: true, sentinelMonitorOK: true, sentinelNumberInMemoryOK: true, @@ -108,7 +134,9 @@ func TestCheckAndHeal(t *testing.T) { name: "Slaves from master wrong", nMasters: 1, nRedis: 3, - forceNewMaster: false, + singleMasterTest: false, + forceNewMasterNoQrm: false, + forceNewMasterFirstBoot: false, slavesOK: false, sentinelMonitorOK: true, sentinelNumberInMemoryOK: true, @@ -122,7 +150,9 @@ func TestCheckAndHeal(t *testing.T) { name: "Sentinels not pointing correct monitor", nMasters: 1, nRedis: 3, - forceNewMaster: false, + singleMasterTest: false, + forceNewMasterNoQrm: false, + forceNewMasterFirstBoot: false, slavesOK: true, sentinelMonitorOK: false, sentinelNumberInMemoryOK: true, @@ -136,7 +166,9 @@ func TestCheckAndHeal(t *testing.T) { name: "Sentinels with wrong number of sentinels", nMasters: 1, nRedis: 3, - forceNewMaster: false, + singleMasterTest: false, + forceNewMasterNoQrm: false, + forceNewMasterFirstBoot: false, slavesOK: true, sentinelMonitorOK: true, sentinelNumberInMemoryOK: false, @@ -150,7 +182,9 @@ func TestCheckAndHeal(t *testing.T) { name: "Sentinels with wrong number of slaves", nMasters: 1, nRedis: 3, - forceNewMaster: false, + singleMasterTest: false, + forceNewMasterNoQrm: false, + forceNewMasterFirstBoot: false, slavesOK: true, sentinelMonitorOK: true, sentinelNumberInMemoryOK: true, @@ -251,6 +285,9 @@ func TestCheckAndHeal(t *testing.T) { allowSentinels = test.allowSentinels rf.Spec.BootstrapNode.AllowSentinels = allowSentinels } + if test.singleMasterTest { + rf.Spec.Redis.Replicas = 1 + } expErr := false continueTests := true @@ -297,18 +334,26 @@ func TestCheckAndHeal(t *testing.T) { mrfc.On("GetNumberMasters", rf).Once().Return(test.nMasters, nil) switch test.nMasters { case 0: - mrfc.On("GetRedisesIPs", rf).Once().Return(make([]string, test.nRedis), nil) - if test.nRedis == 1 { - mrfh.On("MakeMaster", mock.Anything, rf).Once().Return(nil) + //mrfc.On("GetRedisesIPs", rf).Once().Return(make([]string, test.nRedis), nil) + if rf.Spec.Redis.Replicas == 1 { + mrfh.On("SetOldestAsMaster", rf).Once().Return(nil) + continueTests = false break } - if test.forceNewMaster { - mrfc.On("GetMinimumRedisPodTime", rf).Once().Return(1*time.Hour, nil) + mrfc.On("GetMaxRedisPodTime", rf).Once().Return(1*time.Hour, nil) + if test.forceNewMasterNoQrm { + mrfc.On("CheckSentinelQuorum", rf).Once().Return(1, errors.New("")) + mrfh.On("SetOldestAsMaster", rf).Once().Return(nil) + } else if test.forceNewMasterFirstBoot { + mrfc.On("CheckSentinelQuorum", rf).Once().Return(3, nil) + mrfc.On("CheckIfMasterLocalhost", rf).Once().Return(true, nil) mrfh.On("SetOldestAsMaster", rf).Once().Return(nil) } else { - mrfc.On("GetMinimumRedisPodTime", rf).Once().Return(1*time.Second, nil) + mrfc.On("CheckSentinelQuorum", rf).Once().Return(3, nil) + mrfc.On("CheckIfMasterLocalhost", rf).Once().Return(false, nil) continueTests = false } + case 1: break default: diff --git a/operator/redisfailover/service/heal_test.go b/operator/redisfailover/service/heal_test.go index f9feb8ea4..27bd8cc11 100644 --- a/operator/redisfailover/service/heal_test.go +++ b/operator/redisfailover/service/heal_test.go @@ -41,7 +41,7 @@ func TestSetOldestAsMasterNewMasterError(t *testing.T) { healer := rfservice.NewRedisFailoverHealer(ms, mr, log.DummyLogger{}) err := healer.SetOldestAsMaster(rf) - assert.NoError(err) + assert.Error(err) } func TestSetOldestAsMaster(t *testing.T) { @@ -204,8 +204,7 @@ func TestSetMasterOnAllMakeMasterError(t *testing.T) { ms.On("GetStatefulSetPods", namespace, rfservice.GetRedisName(rf)).Once().Return(pods, nil) ms.On("UpdatePodLabels", namespace, mock.AnythingOfType("string"), mock.Anything).Once().Return(nil) mr := &mRedisService.Client{} - mr.On("MakeMaster", "0.0.0.0", "0", "").Once().Return(errors.New("")) - + mr.On("IsMaster", "0.0.0.0", "0", "").Return(false, errors.New("")) healer := rfservice.NewRedisFailoverHealer(ms, mr, log.DummyLogger{}) err := healer.SetMasterOnAll("0.0.0.0", rf) @@ -236,7 +235,7 @@ func TestSetMasterOnAllMakeSlaveOfError(t *testing.T) { ms.On("GetStatefulSetPods", namespace, rfservice.GetRedisName(rf)).Once().Return(pods, nil) ms.On("UpdatePodLabels", namespace, mock.AnythingOfType("string"), mock.Anything).Return(nil) mr := &mRedisService.Client{} - mr.On("MakeMaster", "0.0.0.0", "0", "").Once().Return(nil) + mr.On("IsMaster", "0.0.0.0", "0", "").Return(true, nil) mr.On("MakeSlaveOfWithPort", "1.1.1.1", "0.0.0.0", "0", "").Once().Return(errors.New("")) healer := rfservice.NewRedisFailoverHealer(ms, mr, log.DummyLogger{}) @@ -269,7 +268,7 @@ func TestSetMasterOnAll(t *testing.T) { ms.On("GetStatefulSetPods", namespace, rfservice.GetRedisName(rf)).Once().Return(pods, nil) ms.On("UpdatePodLabels", namespace, mock.AnythingOfType("string"), mock.Anything).Return(nil) mr := &mRedisService.Client{} - mr.On("MakeMaster", "0.0.0.0", "0", "").Once().Return(nil) + mr.On("IsMaster", "0.0.0.0", "0", "").Return(true, nil) mr.On("MakeSlaveOfWithPort", "1.1.1.1", "0.0.0.0", "0", "").Once().Return(nil) healer := rfservice.NewRedisFailoverHealer(ms, mr, log.DummyLogger{})