chiark / gitweb /
test-engine: fix access to unit load path
[elogind.git] / src / test / test-sched-prio.c
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2012 Holger Hans Peter Freyther
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 <sched.h>
23
24 #include "manager.h"
25 #include "macro.h"
26
27 int main(int argc, char *argv[]) {
28         Manager *m = NULL;
29         Unit *idle_ok, *idle_bad, *rr_ok, *rr_bad, *rr_sched;
30         Service *ser;
31         FILE *serial = NULL;
32         FDSet *fdset = NULL;
33         int r;
34
35         /* prepare the test */
36         assert_se(set_unit_path(TEST_DIR) >= 0);
37         r = manager_new(SYSTEMD_USER, true, &m);
38         if (r == -EPERM || r == -EACCES || r == -EADDRINUSE || r == -EHOSTDOWN) {
39                 printf("Skipping test: manager_new: %s", strerror(-r));
40                 return EXIT_TEST_SKIP;
41         }
42         assert(r >= 0);
43         assert_se(manager_startup(m, serial, fdset) >= 0);
44
45         /* load idle ok */
46         assert_se(manager_load_unit(m, "sched_idle_ok.service", NULL, NULL, &idle_ok) >= 0);
47         assert_se(idle_ok->load_state == UNIT_LOADED);
48         ser = SERVICE(idle_ok);
49         assert_se(ser->exec_context.cpu_sched_policy == SCHED_OTHER);
50         assert_se(ser->exec_context.cpu_sched_priority == 0);
51
52         /*
53          * load idle bad. This should print a warning but we have no way to look at it.
54          */
55         assert_se(manager_load_unit(m, "sched_idle_bad.service", NULL, NULL, &idle_bad) >= 0);
56         assert_se(idle_bad->load_state == UNIT_LOADED);
57         ser = SERVICE(idle_ok);
58         assert_se(ser->exec_context.cpu_sched_policy == SCHED_OTHER);
59         assert_se(ser->exec_context.cpu_sched_priority == 0);
60
61         /*
62          * load rr ok.
63          * Test that the default priority is moving from 0 to 1.
64          */
65         assert_se(manager_load_unit(m, "sched_rr_ok.service", NULL, NULL, &rr_ok) >= 0);
66         assert_se(rr_ok->load_state == UNIT_LOADED);
67         ser = SERVICE(rr_ok);
68         assert_se(ser->exec_context.cpu_sched_policy == SCHED_RR);
69         assert_se(ser->exec_context.cpu_sched_priority == 1);
70
71         /*
72          * load rr bad.
73          * Test that the value of 0 and 100 is ignored.
74          */
75         assert_se(manager_load_unit(m, "sched_rr_bad.service", NULL, NULL, &rr_bad) >= 0);
76         assert_se(rr_bad->load_state == UNIT_LOADED);
77         ser = SERVICE(rr_bad);
78         assert_se(ser->exec_context.cpu_sched_policy == SCHED_RR);
79         assert_se(ser->exec_context.cpu_sched_priority == 1);
80
81         /*
82          * load rr change.
83          * Test that anything between 1 and 99 can be set.
84          */
85         assert_se(manager_load_unit(m, "sched_rr_change.service", NULL, NULL, &rr_sched) >= 0);
86         assert_se(rr_sched->load_state == UNIT_LOADED);
87         ser = SERVICE(rr_sched);
88         assert_se(ser->exec_context.cpu_sched_policy == SCHED_RR);
89         assert_se(ser->exec_context.cpu_sched_priority == 99);
90
91         manager_free(m);
92
93         return EXIT_SUCCESS;
94 }