Skip to content

Commit

Permalink
(finalizers): added tests
Browse files Browse the repository at this point in the history
  • Loading branch information
itamar-marom committed Dec 27, 2023
1 parent 6ab17d7 commit a98a90a
Show file tree
Hide file tree
Showing 5 changed files with 220 additions and 4 deletions.
8 changes: 5 additions & 3 deletions controllers/druid/finalizers.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,12 @@ var (
defaultFinalizers []string
)

func addFinalizers(ctx context.Context, sdk client.Client, m *v1alpha1.Druid, emitEvents EventEmitter) error {
func updateFinalizers(ctx context.Context, sdk client.Client, m *v1alpha1.Druid, emitEvents EventEmitter) error {
desiredFinalizers := m.GetFinalizers()
additionFinalizers := defaultFinalizers

if m.Spec.DisablePVCDeletionFinalizer == false {
desiredFinalizers = RemoveString(desiredFinalizers, deletePVCFinalizerName)
if !m.Spec.DisablePVCDeletionFinalizer {
additionFinalizers = append(additionFinalizers, deletePVCFinalizerName)
}

Expand All @@ -34,7 +35,8 @@ func addFinalizers(ctx context.Context, sdk client.Client, m *v1alpha1.Druid, em
}

m.SetFinalizers(desiredFinalizers)
if _, err := writers.Update(ctx, sdk, m, m, emitEvents); err != nil {
_, err := writers.Update(ctx, sdk, m, m, emitEvents)
if err != nil {
return err
}

Expand Down
141 changes: 141 additions & 0 deletions controllers/druid/finalizers_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
package druid

import (
"time"

druidv1alpha1 "github.com/datainfrahq/druid-operator/apis/druid/v1alpha1"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"k8s.io/apimachinery/pkg/types"
)

// +kubebuilder:docs-gen:collapse=Imports

/*
finalizers_test
*/
var _ = Describe("Test finalizers logic", func() {
const (
filePath = "testdata/finalizers.yaml"
timeout = time.Second * 45
interval = time.Millisecond * 250
)

var (
druid = &druidv1alpha1.Druid{}
)

Context("When creating a druid cluster", func() {
It("Should create the druid object", func() {
By("Creating a new druid")
druidCR, err := readDruidClusterSpecFromFile(filePath)
Expect(err).Should(BeNil())
Expect(k8sClient.Create(ctx, druidCR)).To(Succeed())

By("Getting a newly created druid")
Eventually(func() bool {
err := k8sClient.Get(ctx, types.NamespacedName{Name: druidCR.Name, Namespace: druidCR.Namespace}, druid)
return err == nil
}, timeout, interval).Should(BeTrue())
})
It("Should add the delete PVC finalizer", func() {
By("Waiting for the finalizer to be created")
Eventually(func() bool {
err := k8sClient.Get(ctx, types.NamespacedName{Name: druid.Name, Namespace: druid.Namespace}, druid)
if err == nil && ContainsString(druid.GetFinalizers(), deletePVCFinalizerName) {
return true
}
return false
}, timeout, interval).Should(BeTrue())
})
It("Should delete druid successfully", func() {
By("Waiting for the druid cluster to be deleted")
Expect(k8sClient.Delete(ctx, druid)).To(Succeed())
Eventually(func() bool {
err := k8sClient.Get(ctx, types.NamespacedName{Name: druid.Name, Namespace: druid.Namespace}, druid)
return err != nil
}, timeout, interval).Should(BeTrue())
})
})

Context("When creating a druid cluster with disablePVCDeletion", func() {
It("Should create the druid object", func() {
By("Creating a new druid")
druidCR, err := readDruidClusterSpecFromFile(filePath)
druidCR.Spec.DisablePVCDeletionFinalizer = true
Expect(err).Should(BeNil())
Expect(k8sClient.Create(ctx, druidCR)).To(Succeed())

By("Getting a newly created druid")
Eventually(func() bool {
err := k8sClient.Get(ctx, types.NamespacedName{Name: druidCR.Name, Namespace: druidCR.Namespace}, druid)
return err == nil
}, timeout, interval).Should(BeTrue())
})
It("Should not add the delete PVC finalizer", func() {
By("Call for the update finalizer function")
Expect(updateFinalizers(ctx, k8sClient, druid, emitEvent)).Should(BeNil())

By("Getting a updated druid")
Eventually(func() bool {
err := k8sClient.Get(ctx, types.NamespacedName{Name: druid.Name, Namespace: druid.Namespace}, druid)
return err == nil
}, timeout, interval).Should(BeTrue())

By("Checking the absence of the finalizer")
Expect(ContainsString(druid.GetFinalizers(), deletePVCFinalizerName)).Should(BeFalse())
})
It("Should delete druid successfully", func() {
Expect(k8sClient.Delete(ctx, druid)).To(Succeed())
Eventually(func() bool {
err := k8sClient.Get(ctx, types.NamespacedName{Name: druid.Name, Namespace: druid.Namespace}, druid)
return err != nil
}, timeout, interval).Should(BeTrue())
})
})

Context("When creating a druid cluster", func() {
It("Should create the druid object", func() {
By("Creating a new druid")
druidCR, err := readDruidClusterSpecFromFile(filePath)
Expect(err).Should(BeNil())
Expect(k8sClient.Create(ctx, druidCR)).To(Succeed())

By("Getting the CR")
Eventually(func() bool {
err := k8sClient.Get(ctx, types.NamespacedName{Name: druidCR.Name, Namespace: druidCR.Namespace}, druid)
return err == nil
}, timeout, interval).Should(BeTrue())
})
It("Should add the delete PVC finalizer", func() {
By("Waiting for the finalizer to be created")
Eventually(func() bool {
err := k8sClient.Get(ctx, types.NamespacedName{Name: druid.Name, Namespace: druid.Namespace}, druid)
if err == nil && ContainsString(druid.GetFinalizers(), deletePVCFinalizerName) {
return true
}
return false
}, timeout, interval).Should(BeTrue())
})
It("Should remove the delete PVC finalizer", func() {
By("Disabling the deletePVC finalizer")
druid.Spec.DisablePVCDeletionFinalizer = true
Expect(k8sClient.Update(ctx, druid)).To(BeNil())
By("Waiting for the finalizer to be deleted")
Eventually(func() bool {
err := k8sClient.Get(ctx, types.NamespacedName{Name: druid.Name, Namespace: druid.Namespace}, druid)
if err == nil && !ContainsString(druid.GetFinalizers(), deletePVCFinalizerName) {
return true
}
return false
}, timeout, interval).Should(BeTrue())
})
It("Should delete druid successfully", func() {
Expect(k8sClient.Delete(ctx, druid)).To(Succeed())
Eventually(func() bool {
err := k8sClient.Get(ctx, types.NamespacedName{Name: druid.Name, Namespace: druid.Namespace}, druid)
return err != nil
}, timeout, interval).Should(BeTrue())
})
})
})
2 changes: 1 addition & 1 deletion controllers/druid/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ func deployDruidCluster(ctx context.Context, sdk client.Client, m *v1alpha1.Drui
return executeFinalizers(ctx, sdk, m, emitEvents)
}

if err := addFinalizers(ctx, sdk, m, emitEvents); err != nil {
if err := updateFinalizers(ctx, sdk, m, emitEvents); err != nil {
return err
}

Expand Down
3 changes: 3 additions & 0 deletions controllers/druid/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ var (
testEnv *envtest.Environment
ctx context.Context
cancel context.CancelFunc
emitEvent EventEmitter
)

func TestAPIs(t *testing.T) {
Expand Down Expand Up @@ -93,6 +94,8 @@ var _ = BeforeSuite(func() {
}).SetupWithManager(k8sManager)
Expect(err).ToNot(HaveOccurred())

emitEvent = EmitEventFuncs{k8sManager.GetEventRecorderFor("druid-operator")}

go func() {
defer GinkgoRecover()
err = k8sManager.Start(ctx)
Expand Down
70 changes: 70 additions & 0 deletions controllers/druid/testdata/finalizers.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
apiVersion: druid.apache.org/v1alpha1
kind: Druid
metadata:
name: finalizers
namespace: default
spec:
image: apache/druid:25.0.0
startScript: /druid.sh
rollingDeploy: false
securityContext:
fsGroup: 1000
runAsUser: 1000
runAsGroup: 1000
services:
- spec:
type: ClusterIP
commonConfigMountPath: "/opt/druid/conf/druid/cluster/_common"
jvm.options: |-
-server
-XX:MaxDirectMemorySize=10240g
-Duser.timezone=UTC
-Dfile.encoding=UTF-8
-Djava.io.tmpdir=/druid/data
common.runtime.properties: |-
# Metadata Store
druid.metadata.storage.type=derby
druid.metadata.storage.connector.connectURI=jdbc:derby://localhost:1527/druid/data/derbydb/metadata.db;create=true
druid.metadata.storage.connector.host=localhost
druid.metadata.storage.connector.port=1527
druid.metadata.storage.connector.createTables=true
# Deep Storage
druid.storage.type=local
druid.storage.storageDirectory=/druid/deepstorage
# Service discovery
druid.selectors.indexing.serviceName=druid/overlord
druid.selectors.coordinator.serviceName=druid/coordinator
nodes:
brokers:
nodeType: "broker"
kind: "Deployment"
druid.port: 8088
nodeConfigMountPath: "/opt/druid/conf/druid/cluster/query/broker"
replicas: 1
runtime.properties: |-
druid.service=druid/broker
additionalContainer:
- command:
- /bin/sh echo hello
containerName: node-level
image: hello-world
coordinators:
nodeType: "coordinator"
druid.port: 8080
nodeConfigMountPath: "/opt/druid/conf/druid/cluster/master/coordinator-overlord"
replicas: 1
runtime.properties: |-
druid.service=druid/coordinator
druid.coordinator.asOverlord.enabled=true
druid.coordinator.asOverlord.overlordService=druid/overlord
historicals:
nodeType: "historical"
druid.port: 8080
nodeConfigMountPath: "/opt/druid/conf/druid/cluster/data/historical"
replicas: 1
runtime.properties: |-
druid.service=druid/historical
druid.segmentCache.locations=[{\"path\":\"/druid/data/segments\",\"maxSize\":10737418240}]
druid.server.maxSize=10737418240

0 comments on commit a98a90a

Please sign in to comment.