Skip to content

Commit 523d703

Browse files
authored
test(prefernodeswithoutcache): extract repeated expectTerm helper (#5758)
* test(prefernodeswithoutcache): extract repeated expectTerm into file-local helper Signed-off-by: Harsh <harshmastic@gmail.com> (cherry picked from commit f7b499ce4c430c3f714d3165f7efe55ddac01f98) * test(prefernodeswithoutcache): assert mutate injects preferred term Signed-off-by: Harsh <harshmastic@gmail.com> * test(prefernodeswithoutcache): assert non-mutating mutate paths Signed-off-by: Harsh <harshmastic@gmail.com> * test(prefernodeswithoutcache): remove dead selector setup Signed-off-by: Harsh <harshmastic@gmail.com> * test(prefernodeswithoutcache): drop redundant getter spec Signed-off-by: Harsh <harshmastic@gmail.com> --------- Signed-off-by: Harsh <harshmastic@gmail.com>
1 parent 7c3bde6 commit 523d703

1 file changed

Lines changed: 30 additions & 43 deletions

File tree

pkg/webhook/plugins/prefernodeswithoutcache/prefer_nodes_without_cache_test.go

Lines changed: 30 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -26,52 +26,30 @@ import (
2626
"sigs.k8s.io/controller-runtime/pkg/client"
2727
)
2828

29+
// expectedNoDatasetTerm is the canonical PreferredSchedulingTerm asserted across
30+
// multiple specs for getPreferredSchedulingTermForPodWithoutCache.
31+
func expectedNoDatasetTerm() corev1.PreferredSchedulingTerm {
32+
return corev1.PreferredSchedulingTerm{
33+
Weight: 100,
34+
Preference: corev1.NodeSelectorTerm{
35+
MatchExpressions: []corev1.NodeSelectorRequirement{
36+
{
37+
Key: common.GetDatasetNumLabelName(),
38+
Operator: corev1.NodeSelectorOpDoesNotExist,
39+
},
40+
},
41+
},
42+
}
43+
}
44+
2945
var _ = Describe("PreferNodesWithoutCache Plugin", func() {
3046
Describe("getPreferredSchedulingTermForPodWithoutCache", func() {
31-
It("should return correct PreferredSchedulingTerm with selector enabled and disabled", func() {
32-
runtimeInfo, err := base.BuildRuntimeInfo("test", "fluid", "alluxio")
33-
Expect(err).NotTo(HaveOccurred())
34-
35-
runtimeInfo.SetFuseNodeSelector(map[string]string{"test1": "test1"})
47+
It("should return the same PreferredSchedulingTerm on repeated calls", func() {
3648
term := getPreferredSchedulingTermForPodWithoutCache()
49+
Expect(term).To(Equal(expectedNoDatasetTerm()))
3750

38-
expectTerm := corev1.PreferredSchedulingTerm{
39-
Weight: 100,
40-
Preference: corev1.NodeSelectorTerm{
41-
MatchExpressions: []corev1.NodeSelectorRequirement{
42-
{
43-
Key: common.GetDatasetNumLabelName(),
44-
Operator: corev1.NodeSelectorOpDoesNotExist,
45-
},
46-
},
47-
},
48-
}
49-
Expect(term).To(Equal(expectTerm))
50-
51-
runtimeInfo.SetFuseNodeSelector(map[string]string{})
5251
term = getPreferredSchedulingTermForPodWithoutCache()
53-
Expect(term).To(Equal(expectTerm))
54-
})
55-
56-
It("should return correct PreferredSchedulingTerm with default mode", func() {
57-
runtimeInfo, err := base.BuildRuntimeInfo("test", "fluid", "alluxio")
58-
Expect(err).NotTo(HaveOccurred())
59-
60-
runtimeInfo.SetFuseNodeSelector(map[string]string{})
61-
term := getPreferredSchedulingTermForPodWithoutCache()
62-
63-
expectTerm := corev1.PreferredSchedulingTerm{
64-
Weight: 100,
65-
Preference: corev1.NodeSelectorTerm{
66-
MatchExpressions: []corev1.NodeSelectorRequirement{
67-
{
68-
Key: common.GetDatasetNumLabelName(),
69-
Operator: corev1.NodeSelectorOpDoesNotExist,
70-
},
71-
},
72-
},
73-
}
74-
Expect(term).To(Equal(expectTerm))
52+
Expect(term).To(Equal(expectedNoDatasetTerm()))
7553
})
7654
})
7755

@@ -102,12 +80,21 @@ var _ = Describe("PreferNodesWithoutCache Plugin", func() {
10280
shouldStop, err := plugin.Mutate(pod, map[string]base.RuntimeInfoInterface{"test": runtimeInfo})
10381
Expect(err).NotTo(HaveOccurred())
10482
Expect(shouldStop).To(BeTrue())
83+
Expect(pod.Spec.Affinity).To(BeNil())
10584

106-
_, err = plugin.Mutate(pod, map[string]base.RuntimeInfoInterface{})
85+
shouldStop, err = plugin.Mutate(pod, map[string]base.RuntimeInfoInterface{})
10786
Expect(err).NotTo(HaveOccurred())
87+
Expect(shouldStop).To(BeTrue())
88+
Expect(pod.Spec.Affinity).NotTo(BeNil())
89+
Expect(pod.Spec.Affinity.NodeAffinity).NotTo(BeNil())
90+
Expect(pod.Spec.Affinity.NodeAffinity.PreferredDuringSchedulingIgnoredDuringExecution).To(Equal([]corev1.PreferredSchedulingTerm{expectedNoDatasetTerm()}))
10891

109-
_, err = plugin.Mutate(pod, map[string]base.RuntimeInfoInterface{"test": nil})
92+
pod.Spec.Affinity = nil
93+
94+
shouldStop, err = plugin.Mutate(pod, map[string]base.RuntimeInfoInterface{"test": nil})
11095
Expect(err).NotTo(HaveOccurred())
96+
Expect(shouldStop).To(BeTrue())
97+
Expect(pod.Spec.Affinity).To(BeNil())
11198
})
11299
})
113100
})

0 commit comments

Comments
 (0)