From 6fed7e3b9e3f34544fcb6089061938a5e36817b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Burak=20Varl=C4=B1?= Date: Tue, 26 Nov 2024 16:46:51 +0000 Subject: [PATCH] Add test-cases to ensure we don't spawn Mountpoint Pods for different volume types MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: Burak Varlı --- tests/controller/controller_test.go | 70 +++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) diff --git a/tests/controller/controller_test.go b/tests/controller/controller_test.go index cc2c5eb..b5b92df 100644 --- a/tests/controller/controller_test.go +++ b/tests/controller/controller_test.go @@ -361,6 +361,50 @@ var _ = Describe("Mountpoint Controller", func() { }) }) + Context("Different Volume Types", func() { + It("should not schedule a Mountpoint Pod if the Pod only uses an emptyDir volume", func() { + pod := createPod(withVolume("empty-dir", corev1.VolumeSource{ + EmptyDir: &corev1.EmptyDirVolumeSource{}, + })) + pod.schedule("test-node") + + expectNoMountpointPodForWorkloadPod(pod) + }) + + It("should not schedule a Mountpoint Pod if the Pod only uses a hostPath volume", func() { + pod := createPod(withVolume("host-path", corev1.VolumeSource{ + HostPath: &corev1.HostPathVolumeSource{ + Path: "/tmp/mountpoint-test", + Type: ptr.To(corev1.HostPathDirectoryOrCreate), + }, + })) + pod.schedule("test-node") + + expectNoMountpointPodForWorkloadPod(pod) + }) + + It("should not schedule a Mountpoint Pod if the Pod only different volume-types/CSI-drivers", func() { + vol := createVolume(withCSIDriver(ebsCSIDriver)) + vol.bind() + + pod := createPod( + withPVC(vol.pvc), + withVolume("host-path", corev1.VolumeSource{ + HostPath: &corev1.HostPathVolumeSource{ + Path: "/tmp/mountpoint-test", + Type: ptr.To(corev1.HostPathDirectoryOrCreate), + }, + }), + withVolume("empty-dir", corev1.VolumeSource{ + EmptyDir: &corev1.EmptyDirVolumeSource{}, + }), + ) + pod.schedule("test-node") + + expectNoMountpointPodForWorkloadPod(pod) + }) + }) + Context("Mountpoint Pod Management", func() { It("should delete completed Mountpoint Pods", func() { vol := createVolume() @@ -468,6 +512,16 @@ func withPVC(pvc *corev1.PersistentVolumeClaim) podModifier { } } +// withVolume returns a `podModifier` that adds given volume to the Pods volumes. +func withVolume(name string, vol corev1.VolumeSource) podModifier { + return func(pod *corev1.Pod) { + pod.Spec.Volumes = append(pod.Spec.Volumes, corev1.Volume{ + Name: name, + VolumeSource: vol, + }) + } +} + // A podModifier is a function for modifying Pod to be created. type podModifier func(*corev1.Pod) @@ -607,6 +661,22 @@ func expectNoMountpointPodFor(pod *testPod, vol *testVolume) { }}) } +// expectNoMountpointPodForWorkloadPod verifies that there is no Mountpoint Pod scheduled for given `pod`. +// `expectNoMountpointPodFor` is preferable to this method if the `vol` is known as this performs a slower list operation. +func expectNoMountpointPodForWorkloadPod(pod *testPod) { + Consistently(func(g Gomega) { + podList := &corev1.PodList{} + g.Expect(k8sClient.List(ctx, podList, + client.InNamespace(mountpointNamespace), client.MatchingLabels{ + mppod.LabelPodUID: string(pod.UID), + }, + )).To(Succeed()) + + g.Expect(podList.Items).To(BeEmpty(), "Expected empty list but got: %#v", podList) + g.Expect(podList.Continue).To(BeEmpty(), "Continue token on list must be empty but got: %s", podList.Continue) + }, defaultWaitTimeout/2, defaultWaitTimeout/4).Should(Succeed()) +} + // waitAndVerifyMountpointPodFor waits and verifies Mountpoint Pod scheduled for given `pod` and `vol.` func waitAndVerifyMountpointPodFor(pod *testPod, vol *testVolume) { mountpointPod := waitForMountpointPodFor(pod, vol)