Skip to content

Commit

Permalink
Adding tests
Browse files Browse the repository at this point in the history
  • Loading branch information
samof76 committed Sep 3, 2024
1 parent f5bda6e commit 8a6353c
Show file tree
Hide file tree
Showing 3 changed files with 135 additions and 18 deletions.
2 changes: 1 addition & 1 deletion api/redisfailover/v1/validate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ func TestValidate(t *testing.T) {
expectedError: "BootstrapNode must include a host when provided",
},
{
name: "SentinelCustomConfig provided",
name: "SentinelCustomConfig not provided",
rfName: "test",
},
{
Expand Down
45 changes: 28 additions & 17 deletions operator/redisfailover/service/check_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,28 +21,39 @@ import (
)

func generateRF(args ...bool) *redisfailoverv1.RedisFailover {
var disableMyMaster bool
if len(args) > 0 {
disableMyMaster = args[0]
return &redisfailoverv1.RedisFailover{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
Spec: redisfailoverv1.RedisFailoverSpec{
Redis: redisfailoverv1.RedisSettings{
Replicas: int32(3),
},
Sentinel: redisfailoverv1.SentinelSettings{
Replicas: int32(3),
DisableMyMaster: args[0],
},
},
}
} else {
disableMyMaster = false
}

return &redisfailoverv1.RedisFailover{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
Spec: redisfailoverv1.RedisFailoverSpec{
Redis: redisfailoverv1.RedisSettings{
Replicas: int32(3),
return &redisfailoverv1.RedisFailover{
ObjectMeta: metav1.ObjectMeta{
Name: name,
Namespace: namespace,
},
Sentinel: redisfailoverv1.SentinelSettings{
Replicas: int32(3),
DisableMyMaster: disableMyMaster,
Spec: redisfailoverv1.RedisFailoverSpec{
Redis: redisfailoverv1.RedisSettings{
Replicas: int32(3),
},
Sentinel: redisfailoverv1.SentinelSettings{
Replicas: int32(3),
},
},
},
}
}

}

func TestCheckRedisNumberError(t *testing.T) {
Expand Down
106 changes: 106 additions & 0 deletions operator/redisfailover/service/generator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2792,3 +2792,109 @@ func TestSentinelCustomStartupProbe(t *testing.T) {
assert.Equal(test.expectedStartupProbe, startupProbe)
}
}

func TestDisableMyMaster(t *testing.T) {
tests := []struct {
name string
disableMyMaster bool
expectedSentinelReadinessProbe *corev1.Probe
redisShutdownSHScriptConfigMap *corev1.ConfigMap
sentinelConfConfigMap *corev1.ConfigMap
expectedSentinelConfConfigMap *corev1.ConfigMap
expectedRedisShutdownSHScriptConfigMap *corev1.ConfigMap
}{
{
name: "disable_mymaster_false",
disableMyMaster: false,
expectedSentinelReadinessProbe: &corev1.Probe{
InitialDelaySeconds: 30,
TimeoutSeconds: 5,
ProbeHandler: corev1.ProbeHandler{
Exec: &corev1.ExecAction{
Command: []string{
"sh",
"-c",
"redis-cli -h $(hostname) -p 26379 sentinel get-master-addr-by-name mymaster | head -n 1 | grep -vq '127.0.0.1'",
},
},
},
},
expectedSentinelConfConfigMap: &corev1.ConfigMap{
Data: map[string]string{
"sentinel.conf": "sentinel monitor mymaster 127.0.0.1 0 2\nsentinel down-after-milliseconds mymaster 1000\nsentinel failover-timeout mymaster 3000\nsentinel parallel-syncs mymaster 2",
},
},
expectedRedisShutdownSHScriptConfigMap: &corev1.ConfigMap{
Data: map[string]string{
"shutdown.sh": "master=$(redis-cli -h ${RFS_TEST_SERVICE_HOST} -p ${RFS_TEST_SERVICE_PORT_SENTINEL} --csv SENTINEL get-master-addr-by-name mymaster | tr ',' ' ' | tr -d '\\\"' |cut -d' ' -f1)\nif [ \"$master\" = \"$(hostname -i)\" ]; then\nredis-cli -h ${RFS_TEST_SERVICE_HOST} -p ${RFS_TEST_SERVICE_PORT_SENTINEL} SENTINEL failover mymaster\nsleep 31\nfi\ncmd=\"redis-cli -p 0\"\nif [ ! -z \"${REDIS_PASSWORD}\" ]; then\n\texport REDISCLI_AUTH=${REDIS_PASSWORD}\nfi\nsave_command=\"${cmd} save\"\neval $save_command",
},
},
},
{
name: "disable_mymaster_true",
disableMyMaster: true,
expectedSentinelReadinessProbe: &corev1.Probe{
InitialDelaySeconds: 30,
TimeoutSeconds: 5,
ProbeHandler: corev1.ProbeHandler{
Exec: &corev1.ExecAction{
Command: []string{
"sh",
"-c",
"redis-cli -h $(hostname) -p 26379 sentinel get-master-addr-by-name test | head -n 1 | grep -vq '127.0.0.1'",
},
},
},
},
expectedSentinelConfConfigMap: &corev1.ConfigMap{
Data: map[string]string{
"sentinel.conf": "sentinel monitor test 127.0.0.1 0 2\nsentinel down-after-milliseconds test 1000\nsentinel failover-timeout test 3000\nsentinel parallel-syncs test 2",
},
},
expectedRedisShutdownSHScriptConfigMap: &corev1.ConfigMap{
Data: map[string]string{
"shutdown.sh": "master=$(redis-cli -h ${RFS_TEST_SERVICE_HOST} -p ${RFS_TEST_SERVICE_PORT_SENTINEL} --csv SENTINEL get-master-addr-by-name test | tr ',' ' ' | tr -d '\\\"' |cut -d' ' -f1)\nif [ \"$master\" = \"$(hostname -i)\" ]; then\nredis-cli -h ${RFS_TEST_SERVICE_HOST} -p ${RFS_TEST_SERVICE_PORT_SENTINEL} SENTINEL failover test\nsleep 31\nfi\ncmd=\"redis-cli -p 0\"\nif [ ! -z \"${REDIS_PASSWORD}\" ]; then\n\texport REDISCLI_AUTH=${REDIS_PASSWORD}\nfi\nsave_command=\"${cmd} save\"\neval $save_command",
},
},
},
}
for _, test := range tests {
assert := assert.New(t)

var sentinelReadinessProbe *corev1.Probe
rf := generateRF(test.disableMyMaster)
// redisShutdownSHSriptConfigMap := rfservice.generateRedisShutdownConfigMap(rf)

ms := &mK8SService.Services{}
ms.On("CreateOrUpdatePodDisruptionBudget", namespace, mock.Anything).Once().Return(nil, nil)
ms.On("CreateOrUpdateDeployment", namespace, mock.Anything).Once().Run(func(args mock.Arguments) {
d := args.Get(1).(*appsv1.Deployment)
sentinelReadinessProbe = d.Spec.Template.Spec.Containers[0].ReadinessProbe
}).Return(nil)

client := rfservice.NewRedisFailoverKubeClient(ms, log.Dummy, metrics.Dummy)
err := client.EnsureSentinelDeployment(rf, nil, []metav1.OwnerReference{})
assert.NoError(err)
assert.Equal(test.expectedSentinelReadinessProbe, sentinelReadinessProbe)

generatedSentinelConfigMap := corev1.ConfigMap{}
ms.On("CreateOrUpdateConfigMap", namespace, mock.Anything).Once().Run(func(args mock.Arguments) {
cms := args.Get(1).(*corev1.ConfigMap)
generatedSentinelConfigMap = *cms
}).Return(nil)

err = client.EnsureSentinelConfigMap(rf, nil, []metav1.OwnerReference{})
assert.NoError(err)
assert.Equal(test.expectedSentinelConfConfigMap.Data, generatedSentinelConfigMap.Data)

generatedRedisConfigMap := corev1.ConfigMap{}
ms.On("CreateOrUpdateConfigMap", namespace, mock.Anything).Once().Run(func(args mock.Arguments) {
cmr := args.Get(1).(*corev1.ConfigMap)
generatedRedisConfigMap = *cmr
}).Return(nil)

err = client.EnsureRedisShutdownConfigMap(rf, nil, []metav1.OwnerReference{})
assert.NoError(err)
assert.Equal(test.expectedRedisShutdownSHScriptConfigMap.Data, generatedRedisConfigMap.Data)
}
}

0 comments on commit 8a6353c

Please sign in to comment.