dbState sdlgoredis.DbState
}
-func setupHcMockMasterDb(ip, port string, replicas uint32) {
+func setupHcMockMasterDb(ip, port string) {
hcMocks = new(healthCheckMocks)
hcMocks.dbState.MasterDbState.Fields.Role = "master"
hcMocks.dbState.MasterDbState.Fields.Ip = ip
hcMocks.dbState.MasterDbState.Fields.Flags = "master"
}
-func setupHcMockReplicaDb() {
+func setupHcMockReplicaDb(ip, port string) {
hcMocks = new(healthCheckMocks)
hcMocks.dbState.ReplicasDbState = new(sdlgoredis.ReplicasDbState)
hcMocks.dbState.ReplicasDbState.States = []*sdlgoredis.ReplicaDbState{
}
}
+func setupHcMockSentinelDb(ip, port string) {
+ hcMocks = new(healthCheckMocks)
+ hcMocks.dbState.SentinelsDbState = new(sdlgoredis.SentinelsDbState)
+ hcMocks.dbState.SentinelsDbState.States = []*sdlgoredis.SentinelDbState{
+ &sdlgoredis.SentinelDbState{
+ Fields: sdlgoredis.SentinelDbStateFields{
+ Ip: ip,
+ Port: port,
+ },
+ },
+ }
+}
+
func addHcMockReplicaDbState(ip, port, masterLinkOk string) {
if hcMocks.dbState.ReplicasDbState == nil {
hcMocks.dbState.ReplicasDbState = new(sdlgoredis.ReplicasDbState)
)
}
+func addHcMockSentinelDbState(ip, port, flags string) {
+ if hcMocks.dbState.SentinelsDbState == nil {
+ hcMocks.dbState.SentinelsDbState = new(sdlgoredis.SentinelsDbState)
+ }
+ hcMocks.dbState.SentinelsDbState.States = append(hcMocks.dbState.SentinelsDbState.States,
+ &sdlgoredis.SentinelDbState{
+ Fields: sdlgoredis.SentinelDbStateFields{
+ Ip: ip,
+ Port: port,
+ Flags: flags,
+ },
+ },
+ )
+}
+
func newMockDatabase() *cli.Database {
db := &cli.Database{}
hcMocks.dbIface = new(mocks.MockDB)
func runHcCli() (string, error) {
buf := new(bytes.Buffer)
- cmd := cli.NewHealthCheckCmdForTest(newMockDatabase)
+ cmd := cli.NewHealthCheckCmd(newMockDatabase)
cmd.SetOut(buf)
err := cmd.Execute()
var expOkErr error
expHelp := "Usage:\n " + "healthcheck [flags]"
expNokErr := errors.New("unknown flag: --some-unknown-flag")
+ expArgCntErr := errors.New("accepts 0 arg(s), received 1")
tests := []struct {
args string
expErr error
{args: "-h", expErr: expOkErr, expOutput: expHelp},
{args: "--help", expErr: expOkErr, expOutput: expHelp},
{args: "--some-unknown-flag", expErr: expNokErr, expOutput: expHelp},
+ {args: "some-extra-argument", expErr: expArgCntErr, expOutput: expHelp},
}
for _, test := range tests {
buf := new(bytes.Buffer)
- cmd := cli.NewHealthCheckCmd()
+ cmd := cli.NewHealthCheckCmd(newMockDatabase)
cmd.SetOut(buf)
cmd.SetArgs([]string{test.args})
}
func TestCliHealthCheckCanShowHaDeploymentOkStatusCorrectly(t *testing.T) {
- setupHcMockMasterDb("10.20.30.40", "6379", 2)
+ setupHcMockMasterDb("10.20.30.40", "6379")
addHcMockReplicaDbState("1.2.3.4", "6379", "ok")
addHcMockReplicaDbState("5.6.7.8", "6379", "ok")
+ addHcMockSentinelDbState("1.2.3.4", "26379", "sentinel")
+ addHcMockSentinelDbState("5.6.7.8", "26379", "sentinel")
stdout, err := runHcCli()
}
func TestCliHealthCheckCanShowHaDeploymentStatusCorrectlyWhenOneReplicaStateNotUp(t *testing.T) {
- setupHcMockMasterDb("10.20.30.40", "6379", 2)
+ setupHcMockMasterDb("10.20.30.40", "6379")
addHcMockReplicaDbState("1.2.3.4", "6379", "ok")
addHcMockReplicaDbState("5.6.7.8", "6379", "nok")
+ addHcMockSentinelDbState("1.2.3.4", "26379", "sentinel")
+ addHcMockSentinelDbState("5.6.7.8", "26379", "sentinel")
stdout, err := runHcCli()
assert.Contains(t, stdout, "Replica link to the master is down")
}
+func TestCliHealthCheckCanShowHaDeploymentStatusCorrectlyWhenOneSentinelStateNotUp(t *testing.T) {
+ setupHcMockMasterDb("10.20.30.40", "6379")
+ addHcMockReplicaDbState("1.2.3.4", "6379", "ok")
+ addHcMockReplicaDbState("5.6.7.8", "6379", "ok")
+ addHcMockSentinelDbState("1.2.3.4", "26379", "some-failure")
+ addHcMockSentinelDbState("5.6.7.8", "26379", "sentinel")
+
+ stdout, err := runHcCli()
+
+ assert.Nil(t, err)
+ assert.Contains(t, stdout, "Overall status: NOK")
+ assert.Contains(t, stdout, "Replica #1 (1.2.3.4:6379): OK")
+ assert.Contains(t, stdout, "Replica #2 (5.6.7.8:6379): OK")
+ assert.Contains(t, stdout, "Sentinel #1 (1.2.3.4:26379): NOK")
+ assert.Contains(t, stdout, "Sentinel flags are 'some-failure', expected 'sentinel'")
+}
+
func TestCliHealthCheckCanShowHaDeploymentStatusCorrectlyWhenDbStateQueryFails(t *testing.T) {
- setupHcMockMasterDb("10.20.30.40", "6379", 0)
+ setupHcMockMasterDb("10.20.30.40", "6379")
hcMocks.dbErr = errors.New("Some error")
- expCliErr := errors.New("SDL CLI error: Some error")
buf := new(bytes.Buffer)
- cmd := cli.NewHealthCheckCmdForTest(newMockDatabase)
+ cmd := cli.NewHealthCheckCmd(newMockDatabase)
cmd.SetErr(buf)
err := cmd.Execute()
stderr := buf.String()
- assert.Equal(t, expCliErr, err)
- assert.Contains(t, stderr, "Error: "+expCliErr.Error())
+ assert.Equal(t, hcMocks.dbErr, err)
+ assert.Contains(t, stderr, "Error: "+hcMocks.dbErr.Error())
+}
+
+func TestCliHealthCheckCanShowHaDeploymentOkStatusCorrectlyWhenDbStateIsFromReplicaOnly(t *testing.T) {
+ setupHcMockReplicaDb("1.2.3.4", "6379")
+
+ stdout, err := runHcCli()
+
+ assert.Nil(t, err)
+ assert.Contains(t, stdout, "Overall status: NOK")
+ assert.Contains(t, stdout, "Master (): NOK")
}
-func TestCliHealthCheckCanShowHaDeploymentOkStatusCorrectlyWhenDbStateIsFromReplicaServer(t *testing.T) {
- setupHcMockReplicaDb()
+func TestCliHealthCheckCanShowHaDeploymentOkStatusCorrectlyWhenDbStateIsFromSentinelOnly(t *testing.T) {
+ setupHcMockSentinelDb("1.2.3.4", "26379")
stdout, err := runHcCli()
}
func TestCliHealthCheckCanShowStandaloneDeploymentOkStatusCorrectly(t *testing.T) {
- setupHcMockMasterDb("10.20.30.40", "6379", 0)
+ setupHcMockMasterDb("10.20.30.40", "6379")
stdout, err := runHcCli()