From fc8ae3b4e8ba8a346a9d3c7aabcb36f9736244a3 Mon Sep 17 00:00:00 2001 From: Jan Chaloupka Date: Sun, 21 Dec 2025 17:46:29 +0100 Subject: [PATCH 1/3] refactor: replace pod.Spec.Priority with test.SetPodPriority in plugin tests --- .../defaultevictor/defaultevictor_test.go | 8 ++-- .../highnodeutilization_test.go | 6 +-- .../lownodeutilization_test.go | 48 +++++++------------ .../removeduplicates/removeduplicates_test.go | 6 +-- .../pod_antiaffinity_test.go | 2 +- 5 files changed, 25 insertions(+), 45 deletions(-) diff --git a/pkg/framework/plugins/defaultevictor/defaultevictor_test.go b/pkg/framework/plugins/defaultevictor/defaultevictor_test.go index f88b75986..f7fffbcc7 100644 --- a/pkg/framework/plugins/defaultevictor/defaultevictor_test.go +++ b/pkg/framework/plugins/defaultevictor/defaultevictor_test.go @@ -512,7 +512,7 @@ func TestDefaultEvictorFilter(t *testing.T) { pods: []*v1.Pod{ buildTestPod("p14", n1.Name, func(pod *v1.Pod) { test.SetNormalOwnerRef(pod) - pod.Spec.Priority = &highPriority + test.SetPodPriority(pod, highPriority) }), }, priorityThreshold: &lowPriority, @@ -523,7 +523,7 @@ func TestDefaultEvictorFilter(t *testing.T) { buildTestPod("p15", n1.Name, func(pod *v1.Pod) { test.SetNormalOwnerRef(pod) setPodEvictAnnotation(pod) - pod.Spec.Priority = &highPriority + test.SetPodPriority(pod, highPriority) }), }, priorityThreshold: &lowPriority, @@ -557,7 +557,7 @@ func TestDefaultEvictorFilter(t *testing.T) { pods: []*v1.Pod{ buildTestPod("p17", n1.Name, func(pod *v1.Pod) { test.SetNormalOwnerRef(pod) - pod.Spec.Priority = &highPriority + test.SetPodPriority(pod, highPriority) }), }, evictSystemCriticalPods: true, @@ -570,7 +570,7 @@ func TestDefaultEvictorFilter(t *testing.T) { buildTestPod("p17", n1.Name, func(pod *v1.Pod) { test.SetNormalOwnerRef(pod) setPodEvictAnnotation(pod) - pod.Spec.Priority = &highPriority + test.SetPodPriority(pod, highPriority) }), }, evictSystemCriticalPods: true, diff --git a/pkg/framework/plugins/nodeutilization/highnodeutilization_test.go b/pkg/framework/plugins/nodeutilization/highnodeutilization_test.go index 1e7297f2e..e117d617f 100644 --- a/pkg/framework/plugins/nodeutilization/highnodeutilization_test.go +++ b/pkg/framework/plugins/nodeutilization/highnodeutilization_test.go @@ -115,8 +115,7 @@ func TestHighNodeUtilization(t *testing.T) { // A Critical Pod. test.SetNormalOwnerRef(pod) pod.Namespace = "kube-system" - priority := utils.SystemCriticalPriority - pod.Spec.Priority = &priority + test.SetPodPriority(pod, utils.SystemCriticalPriority) }), // These won't be evicted. test.BuildTestPod("p3", 400, 0, n2NodeName, test.SetDSOwnerRef), @@ -168,8 +167,7 @@ func TestHighNodeUtilization(t *testing.T) { // A Critical Pod. test.SetNormalOwnerRef(pod) pod.Namespace = "kube-system" - priority := utils.SystemCriticalPriority - pod.Spec.Priority = &priority + test.SetPodPriority(pod, utils.SystemCriticalPriority) }), // These won't be evicted. test.BuildTestPod("p3", 400, 0, n2NodeName, test.SetRSOwnerRef), diff --git a/pkg/framework/plugins/nodeutilization/lownodeutilization_test.go b/pkg/framework/plugins/nodeutilization/lownodeutilization_test.go index 4cfd10240..233c718e2 100644 --- a/pkg/framework/plugins/nodeutilization/lownodeutilization_test.go +++ b/pkg/framework/plugins/nodeutilization/lownodeutilization_test.go @@ -109,8 +109,7 @@ func TestLowNodeUtilization(t *testing.T) { test.BuildTestPod("p6", 400, 0, n1NodeName, func(pod *v1.Pod) { // A Critical Pod. pod.Namespace = "kube-system" - priority := utils.SystemCriticalPriority - pod.Spec.Priority = &priority + test.SetPodPriority(pod, utils.SystemCriticalPriority) }), test.BuildTestPod("p9", 400, 0, n2NodeName, test.SetRSOwnerRef), }, @@ -172,8 +171,7 @@ func TestLowNodeUtilization(t *testing.T) { test.BuildTestPod("p8", 400, 0, n1NodeName, func(pod *v1.Pod) { // A Critical Pod. pod.Namespace = "kube-system" - priority := utils.SystemCriticalPriority - pod.Spec.Priority = &priority + test.SetPodPriority(pod, utils.SystemCriticalPriority) }), test.BuildTestPod("p9", 400, 0, n2NodeName, test.SetRSOwnerRef), }, @@ -250,8 +248,7 @@ func TestLowNodeUtilization(t *testing.T) { test.BuildTestPod("p8", 400, 0, n1NodeName, func(pod *v1.Pod) { // A Critical Pod. pod.Namespace = "kube-system" - priority := utils.SystemCriticalPriority - pod.Spec.Priority = &priority + test.SetPodPriority(pod, utils.SystemCriticalPriority) }), test.BuildTestPod("p9", 400, 0, n2NodeName, test.SetRSOwnerRef), }, @@ -328,8 +325,7 @@ func TestLowNodeUtilization(t *testing.T) { // A Critical Pod. test.SetNormalOwnerRef(pod) pod.Namespace = "kube-system" - priority := utils.SystemCriticalPriority - pod.Spec.Priority = &priority + test.SetPodPriority(pod, utils.SystemCriticalPriority) }), test.BuildTestPod("p9", 400, 0, n2NodeName, test.SetRSOwnerRef), }, @@ -397,8 +393,7 @@ func TestLowNodeUtilization(t *testing.T) { // A Critical Pod. test.SetNormalOwnerRef(pod) pod.Namespace = "kube-system" - priority := utils.SystemCriticalPriority - pod.Spec.Priority = &priority + test.SetPodPriority(pod, utils.SystemCriticalPriority) }), test.BuildTestPod("p9", 400, 0, n2NodeName, test.SetRSOwnerRef), }, @@ -480,8 +475,7 @@ func TestLowNodeUtilization(t *testing.T) { // A Critical Pod. test.SetNormalOwnerRef(pod) pod.Namespace = "kube-system" - priority := utils.SystemCriticalPriority - pod.Spec.Priority = &priority + test.SetPodPriority(pod, utils.SystemCriticalPriority) }), test.BuildTestPod("p9", 400, 0, n2NodeName, test.SetRSOwnerRef), }, @@ -561,8 +555,7 @@ func TestLowNodeUtilization(t *testing.T) { // A Critical Pod. test.SetNormalOwnerRef(pod) pod.Namespace = "kube-system" - priority := utils.SystemCriticalPriority - pod.Spec.Priority = &priority + test.SetPodPriority(pod, utils.SystemCriticalPriority) }), test.BuildTestPod("p9", 400, 0, n2NodeName, test.SetRSOwnerRef), }, @@ -651,8 +644,7 @@ func TestLowNodeUtilization(t *testing.T) { test.SetNormalOwnerRef(pod) test.SetPodExtendedResourceRequest(pod, extendedResource, 1) pod.Namespace = "kube-system" - priority := utils.SystemCriticalPriority - pod.Spec.Priority = &priority + test.SetPodPriority(pod, utils.SystemCriticalPriority) }), test.BuildTestPod("p9", 0, 0, n2NodeName, func(pod *v1.Pod) { test.SetRSOwnerRef(pod) @@ -813,8 +805,7 @@ func TestLowNodeUtilization(t *testing.T) { // A Critical Pod. test.SetNormalOwnerRef(pod) pod.Namespace = "kube-system" - priority := utils.SystemCriticalPriority - pod.Spec.Priority = &priority + test.SetPodPriority(pod, utils.SystemCriticalPriority) }), }, nodemetricses: []*v1beta1.NodeMetrics{ @@ -890,8 +881,7 @@ func TestLowNodeUtilization(t *testing.T) { // A Critical Pod. test.SetNormalOwnerRef(pod) pod.Namespace = "kube-system" - priority := utils.SystemCriticalPriority - pod.Spec.Priority = &priority + test.SetPodPriority(pod, utils.SystemCriticalPriority) }), }, nodemetricses: []*v1beta1.NodeMetrics{ @@ -993,8 +983,7 @@ func TestLowNodeUtilization(t *testing.T) { // A Critical Pod. test.SetNormalOwnerRef(pod) pod.Namespace = "kube-system" - priority := utils.SystemCriticalPriority - pod.Spec.Priority = &priority + test.SetPodPriority(pod, utils.SystemCriticalPriority) }), test.BuildTestPod("p9", 0, 0, n2NodeName, test.SetRSOwnerRef), }, @@ -1055,8 +1044,7 @@ func TestLowNodeUtilization(t *testing.T) { // A Critical Pod. test.SetNormalOwnerRef(pod) pod.Namespace = "kube-system" - priority := utils.SystemCriticalPriority - pod.Spec.Priority = &priority + test.SetPodPriority(pod, utils.SystemCriticalPriority) }), test.BuildTestPod("p9", 400, 0, n2NodeName, test.SetRSOwnerRef), }, @@ -1124,8 +1112,7 @@ func TestLowNodeUtilization(t *testing.T) { // A Critical Pod. test.SetNormalOwnerRef(pod) pod.Namespace = "kube-system" - priority := utils.SystemCriticalPriority - pod.Spec.Priority = &priority + test.SetPodPriority(pod, utils.SystemCriticalPriority) }), test.BuildTestPod("p9", 400, 3000, n2NodeName, test.SetRSOwnerRef), }, @@ -1236,8 +1223,7 @@ func TestLowNodeUtilization(t *testing.T) { // A Critical Pod. test.SetNormalOwnerRef(pod) pod.Namespace = "kube-system" - priority := utils.SystemCriticalPriority - pod.Spec.Priority = &priority + test.SetPodPriority(pod, utils.SystemCriticalPriority) }), test.BuildTestPod("p9", 0, 0, n2NodeName, test.SetRSOwnerRef), }, @@ -1299,8 +1285,7 @@ func TestLowNodeUtilization(t *testing.T) { test.BuildTestPod("p8", 400, 0, n1NodeName, func(pod *v1.Pod) { // A Critical Pod. pod.Namespace = "kube-system" - priority := utils.SystemCriticalPriority - pod.Spec.Priority = &priority + test.SetPodPriority(pod, utils.SystemCriticalPriority) }), test.BuildTestPod("p9", 400, 0, n2NodeName, test.SetRSOwnerRef), }, @@ -1594,8 +1579,7 @@ func withCriticalPod(pod *v1.Pod) { // A Critical Pod. test.SetNormalOwnerRef(pod) pod.Namespace = "kube-system" - priority := utils.SystemCriticalPriority - pod.Spec.Priority = &priority + test.SetPodPriority(pod, utils.SystemCriticalPriority) } func TestLowNodeUtilizationWithPrometheusMetrics(t *testing.T) { diff --git a/pkg/framework/plugins/removeduplicates/removeduplicates_test.go b/pkg/framework/plugins/removeduplicates/removeduplicates_test.go index 95c229088..7bcc3af40 100644 --- a/pkg/framework/plugins/removeduplicates/removeduplicates_test.go +++ b/pkg/framework/plugins/removeduplicates/removeduplicates_test.go @@ -167,8 +167,7 @@ func TestFindDuplicatePods(t *testing.T) { }), buildTestPodForNode("p7", nodeName1, func(pod *v1.Pod) { pod.Namespace = "kube-system" - priority := utils.SystemCriticalPriority - pod.Spec.Priority = &priority + test.SetPodPriority(pod, utils.SystemCriticalPriority) }), }, nodes: []*v1.Node{ @@ -205,8 +204,7 @@ func TestFindDuplicatePods(t *testing.T) { }), buildTestPodForNode("p7", nodeName1, func(pod *v1.Pod) { pod.Namespace = "kube-system" - priority := utils.SystemCriticalPriority - pod.Spec.Priority = &priority + test.SetPodPriority(pod, utils.SystemCriticalPriority) }), buildTestPodWithRSOwnerRefWithNamespaceForNode1("p8", "test", nil), buildTestPodWithRSOwnerRefWithNamespaceForNode1("p9", "test", nil), diff --git a/pkg/framework/plugins/removepodsviolatinginterpodantiaffinity/pod_antiaffinity_test.go b/pkg/framework/plugins/removepodsviolatinginterpodantiaffinity/pod_antiaffinity_test.go index 2dbc0a7d4..66f35ab67 100644 --- a/pkg/framework/plugins/removepodsviolatinginterpodantiaffinity/pod_antiaffinity_test.go +++ b/pkg/framework/plugins/removepodsviolatinginterpodantiaffinity/pod_antiaffinity_test.go @@ -96,7 +96,7 @@ func buildTestPodP2ForNode1() *v1.Pod { func buildTestPodNonEvictableForNode1() *v1.Pod { criticalPriority := utils.SystemCriticalPriority return buildTestPodForNode1("non-evict", func(pod *v1.Pod) { - pod.Spec.Priority = &criticalPriority + test.SetPodPriority(pod, criticalPriority) setLabelsFooBar(pod) }) } From 988e0b886897bbf62a5c6a8cc11feadac554adfe Mon Sep 17 00:00:00 2001 From: Jan Chaloupka Date: Sun, 21 Dec 2025 18:04:15 +0100 Subject: [PATCH 2/3] refactor: replace pod.Spec.Volumes with test.SetHostPathEmptyDirVolumeSource in plugin tests --- .../defaultevictor/defaultevictor_test.go | 13 +- .../highnodeutilization_test.go | 13 +- .../lownodeutilization_test.go | 193 ++---------------- .../removeduplicates/removeduplicates_test.go | 25 +-- .../toomanyrestarts_test.go | 13 +- .../node_taint_test.go | 13 +- test/test_utils.go | 14 ++ 7 files changed, 36 insertions(+), 248 deletions(-) diff --git a/pkg/framework/plugins/defaultevictor/defaultevictor_test.go b/pkg/framework/plugins/defaultevictor/defaultevictor_test.go index f7fffbcc7..b8ef9a473 100644 --- a/pkg/framework/plugins/defaultevictor/defaultevictor_test.go +++ b/pkg/framework/plugins/defaultevictor/defaultevictor_test.go @@ -23,7 +23,6 @@ import ( v1 "k8s.io/api/core/v1" policyv1 "k8s.io/api/policy/v1" - "k8s.io/apimachinery/pkg/api/resource" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/apimachinery/pkg/util/uuid" @@ -289,17 +288,7 @@ func TestDefaultEvictorFilter(t *testing.T) { } setPodLocalStorage := func(pod *v1.Pod) { - pod.Spec.Volumes = []v1.Volume{ - { - Name: "sample", - VolumeSource: v1.VolumeSource{ - HostPath: &v1.HostPathVolumeSource{Path: "somePath"}, - EmptyDir: &v1.EmptyDirVolumeSource{ - SizeLimit: resource.NewQuantity(int64(10), resource.BinarySI), - }, - }, - }, - } + test.SetHostPathEmptyDirVolumeSource(pod) } setPodPVCVolumeWithFooClaimName := func(pod *v1.Pod) { diff --git a/pkg/framework/plugins/nodeutilization/highnodeutilization_test.go b/pkg/framework/plugins/nodeutilization/highnodeutilization_test.go index e117d617f..2624bf519 100644 --- a/pkg/framework/plugins/nodeutilization/highnodeutilization_test.go +++ b/pkg/framework/plugins/nodeutilization/highnodeutilization_test.go @@ -23,7 +23,6 @@ import ( v1 "k8s.io/api/core/v1" policy "k8s.io/api/policy/v1" - "k8s.io/apimachinery/pkg/api/resource" "k8s.io/apimachinery/pkg/runtime" "k8s.io/client-go/kubernetes/fake" core "k8s.io/client-go/testing" @@ -97,17 +96,7 @@ func TestHighNodeUtilization(t *testing.T) { test.BuildTestPod("p1", 400, 0, n1NodeName, func(pod *v1.Pod) { // A pod with local storage. test.SetNormalOwnerRef(pod) - pod.Spec.Volumes = []v1.Volume{ - { - Name: "sample", - VolumeSource: v1.VolumeSource{ - HostPath: &v1.HostPathVolumeSource{Path: "somePath"}, - EmptyDir: &v1.EmptyDirVolumeSource{ - SizeLimit: resource.NewQuantity(int64(10), resource.BinarySI), - }, - }, - }, - } + test.SetHostPathEmptyDirVolumeSource(pod) // A Mirror Pod. pod.Annotations = test.GetMirrorPodAnnotation() }), diff --git a/pkg/framework/plugins/nodeutilization/lownodeutilization_test.go b/pkg/framework/plugins/nodeutilization/lownodeutilization_test.go index 233c718e2..12fc1f88f 100644 --- a/pkg/framework/plugins/nodeutilization/lownodeutilization_test.go +++ b/pkg/framework/plugins/nodeutilization/lownodeutilization_test.go @@ -23,7 +23,6 @@ import ( v1 "k8s.io/api/core/v1" policy "k8s.io/api/policy/v1" - "k8s.io/apimachinery/pkg/api/resource" "k8s.io/apimachinery/pkg/labels" "k8s.io/apimachinery/pkg/runtime" "k8s.io/client-go/informers" @@ -92,17 +91,7 @@ func TestLowNodeUtilization(t *testing.T) { test.BuildTestPod("p5", 400, 0, n1NodeName, func(pod *v1.Pod) { // A pod with local storage. test.SetNormalOwnerRef(pod) - pod.Spec.Volumes = []v1.Volume{ - { - Name: "sample", - VolumeSource: v1.VolumeSource{ - HostPath: &v1.HostPathVolumeSource{Path: "somePath"}, - EmptyDir: &v1.EmptyDirVolumeSource{ - SizeLimit: resource.NewQuantity(int64(10), resource.BinarySI), - }, - }, - }, - } + test.SetHostPathEmptyDirVolumeSource(pod) // A Mirror Pod. pod.Annotations = test.GetMirrorPodAnnotation() }), @@ -154,17 +143,7 @@ func TestLowNodeUtilization(t *testing.T) { test.BuildTestPod("p7", 400, 0, n1NodeName, func(pod *v1.Pod) { // A pod with local storage. test.SetNormalOwnerRef(pod) - pod.Spec.Volumes = []v1.Volume{ - { - Name: "sample", - VolumeSource: v1.VolumeSource{ - HostPath: &v1.HostPathVolumeSource{Path: "somePath"}, - EmptyDir: &v1.EmptyDirVolumeSource{ - SizeLimit: resource.NewQuantity(int64(10), resource.BinarySI), - }, - }, - }, - } + test.SetHostPathEmptyDirVolumeSource(pod) // A Mirror Pod. pod.Annotations = test.GetMirrorPodAnnotation() }), @@ -231,17 +210,7 @@ func TestLowNodeUtilization(t *testing.T) { test.BuildTestPod("p7", 400, 0, n1NodeName, func(pod *v1.Pod) { // A pod with local storage. test.SetNormalOwnerRef(pod) - pod.Spec.Volumes = []v1.Volume{ - { - Name: "sample", - VolumeSource: v1.VolumeSource{ - HostPath: &v1.HostPathVolumeSource{Path: "somePath"}, - EmptyDir: &v1.EmptyDirVolumeSource{ - SizeLimit: resource.NewQuantity(int64(10), resource.BinarySI), - }, - }, - }, - } + test.SetHostPathEmptyDirVolumeSource(pod) // A Mirror Pod. pod.Annotations = test.GetMirrorPodAnnotation() }), @@ -307,17 +276,7 @@ func TestLowNodeUtilization(t *testing.T) { test.BuildTestPod("p7", 400, 0, n1NodeName, func(pod *v1.Pod) { // A pod with local storage. test.SetNormalOwnerRef(pod) - pod.Spec.Volumes = []v1.Volume{ - { - Name: "sample", - VolumeSource: v1.VolumeSource{ - HostPath: &v1.HostPathVolumeSource{Path: "somePath"}, - EmptyDir: &v1.EmptyDirVolumeSource{ - SizeLimit: resource.NewQuantity(int64(10), resource.BinarySI), - }, - }, - }, - } + test.SetHostPathEmptyDirVolumeSource(pod) // A Mirror Pod. pod.Annotations = test.GetMirrorPodAnnotation() }), @@ -375,17 +334,7 @@ func TestLowNodeUtilization(t *testing.T) { test.BuildTestPod("p7", 400, 0, n1NodeName, func(pod *v1.Pod) { // A pod with local storage. test.SetNormalOwnerRef(pod) - pod.Spec.Volumes = []v1.Volume{ - { - Name: "sample", - VolumeSource: v1.VolumeSource{ - HostPath: &v1.HostPathVolumeSource{Path: "somePath"}, - EmptyDir: &v1.EmptyDirVolumeSource{ - SizeLimit: resource.NewQuantity(int64(10), resource.BinarySI), - }, - }, - }, - } + test.SetHostPathEmptyDirVolumeSource(pod) // A Mirror Pod. pod.Annotations = test.GetMirrorPodAnnotation() }), @@ -457,17 +406,7 @@ func TestLowNodeUtilization(t *testing.T) { // A pod with local storage. test.SetNormalOwnerRef(pod) test.SetPodPriority(pod, lowPriority) - pod.Spec.Volumes = []v1.Volume{ - { - Name: "sample", - VolumeSource: v1.VolumeSource{ - HostPath: &v1.HostPathVolumeSource{Path: "somePath"}, - EmptyDir: &v1.EmptyDirVolumeSource{ - SizeLimit: resource.NewQuantity(int64(10), resource.BinarySI), - }, - }, - }, - } + test.SetHostPathEmptyDirVolumeSource(pod) // A Mirror Pod. pod.Annotations = test.GetMirrorPodAnnotation() }), @@ -537,17 +476,7 @@ func TestLowNodeUtilization(t *testing.T) { test.BuildTestPod("p7", 400, 0, n1NodeName, func(pod *v1.Pod) { // A pod with local storage. test.SetNormalOwnerRef(pod) - pod.Spec.Volumes = []v1.Volume{ - { - Name: "sample", - VolumeSource: v1.VolumeSource{ - HostPath: &v1.HostPathVolumeSource{Path: "somePath"}, - EmptyDir: &v1.EmptyDirVolumeSource{ - SizeLimit: resource.NewQuantity(int64(10), resource.BinarySI), - }, - }, - }, - } + test.SetHostPathEmptyDirVolumeSource(pod) // A Mirror Pod. pod.Annotations = test.GetMirrorPodAnnotation() }), @@ -625,17 +554,7 @@ func TestLowNodeUtilization(t *testing.T) { // A pod with local storage. test.SetNormalOwnerRef(pod) test.SetPodExtendedResourceRequest(pod, extendedResource, 1) - pod.Spec.Volumes = []v1.Volume{ - { - Name: "sample", - VolumeSource: v1.VolumeSource{ - HostPath: &v1.HostPathVolumeSource{Path: "somePath"}, - EmptyDir: &v1.EmptyDirVolumeSource{ - SizeLimit: resource.NewQuantity(int64(10), resource.BinarySI), - }, - }, - }, - } + test.SetHostPathEmptyDirVolumeSource(pod) // A Mirror Pod. pod.Annotations = test.GetMirrorPodAnnotation() }), @@ -787,17 +706,7 @@ func TestLowNodeUtilization(t *testing.T) { test.BuildTestPod("p7", 400, 0, n1NodeName, func(pod *v1.Pod) { // A pod with local storage. test.SetNormalOwnerRef(pod) - pod.Spec.Volumes = []v1.Volume{ - { - Name: "sample", - VolumeSource: v1.VolumeSource{ - HostPath: &v1.HostPathVolumeSource{Path: "somePath"}, - EmptyDir: &v1.EmptyDirVolumeSource{ - SizeLimit: resource.NewQuantity(int64(10), resource.BinarySI), - }, - }, - }, - } + test.SetHostPathEmptyDirVolumeSource(pod) // A Mirror Pod. pod.Annotations = test.GetMirrorPodAnnotation() }), @@ -863,17 +772,7 @@ func TestLowNodeUtilization(t *testing.T) { test.BuildTestPod("p7", 400, 0, n1NodeName, func(pod *v1.Pod) { // A pod with local storage. test.SetNormalOwnerRef(pod) - pod.Spec.Volumes = []v1.Volume{ - { - Name: "sample", - VolumeSource: v1.VolumeSource{ - HostPath: &v1.HostPathVolumeSource{Path: "somePath"}, - EmptyDir: &v1.EmptyDirVolumeSource{ - SizeLimit: resource.NewQuantity(int64(10), resource.BinarySI), - }, - }, - }, - } + test.SetHostPathEmptyDirVolumeSource(pod) // A Mirror Pod. pod.Annotations = test.GetMirrorPodAnnotation() }), @@ -965,17 +864,7 @@ func TestLowNodeUtilization(t *testing.T) { test.BuildTestPod("p7", 400, 0, n1NodeName, func(pod *v1.Pod) { // A pod with local storage. test.SetNormalOwnerRef(pod) - pod.Spec.Volumes = []v1.Volume{ - { - Name: "sample", - VolumeSource: v1.VolumeSource{ - HostPath: &v1.HostPathVolumeSource{Path: "somePath"}, - EmptyDir: &v1.EmptyDirVolumeSource{ - SizeLimit: resource.NewQuantity(int64(10), resource.BinarySI), - }, - }, - }, - } + test.SetHostPathEmptyDirVolumeSource(pod) // A Mirror Pod. pod.Annotations = test.GetMirrorPodAnnotation() }), @@ -1026,17 +915,7 @@ func TestLowNodeUtilization(t *testing.T) { test.BuildTestPod("p7", 400, 0, n1NodeName, func(pod *v1.Pod) { // A pod with local storage. test.SetNormalOwnerRef(pod) - pod.Spec.Volumes = []v1.Volume{ - { - Name: "sample", - VolumeSource: v1.VolumeSource{ - HostPath: &v1.HostPathVolumeSource{Path: "somePath"}, - EmptyDir: &v1.EmptyDirVolumeSource{ - SizeLimit: resource.NewQuantity(int64(10), resource.BinarySI), - }, - }, - }, - } + test.SetHostPathEmptyDirVolumeSource(pod) // A Mirror Pod. pod.Annotations = test.GetMirrorPodAnnotation() }), @@ -1094,17 +973,7 @@ func TestLowNodeUtilization(t *testing.T) { test.BuildTestPod("p7", 400, 375, n1NodeName, func(pod *v1.Pod) { // A pod with local storage. test.SetNormalOwnerRef(pod) - pod.Spec.Volumes = []v1.Volume{ - { - Name: "sample", - VolumeSource: v1.VolumeSource{ - HostPath: &v1.HostPathVolumeSource{Path: "somePath"}, - EmptyDir: &v1.EmptyDirVolumeSource{ - SizeLimit: resource.NewQuantity(int64(10), resource.BinarySI), - }, - }, - }, - } + test.SetHostPathEmptyDirVolumeSource(pod) // A Mirror Pod. pod.Annotations = test.GetMirrorPodAnnotation() }), @@ -1205,17 +1074,7 @@ func TestLowNodeUtilization(t *testing.T) { test.BuildTestPod("p7", 400, 0, n1NodeName, func(pod *v1.Pod) { // A pod with local storage. test.SetNormalOwnerRef(pod) - pod.Spec.Volumes = []v1.Volume{ - { - Name: "sample", - VolumeSource: v1.VolumeSource{ - HostPath: &v1.HostPathVolumeSource{Path: "somePath"}, - EmptyDir: &v1.EmptyDirVolumeSource{ - SizeLimit: resource.NewQuantity(int64(10), resource.BinarySI), - }, - }, - }, - } + test.SetHostPathEmptyDirVolumeSource(pod) // A Mirror Pod. pod.Annotations = test.GetMirrorPodAnnotation() }), @@ -1268,17 +1127,7 @@ func TestLowNodeUtilization(t *testing.T) { test.BuildTestPod("p7", 400, 0, n1NodeName, func(pod *v1.Pod) { // A pod with local storage. test.SetNormalOwnerRef(pod) - pod.Spec.Volumes = []v1.Volume{ - { - Name: "sample", - VolumeSource: v1.VolumeSource{ - HostPath: &v1.HostPathVolumeSource{Path: "somePath"}, - EmptyDir: &v1.EmptyDirVolumeSource{ - SizeLimit: resource.NewQuantity(int64(10), resource.BinarySI), - }, - }, - }, - } + test.SetHostPathEmptyDirVolumeSource(pod) // A Mirror Pod. pod.Annotations = test.GetMirrorPodAnnotation() }), @@ -1560,17 +1409,7 @@ func TestLowNodeUtilizationWithTaints(t *testing.T) { func withLocalStorage(pod *v1.Pod) { // A pod with local storage. test.SetNormalOwnerRef(pod) - pod.Spec.Volumes = []v1.Volume{ - { - Name: "sample", - VolumeSource: v1.VolumeSource{ - HostPath: &v1.HostPathVolumeSource{Path: "somePath"}, - EmptyDir: &v1.EmptyDirVolumeSource{ - SizeLimit: resource.NewQuantity(int64(10), resource.BinarySI), - }, - }, - }, - } + test.SetHostPathEmptyDirVolumeSource(pod) // A Mirror Pod. pod.Annotations = test.GetMirrorPodAnnotation() } diff --git a/pkg/framework/plugins/removeduplicates/removeduplicates_test.go b/pkg/framework/plugins/removeduplicates/removeduplicates_test.go index 7bcc3af40..cfe01ae3d 100644 --- a/pkg/framework/plugins/removeduplicates/removeduplicates_test.go +++ b/pkg/framework/plugins/removeduplicates/removeduplicates_test.go @@ -21,7 +21,6 @@ import ( "testing" v1 "k8s.io/api/core/v1" - "k8s.io/apimachinery/pkg/api/resource" metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" "k8s.io/apimachinery/pkg/runtime" "k8s.io/client-go/kubernetes/fake" @@ -150,17 +149,7 @@ func TestFindDuplicatePods(t *testing.T) { }), buildTestPodForNode("p5", nodeName1, func(pod *v1.Pod) { test.SetNormalOwnerRef(pod) - pod.Spec.Volumes = []v1.Volume{ - { - Name: "sample", - VolumeSource: v1.VolumeSource{ - HostPath: &v1.HostPathVolumeSource{Path: "somePath"}, - EmptyDir: &v1.EmptyDirVolumeSource{ - SizeLimit: resource.NewQuantity(int64(10), resource.BinarySI), - }, - }, - }, - } + test.SetHostPathEmptyDirVolumeSource(pod) }), buildTestPodForNode("p6", nodeName1, func(pod *v1.Pod) { pod.Annotations = test.GetMirrorPodAnnotation() @@ -187,17 +176,7 @@ func TestFindDuplicatePods(t *testing.T) { }), buildTestPodForNode("p5", nodeName1, func(pod *v1.Pod) { test.SetNormalOwnerRef(pod) - pod.Spec.Volumes = []v1.Volume{ - { - Name: "sample", - VolumeSource: v1.VolumeSource{ - HostPath: &v1.HostPathVolumeSource{Path: "somePath"}, - EmptyDir: &v1.EmptyDirVolumeSource{ - SizeLimit: resource.NewQuantity(int64(10), resource.BinarySI), - }, - }, - }, - } + test.SetHostPathEmptyDirVolumeSource(pod) }), buildTestPodForNode("p6", nodeName1, func(pod *v1.Pod) { pod.Annotations = test.GetMirrorPodAnnotation() diff --git a/pkg/framework/plugins/removepodshavingtoomanyrestarts/toomanyrestarts_test.go b/pkg/framework/plugins/removepodshavingtoomanyrestarts/toomanyrestarts_test.go index b5bcddb26..2f4141d37 100644 --- a/pkg/framework/plugins/removepodshavingtoomanyrestarts/toomanyrestarts_test.go +++ b/pkg/framework/plugins/removepodshavingtoomanyrestarts/toomanyrestarts_test.go @@ -22,7 +22,6 @@ import ( "testing" v1 "k8s.io/api/core/v1" - "k8s.io/apimachinery/pkg/api/resource" "k8s.io/apimachinery/pkg/runtime" "k8s.io/client-go/kubernetes/fake" @@ -94,17 +93,7 @@ func initPods(apply func(pod *v1.Pod)) []*v1.Pod { case 7: pods = append(pods, initPodContainersWithStatusRestartCount(fmt.Sprintf("pod-%d", i), i, func(pod *v1.Pod) { test.SetNormalOwnerRef(pod) - pod.Spec.Volumes = []v1.Volume{ - { - Name: "sample", - VolumeSource: v1.VolumeSource{ - HostPath: &v1.HostPathVolumeSource{Path: "somePath"}, - EmptyDir: &v1.EmptyDirVolumeSource{ - SizeLimit: resource.NewQuantity(int64(10), resource.BinarySI), - }, - }, - }, - } + test.SetHostPathEmptyDirVolumeSource(pod) if apply != nil { apply(pod) } diff --git a/pkg/framework/plugins/removepodsviolatingnodetaints/node_taint_test.go b/pkg/framework/plugins/removepodsviolatingnodetaints/node_taint_test.go index 9dc8b1306..8d1781060 100644 --- a/pkg/framework/plugins/removepodsviolatingnodetaints/node_taint_test.go +++ b/pkg/framework/plugins/removepodsviolatingnodetaints/node_taint_test.go @@ -22,7 +22,6 @@ import ( "testing" v1 "k8s.io/api/core/v1" - "k8s.io/apimachinery/pkg/api/resource" "k8s.io/apimachinery/pkg/runtime" "k8s.io/client-go/kubernetes/fake" @@ -135,17 +134,7 @@ func withTestTaintXToleration1(pod *v1.Pod) { } func withLocalStorageVolume(pod *v1.Pod) { - pod.Spec.Volumes = []v1.Volume{ - { - Name: "sample", - VolumeSource: v1.VolumeSource{ - HostPath: &v1.HostPathVolumeSource{Path: "somePath"}, - EmptyDir: &v1.EmptyDirVolumeSource{ - SizeLimit: resource.NewQuantity(int64(10), resource.BinarySI), - }, - }, - }, - } + test.SetHostPathEmptyDirVolumeSource(pod) } func withKubeSystemCriticalPod(pod *v1.Pod) { diff --git a/test/test_utils.go b/test/test_utils.go index 7c67dd247..4d641d723 100644 --- a/test/test_utils.go +++ b/test/test_utils.go @@ -262,6 +262,20 @@ func SetPodPriority(pod *v1.Pod, priority int32) { pod.Spec.Priority = &priority } +func SetHostPathEmptyDirVolumeSource(pod *v1.Pod) { + pod.Spec.Volumes = []v1.Volume{ + { + Name: "sample", + VolumeSource: v1.VolumeSource{ + HostPath: &v1.HostPathVolumeSource{Path: "somePath"}, + EmptyDir: &v1.EmptyDirVolumeSource{ + SizeLimit: resource.NewQuantity(int64(10), resource.BinarySI), + }, + }, + }, + } +} + // SetNodeUnschedulable sets the given node unschedulable func SetNodeUnschedulable(node *v1.Node) { node.Spec.Unschedulable = true From 7ef3673d4cea81cc24d6195bb18eb88bafca7e4c Mon Sep 17 00:00:00 2001 From: Jan Chaloupka Date: Sun, 21 Dec 2025 21:41:59 +0100 Subject: [PATCH 3/3] refactor: inline single-statement apply functions in BuildTestPod calls --- .../nodeutilization/highnodeutilization_test.go | 8 ++------ .../nodeutilization/lownodeutilization_test.go | 16 ++++------------ .../removeduplicates/removeduplicates_test.go | 8 ++------ 3 files changed, 8 insertions(+), 24 deletions(-) diff --git a/pkg/framework/plugins/nodeutilization/highnodeutilization_test.go b/pkg/framework/plugins/nodeutilization/highnodeutilization_test.go index 2624bf519..2609dc7ef 100644 --- a/pkg/framework/plugins/nodeutilization/highnodeutilization_test.go +++ b/pkg/framework/plugins/nodeutilization/highnodeutilization_test.go @@ -236,9 +236,7 @@ func TestHighNodeUtilization(t *testing.T) { test.SetRSOwnerRef(pod) test.MakeBestEffortPod(pod) }), - test.BuildTestPod("p2", 400, 0, n1NodeName, func(pod *v1.Pod) { - test.SetRSOwnerRef(pod) - }), + test.BuildTestPod("p2", 400, 0, n1NodeName, test.SetRSOwnerRef), // These won't be evicted. test.BuildTestPod("p3", 400, 0, n2NodeName, test.SetRSOwnerRef), test.BuildTestPod("p4", 400, 0, n2NodeName, test.SetRSOwnerRef), @@ -453,9 +451,7 @@ func TestHighNodeUtilization(t *testing.T) { // pods in the other nodes must not be evicted // because they do not have the extended // resource defined in their requests. - test.BuildTestPod("p3", 500, 0, n2NodeName, func(pod *v1.Pod) { - test.SetRSOwnerRef(pod) - }), + test.BuildTestPod("p3", 500, 0, n2NodeName, test.SetRSOwnerRef), test.BuildTestPod("p4", 500, 0, n2NodeName, func(pod *v1.Pod) { test.SetRSOwnerRef(pod) }), diff --git a/pkg/framework/plugins/nodeutilization/lownodeutilization_test.go b/pkg/framework/plugins/nodeutilization/lownodeutilization_test.go index 12fc1f88f..3c890d678 100644 --- a/pkg/framework/plugins/nodeutilization/lownodeutilization_test.go +++ b/pkg/framework/plugins/nodeutilization/lownodeutilization_test.go @@ -458,9 +458,7 @@ func TestLowNodeUtilization(t *testing.T) { test.SetRSOwnerRef(pod) test.MakeBestEffortPod(pod) }), - test.BuildTestPod("p3", 400, 0, n1NodeName, func(pod *v1.Pod) { - test.SetRSOwnerRef(pod) - }), + test.BuildTestPod("p3", 400, 0, n1NodeName, test.SetRSOwnerRef), test.BuildTestPod("p4", 400, 0, n1NodeName, func(pod *v1.Pod) { test.SetRSOwnerRef(pod) test.MakeBestEffortPod(pod) @@ -470,9 +468,7 @@ func TestLowNodeUtilization(t *testing.T) { test.MakeBestEffortPod(pod) }), // These won't be evicted. - test.BuildTestPod("p6", 400, 0, n1NodeName, func(pod *v1.Pod) { - test.SetDSOwnerRef(pod) - }), + test.BuildTestPod("p6", 400, 0, n1NodeName, test.SetDSOwnerRef), test.BuildTestPod("p7", 400, 0, n1NodeName, func(pod *v1.Pod) { // A pod with local storage. test.SetNormalOwnerRef(pod) @@ -658,12 +654,8 @@ func TestLowNodeUtilization(t *testing.T) { test.SetRSOwnerRef(pod) test.SetPodExtendedResourceRequest(pod, extendedResource, 7) }), - test.BuildTestPod("p3", 0, 0, n2NodeName, func(pod *v1.Pod) { - test.SetRSOwnerRef(pod) - }), - test.BuildTestPod("p8", 0, 0, n3NodeName, func(pod *v1.Pod) { - test.SetRSOwnerRef(pod) - }), + test.BuildTestPod("p3", 0, 0, n2NodeName, test.SetRSOwnerRef), + test.BuildTestPod("p8", 0, 0, n3NodeName, test.SetRSOwnerRef), test.BuildTestPod("p9", 0, 0, n3NodeName, test.SetRSOwnerRef), }, nodemetricses: []*v1beta1.NodeMetrics{ diff --git a/pkg/framework/plugins/removeduplicates/removeduplicates_test.go b/pkg/framework/plugins/removeduplicates/removeduplicates_test.go index cfe01ae3d..8cb772e49 100644 --- a/pkg/framework/plugins/removeduplicates/removeduplicates_test.go +++ b/pkg/framework/plugins/removeduplicates/removeduplicates_test.go @@ -144,9 +144,7 @@ func TestFindDuplicatePods(t *testing.T) { { description: "Pods are: part of DaemonSet, with local storage, mirror pod annotation, critical pod annotation - none should be evicted.", pods: []*v1.Pod{ - buildTestPodForNode("p4", nodeName1, func(pod *v1.Pod) { - test.SetDSOwnerRef(pod) - }), + buildTestPodForNode("p4", nodeName1, test.SetDSOwnerRef), buildTestPodForNode("p5", nodeName1, func(pod *v1.Pod) { test.SetNormalOwnerRef(pod) test.SetHostPathEmptyDirVolumeSource(pod) @@ -171,9 +169,7 @@ func TestFindDuplicatePods(t *testing.T) { buildTestPodWithRSOwnerRefWithNamespaceForNode1("p1", "dev", nil), buildTestPodWithRSOwnerRefWithNamespaceForNode1("p2", "dev", nil), buildTestPodWithRSOwnerRefWithNamespaceForNode1("p3", "dev", nil), - buildTestPodForNode("p4", nodeName1, func(pod *v1.Pod) { - test.SetDSOwnerRef(pod) - }), + buildTestPodForNode("p4", nodeName1, test.SetDSOwnerRef), buildTestPodForNode("p5", nodeName1, func(pod *v1.Pod) { test.SetNormalOwnerRef(pod) test.SetHostPathEmptyDirVolumeSource(pod)