Debian Codegraph @

src/test/test-cgroup-setup.c

raw
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include "cgroup-setup.h"
4 #include "cgroup-util.h"
5 #include "errno-util.h"
6 #include "path-util.h"
7 #include "process-util.h"
8 #include "string-util.h"
9 #include "tests.h"
10
11 TEST(cg_create) {
12 int r;
13
14 _cleanup_free_ char *here = NULL;
15 ASSERT_OK(cg_pid_get_path_shifted(0, NULL, &here));
16
17 /* cg_* will use path_simplify(), so use it here too otherwise when running in a container at the
18 * root it asserts with "/test-b != //test-b" */
19 _cleanup_free_ char *test_a = ASSERT_NOT_NULL(path_simplify(path_join(here, "/test-a"))),
20 *test_b = ASSERT_NOT_NULL(path_simplify(path_join(here, "/test-b"))),
21 *test_c = ASSERT_NOT_NULL(path_simplify(path_join(here, "/test-b/test-c"))),
22 *test_d = ASSERT_NOT_NULL(path_simplify(path_join(here, "/test-b/test-d")));
23 char *path;
24
25 log_info("Paths for test:\n%s\n%s", test_a, test_b);
26
27 /* Possibly clean up left-overs from aborted previous runs */
28 (void) cg_trim(test_a, /* delete_root= */ true);
29 (void) cg_trim(test_b, /* delete_root= */ true);
30
31 r = cg_create(test_a);
32 if (ERRNO_IS_NEG_FS_WRITE_REFUSED(r) || r == -ENOENT)
33 return (void) log_tests_skipped_errno(r, "%s: Failed to create cgroup %s", __func__, test_a);
34
35 ASSERT_OK_EQ(r, 1);
36 ASSERT_OK_ZERO(cg_create(test_a));
37 ASSERT_OK_EQ(cg_create(test_b), 1);
38 ASSERT_OK_EQ(cg_create(test_c), 1);
39 ASSERT_OK_ZERO(cg_create_and_attach(test_b, 0));
40
41 ASSERT_OK_ZERO(cg_pid_get_path(getpid_cached(), &path));
42 ASSERT_STREQ(path, test_b);
43 free(path);
44
45 ASSERT_OK_ZERO(cg_attach(test_a, 0));
46
47 ASSERT_OK_ZERO(cg_pid_get_path(getpid_cached(), &path));
48 ASSERT_TRUE(path_equal(path, test_a));
49 free(path);
50
51 ASSERT_OK_EQ(cg_create_and_attach(test_d, 0), 1);
52
53 ASSERT_OK_ZERO(cg_pid_get_path(getpid_cached(), &path));
54 ASSERT_TRUE(path_equal(path, test_d));
55 free(path);
56
57 ASSERT_OK_ZERO(cg_get_path(test_d, /* suffix= */ NULL, &path));
58 log_debug("test_d: %s", path);
59 ASSERT_TRUE(path_equal(path, strjoina("/sys/fs/cgroup", test_d)));
60 free(path);
61
62 ASSERT_OK_POSITIVE(cg_is_empty(test_a));
63 ASSERT_OK_ZERO(cg_is_empty(test_b));
64
65 ASSERT_OK_ZERO(cg_kill_recursive(test_a, 0, 0, NULL, NULL, NULL));
66 ASSERT_OK_POSITIVE(cg_kill_recursive(test_b, 0, 0, NULL, NULL, NULL));
67
68 ASSERT_OK(cg_trim(test_a, true));
69 ASSERT_ERROR(cg_trim(test_b, true), EBUSY);
70
71 ASSERT_OK_ZERO(cg_attach(here, 0));
72 ASSERT_OK(cg_trim(test_b, true));
73 }
74
75 static int intro(void) {
76 if (cg_is_available() <= 0)
77 return log_tests_skipped("cgroupfs v2 is not mounted");
78
79 return 0;
80 }
81
82 DEFINE_TEST_MAIN_WITH_INTRO(LOG_DEBUG, intro);