]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
test: rearrange test-cgroup and test-cgroup-util 38877/head
authorYu Watanabe <watanabe.yu+github@gmail.com>
Sat, 30 Aug 2025 12:59:44 +0000 (21:59 +0900)
committerYu Watanabe <watanabe.yu+github@gmail.com>
Wed, 17 Sep 2025 13:20:42 +0000 (22:20 +0900)
src/test/meson.build
src/test/test-cgroup-setup.c [moved from src/test/test-cgroup.c with 60% similarity]
src/test/test-cgroup-util.c

index da04b82d47691f2160d3509087ccad578abf872c..2a4d77a8efb894078e917cafb8c5e4ef57f54998 100644 (file)
@@ -68,8 +68,8 @@ simple_tests += files(
         'test-bus-unit-util.c',
         'test-bus-util.c',
         'test-calendarspec.c',
+        'test-cgroup-setup.c',
         'test-cgroup-util.c',
-        'test-cgroup.c',
         'test-chase.c',
         'test-chid.c',
         'test-clock.c',
similarity index 60%
rename from src/test/test-cgroup.c
rename to src/test/test-cgroup-setup.c
index a14043442f7cfb3f1bf8cb609f89feaa1d1ba92e..ce20c956d2f1a7900344a83eaf5f14d790daad68 100644 (file)
@@ -3,43 +3,11 @@
 #include "cgroup-setup.h"
 #include "cgroup-util.h"
 #include "errno-util.h"
-#include "fd-util.h"
 #include "path-util.h"
 #include "process-util.h"
-#include "stat-util.h"
 #include "string-util.h"
 #include "tests.h"
 
-TEST(cg_split_spec) {
-        char *c, *p;
-
-        ASSERT_OK_ZERO(cg_split_spec("foobar:/", &c, &p));
-        ASSERT_STREQ(c, "foobar");
-        ASSERT_STREQ(p, "/");
-        c = mfree(c);
-        p = mfree(p);
-
-        ASSERT_OK_ZERO(cg_split_spec("foobar:", &c, &p));
-        c = mfree(c);
-        p = mfree(p);
-
-        ASSERT_FAIL(cg_split_spec("foobar:asdfd", &c, &p));
-        ASSERT_FAIL(cg_split_spec(":///", &c, &p));
-        ASSERT_FAIL(cg_split_spec(":", &c, &p));
-        ASSERT_FAIL(cg_split_spec("", &c, &p));
-        ASSERT_FAIL(cg_split_spec("fo/obar:/", &c, &p));
-
-        ASSERT_OK(cg_split_spec("/", &c, &p));
-        ASSERT_NULL(c);
-        ASSERT_STREQ(p, "/");
-        p = mfree(p);
-
-        ASSERT_OK(cg_split_spec("foo", &c, &p));
-        ASSERT_STREQ(c, "foo");
-        ASSERT_NULL(p);
-        c = mfree(c);
-}
-
 TEST(cg_create) {
         int r;
 
@@ -118,45 +86,4 @@ TEST(cg_create) {
         ASSERT_OK(cg_trim(test_b, true));
 }
 
-TEST(id) {
-        _cleanup_free_ char *p = NULL, *p2 = NULL;
-        _cleanup_close_ int fd = -EBADF, fd2 = -EBADF;
-        uint64_t id, id2;
-        int r;
-
-        r = cg_all_unified();
-        if (IN_SET(r, -ENOMEDIUM, -ENOENT))
-                return (void) log_tests_skipped("cgroupfs is not mounted");
-        if (r == 0)
-                return (void) log_tests_skipped("skipping cgroupid test, not running in unified mode");
-        ASSERT_OK_POSITIVE(r);
-
-        fd = cg_path_open(SYSTEMD_CGROUP_CONTROLLER, "/");
-        ASSERT_OK(fd);
-
-        ASSERT_OK(fd_get_path(fd, &p));
-        ASSERT_TRUE(path_equal(p, "/sys/fs/cgroup"));
-
-        ASSERT_OK(cg_fd_get_cgroupid(fd, &id));
-
-        fd2 = cg_cgroupid_open(fd, id);
-
-        if (ERRNO_IS_NEG_PRIVILEGE(fd2))
-                log_notice("Skipping open-by-cgroup-id test because lacking privs.");
-        else if (ERRNO_IS_NEG_NOT_SUPPORTED(fd2))
-                log_notice("Skipping open-by-cgroup-id test because syscall is missing or blocked.");
-        else {
-                ASSERT_OK(fd2);
-
-                ASSERT_OK(fd_get_path(fd2, &p2));
-                ASSERT_TRUE(path_equal(p2, "/sys/fs/cgroup"));
-
-                ASSERT_OK(cg_fd_get_cgroupid(fd2, &id2));
-
-                ASSERT_EQ(id, id2);
-
-                ASSERT_OK_EQ(inode_same_at(fd, NULL, fd2, NULL, AT_EMPTY_PATH), true);
-        }
-}
-
 DEFINE_TEST_MAIN(LOG_DEBUG);
index 552e9a5d280978a37fac85d6dd43bdb180006fe1..fea0733173f869b5c773881dc1a66d097315746e 100644 (file)
@@ -11,6 +11,7 @@
 #include "pidref.h"
 #include "process-util.h"
 #include "special.h"
+#include "stat-util.h"
 #include "string-util.h"
 #include "tests.h"
 
@@ -334,6 +335,36 @@ TEST(controller_is_valid) {
         assert_se(!cg_controller_is_valid("tatü"));
 }
 
+TEST(cg_split_spec) {
+        char *c, *p;
+
+        ASSERT_OK_ZERO(cg_split_spec("foobar:/", &c, &p));
+        ASSERT_STREQ(c, "foobar");
+        ASSERT_STREQ(p, "/");
+        c = mfree(c);
+        p = mfree(p);
+
+        ASSERT_OK_ZERO(cg_split_spec("foobar:", &c, &p));
+        c = mfree(c);
+        p = mfree(p);
+
+        ASSERT_FAIL(cg_split_spec("foobar:asdfd", &c, &p));
+        ASSERT_FAIL(cg_split_spec(":///", &c, &p));
+        ASSERT_FAIL(cg_split_spec(":", &c, &p));
+        ASSERT_FAIL(cg_split_spec("", &c, &p));
+        ASSERT_FAIL(cg_split_spec("fo/obar:/", &c, &p));
+
+        ASSERT_OK(cg_split_spec("/", &c, &p));
+        ASSERT_NULL(c);
+        ASSERT_STREQ(p, "/");
+        p = mfree(p);
+
+        ASSERT_OK(cg_split_spec("foo", &c, &p));
+        ASSERT_STREQ(c, "foo");
+        ASSERT_NULL(p);
+        c = mfree(c);
+}
+
 static void test_slice_to_path_one(const char *unit, const char *path, int error) {
         _cleanup_free_ char *ret = NULL;
         int r;
@@ -473,4 +504,45 @@ TEST(bfq_weight_conversion) {
         assert_se(BFQ_WEIGHT(10000) == 1000);
 }
 
+TEST(cgroupid) {
+        _cleanup_free_ char *p = NULL, *p2 = NULL;
+        _cleanup_close_ int fd = -EBADF, fd2 = -EBADF;
+        uint64_t id, id2;
+        int r;
+
+        r = cg_all_unified();
+        if (IN_SET(r, -ENOMEDIUM, -ENOENT))
+                return (void) log_tests_skipped("cgroupfs is not mounted");
+        if (r == 0)
+                return (void) log_tests_skipped("skipping cgroupid test, not running in unified mode");
+        ASSERT_OK_POSITIVE(r);
+
+        fd = cg_path_open(SYSTEMD_CGROUP_CONTROLLER, "/");
+        ASSERT_OK(fd);
+
+        ASSERT_OK(fd_get_path(fd, &p));
+        ASSERT_TRUE(path_equal(p, "/sys/fs/cgroup"));
+
+        ASSERT_OK(cg_fd_get_cgroupid(fd, &id));
+
+        fd2 = cg_cgroupid_open(fd, id);
+
+        if (ERRNO_IS_NEG_PRIVILEGE(fd2))
+                log_notice("Skipping open-by-cgroup-id test because lacking privs.");
+        else if (ERRNO_IS_NEG_NOT_SUPPORTED(fd2))
+                log_notice("Skipping open-by-cgroup-id test because syscall is missing or blocked.");
+        else {
+                ASSERT_OK(fd2);
+
+                ASSERT_OK(fd_get_path(fd2, &p2));
+                ASSERT_TRUE(path_equal(p2, "/sys/fs/cgroup"));
+
+                ASSERT_OK(cg_fd_get_cgroupid(fd2, &id2));
+
+                ASSERT_EQ(id, id2);
+
+                ASSERT_OK_EQ(inode_same_at(fd, NULL, fd2, NULL, AT_EMPTY_PATH), true);
+        }
+}
+
 DEFINE_TEST_MAIN(LOG_DEBUG);