1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
8 #include "alloc-util.h"
14 #include "string-util.h"
19 typedef void (*test_function_t
)(Manager
*m
);
21 static int setup_test(Manager
**m
) {
22 char **tests_path
= STRV_MAKE("exists", "existsglobFOOBAR", "changed", "modified", "unit",
23 "directorynotempty", "makedirectory");
29 r
= enter_cgroup_subroot(NULL
);
31 return log_tests_skipped("cgroupfs not available");
33 r
= manager_new(RUNTIME_SCOPE_USER
, MANAGER_TEST_RUN_BASIC
, &tmp
);
34 if (manager_errno_skip_test(r
))
35 return log_tests_skipped_errno(r
, "manager_new");
37 assert_se(manager_startup(tmp
, NULL
, NULL
, NULL
) >= 0);
39 STRV_FOREACH(test_path
, tests_path
) {
40 _cleanup_free_
char *p
= NULL
;
42 p
= strjoin("/tmp/test-path_", *test_path
);
45 (void) rm_rf(p
, REMOVE_ROOT
|REMOVE_PHYSICAL
);
53 static void shutdown_test(Manager
*m
) {
59 static Service
*service_for_path(Manager
*m
, Path
*path
, const char *service_name
) {
60 _cleanup_free_
char *tmp
= NULL
;
61 Unit
*service_unit
= NULL
;
67 assert_se(tmp
= strreplace(UNIT(path
)->id
, ".path", ".service"));
68 service_unit
= manager_get_unit(m
, tmp
);
70 service_unit
= manager_get_unit(m
, service_name
);
71 assert_se(service_unit
);
73 return SERVICE(service_unit
);
76 static int _check_states(unsigned line
,
77 Manager
*m
, Path
*path
, Service
*service
, PathState path_state
, ServiceState service_state
) {
81 usec_t end
= now(CLOCK_MONOTONIC
) + 30 * USEC_PER_SEC
;
83 while (path
->state
!= path_state
|| service
->state
!= service_state
||
84 path
->result
!= PATH_SUCCESS
|| service
->result
!= SERVICE_SUCCESS
) {
86 assert_se(sd_event_run(m
->event
, 100 * USEC_PER_MSEC
) >= 0);
88 usec_t n
= now(CLOCK_MONOTONIC
);
89 log_info("line %u: %s: state = %s; result = %s (left: %" PRIi64
")",
92 path_state_to_string(path
->state
),
93 path_result_to_string(path
->result
),
95 log_info("line %u: %s: state = %s; result = %s",
98 service_state_to_string(service
->state
),
99 service_result_to_string(service
->result
));
101 if (service
->state
== SERVICE_FAILED
&&
102 (service
->main_exec_status
.status
== EXIT_CGROUP
|| service
->result
== SERVICE_FAILURE_RESOURCES
)) {
103 const char *ci
= ci_environment();
105 /* On a general purpose system we may fail to start the service for reasons which are
106 * not under our control: permission limits, resource exhaustion, etc. Let's skip the
107 * test in those cases. On developer machines we require proper setup. */
109 return log_notice_errno(SYNTHETIC_ERRNO(ECANCELED
),
110 "Failed to start service %s, aborting test: %s/%s",
112 service_state_to_string(service
->state
),
113 service_result_to_string(service
->result
));
115 /* On Salsa we can't setup cgroups so the unit always fails. The test checks if it
116 * can but continues if it cannot at the beginning, but on Salsa it fails here. */
117 if (streq(ci
, "salsa-ci"))
118 exit(EXIT_TEST_SKIP
);
122 log_error("Test timeout when testing %s", UNIT(path
)->id
);
129 #define check_states(...) _check_states(__LINE__, __VA_ARGS__)
131 static void test_path_exists(Manager
*m
) {
132 const char *test_path
= "/tmp/test-path_exists";
135 Service
*service
= NULL
;
139 assert_se(manager_load_startable_unit_or_warn(m
, "path-exists.path", NULL
, &unit
) >= 0);
142 service
= service_for_path(m
, path
, NULL
);
144 assert_se(unit_start(unit
, NULL
) >= 0);
145 if (check_states(m
, path
, service
, PATH_WAITING
, SERVICE_DEAD
) < 0)
148 assert_se(touch(test_path
) >= 0);
149 if (check_states(m
, path
, service
, PATH_RUNNING
, SERVICE_RUNNING
) < 0)
152 /* Service restarts if file still exists */
153 assert_se(unit_stop(UNIT(service
)) >= 0);
154 if (check_states(m
, path
, service
, PATH_RUNNING
, SERVICE_RUNNING
) < 0)
157 assert_se(rm_rf(test_path
, REMOVE_ROOT
|REMOVE_PHYSICAL
) == 0);
158 assert_se(unit_stop(UNIT(service
)) >= 0);
159 if (check_states(m
, path
, service
, PATH_WAITING
, SERVICE_DEAD
) < 0)
162 assert_se(unit_stop(unit
) >= 0);
165 static void test_path_existsglob(Manager
*m
) {
166 const char *test_path
= "/tmp/test-path_existsglobFOOBAR";
169 Service
*service
= NULL
;
173 assert_se(manager_load_startable_unit_or_warn(m
, "path-existsglob.path", NULL
, &unit
) >= 0);
176 service
= service_for_path(m
, path
, NULL
);
178 assert_se(unit_start(unit
, NULL
) >= 0);
179 if (check_states(m
, path
, service
, PATH_WAITING
, SERVICE_DEAD
) < 0)
182 assert_se(touch(test_path
) >= 0);
183 if (check_states(m
, path
, service
, PATH_RUNNING
, SERVICE_RUNNING
) < 0)
186 /* Service restarts if file still exists */
187 assert_se(unit_stop(UNIT(service
)) >= 0);
188 if (check_states(m
, path
, service
, PATH_RUNNING
, SERVICE_RUNNING
) < 0)
191 assert_se(rm_rf(test_path
, REMOVE_ROOT
|REMOVE_PHYSICAL
) == 0);
192 assert_se(unit_stop(UNIT(service
)) >= 0);
193 if (check_states(m
, path
, service
, PATH_WAITING
, SERVICE_DEAD
) < 0)
196 assert_se(unit_stop(unit
) >= 0);
199 static void test_path_changed(Manager
*m
) {
200 const char *test_path
= "/tmp/test-path_changed";
204 Service
*service
= NULL
;
208 assert_se(manager_load_startable_unit_or_warn(m
, "path-changed.path", NULL
, &unit
) >= 0);
211 service
= service_for_path(m
, path
, NULL
);
213 assert_se(unit_start(unit
, NULL
) >= 0);
214 if (check_states(m
, path
, service
, PATH_WAITING
, SERVICE_DEAD
) < 0)
217 assert_se(touch(test_path
) >= 0);
218 if (check_states(m
, path
, service
, PATH_RUNNING
, SERVICE_RUNNING
) < 0)
221 /* Service does not restart if file still exists */
222 assert_se(unit_stop(UNIT(service
)) >= 0);
223 if (check_states(m
, path
, service
, PATH_WAITING
, SERVICE_DEAD
) < 0)
226 f
= fopen(test_path
, "w");
230 if (check_states(m
, path
, service
, PATH_RUNNING
, SERVICE_RUNNING
) < 0)
233 assert_se(unit_stop(UNIT(service
)) >= 0);
234 if (check_states(m
, path
, service
, PATH_WAITING
, SERVICE_DEAD
) < 0)
237 (void) rm_rf(test_path
, REMOVE_ROOT
|REMOVE_PHYSICAL
);
238 assert_se(unit_stop(unit
) >= 0);
241 static void test_path_modified(Manager
*m
) {
242 _cleanup_fclose_
FILE *f
= NULL
;
243 const char *test_path
= "/tmp/test-path_modified";
246 Service
*service
= NULL
;
250 assert_se(manager_load_startable_unit_or_warn(m
, "path-modified.path", NULL
, &unit
) >= 0);
253 service
= service_for_path(m
, path
, NULL
);
255 assert_se(unit_start(unit
, NULL
) >= 0);
256 if (check_states(m
, path
, service
, PATH_WAITING
, SERVICE_DEAD
) < 0)
259 assert_se(touch(test_path
) >= 0);
260 if (check_states(m
, path
, service
, PATH_RUNNING
, SERVICE_RUNNING
) < 0)
263 /* Service does not restart if file still exists */
264 assert_se(unit_stop(UNIT(service
)) >= 0);
265 if (check_states(m
, path
, service
, PATH_WAITING
, SERVICE_DEAD
) < 0)
268 f
= fopen(test_path
, "w");
272 if (check_states(m
, path
, service
, PATH_RUNNING
, SERVICE_RUNNING
) < 0)
275 assert_se(unit_stop(UNIT(service
)) >= 0);
276 if (check_states(m
, path
, service
, PATH_WAITING
, SERVICE_DEAD
) < 0)
279 (void) rm_rf(test_path
, REMOVE_ROOT
|REMOVE_PHYSICAL
);
280 assert_se(unit_stop(unit
) >= 0);
283 static void test_path_unit(Manager
*m
) {
284 const char *test_path
= "/tmp/test-path_unit";
287 Service
*service
= NULL
;
291 assert_se(manager_load_startable_unit_or_warn(m
, "path-unit.path", NULL
, &unit
) >= 0);
294 service
= service_for_path(m
, path
, "path-mycustomunit.service");
296 assert_se(unit_start(unit
, NULL
) >= 0);
297 if (check_states(m
, path
, service
, PATH_WAITING
, SERVICE_DEAD
) < 0)
300 assert_se(touch(test_path
) >= 0);
301 if (check_states(m
, path
, service
, PATH_RUNNING
, SERVICE_RUNNING
) < 0)
304 assert_se(rm_rf(test_path
, REMOVE_ROOT
|REMOVE_PHYSICAL
) == 0);
305 assert_se(unit_stop(UNIT(service
)) >= 0);
306 if (check_states(m
, path
, service
, PATH_WAITING
, SERVICE_DEAD
) < 0)
309 assert_se(unit_stop(unit
) >= 0);
312 static void test_path_directorynotempty(Manager
*m
) {
313 const char *test_file
, *test_path
= "/tmp/test-path_directorynotempty/";
316 Service
*service
= NULL
;
320 assert_se(manager_load_startable_unit_or_warn(m
, "path-directorynotempty.path", NULL
, &unit
) >= 0);
323 service
= service_for_path(m
, path
, NULL
);
325 assert_se(access(test_path
, F_OK
) < 0);
327 assert_se(unit_start(unit
, NULL
) >= 0);
328 if (check_states(m
, path
, service
, PATH_WAITING
, SERVICE_DEAD
) < 0)
331 /* MakeDirectory default to no */
332 assert_se(access(test_path
, F_OK
) < 0);
334 assert_se(mkdir_p(test_path
, 0755) >= 0);
335 test_file
= strjoina(test_path
, "test_file");
336 assert_se(touch(test_file
) >= 0);
337 if (check_states(m
, path
, service
, PATH_RUNNING
, SERVICE_RUNNING
) < 0)
340 /* Service restarts if directory is still not empty */
341 assert_se(unit_stop(UNIT(service
)) >= 0);
342 if (check_states(m
, path
, service
, PATH_RUNNING
, SERVICE_RUNNING
) < 0)
345 assert_se(rm_rf(test_path
, REMOVE_ROOT
|REMOVE_PHYSICAL
) == 0);
346 assert_se(unit_stop(UNIT(service
)) >= 0);
347 if (check_states(m
, path
, service
, PATH_WAITING
, SERVICE_DEAD
) < 0)
350 assert_se(unit_stop(unit
) >= 0);
353 static void test_path_makedirectory_directorymode(Manager
*m
) {
354 const char *test_path
= "/tmp/test-path_makedirectory/";
360 assert_se(manager_load_startable_unit_or_warn(m
, "path-makedirectory.path", NULL
, &unit
) >= 0);
362 assert_se(access(test_path
, F_OK
) < 0);
364 assert_se(unit_start(unit
, NULL
) >= 0);
366 /* Check if the directory has been created */
367 assert_se(access(test_path
, F_OK
) >= 0);
369 /* Check the mode we specified with DirectoryMode=0744 */
370 assert_se(stat(test_path
, &s
) >= 0);
371 assert_se((s
.st_mode
& S_IRWXU
) == 0700);
372 assert_se((s
.st_mode
& S_IRWXG
) == 0040);
373 assert_se((s
.st_mode
& S_IRWXO
) == 0004);
375 assert_se(unit_stop(unit
) >= 0);
376 (void) rm_rf(test_path
, REMOVE_ROOT
|REMOVE_PHYSICAL
);
379 int main(int argc
, char *argv
[]) {
380 static const test_function_t tests
[] = {
382 test_path_existsglob
,
386 test_path_directorynotempty
,
387 test_path_makedirectory_directorymode
,
391 _cleanup_free_
char *test_path
= NULL
;
392 _cleanup_(rm_rf_physical_and_freep
) char *runtime_dir
= NULL
;
396 test_setup_logging(LOG_INFO
);
398 ASSERT_OK(get_testdata_dir("test-path", &test_path
));
399 ASSERT_OK(setenv_unit_path(test_path
));
400 assert_se(runtime_dir
= setup_fake_runtime_dir());
402 for (const test_function_t
*test
= tests
; *test
; test
++) {
406 /* We create a clean environment for each test */