Skip to content

Commit

Permalink
test(additional containers): adding unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
itamar-marom committed Aug 14, 2023
1 parent e1a8867 commit 2808b45
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 2 deletions.
47 changes: 45 additions & 2 deletions controllers/druid/additional_containers_test.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,65 @@
package druid

import (
"fmt"
"time"

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

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

var _ = Describe("Test Additional Containers", func() {
const (
timeout = time.Second * 45
interval = time.Millisecond * 250
)

Context("When adding cluster-level additional containers", func() {
It("Should add the containers to the pod", func() {
By("By creating a Druid object")
filePath := "testdata/additional-containers.yaml"
druid, err := readDruidClusterSpecFromFile(filePath)
Expect(err).Should(BeNil())

err = k8sClient.Create(ctx, druid)
Expect(err).Should(BeNil())
Expect(k8sClient.Create(ctx, druid)).To(Succeed())

existDruid := &v1alpha1.Druid{}
Eventually(func() bool {
err := k8sClient.Get(ctx, types.NamespacedName{Name: druid.Name, Namespace: druid.Namespace}, existDruid)
return err == nil
}, timeout, interval).Should(BeTrue())

brokerDeployment := &v1.Deployment{}
Eventually(func() bool {
err := k8sClient.Get(ctx, types.NamespacedName{
Namespace: druid.Namespace,
Name: fmt.Sprintf("druid-%s-%s", druid.Name, "brokers"),
}, brokerDeployment)
return err == nil
}, timeout, interval).Should(BeTrue())

Expect(brokerDeployment.Spec.Template.Spec.Containers).ShouldNot(BeNil())

isClusterContainerExists := false
isNodeContainerExists := false
for _, container := range brokerDeployment.Spec.Template.Spec.Containers {
if container.Name == "cluster-level" {
isClusterContainerExists = true
continue
}
if container.Name == "node-level" {
isNodeContainerExists = true
continue
}
}

Expect(isClusterContainerExists).Should(BeTrue())
Expect(isNodeContainerExists).Should(BeTrue())
})
})
})
2 changes: 2 additions & 0 deletions controllers/druid/testdata/additional-containers.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ apiVersion: druid.apache.org/v1alpha1
kind: Druid
metadata:
name: additional-containers
namespace: default
spec:
image: apache/druid:25.0.0
startScript: /druid.sh
Expand Down Expand Up @@ -43,6 +44,7 @@ spec:
nodes:
brokers:
nodeType: "broker"
kind: "Deployment"
druid.port: 8088
nodeConfigMountPath: "/opt/druid/conf/druid/cluster/query/broker"
replicas: 1
Expand Down
10 changes: 10 additions & 0 deletions controllers/druid/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,16 @@ func timeDifference(epochTime1, epochTime2 int64) int64 {
return int64(diff.Seconds())
}

func containsString(all []string, string string) bool {
for _, s := range all {
if s == string {
return true
}
}

return false
}

func hasDuplicateString(slice []string) (bool, string) {
seen := make(map[string]bool)
for _, s := range slice {
Expand Down

0 comments on commit 2808b45

Please sign in to comment.