diff --git a/src/shared/tests.c b/src/shared/tests.c index fc53546be0..a4ffd3ec02 100644 --- a/src/shared/tests.c +++ b/src/shared/tests.c @@ -15,11 +15,14 @@ #undef basename #include "alloc-util.h" +#include "cgroup-setup.h" +#include "cgroup-util.h" #include "env-file.h" #include "env-util.h" #include "fs-util.h" #include "log.h" #include "path-util.h" +#include "random-util.h" #include "strv.h" #include "tests.h" @@ -168,3 +171,26 @@ bool can_memlock(void) { assert_se(munmap(p, CAN_MEMLOCK_SIZE) >= 0); return b; } + +int enter_cgroup_subroot(void) { + _cleanup_free_ char *cgroup_root = NULL, *cgroup_subroot = NULL; + CGroupMask supported; + int r; + + r = cg_pid_get_path(NULL, 0, &cgroup_root); + if (r == -ENOMEDIUM) + return log_warning_errno(r, "cg_pid_get_path(NULL, 0, ...) failed: %m"); + assert(r >= 0); + + assert_se(asprintf(&cgroup_subroot, "%s/%" PRIx64, cgroup_root, random_u64()) >= 0); + assert_se(cg_mask_supported(&supported) >= 0); + + /* If this fails, then we don't mind as the later cgroup operations will fail too, and it's fine if + * we handle any errors at that point. */ + + r = cg_create_everywhere(supported, _CGROUP_MASK_ALL, cgroup_subroot); + if (r < 0) + return r; + + return cg_attach_everywhere(supported, cgroup_subroot, 0, NULL, NULL); +} diff --git a/src/shared/tests.h b/src/shared/tests.h index 72f6f63651..3e4368460d 100644 --- a/src/shared/tests.h +++ b/src/shared/tests.h @@ -3,6 +3,8 @@ #include +#include "sd-daemon.h" + #include "macro.h" static inline bool manager_errno_skip_test(int r) { @@ -17,6 +19,7 @@ static inline bool manager_errno_skip_test(int r) { } char* setup_fake_runtime_dir(void); +int enter_cgroup_subroot(void); const char* get_testdata_dir(void); const char* get_catalog_dir(void); bool slow_tests_enabled(void); @@ -29,3 +32,10 @@ bool have_namespaces(void); /* We use the small but non-trivial limit here */ #define CAN_MEMLOCK_SIZE (512 * 1024U) bool can_memlock(void); + +#define TEST_REQ_RUNNING_SYSTEMD(x) \ + if (sd_booted() > 0) { \ + x; \ + } else { \ + printf("systemd not booted skipping '%s'\n", #x); \ + } diff --git a/src/test/meson.build b/src/test/meson.build index 16050f0a7e..10ac3c973f 100644 --- a/src/test/meson.build +++ b/src/test/meson.build @@ -51,8 +51,7 @@ tests += [ [], []], - [['src/test/test-engine.c', - 'src/test/test-helper.c'], + [['src/test/test-engine.c'], [libcore, libudev, libshared], @@ -141,8 +140,7 @@ tests += [ [], []], - [['src/test/test-unit-name.c', - 'src/test/test-helper.c'], + [['src/test/test-unit-name.c'], [libcore, libshared], [threads, @@ -152,8 +150,7 @@ tests += [ libmount, libblkid]], - [['src/test/test-load-fragment.c', - 'src/test/test-helper.c'], + [['src/test/test-load-fragment.c'], [libcore, libshared], [threads, @@ -428,8 +425,7 @@ tests += [ [libbasic], []], - [['src/test/test-bpf-firewall.c', - 'src/test/test-helper.c'], + [['src/test/test-bpf-firewall.c'], [libcore, libshared], [libmount, @@ -439,8 +435,7 @@ tests += [ libselinux, libblkid]], - [['src/test/test-watch-pid.c', - 'src/test/test-helper.c'], + [['src/test/test-watch-pid.c'], [libcore, libshared], [libmount, @@ -590,14 +585,12 @@ tests += [ libshared], []], - [['src/test/test-cgroup-unit-default.c', - 'src/test/test-helper.c'], + [['src/test/test-cgroup-unit-default.c'], [libcore, libshared], []], - [['src/test/test-cgroup-mask.c', - 'src/test/test-helper.c'], + [['src/test/test-cgroup-mask.c'], [libcore, libshared], [threads, @@ -644,8 +637,7 @@ tests += [ [], '', 'manual'], - [['src/test/test-path.c', - 'src/test/test-helper.c'], + [['src/test/test-path.c'], [libcore, libshared], [threads, @@ -655,8 +647,7 @@ tests += [ libmount, libblkid]], - [['src/test/test-execute.c', - 'src/test/test-helper.c'], + [['src/test/test-execute.c'], [libcore, libshared], [threads, @@ -685,8 +676,7 @@ tests += [ [], []], - [['src/test/test-sched-prio.c', - 'src/test/test-helper.c'], + [['src/test/test-sched-prio.c'], [libcore, libshared], [threads, diff --git a/src/test/test-bpf-firewall.c b/src/test/test-bpf-firewall.c index 0942c61e0f..13f81d48d0 100644 --- a/src/test/test-bpf-firewall.c +++ b/src/test/test-bpf-firewall.c @@ -10,7 +10,6 @@ #include "manager.h" #include "rm-rf.h" #include "service.h" -#include "test-helper.h" #include "tests.h" #include "unit.h" #include "virt.h" diff --git a/src/test/test-cgroup-mask.c b/src/test/test-cgroup-mask.c index ef3755de41..28a3721255 100644 --- a/src/test/test-cgroup-mask.c +++ b/src/test/test-cgroup-mask.c @@ -8,7 +8,6 @@ #include "manager.h" #include "rm-rf.h" #include "string-util.h" -#include "test-helper.h" #include "tests.h" #include "unit.h" diff --git a/src/test/test-cgroup-unit-default.c b/src/test/test-cgroup-unit-default.c index 4fb629217f..c4af2a3cdf 100644 --- a/src/test/test-cgroup-unit-default.c +++ b/src/test/test-cgroup-unit-default.c @@ -5,7 +5,6 @@ #include "cgroup.h" #include "manager.h" #include "rm-rf.h" -#include "test-helper.h" #include "tests.h" #include "unit.h" diff --git a/src/test/test-cgroup-util.c b/src/test/test-cgroup-util.c index e4fbb9edce..83b5156c11 100644 --- a/src/test/test-cgroup-util.c +++ b/src/test/test-cgroup-util.c @@ -13,7 +13,6 @@ #include "stat-util.h" #include "string-util.h" #include "strv.h" -#include "test-helper.h" #include "tests.h" #include "user-util.h" #include "util.h" diff --git a/src/test/test-engine.c b/src/test/test-engine.c index c292ad1c7c..a32e98af11 100644 --- a/src/test/test-engine.c +++ b/src/test/test-engine.c @@ -7,7 +7,6 @@ #include "manager.h" #include "rm-rf.h" #include "strv.h" -#include "test-helper.h" #include "tests.h" #include "service.h" diff --git a/src/test/test-execute.c b/src/test/test-execute.c index 957cc2e384..d846414750 100644 --- a/src/test/test-execute.c +++ b/src/test/test-execute.c @@ -20,7 +20,6 @@ #endif #include "service.h" #include "stat-util.h" -#include "test-helper.h" #include "tests.h" #include "unit.h" #include "user-util.h" diff --git a/src/test/test-helper.c b/src/test/test-helper.c deleted file mode 100644 index 9e9de0132f..0000000000 --- a/src/test/test-helper.c +++ /dev/null @@ -1,30 +0,0 @@ -/* SPDX-License-Identifier: LGPL-2.1+ */ - -#include "test-helper.h" -#include "random-util.h" -#include "alloc-util.h" -#include "cgroup-setup.h" -#include "string-util.h" - -int enter_cgroup_subroot(void) { - _cleanup_free_ char *cgroup_root = NULL, *cgroup_subroot = NULL; - CGroupMask supported; - int r; - - r = cg_pid_get_path(NULL, 0, &cgroup_root); - if (r == -ENOMEDIUM) - return log_warning_errno(r, "cg_pid_get_path(NULL, 0, ...) failed: %m"); - assert(r >= 0); - - assert_se(asprintf(&cgroup_subroot, "%s/%" PRIx64, cgroup_root, random_u64()) >= 0); - assert_se(cg_mask_supported(&supported) >= 0); - - /* If this fails, then we don't mind as the later cgroup operations will fail too, and it's fine if we handle - * any errors at that point. */ - - r = cg_create_everywhere(supported, _CGROUP_MASK_ALL, cgroup_subroot); - if (r < 0) - return r; - - return cg_attach_everywhere(supported, cgroup_subroot, 0, NULL, NULL); -} diff --git a/src/test/test-helper.h b/src/test/test-helper.h deleted file mode 100644 index 6e886ae3de..0000000000 --- a/src/test/test-helper.h +++ /dev/null @@ -1,17 +0,0 @@ -/* SPDX-License-Identifier: LGPL-2.1+ */ -#pragma once - -/*** - Copyright © 2013 Holger Hans Peter Freyther -***/ - -#include "sd-daemon.h" - -#define TEST_REQ_RUNNING_SYSTEMD(x) \ - if (sd_booted() > 0) { \ - x; \ - } else { \ - printf("systemd not booted skipping '%s'\n", #x); \ - } - -int enter_cgroup_subroot(void); diff --git a/src/test/test-load-fragment.c b/src/test/test-load-fragment.c index cb808bfce0..8127afdd76 100644 --- a/src/test/test-load-fragment.c +++ b/src/test/test-load-fragment.c @@ -23,7 +23,6 @@ #include "specifier.h" #include "string-util.h" #include "strv.h" -#include "test-helper.h" #include "tests.h" #include "tmpfile-util.h" #include "user-util.h" diff --git a/src/test/test-path.c b/src/test/test-path.c index 9157c6a13b..2537dc6972 100644 --- a/src/test/test-path.c +++ b/src/test/test-path.c @@ -16,7 +16,6 @@ #include "rm-rf.h" #include "string-util.h" #include "strv.h" -#include "test-helper.h" #include "tests.h" #include "unit.h" #include "util.h" diff --git a/src/test/test-process-util.c b/src/test/test-process-util.c index 762a203577..d0f126b236 100644 --- a/src/test/test-process-util.c +++ b/src/test/test-process-util.c @@ -25,7 +25,6 @@ #include "stdio-util.h" #include "string-util.h" #include "terminal-util.h" -#include "test-helper.h" #include "tests.h" #include "util.h" #include "virt.h" diff --git a/src/test/test-sched-prio.c b/src/test/test-sched-prio.c index d179ae21a6..386cd93cbf 100644 --- a/src/test/test-sched-prio.c +++ b/src/test/test-sched-prio.c @@ -9,7 +9,6 @@ #include "macro.h" #include "manager.h" #include "rm-rf.h" -#include "test-helper.h" #include "tests.h" int main(int argc, char *argv[]) { diff --git a/src/test/test-unit-name.c b/src/test/test-unit-name.c index 52c75406ae..d63647bad2 100644 --- a/src/test/test-unit-name.c +++ b/src/test/test-unit-name.c @@ -15,7 +15,6 @@ #include "special.h" #include "specifier.h" #include "string-util.h" -#include "test-helper.h" #include "tests.h" #include "unit-def.h" #include "unit-name.h" diff --git a/src/test/test-watch-pid.c b/src/test/test-watch-pid.c index 04d7e1fbd8..a26a0c9d9b 100644 --- a/src/test/test-watch-pid.c +++ b/src/test/test-watch-pid.c @@ -4,7 +4,6 @@ #include "manager.h" #include "rm-rf.h" #include "service.h" -#include "test-helper.h" #include "tests.h" int main(int argc, char *argv[]) {