Skip to content

Commit 5d10ab5

Browse files
authored
Merge pull request kubernetes#126606 from googs1025/refactor_kubelet_peemption_ut
refactor(kubelet preemption): merge TestEvictPodsToFreeRequests() method in ut
2 parents aa2938f + 529d13c commit 5d10ab5

File tree

1 file changed

+53
-69
lines changed

1 file changed

+53
-69
lines changed

pkg/kubelet/preemption/preemption_test.go

Lines changed: 53 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -90,93 +90,71 @@ func getTestCriticalPodAdmissionHandler(podProvider *fakePodProvider, podKiller
9090
}
9191
}
9292

93-
func TestEvictPodsToFreeRequestsWithError(t *testing.T) {
94-
type testRun struct {
95-
testName string
96-
inputPods []*v1.Pod
97-
insufficientResources admissionRequirementList
98-
expectErr bool
99-
expectedOutput []*v1.Pod
100-
}
101-
podProvider := newFakePodProvider()
102-
podKiller := newFakePodKiller(true)
103-
criticalPodAdmissionHandler := getTestCriticalPodAdmissionHandler(podProvider, podKiller)
104-
allPods := getTestPods()
105-
runs := []testRun{
106-
{
107-
testName: "multiple pods eviction error",
108-
inputPods: []*v1.Pod{
109-
allPods[clusterCritical], allPods[bestEffort], allPods[burstable], allPods[highRequestBurstable],
110-
allPods[guaranteed], allPods[highRequestGuaranteed]},
111-
insufficientResources: getAdmissionRequirementList(0, 550, 0),
112-
expectErr: false,
113-
expectedOutput: nil,
114-
},
115-
}
116-
for _, r := range runs {
117-
podProvider.setPods(r.inputPods)
118-
outErr := criticalPodAdmissionHandler.evictPodsToFreeRequests(allPods[clusterCritical], r.insufficientResources)
119-
outputPods := podKiller.getKilledPods()
120-
if !r.expectErr && outErr != nil {
121-
t.Errorf("evictPodsToFreeRequests returned an unexpected error during the %s test. Err: %v", r.testName, outErr)
122-
} else if r.expectErr && outErr == nil {
123-
t.Errorf("evictPodsToFreeRequests expected an error but returned a successful output=%v during the %s test.", outputPods, r.testName)
124-
} else if !podListEqual(r.expectedOutput, outputPods) {
125-
t.Errorf("evictPodsToFreeRequests expected %v but got %v during the %s test.", r.expectedOutput, outputPods, r.testName)
126-
}
127-
podKiller.clear()
128-
}
129-
}
130-
13193
func TestEvictPodsToFreeRequests(t *testing.T) {
13294
type testRun struct {
13395
testName string
96+
isPodKillerWithError bool
13497
inputPods []*v1.Pod
13598
insufficientResources admissionRequirementList
13699
expectErr bool
137100
expectedOutput []*v1.Pod
138101
}
139-
podProvider := newFakePodProvider()
140-
podKiller := newFakePodKiller(false)
141-
criticalPodAdmissionHandler := getTestCriticalPodAdmissionHandler(podProvider, podKiller)
142102
allPods := getTestPods()
143103
runs := []testRun{
144104
{
145105
testName: "critical pods cannot be preempted",
106+
isPodKillerWithError: false,
146107
inputPods: []*v1.Pod{allPods[clusterCritical]},
147108
insufficientResources: getAdmissionRequirementList(0, 0, 1),
148109
expectErr: true,
149110
expectedOutput: nil,
150111
},
151112
{
152113
testName: "best effort pods are not preempted when attempting to free resources",
114+
isPodKillerWithError: false,
153115
inputPods: []*v1.Pod{allPods[bestEffort]},
154116
insufficientResources: getAdmissionRequirementList(0, 1, 0),
155117
expectErr: true,
156118
expectedOutput: nil,
157119
},
158120
{
159-
testName: "multiple pods evicted",
121+
testName: "multiple pods evicted",
122+
isPodKillerWithError: false,
160123
inputPods: []*v1.Pod{
161124
allPods[clusterCritical], allPods[bestEffort], allPods[burstable], allPods[highRequestBurstable],
162125
allPods[guaranteed], allPods[highRequestGuaranteed]},
163126
insufficientResources: getAdmissionRequirementList(0, 550, 0),
164127
expectErr: false,
165128
expectedOutput: []*v1.Pod{allPods[highRequestBurstable], allPods[highRequestGuaranteed]},
166129
},
130+
{
131+
testName: "multiple pods with eviction error",
132+
isPodKillerWithError: true,
133+
inputPods: []*v1.Pod{
134+
allPods[clusterCritical], allPods[bestEffort], allPods[burstable], allPods[highRequestBurstable],
135+
allPods[guaranteed], allPods[highRequestGuaranteed]},
136+
insufficientResources: getAdmissionRequirementList(0, 550, 0),
137+
expectErr: false,
138+
expectedOutput: nil,
139+
},
167140
}
168141
for _, r := range runs {
169-
podProvider.setPods(r.inputPods)
170-
outErr := criticalPodAdmissionHandler.evictPodsToFreeRequests(allPods[clusterCritical], r.insufficientResources)
171-
outputPods := podKiller.getKilledPods()
172-
if !r.expectErr && outErr != nil {
173-
t.Errorf("evictPodsToFreeRequests returned an unexpected error during the %s test. Err: %v", r.testName, outErr)
174-
} else if r.expectErr && outErr == nil {
175-
t.Errorf("evictPodsToFreeRequests expected an error but returned a successful output=%v during the %s test.", outputPods, r.testName)
176-
} else if !podListEqual(r.expectedOutput, outputPods) {
177-
t.Errorf("evictPodsToFreeRequests expected %v but got %v during the %s test.", r.expectedOutput, outputPods, r.testName)
178-
}
179-
podKiller.clear()
142+
t.Run(r.testName, func(t *testing.T) {
143+
podProvider := newFakePodProvider()
144+
podKiller := newFakePodKiller(r.isPodKillerWithError)
145+
criticalPodAdmissionHandler := getTestCriticalPodAdmissionHandler(podProvider, podKiller)
146+
podProvider.setPods(r.inputPods)
147+
outErr := criticalPodAdmissionHandler.evictPodsToFreeRequests(allPods[clusterCritical], r.insufficientResources)
148+
outputPods := podKiller.getKilledPods()
149+
if !r.expectErr && outErr != nil {
150+
t.Errorf("evictPodsToFreeRequests returned an unexpected error during the %s test. Err: %v", r.testName, outErr)
151+
} else if r.expectErr && outErr == nil {
152+
t.Errorf("evictPodsToFreeRequests expected an error but returned a successful output=%v during the %s test.", outputPods, r.testName)
153+
} else if !podListEqual(r.expectedOutput, outputPods) {
154+
t.Errorf("evictPodsToFreeRequests expected %v but got %v during the %s test.", r.expectedOutput, outputPods, r.testName)
155+
}
156+
podKiller.clear()
157+
})
180158
}
181159
}
182160

@@ -305,14 +283,16 @@ func TestGetPodsToPreempt(t *testing.T) {
305283
}
306284

307285
for _, r := range runs {
308-
outputPods, outErr := getPodsToPreempt(r.preemptor, r.inputPods, r.insufficientResources)
309-
if !r.expectErr && outErr != nil {
310-
t.Errorf("getPodsToPreempt returned an unexpected error during the %s test. Err: %v", r.testName, outErr)
311-
} else if r.expectErr && outErr == nil {
312-
t.Errorf("getPodsToPreempt expected an error but returned a successful output=%v during the %s test.", outputPods, r.testName)
313-
} else if !podListEqual(r.expectedOutput, outputPods) {
314-
t.Errorf("getPodsToPreempt expected %v but got %v during the %s test.", r.expectedOutput, outputPods, r.testName)
315-
}
286+
t.Run(r.testName, func(t *testing.T) {
287+
outputPods, outErr := getPodsToPreempt(r.preemptor, r.inputPods, r.insufficientResources)
288+
if !r.expectErr && outErr != nil {
289+
t.Errorf("getPodsToPreempt returned an unexpected error during the %s test. Err: %v", r.testName, outErr)
290+
} else if r.expectErr && outErr == nil {
291+
t.Errorf("getPodsToPreempt expected an error but returned a successful output=%v during the %s test.", outputPods, r.testName)
292+
} else if !podListEqual(r.expectedOutput, outputPods) {
293+
t.Errorf("getPodsToPreempt expected %v but got %v during the %s test.", r.expectedOutput, outputPods, r.testName)
294+
}
295+
})
316296
}
317297
}
318298

@@ -351,10 +331,12 @@ func TestAdmissionRequirementsDistance(t *testing.T) {
351331
},
352332
}
353333
for _, run := range runs {
354-
output := run.requirements.distance(run.inputPod)
355-
if output != run.expectedOutput {
356-
t.Errorf("expected: %f, got: %f for %s test", run.expectedOutput, output, run.testName)
357-
}
334+
t.Run(run.testName, func(t *testing.T) {
335+
output := run.requirements.distance(run.inputPod)
336+
if output != run.expectedOutput {
337+
t.Errorf("expected: %f, got: %f for %s test", run.expectedOutput, output, run.testName)
338+
}
339+
})
358340
}
359341
}
360342

@@ -399,10 +381,12 @@ func TestAdmissionRequirementsSubtract(t *testing.T) {
399381
},
400382
}
401383
for _, run := range runs {
402-
output := run.initial.subtract(run.inputPod)
403-
if !admissionRequirementListEqual(output, run.expectedOutput) {
404-
t.Errorf("expected: %s, got: %s for %s test", run.expectedOutput.toString(), output.toString(), run.testName)
405-
}
384+
t.Run(run.testName, func(t *testing.T) {
385+
output := run.initial.subtract(run.inputPod)
386+
if !admissionRequirementListEqual(output, run.expectedOutput) {
387+
t.Errorf("expected: %s, got: %s for %s test", run.expectedOutput.toString(), output.toString(), run.testName)
388+
}
389+
})
406390
}
407391
}
408392

0 commit comments

Comments
 (0)