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

feat: Add test for ISBServiceRollout #50

Merged
merged 8 commits into from
Jun 17, 2024
Merged
Changes from 4 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
201 changes: 150 additions & 51 deletions internal/controller/isbservicerollout_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,73 +18,172 @@ package controller

import (
"context"
"encoding/json"
"time"

. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/runtime"
"k8s.io/apimachinery/pkg/types"

metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

numaflowv1 "github.com/numaproj/numaflow/pkg/apis/numaflow/v1alpha1"
apiv1 "github.com/numaproj/numaplane/pkg/apis/numaplane/v1alpha1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)

var _ = Describe("ISBServiceRollout Controller", func() {
Context("When reconciling a resource", func() {
const resourceName = "test-resource"

ctx := context.Background()

typeNamespacedName := types.NamespacedName{
Name: resourceName,
Namespace: "default", // TODO(user):Modify as needed
}
isbservicerollout := &apiv1.ISBServiceRollout{}

BeforeEach(func() {
By("creating the custom resource for the Kind ISBServiceRollout")
err := k8sClient.Get(ctx, typeNamespacedName, isbservicerollout)
if err != nil && errors.IsNotFound(err) {
resource := &apiv1.ISBServiceRollout{
ObjectMeta: metav1.ObjectMeta{
Name: resourceName,
Namespace: "default",
},
// TODO(user): Specify other spec details if needed.
Spec: apiv1.ISBServiceRolloutSpec{
InterStepBufferService: runtime.RawExtension{
Raw: []byte(`{"field":"val"}`),
},
},
const (
namespace = "default"
isbServiceRolloutName = "isbservicerollout-test"
timeout = 10 * time.Second
interval = 250 * time.Millisecond
)

ctx := context.Background()

isbsSpec := numaflowv1.InterStepBufferServiceSpec{
Redis: &numaflowv1.RedisBufferService{},
JetStream: &numaflowv1.JetStreamBufferService{
Version: "latest",
Persistence: &numaflowv1.PersistenceStrategy{
VolumeSize: &numaflowv1.DefaultVolumeSize,
},
},
}

isbsSpecRaw, err := json.Marshal(isbsSpec)
Expect(err).ToNot(HaveOccurred())

isbServiceRollout := &apiv1.ISBServiceRollout{
ObjectMeta: metav1.ObjectMeta{
Namespace: namespace,
Name: isbServiceRolloutName,
},
Spec: apiv1.ISBServiceRolloutSpec{
InterStepBufferService: runtime.RawExtension{
Raw: isbsSpecRaw,
},
},
}

resourceLookupKey := types.NamespacedName{Name: isbServiceRolloutName, Namespace: namespace}

Context("When applying a ISBServiceRollout spec", func() {
It("Should create the ISBServiceRollout if it does not exist", func() {
Expect(k8sClient.Create(ctx, isbServiceRollout)).Should(Succeed())

createdResource := &apiv1.ISBServiceRollout{}
Eventually(func() bool {
err := k8sClient.Get(ctx, resourceLookupKey, createdResource)
return err == nil
}, timeout, interval).Should(BeTrue())

By("Verifying the content of the ISBServiceRollout spec field")
createdInterStepBufferServiceSpec := numaflowv1.InterStepBufferService{}
Expect(json.Unmarshal(createdResource.Spec.InterStepBufferService.Raw, &createdInterStepBufferServiceSpec)).ToNot(HaveOccurred())
Expect(createdResource.Spec).Should(Equal(isbServiceRollout.Spec))
xdevxy marked this conversation as resolved.
Show resolved Hide resolved
})

It("Should have created an ISBServiceRollout", func() {
createdISBR := &apiv1.ISBServiceRollout{}
Eventually(func() bool {
err := k8sClient.Get(ctx, resourceLookupKey, createdISBR)
return err == nil
}, timeout, interval).Should(BeTrue())

By("Verifying the content of the ISBServiceRollout spec")
createdInterStepBufferServiceSpec := numaflowv1.InterStepBufferService{}
Expect(json.Unmarshal(createdISBR.Spec.InterStepBufferService.Raw, &createdInterStepBufferServiceSpec)).ToNot(HaveOccurred())
Expect(createdISBR.Spec).Should(Equal(isbServiceRollout.Spec))
})
xdevxy marked this conversation as resolved.
Show resolved Hide resolved

It("Should have the ISBServiceRollout Status Phase as Running", func() {
Consistently(func() (apiv1.Phase, error) {
createdISBR := &apiv1.ISBServiceRollout{}
err := k8sClient.Get(ctx, resourceLookupKey, createdISBR)
if err != nil {
return apiv1.Phase(""), err
}
Expect(k8sClient.Create(ctx, resource)).To(Succeed())
return createdISBR.Status.Phase, nil
}, timeout, interval).Should(Equal(apiv1.PhaseRunning))
})

It("Should update the ISBServiceRollout", func() {
By("updating the ISBServiceRollout")

currentISBServiceRollout := &apiv1.ISBServiceRollout{}
Expect(k8sClient.Get(ctx, resourceLookupKey, currentISBServiceRollout)).ToNot(HaveOccurred())

// Prepare a new spec for update
newIsbsSpec := numaflowv1.InterStepBufferServiceSpec{
Redis: &numaflowv1.RedisBufferService{},
JetStream: &numaflowv1.JetStreamBufferService{
Version: "an updated version",
Persistence: &numaflowv1.PersistenceStrategy{
VolumeSize: &numaflowv1.DefaultVolumeSize,
},
},
}

newIsbsSpecRaw, err := json.Marshal(newIsbsSpec)
Expect(err).ToNot(HaveOccurred())

// Update the spec
currentISBServiceRollout.Spec.InterStepBufferService = runtime.RawExtension{Raw: newIsbsSpecRaw}

Expect(k8sClient.Update(ctx, currentISBServiceRollout)).ToNot(HaveOccurred())

By("Verifying the content of the ISBServiceRollout spec field")
Eventually(func() apiv1.ISBServiceRolloutSpec {
updatedResource := &apiv1.ISBServiceRollout{}
_ = k8sClient.Get(ctx, resourceLookupKey, updatedResource)

createdInterStepBufferServiceSpec := numaflowv1.InterStepBufferService{}
Expect(json.Unmarshal(updatedResource.Spec.InterStepBufferService.Raw, &createdInterStepBufferServiceSpec)).ToNot(HaveOccurred())

return updatedResource.Spec
}, timeout, interval).Should(Equal(currentISBServiceRollout.Spec))

})

AfterEach(func() {
// TODO(user): Cleanup logic after each test, like removing the resource instance.
resource := &apiv1.ISBServiceRollout{}
err := k8sClient.Get(ctx, typeNamespacedName, resource)
Expect(err).NotTo(HaveOccurred())
It("Should delete the ISBServiceRollout", func() {
Expect(k8sClient.Delete(ctx, &apiv1.ISBServiceRollout{
ObjectMeta: isbServiceRollout.ObjectMeta,
})).Should(Succeed())

deletedResource := &apiv1.ISBServiceRollout{}
Eventually(func() bool {
err := k8sClient.Get(ctx, resourceLookupKey, deletedResource)
return errors.IsNotFound(err)
}, timeout, interval).Should(BeTrue())

// TODO: use this on real cluster for e2e tests
// NOTE: it's necessary to run on existing cluster to allow for deletion of child resources.
// See https://book.kubebuilder.io/reference/envtest#testing-considerations for more details.
// Could also reuse the env var used to set useExistingCluster to skip or perform the deletion based on CI settings.
// Eventually(func() bool {
// deletedChildResource := &apiv1.ISBServiceRollout{}
// err := k8sClient.Get(ctx, resourceLookupKey, deletedChildResource)
// return errors.IsNotFound(err)
// }, timeout, interval).Should(BeTrue())
})
})

Context("When applying an invalid ISBServiceRollout spec", func() {
It("Should not create the ISBServiceRollout", func() {
Expect(k8sClient.Create(ctx, &apiv1.ISBServiceRollout{
Spec: isbServiceRollout.Spec,
})).To(HaveOccurred())
xdevxy marked this conversation as resolved.
Show resolved Hide resolved

Expect(k8sClient.Create(ctx, &apiv1.ISBServiceRollout{
ObjectMeta: isbServiceRollout.ObjectMeta,
})).To(HaveOccurred())

By("Cleanup the specific resource instance ISBServiceRollout")
Expect(k8sClient.Delete(ctx, resource)).To(Succeed())
Expect(k8sClient.Create(ctx, &apiv1.ISBServiceRollout{
ObjectMeta: isbServiceRollout.ObjectMeta,
Spec: apiv1.ISBServiceRolloutSpec{},
})).To(HaveOccurred())
})
// It("should successfully reconcile the resource", func() {
// By("Reconciling the created resource")
// controllerReconciler := &ISBServiceRolloutReconciler{
// client: k8sClient,
// scheme: k8sClient.Scheme(),
// restConfig: cfg,
// }

// _, err := controllerReconciler.Reconcile(ctx, sigsReconcile.Request{
// NamespacedName: typeNamespacedName,
// })
// Expect(err).NotTo(HaveOccurred())
// // TODO(user): Add more specific assertions depending on your controller's reconciliation logic.
// // Example: If you expect a certain status condition after reconciliation, verify it here.
// })
})
})
Loading