Skip to content

Commit a326cfa

Browse files
authored
Merge pull request kubernetes#125691 from kerthcet/fix/multi-profil
fix flaky integration test about multi profiles
2 parents db9419c + 20a70e2 commit a326cfa

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

pkg/scheduler/scheduler_test.go

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"time"
2727

2828
"github.com/google/go-cmp/cmp"
29+
"github.com/onsi/gomega"
2930
v1 "k8s.io/api/core/v1"
3031
eventsv1 "k8s.io/api/events/v1"
3132
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
@@ -41,6 +42,9 @@ import (
4142
featuregatetesting "k8s.io/component-base/featuregate/testing"
4243
"k8s.io/klog/v2"
4344
"k8s.io/klog/v2/ktesting"
45+
testingclock "k8s.io/utils/clock/testing"
46+
"k8s.io/utils/ptr"
47+
4448
"k8s.io/kubernetes/pkg/features"
4549
schedulerapi "k8s.io/kubernetes/pkg/scheduler/apis/config"
4650
"k8s.io/kubernetes/pkg/scheduler/apis/config/testing/defaults"
@@ -54,8 +58,7 @@ import (
5458
"k8s.io/kubernetes/pkg/scheduler/profile"
5559
st "k8s.io/kubernetes/pkg/scheduler/testing"
5660
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"
5962
)
6063

6164
func TestSchedulerCreation(t *testing.T) {
@@ -994,18 +997,18 @@ func TestFrameworkHandler_IterateOverWaitingPods(t *testing.T) {
994997
fakeClient := fake.NewSimpleClientset(objs...)
995998
informerFactory := informers.NewSharedInformerFactory(fakeClient, 0)
996999
eventBroadcaster := events.NewBroadcaster(&events.EventSinkImpl{Interface: fakeClient.EventsV1()})
1000+
defer eventBroadcaster.Shutdown()
1001+
9971002
eventRecorder := eventBroadcaster.NewRecorder(scheme.Scheme, fakePermit)
9981003

9991004
outOfTreeRegistry := frameworkruntime.Registry{
10001005
fakePermit: newFakePermitPlugin(eventRecorder),
10011006
}
10021007

1003-
_, ctx := ktesting.NewTestContext(t)
1004-
ctx, cancel := context.WithCancel(ctx)
1005-
defer cancel()
1008+
tCtx := utiltesting.Init(t)
10061009

10071010
scheduler, err := New(
1008-
ctx,
1011+
tCtx,
10091012
fakeClient,
10101013
informerFactory,
10111014
nil,
@@ -1034,13 +1037,13 @@ func TestFrameworkHandler_IterateOverWaitingPods(t *testing.T) {
10341037
defer stopFn()
10351038

10361039
// 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)
10401043

10411044
// Send pods to be scheduled.
10421045
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{})
10441047
if err != nil {
10451048
t.Fatal(err)
10461049
}
@@ -1049,18 +1052,16 @@ func TestFrameworkHandler_IterateOverWaitingPods(t *testing.T) {
10491052
// Wait all pods in waitSchedulingPods to be scheduled.
10501053
wg.Wait()
10511054

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+
})
10621062
}
1063-
}
1063+
return actualPodNamesInWaitingPods
1064+
}).WithTimeout(permitTimeout).Should(gomega.Equal(sets.New(tc.expectPodNamesInWaitingPods...)), "unexpected waitingPods in scheduler profile")
10641065
})
10651066
}
10661067
}
@@ -1185,6 +1186,7 @@ func (f fakePermitPlugin) Name() string {
11851186

11861187
const (
11871188
podWaitingReason = "podWaiting"
1189+
permitTimeout = 10 * time.Second
11881190
)
11891191

11901192
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
11931195
f.eventRecorder.Eventf(p, nil, v1.EventTypeWarning, podWaitingReason, "", "")
11941196
}()
11951197

1196-
return framework.NewStatus(framework.Wait), 100 * time.Second
1198+
return framework.NewStatus(framework.Wait), permitTimeout
11971199
}
11981200

11991201
var _ framework.PermitPlugin = &fakePermitPlugin{}

0 commit comments

Comments
 (0)