From 988e0b886897bbf62a5c6a8cc11feadac554adfe Mon Sep 17 00:00:00 2001 From: Jan Chaloupka Date: Sun, 21 Dec 2025 18:04:15 +0100 Subject: [PATCH] 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