From ca20c42a521b977e54bb4790d80473f4e9a8449e Mon Sep 17 00:00:00 2001 From: Giuseppe Scrivano Date: Tue, 27 May 2025 12:15:24 +0200 Subject: [PATCH] test: fix race conditions in /dev/kmsg tests The e2e tests for device access involving /dev/kmsg could fail intermittently. This was due to a race condition where concurrent writes to the kernel log buffer by other processes, while the test was reading from /dev/kmsg, could cause the read to fail with ESPIPE. Fixes: https://github.com/containers/podman/issues/23882 Signed-off-by: Giuseppe Scrivano --- test/e2e/run_device_test.go | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/test/e2e/run_device_test.go b/test/e2e/run_device_test.go index 62ff9b42d3..b0c6158003 100644 --- a/test/e2e/run_device_test.go +++ b/test/e2e/run_device_test.go @@ -50,7 +50,10 @@ var _ = Describe("Podman run device", func() { Expect(session).Should(ExitCleanly()) if !isRootless() { // Kernel 6.9.0 (2024-03) requires SYSLOG - session = podmanTest.Podman([]string{"run", "-q", "--security-opt", "label=disable", "--device", "/dev/kmsg", "--cap-add", "SYS_ADMIN,SYSLOG", ALPINE, "head", "-n", "1", "/dev/kmsg"}) + // Do not read any data from the device (thus the -n 0), because there is a rare race condition that happens + // when another writes to the ring buffer when the device is already opened and that causes the following test + // to fail: https://github.com/containers/podman/issues/23882. + session = podmanTest.Podman([]string{"run", "-q", "--security-opt", "label=disable", "--device", "/dev/kmsg", "--cap-add", "SYS_ADMIN,SYSLOG", ALPINE, "head", "-n", "0", "/dev/kmsg"}) session.WaitWithDefaultTimeout() Expect(session).Should(ExitCleanly()) } @@ -152,9 +155,12 @@ var _ = Describe("Podman run device", func() { }) It("podman run cannot access non default devices", func() { - session := podmanTest.Podman([]string{"run", "-v /dev:/dev-host", ALPINE, "head", "-1", "/dev-host/kmsg"}) + // Unlikely to happen but do not read any data from the device (thus the -n 0), because there is a rare + // race condition that happens so the test would fail for the rare race condition instead of a failure + // reported by open(). More details: https://github.com/containers/podman/issues/23882. + session := podmanTest.Podman([]string{"run", "-v", "/dev:/dev-host", ALPINE, "head", "-n0", "/dev-host/kmsg"}) session.WaitWithDefaultTimeout() - Expect(session).Should(Not(ExitCleanly())) + Expect(session).To(ExitWithErrorRegex(1, "head: /dev-host/kmsg: (Operation not permitted|Permission denied)")) }) })