chiark / gitweb /
macro: add a macro to test whether a value is in a specified list
[elogind.git] / src / test / test-cgroup-mask.c
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>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <sys/types.h>
26 #include <pwd.h>
27
28 #include "manager.h"
29 #include "unit.h"
30 #include "util.h"
31 #include "macro.h"
32 #include "test-helper.h"
33
34 static int test_cgroup_mask(void) {
35         Manager *m;
36         Unit *son, *daughter, *parent, *root;
37         FILE *serial = NULL;
38         FDSet *fdset = NULL;
39         int r;
40
41         /* Prepare the manager. */
42         assert_se(set_unit_path(TEST_DIR) >= 0);
43         r = manager_new(SYSTEMD_USER, &m);
44         if (r == -EPERM || r == -EACCES) {
45                 puts("manager_new: Permission denied. Skipping test.");
46                 return EXIT_TEST_SKIP;
47         }
48         assert(r >= 0);
49         assert_se(manager_startup(m, serial, fdset) >= 0);
50
51         /* Load units and verify hierarchy. */
52         assert_se(manager_load_unit(m, "parent.slice", NULL, NULL, &parent) >= 0);
53         assert_se(manager_load_unit(m, "son.service", NULL, NULL, &son) >= 0);
54         assert_se(manager_load_unit(m, "daughter.service", NULL, NULL, &daughter) >= 0);
55         assert(parent->load_state == UNIT_LOADED);
56         assert(son->load_state == UNIT_LOADED);
57         assert(daughter->load_state == UNIT_LOADED);
58         assert(UNIT_DEREF(son->slice) == parent);
59         assert(UNIT_DEREF(daughter->slice) == parent);
60         root = UNIT_DEREF(parent->slice);
61
62         /* Verify per-unit cgroups settings. */
63         assert(cgroup_context_get_mask(unit_get_cgroup_context(son)) == (CGROUP_CPU | CGROUP_CPUACCT));
64         assert(cgroup_context_get_mask(unit_get_cgroup_context(daughter)) == 0);
65         assert(cgroup_context_get_mask(unit_get_cgroup_context(parent)) == CGROUP_BLKIO);
66         assert(cgroup_context_get_mask(unit_get_cgroup_context(root)) == 0);
67
68         /* Verify aggregation of controller masks. */
69         assert(son->cgroup_members_mask == (CGROUP_CPU | CGROUP_CPUACCT));
70         assert(daughter->cgroup_members_mask == 0);
71         assert(parent->cgroup_members_mask == (CGROUP_CPU | CGROUP_CPUACCT | CGROUP_BLKIO));
72         assert(root->cgroup_members_mask == (CGROUP_CPU | CGROUP_CPUACCT | CGROUP_BLKIO));
73
74         manager_free(m);
75
76         return 0;
77 }
78
79 int main(int argc, char* argv[]) {
80         int rc = 0;
81         TEST_REQ_RUNNING_SYSTEMD(rc = test_cgroup_mask());
82         return rc;
83 }