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