From ab0a0f85989ad824e1ba13c7777d33170a15b6f3 Mon Sep 17 00:00:00 2001 From: chandankumar4 Date: Wed, 12 Jun 2024 09:22:23 +0530 Subject: [PATCH 1/2] Add unit test for crd validation Signed-off-by: chandankumar4 --- ...maflowcontrollerrollout_controller_test.go | 26 ++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/internal/controller/numaflowcontrollerrollout_controller_test.go b/internal/controller/numaflowcontrollerrollout_controller_test.go index 288bd874..6adfe02a 100644 --- a/internal/controller/numaflowcontrollerrollout_controller_test.go +++ b/internal/controller/numaflowcontrollerrollout_controller_test.go @@ -30,14 +30,17 @@ import ( ) var _ = Describe("NumaflowControllerRollout Controller", func() { + const ( + namespace = "default" + ) Context("When reconciling a resource", func() { - const resourceName = "test-resource" + const resourceName = "numaflow-controller" ctx := context.Background() typeNamespacedName := types.NamespacedName{ Name: resourceName, - Namespace: "default", // TODO(user):Modify as needed + Namespace: namespace, // TODO(user):Modify as needed } numaflowcontrollerrollout := &apiv1.NumaflowControllerRollout{} @@ -48,7 +51,7 @@ var _ = Describe("NumaflowControllerRollout Controller", func() { resource := &apiv1.NumaflowControllerRollout{ ObjectMeta: metav1.ObjectMeta{ Name: resourceName, - Namespace: "default", + Namespace: namespace, }, // TODO(user): Specify other spec details if needed. } @@ -65,6 +68,23 @@ var _ = Describe("NumaflowControllerRollout Controller", func() { By("Cleanup the specific resource instance NumaflowControllerRollout") Expect(k8sClient.Delete(ctx, resource)).To(Succeed()) }) + + It("Should throw a CR validation error", func() { + By("Creating a NumaflowControllerRollout resource with an invalid name") + resource := &apiv1.NumaflowControllerRollout{ + ObjectMeta: metav1.ObjectMeta{ + Name: "test-numaflow-controller", // invalid name: only supported name is "numaflow-controller" + Namespace: namespace, + }, + Spec: apiv1.NumaflowControllerRolloutSpec{ + Controller: apiv1.Controller{Version: "1.2.1"}, + }, + } + err := k8sClient.Create(ctx, resource) + Expect(err).NotTo(Succeed()) + Expect(err.Error()).To(ContainSubstring("The metadata name must be 'numaflow-controller'")) + }) + // It("should successfully reconcile the resource", func() { // By("Reconciling the created resource") // controllerReconciler := &NumaflowControllerRolloutReconciler{ From ae98eeaf9c5108f967d4943686cf8f3ccc2c4b92 Mon Sep 17 00:00:00 2001 From: chandankumar4 Date: Tue, 18 Jun 2024 09:28:36 +0530 Subject: [PATCH 2/2] Add unit test for duplicate resource Signed-off-by: chandankumar4 --- ...maflowcontrollerrollout_controller_test.go | 64 +++++++++---------- 1 file changed, 29 insertions(+), 35 deletions(-) diff --git a/internal/controller/numaflowcontrollerrollout_controller_test.go b/internal/controller/numaflowcontrollerrollout_controller_test.go index 6adfe02a..ab41446b 100644 --- a/internal/controller/numaflowcontrollerrollout_controller_test.go +++ b/internal/controller/numaflowcontrollerrollout_controller_test.go @@ -18,13 +18,11 @@ package controller import ( "context" - "strings" - . "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" + "strings" apiv1 "github.com/numaproj/numaplane/pkg/apis/numaplane/v1alpha1" ) @@ -42,49 +40,45 @@ var _ = Describe("NumaflowControllerRollout Controller", func() { Name: resourceName, Namespace: namespace, // TODO(user):Modify as needed } - numaflowcontrollerrollout := &apiv1.NumaflowControllerRollout{} - - BeforeEach(func() { - By("creating the custom resource for the Kind NumaflowControllerRollout") - err := k8sClient.Get(ctx, typeNamespacedName, numaflowcontrollerrollout) - if err != nil && errors.IsNotFound(err) { - resource := &apiv1.NumaflowControllerRollout{ - ObjectMeta: metav1.ObjectMeta{ - Name: resourceName, - Namespace: namespace, - }, - // TODO(user): Specify other spec details if needed. - } - Expect(k8sClient.Create(ctx, resource)).To(Succeed()) - } - }) + + numaflowControllerRollout := apiv1.NumaflowControllerRollout{ + ObjectMeta: metav1.ObjectMeta{ + Name: resourceName, + Namespace: namespace, + }, + Spec: apiv1.NumaflowControllerRolloutSpec{ + Controller: apiv1.Controller{Version: "1.2.1"}, + }, + } AfterEach(func() { - // TODO(user): Cleanup logic after each test, like removing the resource instance. + // Cleanup the resource after each test and ignore the error if it doesn't exist resource := &apiv1.NumaflowControllerRollout{} - err := k8sClient.Get(ctx, typeNamespacedName, resource) - Expect(err).NotTo(HaveOccurred()) - - By("Cleanup the specific resource instance NumaflowControllerRollout") - Expect(k8sClient.Delete(ctx, resource)).To(Succeed()) + _ = k8sClient.Get(ctx, typeNamespacedName, resource) + _ = k8sClient.Delete(ctx, resource) }) It("Should throw a CR validation error", func() { By("Creating a NumaflowControllerRollout resource with an invalid name") - resource := &apiv1.NumaflowControllerRollout{ - ObjectMeta: metav1.ObjectMeta{ - Name: "test-numaflow-controller", // invalid name: only supported name is "numaflow-controller" - Namespace: namespace, - }, - Spec: apiv1.NumaflowControllerRolloutSpec{ - Controller: apiv1.Controller{Version: "1.2.1"}, - }, - } - err := k8sClient.Create(ctx, resource) + resource := numaflowControllerRollout + resource.Name = "test-numaflow-controller" + err := k8sClient.Create(ctx, &resource) Expect(err).NotTo(Succeed()) Expect(err.Error()).To(ContainSubstring("The metadata name must be 'numaflow-controller'")) }) + It("Should throw duplicate resource error", func() { + By("Creating duplicate NumaflowControllerRollout resource with the same name") + resource := numaflowControllerRollout + err := k8sClient.Create(ctx, &resource) + Expect(err).To(Succeed()) + + resource.ResourceVersion = "" // Reset the resource version to create a new resource + err = k8sClient.Create(ctx, &resource) + Expect(err).NotTo(Succeed()) + Expect(err.Error()).To(ContainSubstring("numaflowcontrollerrollouts.numaplane.numaproj.io \"numaflow-controller\" already exists")) + }) + // It("should successfully reconcile the resource", func() { // By("Reconciling the created resource") // controllerReconciler := &NumaflowControllerRolloutReconciler{