From: Daan De Meyer Date: Tue, 19 Dec 2023 15:03:06 +0000 (+0100) Subject: test: Skip various tests when /sys is not mounted X-Git-Tag: v256-rc1~1456^2 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=refs%2Fpull%2F30527%2Fhead;p=thirdparty%2Fsystemd.git test: Skip various tests when /sys is not mounted When running tests in a container, /sys might not be mounted, so let's make sure we skip tests that depend on /sys in this case. --- diff --git a/src/libsystemd/sd-bus/test-bus-creds.c b/src/libsystemd/sd-bus/test-bus-creds.c index 13801becc90..d18ce88a256 100644 --- a/src/libsystemd/sd-bus/test-bus-creds.c +++ b/src/libsystemd/sd-bus/test-bus-creds.c @@ -12,7 +12,7 @@ int main(int argc, char *argv[]) { test_setup_logging(LOG_DEBUG); - if (cg_unified() == -ENOMEDIUM) + if (IN_SET(cg_unified(), -ENOMEDIUM, -ENOENT)) return log_tests_skipped("/sys/fs/cgroup/ not available"); r = sd_bus_creds_new_from_pid(&creds, 0, _SD_BUS_CREDS_ALL); diff --git a/src/libsystemd/sd-device/test-sd-device-monitor.c b/src/libsystemd/sd-device/test-sd-device-monitor.c index e124e0021c3..4f80c4e5794 100644 --- a/src/libsystemd/sd-device/test-sd-device-monitor.c +++ b/src/libsystemd/sd-device/test-sd-device-monitor.c @@ -10,6 +10,7 @@ #include "device-private.h" #include "device-util.h" #include "macro.h" +#include "mountpoint-util.h" #include "path-util.h" #include "stat-util.h" #include "string-util.h" @@ -298,6 +299,9 @@ int main(int argc, char *argv[]) { if (getuid() != 0) return log_tests_skipped("not root"); + if (path_is_mount_point("/sys", NULL, 0) <= 0) + return log_tests_skipped("/sys is not mounted"); + if (path_is_read_only_fs("/sys") > 0) return log_tests_skipped("Running in container"); diff --git a/src/libsystemd/sd-device/test-sd-device-thread.c b/src/libsystemd/sd-device/test-sd-device-thread.c index c99d179b335..539dabd7a6b 100644 --- a/src/libsystemd/sd-device/test-sd-device-thread.c +++ b/src/libsystemd/sd-device/test-sd-device-thread.c @@ -8,6 +8,7 @@ #include "sd-device.h" #include "device-util.h" +#include "tests.h" #define handle_error_errno(error, msg) \ ({ \ @@ -30,6 +31,8 @@ int main(int argc, char *argv[]) { int r; r = sd_device_new_from_syspath(&loopback, "/sys/class/net/lo"); + if (r == -ENODEV) + return log_tests_skipped("Loopback device not found"); if (r < 0) return handle_error_errno(r, "Failed to create loopback device object"); diff --git a/src/libsystemd/sd-device/test-sd-device.c b/src/libsystemd/sd-device/test-sd-device.c index bce99b55186..f64f6013cf7 100644 --- a/src/libsystemd/sd-device/test-sd-device.c +++ b/src/libsystemd/sd-device/test-sd-device.c @@ -11,6 +11,7 @@ #include "errno-util.h" #include "fd-util.h" #include "hashmap.h" +#include "mountpoint-util.h" #include "nulstr-util.h" #include "path-util.h" #include "rm-rf.h" @@ -675,4 +676,11 @@ TEST(devname_from_devnum) { } } -DEFINE_TEST_MAIN(LOG_INFO); +static int intro(void) { + if (path_is_mount_point("/sys", NULL, 0) <= 0) + return log_tests_skipped("/sys is not mounted"); + + return EXIT_SUCCESS; +} + +DEFINE_TEST_MAIN_WITH_INTRO(LOG_INFO, intro); diff --git a/src/libsystemd/sd-login/test-login.c b/src/libsystemd/sd-login/test-login.c index 819f86f7446..313e5e339da 100644 --- a/src/libsystemd/sd-login/test-login.c +++ b/src/libsystemd/sd-login/test-login.c @@ -10,6 +10,7 @@ #include "format-util.h" #include "log.h" #include "missing_syscall.h" +#include "mountpoint-util.h" #include "process-util.h" #include "string-util.h" #include "strv.h" @@ -327,6 +328,9 @@ TEST(monitor) { } static int intro(void) { + if (IN_SET(cg_unified(), -ENOENT, -ENOMEDIUM)) + return log_tests_skipped("cgroupfs is not mounted"); + log_info("/* Information printed is from the live system */"); return EXIT_SUCCESS; } diff --git a/src/libudev/test-udev-device-thread.c b/src/libudev/test-udev-device-thread.c index c082fdca468..fdf08188631 100644 --- a/src/libudev/test-udev-device-thread.c +++ b/src/libudev/test-udev-device-thread.c @@ -6,6 +6,7 @@ #include #include "libudev.h" +#include "tests.h" #define handle_error_errno(error, msg) \ ({ \ @@ -29,8 +30,12 @@ int main(int argc, char *argv[]) { int r; loopback = udev_device_new_from_syspath(NULL, "/sys/class/net/lo"); - if (!loopback) + if (!loopback) { + if (errno == ENODEV) + return log_tests_skipped_errno(errno, "Loopback device not found"); + return handle_error_errno(errno, "Failed to create loopback device object"); + } entry = udev_device_get_properties_list_entry(loopback); udev_list_entry_foreach(e, entry) diff --git a/src/shared/tests.c b/src/shared/tests.c index 3882a180c4a..0322d446a15 100644 --- a/src/shared/tests.c +++ b/src/shared/tests.c @@ -21,6 +21,7 @@ #include "fd-util.h" #include "fs-util.h" #include "log.h" +#include "mountpoint-util.h" #include "namespace-util.h" #include "path-util.h" #include "process-util.h" @@ -266,7 +267,7 @@ static int enter_cgroup(char **ret_cgroup, bool enter_subroot) { log_warning_errno(r, "Couldn't allocate a scope unit for this test, proceeding without."); r = cg_pid_get_path(NULL, 0, &cgroup_root); - if (r == -ENOMEDIUM) + if (IN_SET(r, -ENOMEDIUM, -ENOENT)) return log_warning_errno(r, "cg_pid_get_path(NULL, 0, ...) failed: %m"); assert(r >= 0); diff --git a/src/test/meson.build b/src/test/meson.build index a59461acc44..b0a92bb43b9 100644 --- a/src/test/meson.build +++ b/src/test/meson.build @@ -578,12 +578,18 @@ executables += [ }, test_template + { 'sources' : files('../libsystemd/sd-device/test-sd-device-thread.c'), - 'link_with' : libsystemd, + 'link_with' : [ + libbasic, + libsystemd, + ], 'dependencies' : threads, }, test_template + { 'sources' : files('../libudev/test-udev-device-thread.c'), - 'link_with' : libudev, + 'link_with' : [ + libbasic, + libudev, + ], 'dependencies' : threads, }, test_template + { diff --git a/src/test/test-cgroup-util.c b/src/test/test-cgroup-util.c index 51f52d9a4ef..cb5a981dc3c 100644 --- a/src/test/test-cgroup-util.c +++ b/src/test/test-cgroup-util.c @@ -362,7 +362,7 @@ TEST(cg_tests) { int all, hybrid, systemd, r; r = cg_unified(); - if (r == -ENOMEDIUM) { + if (IN_SET(r, -ENOENT, -ENOMEDIUM)) { log_tests_skipped("cgroup not mounted"); return; } @@ -395,7 +395,7 @@ TEST(cg_get_keyed_attribute) { int i, r; r = cg_get_keyed_attribute("cpu", "/init.scope", "no_such_file", STRV_MAKE("no_such_attr"), &val); - if (r == -ENOMEDIUM || ERRNO_IS_PRIVILEGE(r)) { + if (IN_SET(r, -ENOMEDIUM, -ENOENT) || ERRNO_IS_PRIVILEGE(r)) { log_info_errno(r, "Skipping most of %s, /sys/fs/cgroup not accessible: %m", __func__); return; } diff --git a/src/test/test-cgroup.c b/src/test/test-cgroup.c index 0fbd6351a4a..9b174d708b5 100644 --- a/src/test/test-cgroup.c +++ b/src/test/test-cgroup.c @@ -44,8 +44,8 @@ TEST(cg_create) { int r; r = cg_unified_cached(false); - if (r == -ENOMEDIUM) { - log_tests_skipped("cgroup not mounted"); + if (IN_SET(r, -ENOMEDIUM, -ENOENT)) { + log_tests_skipped("cgroupfs is not mounted"); return; } assert_se(r >= 0); diff --git a/src/test/test-condition.c b/src/test/test-condition.c index bb987613e15..66208463606 100644 --- a/src/test/test-condition.c +++ b/src/test/test-condition.c @@ -139,8 +139,8 @@ TEST(condition_test_control_group_hierarchy) { int r; r = cg_unified(); - if (r == -ENOMEDIUM) { - log_tests_skipped("cgroup not mounted"); + if (IN_SET(r, -ENOMEDIUM, -ENOENT)) { + log_tests_skipped("cgroupfs is not mounted"); return; } assert_se(r >= 0); @@ -163,8 +163,8 @@ TEST(condition_test_control_group_controller) { int r; r = cg_unified(); - if (r == -ENOMEDIUM) { - log_tests_skipped("cgroup not mounted"); + if (IN_SET(r, -ENOMEDIUM, -ENOENT)) { + log_tests_skipped("cgroupfs is not mounted"); return; } assert_se(r >= 0); diff --git a/src/test/test-mountpoint-util.c b/src/test/test-mountpoint-util.c index ff447c65821..5dcec9658d5 100644 --- a/src/test/test-mountpoint-util.c +++ b/src/test/test-mountpoint-util.c @@ -138,10 +138,10 @@ TEST(path_is_mount_point) { assert_se(path_is_mount_point("/proc/1/", NULL, AT_SYMLINK_FOLLOW) == 0); assert_se(path_is_mount_point("/proc/1/", NULL, 0) == 0); - assert_se(path_is_mount_point("/sys", NULL, AT_SYMLINK_FOLLOW) > 0); - assert_se(path_is_mount_point("/sys", NULL, 0) > 0); - assert_se(path_is_mount_point("/sys/", NULL, AT_SYMLINK_FOLLOW) > 0); - assert_se(path_is_mount_point("/sys/", NULL, 0) > 0); + assert_se(path_is_mount_point("/dev", NULL, AT_SYMLINK_FOLLOW) > 0); + assert_se(path_is_mount_point("/dev", NULL, 0) > 0); + assert_se(path_is_mount_point("/dev/", NULL, AT_SYMLINK_FOLLOW) > 0); + assert_se(path_is_mount_point("/dev/", NULL, 0) > 0); /* we'll create a hierarchy of different kinds of dir/file/link * layouts: diff --git a/src/test/test-watch-pid.c b/src/test/test-watch-pid.c index b0c2c065e42..271af24c804 100644 --- a/src/test/test-watch-pid.c +++ b/src/test/test-watch-pid.c @@ -18,7 +18,7 @@ int main(int argc, char *argv[]) { if (getuid() != 0) return log_tests_skipped("not root"); r = enter_cgroup_subroot(NULL); - if (r == -ENOMEDIUM) + if (r < 0) return log_tests_skipped("cgroupfs not available"); _cleanup_free_ char *unit_dir = NULL; diff --git a/src/udev/test-udev-format.c b/src/udev/test-udev-format.c index d8e38082fdc..18a60b351e5 100644 --- a/src/udev/test-udev-format.c +++ b/src/udev/test-udev-format.c @@ -1,5 +1,6 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ +#include "mountpoint-util.h" #include "string-util.h" #include "tests.h" #include "udev-format.h" @@ -34,4 +35,11 @@ TEST(udev_resolve_subsys_kernel) { test_udev_resolve_subsys_kernel_one("[net/lo]/address", true, 0, "00:00:00:00:00:00"); } -DEFINE_TEST_MAIN(LOG_DEBUG); +static int intro(void) { + if (path_is_mount_point("/sys", NULL, 0) <= 0) + return log_tests_skipped("/sys is not mounted"); + + return EXIT_SUCCESS; +} + +DEFINE_TEST_MAIN_WITH_INTRO(LOG_DEBUG, intro); diff --git a/src/udev/test-udev-spawn.c b/src/udev/test-udev-spawn.c index 4f43facd805..447e50d978f 100644 --- a/src/udev/test-udev-spawn.c +++ b/src/udev/test-udev-spawn.c @@ -1,5 +1,6 @@ /* SPDX-License-Identifier: LGPL-2.1-or-later */ +#include "mountpoint-util.h" #include "path-util.h" #include "signal-util.h" #include "strv.h" @@ -80,6 +81,9 @@ static void test2(void) { int main(int argc, char *argv[]) { _cleanup_free_ char *self = NULL; + if (path_is_mount_point("/sys", NULL, 0) <= 0) + return log_tests_skipped("/sys is not mounted"); + if (argc > 1) { if (streq(argv[1], "test1")) test1();