]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-cgroup-mask.c
tree-wide: sort includes
[thirdparty/systemd.git] / src / test / test-cgroup-mask.c
CommitLineData
6414b7c9
DS
1/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3/***
4 This file is part of systemd.
5
6 Copyright 2013 David Strauss
7
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
12
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
17
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
20***/
21
22#include <stdio.h>
6414b7c9 23
6414b7c9 24#include "macro.h"
cf0fbc49 25#include "manager.h"
6414b7c9 26#include "test-helper.h"
cf0fbc49 27#include "unit.h"
6414b7c9
DS
28
29static int test_cgroup_mask(void) {
39883f62 30 Manager *m = NULL;
bc432dc7 31 Unit *son, *daughter, *parent, *root, *grandchild, *parent_deep;
6414b7c9
DS
32 FILE *serial = NULL;
33 FDSet *fdset = NULL;
34 int r;
35
36 /* Prepare the manager. */
0d8c31ff 37 assert_se(set_unit_path(TEST_DIR) >= 0);
b2c23da8 38 r = manager_new(MANAGER_USER, true, &m);
6414b7c9
DS
39 if (r == -EPERM || r == -EACCES) {
40 puts("manager_new: Permission denied. Skipping test.");
41 return EXIT_TEST_SKIP;
42 }
9ded9cd1
LP
43
44 /* Turn off all kinds of default accouning, so that we can
45 * verify the masks resulting of our configuration and nothing
46 * else. */
47 m->default_cpu_accounting =
48 m->default_memory_accounting =
49 m->default_blockio_accounting =
50 m->default_tasks_accounting = false;
51 m->default_tasks_max = (uint64_t) -1;
52
bdf7026e 53 assert_se(r >= 0);
6414b7c9
DS
54 assert_se(manager_startup(m, serial, fdset) >= 0);
55
56 /* Load units and verify hierarchy. */
57 assert_se(manager_load_unit(m, "parent.slice", NULL, NULL, &parent) >= 0);
58 assert_se(manager_load_unit(m, "son.service", NULL, NULL, &son) >= 0);
59 assert_se(manager_load_unit(m, "daughter.service", NULL, NULL, &daughter) >= 0);
bc432dc7
LP
60 assert_se(manager_load_unit(m, "grandchild.service", NULL, NULL, &grandchild) >= 0);
61 assert_se(manager_load_unit(m, "parent-deep.slice", NULL, NULL, &parent_deep) >= 0);
bdf7026e
TA
62 assert_se(parent->load_state == UNIT_LOADED);
63 assert_se(son->load_state == UNIT_LOADED);
64 assert_se(daughter->load_state == UNIT_LOADED);
65 assert_se(grandchild->load_state == UNIT_LOADED);
66 assert_se(parent_deep->load_state == UNIT_LOADED);
67 assert_se(UNIT_DEREF(son->slice) == parent);
68 assert_se(UNIT_DEREF(daughter->slice) == parent);
69 assert_se(UNIT_DEREF(parent_deep->slice) == parent);
70 assert_se(UNIT_DEREF(grandchild->slice) == parent_deep);
6414b7c9
DS
71 root = UNIT_DEREF(parent->slice);
72
73 /* Verify per-unit cgroups settings. */
efdb0237
LP
74 assert_se(unit_get_own_mask(son) == (CGROUP_MASK_CPU | CGROUP_MASK_CPUACCT));
75 assert_se(unit_get_own_mask(daughter) == 0);
76 assert_se(unit_get_own_mask(grandchild) == 0);
77 assert_se(unit_get_own_mask(parent_deep) == CGROUP_MASK_MEMORY);
78 assert_se(unit_get_own_mask(parent) == CGROUP_MASK_BLKIO);
79 assert_se(unit_get_own_mask(root) == 0);
bc432dc7
LP
80
81 /* Verify aggregation of member masks */
bdf7026e
TA
82 assert_se(unit_get_members_mask(son) == 0);
83 assert_se(unit_get_members_mask(daughter) == 0);
84 assert_se(unit_get_members_mask(grandchild) == 0);
85 assert_se(unit_get_members_mask(parent_deep) == 0);
efdb0237
LP
86 assert_se(unit_get_members_mask(parent) == (CGROUP_MASK_CPU | CGROUP_MASK_CPUACCT | CGROUP_MASK_MEMORY));
87 assert_se(unit_get_members_mask(root) == (CGROUP_MASK_CPU | CGROUP_MASK_CPUACCT | CGROUP_MASK_BLKIO | CGROUP_MASK_MEMORY));
bc432dc7
LP
88
89 /* Verify aggregation of sibling masks. */
efdb0237
LP
90 assert_se(unit_get_siblings_mask(son) == (CGROUP_MASK_CPU | CGROUP_MASK_CPUACCT | CGROUP_MASK_MEMORY));
91 assert_se(unit_get_siblings_mask(daughter) == (CGROUP_MASK_CPU | CGROUP_MASK_CPUACCT | CGROUP_MASK_MEMORY));
bdf7026e 92 assert_se(unit_get_siblings_mask(grandchild) == 0);
efdb0237
LP
93 assert_se(unit_get_siblings_mask(parent_deep) == (CGROUP_MASK_CPU | CGROUP_MASK_CPUACCT | CGROUP_MASK_MEMORY));
94 assert_se(unit_get_siblings_mask(parent) == (CGROUP_MASK_CPU | CGROUP_MASK_CPUACCT | CGROUP_MASK_BLKIO | CGROUP_MASK_MEMORY));
95 assert_se(unit_get_siblings_mask(root) == (CGROUP_MASK_CPU | CGROUP_MASK_CPUACCT | CGROUP_MASK_BLKIO | CGROUP_MASK_MEMORY));
bc432dc7
LP
96
97 /* Verify aggregation of target masks. */
efdb0237
LP
98 assert_se(unit_get_target_mask(son) == ((CGROUP_MASK_CPU | CGROUP_MASK_CPUACCT | CGROUP_MASK_MEMORY) & m->cgroup_supported));
99 assert_se(unit_get_target_mask(daughter) == ((CGROUP_MASK_CPU | CGROUP_MASK_CPUACCT | CGROUP_MASK_MEMORY) & m->cgroup_supported));
bdf7026e 100 assert_se(unit_get_target_mask(grandchild) == 0);
efdb0237
LP
101 assert_se(unit_get_target_mask(parent_deep) == ((CGROUP_MASK_CPU | CGROUP_MASK_CPUACCT | CGROUP_MASK_MEMORY) & m->cgroup_supported));
102 assert_se(unit_get_target_mask(parent) == ((CGROUP_MASK_CPU | CGROUP_MASK_CPUACCT | CGROUP_MASK_BLKIO | CGROUP_MASK_MEMORY) & m->cgroup_supported));
103 assert_se(unit_get_target_mask(root) == ((CGROUP_MASK_CPU | CGROUP_MASK_CPUACCT | CGROUP_MASK_BLKIO | CGROUP_MASK_MEMORY) & m->cgroup_supported));
6414b7c9
DS
104
105 manager_free(m);
106
107 return 0;
108}
109
110int main(int argc, char* argv[]) {
111 int rc = 0;
112 TEST_REQ_RUNNING_SYSTEMD(rc = test_cgroup_mask());
113 return rc;
114}