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

Isb service rollout test #48

Closed
wants to merge 15 commits into from
Closed
191 changes: 127 additions & 64 deletions internal/controller/isbservicerollout_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,74 +17,137 @@
package controller

import (
"context"
"context"
"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"
. "github.com/onsi/ginkgo/v2"
. "github.com/onsi/gomega"
"k8s.io/apimachinery/pkg/api/errors"
"k8s.io/apimachinery/pkg/types"

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

apiv1 "github.com/numaproj/numaplane/pkg/apis/numaplane/v1alpha1"
apiv1 "github.com/numaproj/numaplane/pkg/apis/numaplane/v1alpha1"
)

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()

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

Check failure on line 49 in internal/controller/isbservicerollout_controller_test.go

View workflow job for this annotation

GitHub Actions / Lint

undefined: runtime
Raw: []byte(`{
"apiVersion": "v1",
"kind": "Service",
"metadata": {
"name": "my-service"
},
"spec": {
"selector": {
"app": "MyApp"
},
"ports": [{
"protocol": "TCP",
"port": 80,
"targetPort": 9376
}]
}
}`),
},
}
Expect(k8sClient.Create(ctx, resource)).To(Succeed())
}
})

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())

By("Cleanup the specific resource instance ISBServiceRollout")
Expect(k8sClient.Delete(ctx, resource)).To(Succeed())
})
// 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.
// })
})
})
},
}

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")
Expect(createdResource.Spec).Should(Equal(isbServiceRollout.Spec))
})

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

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

// Update the spec here
currentISBServiceRollout.Spec.InterStepBufferService = runtime.RawExtension{

Check failure on line 94 in internal/controller/isbservicerollout_controller_test.go

View workflow job for this annotation

GitHub Actions / Lint

undefined: runtime (typecheck)
Raw: []byte(`{
"apiVersion": "v1",
"kind": "Service",
"metadata": {
"name": "my-updated-service"
},
"spec": {
"selector": {
"app": "MyUpdatedApp"
},
"ports": [{
"protocol": "TCP",
"port": 8080,
"targetPort": 9377
}]
}
}`),
}

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)
return updatedResource.Spec
}, timeout, interval).Should(Equal(currentISBServiceRollout.Spec))
})

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())
})
})

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())

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

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