-
Notifications
You must be signed in to change notification settings - Fork 23
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
167 additions
and
11 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
package controllers | ||
|
||
import ( | ||
"context" | ||
"github.com/google/uuid" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
routev1 "github.com/openshift/api/route/v1" | ||
trustyaiopendatahubiov1alpha1 "github.com/trustyai-explainability/trustyai-service-operator/api/v1alpha1" | ||
"k8s.io/apimachinery/pkg/types" | ||
"k8s.io/client-go/kubernetes/scheme" | ||
"time" | ||
) | ||
|
||
var _ = Describe("Route Reconciliation", func() { | ||
|
||
BeforeEach(func() { | ||
reconciler = &TrustyAIServiceReconciler{ | ||
Client: k8sClient, | ||
Scheme: scheme.Scheme, | ||
EventRecorder: recorder, | ||
} | ||
ctx = context.Background() | ||
}) | ||
|
||
Context("When Route does not exist", func() { | ||
var instance *trustyaiopendatahubiov1alpha1.TrustyAIService | ||
It("Should create Route successfully", func() { | ||
namespace := "route-test-namespace-1" | ||
instance = createDefaultCR(namespace) | ||
instance.ObjectMeta.UID = types.UID(uuid.New().String()) // Set a UID for the TrustyAIService | ||
Eventually(func() error { | ||
return createNamespace(ctx, k8sClient, namespace) | ||
}, time.Second*10, time.Millisecond*250).Should(Succeed(), "failed to create namespace") | ||
|
||
err := reconciler.reconcileRoute(instance, ctx) | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
route := &routev1.Route{} | ||
err = reconciler.Client.Get(ctx, types.NamespacedName{Name: instance.Name, Namespace: instance.Namespace}, route) | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(route).ToNot(BeNil()) | ||
Expect(route.Spec.To.Name).To(Equal(instance.Name)) | ||
}) | ||
}) | ||
|
||
Context("When Route exists and is the same", func() { | ||
var instance *trustyaiopendatahubiov1alpha1.TrustyAIService | ||
It("Should not update Route", func() { | ||
namespace := "route-test-namespace-2" | ||
instance = createDefaultCR(namespace) | ||
instance.ObjectMeta.UID = types.UID(uuid.New().String()) // Set a UID for the TrustyAIService | ||
Eventually(func() error { | ||
return createNamespace(ctx, k8sClient, namespace) | ||
}, time.Second*10, time.Millisecond*250).Should(Succeed(), "failed to create namespace") | ||
|
||
// Create a Route with the expected spec | ||
existingRoute, _ := reconciler.createRouteObject(instance) | ||
Expect(reconciler.Client.Create(ctx, existingRoute)).To(Succeed()) | ||
|
||
err := reconciler.reconcileRoute(instance, ctx) | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
// Fetch the Route | ||
route := &routev1.Route{} | ||
err = reconciler.Client.Get(ctx, types.NamespacedName{Name: instance.Name, Namespace: instance.Namespace}, route) | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(route.Spec).To(Equal(existingRoute.Spec)) | ||
}) | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
package controllers | ||
|
||
import ( | ||
"context" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
trustyaiopendatahubiov1alpha1 "github.com/trustyai-explainability/trustyai-service-operator/api/v1alpha1" | ||
corev1 "k8s.io/api/core/v1" | ||
"k8s.io/apimachinery/pkg/api/resource" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/apimachinery/pkg/types" | ||
"k8s.io/client-go/kubernetes/scheme" | ||
"k8s.io/client-go/tools/record" | ||
"sigs.k8s.io/controller-runtime/pkg/client/fake" | ||
"time" | ||
) | ||
|
||
var _ = Describe("PVC Reconciliation", func() { | ||
|
||
BeforeEach(func() { | ||
k8sClient = fake.NewClientBuilder().WithScheme(scheme.Scheme).Build() | ||
recorder = record.NewFakeRecorder(10) | ||
reconciler = &TrustyAIServiceReconciler{ | ||
Client: k8sClient, | ||
Scheme: scheme.Scheme, | ||
EventRecorder: recorder, | ||
} | ||
ctx = context.Background() | ||
}) | ||
|
||
Context("when the PVC does not exist", func() { | ||
var instance *trustyaiopendatahubiov1alpha1.TrustyAIService | ||
It("should create a new PVC and emit an event", func() { | ||
namespace := "pvc-test-namespace-1" | ||
instance = createDefaultCR(namespace) | ||
Eventually(func() error { | ||
return createNamespace(ctx, k8sClient, namespace) | ||
}, time.Second*10, time.Millisecond*250).Should(Succeed(), "failed to create namespace") | ||
|
||
err := reconciler.ensurePVC(ctx, instance) | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
pvc := &corev1.PersistentVolumeClaim{} | ||
err = k8sClient.Get(ctx, types.NamespacedName{Name: generatePVCName(instance), Namespace: instance.Namespace}, pvc) | ||
Expect(err).ToNot(HaveOccurred()) | ||
Expect(pvc).ToNot(BeNil()) | ||
Expect(pvc.Spec.Resources.Requests[corev1.ResourceStorage]).To(Equal(resource.MustParse(instance.Spec.Storage.Size))) | ||
|
||
// Check for event | ||
Eventually(recorder.Events).Should(Receive(ContainSubstring("Created PVC"))) | ||
}) | ||
}) | ||
|
||
Context("when the PVC already exists", func() { | ||
var instance *trustyaiopendatahubiov1alpha1.TrustyAIService | ||
It("should not attempt to create the PVC", func() { | ||
namespace := "pvc-test-namespace-2" | ||
instance = createDefaultCR(namespace) | ||
Eventually(func() error { | ||
return createNamespace(ctx, k8sClient, namespace) | ||
}, time.Second*10, time.Millisecond*250).Should(Succeed(), "failed to create namespace") | ||
|
||
// Simulate existing PVC | ||
existingPVC := &corev1.PersistentVolumeClaim{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: generatePVCName(instance), | ||
Namespace: instance.Namespace, | ||
}, | ||
} | ||
err := k8sClient.Create(ctx, existingPVC) | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
err = reconciler.ensurePVC(ctx, instance) | ||
Expect(err).ToNot(HaveOccurred()) | ||
|
||
// Check no event related for PVC creation | ||
Consistently(recorder.Events).ShouldNot(Receive(ContainSubstring("Created PVC"))) | ||
}) | ||
}) | ||
|
||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters