]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/test/test-engine.c
man: extend documentation on the unit name escaping logic
[thirdparty/systemd.git] / src / test / test-engine.c
CommitLineData
a7334b09
LP
1/***
2 This file is part of systemd.
3
4 Copyright 2010 Lennart Poettering
5
6 systemd is free software; you can redistribute it and/or modify it
5430f7f2
LP
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
a7334b09
LP
9 (at your option) any later version.
10
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
5430f7f2 14 Lesser General Public License for more details.
a7334b09 15
5430f7f2 16 You should have received a copy of the GNU Lesser General Public License
a7334b09
LP
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
18***/
19
279ade13 20#include <errno.h>
cf0fbc49 21#include <stdio.h>
279ade13 22#include <string.h>
279ade13 23
0d8c31ff 24#include "bus-util.h"
cf0fbc49 25#include "manager.h"
d2120590 26#include "rm-rf.h"
8b3aa503 27#include "test-helper.h"
d2120590 28#include "tests.h"
279ade13
LP
29
30int main(int argc, char *argv[]) {
f942504e 31 _cleanup_(rm_rf_physical_and_freep) char *runtime_dir = NULL;
4afd3348 32 _cleanup_(sd_bus_error_free) sd_bus_error err = SD_BUS_ERROR_NULL;
279ade13 33 Manager *m = NULL;
87f0e418 34 Unit *a = NULL, *b = NULL, *c = NULL, *d = NULL, *e = NULL, *g = NULL, *h = NULL;
0d8c31ff
ZJS
35 FILE *serial = NULL;
36 FDSet *fdset = NULL;
279ade13 37 Job *j;
0d8c31ff 38 int r;
279ade13 39
651d47d1
ZJS
40 r = enter_cgroup_subroot();
41 if (r == -ENOMEDIUM) {
42 log_notice_errno(r, "Skipping test: cgroupfs not available");
43 return EXIT_TEST_SKIP;
44 }
8c759b33 45
0d8c31ff 46 /* prepare the test */
cc100a5a 47 assert_se(set_unit_path(get_testdata_dir("")) >= 0);
3e29e810 48 assert_se(runtime_dir = setup_fake_runtime_dir());
e0a3da1f 49 r = manager_new(UNIT_FILE_USER, MANAGER_TEST_RUN_MINIMAL, &m);
8b3aa503 50 if (MANAGER_SKIP_TEST(r)) {
9eec7d12 51 log_notice_errno(r, "Skipping test: manager_new: %m");
0d8c31ff
ZJS
52 return EXIT_TEST_SKIP;
53 }
bdf7026e 54 assert_se(r >= 0);
0d8c31ff 55 assert_se(manager_startup(m, serial, fdset) >= 0);
279ade13 56
e094e853 57 printf("Load1:\n");
398ef8ba
LP
58 assert_se(manager_load_unit(m, "a.service", NULL, NULL, &a) >= 0);
59 assert_se(manager_load_unit(m, "b.service", NULL, NULL, &b) >= 0);
60 assert_se(manager_load_unit(m, "c.service", NULL, NULL, &c) >= 0);
87f0e418 61 manager_dump_units(m, stdout, "\t");
279ade13
LP
62
63 printf("Test1: (Trivial)\n");
4bd29fe5 64 r = manager_add_job(m, JOB_START, c, JOB_REPLACE, &err, &j);
0d8c31ff
ZJS
65 if (sd_bus_error_is_set(&err))
66 log_error("error: %s: %s", err.name, err.message);
bdf7026e 67 assert_se(r == 0);
279ade13
LP
68 manager_dump_jobs(m, stdout, "\t");
69
e094e853 70 printf("Load2:\n");
279ade13 71 manager_clear_jobs(m);
398ef8ba
LP
72 assert_se(manager_load_unit(m, "d.service", NULL, NULL, &d) >= 0);
73 assert_se(manager_load_unit(m, "e.service", NULL, NULL, &e) >= 0);
87f0e418 74 manager_dump_units(m, stdout, "\t");
279ade13
LP
75
76 printf("Test2: (Cyclic Order, Unfixable)\n");
4bd29fe5 77 assert_se(manager_add_job(m, JOB_START, d, JOB_REPLACE, NULL, &j) == -EDEADLK);
279ade13
LP
78 manager_dump_jobs(m, stdout, "\t");
79
1ffba6fe 80 printf("Test3: (Cyclic Order, Fixable, Garbage Collector)\n");
4bd29fe5 81 assert_se(manager_add_job(m, JOB_START, e, JOB_REPLACE, NULL, &j) == 0);
279ade13
LP
82 manager_dump_jobs(m, stdout, "\t");
83
1ffba6fe 84 printf("Test4: (Identical transaction)\n");
4bd29fe5 85 assert_se(manager_add_job(m, JOB_START, e, JOB_FAIL, NULL, &j) == 0);
1ffba6fe
LP
86 manager_dump_jobs(m, stdout, "\t");
87
e094e853 88 printf("Load3:\n");
398ef8ba 89 assert_se(manager_load_unit(m, "g.service", NULL, NULL, &g) >= 0);
87f0e418 90 manager_dump_units(m, stdout, "\t");
1ffba6fe
LP
91
92 printf("Test5: (Colliding transaction, fail)\n");
4bd29fe5 93 assert_se(manager_add_job(m, JOB_START, g, JOB_FAIL, NULL, &j) == -EDEADLK);
1ffba6fe
LP
94
95 printf("Test6: (Colliding transaction, replace)\n");
4bd29fe5 96 assert_se(manager_add_job(m, JOB_START, g, JOB_REPLACE, NULL, &j) == 0);
1ffba6fe
LP
97 manager_dump_jobs(m, stdout, "\t");
98
35b8ca3a 99 printf("Test7: (Unmergeable job type, fail)\n");
4bd29fe5 100 assert_se(manager_add_job(m, JOB_STOP, g, JOB_FAIL, NULL, &j) == -EDEADLK);
e094e853
LP
101
102 printf("Test8: (Mergeable job type, fail)\n");
4bd29fe5 103 assert_se(manager_add_job(m, JOB_RESTART, g, JOB_FAIL, NULL, &j) == 0);
e094e853
LP
104 manager_dump_jobs(m, stdout, "\t");
105
35b8ca3a 106 printf("Test9: (Unmergeable job type, replace)\n");
4bd29fe5 107 assert_se(manager_add_job(m, JOB_STOP, g, JOB_REPLACE, NULL, &j) == 0);
e094e853
LP
108 manager_dump_jobs(m, stdout, "\t");
109
110 printf("Load4:\n");
398ef8ba 111 assert_se(manager_load_unit(m, "h.service", NULL, NULL, &h) >= 0);
87f0e418 112 manager_dump_units(m, stdout, "\t");
e094e853 113
35b8ca3a 114 printf("Test10: (Unmergeable job type of auxiliary job, fail)\n");
4bd29fe5 115 assert_se(manager_add_job(m, JOB_START, h, JOB_FAIL, NULL, &j) == 0);
e094e853
LP
116 manager_dump_jobs(m, stdout, "\t");
117
279ade13
LP
118 manager_free(m);
119
120 return 0;
121}