Skip to content

Commit 5589459

Browse files
committed
refactor integ test
1 parent 6c6be93 commit 5589459

File tree

1 file changed

+54
-83
lines changed

1 file changed

+54
-83
lines changed

test/integration/scheduler/queue_test.go

Lines changed: 54 additions & 83 deletions
Original file line numberDiff line numberDiff line change
@@ -57,27 +57,27 @@ import (
5757

5858
func TestSchedulingGates(t *testing.T) {
5959
tests := []struct {
60-
name string
61-
pods []*v1.Pod
62-
want []string
63-
rmPodsSchedulingGates []int
64-
wantPostGatesRemoval []string
60+
name string
61+
pods []*v1.Pod
62+
schedule []string
63+
delete []string
64+
rmGates []string
6565
}{
6666
{
6767
name: "regular pods",
6868
pods: []*v1.Pod{
6969
st.MakePod().Name("p1").Container("pause").Obj(),
7070
st.MakePod().Name("p2").Container("pause").Obj(),
7171
},
72-
want: []string{"p1", "p2"},
72+
schedule: []string{"p1", "p2"},
7373
},
7474
{
7575
name: "one pod carrying scheduling gates",
7676
pods: []*v1.Pod{
7777
st.MakePod().Name("p1").SchedulingGates([]string{"foo"}).Container("pause").Obj(),
7878
st.MakePod().Name("p2").Container("pause").Obj(),
7979
},
80-
want: []string{"p2"},
80+
schedule: []string{"p2"},
8181
},
8282
{
8383
name: "two pod carrying scheduling gates, and remove gates of one pod",
@@ -86,9 +86,18 @@ func TestSchedulingGates(t *testing.T) {
8686
st.MakePod().Name("p2").SchedulingGates([]string{"bar"}).Container("pause").Obj(),
8787
st.MakePod().Name("p3").Container("pause").Obj(),
8888
},
89-
want: []string{"p3"},
90-
rmPodsSchedulingGates: []int{1}, // remove gates of 'p2'
91-
wantPostGatesRemoval: []string{"p2"},
89+
schedule: []string{"p3"},
90+
rmGates: []string{"p2"},
91+
},
92+
{
93+
name: "gated pod schedulable after deleting the scheduled pod and removing gate",
94+
pods: []*v1.Pod{
95+
st.MakePod().Name("p1").SchedulingGates([]string{"foo"}).Container("pause").Obj(),
96+
st.MakePod().Name("p2").Container("pause").Obj(),
97+
},
98+
schedule: []string{"p2"},
99+
delete: []string{"p2"},
100+
rmGates: []string{"p1"},
92101
},
93102
}
94103

@@ -107,6 +116,15 @@ func TestSchedulingGates(t *testing.T) {
107116
testutils.SyncSchedulerInformerFactory(testCtx)
108117

109118
cs, ns, ctx := testCtx.ClientSet, testCtx.NS.Name, testCtx.Ctx
119+
120+
// Create node, so we can schedule pods.
121+
node := st.MakeNode().Name("node").Obj()
122+
if _, err := cs.CoreV1().Nodes().Create(ctx, node, metav1.CreateOptions{}); err != nil {
123+
t.Fatal("Failed to create node")
124+
125+
}
126+
127+
// Create pods.
110128
for _, p := range tt.pods {
111129
p.Namespace = ns
112130
if _, err := cs.CoreV1().Pods(ns).Create(ctx, p, metav1.CreateOptions{}); err != nil {
@@ -122,95 +140,48 @@ func TestSchedulingGates(t *testing.T) {
122140
t.Fatal(err)
123141
}
124142

125-
// Pop the expected pods out. They should be de-queueable.
126-
for _, wantPod := range tt.want {
127-
podInfo := testutils.NextPodOrDie(t, testCtx)
128-
if got := podInfo.Pod.Name; got != wantPod {
129-
t.Errorf("Want %v to be popped out, but got %v", wantPod, got)
143+
// Schedule pods.
144+
for _, podName := range tt.schedule {
145+
testCtx.Scheduler.ScheduleOne(testCtx.Ctx)
146+
if err := wait.PollUntilContextTimeout(ctx, time.Millisecond*200, wait.ForeverTestTimeout, false, testutils.PodScheduled(cs, ns, podName)); err != nil {
147+
t.Fatalf("Failed to schedule %s", podName)
130148
}
131149
}
132150

133-
if len(tt.rmPodsSchedulingGates) == 0 {
134-
return
151+
// Delete pods, which triggers AssignedPodDelete event in the scheduling queue.
152+
for _, podName := range tt.delete {
153+
if err := cs.CoreV1().Pods(ns).Delete(ctx, podName, metav1.DeleteOptions{}); err != nil {
154+
t.Fatalf("Error calling Delete on %s", podName)
155+
}
156+
if err := wait.PollUntilContextTimeout(ctx, time.Millisecond*200, wait.ForeverTestTimeout, false, testutils.PodDeleted(ctx, cs, ns, podName)); err != nil {
157+
t.Fatalf("Failed to delete %s", podName)
158+
}
159+
}
160+
161+
// Ensure gated pods are not in ActiveQ
162+
if len(testCtx.Scheduler.SchedulingQueue.PodsInActiveQ()) > 0 {
163+
t.Fatal("Expected no schedulable pods")
135164
}
165+
136166
// Remove scheduling gates from the pod spec.
137-
for _, idx := range tt.rmPodsSchedulingGates {
167+
for _, podName := range tt.rmGates {
138168
patch := `{"spec": {"schedulingGates": null}}`
139-
podName := tt.pods[idx].Name
140169
if _, err := cs.CoreV1().Pods(ns).Patch(ctx, podName, types.StrategicMergePatchType, []byte(patch), metav1.PatchOptions{}); err != nil {
141170
t.Fatalf("Failed to patch pod %v: %v", podName, err)
142171
}
143172
}
144-
// Pop the expected pods out. They should be de-queueable.
145-
for _, wantPod := range tt.wantPostGatesRemoval {
146-
podInfo := testutils.NextPodOrDie(t, testCtx)
147-
if got := podInfo.Pod.Name; got != wantPod {
148-
t.Errorf("Want %v to be popped out, but got %v", wantPod, got)
173+
174+
// Schedule pods which no longer have gates.
175+
for _, podName := range tt.rmGates {
176+
testCtx.Scheduler.ScheduleOne(testCtx.Ctx)
177+
if err := wait.PollUntilContextTimeout(ctx, time.Millisecond*200, wait.ForeverTestTimeout, false, testutils.PodScheduled(cs, ns, podName)); err != nil {
178+
t.Fatalf("Failed to schedule %s", podName)
149179
}
150180
}
151181
})
152182
}
153183
}
154184

155-
// We create a gated and non-gated pod. After scheduling and deleting the
156-
// non-gated pod, we ensure that the gated pod is schedulable after the removal
157-
// of its gate.
158-
func TestGatedPodSchedulableAfterEvent(t *testing.T) {
159-
testCtx := testutils.InitTestSchedulerWithOptions(
160-
t,
161-
testutils.InitTestAPIServer(t, "pod-scheduling-gates", nil),
162-
0,
163-
scheduler.WithPodInitialBackoffSeconds(0),
164-
scheduler.WithPodMaxBackoffSeconds(0),
165-
)
166-
testutils.SyncSchedulerInformerFactory(testCtx)
167-
cs, ns, ctx := testCtx.ClientSet, testCtx.NS.Name, testCtx.Ctx
168-
169-
// create initially gated pod
170-
pod1 := st.MakePod().Namespace(ns).Name("p1").Container("pause").SchedulingGates([]string{"foo"}).Obj()
171-
if _, err := cs.CoreV1().Pods(ns).Create(ctx, pod1, metav1.CreateOptions{}); err != nil {
172-
t.Fatal("Failed to create p1")
173-
}
174-
175-
// create immediately schedulable pod
176-
pod2 := st.MakePod().Namespace(ns).Name("p2").Container("pause").Obj()
177-
if _, err := cs.CoreV1().Pods(ns).Create(ctx, pod2, metav1.CreateOptions{}); err != nil {
178-
t.Fatal("Failed to create p2")
179-
}
180-
181-
// create node on which to schedule pods
182-
node := st.MakeNode().Name("node1").Obj()
183-
if _, err := cs.CoreV1().Nodes().Create(ctx, node, metav1.CreateOptions{}); err != nil {
184-
t.Fatal("Failed to create node1")
185-
}
186-
187-
// schedule p2
188-
testCtx.Scheduler.ScheduleOne(testCtx.Ctx)
189-
if err := wait.PollUntilContextTimeout(ctx, 200*time.Millisecond, wait.ForeverTestTimeout, false, testutils.PodScheduled(cs, ns, "p2")); err != nil {
190-
t.Fatal("Failed to schedule p2")
191-
}
192-
193-
// delete p2, which triggers DeletePodFromCache event
194-
if err := cs.CoreV1().Pods(ns).Delete(ctx, "p2", metav1.DeleteOptions{}); err != nil {
195-
t.Fatal("Error calling Delete on p2")
196-
}
197-
if err := wait.PollUntilContextTimeout(ctx, 200*time.Millisecond, wait.ForeverTestTimeout, false, testutils.PodDeleted(ctx, cs, ns, "p2")); err != nil {
198-
t.Fatal("Failed to delete p2")
199-
}
200-
201-
// remove gate from p1
202-
patch := `{"spec": {"schedulingGates": null}}`
203-
if _, err := cs.CoreV1().Pods(ns).Patch(ctx, "p1", types.StrategicMergePatchType, []byte(patch), metav1.PatchOptions{}); err != nil {
204-
t.Fatal("Failed to remove schedulingGates from p1")
205-
}
206-
207-
// schedule p1
208-
testCtx.Scheduler.ScheduleOne(testCtx.Ctx)
209-
if err := wait.PollUntilContextTimeout(ctx, 200*time.Millisecond, wait.ForeverTestTimeout, false, testutils.PodScheduled(cs, ns, "p1")); err != nil {
210-
t.Fatal("Failed to schedule p1")
211-
}
212-
}
213-
214185
// TestCoreResourceEnqueue verify Pods failed by in-tree default plugins can be
215186
// moved properly upon their registered events.
216187
func TestCoreResourceEnqueue(t *testing.T) {

0 commit comments

Comments
 (0)