Skip to content

Commit

Permalink
Add redis sentinel integration tests
Browse files Browse the repository at this point in the history
Signed-off-by: Mathieu Cesbron <[email protected]>
  • Loading branch information
MathieuCesbron committed Jan 20, 2024
1 parent 9bec010 commit 7dda6ee
Showing 1 changed file with 149 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())
})
})
})
})

0 comments on commit 7dda6ee

Please sign in to comment.