]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
test: skip several test cases when running in chroot
authorYu Watanabe <watanabe.yu+github@gmail.com>
Tue, 9 Sep 2025 06:15:02 +0000 (15:15 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 17 Sep 2025 13:20:42 +0000 (22:20 +0900)
When we are running in chroot, safe_fork() with FORK_MOUNTNS_SLAVE fails
with the following eror:
```
Failed to set mount propagation to MS_SLAVE for all mounts: Invalid argument
```
Let's skip the test cases when we are running in chroot.

src/libsystemd/sd-device/test-sd-device.c
src/test/test-execute.c
src/test/test-mount-util.c
src/test/test-mountpoint-util.c

index e1ee9e433fbce74eeb5d8f1a0ac425d2dd03d1e0..dc4a32e8a188f0e3b72a744f8e70afe3937fac87 100644 (file)
@@ -5,6 +5,7 @@
 
 #include "sd-event.h"
 
+#include "capability-util.h"
 #include "device-internal.h"
 #include "device-private.h"
 #include "device-util.h"
 #include "tests.h"
 #include "tmpfile-util.h"
 #include "udev-util.h"
+#include "virt.h"
 
 TEST(mdio_bus) {
         int r;
 
         /* For issue #37711 */
 
-        if (getuid() != 0)
-                return (void) log_tests_skipped("not running as root");
+        if (getuid() != 0 || have_effective_cap(CAP_SYS_ADMIN) <= 0)
+                return (void) log_tests_skipped("Not privileged");
+        if (running_in_chroot() > 0)
+                return (void) log_tests_skipped("Running in chroot");
 
         ASSERT_OK(r = safe_fork("(mdio_bus)", FORK_CLOSE_ALL_FDS|FORK_DEATHSIG_SIGTERM|FORK_REOPEN_LOG|FORK_LOG|FORK_WAIT|FORK_NEW_MOUNTNS|FORK_MOUNTNS_SLAVE, NULL));
         if (r == 0) {
index 4dad93cfb4f6ab8e68ddbab0256273edd6208230..916ade79606cf72f0d04b434e5d664b0213f0a1a 100644 (file)
@@ -1613,6 +1613,9 @@ static int intro(void) {
         if (geteuid() != 0 || have_effective_cap(CAP_SYS_ADMIN) <= 0)
                 return log_tests_skipped("not privileged");
 
+        if (running_in_chroot() > 0)
+                return log_tests_skipped("running in chroot");
+
         if (enter_cgroup_subroot(NULL) == -ENOMEDIUM)
                 return log_tests_skipped("cgroupfs not available");
 
index a83de3f2b00657f750036f6655c47bb74b569d97..57d9a050a5cb1add4455992b8e51cfc4b96094e7 100644 (file)
@@ -23,6 +23,7 @@
 #include "strv.h"
 #include "tests.h"
 #include "tmpfile-util.h"
+#include "virt.h"
 
 #define FORK_COMMON_FLAGS                       \
         (FORK_CLOSE_ALL_FDS |                   \
@@ -36,7 +37,9 @@
 
 #define CHECK_PRIV                                                      \
         if (geteuid() != 0 || have_effective_cap(CAP_SYS_ADMIN) <= 0)   \
-                return (void) log_tests_skipped("Not privileged");
+                return (void) log_tests_skipped("Not privileged");      \
+        if (running_in_chroot() > 0)                                    \
+                return (void) log_tests_skipped("running in chroot");
 
 TEST(mount_option_mangle) {
         char *opts = NULL;
index ceb59ce4555e5250e14df93de43c30cc7f9de34e..1da10e9ccc6e2303ff03d4864e3030288acc0f88 100644 (file)
@@ -18,6 +18,7 @@
 #include "string-util.h"
 #include "tests.h"
 #include "tmpfile-util.h"
+#include "virt.h"
 
 static void test_mount_propagation_flag_one(const char *name, int ret, unsigned long expected) {
         unsigned long flags;
@@ -455,6 +456,12 @@ static int intro(void) {
         /* let's move into our own mount namespace with all propagation from the host turned off, so
          * that /proc/self/mountinfo is static and constant for the whole time our test runs. */
 
+        if (running_in_chroot() > 0) {
+                /* We cannot remount file system with MS_PRIVATE when running in chroot. */
+                log_notice("Running in chroot, proceeding in originating mount namespace.");
+                return EXIT_SUCCESS;
+        }
+
         if (unshare(CLONE_NEWNS) < 0) {
                 if (!ERRNO_IS_PRIVILEGE(errno))
                         return log_error_errno(errno, "Failed to detach mount namespace: %m");