From 39302808245e219df1cfb8b06c3f0c8dd3ade788 Mon Sep 17 00:00:00 2001 From: odubajDT Date: Tue, 7 Mar 2023 11:01:53 +0100 Subject: [PATCH 1/2] test: introduce component test for KeptnTaskDefinition controller Signed-off-by: odubajDT --- .../taskdefinitioncontroller_test.go | 207 ++++++++++++++++++ 1 file changed, 207 insertions(+) create mode 100644 operator/test/component/taskdefinitioncontroller_test.go diff --git a/operator/test/component/taskdefinitioncontroller_test.go b/operator/test/component/taskdefinitioncontroller_test.go new file mode 100644 index 0000000000..5cc107ea39 --- /dev/null +++ b/operator/test/component/taskdefinitioncontroller_test.go @@ -0,0 +1,207 @@ +package component + +import ( + "context" + + klcv1alpha3 "github.com/keptn/lifecycle-toolkit/operator/apis/lifecycle/v1alpha3" + "github.com/keptn/lifecycle-toolkit/operator/controllers/lifecycle/interfaces" + "github.com/keptn/lifecycle-toolkit/operator/controllers/lifecycle/keptntaskdefinition" + . "github.com/onsi/ginkgo/v2" + . "github.com/onsi/gomega" + v1 "k8s.io/api/core/v1" + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" + "k8s.io/apimachinery/pkg/types" +) + +var _ = Describe("KeptnTaskDefinitionController", Ordered, func() { + var ( + taskDefinitionName string + namespace string + ) + + BeforeAll(func() { + // setup once + By("Waiting for Manager") + Eventually(func() bool { + return k8sManager != nil + }).Should(Equal(true)) + + By("Creating the Controller") + + ////setup controllers here + controllers := []interfaces.Controller{&keptntaskdefinition.KeptnTaskDefinitionReconciler{ + Client: k8sManager.GetClient(), + Scheme: k8sManager.GetScheme(), + Recorder: k8sManager.GetEventRecorderFor("test-taskdefinition-controller"), + Log: GinkgoLogr, + }} + setupManager(controllers) // we can register multiple time the same controller + }) + + BeforeEach(func() { // list var here they will be copied for every spec + taskDefinitionName = "my-taskdefinition-reconciling" + namespace = "default" // namespaces are not deleted in the api so be careful + }) + + Describe("Creation of a TaskDefinition", func() { + var ( + taskDefinition *klcv1alpha3.KeptnTaskDefinition + configmap *v1.ConfigMap + ) + Context("Reconcile TaskDefinition", func() { + BeforeEach(func() { + }) + + It("create ConfigMap from inline function", func() { + By("Create TaskDefinition") + taskDefinition = &klcv1alpha3.KeptnTaskDefinition{ + ObjectMeta: metav1.ObjectMeta{ + Name: taskDefinitionName, + Namespace: namespace, + }, + Spec: klcv1alpha3.KeptnTaskDefinitionSpec{ + Function: klcv1alpha3.FunctionSpec{ + Inline: klcv1alpha3.Inline{ + Code: "console.log(Hello);", + }, + }, + }, + } + + err := k8sClient.Create(context.TODO(), taskDefinition) + Expect(err).To(BeNil()) + + By("Check if ConfigMap was created") + + configmap = &v1.ConfigMap{} + Eventually(func(g Gomega) { + err := k8sClient.Get(context.TODO(), types.NamespacedName{ + Namespace: namespace, + Name: "keptnfn-" + taskDefinitionName, + }, configmap) + g.Expect(err).To(BeNil()) + g.Expect(configmap.Data["code"]).To(Equal(taskDefinition.Spec.Function.Inline.Code)) + + }, "30s").Should(Succeed()) + + By("Check if TaskDefinition was updated") + + taskDefinition2 := &klcv1alpha3.KeptnTaskDefinition{} + Eventually(func(g Gomega) { + err := k8sClient.Get(context.TODO(), types.NamespacedName{ + Namespace: namespace, + Name: taskDefinition.Name, + }, taskDefinition2) + g.Expect(err).To(BeNil()) + g.Expect(taskDefinition2.Status.Function.ConfigMap).To(Equal(configmap.Name)) + + }, "30s").Should(Succeed()) + + err = k8sClient.Delete(context.TODO(), configmap) + logErrorIfPresent(err) + }) + + It("TaskDefinition referencing existing Configmap", func() { + By("Create TaskDefinition") + taskDefinition = &klcv1alpha3.KeptnTaskDefinition{ + ObjectMeta: metav1.ObjectMeta{ + Name: taskDefinitionName, + Namespace: namespace, + }, + Spec: klcv1alpha3.KeptnTaskDefinitionSpec{ + Function: klcv1alpha3.FunctionSpec{ + ConfigMapReference: klcv1alpha3.ConfigMapReference{ + Name: "my-configmap", + }, + }, + }, + } + + err := k8sClient.Create(context.TODO(), taskDefinition) + Expect(err).To(BeNil()) + + By("Create ConfigMap") + + configmap = &v1.ConfigMap{ + ObjectMeta: metav1.ObjectMeta{ + Name: "my-configmap", + Namespace: namespace, + }, + Data: map[string]string{ + "code": "console.log(Hello);", + }, + } + + err = k8sClient.Create(context.TODO(), configmap) + Expect(err).To(BeNil()) + + By("Check if TaskDefinition was updated") + + taskDefinition2 := &klcv1alpha3.KeptnTaskDefinition{} + Eventually(func(g Gomega) { + err := k8sClient.Get(context.TODO(), types.NamespacedName{ + Namespace: namespace, + Name: taskDefinition.Name, + }, taskDefinition2) + g.Expect(err).To(BeNil()) + g.Expect(taskDefinition2.Status.Function.ConfigMap).To(Equal(configmap.Name)) + + }, "30s").Should(Succeed()) + + err = k8sClient.Delete(context.TODO(), configmap) + logErrorIfPresent(err) + }) + + It("TaskDefinition referencing non-existing Configmap", func() { + By("Create TaskDefinition") + taskDefinition = &klcv1alpha3.KeptnTaskDefinition{ + ObjectMeta: metav1.ObjectMeta{ + Name: taskDefinitionName, + Namespace: namespace, + }, + Spec: klcv1alpha3.KeptnTaskDefinitionSpec{ + Function: klcv1alpha3.FunctionSpec{ + ConfigMapReference: klcv1alpha3.ConfigMapReference{ + Name: "my-configmap", + }, + }, + }, + } + + err := k8sClient.Create(context.TODO(), taskDefinition) + Expect(err).To(BeNil()) + + By("Check that ConfigMap does not exists") + + configmap = &v1.ConfigMap{} + Eventually(func(g Gomega) { + err := k8sClient.Get(context.TODO(), types.NamespacedName{ + Namespace: namespace, + Name: "my-configmap", + }, configmap) + g.Expect(err).NotTo(BeNil()) + }, "30s").Should(Succeed()) + + By("Check that TaskDefinition was not updated") + + taskDefinition2 := &klcv1alpha3.KeptnTaskDefinition{} + Eventually(func(g Gomega) { + err := k8sClient.Get(context.TODO(), types.NamespacedName{ + Namespace: namespace, + Name: taskDefinition.Name, + }, taskDefinition2) + g.Expect(err).To(BeNil()) + g.Expect(taskDefinition2.Status.Function.ConfigMap).To(Equal("")) + + }, "30s").Should(Succeed()) + + }) + + AfterEach(func() { + err := k8sClient.Delete(context.TODO(), taskDefinition) + logErrorIfPresent(err) + + }) + }) + }) +}) From 3cf1d65c5fd760b4a654a0b0289ccd2887fbcbb5 Mon Sep 17 00:00:00 2001 From: odubajDT Date: Tue, 7 Mar 2023 11:08:21 +0100 Subject: [PATCH 2/2] fix Signed-off-by: odubajDT --- operator/test/component/taskdefinitioncontroller_test.go | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/operator/test/component/taskdefinitioncontroller_test.go b/operator/test/component/taskdefinitioncontroller_test.go index 5cc107ea39..5823553be3 100644 --- a/operator/test/component/taskdefinitioncontroller_test.go +++ b/operator/test/component/taskdefinitioncontroller_test.go @@ -162,7 +162,7 @@ var _ = Describe("KeptnTaskDefinitionController", Ordered, func() { Spec: klcv1alpha3.KeptnTaskDefinitionSpec{ Function: klcv1alpha3.FunctionSpec{ ConfigMapReference: klcv1alpha3.ConfigMapReference{ - Name: "my-configmap", + Name: "my-configmap-non-existing", }, }, }, @@ -177,7 +177,7 @@ var _ = Describe("KeptnTaskDefinitionController", Ordered, func() { Eventually(func(g Gomega) { err := k8sClient.Get(context.TODO(), types.NamespacedName{ Namespace: namespace, - Name: "my-configmap", + Name: "my-configmap-non-existing", }, configmap) g.Expect(err).NotTo(BeNil()) }, "30s").Should(Succeed()) @@ -191,7 +191,7 @@ var _ = Describe("KeptnTaskDefinitionController", Ordered, func() { Name: taskDefinition.Name, }, taskDefinition2) g.Expect(err).To(BeNil()) - g.Expect(taskDefinition2.Status.Function.ConfigMap).To(Equal("")) + g.Expect(taskDefinition2.Status.Function.ConfigMap).NotTo(Equal(configmap.Name)) }, "30s").Should(Succeed())