]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
bc999297 | 2 | |
bc999297 | 3 | #include <stdbool.h> |
07630cea | 4 | #include <stdio.h> |
edb3ca0d FB |
5 | #include <sys/stat.h> |
6 | #include <sys/types.h> | |
bc999297 | 7 | |
b5efdb8a | 8 | #include "alloc-util.h" |
57b7a260 | 9 | #include "all-units.h" |
f4f15635 LP |
10 | #include "fd-util.h" |
11 | #include "fs-util.h" | |
bc999297 | 12 | #include "macro.h" |
07630cea | 13 | #include "manager.h" |
bc999297 | 14 | #include "mkdir.h" |
55890a40 | 15 | #include "path-util.h" |
c6878637 | 16 | #include "rm-rf.h" |
07630cea LP |
17 | #include "string-util.h" |
18 | #include "strv.h" | |
8b3aa503 | 19 | #include "test-helper.h" |
d2120590 | 20 | #include "tests.h" |
07630cea LP |
21 | #include "unit.h" |
22 | #include "util.h" | |
bc999297 RC |
23 | |
24 | typedef void (*test_function_t)(Manager *m); | |
25 | ||
26 | static int setup_test(Manager **m) { | |
27 | char **tests_path = STRV_MAKE("exists", "existsglobFOOBAR", "changed", "modified", "unit", | |
28 | "directorynotempty", "makedirectory"); | |
29 | char **test_path; | |
a7f7d1bd | 30 | Manager *tmp = NULL; |
bc999297 RC |
31 | int r; |
32 | ||
33 | assert_se(m); | |
34 | ||
651d47d1 | 35 | r = enter_cgroup_subroot(); |
317bb217 ZJS |
36 | if (r == -ENOMEDIUM) |
37 | return log_tests_skipped("cgroupfs not available"); | |
8c759b33 | 38 | |
e8112e67 | 39 | r = manager_new(UNIT_FILE_USER, MANAGER_TEST_RUN_BASIC, &tmp); |
8b3aa503 | 40 | if (MANAGER_SKIP_TEST(r)) { |
9eec7d12 | 41 | log_notice_errno(r, "Skipping test: manager_new: %m"); |
317bb217 | 42 | return EXIT_TEST_SKIP; |
bc999297 RC |
43 | } |
44 | assert_se(r >= 0); | |
45 | assert_se(manager_startup(tmp, NULL, NULL) >= 0); | |
46 | ||
47 | STRV_FOREACH(test_path, tests_path) { | |
c6878637 LP |
48 | _cleanup_free_ char *p = NULL; |
49 | ||
605405c6 | 50 | p = strjoin("/tmp/test-path_", *test_path); |
c6878637 LP |
51 | assert_se(p); |
52 | ||
53 | (void) rm_rf(p, REMOVE_ROOT|REMOVE_PHYSICAL); | |
bc999297 RC |
54 | } |
55 | ||
56 | *m = tmp; | |
57 | ||
58 | return 0; | |
59 | } | |
60 | ||
61 | static void shutdown_test(Manager *m) { | |
62 | assert_se(m); | |
63 | ||
64 | manager_free(m); | |
65 | } | |
66 | ||
67 | static void check_stop_unlink(Manager *m, Unit *unit, const char *test_path, const char *service_name) { | |
68 | _cleanup_free_ char *tmp = NULL; | |
69 | Unit *service_unit = NULL; | |
70 | Service *service = NULL; | |
71 | usec_t ts; | |
72 | usec_t timeout = 2 * USEC_PER_SEC; | |
73 | ||
74 | assert_se(m); | |
75 | assert_se(unit); | |
76 | assert_se(test_path); | |
77 | ||
78 | if (!service_name) { | |
79 | assert_se(tmp = strreplace(unit->id, ".path", ".service")); | |
80 | service_unit = manager_get_unit(m, tmp); | |
81 | } else | |
82 | service_unit = manager_get_unit(m, service_name); | |
83 | assert_se(service_unit); | |
84 | service = SERVICE(service_unit); | |
85 | ||
86 | ts = now(CLOCK_MONOTONIC); | |
f131770b | 87 | /* We process events until the service related to the path has been successfully started */ |
9ed794a3 | 88 | while (service->result != SERVICE_SUCCESS || service->state != SERVICE_START) { |
bc999297 RC |
89 | usec_t n; |
90 | int r; | |
91 | ||
92 | r = sd_event_run(m->event, 100 * USEC_PER_MSEC); | |
93 | assert_se(r >= 0); | |
94 | ||
95 | printf("%s: state = %s; result = %s \n", | |
96 | service_unit->id, | |
97 | service_state_to_string(service->state), | |
98 | service_result_to_string(service->result)); | |
99 | ||
bc999297 RC |
100 | /* But we timeout if the service has not been started in the allocated time */ |
101 | n = now(CLOCK_MONOTONIC); | |
102 | if (ts + timeout < n) { | |
103 | log_error("Test timeout when testing %s", unit->id); | |
104 | exit(EXIT_FAILURE); | |
105 | } | |
106 | } | |
107 | ||
108 | assert_se(UNIT_VTABLE(unit)->stop(unit) >= 0); | |
c6878637 | 109 | (void) rm_rf(test_path, REMOVE_ROOT|REMOVE_PHYSICAL); |
bc999297 RC |
110 | } |
111 | ||
112 | static void test_path_exists(Manager *m) { | |
113 | const char *test_path = "/tmp/test-path_exists"; | |
114 | Unit *unit = NULL; | |
115 | ||
116 | assert_se(m); | |
117 | ||
ba412430 | 118 | assert_se(manager_load_startable_unit_or_warn(m, "path-exists.path", NULL, &unit) >= 0); |
bc999297 RC |
119 | assert_se(UNIT_VTABLE(unit)->start(unit) >= 0); |
120 | ||
121 | assert_se(touch(test_path) >= 0); | |
122 | ||
123 | check_stop_unlink(m, unit, test_path, NULL); | |
124 | } | |
125 | ||
126 | static void test_path_existsglob(Manager *m) { | |
127 | const char *test_path = "/tmp/test-path_existsglobFOOBAR"; | |
128 | Unit *unit = NULL; | |
129 | ||
130 | assert_se(m); | |
ba412430 | 131 | assert_se(manager_load_startable_unit_or_warn(m, "path-existsglob.path", NULL, &unit) >= 0); |
bc999297 RC |
132 | assert_se(UNIT_VTABLE(unit)->start(unit) >= 0); |
133 | ||
134 | assert_se(touch(test_path) >= 0); | |
135 | ||
136 | check_stop_unlink(m, unit, test_path, NULL); | |
137 | } | |
138 | ||
139 | static void test_path_changed(Manager *m) { | |
140 | const char *test_path = "/tmp/test-path_changed"; | |
141 | FILE *f; | |
142 | Unit *unit = NULL; | |
143 | ||
144 | assert_se(m); | |
145 | ||
146 | assert_se(touch(test_path) >= 0); | |
147 | ||
ba412430 | 148 | assert_se(manager_load_startable_unit_or_warn(m, "path-changed.path", NULL, &unit) >= 0); |
bc999297 RC |
149 | assert_se(UNIT_VTABLE(unit)->start(unit) >= 0); |
150 | ||
151 | f = fopen(test_path, "w"); | |
152 | assert_se(f); | |
153 | fclose(f); | |
154 | ||
155 | check_stop_unlink(m, unit, test_path, NULL); | |
156 | } | |
157 | ||
158 | static void test_path_modified(Manager *m) { | |
159 | _cleanup_fclose_ FILE *f = NULL; | |
160 | const char *test_path = "/tmp/test-path_modified"; | |
161 | Unit *unit = NULL; | |
162 | ||
163 | assert_se(m); | |
164 | ||
165 | assert_se(touch(test_path) >= 0); | |
166 | ||
ba412430 | 167 | assert_se(manager_load_startable_unit_or_warn(m, "path-modified.path", NULL, &unit) >= 0); |
bc999297 RC |
168 | assert_se(UNIT_VTABLE(unit)->start(unit) >= 0); |
169 | ||
170 | f = fopen(test_path, "w"); | |
171 | assert_se(f); | |
172 | fputs("test", f); | |
173 | ||
174 | check_stop_unlink(m, unit, test_path, NULL); | |
175 | } | |
176 | ||
177 | static void test_path_unit(Manager *m) { | |
178 | const char *test_path = "/tmp/test-path_unit"; | |
179 | Unit *unit = NULL; | |
180 | ||
181 | assert_se(m); | |
182 | ||
ba412430 | 183 | assert_se(manager_load_startable_unit_or_warn(m, "path-unit.path", NULL, &unit) >= 0); |
bc999297 RC |
184 | assert_se(UNIT_VTABLE(unit)->start(unit) >= 0); |
185 | ||
186 | assert_se(touch(test_path) >= 0); | |
187 | ||
188 | check_stop_unlink(m, unit, test_path, "path-mycustomunit.service"); | |
189 | } | |
190 | ||
191 | static void test_path_directorynotempty(Manager *m) { | |
192 | const char *test_path = "/tmp/test-path_directorynotempty/"; | |
193 | Unit *unit = NULL; | |
194 | ||
195 | assert_se(m); | |
196 | ||
197 | assert_se(access(test_path, F_OK) < 0); | |
198 | ||
ba412430 | 199 | assert_se(manager_load_startable_unit_or_warn(m, "path-directorynotempty.path", NULL, &unit) >= 0); |
bc999297 RC |
200 | assert_se(UNIT_VTABLE(unit)->start(unit) >= 0); |
201 | ||
202 | /* MakeDirectory default to no */ | |
203 | assert_se(access(test_path, F_OK) < 0); | |
204 | ||
205 | assert_se(mkdir_p(test_path, 0755) >= 0); | |
63c372cb | 206 | assert_se(touch(strjoina(test_path, "test_file")) >= 0); |
bc999297 RC |
207 | |
208 | check_stop_unlink(m, unit, test_path, NULL); | |
209 | } | |
210 | ||
211 | static void test_path_makedirectory_directorymode(Manager *m) { | |
212 | const char *test_path = "/tmp/test-path_makedirectory/"; | |
213 | Unit *unit = NULL; | |
214 | struct stat s; | |
215 | ||
216 | assert_se(m); | |
217 | ||
218 | assert_se(access(test_path, F_OK) < 0); | |
219 | ||
ba412430 | 220 | assert_se(manager_load_startable_unit_or_warn(m, "path-makedirectory.path", NULL, &unit) >= 0); |
bc999297 RC |
221 | assert_se(UNIT_VTABLE(unit)->start(unit) >= 0); |
222 | ||
223 | /* Check if the directory has been created */ | |
224 | assert_se(access(test_path, F_OK) >= 0); | |
225 | ||
226 | /* Check the mode we specified with DirectoryMode=0744 */ | |
227 | assert_se(stat(test_path, &s) >= 0); | |
228 | assert_se((s.st_mode & S_IRWXU) == 0700); | |
229 | assert_se((s.st_mode & S_IRWXG) == 0040); | |
230 | assert_se((s.st_mode & S_IRWXO) == 0004); | |
231 | ||
232 | assert_se(UNIT_VTABLE(unit)->stop(unit) >= 0); | |
c6878637 | 233 | (void) rm_rf(test_path, REMOVE_ROOT|REMOVE_PHYSICAL); |
bc999297 RC |
234 | } |
235 | ||
236 | int main(int argc, char *argv[]) { | |
d2120590 | 237 | static const test_function_t tests[] = { |
bc999297 RC |
238 | test_path_exists, |
239 | test_path_existsglob, | |
240 | test_path_changed, | |
241 | test_path_modified, | |
242 | test_path_unit, | |
243 | test_path_directorynotempty, | |
244 | test_path_makedirectory_directorymode, | |
245 | NULL, | |
246 | }; | |
d2120590 | 247 | |
f942504e | 248 | _cleanup_(rm_rf_physical_and_freep) char *runtime_dir = NULL; |
55890a40 | 249 | _cleanup_free_ char *test_path = NULL; |
d2120590 | 250 | const test_function_t *test = NULL; |
bc999297 RC |
251 | Manager *m = NULL; |
252 | ||
edb3ca0d FB |
253 | umask(022); |
254 | ||
bc999297 RC |
255 | log_parse_environment(); |
256 | log_open(); | |
257 | ||
55890a40 FB |
258 | test_path = path_join(NULL, get_testdata_dir(), "test-path"); |
259 | assert_se(set_unit_path(test_path) >= 0); | |
3e29e810 | 260 | assert_se(runtime_dir = setup_fake_runtime_dir()); |
bc999297 RC |
261 | |
262 | for (test = tests; test && *test; test++) { | |
263 | int r; | |
264 | ||
265 | /* We create a clean environment for each test */ | |
266 | r = setup_test(&m); | |
317bb217 ZJS |
267 | if (r != 0) |
268 | return r; | |
bc999297 RC |
269 | |
270 | (*test)(m); | |
271 | ||
272 | shutdown_test(m); | |
273 | } | |
274 | ||
275 | return 0; | |
276 | } |