Skip to content

Commit

Permalink
test: add redis sentinel integration tests (#755)
Browse files Browse the repository at this point in the history
* Add redis sentinel in suite test

Signed-off-by: Mathieu Cesbron <[email protected]>

* Add redis sentinel integration tests

Signed-off-by: Mathieu Cesbron <[email protected]>

---------

Signed-off-by: Mathieu Cesbron <[email protected]>
  • Loading branch information
MathieuCesbron authored Jan 21, 2024
1 parent f9938bc commit 2e1ba97
Show file tree
Hide file tree
Showing 2 changed files with 157 additions and 0 deletions.
149 changes: 149 additions & 0 deletions controllers/redissentinel_controller_test.go
Original file line number Diff line number Diff line change
@@ -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())
})
})
})
})
8 changes: 8 additions & 0 deletions controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down

0 comments on commit 2e1ba97

Please sign in to comment.