Skip to content

Commit 3cfdf3c

Browse files
authored
Merge pull request kubernetes#120434 from pohly/scheduler-backoff-metric-test
scheduler: fix TestIncomingPodsMetrics unit test
2 parents a919079 + 819edda commit 3cfdf3c

File tree

1 file changed

+56
-29
lines changed

1 file changed

+56
-29
lines changed

pkg/scheduler/internal/queue/scheduling_queue_test.go

Lines changed: 56 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2255,7 +2255,7 @@ func TestPriorityQueue_initPodMaxInUnschedulablePodsDuration(t *testing.T) {
22552255
var podInfoList []*framework.QueuedPodInfo
22562256

22572257
for i, op := range test.operations {
2258-
op(logger, queue, test.operands[i])
2258+
op(t, logger, queue, test.operands[i])
22592259
}
22602260

22612261
expectedLen := len(test.expected)
@@ -2278,31 +2278,58 @@ func TestPriorityQueue_initPodMaxInUnschedulablePodsDuration(t *testing.T) {
22782278
}
22792279
}
22802280

2281-
type operation func(logger klog.Logger, queue *PriorityQueue, pInfo *framework.QueuedPodInfo)
2281+
type operation func(t *testing.T, logger klog.Logger, queue *PriorityQueue, pInfo *framework.QueuedPodInfo)
22822282

22832283
var (
2284-
add = func(logger klog.Logger, queue *PriorityQueue, pInfo *framework.QueuedPodInfo) {
2285-
queue.Add(logger, pInfo.Pod)
2284+
add = func(t *testing.T, logger klog.Logger, queue *PriorityQueue, pInfo *framework.QueuedPodInfo) {
2285+
if err := queue.Add(logger, pInfo.Pod); err != nil {
2286+
t.Fatalf("Unexpected error during Add: %v", err)
2287+
}
22862288
}
2287-
addUnschedulablePodBackToUnschedulablePods = func(logger klog.Logger, queue *PriorityQueue, pInfo *framework.QueuedPodInfo) {
2289+
popAndRequeueAsUnschedulable = func(t *testing.T, logger klog.Logger, queue *PriorityQueue, pInfo *framework.QueuedPodInfo) {
22882290
// To simulate the pod is failed in scheduling in the real world, Pop() the pod from activeQ before AddUnschedulableIfNotPresent() below.
2289-
queue.activeQ.Add(queue.newQueuedPodInfo(pInfo.Pod))
2290-
if p, err := queue.Pop(); err != nil || p.Pod != pInfo.Pod {
2291-
panic(fmt.Sprintf("Expected: %v after Pop, but got: %v", pInfo.Pod.Name, p.Pod.Name))
2291+
// UnschedulablePlugins will get cleared by Pop, so make a copy first.
2292+
unschedulablePlugins := pInfo.UnschedulablePlugins.Clone()
2293+
if err := queue.activeQ.Add(queue.newQueuedPodInfo(pInfo.Pod)); err != nil {
2294+
t.Fatalf("Unexpected error during Add: %v", err)
2295+
}
2296+
p, err := queue.Pop()
2297+
if err != nil {
2298+
t.Fatalf("Unexpected error during Pop: %v", err)
2299+
}
2300+
if p.Pod != pInfo.Pod {
2301+
t.Fatalf("Expected: %v after Pop, but got: %v", pInfo.Pod.Name, p.Pod.Name)
2302+
}
2303+
// Simulate plugins that are waiting for some events.
2304+
p.UnschedulablePlugins = unschedulablePlugins
2305+
if err := queue.AddUnschedulableIfNotPresent(logger, p, 1); err != nil {
2306+
t.Fatalf("Unexpected error during AddUnschedulableIfNotPresent: %v", err)
22922307
}
2293-
2294-
queue.AddUnschedulableIfNotPresent(logger, pInfo, 1)
22952308
}
2296-
addUnschedulablePodBackToBackoffQ = func(logger klog.Logger, queue *PriorityQueue, pInfo *framework.QueuedPodInfo) {
2297-
queue.AddUnschedulableIfNotPresent(logger, pInfo, 1)
2309+
popAndRequeueAsBackoff = func(t *testing.T, logger klog.Logger, queue *PriorityQueue, pInfo *framework.QueuedPodInfo) {
2310+
// To simulate the pod is failed in scheduling in the real world, Pop() the pod from activeQ before AddUnschedulableIfNotPresent() below.
2311+
if err := queue.activeQ.Add(queue.newQueuedPodInfo(pInfo.Pod)); err != nil {
2312+
t.Fatalf("Unexpected error during Add: %v", err)
2313+
}
2314+
p, err := queue.Pop()
2315+
if err != nil {
2316+
t.Fatalf("Unexpected error during Pop: %v", err)
2317+
}
2318+
if p.Pod != pInfo.Pod {
2319+
t.Fatalf("Expected: %v after Pop, but got: %v", pInfo.Pod.Name, p.Pod.Name)
2320+
}
2321+
// When there is no known unschedulable plugin, pods always go to the backoff queue.
2322+
if err := queue.AddUnschedulableIfNotPresent(logger, p, 1); err != nil {
2323+
t.Fatalf("Unexpected error during AddUnschedulableIfNotPresent: %v", err)
2324+
}
22982325
}
2299-
addPodActiveQ = func(logger klog.Logger, queue *PriorityQueue, pInfo *framework.QueuedPodInfo) {
2326+
addPodActiveQ = func(t *testing.T, logger klog.Logger, queue *PriorityQueue, pInfo *framework.QueuedPodInfo) {
23002327
queue.activeQ.Add(pInfo)
23012328
}
2302-
updatePodActiveQ = func(logger klog.Logger, queue *PriorityQueue, pInfo *framework.QueuedPodInfo) {
2329+
updatePodActiveQ = func(t *testing.T, logger klog.Logger, queue *PriorityQueue, pInfo *framework.QueuedPodInfo) {
23032330
queue.activeQ.Update(pInfo)
23042331
}
2305-
addPodUnschedulablePods = func(logger klog.Logger, queue *PriorityQueue, pInfo *framework.QueuedPodInfo) {
2332+
addPodUnschedulablePods = func(t *testing.T, logger klog.Logger, queue *PriorityQueue, pInfo *framework.QueuedPodInfo) {
23062333
if !pInfo.Gated {
23072334
// Update pod condition to unschedulable.
23082335
podutil.UpdatePodCondition(&pInfo.Pod.Status, &v1.PodCondition{
@@ -2314,28 +2341,28 @@ var (
23142341
}
23152342
queue.unschedulablePods.addOrUpdate(pInfo)
23162343
}
2317-
deletePod = func(_ klog.Logger, queue *PriorityQueue, pInfo *framework.QueuedPodInfo) {
2344+
deletePod = func(t *testing.T, _ klog.Logger, queue *PriorityQueue, pInfo *framework.QueuedPodInfo) {
23182345
queue.Delete(pInfo.Pod)
23192346
}
2320-
updatePodQueueable = func(logger klog.Logger, queue *PriorityQueue, pInfo *framework.QueuedPodInfo) {
2347+
updatePodQueueable = func(t *testing.T, logger klog.Logger, queue *PriorityQueue, pInfo *framework.QueuedPodInfo) {
23212348
newPod := pInfo.Pod.DeepCopy()
23222349
newPod.Labels = map[string]string{"queueable": ""}
23232350
queue.Update(logger, pInfo.Pod, newPod)
23242351
}
2325-
addPodBackoffQ = func(logger klog.Logger, queue *PriorityQueue, pInfo *framework.QueuedPodInfo) {
2352+
addPodBackoffQ = func(t *testing.T, logger klog.Logger, queue *PriorityQueue, pInfo *framework.QueuedPodInfo) {
23262353
queue.podBackoffQ.Add(pInfo)
23272354
}
2328-
moveAllToActiveOrBackoffQ = func(logger klog.Logger, queue *PriorityQueue, _ *framework.QueuedPodInfo) {
2355+
moveAllToActiveOrBackoffQ = func(t *testing.T, logger klog.Logger, queue *PriorityQueue, _ *framework.QueuedPodInfo) {
23292356
queue.MoveAllToActiveOrBackoffQueue(logger, UnschedulableTimeout, nil, nil, nil)
23302357
}
2331-
flushBackoffQ = func(logger klog.Logger, queue *PriorityQueue, _ *framework.QueuedPodInfo) {
2358+
flushBackoffQ = func(t *testing.T, logger klog.Logger, queue *PriorityQueue, _ *framework.QueuedPodInfo) {
23322359
queue.clock.(*testingclock.FakeClock).Step(2 * time.Second)
23332360
queue.flushBackoffQCompleted(logger)
23342361
}
2335-
moveClockForward = func(logger klog.Logger, queue *PriorityQueue, _ *framework.QueuedPodInfo) {
2362+
moveClockForward = func(t *testing.T, logger klog.Logger, queue *PriorityQueue, _ *framework.QueuedPodInfo) {
23362363
queue.clock.(*testingclock.FakeClock).Step(2 * time.Second)
23372364
}
2338-
flushUnschedulerQ = func(logger klog.Logger, queue *PriorityQueue, _ *framework.QueuedPodInfo) {
2365+
flushUnschedulerQ = func(t *testing.T, logger klog.Logger, queue *PriorityQueue, _ *framework.QueuedPodInfo) {
23392366
queue.clock.(*testingclock.FakeClock).Step(queue.podMaxInUnschedulablePodsDuration)
23402367
queue.flushUnschedulablePodsLeftover(logger)
23412368
}
@@ -2413,7 +2440,7 @@ func TestPodTimestamp(t *testing.T) {
24132440
var podInfoList []*framework.QueuedPodInfo
24142441

24152442
for i, op := range test.operations {
2416-
op(logger, queue, test.operands[i])
2443+
op(t, logger, queue, test.operands[i])
24172444
}
24182445

24192446
expectedLen := len(test.expected)
@@ -2698,7 +2725,7 @@ scheduler_plugin_execution_duration_seconds_count{extension_point="PreEnqueue",p
26982725
queue := NewTestQueue(ctx, newDefaultQueueSort(), WithClock(testingclock.NewFakeClock(timestamp)), WithPreEnqueuePluginMap(m), WithPluginMetricsSamplePercent(test.pluginMetricsSamplePercent), WithMetricsRecorder(*recorder))
26992726
for i, op := range test.operations {
27002727
for _, pInfo := range test.operands[i] {
2701-
op(logger, queue, pInfo)
2728+
op(t, logger, queue, pInfo)
27022729
}
27032730
}
27042731

@@ -2856,7 +2883,7 @@ func TestIncomingPodsMetrics(t *testing.T) {
28562883
{
28572884
name: "add pods to unschedulablePods",
28582885
operations: []operation{
2859-
addUnschedulablePodBackToUnschedulablePods,
2886+
popAndRequeueAsUnschedulable,
28602887
},
28612888
want: `
28622889
scheduler_queue_incoming_pods_total{event="ScheduleAttemptFailure",queue="unschedulable"} 3
@@ -2865,7 +2892,7 @@ func TestIncomingPodsMetrics(t *testing.T) {
28652892
{
28662893
name: "add pods to unschedulablePods and then move all to backoffQ",
28672894
operations: []operation{
2868-
addUnschedulablePodBackToUnschedulablePods,
2895+
popAndRequeueAsUnschedulable,
28692896
moveAllToActiveOrBackoffQ,
28702897
},
28712898
want: ` scheduler_queue_incoming_pods_total{event="ScheduleAttemptFailure",queue="unschedulable"} 3
@@ -2875,7 +2902,7 @@ func TestIncomingPodsMetrics(t *testing.T) {
28752902
{
28762903
name: "add pods to unschedulablePods and then move all to activeQ",
28772904
operations: []operation{
2878-
addUnschedulablePodBackToUnschedulablePods,
2905+
popAndRequeueAsUnschedulable,
28792906
moveClockForward,
28802907
moveAllToActiveOrBackoffQ,
28812908
},
@@ -2886,7 +2913,7 @@ func TestIncomingPodsMetrics(t *testing.T) {
28862913
{
28872914
name: "make some pods subject to backoff and add them to backoffQ, then flush backoffQ",
28882915
operations: []operation{
2889-
addUnschedulablePodBackToBackoffQ,
2916+
popAndRequeueAsBackoff,
28902917
moveClockForward,
28912918
flushBackoffQ,
28922919
},
@@ -2905,7 +2932,7 @@ func TestIncomingPodsMetrics(t *testing.T) {
29052932
queue := NewTestQueue(ctx, newDefaultQueueSort(), WithClock(testingclock.NewFakeClock(timestamp)))
29062933
for _, op := range test.operations {
29072934
for _, pInfo := range pInfos {
2908-
op(logger, queue, pInfo)
2935+
op(t, logger, queue, pInfo)
29092936
}
29102937
}
29112938
metricName := metrics.SchedulerSubsystem + "_" + metrics.SchedulerQueueIncomingPods.Name

0 commit comments

Comments
 (0)