]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-cgroup.c
test: Use TEST macro
[thirdparty/systemd.git] / src / test / test-cgroup.c
CommitLineData
db9ecf05 1/* SPDX-License-Identifier: LGPL-2.1-or-later */
8c6db833 2
07630cea 3#include <unistd.h>
8c6db833 4
fdb3deca 5#include "cgroup-setup.h"
8c6db833 6#include "cgroup-util.h"
67da3323 7#include "errno-util.h"
9eb977db 8#include "path-util.h"
dccca82b 9#include "process-util.h"
07630cea 10#include "string-util.h"
b35e9974 11#include "tests.h"
8c6db833 12
4f7452a8 13TEST(cg_split_spec) {
35d2e7ec 14 char *c, *p;
8c6db833 15
b35e9974
ZJS
16 assert_se(cg_split_spec("foobar:/", &c, &p) == 0);
17 assert_se(streq(c, "foobar"));
18 assert_se(streq(p, "/"));
19 c = mfree(c);
20 p = mfree(p);
21
22 assert_se(cg_split_spec("foobar:", &c, &p) == 0);
1c132196
ZJS
23 c = mfree(c);
24 p = mfree(p);
25
b35e9974
ZJS
26 assert_se(cg_split_spec("foobar:asdfd", &c, &p) < 0);
27 assert_se(cg_split_spec(":///", &c, &p) < 0);
28 assert_se(cg_split_spec(":", &c, &p) < 0);
29 assert_se(cg_split_spec("", &c, &p) < 0);
30 assert_se(cg_split_spec("fo/obar:/", &c, &p) < 0);
31
32 assert_se(cg_split_spec("/", &c, &p) >= 0);
33 assert_se(c == NULL);
34 assert_se(streq(p, "/"));
35 p = mfree(p);
36
37 assert_se(cg_split_spec("foo", &c, &p) >= 0);
38 assert_se(streq(c, "foo"));
39 assert_se(p == NULL);
40 c = mfree(c);
41}
42
4f7452a8 43TEST(cg_create) {
af5654d3
DS
44 int r;
45
46 r = cg_unified_cached(false);
b66789a9
DS
47 if (r == -ENOMEDIUM) {
48 log_tests_skipped("cgroup not mounted");
af5654d3
DS
49 return;
50 }
b66789a9 51 assert_se(r >= 0);
67da3323
ZJS
52
53 _cleanup_free_ char *here = NULL;
54 assert_se(cg_pid_get_path_shifted(0, NULL, &here) >= 0);
55
56 const char *test_a = prefix_roota(here, "/test-a"),
57 *test_b = prefix_roota(here, "/test-b"),
58 *test_c = prefix_roota(here, "/test-b/test-c"),
59 *test_d = prefix_roota(here, "/test-b/test-d");
b35e9974
ZJS
60 char *path;
61
67da3323
ZJS
62 log_info("Paths for test:\n%s\n%s", test_a, test_b);
63
64 r = cg_create(SYSTEMD_CGROUP_CONTROLLER, test_a);
65 if (IN_SET(r, -EPERM, -EACCES, -EROFS)) {
66 log_info_errno(r, "Skipping %s: %m", __func__);
67 return;
68 }
b35e9974 69
67da3323
ZJS
70 assert_se(r == 1);
71 assert_se(cg_create(SYSTEMD_CGROUP_CONTROLLER, test_a) == 0);
72 assert_se(cg_create(SYSTEMD_CGROUP_CONTROLLER, test_b) == 1);
73 assert_se(cg_create(SYSTEMD_CGROUP_CONTROLLER, test_c) == 1);
74 assert_se(cg_create_and_attach(SYSTEMD_CGROUP_CONTROLLER, test_b, 0) == 0);
8c6db833 75
df0ff127 76 assert_se(cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, getpid_cached(), &path) == 0);
67da3323 77 assert_se(streq(path, test_b));
8c6db833
LP
78 free(path);
79
67da3323 80 assert_se(cg_attach(SYSTEMD_CGROUP_CONTROLLER, test_a, 0) == 0);
8c6db833 81
df0ff127 82 assert_se(cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, getpid_cached(), &path) == 0);
67da3323 83 assert_se(path_equal(path, test_a));
8c6db833
LP
84 free(path);
85
67da3323 86 assert_se(cg_create_and_attach(SYSTEMD_CGROUP_CONTROLLER, test_d, 0) == 1);
8c6db833 87
df0ff127 88 assert_se(cg_pid_get_path(SYSTEMD_CGROUP_CONTROLLER, getpid_cached(), &path) == 0);
67da3323 89 assert_se(path_equal(path, test_d));
8c6db833
LP
90 free(path);
91
67da3323
ZJS
92 assert_se(cg_get_path(SYSTEMD_CGROUP_CONTROLLER, test_d, NULL, &path) == 0);
93 log_debug("test_d: %s", path);
94 const char *full_d;
95 if (cg_all_unified())
96 full_d = strjoina("/sys/fs/cgroup", test_d);
97 else if (cg_hybrid_unified())
98 full_d = strjoina("/sys/fs/cgroup/unified", test_d);
99 else
100 full_d = strjoina("/sys/fs/cgroup/systemd", test_d);
101 assert_se(path_equal(path, full_d));
8c6db833
LP
102 free(path);
103
67da3323
ZJS
104 assert_se(cg_is_empty(SYSTEMD_CGROUP_CONTROLLER, test_a) > 0);
105 assert_se(cg_is_empty(SYSTEMD_CGROUP_CONTROLLER, test_b) > 0);
106 assert_se(cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, test_a) > 0);
107 assert_se(cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, test_b) == 0);
8c6db833 108
67da3323
ZJS
109 assert_se(cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, test_a, 0, 0, NULL, NULL, NULL) == 0);
110 assert_se(cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, test_b, 0, 0, NULL, NULL, NULL) > 0);
8c6db833 111
67da3323 112 assert_se(cg_migrate_recursive(SYSTEMD_CGROUP_CONTROLLER, test_b, SYSTEMD_CGROUP_CONTROLLER, test_a, 0) > 0);
8c6db833 113
67da3323
ZJS
114 assert_se(cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, test_a) == 0);
115 assert_se(cg_is_empty_recursive(SYSTEMD_CGROUP_CONTROLLER, test_b) > 0);
8c6db833 116
67da3323
ZJS
117 assert_se(cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, test_a, 0, 0, NULL, NULL, NULL) > 0);
118 assert_se(cg_kill_recursive(SYSTEMD_CGROUP_CONTROLLER, test_b, 0, 0, NULL, NULL, NULL) == 0);
8c6db833 119
67da3323 120 cg_trim(SYSTEMD_CGROUP_CONTROLLER, test_b, false);
8c6db833 121
67da3323
ZJS
122 assert_se(cg_rmdir(SYSTEMD_CGROUP_CONTROLLER, test_b) == 0);
123 assert_se(cg_rmdir(SYSTEMD_CGROUP_CONTROLLER, test_a) < 0);
124 assert_se(cg_migrate_recursive(SYSTEMD_CGROUP_CONTROLLER, test_a, SYSTEMD_CGROUP_CONTROLLER, here, 0) > 0);
125 assert_se(cg_rmdir(SYSTEMD_CGROUP_CONTROLLER, test_a) == 0);
b35e9974 126}
35d2e7ec 127
4f7452a8 128DEFINE_TEST_MAIN(LOG_DEBUG);