1
0
mirror of https://github.com/kubernetes-sigs/descheduler.git synced 2026-01-25 20:59:28 +01:00

refactor(TestPodLifeTime): inline node creation in each unit test to avoid accidental node spec updates

This commit is contained in:
Jan Chaloupka
2025-12-10 14:39:04 +01:00
parent bbc3eef1c9
commit 94a0fbdcbb

View File

@@ -35,71 +35,75 @@ import (
)
func TestPodLifeTime(t *testing.T) {
node1 := test.BuildTestNode("n1", 2000, 3000, 10, nil)
const nodeName1 = "n1"
buildTestNode1 := func() *v1.Node {
return test.BuildTestNode(nodeName1, 2000, 3000, 10, nil)
}
olderPodCreationTime := metav1.NewTime(time.Date(2009, time.November, 10, 23, 0, 0, 0, time.UTC))
newerPodCreationTime := metav1.NewTime(time.Now())
// Setup pods, one should be evicted
p1 := test.BuildTestPod("p1", 100, 0, node1.Name, func(pod *v1.Pod) {
p1 := test.BuildTestPod("p1", 100, 0, nodeName1, func(pod *v1.Pod) {
pod.Namespace = "dev"
pod.ObjectMeta.CreationTimestamp = newerPodCreationTime
test.SetRSOwnerRef(pod)
})
p2 := test.BuildTestPod("p2", 100, 0, node1.Name, func(pod *v1.Pod) {
p2 := test.BuildTestPod("p2", 100, 0, nodeName1, func(pod *v1.Pod) {
pod.Namespace = "dev"
pod.ObjectMeta.CreationTimestamp = olderPodCreationTime
test.SetRSOwnerRef(pod)
})
// Setup pods, zero should be evicted
p3 := test.BuildTestPod("p3", 100, 0, node1.Name, func(pod *v1.Pod) {
p3 := test.BuildTestPod("p3", 100, 0, nodeName1, func(pod *v1.Pod) {
pod.Namespace = "dev"
pod.ObjectMeta.CreationTimestamp = newerPodCreationTime
test.SetRSOwnerRef(pod)
})
p4 := test.BuildTestPod("p4", 100, 0, node1.Name, func(pod *v1.Pod) {
p4 := test.BuildTestPod("p4", 100, 0, nodeName1, func(pod *v1.Pod) {
pod.Namespace = "dev"
pod.ObjectMeta.CreationTimestamp = newerPodCreationTime
test.SetRSOwnerRef(pod)
})
// Setup pods, one should be evicted
p5 := test.BuildTestPod("p5", 100, 0, node1.Name, func(pod *v1.Pod) {
p5 := test.BuildTestPod("p5", 100, 0, nodeName1, func(pod *v1.Pod) {
pod.Namespace = "dev"
pod.ObjectMeta.CreationTimestamp = newerPodCreationTime
test.SetRSOwnerRef(pod)
})
p6 := test.BuildTestPod("p6", 100, 0, node1.Name, func(pod *v1.Pod) {
p6 := test.BuildTestPod("p6", 100, 0, nodeName1, func(pod *v1.Pod) {
pod.Namespace = "dev"
pod.ObjectMeta.CreationTimestamp = metav1.NewTime(time.Now().Add(-time.Second * 605))
test.SetRSOwnerRef(pod)
})
// Setup pods, zero should be evicted
p7 := test.BuildTestPod("p7", 100, 0, node1.Name, func(pod *v1.Pod) {
p7 := test.BuildTestPod("p7", 100, 0, nodeName1, func(pod *v1.Pod) {
pod.Namespace = "dev"
pod.ObjectMeta.CreationTimestamp = newerPodCreationTime
})
p8 := test.BuildTestPod("p8", 100, 0, node1.Name, func(pod *v1.Pod) {
p8 := test.BuildTestPod("p8", 100, 0, nodeName1, func(pod *v1.Pod) {
pod.Namespace = "dev"
pod.ObjectMeta.CreationTimestamp = metav1.NewTime(time.Now().Add(-time.Second * 595))
})
// Setup two old pods with different status phases
p9 := test.BuildTestPod("p9", 100, 0, node1.Name, func(pod *v1.Pod) {
p9 := test.BuildTestPod("p9", 100, 0, nodeName1, func(pod *v1.Pod) {
pod.Namespace = "dev"
pod.ObjectMeta.CreationTimestamp = olderPodCreationTime
pod.Status.Phase = "Pending"
test.SetRSOwnerRef(pod)
})
p10 := test.BuildTestPod("p10", 100, 0, node1.Name, func(pod *v1.Pod) {
p10 := test.BuildTestPod("p10", 100, 0, nodeName1, func(pod *v1.Pod) {
pod.Namespace = "dev"
pod.ObjectMeta.CreationTimestamp = olderPodCreationTime
pod.Status.Phase = "Running"
test.SetRSOwnerRef(pod)
})
p11 := test.BuildTestPod("p11", 100, 0, node1.Name, func(pod *v1.Pod) {
p11 := test.BuildTestPod("p11", 100, 0, nodeName1, func(pod *v1.Pod) {
pod.Spec.Volumes = []v1.Volume{
{
Name: "pvc", VolumeSource: v1.VolumeSource{
@@ -113,33 +117,33 @@ func TestPodLifeTime(t *testing.T) {
})
// Setup two old pods with different labels
p12 := test.BuildTestPod("p12", 100, 0, node1.Name, func(pod *v1.Pod) {
p12 := test.BuildTestPod("p12", 100, 0, nodeName1, func(pod *v1.Pod) {
pod.Namespace = "dev"
pod.ObjectMeta.CreationTimestamp = olderPodCreationTime
pod.ObjectMeta.Labels = map[string]string{"foo": "bar"}
test.SetRSOwnerRef(pod)
})
p13 := test.BuildTestPod("p13", 100, 0, node1.Name, func(pod *v1.Pod) {
p13 := test.BuildTestPod("p13", 100, 0, nodeName1, func(pod *v1.Pod) {
pod.Namespace = "dev"
pod.ObjectMeta.CreationTimestamp = olderPodCreationTime
pod.ObjectMeta.Labels = map[string]string{"foo": "bar1"}
test.SetRSOwnerRef(pod)
})
p14 := test.BuildTestPod("p14", 100, 0, node1.Name, func(pod *v1.Pod) {
p14 := test.BuildTestPod("p14", 100, 0, nodeName1, func(pod *v1.Pod) {
pod.Namespace = "dev"
pod.ObjectMeta.CreationTimestamp = olderPodCreationTime
test.SetRSOwnerRef(pod)
pod.DeletionTimestamp = &metav1.Time{}
})
p15 := test.BuildTestPod("p15", 100, 0, node1.Name, func(pod *v1.Pod) {
p15 := test.BuildTestPod("p15", 100, 0, nodeName1, func(pod *v1.Pod) {
pod.Namespace = "dev"
pod.ObjectMeta.CreationTimestamp = olderPodCreationTime
test.SetRSOwnerRef(pod)
pod.DeletionTimestamp = &metav1.Time{}
})
p16 := test.BuildTestPod("p16", 100, 0, node1.Name, func(pod *v1.Pod) {
p16 := test.BuildTestPod("p16", 100, 0, nodeName1, func(pod *v1.Pod) {
pod.Namespace = "dev"
pod.ObjectMeta.CreationTimestamp = olderPodCreationTime
pod.Status.Phase = v1.PodUnknown
@@ -165,7 +169,7 @@ func TestPodLifeTime(t *testing.T) {
MaxPodLifeTimeSeconds: &maxLifeTime,
},
pods: []*v1.Pod{p1, p2},
nodes: []*v1.Node{node1},
nodes: []*v1.Node{buildTestNode1()},
expectedEvictedPodCount: 1,
},
{
@@ -174,7 +178,7 @@ func TestPodLifeTime(t *testing.T) {
MaxPodLifeTimeSeconds: &maxLifeTime,
},
pods: []*v1.Pod{p3, p4},
nodes: []*v1.Node{node1},
nodes: []*v1.Node{buildTestNode1()},
expectedEvictedPodCount: 0,
},
{
@@ -183,7 +187,7 @@ func TestPodLifeTime(t *testing.T) {
MaxPodLifeTimeSeconds: &maxLifeTime,
},
pods: []*v1.Pod{p5, p6},
nodes: []*v1.Node{node1},
nodes: []*v1.Node{buildTestNode1()},
expectedEvictedPodCount: 1,
},
{
@@ -192,7 +196,7 @@ func TestPodLifeTime(t *testing.T) {
MaxPodLifeTimeSeconds: &maxLifeTime,
},
pods: []*v1.Pod{p7, p8},
nodes: []*v1.Node{node1},
nodes: []*v1.Node{buildTestNode1()},
expectedEvictedPodCount: 0,
},
{
@@ -203,7 +207,7 @@ func TestPodLifeTime(t *testing.T) {
},
pods: []*v1.Pod{
p9,
test.BuildTestPod("container-creating-stuck", 0, 0, node1.Name, func(pod *v1.Pod) {
test.BuildTestPod("container-creating-stuck", 0, 0, nodeName1, func(pod *v1.Pod) {
pod.Status.ContainerStatuses = []v1.ContainerStatus{
{
State: v1.ContainerState{
@@ -215,7 +219,7 @@ func TestPodLifeTime(t *testing.T) {
pod.ObjectMeta.CreationTimestamp = olderPodCreationTime
}),
},
nodes: []*v1.Node{node1},
nodes: []*v1.Node{buildTestNode1()},
expectedEvictedPodCount: 1,
},
{
@@ -226,7 +230,7 @@ func TestPodLifeTime(t *testing.T) {
},
pods: []*v1.Pod{
p9,
test.BuildTestPod("pod-initializing-stuck", 0, 0, node1.Name, func(pod *v1.Pod) {
test.BuildTestPod("pod-initializing-stuck", 0, 0, nodeName1, func(pod *v1.Pod) {
pod.Status.ContainerStatuses = []v1.ContainerStatus{
{
State: v1.ContainerState{
@@ -238,7 +242,7 @@ func TestPodLifeTime(t *testing.T) {
pod.ObjectMeta.CreationTimestamp = olderPodCreationTime
}),
},
nodes: []*v1.Node{node1},
nodes: []*v1.Node{buildTestNode1()},
expectedEvictedPodCount: 1,
},
{
@@ -248,7 +252,7 @@ func TestPodLifeTime(t *testing.T) {
States: []string{"Pending"},
},
pods: []*v1.Pod{p9, p10},
nodes: []*v1.Node{node1},
nodes: []*v1.Node{buildTestNode1()},
expectedEvictedPodCount: 1,
},
{
@@ -257,7 +261,7 @@ func TestPodLifeTime(t *testing.T) {
MaxPodLifeTimeSeconds: &maxLifeTime,
},
pods: []*v1.Pod{p11},
nodes: []*v1.Node{node1},
nodes: []*v1.Node{buildTestNode1()},
expectedEvictedPodCount: 0,
ignorePvcPods: true,
},
@@ -267,7 +271,7 @@ func TestPodLifeTime(t *testing.T) {
MaxPodLifeTimeSeconds: &maxLifeTime,
},
pods: []*v1.Pod{p11},
nodes: []*v1.Node{node1},
nodes: []*v1.Node{buildTestNode1()},
expectedEvictedPodCount: 1,
},
{
@@ -279,7 +283,7 @@ func TestPodLifeTime(t *testing.T) {
},
},
pods: []*v1.Pod{p12, p13},
nodes: []*v1.Node{node1},
nodes: []*v1.Node{buildTestNode1()},
expectedEvictedPodCount: 1,
},
{
@@ -291,7 +295,7 @@ func TestPodLifeTime(t *testing.T) {
},
},
pods: []*v1.Pod{p14, p15},
nodes: []*v1.Node{node1},
nodes: []*v1.Node{buildTestNode1()},
expectedEvictedPodCount: 0,
},
{
@@ -300,7 +304,7 @@ func TestPodLifeTime(t *testing.T) {
MaxPodLifeTimeSeconds: &maxLifeTime,
},
pods: []*v1.Pod{p1, p2, p9},
nodes: []*v1.Node{node1},
nodes: []*v1.Node{buildTestNode1()},
expectedEvictedPodCount: 2,
maxPodsToEvictPerNode: nil,
maxPodsToEvictPerNamespace: nil,
@@ -311,7 +315,7 @@ func TestPodLifeTime(t *testing.T) {
MaxPodLifeTimeSeconds: &maxLifeTime,
},
pods: []*v1.Pod{p1, p2, p9},
nodes: []*v1.Node{node1},
nodes: []*v1.Node{buildTestNode1()},
maxPodsToEvictPerNamespace: utilptr.To[uint](1),
expectedEvictedPodCount: 1,
},
@@ -321,7 +325,7 @@ func TestPodLifeTime(t *testing.T) {
MaxPodLifeTimeSeconds: &maxLifeTime,
},
pods: []*v1.Pod{p1, p2, p9},
nodes: []*v1.Node{node1},
nodes: []*v1.Node{buildTestNode1()},
maxPodsToEvictPerNamespace: utilptr.To[uint](2),
maxPodsToEvictTotal: utilptr.To[uint](1),
expectedEvictedPodCount: 1,
@@ -332,7 +336,7 @@ func TestPodLifeTime(t *testing.T) {
MaxPodLifeTimeSeconds: &maxLifeTime,
},
pods: []*v1.Pod{p1, p2, p9},
nodes: []*v1.Node{node1},
nodes: []*v1.Node{buildTestNode1()},
maxPodsToEvictPerNode: utilptr.To[uint](1),
expectedEvictedPodCount: 1,
},
@@ -343,7 +347,7 @@ func TestPodLifeTime(t *testing.T) {
States: []string{"ImagePullBackOff"},
},
pods: []*v1.Pod{p9},
nodes: []*v1.Node{node1},
nodes: []*v1.Node{buildTestNode1()},
expectedEvictedPodCount: 1,
applyPodsFunc: func(pods []*v1.Pod) {
pods[0].Status.ContainerStatuses = []v1.ContainerStatus{
@@ -362,7 +366,7 @@ func TestPodLifeTime(t *testing.T) {
States: []string{"CrashLoopBackOff"},
},
pods: []*v1.Pod{p9},
nodes: []*v1.Node{node1},
nodes: []*v1.Node{buildTestNode1()},
expectedEvictedPodCount: 1,
applyPodsFunc: func(pods []*v1.Pod) {
pods[0].Status.ContainerStatuses = []v1.ContainerStatus{
@@ -381,7 +385,7 @@ func TestPodLifeTime(t *testing.T) {
States: []string{"CreateContainerConfigError"},
},
pods: []*v1.Pod{p9},
nodes: []*v1.Node{node1},
nodes: []*v1.Node{buildTestNode1()},
expectedEvictedPodCount: 1,
applyPodsFunc: func(pods []*v1.Pod) {
pods[0].Status.ContainerStatuses = []v1.ContainerStatus{
@@ -400,7 +404,7 @@ func TestPodLifeTime(t *testing.T) {
States: []string{"ErrImagePull"},
},
pods: []*v1.Pod{p9},
nodes: []*v1.Node{node1},
nodes: []*v1.Node{buildTestNode1()},
expectedEvictedPodCount: 1,
applyPodsFunc: func(pods []*v1.Pod) {
pods[0].Status.ContainerStatuses = []v1.ContainerStatus{
@@ -419,7 +423,7 @@ func TestPodLifeTime(t *testing.T) {
States: []string{"CreateContainerError"},
},
pods: []*v1.Pod{p9},
nodes: []*v1.Node{node1},
nodes: []*v1.Node{buildTestNode1()},
expectedEvictedPodCount: 0,
applyPodsFunc: func(pods []*v1.Pod) {
pods[0].Status.InitContainerStatuses = []v1.ContainerStatus{
@@ -439,7 +443,7 @@ func TestPodLifeTime(t *testing.T) {
IncludingInitContainers: true,
},
pods: []*v1.Pod{p9},
nodes: []*v1.Node{node1},
nodes: []*v1.Node{buildTestNode1()},
expectedEvictedPodCount: 1,
applyPodsFunc: func(pods []*v1.Pod) {
pods[0].Status.InitContainerStatuses = []v1.ContainerStatus{
@@ -458,7 +462,7 @@ func TestPodLifeTime(t *testing.T) {
States: []string{"CreateContainerError"},
},
pods: []*v1.Pod{p9},
nodes: []*v1.Node{node1},
nodes: []*v1.Node{buildTestNode1()},
expectedEvictedPodCount: 0,
applyPodsFunc: func(pods []*v1.Pod) {
pods[0].Status.InitContainerStatuses = []v1.ContainerStatus{
@@ -478,7 +482,7 @@ func TestPodLifeTime(t *testing.T) {
IncludingEphemeralContainers: true,
},
pods: []*v1.Pod{p9},
nodes: []*v1.Node{node1},
nodes: []*v1.Node{buildTestNode1()},
expectedEvictedPodCount: 1,
applyPodsFunc: func(pods []*v1.Pod) {
pods[0].Status.EphemeralContainerStatuses = []v1.ContainerStatus{
@@ -497,7 +501,7 @@ func TestPodLifeTime(t *testing.T) {
States: []string{"CreateContainerError"},
},
pods: []*v1.Pod{p9},
nodes: []*v1.Node{node1},
nodes: []*v1.Node{buildTestNode1()},
expectedEvictedPodCount: 1,
applyPodsFunc: func(pods []*v1.Pod) {
pods[0].Status.ContainerStatuses = []v1.ContainerStatus{
@@ -516,7 +520,7 @@ func TestPodLifeTime(t *testing.T) {
States: []string{"InvalidImageName"},
},
pods: []*v1.Pod{p9},
nodes: []*v1.Node{node1},
nodes: []*v1.Node{buildTestNode1()},
expectedEvictedPodCount: 1,
applyPodsFunc: func(pods []*v1.Pod) {
pods[0].Status.ContainerStatuses = []v1.ContainerStatus{
@@ -535,7 +539,7 @@ func TestPodLifeTime(t *testing.T) {
States: []string{"NodeLost"},
},
pods: []*v1.Pod{p9},
nodes: []*v1.Node{node1},
nodes: []*v1.Node{buildTestNode1()},
expectedEvictedPodCount: 1,
applyPodsFunc: func(pods []*v1.Pod) {
pods[0].Status.Reason = "NodeLost"
@@ -548,7 +552,7 @@ func TestPodLifeTime(t *testing.T) {
States: []string{"NodeAffinity"},
},
pods: []*v1.Pod{p9},
nodes: []*v1.Node{node1},
nodes: []*v1.Node{buildTestNode1()},
expectedEvictedPodCount: 1,
applyPodsFunc: func(pods []*v1.Pod) {
pods[0].Status.Reason = "NodeAffinity"
@@ -561,7 +565,7 @@ func TestPodLifeTime(t *testing.T) {
States: []string{"Shutdown"},
},
pods: []*v1.Pod{p9},
nodes: []*v1.Node{node1},
nodes: []*v1.Node{buildTestNode1()},
expectedEvictedPodCount: 1,
applyPodsFunc: func(pods []*v1.Pod) {
pods[0].Status.Reason = "Shutdown"
@@ -574,7 +578,7 @@ func TestPodLifeTime(t *testing.T) {
States: []string{"UnexpectedAdmissionError"},
},
pods: []*v1.Pod{p9},
nodes: []*v1.Node{node1},
nodes: []*v1.Node{buildTestNode1()},
expectedEvictedPodCount: 1,
applyPodsFunc: func(pods []*v1.Pod) {
pods[0].Status.Reason = "UnexpectedAdmissionError"
@@ -587,7 +591,7 @@ func TestPodLifeTime(t *testing.T) {
States: []string{string(v1.PodSucceeded)},
},
pods: []*v1.Pod{p16},
nodes: []*v1.Node{node1},
nodes: []*v1.Node{buildTestNode1()},
expectedEvictedPodCount: 1,
applyPodsFunc: func(pods []*v1.Pod) {
pods[0].Status.Phase = v1.PodSucceeded
@@ -600,7 +604,7 @@ func TestPodLifeTime(t *testing.T) {
States: []string{string(v1.PodFailed)},
},
pods: []*v1.Pod{p16},
nodes: []*v1.Node{node1},
nodes: []*v1.Node{buildTestNode1()},
expectedEvictedPodCount: 1,
applyPodsFunc: func(pods []*v1.Pod) {
pods[0].Status.Phase = v1.PodFailed
@@ -613,7 +617,7 @@ func TestPodLifeTime(t *testing.T) {
States: []string{string(v1.PodUnknown)},
},
pods: []*v1.Pod{p16},
nodes: []*v1.Node{node1},
nodes: []*v1.Node{buildTestNode1()},
expectedEvictedPodCount: 1,
applyPodsFunc: func(pods []*v1.Pod) {
pods[0].Status.Phase = v1.PodUnknown
@@ -626,7 +630,7 @@ func TestPodLifeTime(t *testing.T) {
States: []string{"ContainerCreating"},
},
pods: []*v1.Pod{p9},
nodes: []*v1.Node{node1},
nodes: []*v1.Node{buildTestNode1()},
expectedEvictedPodCount: 0,
applyPodsFunc: func(pods []*v1.Pod) {
pods[0].Status.ContainerStatuses = []v1.ContainerStatus{