2 This file is part of systemd.
4 Copyright 2014 Ronny Chevalier
6 systemd is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as published by
8 the Free Software Foundation; either version 2.1 of the License, or
9 (at your option) any later version.
11 systemd is distributed in the hope that it will be useful, but
12 WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Lesser General Public License for more details.
16 You should have received a copy of the GNU Lesser General Public License
17 along with systemd; If not, see <http://www.gnu.org/licenses/>.
23 #include "alloc-util.h"
30 #include "string-util.h"
32 #include "test-helper.h"
37 typedef void (*test_function_t
)(Manager
*m
);
39 static int setup_test(Manager
**m
) {
40 char **tests_path
= STRV_MAKE("exists", "existsglobFOOBAR", "changed", "modified", "unit",
41 "directorynotempty", "makedirectory");
48 enter_cgroup_subroot();
50 r
= manager_new(UNIT_FILE_USER
, MANAGER_TEST_RUN_MINIMAL
, &tmp
);
51 if (MANAGER_SKIP_TEST(r
)) {
52 log_notice_errno(r
, "Skipping test: manager_new: %m");
53 return -EXIT_TEST_SKIP
;
56 assert_se(manager_startup(tmp
, NULL
, NULL
) >= 0);
58 STRV_FOREACH(test_path
, tests_path
) {
59 _cleanup_free_
char *p
= NULL
;
61 p
= strjoin("/tmp/test-path_", *test_path
);
64 (void) rm_rf(p
, REMOVE_ROOT
|REMOVE_PHYSICAL
);
72 static void shutdown_test(Manager
*m
) {
78 static void check_stop_unlink(Manager
*m
, Unit
*unit
, const char *test_path
, const char *service_name
) {
79 _cleanup_free_
char *tmp
= NULL
;
80 Unit
*service_unit
= NULL
;
81 Service
*service
= NULL
;
83 usec_t timeout
= 2 * USEC_PER_SEC
;
90 assert_se(tmp
= strreplace(unit
->id
, ".path", ".service"));
91 service_unit
= manager_get_unit(m
, tmp
);
93 service_unit
= manager_get_unit(m
, service_name
);
94 assert_se(service_unit
);
95 service
= SERVICE(service_unit
);
97 ts
= now(CLOCK_MONOTONIC
);
98 /* We process events until the service related to the path has been successfully started */
99 while (service
->result
!= SERVICE_SUCCESS
|| service
->state
!= SERVICE_START
) {
103 r
= sd_event_run(m
->event
, 100 * USEC_PER_MSEC
);
106 printf("%s: state = %s; result = %s \n",
108 service_state_to_string(service
->state
),
109 service_result_to_string(service
->result
));
112 /* But we timeout if the service has not been started in the allocated time */
113 n
= now(CLOCK_MONOTONIC
);
114 if (ts
+ timeout
< n
) {
115 log_error("Test timeout when testing %s", unit
->id
);
120 assert_se(UNIT_VTABLE(unit
)->stop(unit
) >= 0);
121 (void) rm_rf(test_path
, REMOVE_ROOT
|REMOVE_PHYSICAL
);
124 static void test_path_exists(Manager
*m
) {
125 const char *test_path
= "/tmp/test-path_exists";
130 assert_se(manager_load_unit(m
, "path-exists.path", NULL
, NULL
, &unit
) >= 0);
131 assert_se(UNIT_VTABLE(unit
)->start(unit
) >= 0);
133 assert_se(touch(test_path
) >= 0);
135 check_stop_unlink(m
, unit
, test_path
, NULL
);
138 static void test_path_existsglob(Manager
*m
) {
139 const char *test_path
= "/tmp/test-path_existsglobFOOBAR";
143 assert_se(manager_load_unit(m
, "path-existsglob.path", NULL
, NULL
, &unit
) >= 0);
144 assert_se(UNIT_VTABLE(unit
)->start(unit
) >= 0);
146 assert_se(touch(test_path
) >= 0);
148 check_stop_unlink(m
, unit
, test_path
, NULL
);
151 static void test_path_changed(Manager
*m
) {
152 const char *test_path
= "/tmp/test-path_changed";
158 assert_se(touch(test_path
) >= 0);
160 assert_se(manager_load_unit(m
, "path-changed.path", NULL
, NULL
, &unit
) >= 0);
161 assert_se(UNIT_VTABLE(unit
)->start(unit
) >= 0);
163 f
= fopen(test_path
, "w");
167 check_stop_unlink(m
, unit
, test_path
, NULL
);
170 static void test_path_modified(Manager
*m
) {
171 _cleanup_fclose_
FILE *f
= NULL
;
172 const char *test_path
= "/tmp/test-path_modified";
177 assert_se(touch(test_path
) >= 0);
179 assert_se(manager_load_unit(m
, "path-modified.path", NULL
, NULL
, &unit
) >= 0);
180 assert_se(UNIT_VTABLE(unit
)->start(unit
) >= 0);
182 f
= fopen(test_path
, "w");
186 check_stop_unlink(m
, unit
, test_path
, NULL
);
189 static void test_path_unit(Manager
*m
) {
190 const char *test_path
= "/tmp/test-path_unit";
195 assert_se(manager_load_unit(m
, "path-unit.path", NULL
, NULL
, &unit
) >= 0);
196 assert_se(UNIT_VTABLE(unit
)->start(unit
) >= 0);
198 assert_se(touch(test_path
) >= 0);
200 check_stop_unlink(m
, unit
, test_path
, "path-mycustomunit.service");
203 static void test_path_directorynotempty(Manager
*m
) {
204 const char *test_path
= "/tmp/test-path_directorynotempty/";
209 assert_se(access(test_path
, F_OK
) < 0);
211 assert_se(manager_load_unit(m
, "path-directorynotempty.path", NULL
, NULL
, &unit
) >= 0);
212 assert_se(UNIT_VTABLE(unit
)->start(unit
) >= 0);
214 /* MakeDirectory default to no */
215 assert_se(access(test_path
, F_OK
) < 0);
217 assert_se(mkdir_p(test_path
, 0755) >= 0);
218 assert_se(touch(strjoina(test_path
, "test_file")) >= 0);
220 check_stop_unlink(m
, unit
, test_path
, NULL
);
223 static void test_path_makedirectory_directorymode(Manager
*m
) {
224 const char *test_path
= "/tmp/test-path_makedirectory/";
230 assert_se(access(test_path
, F_OK
) < 0);
232 assert_se(manager_load_unit(m
, "path-makedirectory.path", NULL
, NULL
, &unit
) >= 0);
233 assert_se(UNIT_VTABLE(unit
)->start(unit
) >= 0);
235 /* Check if the directory has been created */
236 assert_se(access(test_path
, F_OK
) >= 0);
238 /* Check the mode we specified with DirectoryMode=0744 */
239 assert_se(stat(test_path
, &s
) >= 0);
240 assert_se((s
.st_mode
& S_IRWXU
) == 0700);
241 assert_se((s
.st_mode
& S_IRWXG
) == 0040);
242 assert_se((s
.st_mode
& S_IRWXO
) == 0004);
244 assert_se(UNIT_VTABLE(unit
)->stop(unit
) >= 0);
245 (void) rm_rf(test_path
, REMOVE_ROOT
|REMOVE_PHYSICAL
);
248 int main(int argc
, char *argv
[]) {
249 static const test_function_t tests
[] = {
251 test_path_existsglob
,
255 test_path_directorynotempty
,
256 test_path_makedirectory_directorymode
,
260 _cleanup_(rm_rf_physical_and_freep
) char *runtime_dir
= NULL
;
261 const test_function_t
*test
= NULL
;
264 log_parse_environment();
267 assert_se(set_unit_path(get_testdata_dir("/test-path")) >= 0);
268 assert_se(runtime_dir
= setup_fake_runtime_dir());
270 for (test
= tests
; test
&& *test
; test
++) {
273 /* We create a clean environment for each test */