]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
test: Skip various tests when /sys is not mounted 30527/head
authorDaan De Meyer <daan.j.demeyer@gmail.com>
Tue, 19 Dec 2023 15:03:06 +0000 (16:03 +0100)
committerDaan De Meyer <daan.j.demeyer@gmail.com>
Tue, 19 Dec 2023 15:38:57 +0000 (16:38 +0100)
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.

15 files changed:
src/libsystemd/sd-bus/test-bus-creds.c
src/libsystemd/sd-device/test-sd-device-monitor.c
src/libsystemd/sd-device/test-sd-device-thread.c
src/libsystemd/sd-device/test-sd-device.c
src/libsystemd/sd-login/test-login.c
src/libudev/test-udev-device-thread.c
src/shared/tests.c
src/test/meson.build
src/test/test-cgroup-util.c
src/test/test-cgroup.c
src/test/test-condition.c
src/test/test-mountpoint-util.c
src/test/test-watch-pid.c
src/udev/test-udev-format.c
src/udev/test-udev-spawn.c

index 13801becc90565a39f3946c8f52fd9b624557608..d18ce88a25612bfb13c0a982e8b8e38a92752819 100644 (file)
@@ -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);
index e124e0021c352240641330001978c4b65c3087e0..4f80c4e57941caaf74b0f4c24791d6c73dea6df4 100644 (file)
@@ -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");
 
index c99d179b3351a079807106e7deebe9b20e837149..539dabd7a6b75d4b2d09bdfcc2def80334d3b72b 100644 (file)
@@ -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");
 
index bce99b55186a2392d2511d0444577228ca76200f..f64f6013cf723b2f2790f69e6d3c8f0e6938db3b 100644 (file)
@@ -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);
index 819f86f7446558026dac76677d4a7712e894dbfb..313e5e339da45ad135c8888d70a02d0e7bc3549b 100644 (file)
@@ -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;
 }
index c082fdca46843e03fc078df71b254e5854f3b43d..fdf0818863135d3125faedca6cb0ebc24fe2e8f9 100644 (file)
@@ -6,6 +6,7 @@
 #include <stdlib.h>
 
 #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)
index 3882a180c4ada07d7dbc251b422fc91b58be74e3..0322d446a157d5a3d622b92922c1ced22564b044 100644 (file)
@@ -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);
 
index a59461acc44afa2bcf2a08d092160b1e13e2e7c1..b0a92bb43b9aa1a475f60b255387a83b121d03b0 100644 (file)
@@ -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 + {
index 51f52d9a4ef933c1febc7e186370c390d87a40bf..cb5a981dc3c0871fa528dc0d2a52c8fd0226bf45 100644 (file)
@@ -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;
         }
index 0fbd6351a4abfa0a5f20a4e9e4f9a3f06b1b0f26..9b174d708b562b572793202cf54f7a39743045f4 100644 (file)
@@ -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);
index bb987613e151fa786f559e6d3034079aa6ebb6e3..66208463606c8a04b1c1ad2a1350e2bffe307abd 100644 (file)
@@ -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);
index ff447c658218bfafad6bb42233cf6f6351952cdd..5dcec9658d53aa7e92b371eea6edb3b6d2120409 100644 (file)
@@ -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:
index b0c2c065e42904c4db698fc190544e020910c8e5..271af24c8040af4d5a89be7a296f2880def43643 100644 (file)
@@ -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;
index d8e38082fdcbfdc7ef1813b75a026c4da736258e..18a60b351e57586bd0c332bb3e426c4301889ec9 100644 (file)
@@ -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);
index 4f43facd8054b0c9a7d5e4f742c0707ee446795a..447e50d978fcb90fe26ce8610f3a93997e3835e8 100644 (file)
@@ -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();