Skip to content

Commit 2dc7e03

Browse files
committed
Enhance unit test
1 parent 53872cf commit 2dc7e03

File tree

1 file changed

+39
-19
lines changed

1 file changed

+39
-19
lines changed

internal/controller/handler_test.go

Lines changed: 39 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -528,7 +528,23 @@ var _ = Describe("eventHandler", func() {
528528
poolUID1 := types.UID("uid1")
529529
poolUID2 := types.UID("uid2")
530530

531-
fakeProcessor.ProcessReturns(&graph.Graph{
531+
pool1 := &inference.InferencePool{
532+
ObjectMeta: metav1.ObjectMeta{
533+
Name: poolName1,
534+
Namespace: namespace,
535+
UID: poolUID1,
536+
},
537+
Spec: inference.InferencePoolSpec{
538+
Selector: inference.LabelSelector{
539+
MatchLabels: map[inference.LabelKey]inference.LabelValue{"app": "foo"},
540+
},
541+
TargetPorts: []inference.Port{
542+
{Number: 8081},
543+
},
544+
},
545+
}
546+
547+
g := &graph.Graph{
532548
Gateways: map[types.NamespacedName]*graph.Gateway{
533549
{}: {
534550
Source: &gatewayv1.Gateway{
@@ -541,23 +557,7 @@ var _ = Describe("eventHandler", func() {
541557
},
542558
},
543559
ReferencedInferencePools: map[types.NamespacedName]*graph.ReferencedInferencePool{
544-
{Namespace: namespace, Name: poolName1}: {
545-
Source: &inference.InferencePool{
546-
ObjectMeta: metav1.ObjectMeta{
547-
Name: poolName1,
548-
Namespace: namespace,
549-
UID: poolUID1,
550-
},
551-
Spec: inference.InferencePoolSpec{
552-
Selector: inference.LabelSelector{
553-
MatchLabels: map[inference.LabelKey]inference.LabelValue{"app": "foo"},
554-
},
555-
TargetPorts: []inference.Port{
556-
{Number: 8081},
557-
},
558-
},
559-
},
560-
},
560+
{Namespace: namespace, Name: poolName1}: {Source: pool1},
561561
{Namespace: namespace, Name: poolName2}: {
562562
Source: &inference.InferencePool{
563563
ObjectMeta: metav1.ObjectMeta{
@@ -576,7 +576,9 @@ var _ = Describe("eventHandler", func() {
576576
},
577577
},
578578
},
579-
})
579+
}
580+
581+
fakeProcessor.ProcessReturns(g)
580582

581583
e := &events.UpsertEvent{Resource: &gatewayv1.HTTPRoute{}}
582584
batch := []any{e}
@@ -608,6 +610,24 @@ var _ = Describe("eventHandler", func() {
608610
Expect(svc2.OwnerReferences).To(HaveLen(1))
609611
Expect(svc2.OwnerReferences[0].Name).To(Equal(poolName2))
610612
Expect(svc2.OwnerReferences[0].UID).To(Equal(poolUID2))
613+
614+
// Now update pool1's selector and ensure the Service selector is updated
615+
updatedSelector := map[inference.LabelKey]inference.LabelValue{"app": "baz"}
616+
pool1.Spec.Selector.MatchLabels = updatedSelector
617+
618+
// Simulate the updated pool in the graph
619+
g.ReferencedInferencePools[types.NamespacedName{Namespace: namespace, Name: poolName1}].Source = pool1
620+
fakeProcessor.ProcessReturns(g)
621+
622+
e = &events.UpsertEvent{Resource: &inference.InferencePool{}}
623+
batch = []any{e}
624+
handler.HandleEventBatch(context.Background(), logr.Discard(), batch)
625+
626+
// Check that the Service selector was updated
627+
svc1 = &v1.Service{}
628+
err = fakeK8sClient.Get(context.Background(), types.NamespacedName{Name: svcName1, Namespace: namespace}, svc1)
629+
Expect(err).ToNot(HaveOccurred())
630+
Expect(svc1.Spec.Selector).To(HaveKeyWithValue("app", "baz"))
611631
})
612632

613633
It("should panic for an unknown event type", func() {

0 commit comments

Comments
 (0)