Skip to content

Commit d1ef847

Browse files
committed
fix: pod webhook unit tests
1 parent 84abd8e commit d1ef847

File tree

1 file changed

+69
-41
lines changed

1 file changed

+69
-41
lines changed

pkg/webhooks/pod/pod_test.go

Lines changed: 69 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -50,57 +50,55 @@ var _ = Describe("Webhook", func() {
5050
}
5151
)
5252

53-
Context("1 - Check the new created toleration according to the PodOffloadingStrategy", func() {
53+
Context("Check the new created toleration according to the PodOffloadingStrategy", func() {
5454
emptyToleration := corev1.Toleration{}
55-
DescribeTable("3 Different type of PodOffloadingStrategy",
55+
DescribeTable("Test for each PodOffloadingStrategy",
5656
func(strategy offloadingv1beta1.PodOffloadingStrategyType, expectedToleration corev1.Toleration) {
5757
By(fmt.Sprintf("Testing %s", strategy))
5858
toleration, err := createTolerationFromNamespaceOffloading(strategy)
59-
if strategy == offloadingv1beta1.LocalPodOffloadingStrategyType {
59+
if strategy == "" {
6060
Expect(err).To(HaveOccurred())
6161
} else {
6262
Expect(err).ToNot(HaveOccurred())
6363
}
6464
Expect(toleration.MatchToleration(&expectedToleration)).To(BeTrue())
6565
},
66+
Entry("", offloadingv1beta1.LocalPodOffloadingStrategyType, emptyToleration),
6667
Entry("LocalPodOffloadingStrategyType", offloadingv1beta1.LocalPodOffloadingStrategyType, emptyToleration),
6768
Entry("RemotePodOffloadingStrategyType", offloadingv1beta1.RemotePodOffloadingStrategyType, virtualNodeToleration),
6869
Entry("LocalAndRemotePodOffloadingStrategyType", offloadingv1beta1.LocalAndRemotePodOffloadingStrategyType, virtualNodeToleration),
6970
)
7071
})
7172

72-
Context("2 - Check the NodeSelector imposed by the NamespaceOffloading", func() {
73-
// slice with 3 namespaceOffloading one for each PodOffloadingStrategy
74-
namespaceOffloadings := []offloadingv1beta1.NamespaceOffloading{
75-
testutils.GetNamespaceOffloading(offloadingv1beta1.LocalPodOffloadingStrategyType),
76-
testutils.GetNamespaceOffloading(offloadingv1beta1.RemotePodOffloadingStrategyType),
77-
testutils.GetNamespaceOffloading(offloadingv1beta1.LocalAndRemotePodOffloadingStrategyType),
78-
}
73+
Context("Check the NodeSelector imposed by the NamespaceOffloading", func() {
74+
localNamespaceOffloading := testutils.GetNamespaceOffloading(offloadingv1beta1.LocalPodOffloadingStrategyType)
75+
remoteNamespaceOffloading := testutils.GetNamespaceOffloading(offloadingv1beta1.RemotePodOffloadingStrategyType)
76+
localAndRemoteNamespaceOffloading := testutils.GetNamespaceOffloading(offloadingv1beta1.LocalAndRemotePodOffloadingStrategyType)
7977

80-
nodeSelectors := []corev1.NodeSelector{
81-
{},
82-
testutils.GetImposedNodeSelector(offloadingv1beta1.RemotePodOffloadingStrategyType),
83-
testutils.GetImposedNodeSelector(offloadingv1beta1.LocalAndRemotePodOffloadingStrategyType),
84-
}
85-
DescribeTable("3 Different type of PodOffloadingStrategy",
86-
func(namespaceOffloading offloadingv1beta1.NamespaceOffloading, expectedNodeSelector corev1.NodeSelector) {
78+
remoteNodeSelector := testutils.GetImposedNodeSelector(offloadingv1beta1.RemotePodOffloadingStrategyType)
79+
localAndRemoteNodeSelector := testutils.GetImposedNodeSelector(offloadingv1beta1.LocalAndRemotePodOffloadingStrategyType)
80+
81+
DescribeTable("Test for each PodOffloadingStrategy",
82+
func(namespaceOffloading offloadingv1beta1.NamespaceOffloading, expectedNodeSelector *corev1.NodeSelector) {
8783
By(fmt.Sprintf("Testing %s", namespaceOffloading.Spec.PodOffloadingStrategy))
8884
nodeSelector, err := createNodeSelectorFromNamespaceOffloading(&namespaceOffloading)
89-
if namespaceOffloading.Spec.PodOffloadingStrategy == offloadingv1beta1.LocalPodOffloadingStrategyType {
90-
Expect(nodeSelector).To(BeNil())
91-
Expect(err).To(HaveOccurred())
85+
Expect(err).ToNot(HaveOccurred())
86+
if expectedNodeSelector == nil {
87+
Expect(nodeSelector).To(BeNil(), "Expected the node selectors to be nil")
9288
} else {
93-
Expect(err).ToNot(HaveOccurred())
94-
Expect(nodeSelector).To(PointTo(Equal(expectedNodeSelector)))
89+
Expect(nodeSelector).To(
90+
PointTo(Equal(*expectedNodeSelector)),
91+
"Node selectors are not the expected ones",
92+
)
9593
}
9694
},
97-
Entry("LocalPodOffloadingStrategyType", namespaceOffloadings[0], nodeSelectors[0]),
98-
Entry("RemotePodOffloadingStrategyType", namespaceOffloadings[1], nodeSelectors[1]),
99-
Entry("LocalAndRemotePodOffloadingStrategyType", namespaceOffloadings[2], nodeSelectors[2]),
95+
Entry("LocalPodOffloadingStrategyType", localNamespaceOffloading, nil),
96+
Entry("RemotePodOffloadingStrategyType", remoteNamespaceOffloading, &remoteNodeSelector),
97+
Entry("LocalAndRemotePodOffloadingStrategyType", localAndRemoteNamespaceOffloading, &localAndRemoteNodeSelector),
10098
)
10199
})
102100

103-
Context("3 - Check if the pod NodeSelector is correctly merged with the NamespaceOffloading NodeSelector", func() {
101+
Context("Check if the pod NodeSelector is correctly merged with the NamespaceOffloading NodeSelector", func() {
104102
It("Check the merged NodeSelector", func() {
105103
podNodeSelector := testutils.GetPodNodeSelector()
106104
imposedNodeSelector := testutils.GetImposedNodeSelector("")
@@ -113,7 +111,7 @@ var _ = Describe("Webhook", func() {
113111
Context("4 - Check how the new NodeSelector is inserted into the pod", func() {
114112
// imposedNodeSelector that all Pod without NodeAffinity specified by user must have
115113
imposedNodeSelector := testutils.GetImposedNodeSelector("")
116-
// mergedNodeSelector is a merge of NodeSelector specified in NamespaceOffloading and NodeSelector
114+
// mergedNodeSelector is a merge of the NodeSelectors specified in NamespaceOffloading and NodeSelector
117115
// specified by the user
118116
mergedNodeSelector := testutils.GetMergedNodeSelector("")
119117
// A fake PodAffinity to test if it is preserved.
@@ -236,7 +234,7 @@ var _ = Describe("Webhook", func() {
236234
})
237235
})
238236

239-
Context("5 - Call the mutatePod function and observe the pod is correctly mutated", func() {
237+
Context("Test the mutatePod function and observe the pod is correctly mutated", func() {
240238

241239
podNodeSelector := testutils.GetPodNodeSelector()
242240
pod := corev1.Pod{
@@ -260,36 +258,66 @@ var _ = Describe("Webhook", func() {
260258
},
261259
}
262260

263-
It("Check the toleration added and the new NodeSelector", func() {
261+
It("Check that pod is mutated according to LocalAndRemote policy", func() {
264262
namespaceOffloading := testutils.GetNamespaceOffloading(offloadingv1beta1.LocalAndRemotePodOffloadingStrategyType)
265263
podTest := pod.DeepCopy()
266-
err := mutatePod(&namespaceOffloading, podTest, true)
264+
err := mutatePod(&namespaceOffloading, podTest, "liqo")
267265
Expect(err).ToNot(HaveOccurred())
268-
Expect(len(podTest.Spec.Tolerations) == 2).To(BeTrue())
269-
Expect(podTest.Spec.Tolerations[1].MatchToleration(&virtualNodeToleration)).To(BeTrue())
266+
Expect(len(podTest.Spec.Tolerations)).To(Equal(2), "Unexpected number of tolerations")
267+
Expect(podTest.Spec.Tolerations[1].MatchToleration(&virtualNodeToleration)).To(
268+
BeTrue(),
269+
"Added tolerations do not match the expected one",
270+
)
270271
mergedNodeSelector := testutils.GetMergedNodeSelector(offloadingv1beta1.LocalAndRemotePodOffloadingStrategyType)
271-
Expect(*podTest.Spec.Affinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution).To(Equal(mergedNodeSelector))
272+
Expect(*podTest.Spec.Affinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution).To(
273+
Equal(mergedNodeSelector),
274+
"Node affinities do not match the expected ones",
275+
)
272276
})
273277

274278
It("With LocalPodOffloadingStrategy check that pod is not mutated ", func() {
275279
namespaceOffloading := testutils.GetNamespaceOffloading(offloadingv1beta1.LocalPodOffloadingStrategyType)
276280
podTest := pod.DeepCopy()
277281
oldPodNodeSelector := *podTest.Spec.Affinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution
278-
err := mutatePod(&namespaceOffloading, podTest, true)
282+
err := mutatePod(&namespaceOffloading, podTest, "liqo")
283+
Expect(err).ToNot(HaveOccurred())
284+
Expect(podTest.Spec.Tolerations).To(Equal(pod.Spec.Tolerations), "No changes expected in tolerations")
285+
Expect(*podTest.Spec.Affinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution).To(
286+
Equal(oldPodNodeSelector),
287+
"Node affinities do not match the expected ones",
288+
)
289+
})
290+
291+
It("Checks that pod is not mutated when the Liqo runtime class is defined ", func() {
292+
namespaceOffloading := testutils.GetNamespaceOffloading(offloadingv1beta1.LocalAndRemotePodOffloadingStrategyType)
293+
podTest := pod.DeepCopy()
294+
runtimeClassName := "my-liqo-runtime"
295+
podTest.Spec.RuntimeClassName = &runtimeClassName
296+
297+
err := mutatePod(&namespaceOffloading, podTest, runtimeClassName)
279298
Expect(err).ToNot(HaveOccurred())
280-
Expect(len(podTest.Spec.Tolerations) == 1).To(BeTrue())
281-
Expect(*podTest.Spec.Affinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution).To(Equal(oldPodNodeSelector))
299+
Expect(podTest.Spec.Tolerations).To(Equal(pod.Spec.Tolerations), "No changes expected in tolerations")
300+
Expect(*podTest.Spec.Affinity).To(Equal(*pod.Spec.Affinity), "No changes expected in node affinities")
282301
})
283302

284-
It("With disable add toleration check that pod is mutated ", func() {
303+
It("Checks that pod is mutated when a runtime class different than Liqo is defined ", func() {
285304
namespaceOffloading := testutils.GetNamespaceOffloading(offloadingv1beta1.LocalAndRemotePodOffloadingStrategyType)
286305
podTest := pod.DeepCopy()
287-
err := mutatePod(&namespaceOffloading, podTest, false)
306+
runtimeClassName := "my-custom-runtime"
307+
podTest.Spec.RuntimeClassName = &runtimeClassName
308+
309+
err := mutatePod(&namespaceOffloading, podTest, "liqo")
288310
Expect(err).ToNot(HaveOccurred())
289-
Expect(len(podTest.Spec.Tolerations)).To(BeNumerically("==", 1))
290-
Expect(podTest.Spec.Tolerations[0].MatchToleration(&pod.Spec.Tolerations[0])).To(BeTrue())
311+
Expect(len(podTest.Spec.Tolerations)).To(Equal(2), "Unexpected number of tolerations")
312+
Expect(podTest.Spec.Tolerations[1].MatchToleration(&virtualNodeToleration)).To(
313+
BeTrue(),
314+
"Added tolerations do not match the expected ones",
315+
)
291316
mergedNodeSelector := testutils.GetMergedNodeSelector(offloadingv1beta1.LocalAndRemotePodOffloadingStrategyType)
292-
Expect(*podTest.Spec.Affinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution).To(Equal(mergedNodeSelector))
317+
Expect(*podTest.Spec.Affinity.NodeAffinity.RequiredDuringSchedulingIgnoredDuringExecution).To(
318+
Equal(mergedNodeSelector),
319+
"Node affinities do not match the expected ones",
320+
)
293321
})
294322
})
295323
})

0 commit comments

Comments
 (0)