From 102d79ec93c97c3980cc5a04189790b438b4516f Mon Sep 17 00:00:00 2001 From: Kensei Nakada Date: Tue, 5 Nov 2024 22:48:00 +0900 Subject: [PATCH] add: add a test case for Activate --- .../backend/queue/scheduling_queue_test.go | 61 ++++++++++++++++++- 1 file changed, 60 insertions(+), 1 deletion(-) diff --git a/pkg/scheduler/backend/queue/scheduling_queue_test.go b/pkg/scheduler/backend/queue/scheduling_queue_test.go index aff395abf00..5c74bd130ef 100644 --- a/pkg/scheduler/backend/queue/scheduling_queue_test.go +++ b/pkg/scheduler/backend/queue/scheduling_queue_test.go @@ -1292,13 +1292,17 @@ func TestPriorityQueue_Delete(t *testing.T) { } func TestPriorityQueue_Activate(t *testing.T) { + metrics.Register() tests := []struct { name string qPodInfoInUnschedulablePods []*framework.QueuedPodInfo qPodInfoInPodBackoffQ []*framework.QueuedPodInfo qPodInActiveQ []*v1.Pod qPodInfoToActivate *framework.QueuedPodInfo + qPodInInFlightPod *v1.Pod + expectedInFlightEvent *clusterEvent want []*framework.QueuedPodInfo + qHintEnabled bool }{ { name: "pod already in activeQ", @@ -1311,6 +1315,21 @@ func TestPriorityQueue_Activate(t *testing.T) { qPodInfoToActivate: &framework.QueuedPodInfo{PodInfo: highPriNominatedPodInfo}, want: []*framework.QueuedPodInfo{}, }, + { + name: "[QHint] pod not in unschedulablePods/podBackoffQ but in-flight", + qPodInfoToActivate: &framework.QueuedPodInfo{PodInfo: highPriNominatedPodInfo}, + qPodInInFlightPod: highPriNominatedPodInfo.Pod, + expectedInFlightEvent: &clusterEvent{oldObj: (*v1.Pod)(nil), newObj: highPriNominatedPodInfo.Pod, event: framework.EventForceActivate}, + want: []*framework.QueuedPodInfo{}, + qHintEnabled: true, + }, + { + name: "[QHint] pod not in unschedulablePods/podBackoffQ and not in-flight", + qPodInfoToActivate: &framework.QueuedPodInfo{PodInfo: highPriNominatedPodInfo}, + qPodInInFlightPod: medPriorityPodInfo.Pod, // different pod is in-flight + want: []*framework.QueuedPodInfo{}, + qHintEnabled: true, + }, { name: "pod in unschedulablePods", qPodInfoInUnschedulablePods: []*framework.QueuedPodInfo{{PodInfo: highPriNominatedPodInfo}}, @@ -1327,12 +1346,30 @@ func TestPriorityQueue_Activate(t *testing.T) { for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { + featuregatetesting.SetFeatureGateDuringTest(t, utilfeature.DefaultFeatureGate, features.SchedulerQueueingHints, tt.qHintEnabled) var objs []runtime.Object logger, ctx := ktesting.NewTestContext(t) ctx, cancel := context.WithCancel(ctx) defer cancel() q := NewTestQueueWithObjects(ctx, newDefaultQueueSort(), objs) + if tt.qPodInInFlightPod != nil { + // Put -> Pop the Pod to make it registered in inFlightPods. + q.activeQ.underLock(func(unlockedActiveQ unlockedActiveQueuer) { + unlockedActiveQ.AddOrUpdate(newQueuedPodInfoForLookup(tt.qPodInInFlightPod)) + }) + p, err := q.activeQ.pop(logger) + if err != nil { + t.Fatalf("Pop failed: %v", err) + } + if p.Pod.Name != tt.qPodInInFlightPod.Name { + t.Errorf("Unexpected popped pod: %v", p.Pod.Name) + } + if len(q.activeQ.listInFlightEvents()) != 1 { + t.Fatal("Expected the pod to be recorded in in-flight events, but it doesn't") + } + } + // Prepare activeQ/unschedulablePods/podBackoffQ according to the table for _, qPod := range tt.qPodInActiveQ { q.Add(logger, qPod) @@ -1351,7 +1388,29 @@ func TestPriorityQueue_Activate(t *testing.T) { // Check the result after activation by the length of activeQ if wantLen := len(tt.want); q.activeQ.len() != wantLen { - t.Errorf("length compare: want %v, got %v", wantLen, q.activeQ.len()) + t.Fatalf("length compare: want %v, got %v", wantLen, q.activeQ.len()) + } + + if tt.expectedInFlightEvent != nil { + if len(q.activeQ.listInFlightEvents()) != 2 { + t.Fatalf("Expected two in-flight event to be recorded, but got %v events", len(q.activeQ.listInFlightEvents())) + } + found := false + for _, e := range q.activeQ.listInFlightEvents() { + event, ok := e.(*clusterEvent) + if !ok { + continue + } + + if d := cmp.Diff(tt.expectedInFlightEvent, event, cmpopts.EquateComparable(clusterEvent{})); d != "" { + t.Fatalf("Unexpected in-flight event (-want, +got):\n%s", d) + } + found = true + } + + if !found { + t.Fatalf("Expected in-flight event to be recorded, but it wasn't.") + } } // Check if the specific pod exists in activeQ