From ac652d3c79b87eb025575c87535aebbcdfa9f706 Mon Sep 17 00:00:00 2001 From: r0hansaxena Date: Tue, 7 Apr 2026 00:19:23 +0530 Subject: [PATCH] test(prefernodeswithoutcache): add mutate path tests Signed-off-by: r0hansaxena --- .../prefer_nodes_without_cache_test.go | 26 ++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/pkg/webhook/plugins/prefernodeswithoutcache/prefer_nodes_without_cache_test.go b/pkg/webhook/plugins/prefernodeswithoutcache/prefer_nodes_without_cache_test.go index bc3497a689f..e2dc898855c 100644 --- a/pkg/webhook/plugins/prefernodeswithoutcache/prefer_nodes_without_cache_test.go +++ b/pkg/webhook/plugins/prefernodeswithoutcache/prefer_nodes_without_cache_test.go @@ -91,7 +91,7 @@ var _ = Describe("PreferNodesWithoutCache Plugin", func() { } }) - It("should create plugin and mutate pod correctly", func() { + It("should return early and keep pod unchanged when runtimeInfos is not empty", func() { plugin, err := NewPlugin(cl, "") Expect(err).NotTo(HaveOccurred()) Expect(plugin.GetName()).To(Equal(Name)) @@ -102,12 +102,32 @@ var _ = Describe("PreferNodesWithoutCache Plugin", func() { shouldStop, err := plugin.Mutate(pod, map[string]base.RuntimeInfoInterface{"test": runtimeInfo}) Expect(err).NotTo(HaveOccurred()) Expect(shouldStop).To(BeTrue()) + Expect(pod.Spec.Affinity).To(BeNil()) + }) + + It("should inject preferred scheduling term when runtimeInfos is empty", func() { + plugin, err := NewPlugin(cl, "") + Expect(err).NotTo(HaveOccurred()) + + shouldStop, err := plugin.Mutate(pod, map[string]base.RuntimeInfoInterface{}) + Expect(err).NotTo(HaveOccurred()) + Expect(shouldStop).To(BeTrue()) + Expect(pod.Spec.Affinity).NotTo(BeNil()) + Expect(pod.Spec.Affinity.NodeAffinity).NotTo(BeNil()) + Expect(pod.Spec.Affinity.NodeAffinity.PreferredDuringSchedulingIgnoredDuringExecution).To(HaveLen(1)) + Expect(pod.Spec.Affinity.NodeAffinity.PreferredDuringSchedulingIgnoredDuringExecution[0]).To( + Equal(getPreferredSchedulingTermForPodWithoutCache()), + ) + }) - _, err = plugin.Mutate(pod, map[string]base.RuntimeInfoInterface{}) + It("should handle nil runtime info entries without error", func() { + plugin, err := NewPlugin(cl, "") Expect(err).NotTo(HaveOccurred()) - _, err = plugin.Mutate(pod, map[string]base.RuntimeInfoInterface{"test": nil}) + shouldStop, err := plugin.Mutate(pod, map[string]base.RuntimeInfoInterface{"test": nil}) Expect(err).NotTo(HaveOccurred()) + Expect(shouldStop).To(BeTrue()) + Expect(pod.Spec.Affinity).To(BeNil()) }) }) })