@@ -67,14 +67,15 @@ func TestInterPodAffinity(t *testing.T) {
67
67
podLabel2 := map [string ]string {"security" : "S1" }
68
68
69
69
tests := []struct {
70
+ name string
70
71
pod * v1.Pod
71
72
pods []* v1.Pod
72
73
node * v1.Node
73
74
fits bool
74
75
errorType string
75
- test string
76
76
}{
77
77
{
78
+ name : "validates that a pod with an invalid podAffinity is rejected because of the LabelSelectorRequirement is invalid" ,
78
79
pod : & v1.Pod {
79
80
ObjectMeta : metav1.ObjectMeta {
80
81
Name : "fakename" ,
@@ -104,9 +105,9 @@ func TestInterPodAffinity(t *testing.T) {
104
105
node : nodes [0 ],
105
106
fits : false ,
106
107
errorType : "invalidPod" ,
107
- test : "validates that a pod with an invalid podAffinity is rejected because of the LabelSelectorRequirement is invalid" ,
108
108
},
109
109
{
110
+ name : "validates that Inter-pod-Affinity is respected if not matching" ,
110
111
pod : & v1.Pod {
111
112
ObjectMeta : metav1.ObjectMeta {
112
113
Name : "fakename" ,
@@ -136,9 +137,9 @@ func TestInterPodAffinity(t *testing.T) {
136
137
},
137
138
node : nodes [0 ],
138
139
fits : false ,
139
- test : "validates that Inter-pod-Affinity is respected if not matching" ,
140
140
},
141
141
{
142
+ name : "validates that InterPodAffinity is respected if matching. requiredDuringSchedulingIgnoredDuringExecution in PodAffinity using In operator that matches the existing pod" ,
142
143
pod : & v1.Pod {
143
144
ObjectMeta : metav1.ObjectMeta {
144
145
Name : "fakename" ,
@@ -179,9 +180,9 @@ func TestInterPodAffinity(t *testing.T) {
179
180
},
180
181
node : nodes [0 ],
181
182
fits : true ,
182
- test : "validates that InterPodAffinity is respected if matching. requiredDuringSchedulingIgnoredDuringExecution in PodAffinity using In operator that matches the existing pod" ,
183
183
},
184
184
{
185
+ name : "validates that InterPodAffinity is respected if matching. requiredDuringSchedulingIgnoredDuringExecution in PodAffinity using not in operator in labelSelector that matches the existing pod" ,
185
186
pod : & v1.Pod {
186
187
ObjectMeta : metav1.ObjectMeta {
187
188
Name : "fakename" ,
@@ -217,9 +218,9 @@ func TestInterPodAffinity(t *testing.T) {
217
218
Labels : podLabel }}},
218
219
node : nodes [0 ],
219
220
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" ,
221
221
},
222
222
{
223
+ name : "validates that inter-pod-affinity is respected when pods have different Namespaces" ,
223
224
pod : & v1.Pod {
224
225
ObjectMeta : metav1.ObjectMeta {
225
226
Name : "fakename" ,
@@ -256,9 +257,9 @@ func TestInterPodAffinity(t *testing.T) {
256
257
Labels : podLabel , Namespace : "ns" }}},
257
258
node : nodes [0 ],
258
259
fits : false ,
259
- test : "validates that inter-pod-affinity is respected when pods have different Namespaces" ,
260
260
},
261
261
{
262
+ name : "Doesn't satisfy the PodAffinity because of unmatching labelSelector with the existing pod" ,
262
263
pod : & v1.Pod {
263
264
ObjectMeta : metav1.ObjectMeta {
264
265
Name : "fakename" ,
@@ -293,9 +294,9 @@ func TestInterPodAffinity(t *testing.T) {
293
294
Labels : podLabel }}},
294
295
node : nodes [0 ],
295
296
fits : false ,
296
- test : "Doesn't satisfy the PodAffinity because of unmatching labelSelector with the existing pod" ,
297
297
},
298
298
{
299
+ name : "validates that InterPodAffinity is respected if matching with multiple affinities in multiple RequiredDuringSchedulingIgnoredDuringExecution " ,
299
300
pod : & v1.Pod {
300
301
ObjectMeta : metav1.ObjectMeta {
301
302
Name : "fakename" ,
@@ -347,9 +348,9 @@ func TestInterPodAffinity(t *testing.T) {
347
348
Labels : podLabel }}},
348
349
node : nodes [0 ],
349
350
fits : true ,
350
- test : "validates that InterPodAffinity is respected if matching with multiple affinities in multiple RequiredDuringSchedulingIgnoredDuringExecution " ,
351
351
},
352
352
{
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." ,
353
354
pod : & v1.Pod {
354
355
ObjectMeta : metav1.ObjectMeta {
355
356
Labels : podLabel2 ,
@@ -401,9 +402,9 @@ func TestInterPodAffinity(t *testing.T) {
401
402
Labels : podLabel }}},
402
403
node : nodes [0 ],
403
404
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." ,
405
405
},
406
406
{
407
+ name : "validates that InterPod Affinity and AntiAffinity is respected if matching" ,
407
408
pod : & v1.Pod {
408
409
ObjectMeta : metav1.ObjectMeta {
409
410
Name : "fakename" ,
@@ -454,9 +455,9 @@ func TestInterPodAffinity(t *testing.T) {
454
455
Labels : podLabel }}},
455
456
node : nodes [0 ],
456
457
fits : true ,
457
- test : "validates that InterPod Affinity and AntiAffinity is respected if matching" ,
458
458
},
459
459
{
460
+ name : "satisfies the PodAffinity and PodAntiAffinity and PodAntiAffinity symmetry with the existing pod" ,
460
461
pod : & v1.Pod {
461
462
ObjectMeta : metav1.ObjectMeta {
462
463
Name : "fakename" ,
@@ -531,9 +532,9 @@ func TestInterPodAffinity(t *testing.T) {
531
532
},
532
533
node : nodes [0 ],
533
534
fits : true ,
534
- test : "satisfies the PodAffinity and PodAntiAffinity and PodAntiAffinity symmetry with the existing pod" ,
535
535
},
536
536
{
537
+ name : "satisfies the PodAffinity but doesn't satisfies the PodAntiAffinity with the existing pod" ,
537
538
pod : & v1.Pod {
538
539
ObjectMeta : metav1.ObjectMeta {
539
540
Name : "fakename" ,
@@ -584,9 +585,9 @@ func TestInterPodAffinity(t *testing.T) {
584
585
Labels : podLabel }}},
585
586
node : nodes [0 ],
586
587
fits : false ,
587
- test : "satisfies the PodAffinity but doesn't satisfies the PodAntiAffinity with the existing pod" ,
588
588
},
589
589
{
590
+ name : "satisfies the PodAffinity and PodAntiAffinity but doesn't satisfies PodAntiAffinity symmetry with the existing pod" ,
590
591
pod : & v1.Pod {
591
592
ObjectMeta : metav1.ObjectMeta {
592
593
Name : "fakename" ,
@@ -661,9 +662,9 @@ func TestInterPodAffinity(t *testing.T) {
661
662
},
662
663
node : nodes [0 ],
663
664
fits : false ,
664
- test : "satisfies the PodAffinity and PodAntiAffinity but doesn't satisfies PodAntiAffinity symmetry with the existing pod" ,
665
665
},
666
666
{
667
+ name : "pod matches its own Label in PodAffinity and that matches the existing pod Labels" ,
667
668
pod : & v1.Pod {
668
669
ObjectMeta : metav1.ObjectMeta {
669
670
Name : "fakename" ,
@@ -698,9 +699,9 @@ func TestInterPodAffinity(t *testing.T) {
698
699
Labels : podLabel }}},
699
700
node : nodes [0 ],
700
701
fits : false ,
701
- test : "pod matches its own Label in PodAffinity and that matches the existing pod Labels" ,
702
702
},
703
703
{
704
+ name : "Verify that PodAntiAffinity of an existing pod is respected when PodAntiAffinity symmetry is not satisfied with the existing pod" ,
704
705
pod : & v1.Pod {
705
706
ObjectMeta : metav1.ObjectMeta {
706
707
Name : "fakename" ,
@@ -738,9 +739,9 @@ func TestInterPodAffinity(t *testing.T) {
738
739
},
739
740
node : nodes [0 ],
740
741
fits : false ,
741
- test : "Verify that PodAntiAffinity of an existing pod is respected when PodAntiAffinity symmetry is not satisfied with the existing pod" ,
742
742
},
743
743
{
744
+ name : "Verify that PodAntiAffinity from existing pod is respected when pod statisfies PodAntiAffinity symmetry with the existing pod" ,
744
745
pod : & v1.Pod {
745
746
ObjectMeta : metav1.ObjectMeta {
746
747
Name : "fake-name" ,
@@ -778,9 +779,9 @@ func TestInterPodAffinity(t *testing.T) {
778
779
},
779
780
node : nodes [0 ],
780
781
fits : true ,
781
- test : "Verify that PodAntiAffinity from existing pod is respected when pod statisfies PodAntiAffinity symmetry with the existing pod" ,
782
782
},
783
783
{
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." ,
784
785
pod : & v1.Pod {
785
786
ObjectMeta : metav1.ObjectMeta {Name : "fake-name2" },
786
787
Spec : v1.PodSpec {
@@ -812,67 +813,68 @@ func TestInterPodAffinity(t *testing.T) {
812
813
NodeName : nodes [0 ].Name }, ObjectMeta : metav1.ObjectMeta {Name : "fakename" , Labels : map [string ]string {"foo" : "abc" }}},
813
814
},
814
815
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." ,
816
816
},
817
817
}
818
818
819
819
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
+ }
830
836
}
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 {} )
832
838
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
+ }
840
842
}
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
- }
851
843
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 ))
864
846
} else {
865
- nsName = testCtx . NS . Name
847
+ err = wait . Poll ( pollInterval , wait . ForeverTestTimeout , podUnschedulable ( cs , testPod . Namespace , testPod . Name ))
866
848
}
867
- err = cs .CoreV1 ().Pods (nsName ).Delete (context .TODO (), pod .Name , * metav1 .NewDeleteOptions (0 ))
868
849
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 )
870
851
}
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 ))
872
854
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 )
874
856
}
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
+ })
876
878
}
877
879
}
878
880
@@ -1005,16 +1007,16 @@ func TestEvenPodsSpreadPredicate(t *testing.T) {
1005
1007
for _ , pod := range tt .existingPods {
1006
1008
createdPod , err := cs .CoreV1 ().Pods (pod .Namespace ).Create (context .TODO (), pod , metav1.CreateOptions {})
1007
1009
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 )
1009
1011
}
1010
1012
err = wait .Poll (pollInterval , wait .ForeverTestTimeout , testutils .PodScheduled (cs , createdPod .Namespace , createdPod .Name ))
1011
1013
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 )
1013
1015
}
1014
1016
}
1015
1017
testPod , err := cs .CoreV1 ().Pods (tt .incomingPod .Namespace ).Create (context .TODO (), tt .incomingPod , metav1.CreateOptions {})
1016
1018
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 )
1018
1020
}
1019
1021
1020
1022
if tt .fits {
0 commit comments