Skip to content

Commit

Permalink
Add redis cluster integration tests (#753)
Browse files Browse the repository at this point in the history
* Move var integration tests

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

* Add dynamic k8s client in integration tests

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

* Add Integration tests for redis cluster

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

---------

Signed-off-by: Mathieu Cesbron <[email protected]>
  • Loading branch information
MathieuCesbron authored Jan 18, 2024
1 parent 629c2df commit 6bf05bc
Show file tree
Hide file tree
Showing 4 changed files with 170 additions and 11 deletions.
8 changes: 0 additions & 8 deletions controllers/redis_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package controllers
import (
"context"
"fmt"
"time"

appsv1 "k8s.io/api/apps/v1"

Expand All @@ -17,13 +16,6 @@ import (
"k8s.io/apimachinery/pkg/types"
)

const (
ns = "default"

timeout = time.Second * 5
interval = time.Millisecond * 250
)

var _ = Describe("Redis standalone test", func() {
var (
redisCR redisv1beta2.Redis
Expand Down
145 changes: 145 additions & 0 deletions controllers/rediscluster_controller_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,145 @@
package controllers

import (
"context"
"fmt"

appsv1 "k8s.io/api/apps/v1"
corev1 "k8s.io/api/core/v1"

redisv1beta2 "github.com/OT-CONTAINER-KIT/redis-operator/api/v1beta2"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"

"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/types"
)

var _ = Describe("Redis cluster test", func() {
var (
redisClusterCR redisv1beta2.RedisCluster
redisClusterCRName string
size int32
// version string
// Used to create unique name for each test
testCount int
)

JustBeforeEach(func() {
size = 3
// version = "v7"
redisClusterCR = redisv1beta2.RedisCluster{
TypeMeta: metav1.TypeMeta{
APIVersion: "redis.redis.opstreelabs.in/v1beta2",
Kind: "RedisCluster",
},
ObjectMeta: metav1.ObjectMeta{
Name: redisClusterCRName,
Namespace: ns,
},
Spec: redisv1beta2.RedisClusterSpec{
Size: &size,
// ClusterVersion: &version,
Storage: &redisv1beta2.ClusterStorage{},
},
}
Expect(k8sClient.Create(context.TODO(), &redisClusterCR)).Should(Succeed())
testCount++
})

BeforeEach(func() {
redisClusterCRName = fmt.Sprintf("redis-cluster-%d", testCount)
})

Context("When creating a redis cluster CR", func() {
It("should create a statefulset", func() {

sts := &appsv1.StatefulSet{}
Eventually(func() error {
return k8sClient.Get(context.TODO(), types.NamespacedName{
Name: redisClusterCRName + "-leader",
Namespace: ns,
}, sts)
}, timeout, interval).Should(BeNil())

Expect(sts.Labels).To(Equal(map[string]string{
"app": redisClusterCRName + "-leader",
"redis_setup_type": "cluster",
"role": "leader",
}))

Expect(*sts.Spec.Replicas).To(BeEquivalentTo(3))
Expect(sts.Spec.ServiceName).To(Equal(redisClusterCRName + "-leader-headless"))
})

It("should create a service", func() {
svc := &corev1.Service{}
Eventually(func() error {
return k8sClient.Get(context.TODO(), types.NamespacedName{
Name: redisClusterCRName + "-leader",
Namespace: ns,
}, svc)
}, timeout, interval).Should(BeNil())

Expect(svc.Labels).To(Equal(map[string]string{
"app": redisClusterCRName + "-leader",
"redis_setup_type": "cluster",
"role": "leader",
}))
})

It("should create a headless service", func() {
svc := &corev1.Service{}
Eventually(func() error {
return k8sClient.Get(context.TODO(), types.NamespacedName{
Name: redisClusterCRName + "-leader-headless",
Namespace: ns,
}, svc)
}, timeout, interval).Should(BeNil())

Expect(svc.Labels).To(Equal(map[string]string{
"app": redisClusterCRName + "-leader",
"redis_setup_type": "cluster",
"role": "leader",
}))
})

It("should create additional service", func() {
svc := &corev1.Service{}
Eventually(func() error {
return k8sClient.Get(context.TODO(), types.NamespacedName{
Name: redisClusterCRName + "-leader-additional",
Namespace: ns,
}, svc)
}, timeout, interval).Should(BeNil())

Expect(svc.Labels).To(Equal(map[string]string{
"app": redisClusterCRName + "-leader",
"redis_setup_type": "cluster",
"role": "leader",
}))
})

Context("then deleting the redis cluster CR", func() {
It("should delete the statefulset", func() {
redisClusterCR := &redisv1beta2.RedisCluster{
ObjectMeta: metav1.ObjectMeta{
Name: redisClusterCRName,
Namespace: ns,
},
}
Expect(k8sClient.Delete(context.TODO(), redisClusterCR)).To(BeNil())

Eventually(func() bool {
sts := &appsv1.StatefulSet{}
err := k8sClient.Get(context.TODO(), types.NamespacedName{
Name: redisClusterCRName,
Namespace: ns,
}, sts)
return errors.IsNotFound(err)
}, timeout, interval).Should(BeTrue())
})
})
})
})
26 changes: 23 additions & 3 deletions controllers/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"github.com/onsi/gomega/gexec"
"k8s.io/client-go/dynamic"
"k8s.io/client-go/kubernetes"
"k8s.io/client-go/kubernetes/scheme"
ctrl "sigs.k8s.io/controller-runtime"
Expand All @@ -41,6 +42,13 @@ import (
var k8sClient client.Client
var testEnv *envtest.Environment

const (
ns = "default"

timeout = time.Second * 10
interval = time.Millisecond * 250
)

func TestAPIs(t *testing.T) {
RegisterFailHandler(Fail)

Expand Down Expand Up @@ -83,10 +91,22 @@ var _ = BeforeSuite(func() {
k8sClient, err := kubernetes.NewForConfig(cfg)
Expect(err).ToNot(HaveOccurred())

dk8sClient, err := dynamic.NewForConfig(cfg)
Expect(err).ToNot(HaveOccurred())

err = (&RedisReconciler{
Client: k8sManager.GetClient(),
K8sClient: k8sClient,
Scheme: k8sManager.GetScheme(),
Client: k8sManager.GetClient(),
K8sClient: k8sClient,
Dk8sClient: dk8sClient,
Scheme: k8sManager.GetScheme(),
}).SetupWithManager(k8sManager)
Expect(err).ToNot(HaveOccurred())

err = (&RedisClusterReconciler{
Client: k8sManager.GetClient(),
K8sClient: k8sClient,
Dk8sClient: dk8sClient,
Scheme: k8sManager.GetScheme(),
}).SetupWithManager(k8sManager)
Expect(err).ToNot(HaveOccurred())

Expand Down
2 changes: 2 additions & 0 deletions k8sutils/redis-cluster.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package k8sutils

import (
"fmt"
"strconv"
"strings"

Expand Down Expand Up @@ -34,6 +35,7 @@ type RedisClusterService struct {

// generateRedisClusterParams generates Redis cluster information
func generateRedisClusterParams(cr *redisv1beta2.RedisCluster, replicas int32, externalConfig *string, params RedisClusterSTS) statefulSetParameters {
fmt.Println(cr.Spec.Storage.NodeConfVolume)
res := statefulSetParameters{
Replicas: &replicas,
ClusterMode: true,
Expand Down

0 comments on commit 6bf05bc

Please sign in to comment.