@@ -26,6 +26,7 @@ import (
26
26
"time"
27
27
28
28
"github.com/google/go-cmp/cmp"
29
+ "github.com/onsi/gomega"
29
30
v1 "k8s.io/api/core/v1"
30
31
eventsv1 "k8s.io/api/events/v1"
31
32
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -41,6 +42,9 @@ import (
41
42
featuregatetesting "k8s.io/component-base/featuregate/testing"
42
43
"k8s.io/klog/v2"
43
44
"k8s.io/klog/v2/ktesting"
45
+ testingclock "k8s.io/utils/clock/testing"
46
+ "k8s.io/utils/ptr"
47
+
44
48
"k8s.io/kubernetes/pkg/features"
45
49
schedulerapi "k8s.io/kubernetes/pkg/scheduler/apis/config"
46
50
"k8s.io/kubernetes/pkg/scheduler/apis/config/testing/defaults"
@@ -54,8 +58,7 @@ import (
54
58
"k8s.io/kubernetes/pkg/scheduler/profile"
55
59
st "k8s.io/kubernetes/pkg/scheduler/testing"
56
60
tf "k8s.io/kubernetes/pkg/scheduler/testing/framework"
57
- testingclock "k8s.io/utils/clock/testing"
58
- "k8s.io/utils/ptr"
61
+ utiltesting "k8s.io/kubernetes/test/utils/ktesting"
59
62
)
60
63
61
64
func TestSchedulerCreation (t * testing.T ) {
@@ -994,18 +997,18 @@ func TestFrameworkHandler_IterateOverWaitingPods(t *testing.T) {
994
997
fakeClient := fake .NewSimpleClientset (objs ... )
995
998
informerFactory := informers .NewSharedInformerFactory (fakeClient , 0 )
996
999
eventBroadcaster := events .NewBroadcaster (& events.EventSinkImpl {Interface : fakeClient .EventsV1 ()})
1000
+ defer eventBroadcaster .Shutdown ()
1001
+
997
1002
eventRecorder := eventBroadcaster .NewRecorder (scheme .Scheme , fakePermit )
998
1003
999
1004
outOfTreeRegistry := frameworkruntime.Registry {
1000
1005
fakePermit : newFakePermitPlugin (eventRecorder ),
1001
1006
}
1002
1007
1003
- _ , ctx := ktesting .NewTestContext (t )
1004
- ctx , cancel := context .WithCancel (ctx )
1005
- defer cancel ()
1008
+ tCtx := utiltesting .Init (t )
1006
1009
1007
1010
scheduler , err := New (
1008
- ctx ,
1011
+ tCtx ,
1009
1012
fakeClient ,
1010
1013
informerFactory ,
1011
1014
nil ,
@@ -1034,13 +1037,13 @@ func TestFrameworkHandler_IterateOverWaitingPods(t *testing.T) {
1034
1037
defer stopFn ()
1035
1038
1036
1039
// Run scheduler.
1037
- informerFactory .Start (ctx .Done ())
1038
- informerFactory .WaitForCacheSync (ctx .Done ())
1039
- go scheduler .Run (ctx )
1040
+ informerFactory .Start (tCtx .Done ())
1041
+ informerFactory .WaitForCacheSync (tCtx .Done ())
1042
+ go scheduler .Run (tCtx )
1040
1043
1041
1044
// Send pods to be scheduled.
1042
1045
for _ , p := range tc .waitSchedulingPods {
1043
- _ , err = fakeClient .CoreV1 ().Pods ("" ).Create (ctx , p , metav1.CreateOptions {})
1046
+ _ , err = fakeClient .CoreV1 ().Pods ("" ).Create (tCtx , p , metav1.CreateOptions {})
1044
1047
if err != nil {
1045
1048
t .Fatal (err )
1046
1049
}
@@ -1049,18 +1052,16 @@ func TestFrameworkHandler_IterateOverWaitingPods(t *testing.T) {
1049
1052
// Wait all pods in waitSchedulingPods to be scheduled.
1050
1053
wg .Wait ()
1051
1054
1052
- // Ensure that all waitingPods in scheduler can be obtained from any profiles.
1053
- for _ , fwk := range scheduler .Profiles {
1054
- actualPodNamesInWaitingPods := sets .NewString ()
1055
- fwk .IterateOverWaitingPods (func (pod framework.WaitingPod ) {
1056
- actualPodNamesInWaitingPods .Insert (pod .GetPod ().Name )
1057
- })
1058
- // Validate the name of pods in waitingPods matches expectations.
1059
- if actualPodNamesInWaitingPods .Len () != len (tc .expectPodNamesInWaitingPods ) ||
1060
- ! actualPodNamesInWaitingPods .HasAll (tc .expectPodNamesInWaitingPods ... ) {
1061
- t .Fatalf ("Unexpected waitingPods in scheduler profile %s, expect: %#v, got: %#v" , fwk .ProfileName (), tc .expectPodNamesInWaitingPods , actualPodNamesInWaitingPods .List ())
1055
+ utiltesting .Eventually (tCtx , func (utiltesting.TContext ) sets.Set [string ] {
1056
+ // Ensure that all waitingPods in scheduler can be obtained from any profiles.
1057
+ actualPodNamesInWaitingPods := sets .New [string ]()
1058
+ for _ , fwk := range scheduler .Profiles {
1059
+ fwk .IterateOverWaitingPods (func (pod framework.WaitingPod ) {
1060
+ actualPodNamesInWaitingPods .Insert (pod .GetPod ().Name )
1061
+ })
1062
1062
}
1063
- }
1063
+ return actualPodNamesInWaitingPods
1064
+ }).WithTimeout (permitTimeout ).Should (gomega .Equal (sets .New (tc .expectPodNamesInWaitingPods ... )), "unexpected waitingPods in scheduler profile" )
1064
1065
})
1065
1066
}
1066
1067
}
@@ -1185,6 +1186,7 @@ func (f fakePermitPlugin) Name() string {
1185
1186
1186
1187
const (
1187
1188
podWaitingReason = "podWaiting"
1189
+ permitTimeout = 10 * time .Second
1188
1190
)
1189
1191
1190
1192
func (f fakePermitPlugin ) Permit (ctx context.Context , state * framework.CycleState , p * v1.Pod , nodeName string ) (* framework.Status , time.Duration ) {
@@ -1193,7 +1195,7 @@ func (f fakePermitPlugin) Permit(ctx context.Context, state *framework.CycleStat
1193
1195
f .eventRecorder .Eventf (p , nil , v1 .EventTypeWarning , podWaitingReason , "" , "" )
1194
1196
}()
1195
1197
1196
- return framework .NewStatus (framework .Wait ), 100 * time . Second
1198
+ return framework .NewStatus (framework .Wait ), permitTimeout
1197
1199
}
1198
1200
1199
1201
var _ framework.PermitPlugin = & fakePermitPlugin {}
0 commit comments