From f5c8c0911306661f062a8297eac0aee7deeac6f8 Mon Sep 17 00:00:00 2001 From: Cosmin Tupangiu Date: Wed, 18 May 2022 10:46:45 +0200 Subject: add tests and fix bug when char device pass the test as block device - add test - fix bug when a character device set in a volume as a block device is seen as block device in _pkg/specgen/generate/kube/volume.go_. At this stage the type does not matter much because the devices are recreated at lower layer but the bug allowed a CharDevice volume to be passed to lower layer as a BlockDevice. Signed-off-by: Cosmin Tupangiu --- test/e2e/play_kube_test.go | 127 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 127 insertions(+) (limited to 'test') diff --git a/test/e2e/play_kube_test.go b/test/e2e/play_kube_test.go index 216c3357c..161813194 100644 --- a/test/e2e/play_kube_test.go +++ b/test/e2e/play_kube_test.go @@ -3685,4 +3685,131 @@ ENV OPENJ9_JAVA_OPTIONS=%q Expect(usernsInCtr).Should(Exit(0)) Expect(string(usernsInCtr.Out.Contents())).To(Not(Equal(string(initialUsernsConfig)))) }) + + // Check the block devices are exposed inside container + It("podman play kube expose block device inside container", func() { + SkipIfRootless("It needs root access to create devices") + Expect(os.MkdirAll("/dev/foodevdir", os.ModePerm)).To(BeNil()) + defer os.RemoveAll("/dev/foodevdir") + + devicePath := "/dev/foodevdir/blockdevice" + mknod := SystemExec("mknod", []string{devicePath, "b", "7", "0"}) + mknod.WaitWithDefaultTimeout() + Expect(mknod).Should(Exit(0)) + + blockVolume := getHostPathVolume("BlockDevice", devicePath) + + pod := getPod(withVolume(blockVolume), withCtr(getCtr(withImage(registry), withCmd(nil), withArg(nil), withVolumeMount(devicePath, false)))) + err = generateKubeYaml("pod", pod, kubeYaml) + Expect(err).To(BeNil()) + + kube := podmanTest.Podman([]string{"play", "kube", kubeYaml}) + kube.WaitWithDefaultTimeout() + Expect(kube).Should(Exit(0)) + + // Container should be in running state + inspect := podmanTest.Podman([]string{"inspect", "--format", "{{.State.Status}}", "testPod-" + defaultCtrName}) + inspect.WaitWithDefaultTimeout() + Expect(inspect).Should(Exit(0)) + Expect(inspect.OutputToString()).To(ContainSubstring("running")) + + // Container should have a block device /dev/loop1 + inspect = podmanTest.Podman([]string{"inspect", "--format", "{{.HostConfig.Devices}}", "testPod-" + defaultCtrName}) + inspect.WaitWithDefaultTimeout() + Expect(inspect).Should(Exit(0)) + Expect(inspect.OutputToString()).To(ContainSubstring(devicePath)) + }) + + // Check the char devices are exposed inside container + It("podman play kube expose character device inside container", func() { + SkipIfRootless("It needs root access to create devices") + Expect(os.MkdirAll("/dev/foodevdir", os.ModePerm)).To(BeNil()) + defer os.RemoveAll("/dev/foodevdir") + + devicePath := "/dev/foodevdir/chardevice" + mknod := SystemExec("mknod", []string{devicePath, "c", "3", "1"}) + mknod.WaitWithDefaultTimeout() + Expect(mknod).Should(Exit(0)) + + charVolume := getHostPathVolume("CharDevice", devicePath) + + pod := getPod(withVolume(charVolume), withCtr(getCtr(withImage(registry), withCmd(nil), withArg(nil), withVolumeMount(devicePath, false)))) + err = generateKubeYaml("pod", pod, kubeYaml) + Expect(err).To(BeNil()) + + kube := podmanTest.Podman([]string{"play", "kube", kubeYaml}) + kube.WaitWithDefaultTimeout() + Expect(kube).Should(Exit(0)) + + // Container should be in running state + inspect := podmanTest.Podman([]string{"inspect", "--format", "{{.State.Status}}", "testPod-" + defaultCtrName}) + inspect.WaitWithDefaultTimeout() + Expect(inspect).Should(Exit(0)) + Expect(inspect.OutputToString()).To(ContainSubstring("running")) + + // Container should have a block device /dev/loop1 + inspect = podmanTest.Podman([]string{"inspect", "--format", "{{.HostConfig.Devices}}", "testPod-" + defaultCtrName}) + inspect.WaitWithDefaultTimeout() + Expect(inspect).Should(Exit(0)) + Expect(inspect.OutputToString()).To(ContainSubstring(devicePath)) + }) + + It("podman play kube reports error when the device does not exists", func() { + SkipIfRootless("It needs root access to create devices") + + devicePath := "/dev/foodevdir/baddevice" + + blockVolume := getHostPathVolume("BlockDevice", devicePath) + + pod := getPod(withVolume(blockVolume), withCtr(getCtr(withImage(registry), withCmd(nil), withArg(nil), withVolumeMount(devicePath, false)))) + err = generateKubeYaml("pod", pod, kubeYaml) + Expect(err).To(BeNil()) + + kube := podmanTest.Podman([]string{"play", "kube", kubeYaml}) + kube.WaitWithDefaultTimeout() + Expect(kube).Should(Exit(125)) + }) + + It("podman play kube reports error when we try to expose char device as block device", func() { + SkipIfRootless("It needs root access to create devices") + Expect(os.MkdirAll("/dev/foodevdir", os.ModePerm)).To(BeNil()) + defer os.RemoveAll("/dev/foodevdir") + + devicePath := "/dev/foodevdir/chardevice" + mknod := SystemExec("mknod", []string{devicePath, "c", "3", "1"}) + mknod.WaitWithDefaultTimeout() + Expect(mknod).Should(Exit(0)) + + charVolume := getHostPathVolume("BlockDevice", devicePath) + + pod := getPod(withVolume(charVolume), withCtr(getCtr(withImage(registry), withCmd(nil), withArg(nil), withVolumeMount(devicePath, false)))) + err = generateKubeYaml("pod", pod, kubeYaml) + Expect(err).To(BeNil()) + + kube := podmanTest.Podman([]string{"play", "kube", kubeYaml}) + kube.WaitWithDefaultTimeout() + Expect(kube).Should(Exit(125)) + }) + + It("podman play kube reports error when we try to expose block device as char device", func() { + SkipIfRootless("It needs root access to create devices") + Expect(os.MkdirAll("/dev/foodevdir", os.ModePerm)).To(BeNil()) + defer os.RemoveAll("/dev/foodevdir") + + devicePath := "/dev/foodevdir/blockdevice" + mknod := SystemExec("mknod", []string{devicePath, "b", "7", "0"}) + mknod.WaitWithDefaultTimeout() + Expect(mknod).Should(Exit(0)) + + charVolume := getHostPathVolume("CharDevice", devicePath) + + pod := getPod(withVolume(charVolume), withCtr(getCtr(withImage(registry), withCmd(nil), withArg(nil), withVolumeMount(devicePath, false)))) + err = generateKubeYaml("pod", pod, kubeYaml) + Expect(err).To(BeNil()) + + kube := podmanTest.Podman([]string{"play", "kube", kubeYaml}) + kube.WaitWithDefaultTimeout() + Expect(kube).Should(Exit(125)) + }) + }) -- cgit v1.2.3-54-g00ecf From 4960a17a56523c0c022992e841262f89312db694 Mon Sep 17 00:00:00 2001 From: Cosmin Tupangiu Date: Mon, 23 May 2022 10:33:12 +0200 Subject: fix tests by randomize the device folder name e2e tests tends to fail when running with multiple nodes because the same device folder name is used accross all nodes Signed-off-by: Cosmin Tupangiu --- test/e2e/play_kube_test.go | 52 ++++++++++++++++++++++++++++++++-------------- 1 file changed, 36 insertions(+), 16 deletions(-) (limited to 'test') diff --git a/test/e2e/play_kube_test.go b/test/e2e/play_kube_test.go index 161813194..31044f68b 100644 --- a/test/e2e/play_kube_test.go +++ b/test/e2e/play_kube_test.go @@ -21,6 +21,7 @@ import ( "github.com/containers/podman/v4/pkg/util" . "github.com/containers/podman/v4/test/utils" "github.com/containers/storage/pkg/stringid" + "github.com/google/uuid" . "github.com/onsi/ginkgo" . "github.com/onsi/gomega" "github.com/onsi/gomega/format" @@ -3687,12 +3688,17 @@ ENV OPENJ9_JAVA_OPTIONS=%q }) // Check the block devices are exposed inside container - It("podman play kube expose block device inside container", func() { + It("ddpodman play kube expose block device inside container", func() { SkipIfRootless("It needs root access to create devices") - Expect(os.MkdirAll("/dev/foodevdir", os.ModePerm)).To(BeNil()) - defer os.RemoveAll("/dev/foodevdir") - devicePath := "/dev/foodevdir/blockdevice" + // randomize the folder name to avoid error when running tests with multiple nodes + uuid, err := uuid.NewUUID() + Expect(err).To(BeNil()) + devFolder := fmt.Sprintf("/dev/foodev%x", uuid[:6]) + Expect(os.MkdirAll(devFolder, os.ModePerm)).To(BeNil()) + defer os.RemoveAll(devFolder) + + devicePath := fmt.Sprintf("%s/blockdevice", devFolder) mknod := SystemExec("mknod", []string{devicePath, "b", "7", "0"}) mknod.WaitWithDefaultTimeout() Expect(mknod).Should(Exit(0)) @@ -3721,12 +3727,17 @@ ENV OPENJ9_JAVA_OPTIONS=%q }) // Check the char devices are exposed inside container - It("podman play kube expose character device inside container", func() { + It("ddpodman play kube expose character device inside container", func() { SkipIfRootless("It needs root access to create devices") - Expect(os.MkdirAll("/dev/foodevdir", os.ModePerm)).To(BeNil()) - defer os.RemoveAll("/dev/foodevdir") - devicePath := "/dev/foodevdir/chardevice" + // randomize the folder name to avoid error when running tests with multiple nodes + uuid, err := uuid.NewUUID() + Expect(err).To(BeNil()) + devFolder := fmt.Sprintf("/dev/foodev%x", uuid[:6]) + Expect(os.MkdirAll(devFolder, os.ModePerm)).To(BeNil()) + defer os.RemoveAll(devFolder) + + devicePath := fmt.Sprintf("%s/chardevice", devFolder) mknod := SystemExec("mknod", []string{devicePath, "c", "3", "1"}) mknod.WaitWithDefaultTimeout() Expect(mknod).Should(Exit(0)) @@ -3770,12 +3781,17 @@ ENV OPENJ9_JAVA_OPTIONS=%q Expect(kube).Should(Exit(125)) }) - It("podman play kube reports error when we try to expose char device as block device", func() { + It("ddpodman play kube reports error when we try to expose char device as block device", func() { SkipIfRootless("It needs root access to create devices") - Expect(os.MkdirAll("/dev/foodevdir", os.ModePerm)).To(BeNil()) - defer os.RemoveAll("/dev/foodevdir") - devicePath := "/dev/foodevdir/chardevice" + // randomize the folder name to avoid error when running tests with multiple nodes + uuid, err := uuid.NewUUID() + Expect(err).To(BeNil()) + devFolder := fmt.Sprintf("/dev/foodev%x", uuid[:6]) + Expect(os.MkdirAll(devFolder, os.ModePerm)).To(BeNil()) + defer os.RemoveAll(devFolder) + + devicePath := fmt.Sprintf("%s/chardevice", devFolder) mknod := SystemExec("mknod", []string{devicePath, "c", "3", "1"}) mknod.WaitWithDefaultTimeout() Expect(mknod).Should(Exit(0)) @@ -3791,12 +3807,16 @@ ENV OPENJ9_JAVA_OPTIONS=%q Expect(kube).Should(Exit(125)) }) - It("podman play kube reports error when we try to expose block device as char device", func() { + It("ddpodman play kube reports error when we try to expose block device as char device", func() { SkipIfRootless("It needs root access to create devices") - Expect(os.MkdirAll("/dev/foodevdir", os.ModePerm)).To(BeNil()) - defer os.RemoveAll("/dev/foodevdir") - devicePath := "/dev/foodevdir/blockdevice" + // randomize the folder name to avoid error when running tests with multiple nodes + uuid, err := uuid.NewUUID() + Expect(err).To(BeNil()) + devFolder := fmt.Sprintf("/dev/foodev%x", uuid[:6]) + Expect(os.MkdirAll(devFolder, os.ModePerm)).To(BeNil()) + + devicePath := fmt.Sprintf("%s/blockdevice", devFolder) mknod := SystemExec("mknod", []string{devicePath, "b", "7", "0"}) mknod.WaitWithDefaultTimeout() Expect(mknod).Should(Exit(0)) -- cgit v1.2.3-54-g00ecf