Skip to content

Commit 38f68d5

Browse files
authored
Merge pull request kubernetes#127256 from dom4ha/scheduler_test_logging
Enable testing logger in the remaining scheduler tests.
2 parents e3a81ab + 2a2b743 commit 38f68d5

File tree

14 files changed

+130
-92
lines changed

14 files changed

+130
-92
lines changed

pkg/scheduler/backend/queue/scheduling_queue_test.go

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1425,8 +1425,8 @@ func TestPriorityQueue_addToActiveQ(t *testing.T) {
14251425

14261426
for _, tt := range tests {
14271427
t.Run(tt.name, func(t *testing.T) {
1428-
ctx, cancel := context.WithCancel(context.Background())
1429-
logger := klog.FromContext(ctx)
1428+
logger, ctx := ktesting.NewTestContext(t)
1429+
ctx, cancel := context.WithCancel(ctx)
14301430
defer cancel()
14311431

14321432
m := map[string][]framework.PreEnqueuePlugin{"": tt.plugins}
@@ -1507,7 +1507,7 @@ func BenchmarkMoveAllToActiveOrBackoffQueue(b *testing.B) {
15071507
for _, tt := range tests {
15081508
for _, podsInUnschedulablePods := range []int{1000, 5000} {
15091509
b.Run(fmt.Sprintf("%v-%v", tt.name, podsInUnschedulablePods), func(b *testing.B) {
1510-
logger, _ := ktesting.NewTestContext(b)
1510+
logger, ctx := ktesting.NewTestContext(b)
15111511
for i := 0; i < b.N; i++ {
15121512
b.StopTimer()
15131513
c := testingclock.NewFakeClock(time.Now())
@@ -1528,7 +1528,7 @@ func BenchmarkMoveAllToActiveOrBackoffQueue(b *testing.B) {
15281528
}
15291529
}
15301530

1531-
ctx, cancel := context.WithCancel(context.Background())
1531+
ctx, cancel := context.WithCancel(ctx)
15321532
defer cancel()
15331533
q := NewTestQueue(ctx, newDefaultQueueSort(), WithClock(c), WithQueueingHintMapPerProfile(m))
15341534

@@ -2103,14 +2103,14 @@ func TestPriorityQueue_NominatedPodDeleted(t *testing.T) {
21032103

21042104
for _, tt := range tests {
21052105
t.Run(tt.name, func(t *testing.T) {
2106-
logger, _ := ktesting.NewTestContext(t)
2106+
logger, ctx := ktesting.NewTestContext(t)
21072107
cs := fake.NewClientset(tt.podInfo.Pod)
21082108
informerFactory := informers.NewSharedInformerFactory(cs, 0)
21092109
podLister := informerFactory.Core().V1().Pods().Lister()
21102110

21112111
// Build a PriorityQueue.
21122112
q := NewPriorityQueue(newDefaultQueueSort(), informerFactory, WithPodLister(podLister))
2113-
ctx, cancel := context.WithCancel(context.Background())
2113+
ctx, cancel := context.WithCancel(ctx)
21142114
defer cancel()
21152115
informerFactory.Start(ctx.Done())
21162116
informerFactory.WaitForCacheSync(ctx.Done())
@@ -2269,7 +2269,8 @@ func TestPriorityQueue_UpdateNominatedPodForNode(t *testing.T) {
22692269
}
22702270

22712271
func TestPriorityQueue_NewWithOptions(t *testing.T) {
2272-
ctx, cancel := context.WithCancel(context.Background())
2272+
_, ctx := ktesting.NewTestContext(t)
2273+
ctx, cancel := context.WithCancel(ctx)
22732274
defer cancel()
22742275
q := NewTestQueue(ctx,
22752276
newDefaultQueueSort(),
@@ -3631,7 +3632,8 @@ func TestPriorityQueue_calculateBackoffDuration(t *testing.T) {
36313632
}
36323633
for _, tt := range tests {
36333634
t.Run(tt.name, func(t *testing.T) {
3634-
ctx, cancel := context.WithCancel(context.Background())
3635+
_, ctx := ktesting.NewTestContext(t)
3636+
ctx, cancel := context.WithCancel(ctx)
36353637
defer cancel()
36363638
q := NewTestQueue(ctx, newDefaultQueueSort(), WithPodInitialBackoffDuration(tt.initialBackoffDuration), WithPodMaxBackoffDuration(tt.maxBackoffDuration))
36373639
if got := q.calculateBackoffDuration(tt.podInfo); got != tt.want {

pkg/scheduler/framework/parallelize/error_channel_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ import (
2020
"context"
2121
"errors"
2222
"testing"
23+
24+
"k8s.io/klog/v2/ktesting"
2325
)
2426

2527
func TestErrorChannel(t *testing.T) {
@@ -35,7 +37,8 @@ func TestErrorChannel(t *testing.T) {
3537
t.Errorf("expect %v from err channel, but got %v", err, actualErr)
3638
}
3739

38-
ctx, cancel := context.WithCancel(context.Background())
40+
_, ctx := ktesting.NewTestContext(t)
41+
ctx, cancel := context.WithCancel(ctx)
3942
errCh.SendErrorWithCancel(err, cancel)
4043
if actualErr := errCh.ReceiveError(); actualErr != err {
4144
t.Errorf("expect %v from err channel, but got %v", err, actualErr)

pkg/scheduler/framework/plugins/interpodaffinity/filtering_test.go

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -982,11 +982,12 @@ func TestPreFilterDisabled(t *testing.T) {
982982
nodeInfo := framework.NewNodeInfo()
983983
node := v1.Node{}
984984
nodeInfo.SetNode(&node)
985-
ctx, cancel := context.WithCancel(context.Background())
985+
_, ctx := ktesting.NewTestContext(t)
986+
ctx, cancel := context.WithCancel(ctx)
986987
defer cancel()
987988
p := plugintesting.SetupPluginWithInformers(ctx, t, New, &config.InterPodAffinityArgs{}, cache.NewEmptySnapshot(), nil)
988989
cycleState := framework.NewCycleState()
989-
gotStatus := p.(framework.FilterPlugin).Filter(context.Background(), cycleState, pod, nodeInfo)
990+
gotStatus := p.(framework.FilterPlugin).Filter(ctx, cycleState, pod, nodeInfo)
990991
wantStatus := framework.AsStatus(fmt.Errorf(`error reading "PreFilterInterPodAffinity" from cycleState: %w`, framework.ErrNotFound))
991992
if !reflect.DeepEqual(gotStatus, wantStatus) {
992993
t.Errorf("status does not match: %v, want: %v", gotStatus, wantStatus)
@@ -1244,7 +1245,7 @@ func TestPreFilterStateAddRemovePod(t *testing.T) {
12441245
return p.(*InterPodAffinity), cycleState, state, snapshot
12451246
}
12461247

1247-
ctx := context.Background()
1248+
_, ctx := ktesting.NewTestContext(t)
12481249
// allPodsState is the state produced when all pods, including test.addedPod are given to prefilter.
12491250
_, _, allPodsState, _ := getState(append(test.existingPods, test.addedPod))
12501251

@@ -1277,7 +1278,7 @@ func TestPreFilterStateAddRemovePod(t *testing.T) {
12771278
}
12781279

12791280
// Remove the added pod pod and make sure it is equal to the original state.
1280-
if err := ipa.RemovePod(context.Background(), cycleState, test.pendingPod, mustNewPodInfo(t, test.addedPod), nodeInfo); err != nil {
1281+
if err := ipa.RemovePod(ctx, cycleState, test.pendingPod, mustNewPodInfo(t, test.addedPod), nodeInfo); err != nil {
12811282
t.Errorf("error removing pod from meta: %v", err)
12821283
}
12831284
if !reflect.DeepEqual(originalState, state) {
@@ -1429,7 +1430,8 @@ func TestGetTPMapMatchingIncomingAffinityAntiAffinity(t *testing.T) {
14291430
t.Run(tt.name, func(t *testing.T) {
14301431
snapshot := cache.NewSnapshot(tt.existingPods, tt.nodes)
14311432
l, _ := snapshot.NodeInfos().List()
1432-
ctx, cancel := context.WithCancel(context.Background())
1433+
_, ctx := ktesting.NewTestContext(t)
1434+
ctx, cancel := context.WithCancel(ctx)
14331435
defer cancel()
14341436
p := plugintesting.SetupPluginWithInformers(ctx, t, New, &config.InterPodAffinityArgs{}, snapshot, nil)
14351437
gotAffinityPodsMap, gotAntiAffinityPodsMap := p.(*InterPodAffinity).getIncomingAffinityAntiAffinityCounts(ctx, mustNewPodInfo(t, tt.pod), l)

pkg/scheduler/framework/plugins/nodeaffinity/node_affinity_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -915,15 +915,15 @@ func TestNodeAffinity(t *testing.T) {
915915
state := framework.NewCycleState()
916916
var gotStatus *framework.Status
917917
if test.runPreFilter {
918-
gotPreFilterResult, gotStatus := p.(framework.PreFilterPlugin).PreFilter(context.Background(), state, test.pod)
918+
gotPreFilterResult, gotStatus := p.(framework.PreFilterPlugin).PreFilter(ctx, state, test.pod)
919919
if diff := cmp.Diff(test.wantPreFilterStatus, gotStatus); diff != "" {
920920
t.Errorf("unexpected PreFilter Status (-want,+got):\n%s", diff)
921921
}
922922
if diff := cmp.Diff(test.wantPreFilterResult, gotPreFilterResult); diff != "" {
923923
t.Errorf("unexpected PreFilterResult (-want,+got):\n%s", diff)
924924
}
925925
}
926-
gotStatus = p.(framework.FilterPlugin).Filter(context.Background(), state, test.pod, nodeInfo)
926+
gotStatus = p.(framework.FilterPlugin).Filter(ctx, state, test.pod, nodeInfo)
927927
if diff := cmp.Diff(test.wantStatus, gotStatus); diff != "" {
928928
t.Errorf("unexpected Filter Status (-want,+got):\n%s", diff)
929929
}

pkg/scheduler/framework/plugins/noderesources/fit_test.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -732,7 +732,7 @@ func TestRestartableInitContainers(t *testing.T) {
732732
t.Fatal(err)
733733
}
734734
cycleState := framework.NewCycleState()
735-
_, preFilterStatus := p.(framework.PreFilterPlugin).PreFilter(context.Background(), cycleState, test.pod)
735+
_, preFilterStatus := p.(framework.PreFilterPlugin).PreFilter(ctx, cycleState, test.pod)
736736
if diff := cmp.Diff(test.wantPreFilterStatus, preFilterStatus); diff != "" {
737737
t.Error("status does not match (-expected +actual):\n", diff)
738738
}
@@ -1112,7 +1112,8 @@ func TestEventsToRegister(t *testing.T) {
11121112
for _, test := range tests {
11131113
t.Run(test.name, func(t *testing.T) {
11141114
fp := &Fit{enableInPlacePodVerticalScaling: test.inPlacePodVerticalScalingEnabled}
1115-
actualClusterEvents, err := fp.EventsToRegister(context.TODO())
1115+
_, ctx := ktesting.NewTestContext(t)
1116+
actualClusterEvents, err := fp.EventsToRegister(ctx)
11161117
if err != nil {
11171118
t.Fatal(err)
11181119
}

pkg/scheduler/framework/plugins/noderesources/requested_to_capacity_ratio_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -328,11 +328,11 @@ func TestResourceBinPackingSingleExtended(t *testing.T) {
328328

329329
var gotList framework.NodeScoreList
330330
for _, n := range test.nodes {
331-
status := p.(framework.PreScorePlugin).PreScore(context.Background(), state, test.pod, tf.BuildNodeInfos(test.nodes))
331+
status := p.(framework.PreScorePlugin).PreScore(ctx, state, test.pod, tf.BuildNodeInfos(test.nodes))
332332
if !status.IsSuccess() {
333333
t.Errorf("PreScore is expected to return success, but didn't. Got status: %v", status)
334334
}
335-
score, status := p.(framework.ScorePlugin).Score(context.Background(), state, test.pod, n.Name)
335+
score, status := p.(framework.ScorePlugin).Score(ctx, state, test.pod, n.Name)
336336
if !status.IsSuccess() {
337337
t.Errorf("Score is expected to return success, but didn't. Got status: %v", status)
338338
}
@@ -554,14 +554,14 @@ func TestResourceBinPackingMultipleExtended(t *testing.T) {
554554
t.Fatalf("unexpected error: %v", err)
555555
}
556556

557-
status := p.(framework.PreScorePlugin).PreScore(context.Background(), state, test.pod, tf.BuildNodeInfos(test.nodes))
557+
status := p.(framework.PreScorePlugin).PreScore(ctx, state, test.pod, tf.BuildNodeInfos(test.nodes))
558558
if !status.IsSuccess() {
559559
t.Errorf("PreScore is expected to return success, but didn't. Got status: %v", status)
560560
}
561561

562562
var gotScores framework.NodeScoreList
563563
for _, n := range test.nodes {
564-
score, status := p.(framework.ScorePlugin).Score(context.Background(), state, test.pod, n.Name)
564+
score, status := p.(framework.ScorePlugin).Score(ctx, state, test.pod, n.Name)
565565
if !status.IsSuccess() {
566566
t.Errorf("Score is expected to return success, but didn't. Got status: %v", status)
567567
}

pkg/scheduler/framework/plugins/podtopologyspread/filtering_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3064,7 +3064,7 @@ func TestSingleConstraint(t *testing.T) {
30643064

30653065
for _, node := range tt.nodes {
30663066
nodeInfo, _ := snapshot.NodeInfos().Get(node.Name)
3067-
status := p.Filter(context.Background(), state, tt.pod, nodeInfo)
3067+
status := p.Filter(ctx, state, tt.pod, nodeInfo)
30683068
if len(tt.wantStatusCode) != 0 && status.Code() != tt.wantStatusCode[node.Name] {
30693069
t.Errorf("[%s]: expected status code %v got %v", node.Name, tt.wantStatusCode[node.Name], status.Code())
30703070
}
@@ -3408,7 +3408,7 @@ func TestMultipleConstraints(t *testing.T) {
34083408

34093409
for _, node := range tt.nodes {
34103410
nodeInfo, _ := snapshot.NodeInfos().Get(node.Name)
3411-
status := p.Filter(context.Background(), state, tt.pod, nodeInfo)
3411+
status := p.Filter(ctx, state, tt.pod, nodeInfo)
34123412
if len(tt.wantStatusCode) != 0 && status.Code() != tt.wantStatusCode[node.Name] {
34133413
t.Errorf("[%s]: expected error code %v got %v", node.Name, tt.wantStatusCode[node.Name], status.Code())
34143414
}
@@ -3425,7 +3425,7 @@ func TestPreFilterDisabled(t *testing.T) {
34253425
_, ctx := ktesting.NewTestContext(t)
34263426
p := plugintesting.SetupPlugin(ctx, t, topologySpreadFunc, &config.PodTopologySpreadArgs{DefaultingType: config.ListDefaulting}, cache.NewEmptySnapshot())
34273427
cycleState := framework.NewCycleState()
3428-
gotStatus := p.(*PodTopologySpread).Filter(context.Background(), cycleState, pod, nodeInfo)
3428+
gotStatus := p.(*PodTopologySpread).Filter(ctx, cycleState, pod, nodeInfo)
34293429
wantStatus := framework.AsStatus(fmt.Errorf(`reading "PreFilterPodTopologySpread" from cycleState: %w`, framework.ErrNotFound))
34303430
if !reflect.DeepEqual(gotStatus, wantStatus) {
34313431
t.Errorf("status does not match: %v, want: %v", gotStatus, wantStatus)

pkg/scheduler/framework/plugins/podtopologyspread/scoring_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,14 +1429,14 @@ func BenchmarkTestPodTopologySpreadScore(b *testing.B) {
14291429
var gotList framework.NodeScoreList
14301430
for _, n := range filteredNodes {
14311431
nodeName := n.Name
1432-
score, status := p.Score(context.Background(), state, tt.pod, nodeName)
1432+
score, status := p.Score(ctx, state, tt.pod, nodeName)
14331433
if !status.IsSuccess() {
14341434
b.Fatalf("unexpected error: %v", status)
14351435
}
14361436
gotList = append(gotList, framework.NodeScore{Name: nodeName, Score: score})
14371437
}
14381438

1439-
status = p.NormalizeScore(context.Background(), state, tt.pod, gotList)
1439+
status = p.NormalizeScore(ctx, state, tt.pod, gotList)
14401440
if !status.IsSuccess() {
14411441
b.Fatal(status)
14421442
}

pkg/scheduler/framework/plugins/volumerestrictions/volume_restrictions_test.go

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,8 @@ func TestGCEDiskConflicts(t *testing.T) {
9999

100100
for _, test := range tests {
101101
t.Run(test.name, func(t *testing.T) {
102-
ctx, cancel := context.WithCancel(context.Background())
102+
_, ctx := ktesting.NewTestContext(t)
103+
ctx, cancel := context.WithCancel(ctx)
103104
defer cancel()
104105
p := newPlugin(ctx, t)
105106
cycleState := framework.NewCycleState()
@@ -173,7 +174,8 @@ func TestAWSDiskConflicts(t *testing.T) {
173174

174175
for _, test := range tests {
175176
t.Run(test.name, func(t *testing.T) {
176-
ctx, cancel := context.WithCancel(context.Background())
177+
_, ctx := ktesting.NewTestContext(t)
178+
ctx, cancel := context.WithCancel(ctx)
177179
defer cancel()
178180
p := newPlugin(ctx, t)
179181
cycleState := framework.NewCycleState()
@@ -253,7 +255,8 @@ func TestRBDDiskConflicts(t *testing.T) {
253255

254256
for _, test := range tests {
255257
t.Run(test.name, func(t *testing.T) {
256-
ctx, cancel := context.WithCancel(context.Background())
258+
_, ctx := ktesting.NewTestContext(t)
259+
ctx, cancel := context.WithCancel(ctx)
257260
defer cancel()
258261
p := newPlugin(ctx, t)
259262
cycleState := framework.NewCycleState()
@@ -333,7 +336,8 @@ func TestISCSIDiskConflicts(t *testing.T) {
333336

334337
for _, test := range tests {
335338
t.Run(test.name, func(t *testing.T) {
336-
ctx, cancel := context.WithCancel(context.Background())
339+
_, ctx := ktesting.NewTestContext(t)
340+
ctx, cancel := context.WithCancel(ctx)
337341
defer cancel()
338342
p := newPlugin(ctx, t)
339343
cycleState := framework.NewCycleState()
@@ -460,7 +464,8 @@ func TestAccessModeConflicts(t *testing.T) {
460464

461465
for _, test := range tests {
462466
t.Run(test.name, func(t *testing.T) {
463-
ctx, cancel := context.WithCancel(context.Background())
467+
_, ctx := ktesting.NewTestContext(t)
468+
ctx, cancel := context.WithCancel(ctx)
464469
defer cancel()
465470
p := newPluginWithListers(ctx, t, test.existingPods, test.existingNodes, test.existingPVCs)
466471
cycleState := framework.NewCycleState()
@@ -660,8 +665,8 @@ func Test_isSchedulableAfterPodDeleted(t *testing.T) {
660665

661666
for name, tc := range testcases {
662667
t.Run(name, func(t *testing.T) {
663-
logger, _ := ktesting.NewTestContext(t)
664-
ctx, cancel := context.WithCancel(context.Background())
668+
logger, ctx := ktesting.NewTestContext(t)
669+
ctx, cancel := context.WithCancel(ctx)
665670
defer cancel()
666671
p := newPluginWithListers(ctx, t, tc.existingPods, nil, []*v1.PersistentVolumeClaim{tc.existingPVC})
667672

@@ -754,8 +759,8 @@ func Test_isSchedulableAfterPersistentVolumeClaimChange(t *testing.T) {
754759

755760
for name, tc := range testcases {
756761
t.Run(name, func(t *testing.T) {
757-
logger, _ := ktesting.NewTestContext(t)
758-
ctx, cancel := context.WithCancel(context.Background())
762+
logger, ctx := ktesting.NewTestContext(t)
763+
ctx, cancel := context.WithCancel(ctx)
759764
defer cancel()
760765
p := newPluginWithListers(ctx, t, tc.existingPods, nil, []*v1.PersistentVolumeClaim{tc.newObj.(*v1.PersistentVolumeClaim)})
761766

pkg/scheduler/framework/plugins/volumezone/volume_zone_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -803,7 +803,8 @@ func BenchmarkVolumeZone(b *testing.B) {
803803

804804
for _, tt := range tests {
805805
b.Run(tt.Name, func(b *testing.B) {
806-
ctx, cancel := context.WithCancel(context.Background())
806+
_, ctx := ktesting.NewTestContext(b)
807+
ctx, cancel := context.WithCancel(ctx)
807808
defer cancel()
808809
nodes := makeNodesWithTopologyZone(tt.NumNodes)
809810
pl := newPluginWithListers(ctx, b, []*v1.Pod{tt.Pod}, nodes, makePVCsWithPV(tt.NumPVC), makePVsWithZoneLabel(tt.NumPV))

0 commit comments

Comments
 (0)