Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit test for crd validation #53

Merged
merged 2 commits into from
Jun 18, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 40 additions & 26 deletions internal/controller/numaflowcontrollerrollout_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,53 +18,67 @@ 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"
)

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{
ObjectMeta: metav1.ObjectMeta{
Name: resourceName,
Namespace: namespace,
},
Spec: apiv1.NumaflowControllerRolloutSpec{
Controller: apiv1.Controller{Version: "1.2.1"},
},
}
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: "default",
},
// TODO(user): Specify other spec details if needed.
}
Expect(k8sClient.Create(ctx, resource)).To(Succeed())
}
})

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())
_ = k8sClient.Get(ctx, typeNamespacedName, resource)
_ = k8sClient.Delete(ctx, resource)
})

By("Cleanup the specific resource instance NumaflowControllerRollout")
Expect(k8sClient.Delete(ctx, resource)).To(Succeed())
It("Should throw a CR validation error", func() {
xdevxy marked this conversation as resolved.
Show resolved Hide resolved
By("Creating a NumaflowControllerRollout resource with an invalid name")
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{
Expand Down
Loading