From 8a6353c92080ac1b688b07a8e1c07af608d1eb40 Mon Sep 17 00:00:00 2001 From: M Samuel Vijaykumar Date: Tue, 3 Sep 2024 14:53:50 +0530 Subject: [PATCH] Adding tests --- api/redisfailover/v1/validate_test.go | 2 +- operator/redisfailover/service/check_test.go | 45 +++++--- .../redisfailover/service/generator_test.go | 106 ++++++++++++++++++ 3 files changed, 135 insertions(+), 18 deletions(-) diff --git a/api/redisfailover/v1/validate_test.go b/api/redisfailover/v1/validate_test.go index 30eede285..ac97f2ce6 100644 --- a/api/redisfailover/v1/validate_test.go +++ b/api/redisfailover/v1/validate_test.go @@ -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", }, { diff --git a/operator/redisfailover/service/check_test.go b/operator/redisfailover/service/check_test.go index 4ff62a636..5c0bba581 100644 --- a/operator/redisfailover/service/check_test.go +++ b/operator/redisfailover/service/check_test.go @@ -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) { diff --git a/operator/redisfailover/service/generator_test.go b/operator/redisfailover/service/generator_test.go index b14317072..a49f1f9cd 100644 --- a/operator/redisfailover/service/generator_test.go +++ b/operator/redisfailover/service/generator_test.go @@ -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) + } +}