1
0
mirror of https://github.com/kubernetes-sigs/descheduler.git synced 2026-01-26 13:29:11 +01:00

Merge pull request #162 from tammert/duplicate-test-logic-rework

Improved logic for duplicates testing
This commit is contained in:
Kubernetes Prow Robot
2019-07-05 08:00:38 -07:00
committed by GitHub

View File

@@ -86,64 +86,58 @@ func TestFindDuplicatePods(t *testing.T) {
// A Critical Pod. // A Critical Pod.
p7.Annotations = test.GetCriticalPodAnnotation() p7.Annotations = test.GetCriticalPodAnnotation()
tests := []struct { testCases := []struct {
description string description string
maxPodsToEvict int maxPodsToEvict int
pods []v1.Pod pods []v1.Pod
expectedEvictedPodCount int expectedEvictedPodCount int
}{ }{
{ {
description: "Three pods in the default Namespace, bound to same ReplicaSet. 2 should get deleted", description: "Three pods in the `dev` Namespace, bound to same ReplicaSet. 2 should be evicted.",
maxPodsToEvict: 2, maxPodsToEvict: 5,
pods: []v1.Pod{*p1, *p2, *p3}, pods: []v1.Pod{*p1, *p2, *p3},
expectedEvictedPodCount: 2, expectedEvictedPodCount: 2,
}, },
{ {
description: "Three Pods in the test Namespace, bound to same ReplicaSet. 2 should be evicted", description: "Three Pods in the `test` Namespace, bound to same ReplicaSet. 2 should be evicted.",
maxPodsToEvict: 2, maxPodsToEvict: 5,
pods: []v1.Pod{*p8, *p9, *p10}, pods: []v1.Pod{*p8, *p9, *p10},
expectedEvictedPodCount: 2, expectedEvictedPodCount: 2,
}, },
{ {
description: "pods are part of daemonset and local storage - none should get deleted", description: "Three Pods in the `dev` Namespace, three Pods in the `test` Namespace. Bound to ReplicaSet with same name. 4 should be evicted.",
maxPodsToEvict: 2, maxPodsToEvict: 5,
pods: []v1.Pod{*p4, *p5}, pods: []v1.Pod{*p1, *p2, *p3, *p8, *p9, *p10},
expectedEvictedPodCount: 0,
},
{
description: "pods are mirror pod and critical pod - none should get deleted",
maxPodsToEvict: 2,
pods: []v1.Pod{*p6, *p7},
expectedEvictedPodCount: 0,
},
{
description: "pods are part of replicaset, daemonset and local storage - all duplicate replicaset pods should get deleted",
maxPodsToEvict: 4,
pods: []v1.Pod{*p1, *p2, *p3, *p8, *p9, *p10, *p4, *p5},
expectedEvictedPodCount: 4, expectedEvictedPodCount: 4,
}, },
{ {
description: "pods are part of replicaset, daemonset, local storage, mirror pod and critical pod - all duplicate replicaset pods should get deleted", description: "Pods are: part of DaemonSet, with local storage, mirror pod annotation, critical pod annotation - none should be evicted.",
maxPodsToEvict: 2,
pods: []v1.Pod{*p4, *p5, *p6, *p7},
expectedEvictedPodCount: 0,
},
{
description: "Test all Pods: 4 should be evicted.",
maxPodsToEvict: 5, maxPodsToEvict: 5,
pods: []v1.Pod{*p1, *p2, *p3, *p8, *p9, *p10, *p4, *p5, *p6, *p7}, pods: []v1.Pod{*p1, *p2, *p3, *p4, *p5, *p6, *p7, *p8, *p9, *p10},
expectedEvictedPodCount: 4, expectedEvictedPodCount: 4,
}, },
} }
for _, test := range tests { for _, testCase := range testCases {
npe := nodePodEvictedCount{} npe := nodePodEvictedCount{}
npe[node] = 0 npe[node] = 0
fakeClient := &fake.Clientset{} fakeClient := &fake.Clientset{}
fakeClient.Fake.AddReactor("list", "pods", func(action core.Action) (bool, runtime.Object, error) { fakeClient.Fake.AddReactor("list", "pods", func(action core.Action) (bool, runtime.Object, error) {
return true, &v1.PodList{Items: test.pods}, nil return true, &v1.PodList{Items: testCase.pods}, nil
}) })
fakeClient.Fake.AddReactor("get", "nodes", func(action core.Action) (bool, runtime.Object, error) { fakeClient.Fake.AddReactor("get", "nodes", func(action core.Action) (bool, runtime.Object, error) {
return true, node, nil return true, node, nil
}) })
podsEvicted := deleteDuplicatePods(fakeClient, "v1", []*v1.Node{node}, false, npe, test.maxPodsToEvict, false) podsEvicted := deleteDuplicatePods(fakeClient, "v1", []*v1.Node{node}, false, npe, testCase.maxPodsToEvict, false)
if podsEvicted != test.expectedEvictedPodCount { if podsEvicted != testCase.expectedEvictedPodCount {
t.Errorf("Test error for Desc: %s. Expected deleted pods count %v , got %v", test.description, test.expectedEvictedPodCount, podsEvicted) t.Errorf("Test error for description: %s. Expected evicted pods count %v, got %v", testCase.description, testCase.expectedEvictedPodCount, podsEvicted)
} }
} }