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");
33 r
= enter_cgroup_subroot(NULL
);
35 return log_tests_skipped("cgroupfs not available");
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");
41 assert_se(manager_startup(tmp
, NULL
, NULL
, NULL
) >= 0);
43 STRV_FOREACH(test_path
, tests_path
) {
44 _cleanup_free_
char *p
= NULL
;
46 p
= strjoin("/tmp/test-path_", *test_path
);
49 (void) rm_rf(p
, REMOVE_ROOT
|REMOVE_PHYSICAL
);
57 static void shutdown_test(Manager
*m
) {
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
;
71 assert_se(tmp
= strreplace(UNIT(path
)->id
, ".path", ".service"));
72 service_unit
= manager_get_unit(m
, tmp
);
74 service_unit
= manager_get_unit(m
, service_name
);
75 assert_se(service_unit
);
77 return SERVICE(service_unit
);
80 static int _check_states(unsigned line
,
81 Manager
*m
, Path
*path
, Service
*service
, PathState path_state
, ServiceState service_state
) {
85 usec_t end
= now(CLOCK_MONOTONIC
) + 30 * USEC_PER_SEC
;
87 while (path
->state
!= path_state
|| service
->state
!= service_state
||
88 path
->result
!= PATH_SUCCESS
|| service
->result
!= SERVICE_SUCCESS
) {
90 assert_se(sd_event_run(m
->event
, 100 * USEC_PER_MSEC
) >= 0);
92 usec_t n
= now(CLOCK_MONOTONIC
);
93 log_info("line %u: %s: state = %s; result = %s (left: %" PRIi64
")",
96 path_state_to_string(path
->state
),
97 path_result_to_string(path
->result
),
99 log_info("line %u: %s: state = %s; result = %s",
102 service_state_to_string(service
->state
),
103 service_result_to_string(service
->result
));
105 if (service
->state
== SERVICE_FAILED
&&
106 service
->main_exec_status
.status
== EXIT_CGROUP
&&
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",
114 service_state_to_string(service
->state
),
115 service_result_to_string(service
->result
));
118 log_error("Test timeout when testing %s", UNIT(path
)->id
);
125 #define check_states(...) _check_states(__LINE__, __VA_ARGS__)
127 static void test_path_exists(Manager
*m
) {
128 const char *test_path
= "/tmp/test-path_exists";
131 Service
*service
= NULL
;
135 assert_se(manager_load_startable_unit_or_warn(m
, "path-exists.path", NULL
, &unit
) >= 0);
138 service
= service_for_path(m
, path
, NULL
);
140 assert_se(unit_start(unit
) >= 0);
141 if (check_states(m
, path
, service
, PATH_WAITING
, SERVICE_DEAD
) < 0)
144 assert_se(touch(test_path
) >= 0);
145 if (check_states(m
, path
, service
, PATH_RUNNING
, SERVICE_RUNNING
) < 0)
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)
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)
158 assert_se(unit_stop(unit
) >= 0);
161 static void test_path_existsglob(Manager
*m
) {
162 const char *test_path
= "/tmp/test-path_existsglobFOOBAR";
165 Service
*service
= NULL
;
169 assert_se(manager_load_startable_unit_or_warn(m
, "path-existsglob.path", NULL
, &unit
) >= 0);
172 service
= service_for_path(m
, path
, NULL
);
174 assert_se(unit_start(unit
) >= 0);
175 if (check_states(m
, path
, service
, PATH_WAITING
, SERVICE_DEAD
) < 0)
178 assert_se(touch(test_path
) >= 0);
179 if (check_states(m
, path
, service
, PATH_RUNNING
, SERVICE_RUNNING
) < 0)
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)
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)
192 assert_se(unit_stop(unit
) >= 0);
195 static void test_path_changed(Manager
*m
) {
196 const char *test_path
= "/tmp/test-path_changed";
200 Service
*service
= NULL
;
204 assert_se(manager_load_startable_unit_or_warn(m
, "path-changed.path", NULL
, &unit
) >= 0);
207 service
= service_for_path(m
, path
, NULL
);
209 assert_se(unit_start(unit
) >= 0);
210 if (check_states(m
, path
, service
, PATH_WAITING
, SERVICE_DEAD
) < 0)
213 assert_se(touch(test_path
) >= 0);
214 if (check_states(m
, path
, service
, PATH_RUNNING
, SERVICE_RUNNING
) < 0)
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)
222 f
= fopen(test_path
, "w");
226 if (check_states(m
, path
, service
, PATH_RUNNING
, SERVICE_RUNNING
) < 0)
229 assert_se(unit_stop(UNIT(service
)) >= 0);
230 if (check_states(m
, path
, service
, PATH_WAITING
, SERVICE_DEAD
) < 0)
233 (void) rm_rf(test_path
, REMOVE_ROOT
|REMOVE_PHYSICAL
);
234 assert_se(unit_stop(unit
) >= 0);
237 static void test_path_modified(Manager
*m
) {
238 _cleanup_fclose_
FILE *f
= NULL
;
239 const char *test_path
= "/tmp/test-path_modified";
242 Service
*service
= NULL
;
246 assert_se(manager_load_startable_unit_or_warn(m
, "path-modified.path", NULL
, &unit
) >= 0);
249 service
= service_for_path(m
, path
, NULL
);
251 assert_se(unit_start(unit
) >= 0);
252 if (check_states(m
, path
, service
, PATH_WAITING
, SERVICE_DEAD
) < 0)
255 assert_se(touch(test_path
) >= 0);
256 if (check_states(m
, path
, service
, PATH_RUNNING
, SERVICE_RUNNING
) < 0)
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)
264 f
= fopen(test_path
, "w");
268 if (check_states(m
, path
, service
, PATH_RUNNING
, SERVICE_RUNNING
) < 0)
271 assert_se(unit_stop(UNIT(service
)) >= 0);
272 if (check_states(m
, path
, service
, PATH_WAITING
, SERVICE_DEAD
) < 0)
275 (void) rm_rf(test_path
, REMOVE_ROOT
|REMOVE_PHYSICAL
);
276 assert_se(unit_stop(unit
) >= 0);
279 static void test_path_unit(Manager
*m
) {
280 const char *test_path
= "/tmp/test-path_unit";
283 Service
*service
= NULL
;
287 assert_se(manager_load_startable_unit_or_warn(m
, "path-unit.path", NULL
, &unit
) >= 0);
290 service
= service_for_path(m
, path
, "path-mycustomunit.service");
292 assert_se(unit_start(unit
) >= 0);
293 if (check_states(m
, path
, service
, PATH_WAITING
, SERVICE_DEAD
) < 0)
296 assert_se(touch(test_path
) >= 0);
297 if (check_states(m
, path
, service
, PATH_RUNNING
, SERVICE_RUNNING
) < 0)
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)
305 assert_se(unit_stop(unit
) >= 0);
308 static void test_path_directorynotempty(Manager
*m
) {
309 const char *test_file
, *test_path
= "/tmp/test-path_directorynotempty/";
312 Service
*service
= NULL
;
316 assert_se(manager_load_startable_unit_or_warn(m
, "path-directorynotempty.path", NULL
, &unit
) >= 0);
319 service
= service_for_path(m
, path
, NULL
);
321 assert_se(access(test_path
, F_OK
) < 0);
323 assert_se(unit_start(unit
) >= 0);
324 if (check_states(m
, path
, service
, PATH_WAITING
, SERVICE_DEAD
) < 0)
327 /* MakeDirectory default to no */
328 assert_se(access(test_path
, F_OK
) < 0);
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)
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)
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)
346 assert_se(unit_stop(unit
) >= 0);
349 static void test_path_makedirectory_directorymode(Manager
*m
) {
350 const char *test_path
= "/tmp/test-path_makedirectory/";
356 assert_se(manager_load_startable_unit_or_warn(m
, "path-makedirectory.path", NULL
, &unit
) >= 0);
358 assert_se(access(test_path
, F_OK
) < 0);
360 assert_se(unit_start(unit
) >= 0);
362 /* Check if the directory has been created */
363 assert_se(access(test_path
, F_OK
) >= 0);
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);
371 assert_se(unit_stop(unit
) >= 0);
372 (void) rm_rf(test_path
, REMOVE_ROOT
|REMOVE_PHYSICAL
);
375 int main(int argc
, char *argv
[]) {
376 static const test_function_t tests
[] = {
378 test_path_existsglob
,
382 test_path_directorynotempty
,
383 test_path_makedirectory_directorymode
,
387 _cleanup_free_
char *test_path
= NULL
;
388 _cleanup_(rm_rf_physical_and_freep
) char *runtime_dir
= NULL
;
392 test_setup_logging(LOG_INFO
);
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());
398 for (const test_function_t
*test
= tests
; *test
; test
++) {
402 /* We create a clean environment for each test */