Skip to content

Commit fc30f88

Browse files
authored
Merge pull request kubernetes#91849 from knabben/scheduler-integration-pre
Scheduler integration tests standarization
2 parents c5894ca + 5e679cf commit fc30f88

File tree

3 files changed

+353
-342
lines changed

3 files changed

+353
-342
lines changed

test/integration/scheduler/predicates_test.go

Lines changed: 68 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,15 @@ func TestInterPodAffinity(t *testing.T) {
6767
podLabel2 := map[string]string{"security": "S1"}
6868

6969
tests := []struct {
70+
name string
7071
pod *v1.Pod
7172
pods []*v1.Pod
7273
node *v1.Node
7374
fits bool
7475
errorType string
75-
test string
7676
}{
7777
{
78+
name: "validates that a pod with an invalid podAffinity is rejected because of the LabelSelectorRequirement is invalid",
7879
pod: &v1.Pod{
7980
ObjectMeta: metav1.ObjectMeta{
8081
Name: "fakename",
@@ -104,9 +105,9 @@ func TestInterPodAffinity(t *testing.T) {
104105
node: nodes[0],
105106
fits: false,
106107
errorType: "invalidPod",
107-
test: "validates that a pod with an invalid podAffinity is rejected because of the LabelSelectorRequirement is invalid",
108108
},
109109
{
110+
name: "validates that Inter-pod-Affinity is respected if not matching",
110111
pod: &v1.Pod{
111112
ObjectMeta: metav1.ObjectMeta{
112113
Name: "fakename",
@@ -136,9 +137,9 @@ func TestInterPodAffinity(t *testing.T) {
136137
},
137138
node: nodes[0],
138139
fits: false,
139-
test: "validates that Inter-pod-Affinity is respected if not matching",
140140
},
141141
{
142+
name: "validates that InterPodAffinity is respected if matching. requiredDuringSchedulingIgnoredDuringExecution in PodAffinity using In operator that matches the existing pod",
142143
pod: &v1.Pod{
143144
ObjectMeta: metav1.ObjectMeta{
144145
Name: "fakename",
@@ -179,9 +180,9 @@ func TestInterPodAffinity(t *testing.T) {
179180
},
180181
node: nodes[0],
181182
fits: true,
182-
test: "validates that InterPodAffinity is respected if matching. requiredDuringSchedulingIgnoredDuringExecution in PodAffinity using In operator that matches the existing pod",
183183
},
184184
{
185+
name: "validates that InterPodAffinity is respected if matching. requiredDuringSchedulingIgnoredDuringExecution in PodAffinity using not in operator in labelSelector that matches the existing pod",
185186
pod: &v1.Pod{
186187
ObjectMeta: metav1.ObjectMeta{
187188
Name: "fakename",
@@ -217,9 +218,9 @@ func TestInterPodAffinity(t *testing.T) {
217218
Labels: podLabel}}},
218219
node: nodes[0],
219220
fits: true,
220-
test: "validates that InterPodAffinity is respected if matching. requiredDuringSchedulingIgnoredDuringExecution in PodAffinity using not in operator in labelSelector that matches the existing pod",
221221
},
222222
{
223+
name: "validates that inter-pod-affinity is respected when pods have different Namespaces",
223224
pod: &v1.Pod{
224225
ObjectMeta: metav1.ObjectMeta{
225226
Name: "fakename",
@@ -256,9 +257,9 @@ func TestInterPodAffinity(t *testing.T) {
256257
Labels: podLabel, Namespace: "ns"}}},
257258
node: nodes[0],
258259
fits: false,
259-
test: "validates that inter-pod-affinity is respected when pods have different Namespaces",
260260
},
261261
{
262+
name: "Doesn't satisfy the PodAffinity because of unmatching labelSelector with the existing pod",
262263
pod: &v1.Pod{
263264
ObjectMeta: metav1.ObjectMeta{
264265
Name: "fakename",
@@ -293,9 +294,9 @@ func TestInterPodAffinity(t *testing.T) {
293294
Labels: podLabel}}},
294295
node: nodes[0],
295296
fits: false,
296-
test: "Doesn't satisfy the PodAffinity because of unmatching labelSelector with the existing pod",
297297
},
298298
{
299+
name: "validates that InterPodAffinity is respected if matching with multiple affinities in multiple RequiredDuringSchedulingIgnoredDuringExecution ",
299300
pod: &v1.Pod{
300301
ObjectMeta: metav1.ObjectMeta{
301302
Name: "fakename",
@@ -347,9 +348,9 @@ func TestInterPodAffinity(t *testing.T) {
347348
Labels: podLabel}}},
348349
node: nodes[0],
349350
fits: true,
350-
test: "validates that InterPodAffinity is respected if matching with multiple affinities in multiple RequiredDuringSchedulingIgnoredDuringExecution ",
351351
},
352352
{
353+
name: "The labelSelector requirements(items of matchExpressions) are ANDed, the pod cannot schedule onto the node because one of the matchExpression items doesn't match.",
353354
pod: &v1.Pod{
354355
ObjectMeta: metav1.ObjectMeta{
355356
Labels: podLabel2,
@@ -401,9 +402,9 @@ func TestInterPodAffinity(t *testing.T) {
401402
Labels: podLabel}}},
402403
node: nodes[0],
403404
fits: false,
404-
test: "The labelSelector requirements(items of matchExpressions) are ANDed, the pod cannot schedule onto the node because one of the matchExpression items doesn't match.",
405405
},
406406
{
407+
name: "validates that InterPod Affinity and AntiAffinity is respected if matching",
407408
pod: &v1.Pod{
408409
ObjectMeta: metav1.ObjectMeta{
409410
Name: "fakename",
@@ -454,9 +455,9 @@ func TestInterPodAffinity(t *testing.T) {
454455
Labels: podLabel}}},
455456
node: nodes[0],
456457
fits: true,
457-
test: "validates that InterPod Affinity and AntiAffinity is respected if matching",
458458
},
459459
{
460+
name: "satisfies the PodAffinity and PodAntiAffinity and PodAntiAffinity symmetry with the existing pod",
460461
pod: &v1.Pod{
461462
ObjectMeta: metav1.ObjectMeta{
462463
Name: "fakename",
@@ -531,9 +532,9 @@ func TestInterPodAffinity(t *testing.T) {
531532
},
532533
node: nodes[0],
533534
fits: true,
534-
test: "satisfies the PodAffinity and PodAntiAffinity and PodAntiAffinity symmetry with the existing pod",
535535
},
536536
{
537+
name: "satisfies the PodAffinity but doesn't satisfies the PodAntiAffinity with the existing pod",
537538
pod: &v1.Pod{
538539
ObjectMeta: metav1.ObjectMeta{
539540
Name: "fakename",
@@ -584,9 +585,9 @@ func TestInterPodAffinity(t *testing.T) {
584585
Labels: podLabel}}},
585586
node: nodes[0],
586587
fits: false,
587-
test: "satisfies the PodAffinity but doesn't satisfies the PodAntiAffinity with the existing pod",
588588
},
589589
{
590+
name: "satisfies the PodAffinity and PodAntiAffinity but doesn't satisfies PodAntiAffinity symmetry with the existing pod",
590591
pod: &v1.Pod{
591592
ObjectMeta: metav1.ObjectMeta{
592593
Name: "fakename",
@@ -661,9 +662,9 @@ func TestInterPodAffinity(t *testing.T) {
661662
},
662663
node: nodes[0],
663664
fits: false,
664-
test: "satisfies the PodAffinity and PodAntiAffinity but doesn't satisfies PodAntiAffinity symmetry with the existing pod",
665665
},
666666
{
667+
name: "pod matches its own Label in PodAffinity and that matches the existing pod Labels",
667668
pod: &v1.Pod{
668669
ObjectMeta: metav1.ObjectMeta{
669670
Name: "fakename",
@@ -698,9 +699,9 @@ func TestInterPodAffinity(t *testing.T) {
698699
Labels: podLabel}}},
699700
node: nodes[0],
700701
fits: false,
701-
test: "pod matches its own Label in PodAffinity and that matches the existing pod Labels",
702702
},
703703
{
704+
name: "Verify that PodAntiAffinity of an existing pod is respected when PodAntiAffinity symmetry is not satisfied with the existing pod",
704705
pod: &v1.Pod{
705706
ObjectMeta: metav1.ObjectMeta{
706707
Name: "fakename",
@@ -738,9 +739,9 @@ func TestInterPodAffinity(t *testing.T) {
738739
},
739740
node: nodes[0],
740741
fits: false,
741-
test: "Verify that PodAntiAffinity of an existing pod is respected when PodAntiAffinity symmetry is not satisfied with the existing pod",
742742
},
743743
{
744+
name: "Verify that PodAntiAffinity from existing pod is respected when pod statisfies PodAntiAffinity symmetry with the existing pod",
744745
pod: &v1.Pod{
745746
ObjectMeta: metav1.ObjectMeta{
746747
Name: "fake-name",
@@ -778,9 +779,9 @@ func TestInterPodAffinity(t *testing.T) {
778779
},
779780
node: nodes[0],
780781
fits: true,
781-
test: "Verify that PodAntiAffinity from existing pod is respected when pod statisfies PodAntiAffinity symmetry with the existing pod",
782782
},
783783
{
784+
name: "nodes[0] and nodes[1] have same topologyKey and label value. nodes[0] has an existing pod that matches the inter pod affinity rule. The new pod can not be scheduled onto either of the two nodes.",
784785
pod: &v1.Pod{
785786
ObjectMeta: metav1.ObjectMeta{Name: "fake-name2"},
786787
Spec: v1.PodSpec{
@@ -812,67 +813,68 @@ func TestInterPodAffinity(t *testing.T) {
812813
NodeName: nodes[0].Name}, ObjectMeta: metav1.ObjectMeta{Name: "fakename", Labels: map[string]string{"foo": "abc"}}},
813814
},
814815
fits: false,
815-
test: "nodes[0] and nodes[1] have same topologyKey and label value. nodes[0] has an existing pod that matches the inter pod affinity rule. The new pod can not be scheduled onto either of the two nodes.",
816816
},
817817
}
818818

819819
for _, test := range tests {
820-
for _, pod := range test.pods {
821-
var nsName string
822-
if pod.Namespace != "" {
823-
nsName = pod.Namespace
824-
} else {
825-
nsName = testCtx.NS.Name
826-
}
827-
createdPod, err := cs.CoreV1().Pods(nsName).Create(context.TODO(), pod, metav1.CreateOptions{})
828-
if err != nil {
829-
t.Fatalf("Test Failed: error, %v, while creating pod during test: %v", err, test.test)
820+
t.Run(test.name, func(t *testing.T) {
821+
for _, pod := range test.pods {
822+
var nsName string
823+
if pod.Namespace != "" {
824+
nsName = pod.Namespace
825+
} else {
826+
nsName = testCtx.NS.Name
827+
}
828+
createdPod, err := cs.CoreV1().Pods(nsName).Create(context.TODO(), pod, metav1.CreateOptions{})
829+
if err != nil {
830+
t.Fatalf("Error while creating pod: %v", err)
831+
}
832+
err = wait.Poll(pollInterval, wait.ForeverTestTimeout, testutils.PodScheduled(cs, createdPod.Namespace, createdPod.Name))
833+
if err != nil {
834+
t.Errorf("Error while creating pod: %v", err)
835+
}
830836
}
831-
err = wait.Poll(pollInterval, wait.ForeverTestTimeout, testutils.PodScheduled(cs, createdPod.Namespace, createdPod.Name))
837+
testPod, err := cs.CoreV1().Pods(testCtx.NS.Name).Create(context.TODO(), test.pod, metav1.CreateOptions{})
832838
if err != nil {
833-
t.Errorf("Test Failed: error, %v, while waiting for pod during test, %v", err, test)
834-
}
835-
}
836-
testPod, err := cs.CoreV1().Pods(testCtx.NS.Name).Create(context.TODO(), test.pod, metav1.CreateOptions{})
837-
if err != nil {
838-
if !(test.errorType == "invalidPod" && apierrors.IsInvalid(err)) {
839-
t.Fatalf("Test Failed: error, %v, while creating pod during test: %v", err, test.test)
839+
if !(test.errorType == "invalidPod" && apierrors.IsInvalid(err)) {
840+
t.Fatalf("Error while creating pod: %v", err)
841+
}
840842
}
841-
}
842-
843-
if test.fits {
844-
err = wait.Poll(pollInterval, wait.ForeverTestTimeout, testutils.PodScheduled(cs, testPod.Namespace, testPod.Name))
845-
} else {
846-
err = wait.Poll(pollInterval, wait.ForeverTestTimeout, podUnschedulable(cs, testPod.Namespace, testPod.Name))
847-
}
848-
if err != nil {
849-
t.Errorf("Test Failed: %v, err %v, test.fits %v", test.test, err, test.fits)
850-
}
851843

852-
err = cs.CoreV1().Pods(testCtx.NS.Name).Delete(context.TODO(), test.pod.Name, *metav1.NewDeleteOptions(0))
853-
if err != nil {
854-
t.Errorf("Test Failed: error, %v, while deleting pod during test: %v", err, test.test)
855-
}
856-
err = wait.Poll(pollInterval, wait.ForeverTestTimeout, testutils.PodDeleted(cs, testCtx.NS.Name, test.pod.Name))
857-
if err != nil {
858-
t.Errorf("Test Failed: error, %v, while waiting for pod to get deleted, %v", err, test.test)
859-
}
860-
for _, pod := range test.pods {
861-
var nsName string
862-
if pod.Namespace != "" {
863-
nsName = pod.Namespace
844+
if test.fits {
845+
err = wait.Poll(pollInterval, wait.ForeverTestTimeout, testutils.PodScheduled(cs, testPod.Namespace, testPod.Name))
864846
} else {
865-
nsName = testCtx.NS.Name
847+
err = wait.Poll(pollInterval, wait.ForeverTestTimeout, podUnschedulable(cs, testPod.Namespace, testPod.Name))
866848
}
867-
err = cs.CoreV1().Pods(nsName).Delete(context.TODO(), pod.Name, *metav1.NewDeleteOptions(0))
868849
if err != nil {
869-
t.Errorf("Test Failed: error, %v, while deleting pod during test: %v", err, test.test)
850+
t.Errorf("Error while trying to fit a pod: %v", err)
870851
}
871-
err = wait.Poll(pollInterval, wait.ForeverTestTimeout, testutils.PodDeleted(cs, nsName, pod.Name))
852+
853+
err = cs.CoreV1().Pods(testCtx.NS.Name).Delete(context.TODO(), test.pod.Name, *metav1.NewDeleteOptions(0))
872854
if err != nil {
873-
t.Errorf("Test Failed: error, %v, while waiting for pod to get deleted, %v", err, test.test)
855+
t.Errorf("Error while deleting pod: %v", err)
874856
}
875-
}
857+
err = wait.Poll(pollInterval, wait.ForeverTestTimeout, testutils.PodDeleted(cs, testCtx.NS.Name, test.pod.Name))
858+
if err != nil {
859+
t.Errorf("Error while waiting for pod to get deleted: %v", err)
860+
}
861+
for _, pod := range test.pods {
862+
var nsName string
863+
if pod.Namespace != "" {
864+
nsName = pod.Namespace
865+
} else {
866+
nsName = testCtx.NS.Name
867+
}
868+
err = cs.CoreV1().Pods(nsName).Delete(context.TODO(), pod.Name, *metav1.NewDeleteOptions(0))
869+
if err != nil {
870+
t.Errorf("Error while deleting pod: %v", err)
871+
}
872+
err = wait.Poll(pollInterval, wait.ForeverTestTimeout, testutils.PodDeleted(cs, nsName, pod.Name))
873+
if err != nil {
874+
t.Errorf("Error while waiting for pod to get deleted: %v", err)
875+
}
876+
}
877+
})
876878
}
877879
}
878880

@@ -1005,16 +1007,16 @@ func TestEvenPodsSpreadPredicate(t *testing.T) {
10051007
for _, pod := range tt.existingPods {
10061008
createdPod, err := cs.CoreV1().Pods(pod.Namespace).Create(context.TODO(), pod, metav1.CreateOptions{})
10071009
if err != nil {
1008-
t.Fatalf("Test Failed: error while creating pod during test: %v", err)
1010+
t.Fatalf("Error while creating pod during test: %v", err)
10091011
}
10101012
err = wait.Poll(pollInterval, wait.ForeverTestTimeout, testutils.PodScheduled(cs, createdPod.Namespace, createdPod.Name))
10111013
if err != nil {
1012-
t.Errorf("Test Failed: error while waiting for pod during test: %v", err)
1014+
t.Errorf("Error while waiting for pod during test: %v", err)
10131015
}
10141016
}
10151017
testPod, err := cs.CoreV1().Pods(tt.incomingPod.Namespace).Create(context.TODO(), tt.incomingPod, metav1.CreateOptions{})
10161018
if err != nil && !apierrors.IsInvalid(err) {
1017-
t.Fatalf("Test Failed: error while creating pod during test: %v", err)
1019+
t.Fatalf("Error while creating pod during test: %v", err)
10181020
}
10191021

10201022
if tt.fits {

0 commit comments

Comments
 (0)