Skip to content

Commit

Permalink
Fix flaky test in maxSurge integration tests
Browse files Browse the repository at this point in the history
Signed-off-by: kerthcet <kerthcet@gmail.com>
  • Loading branch information
kerthcet committed Jul 1, 2024
1 parent a7ef06d commit 20a70e2
Showing 1 changed file with 22 additions and 21 deletions.
43 changes: 22 additions & 21 deletions pkg/scheduler/scheduler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
"time"

"github.com/google/go-cmp/cmp"
"github.com/onsi/gomega"
v1 "k8s.io/api/core/v1"
eventsv1 "k8s.io/api/events/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
Expand All @@ -41,6 +42,9 @@ import (
featuregatetesting "k8s.io/component-base/featuregate/testing"
"k8s.io/klog/v2"
"k8s.io/klog/v2/ktesting"
testingclock "k8s.io/utils/clock/testing"
"k8s.io/utils/ptr"

"k8s.io/kubernetes/pkg/features"
schedulerapi "k8s.io/kubernetes/pkg/scheduler/apis/config"
"k8s.io/kubernetes/pkg/scheduler/apis/config/testing/defaults"
Expand All @@ -54,8 +58,7 @@ import (
"k8s.io/kubernetes/pkg/scheduler/profile"
st "k8s.io/kubernetes/pkg/scheduler/testing"
tf "k8s.io/kubernetes/pkg/scheduler/testing/framework"
testingclock "k8s.io/utils/clock/testing"
"k8s.io/utils/ptr"
utiltesting "k8s.io/kubernetes/test/utils/ktesting"
)

func TestSchedulerCreation(t *testing.T) {
Expand Down Expand Up @@ -994,18 +997,18 @@ func TestFrameworkHandler_IterateOverWaitingPods(t *testing.T) {
fakeClient := fake.NewSimpleClientset(objs...)
informerFactory := informers.NewSharedInformerFactory(fakeClient, 0)
eventBroadcaster := events.NewBroadcaster(&events.EventSinkImpl{Interface: fakeClient.EventsV1()})
defer eventBroadcaster.Shutdown()

eventRecorder := eventBroadcaster.NewRecorder(scheme.Scheme, fakePermit)

outOfTreeRegistry := frameworkruntime.Registry{
fakePermit: newFakePermitPlugin(eventRecorder),
}

_, ctx := ktesting.NewTestContext(t)
ctx, cancel := context.WithCancel(ctx)
defer cancel()
tCtx := utiltesting.Init(t)

scheduler, err := New(
ctx,
tCtx,
fakeClient,
informerFactory,
nil,
Expand Down Expand Up @@ -1034,13 +1037,13 @@ func TestFrameworkHandler_IterateOverWaitingPods(t *testing.T) {
defer stopFn()

// Run scheduler.
informerFactory.Start(ctx.Done())
informerFactory.WaitForCacheSync(ctx.Done())
go scheduler.Run(ctx)
informerFactory.Start(tCtx.Done())
informerFactory.WaitForCacheSync(tCtx.Done())
go scheduler.Run(tCtx)

// Send pods to be scheduled.
for _, p := range tc.waitSchedulingPods {
_, err = fakeClient.CoreV1().Pods("").Create(ctx, p, metav1.CreateOptions{})
_, err = fakeClient.CoreV1().Pods("").Create(tCtx, p, metav1.CreateOptions{})
if err != nil {
t.Fatal(err)
}
Expand All @@ -1049,18 +1052,16 @@ func TestFrameworkHandler_IterateOverWaitingPods(t *testing.T) {
// Wait all pods in waitSchedulingPods to be scheduled.
wg.Wait()

// Ensure that all waitingPods in scheduler can be obtained from any profiles.
for _, fwk := range scheduler.Profiles {
actualPodNamesInWaitingPods := sets.NewString()
fwk.IterateOverWaitingPods(func(pod framework.WaitingPod) {
actualPodNamesInWaitingPods.Insert(pod.GetPod().Name)
})
// Validate the name of pods in waitingPods matches expectations.
if actualPodNamesInWaitingPods.Len() != len(tc.expectPodNamesInWaitingPods) ||
!actualPodNamesInWaitingPods.HasAll(tc.expectPodNamesInWaitingPods...) {
t.Fatalf("Unexpected waitingPods in scheduler profile %s, expect: %#v, got: %#v", fwk.ProfileName(), tc.expectPodNamesInWaitingPods, actualPodNamesInWaitingPods.List())
utiltesting.Eventually(tCtx, func(utiltesting.TContext) sets.Set[string] {
// Ensure that all waitingPods in scheduler can be obtained from any profiles.
actualPodNamesInWaitingPods := sets.New[string]()
for _, fwk := range scheduler.Profiles {
fwk.IterateOverWaitingPods(func(pod framework.WaitingPod) {
actualPodNamesInWaitingPods.Insert(pod.GetPod().Name)
})
}
}
return actualPodNamesInWaitingPods
}).WithTimeout(permitTimeout).Should(gomega.Equal(sets.New(tc.expectPodNamesInWaitingPods...)), "unexpected waitingPods in scheduler profile")
})
}
}
Expand Down

0 comments on commit 20a70e2

Please sign in to comment.