]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/test/test-path.c
529487d1ad84a164373335ed030cca6e86a60456
[thirdparty/systemd.git] / src / test / test-path.c
1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2
3 #include <stdbool.h>
4 #include <sys/stat.h>
5 #include <sys/types.h>
6
7 #include "alloc-util.h"
8 #include "all-units.h"
9 #include "fd-util.h"
10 #include "fs-util.h"
11 #include "macro.h"
12 #include "manager.h"
13 #include "mkdir.h"
14 #include "path-util.h"
15 #include "rm-rf.h"
16 #include "string-util.h"
17 #include "strv.h"
18 #include "tests.h"
19 #include "unit.h"
20 #include "util.h"
21
22 typedef void (*test_function_t)(Manager *m);
23
24 static int setup_test(Manager **m) {
25 char **tests_path = STRV_MAKE("exists", "existsglobFOOBAR", "changed", "modified", "unit",
26 "directorynotempty", "makedirectory");
27 char **test_path;
28 Manager *tmp = NULL;
29 int r;
30
31 assert_se(m);
32
33 r = enter_cgroup_subroot(NULL);
34 if (r == -ENOMEDIUM)
35 return log_tests_skipped("cgroupfs not available");
36
37 r = manager_new(UNIT_FILE_USER, MANAGER_TEST_RUN_BASIC, &tmp);
38 if (manager_errno_skip_test(r))
39 return log_tests_skipped_errno(r, "manager_new");
40 assert_se(r >= 0);
41 assert_se(manager_startup(tmp, NULL, NULL, NULL) >= 0);
42
43 STRV_FOREACH(test_path, tests_path) {
44 _cleanup_free_ char *p = NULL;
45
46 p = strjoin("/tmp/test-path_", *test_path);
47 assert_se(p);
48
49 (void) rm_rf(p, REMOVE_ROOT|REMOVE_PHYSICAL);
50 }
51
52 *m = tmp;
53
54 return 0;
55 }
56
57 static void shutdown_test(Manager *m) {
58 assert_se(m);
59
60 manager_free(m);
61 }
62
63 static Service *service_for_path(Manager *m, Path *path, const char *service_name) {
64 _cleanup_free_ char *tmp = NULL;
65 Unit *service_unit = NULL;
66
67 assert_se(m);
68 assert_se(path);
69
70 if (!service_name) {
71 assert_se(tmp = strreplace(UNIT(path)->id, ".path", ".service"));
72 service_unit = manager_get_unit(m, tmp);
73 } else
74 service_unit = manager_get_unit(m, service_name);
75 assert_se(service_unit);
76
77 return SERVICE(service_unit);
78 }
79
80 static int _check_states(unsigned line,
81 Manager *m, Path *path, Service *service, PathState path_state, ServiceState service_state) {
82 assert_se(m);
83 assert_se(service);
84
85 usec_t end = now(CLOCK_MONOTONIC) + 30 * USEC_PER_SEC;
86
87 while (path->state != path_state || service->state != service_state ||
88 path->result != PATH_SUCCESS || service->result != SERVICE_SUCCESS) {
89
90 assert_se(sd_event_run(m->event, 100 * USEC_PER_MSEC) >= 0);
91
92 usec_t n = now(CLOCK_MONOTONIC);
93 log_info("line %u: %s: state = %s; result = %s (left: %" PRIi64 ")",
94 line,
95 UNIT(path)->id,
96 path_state_to_string(path->state),
97 path_result_to_string(path->result),
98 end - n);
99 log_info("line %u: %s: state = %s; result = %s",
100 line,
101 UNIT(service)->id,
102 service_state_to_string(service->state),
103 service_result_to_string(service->result));
104
105 if (service->state == SERVICE_FAILED &&
106 service->main_exec_status.status == EXIT_CGROUP &&
107 !ci_environment())
108 /* On a general purpose system we may fail to start the service for reasons which are
109 * not under our control: permission limits, resource exhaustion, etc. Let's skip the
110 * test in those cases. On developer machines we require proper setup. */
111 return log_notice_errno(SYNTHETIC_ERRNO(ECANCELED),
112 "Failed to start service %s, aborting test: %s/%s",
113 UNIT(service)->id,
114 service_state_to_string(service->state),
115 service_result_to_string(service->result));
116
117 if (n >= end) {
118 log_error("Test timeout when testing %s", UNIT(path)->id);
119 exit(EXIT_FAILURE);
120 }
121 }
122
123 return 0;
124 }
125 #define check_states(...) _check_states(__LINE__, __VA_ARGS__)
126
127 static void test_path_exists(Manager *m) {
128 const char *test_path = "/tmp/test-path_exists";
129 Unit *unit = NULL;
130 Path *path = NULL;
131 Service *service = NULL;
132
133 assert_se(m);
134
135 assert_se(manager_load_startable_unit_or_warn(m, "path-exists.path", NULL, &unit) >= 0);
136
137 path = PATH(unit);
138 service = service_for_path(m, path, NULL);
139
140 assert_se(unit_start(unit) >= 0);
141 if (check_states(m, path, service, PATH_WAITING, SERVICE_DEAD) < 0)
142 return;
143
144 assert_se(touch(test_path) >= 0);
145 if (check_states(m, path, service, PATH_RUNNING, SERVICE_RUNNING) < 0)
146 return;
147
148 /* Service restarts if file still exists */
149 assert_se(unit_stop(UNIT(service)) >= 0);
150 if (check_states(m, path, service, PATH_RUNNING, SERVICE_RUNNING) < 0)
151 return;
152
153 assert_se(rm_rf(test_path, REMOVE_ROOT|REMOVE_PHYSICAL) == 0);
154 assert_se(unit_stop(UNIT(service)) >= 0);
155 if (check_states(m, path, service, PATH_WAITING, SERVICE_DEAD) < 0)
156 return;
157
158 assert_se(unit_stop(unit) >= 0);
159 }
160
161 static void test_path_existsglob(Manager *m) {
162 const char *test_path = "/tmp/test-path_existsglobFOOBAR";
163 Unit *unit = NULL;
164 Path *path = NULL;
165 Service *service = NULL;
166
167 assert_se(m);
168
169 assert_se(manager_load_startable_unit_or_warn(m, "path-existsglob.path", NULL, &unit) >= 0);
170
171 path = PATH(unit);
172 service = service_for_path(m, path, NULL);
173
174 assert_se(unit_start(unit) >= 0);
175 if (check_states(m, path, service, PATH_WAITING, SERVICE_DEAD) < 0)
176 return;
177
178 assert_se(touch(test_path) >= 0);
179 if (check_states(m, path, service, PATH_RUNNING, SERVICE_RUNNING) < 0)
180 return;
181
182 /* Service restarts if file still exists */
183 assert_se(unit_stop(UNIT(service)) >= 0);
184 if (check_states(m, path, service, PATH_RUNNING, SERVICE_RUNNING) < 0)
185 return;
186
187 assert_se(rm_rf(test_path, REMOVE_ROOT|REMOVE_PHYSICAL) == 0);
188 assert_se(unit_stop(UNIT(service)) >= 0);
189 if (check_states(m, path, service, PATH_WAITING, SERVICE_DEAD) < 0)
190 return;
191
192 assert_se(unit_stop(unit) >= 0);
193 }
194
195 static void test_path_changed(Manager *m) {
196 const char *test_path = "/tmp/test-path_changed";
197 FILE *f;
198 Unit *unit = NULL;
199 Path *path = NULL;
200 Service *service = NULL;
201
202 assert_se(m);
203
204 assert_se(manager_load_startable_unit_or_warn(m, "path-changed.path", NULL, &unit) >= 0);
205
206 path = PATH(unit);
207 service = service_for_path(m, path, NULL);
208
209 assert_se(unit_start(unit) >= 0);
210 if (check_states(m, path, service, PATH_WAITING, SERVICE_DEAD) < 0)
211 return;
212
213 assert_se(touch(test_path) >= 0);
214 if (check_states(m, path, service, PATH_RUNNING, SERVICE_RUNNING) < 0)
215 return;
216
217 /* Service does not restart if file still exists */
218 assert_se(unit_stop(UNIT(service)) >= 0);
219 if (check_states(m, path, service, PATH_WAITING, SERVICE_DEAD) < 0)
220 return;
221
222 f = fopen(test_path, "w");
223 assert_se(f);
224 fclose(f);
225
226 if (check_states(m, path, service, PATH_RUNNING, SERVICE_RUNNING) < 0)
227 return;
228
229 assert_se(unit_stop(UNIT(service)) >= 0);
230 if (check_states(m, path, service, PATH_WAITING, SERVICE_DEAD) < 0)
231 return;
232
233 (void) rm_rf(test_path, REMOVE_ROOT|REMOVE_PHYSICAL);
234 assert_se(unit_stop(unit) >= 0);
235 }
236
237 static void test_path_modified(Manager *m) {
238 _cleanup_fclose_ FILE *f = NULL;
239 const char *test_path = "/tmp/test-path_modified";
240 Unit *unit = NULL;
241 Path *path = NULL;
242 Service *service = NULL;
243
244 assert_se(m);
245
246 assert_se(manager_load_startable_unit_or_warn(m, "path-modified.path", NULL, &unit) >= 0);
247
248 path = PATH(unit);
249 service = service_for_path(m, path, NULL);
250
251 assert_se(unit_start(unit) >= 0);
252 if (check_states(m, path, service, PATH_WAITING, SERVICE_DEAD) < 0)
253 return;
254
255 assert_se(touch(test_path) >= 0);
256 if (check_states(m, path, service, PATH_RUNNING, SERVICE_RUNNING) < 0)
257 return;
258
259 /* Service does not restart if file still exists */
260 assert_se(unit_stop(UNIT(service)) >= 0);
261 if (check_states(m, path, service, PATH_WAITING, SERVICE_DEAD) < 0)
262 return;
263
264 f = fopen(test_path, "w");
265 assert_se(f);
266 fputs("test", f);
267
268 if (check_states(m, path, service, PATH_RUNNING, SERVICE_RUNNING) < 0)
269 return;
270
271 assert_se(unit_stop(UNIT(service)) >= 0);
272 if (check_states(m, path, service, PATH_WAITING, SERVICE_DEAD) < 0)
273 return;
274
275 (void) rm_rf(test_path, REMOVE_ROOT|REMOVE_PHYSICAL);
276 assert_se(unit_stop(unit) >= 0);
277 }
278
279 static void test_path_unit(Manager *m) {
280 const char *test_path = "/tmp/test-path_unit";
281 Unit *unit = NULL;
282 Path *path = NULL;
283 Service *service = NULL;
284
285 assert_se(m);
286
287 assert_se(manager_load_startable_unit_or_warn(m, "path-unit.path", NULL, &unit) >= 0);
288
289 path = PATH(unit);
290 service = service_for_path(m, path, "path-mycustomunit.service");
291
292 assert_se(unit_start(unit) >= 0);
293 if (check_states(m, path, service, PATH_WAITING, SERVICE_DEAD) < 0)
294 return;
295
296 assert_se(touch(test_path) >= 0);
297 if (check_states(m, path, service, PATH_RUNNING, SERVICE_RUNNING) < 0)
298 return;
299
300 assert_se(rm_rf(test_path, REMOVE_ROOT|REMOVE_PHYSICAL) == 0);
301 assert_se(unit_stop(UNIT(service)) >= 0);
302 if (check_states(m, path, service, PATH_WAITING, SERVICE_DEAD) < 0)
303 return;
304
305 assert_se(unit_stop(unit) >= 0);
306 }
307
308 static void test_path_directorynotempty(Manager *m) {
309 const char *test_file, *test_path = "/tmp/test-path_directorynotempty/";
310 Unit *unit = NULL;
311 Path *path = NULL;
312 Service *service = NULL;
313
314 assert_se(m);
315
316 assert_se(manager_load_startable_unit_or_warn(m, "path-directorynotempty.path", NULL, &unit) >= 0);
317
318 path = PATH(unit);
319 service = service_for_path(m, path, NULL);
320
321 assert_se(access(test_path, F_OK) < 0);
322
323 assert_se(unit_start(unit) >= 0);
324 if (check_states(m, path, service, PATH_WAITING, SERVICE_DEAD) < 0)
325 return;
326
327 /* MakeDirectory default to no */
328 assert_se(access(test_path, F_OK) < 0);
329
330 assert_se(mkdir_p(test_path, 0755) >= 0);
331 test_file = strjoina(test_path, "test_file");
332 assert_se(touch(test_file) >= 0);
333 if (check_states(m, path, service, PATH_RUNNING, SERVICE_RUNNING) < 0)
334 return;
335
336 /* Service restarts if directory is still not empty */
337 assert_se(unit_stop(UNIT(service)) >= 0);
338 if (check_states(m, path, service, PATH_RUNNING, SERVICE_RUNNING) < 0)
339 return;
340
341 assert_se(rm_rf(test_path, REMOVE_ROOT|REMOVE_PHYSICAL) == 0);
342 assert_se(unit_stop(UNIT(service)) >= 0);
343 if (check_states(m, path, service, PATH_WAITING, SERVICE_DEAD) < 0)
344 return;
345
346 assert_se(unit_stop(unit) >= 0);
347 }
348
349 static void test_path_makedirectory_directorymode(Manager *m) {
350 const char *test_path = "/tmp/test-path_makedirectory/";
351 Unit *unit = NULL;
352 struct stat s;
353
354 assert_se(m);
355
356 assert_se(manager_load_startable_unit_or_warn(m, "path-makedirectory.path", NULL, &unit) >= 0);
357
358 assert_se(access(test_path, F_OK) < 0);
359
360 assert_se(unit_start(unit) >= 0);
361
362 /* Check if the directory has been created */
363 assert_se(access(test_path, F_OK) >= 0);
364
365 /* Check the mode we specified with DirectoryMode=0744 */
366 assert_se(stat(test_path, &s) >= 0);
367 assert_se((s.st_mode & S_IRWXU) == 0700);
368 assert_se((s.st_mode & S_IRWXG) == 0040);
369 assert_se((s.st_mode & S_IRWXO) == 0004);
370
371 assert_se(unit_stop(unit) >= 0);
372 (void) rm_rf(test_path, REMOVE_ROOT|REMOVE_PHYSICAL);
373 }
374
375 int main(int argc, char *argv[]) {
376 static const test_function_t tests[] = {
377 test_path_exists,
378 test_path_existsglob,
379 test_path_changed,
380 test_path_modified,
381 test_path_unit,
382 test_path_directorynotempty,
383 test_path_makedirectory_directorymode,
384 NULL,
385 };
386
387 _cleanup_free_ char *test_path = NULL;
388 _cleanup_(rm_rf_physical_and_freep) char *runtime_dir = NULL;
389
390 umask(022);
391
392 test_setup_logging(LOG_INFO);
393
394 assert_se(get_testdata_dir("test-path", &test_path) >= 0);
395 assert_se(set_unit_path(test_path) >= 0);
396 assert_se(runtime_dir = setup_fake_runtime_dir());
397
398 for (const test_function_t *test = tests; *test; test++) {
399 Manager *m = NULL;
400 int r;
401
402 /* We create a clean environment for each test */
403 r = setup_test(&m);
404 if (r != 0)
405 return r;
406
407 (*test)(m);
408
409 shutdown_test(m);
410 }
411
412 return 0;
413 }