From 2e1ba9764507475e7826be60aa4f457763b16ad6 Mon Sep 17 00:00:00 2001 From: Mathieu Cesbron <45229023+MathieuCesbron@users.noreply.github.com> Date: Sun, 21 Jan 2024 03:52:44 +0100 Subject: [PATCH] test: add redis sentinel integration tests (#755) * Add redis sentinel in suite test Signed-off-by: Mathieu Cesbron * Add redis sentinel integration tests Signed-off-by: Mathieu Cesbron --------- Signed-off-by: Mathieu Cesbron --- controllers/redissentinel_controller_test.go | 149 +++++++++++++++++++ controllers/suite_test.go | 8 + 2 files changed, 157 insertions(+) create mode 100644 controllers/redissentinel_controller_test.go diff --git a/controllers/redissentinel_controller_test.go b/controllers/redissentinel_controller_test.go new file mode 100644 index 000000000..f53fafb8c --- /dev/null +++ b/controllers/redissentinel_controller_test.go @@ -0,0 +1,149 @@ +package controllers + +import ( + "context" + "fmt" + + redisv1beta2 "github.com/OT-CONTAINER-KIT/redis-operator/api/v1beta2" + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + appsv1 "k8s.io/api/apps/v1" + corev1 "k8s.io/api/core/v1" + "k8s.io/apimachinery/pkg/api/errors" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/types" +) + +var _ = Describe("Redis sentinel test", func() { + var ( + redisSentinelCR redisv1beta2.RedisSentinel + redisSentinelCRName string + size int32 + // Used to create unique name for each test + testCount int + ) + + JustBeforeEach(func() { + size = 3 + redisSentinelCR = redisv1beta2.RedisSentinel{ + TypeMeta: metav1.TypeMeta{ + APIVersion: "redis.redis.opstreelabs.in/v1beta2", + Kind: "RedisReplication", + }, + ObjectMeta: metav1.ObjectMeta{ + Name: redisSentinelCRName, + Namespace: ns, + }, + Spec: redisv1beta2.RedisSentinelSpec{ + Size: &size, + }, + } + Expect(k8sClient.Create(context.TODO(), &redisSentinelCR)).Should(Succeed()) + testCount++ + }) + + BeforeEach(func() { + redisSentinelCRName = fmt.Sprintf("redis-sentinel-%d", testCount) + }) + + Context("When creating a redis sentinel CR", func() { + It("should create a statefulset", func() { + + // Eventually(func() int { + // list := &appsv1.StatefulSetList{} + // // list := &redisv1beta2.RedisClusterList{} + // // list := &corev1.PodList{} + // // list := &corev1.ServiceList{} + + // err := k8sClient.List(context.TODO(), list) + // if err != nil { + // return -1 + // } + // for _, v := range list.Items { + // fmt.Println(v.Name) + // } + // return len(list.Items) + // }, timeout, interval).Should(Equal(3)) + + sts := &appsv1.StatefulSet{} + Eventually(func() error { + return k8sClient.Get(context.TODO(), types.NamespacedName{ + Name: redisSentinelCRName + "-sentinel", + Namespace: ns, + }, sts) + }, timeout, interval).Should(BeNil()) + + Expect(*sts.Spec.Replicas).To(BeEquivalentTo(3)) + Expect(sts.Spec.ServiceName).To(Equal(redisSentinelCRName + "-sentinel-headless")) + }) + + It("should create a service", func() { + svc := &corev1.Service{} + Eventually(func() error { + return k8sClient.Get(context.TODO(), types.NamespacedName{ + Name: redisSentinelCRName + "-sentinel", + Namespace: ns, + }, svc) + }, timeout, interval).Should(BeNil()) + + Expect(svc.Labels).To(Equal(map[string]string{ + "app": redisSentinelCRName + "-sentinel", + "redis_setup_type": "sentinel", + "role": "sentinel", + })) + }) + + It("should create a headless service", func() { + svc := &corev1.Service{} + Eventually(func() error { + return k8sClient.Get(context.TODO(), types.NamespacedName{ + Name: redisSentinelCRName + "-sentinel-headless", + Namespace: ns, + }, svc) + }, timeout, interval).Should(BeNil()) + + Expect(svc.Labels).To(Equal(map[string]string{ + "app": redisSentinelCRName + "-sentinel", + "redis_setup_type": "sentinel", + "role": "sentinel", + })) + }) + + It("should create additional service", func() { + svc := &corev1.Service{} + Eventually(func() error { + return k8sClient.Get(context.TODO(), types.NamespacedName{ + Name: redisSentinelCRName + "-sentinel-additional", + Namespace: ns, + }, svc) + }, timeout, interval).Should(BeNil()) + + Expect(svc.Labels).To(Equal(map[string]string{ + "app": redisSentinelCRName + "-sentinel", + "redis_setup_type": "sentinel", + "role": "sentinel", + })) + }) + + Context("then deleting the redis sentinel CR", func() { + It("should delete the statefulset", func() { + redisSentinelCR := &redisv1beta2.RedisSentinel{ + ObjectMeta: metav1.ObjectMeta{ + Name: redisSentinelCRName, + Namespace: ns, + }, + } + Expect(k8sClient.Delete(context.TODO(), redisSentinelCR)).To(BeNil()) + + Eventually(func() bool { + sts := &appsv1.StatefulSet{} + err := k8sClient.Get(context.TODO(), types.NamespacedName{ + Name: redisSentinelCRName + "-sentinel", + Namespace: ns, + }, sts) + return errors.IsNotFound(err) + }, timeout, interval).Should(BeTrue()) + }) + }) + }) +}) diff --git a/controllers/suite_test.go b/controllers/suite_test.go index cb19c5b01..0ab8268ff 100644 --- a/controllers/suite_test.go +++ b/controllers/suite_test.go @@ -118,6 +118,14 @@ var _ = BeforeSuite(func() { }).SetupWithManager(k8sManager) Expect(err).ToNot(HaveOccurred()) + err = (&RedisSentinelReconciler{ + Client: k8sManager.GetClient(), + K8sClient: k8sClient, + Dk8sClient: dk8sClient, + Scheme: k8sManager.GetScheme(), + }).SetupWithManager(k8sManager) + Expect(err).ToNot(HaveOccurred()) + go func() { defer GinkgoRecover() err = k8sManager.Start(ctrl.SetupSignalHandler())