Merge pull request #126606 from googs1025/refactor_kubelet_peemption_ut

refactor(kubelet preemption): merge TestEvictPodsToFreeRequests() method in ut
This commit is contained in:
Kubernetes Prow Robot 2024-08-14 00:21:13 -07:00 committed by GitHub
commit 5d10ab5cd5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -90,59 +90,20 @@ func getTestCriticalPodAdmissionHandler(podProvider *fakePodProvider, podKiller
} }
} }
func TestEvictPodsToFreeRequestsWithError(t *testing.T) {
type testRun struct {
testName string
inputPods []*v1.Pod
insufficientResources admissionRequirementList
expectErr bool
expectedOutput []*v1.Pod
}
podProvider := newFakePodProvider()
podKiller := newFakePodKiller(true)
criticalPodAdmissionHandler := getTestCriticalPodAdmissionHandler(podProvider, podKiller)
allPods := getTestPods()
runs := []testRun{
{
testName: "multiple pods eviction error",
inputPods: []*v1.Pod{
allPods[clusterCritical], allPods[bestEffort], allPods[burstable], allPods[highRequestBurstable],
allPods[guaranteed], allPods[highRequestGuaranteed]},
insufficientResources: getAdmissionRequirementList(0, 550, 0),
expectErr: false,
expectedOutput: nil,
},
}
for _, r := range runs {
podProvider.setPods(r.inputPods)
outErr := criticalPodAdmissionHandler.evictPodsToFreeRequests(allPods[clusterCritical], r.insufficientResources)
outputPods := podKiller.getKilledPods()
if !r.expectErr && outErr != nil {
t.Errorf("evictPodsToFreeRequests returned an unexpected error during the %s test. Err: %v", r.testName, outErr)
} else if r.expectErr && outErr == nil {
t.Errorf("evictPodsToFreeRequests expected an error but returned a successful output=%v during the %s test.", outputPods, r.testName)
} else if !podListEqual(r.expectedOutput, outputPods) {
t.Errorf("evictPodsToFreeRequests expected %v but got %v during the %s test.", r.expectedOutput, outputPods, r.testName)
}
podKiller.clear()
}
}
func TestEvictPodsToFreeRequests(t *testing.T) { func TestEvictPodsToFreeRequests(t *testing.T) {
type testRun struct { type testRun struct {
testName string testName string
isPodKillerWithError bool
inputPods []*v1.Pod inputPods []*v1.Pod
insufficientResources admissionRequirementList insufficientResources admissionRequirementList
expectErr bool expectErr bool
expectedOutput []*v1.Pod expectedOutput []*v1.Pod
} }
podProvider := newFakePodProvider()
podKiller := newFakePodKiller(false)
criticalPodAdmissionHandler := getTestCriticalPodAdmissionHandler(podProvider, podKiller)
allPods := getTestPods() allPods := getTestPods()
runs := []testRun{ runs := []testRun{
{ {
testName: "critical pods cannot be preempted", testName: "critical pods cannot be preempted",
isPodKillerWithError: false,
inputPods: []*v1.Pod{allPods[clusterCritical]}, inputPods: []*v1.Pod{allPods[clusterCritical]},
insufficientResources: getAdmissionRequirementList(0, 0, 1), insufficientResources: getAdmissionRequirementList(0, 0, 1),
expectErr: true, expectErr: true,
@ -150,13 +111,15 @@ func TestEvictPodsToFreeRequests(t *testing.T) {
}, },
{ {
testName: "best effort pods are not preempted when attempting to free resources", testName: "best effort pods are not preempted when attempting to free resources",
isPodKillerWithError: false,
inputPods: []*v1.Pod{allPods[bestEffort]}, inputPods: []*v1.Pod{allPods[bestEffort]},
insufficientResources: getAdmissionRequirementList(0, 1, 0), insufficientResources: getAdmissionRequirementList(0, 1, 0),
expectErr: true, expectErr: true,
expectedOutput: nil, expectedOutput: nil,
}, },
{ {
testName: "multiple pods evicted", testName: "multiple pods evicted",
isPodKillerWithError: false,
inputPods: []*v1.Pod{ inputPods: []*v1.Pod{
allPods[clusterCritical], allPods[bestEffort], allPods[burstable], allPods[highRequestBurstable], allPods[clusterCritical], allPods[bestEffort], allPods[burstable], allPods[highRequestBurstable],
allPods[guaranteed], allPods[highRequestGuaranteed]}, allPods[guaranteed], allPods[highRequestGuaranteed]},
@ -164,19 +127,34 @@ func TestEvictPodsToFreeRequests(t *testing.T) {
expectErr: false, expectErr: false,
expectedOutput: []*v1.Pod{allPods[highRequestBurstable], allPods[highRequestGuaranteed]}, expectedOutput: []*v1.Pod{allPods[highRequestBurstable], allPods[highRequestGuaranteed]},
}, },
{
testName: "multiple pods with eviction error",
isPodKillerWithError: true,
inputPods: []*v1.Pod{
allPods[clusterCritical], allPods[bestEffort], allPods[burstable], allPods[highRequestBurstable],
allPods[guaranteed], allPods[highRequestGuaranteed]},
insufficientResources: getAdmissionRequirementList(0, 550, 0),
expectErr: false,
expectedOutput: nil,
},
} }
for _, r := range runs { for _, r := range runs {
podProvider.setPods(r.inputPods) t.Run(r.testName, func(t *testing.T) {
outErr := criticalPodAdmissionHandler.evictPodsToFreeRequests(allPods[clusterCritical], r.insufficientResources) podProvider := newFakePodProvider()
outputPods := podKiller.getKilledPods() podKiller := newFakePodKiller(r.isPodKillerWithError)
if !r.expectErr && outErr != nil { criticalPodAdmissionHandler := getTestCriticalPodAdmissionHandler(podProvider, podKiller)
t.Errorf("evictPodsToFreeRequests returned an unexpected error during the %s test. Err: %v", r.testName, outErr) podProvider.setPods(r.inputPods)
} else if r.expectErr && outErr == nil { outErr := criticalPodAdmissionHandler.evictPodsToFreeRequests(allPods[clusterCritical], r.insufficientResources)
t.Errorf("evictPodsToFreeRequests expected an error but returned a successful output=%v during the %s test.", outputPods, r.testName) outputPods := podKiller.getKilledPods()
} else if !podListEqual(r.expectedOutput, outputPods) { if !r.expectErr && outErr != nil {
t.Errorf("evictPodsToFreeRequests expected %v but got %v during the %s test.", r.expectedOutput, outputPods, r.testName) t.Errorf("evictPodsToFreeRequests returned an unexpected error during the %s test. Err: %v", r.testName, outErr)
} } else if r.expectErr && outErr == nil {
podKiller.clear() t.Errorf("evictPodsToFreeRequests expected an error but returned a successful output=%v during the %s test.", outputPods, r.testName)
} else if !podListEqual(r.expectedOutput, outputPods) {
t.Errorf("evictPodsToFreeRequests expected %v but got %v during the %s test.", r.expectedOutput, outputPods, r.testName)
}
podKiller.clear()
})
} }
} }
@ -305,14 +283,16 @@ func TestGetPodsToPreempt(t *testing.T) {
} }
for _, r := range runs { for _, r := range runs {
outputPods, outErr := getPodsToPreempt(r.preemptor, r.inputPods, r.insufficientResources) t.Run(r.testName, func(t *testing.T) {
if !r.expectErr && outErr != nil { outputPods, outErr := getPodsToPreempt(r.preemptor, r.inputPods, r.insufficientResources)
t.Errorf("getPodsToPreempt returned an unexpected error during the %s test. Err: %v", r.testName, outErr) if !r.expectErr && outErr != nil {
} else if r.expectErr && outErr == nil { t.Errorf("getPodsToPreempt returned an unexpected error during the %s test. Err: %v", r.testName, outErr)
t.Errorf("getPodsToPreempt expected an error but returned a successful output=%v during the %s test.", outputPods, r.testName) } else if r.expectErr && outErr == nil {
} else if !podListEqual(r.expectedOutput, outputPods) { t.Errorf("getPodsToPreempt expected an error but returned a successful output=%v during the %s test.", outputPods, r.testName)
t.Errorf("getPodsToPreempt expected %v but got %v during the %s test.", r.expectedOutput, outputPods, r.testName) } else if !podListEqual(r.expectedOutput, outputPods) {
} t.Errorf("getPodsToPreempt expected %v but got %v during the %s test.", r.expectedOutput, outputPods, r.testName)
}
})
} }
} }
@ -351,10 +331,12 @@ func TestAdmissionRequirementsDistance(t *testing.T) {
}, },
} }
for _, run := range runs { for _, run := range runs {
output := run.requirements.distance(run.inputPod) t.Run(run.testName, func(t *testing.T) {
if output != run.expectedOutput { output := run.requirements.distance(run.inputPod)
t.Errorf("expected: %f, got: %f for %s test", run.expectedOutput, output, run.testName) if output != run.expectedOutput {
} t.Errorf("expected: %f, got: %f for %s test", run.expectedOutput, output, run.testName)
}
})
} }
} }
@ -399,10 +381,12 @@ func TestAdmissionRequirementsSubtract(t *testing.T) {
}, },
} }
for _, run := range runs { for _, run := range runs {
output := run.initial.subtract(run.inputPod) t.Run(run.testName, func(t *testing.T) {
if !admissionRequirementListEqual(output, run.expectedOutput) { output := run.initial.subtract(run.inputPod)
t.Errorf("expected: %s, got: %s for %s test", run.expectedOutput.toString(), output.toString(), run.testName) if !admissionRequirementListEqual(output, run.expectedOutput) {
} t.Errorf("expected: %s, got: %s for %s test", run.expectedOutput.toString(), output.toString(), run.testName)
}
})
} }
} }