Debian Codegraph @

src/test/test-path.c

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