1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
7 #include "alloc-util.h"
14 #include "path-util.h"
16 #include "string-util.h"
22 typedef void (*test_function_t
)(Manager
*m
);
24 static int setup_test(Manager
**m
) {
25 char **tests_path
= STRV_MAKE("exists", "existsglobFOOBAR", "changed", "modified", "unit",
26 "directorynotempty", "makedirectory");
32 r
= enter_cgroup_subroot(NULL
);
34 return log_tests_skipped("cgroupfs not available");
36 r
= manager_new(UNIT_FILE_USER
, MANAGER_TEST_RUN_BASIC
, &tmp
);
37 if (manager_errno_skip_test(r
))
38 return log_tests_skipped_errno(r
, "manager_new");
40 assert_se(manager_startup(tmp
, NULL
, NULL
, NULL
) >= 0);
42 STRV_FOREACH(test_path
, tests_path
) {
43 _cleanup_free_
char *p
= NULL
;
45 p
= strjoin("/tmp/test-path_", *test_path
);
48 (void) rm_rf(p
, REMOVE_ROOT
|REMOVE_PHYSICAL
);
56 static void shutdown_test(Manager
*m
) {
62 static Service
*service_for_path(Manager
*m
, Path
*path
, const char *service_name
) {
63 _cleanup_free_
char *tmp
= NULL
;
64 Unit
*service_unit
= NULL
;
70 assert_se(tmp
= strreplace(UNIT(path
)->id
, ".path", ".service"));
71 service_unit
= manager_get_unit(m
, tmp
);
73 service_unit
= manager_get_unit(m
, service_name
);
74 assert_se(service_unit
);
76 return SERVICE(service_unit
);
79 static int _check_states(unsigned line
,
80 Manager
*m
, Path
*path
, Service
*service
, PathState path_state
, ServiceState service_state
) {
84 usec_t end
= now(CLOCK_MONOTONIC
) + 30 * USEC_PER_SEC
;
86 while (path
->state
!= path_state
|| service
->state
!= service_state
||
87 path
->result
!= PATH_SUCCESS
|| service
->result
!= SERVICE_SUCCESS
) {
89 assert_se(sd_event_run(m
->event
, 100 * USEC_PER_MSEC
) >= 0);
91 usec_t n
= now(CLOCK_MONOTONIC
);
92 log_info("line %u: %s: state = %s; result = %s (left: %" PRIi64
")",
95 path_state_to_string(path
->state
),
96 path_result_to_string(path
->result
),
98 log_info("line %u: %s: state = %s; result = %s",
101 service_state_to_string(service
->state
),
102 service_result_to_string(service
->result
));
104 if (service
->state
== SERVICE_FAILED
&&
105 service
->main_exec_status
.status
== EXIT_CGROUP
&&
107 /* On a general purpose system we may fail to start the service for reasons which are
108 * not under our control: permission limits, resource exhaustion, etc. Let's skip the
109 * test in those cases. On developer machines we require proper setup. */
110 return log_notice_errno(SYNTHETIC_ERRNO(ECANCELED
),
111 "Failed to start service %s, aborting test: %s/%s",
113 service_state_to_string(service
->state
),
114 service_result_to_string(service
->result
));
117 log_error("Test timeout when testing %s", UNIT(path
)->id
);
124 #define check_states(...) _check_states(__LINE__, __VA_ARGS__)
126 static void test_path_exists(Manager
*m
) {
127 const char *test_path
= "/tmp/test-path_exists";
130 Service
*service
= NULL
;
134 assert_se(manager_load_startable_unit_or_warn(m
, "path-exists.path", NULL
, &unit
) >= 0);
137 service
= service_for_path(m
, path
, NULL
);
139 assert_se(unit_start(unit
) >= 0);
140 if (check_states(m
, path
, service
, PATH_WAITING
, SERVICE_DEAD
) < 0)
143 assert_se(touch(test_path
) >= 0);
144 if (check_states(m
, path
, service
, PATH_RUNNING
, SERVICE_RUNNING
) < 0)
147 /* Service restarts if file still exists */
148 assert_se(unit_stop(UNIT(service
)) >= 0);
149 if (check_states(m
, path
, service
, PATH_RUNNING
, SERVICE_RUNNING
) < 0)
152 assert_se(rm_rf(test_path
, REMOVE_ROOT
|REMOVE_PHYSICAL
) == 0);
153 assert_se(unit_stop(UNIT(service
)) >= 0);
154 if (check_states(m
, path
, service
, PATH_WAITING
, SERVICE_DEAD
) < 0)
157 assert_se(unit_stop(unit
) >= 0);
160 static void test_path_existsglob(Manager
*m
) {
161 const char *test_path
= "/tmp/test-path_existsglobFOOBAR";
164 Service
*service
= NULL
;
168 assert_se(manager_load_startable_unit_or_warn(m
, "path-existsglob.path", NULL
, &unit
) >= 0);
171 service
= service_for_path(m
, path
, NULL
);
173 assert_se(unit_start(unit
) >= 0);
174 if (check_states(m
, path
, service
, PATH_WAITING
, SERVICE_DEAD
) < 0)
177 assert_se(touch(test_path
) >= 0);
178 if (check_states(m
, path
, service
, PATH_RUNNING
, SERVICE_RUNNING
) < 0)
181 /* Service restarts if file still exists */
182 assert_se(unit_stop(UNIT(service
)) >= 0);
183 if (check_states(m
, path
, service
, PATH_RUNNING
, SERVICE_RUNNING
) < 0)
186 assert_se(rm_rf(test_path
, REMOVE_ROOT
|REMOVE_PHYSICAL
) == 0);
187 assert_se(unit_stop(UNIT(service
)) >= 0);
188 if (check_states(m
, path
, service
, PATH_WAITING
, SERVICE_DEAD
) < 0)
191 assert_se(unit_stop(unit
) >= 0);
194 static void test_path_changed(Manager
*m
) {
195 const char *test_path
= "/tmp/test-path_changed";
199 Service
*service
= NULL
;
203 assert_se(manager_load_startable_unit_or_warn(m
, "path-changed.path", NULL
, &unit
) >= 0);
206 service
= service_for_path(m
, path
, NULL
);
208 assert_se(unit_start(unit
) >= 0);
209 if (check_states(m
, path
, service
, PATH_WAITING
, SERVICE_DEAD
) < 0)
212 assert_se(touch(test_path
) >= 0);
213 if (check_states(m
, path
, service
, PATH_RUNNING
, SERVICE_RUNNING
) < 0)
216 /* Service does not restart if file still exists */
217 assert_se(unit_stop(UNIT(service
)) >= 0);
218 if (check_states(m
, path
, service
, PATH_WAITING
, SERVICE_DEAD
) < 0)
221 f
= fopen(test_path
, "w");
225 if (check_states(m
, path
, service
, PATH_RUNNING
, SERVICE_RUNNING
) < 0)
228 assert_se(unit_stop(UNIT(service
)) >= 0);
229 if (check_states(m
, path
, service
, PATH_WAITING
, SERVICE_DEAD
) < 0)
232 (void) rm_rf(test_path
, REMOVE_ROOT
|REMOVE_PHYSICAL
);
233 assert_se(unit_stop(unit
) >= 0);
236 static void test_path_modified(Manager
*m
) {
237 _cleanup_fclose_
FILE *f
= NULL
;
238 const char *test_path
= "/tmp/test-path_modified";
241 Service
*service
= NULL
;
245 assert_se(manager_load_startable_unit_or_warn(m
, "path-modified.path", NULL
, &unit
) >= 0);
248 service
= service_for_path(m
, path
, NULL
);
250 assert_se(unit_start(unit
) >= 0);
251 if (check_states(m
, path
, service
, PATH_WAITING
, SERVICE_DEAD
) < 0)
254 assert_se(touch(test_path
) >= 0);
255 if (check_states(m
, path
, service
, PATH_RUNNING
, SERVICE_RUNNING
) < 0)
258 /* Service does not restart if file still exists */
259 assert_se(unit_stop(UNIT(service
)) >= 0);
260 if (check_states(m
, path
, service
, PATH_WAITING
, SERVICE_DEAD
) < 0)
263 f
= fopen(test_path
, "w");
267 if (check_states(m
, path
, service
, PATH_RUNNING
, SERVICE_RUNNING
) < 0)
270 assert_se(unit_stop(UNIT(service
)) >= 0);
271 if (check_states(m
, path
, service
, PATH_WAITING
, SERVICE_DEAD
) < 0)
274 (void) rm_rf(test_path
, REMOVE_ROOT
|REMOVE_PHYSICAL
);
275 assert_se(unit_stop(unit
) >= 0);
278 static void test_path_unit(Manager
*m
) {
279 const char *test_path
= "/tmp/test-path_unit";
282 Service
*service
= NULL
;
286 assert_se(manager_load_startable_unit_or_warn(m
, "path-unit.path", NULL
, &unit
) >= 0);
289 service
= service_for_path(m
, path
, "path-mycustomunit.service");
291 assert_se(unit_start(unit
) >= 0);
292 if (check_states(m
, path
, service
, PATH_WAITING
, SERVICE_DEAD
) < 0)
295 assert_se(touch(test_path
) >= 0);
296 if (check_states(m
, path
, service
, PATH_RUNNING
, SERVICE_RUNNING
) < 0)
299 assert_se(rm_rf(test_path
, REMOVE_ROOT
|REMOVE_PHYSICAL
) == 0);
300 assert_se(unit_stop(UNIT(service
)) >= 0);
301 if (check_states(m
, path
, service
, PATH_WAITING
, SERVICE_DEAD
) < 0)
304 assert_se(unit_stop(unit
) >= 0);
307 static void test_path_directorynotempty(Manager
*m
) {
308 const char *test_file
, *test_path
= "/tmp/test-path_directorynotempty/";
311 Service
*service
= NULL
;
315 assert_se(manager_load_startable_unit_or_warn(m
, "path-directorynotempty.path", NULL
, &unit
) >= 0);
318 service
= service_for_path(m
, path
, NULL
);
320 assert_se(access(test_path
, F_OK
) < 0);
322 assert_se(unit_start(unit
) >= 0);
323 if (check_states(m
, path
, service
, PATH_WAITING
, SERVICE_DEAD
) < 0)
326 /* MakeDirectory default to no */
327 assert_se(access(test_path
, F_OK
) < 0);
329 assert_se(mkdir_p(test_path
, 0755) >= 0);
330 test_file
= strjoina(test_path
, "test_file");
331 assert_se(touch(test_file
) >= 0);
332 if (check_states(m
, path
, service
, PATH_RUNNING
, SERVICE_RUNNING
) < 0)
335 /* Service restarts if directory is still not empty */
336 assert_se(unit_stop(UNIT(service
)) >= 0);
337 if (check_states(m
, path
, service
, PATH_RUNNING
, SERVICE_RUNNING
) < 0)
340 assert_se(rm_rf(test_path
, REMOVE_ROOT
|REMOVE_PHYSICAL
) == 0);
341 assert_se(unit_stop(UNIT(service
)) >= 0);
342 if (check_states(m
, path
, service
, PATH_WAITING
, SERVICE_DEAD
) < 0)
345 assert_se(unit_stop(unit
) >= 0);
348 static void test_path_makedirectory_directorymode(Manager
*m
) {
349 const char *test_path
= "/tmp/test-path_makedirectory/";
355 assert_se(manager_load_startable_unit_or_warn(m
, "path-makedirectory.path", NULL
, &unit
) >= 0);
357 assert_se(access(test_path
, F_OK
) < 0);
359 assert_se(unit_start(unit
) >= 0);
361 /* Check if the directory has been created */
362 assert_se(access(test_path
, F_OK
) >= 0);
364 /* Check the mode we specified with DirectoryMode=0744 */
365 assert_se(stat(test_path
, &s
) >= 0);
366 assert_se((s
.st_mode
& S_IRWXU
) == 0700);
367 assert_se((s
.st_mode
& S_IRWXG
) == 0040);
368 assert_se((s
.st_mode
& S_IRWXO
) == 0004);
370 assert_se(unit_stop(unit
) >= 0);
371 (void) rm_rf(test_path
, REMOVE_ROOT
|REMOVE_PHYSICAL
);
374 int main(int argc
, char *argv
[]) {
375 static const test_function_t tests
[] = {
377 test_path_existsglob
,
381 test_path_directorynotempty
,
382 test_path_makedirectory_directorymode
,
386 _cleanup_free_
char *test_path
= NULL
;
387 _cleanup_(rm_rf_physical_and_freep
) char *runtime_dir
= NULL
;
391 test_setup_logging(LOG_INFO
);
393 assert_se(get_testdata_dir("test-path", &test_path
) >= 0);
394 assert_se(set_unit_path(test_path
) >= 0);
395 assert_se(runtime_dir
= setup_fake_runtime_dir());
397 for (const test_function_t
*test
= tests
; *test
; test
++) {
401 /* We create a clean environment for each test */