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/>.
27 #include "string-util.h"
33 typedef void (*test_function_t
)(Manager
*m
);
35 static int setup_test(Manager
**m
) {
36 char **tests_path
= STRV_MAKE("exists", "existsglobFOOBAR", "changed", "modified", "unit",
37 "directorynotempty", "makedirectory");
44 r
= manager_new(MANAGER_USER
, true, &tmp
);
45 if (IN_SET(r
, -EPERM
, -EACCES
, -EADDRINUSE
, -EHOSTDOWN
, -ENOENT
, -ENOEXEC
)) {
46 printf("Skipping test: manager_new: %s", strerror(-r
));
47 return -EXIT_TEST_SKIP
;
50 assert_se(manager_startup(tmp
, NULL
, NULL
) >= 0);
52 STRV_FOREACH(test_path
, tests_path
) {
53 _cleanup_free_
char *p
= NULL
;
55 p
= strjoin("/tmp/test-path_", *test_path
, NULL
);
58 (void) rm_rf(p
, REMOVE_ROOT
|REMOVE_PHYSICAL
);
66 static void shutdown_test(Manager
*m
) {
72 static void check_stop_unlink(Manager
*m
, Unit
*unit
, const char *test_path
, const char *service_name
) {
73 _cleanup_free_
char *tmp
= NULL
;
74 Unit
*service_unit
= NULL
;
75 Service
*service
= NULL
;
77 usec_t timeout
= 2 * USEC_PER_SEC
;
84 assert_se(tmp
= strreplace(unit
->id
, ".path", ".service"));
85 service_unit
= manager_get_unit(m
, tmp
);
87 service_unit
= manager_get_unit(m
, service_name
);
88 assert_se(service_unit
);
89 service
= SERVICE(service_unit
);
91 ts
= now(CLOCK_MONOTONIC
);
92 /* We process events until the service related to the path has been successfully started */
93 while(service
->result
!= SERVICE_SUCCESS
|| service
->state
!= SERVICE_START
) {
97 r
= sd_event_run(m
->event
, 100 * USEC_PER_MSEC
);
100 printf("%s: state = %s; result = %s \n",
102 service_state_to_string(service
->state
),
103 service_result_to_string(service
->result
));
106 /* But we timeout if the service has not been started in the allocated time */
107 n
= now(CLOCK_MONOTONIC
);
108 if (ts
+ timeout
< n
) {
109 log_error("Test timeout when testing %s", unit
->id
);
114 assert_se(UNIT_VTABLE(unit
)->stop(unit
) >= 0);
115 (void) rm_rf(test_path
, REMOVE_ROOT
|REMOVE_PHYSICAL
);
118 static void test_path_exists(Manager
*m
) {
119 const char *test_path
= "/tmp/test-path_exists";
124 assert_se(manager_load_unit(m
, "path-exists.path", NULL
, NULL
, &unit
) >= 0);
125 assert_se(UNIT_VTABLE(unit
)->start(unit
) >= 0);
127 assert_se(touch(test_path
) >= 0);
129 check_stop_unlink(m
, unit
, test_path
, NULL
);
132 static void test_path_existsglob(Manager
*m
) {
133 const char *test_path
= "/tmp/test-path_existsglobFOOBAR";
137 assert_se(manager_load_unit(m
, "path-existsglob.path", NULL
, NULL
, &unit
) >= 0);
138 assert_se(UNIT_VTABLE(unit
)->start(unit
) >= 0);
140 assert_se(touch(test_path
) >= 0);
142 check_stop_unlink(m
, unit
, test_path
, NULL
);
145 static void test_path_changed(Manager
*m
) {
146 const char *test_path
= "/tmp/test-path_changed";
152 assert_se(touch(test_path
) >= 0);
154 assert_se(manager_load_unit(m
, "path-changed.path", NULL
, NULL
, &unit
) >= 0);
155 assert_se(UNIT_VTABLE(unit
)->start(unit
) >= 0);
157 f
= fopen(test_path
, "w");
161 check_stop_unlink(m
, unit
, test_path
, NULL
);
164 static void test_path_modified(Manager
*m
) {
165 _cleanup_fclose_
FILE *f
= NULL
;
166 const char *test_path
= "/tmp/test-path_modified";
171 assert_se(touch(test_path
) >= 0);
173 assert_se(manager_load_unit(m
, "path-modified.path", NULL
, NULL
, &unit
) >= 0);
174 assert_se(UNIT_VTABLE(unit
)->start(unit
) >= 0);
176 f
= fopen(test_path
, "w");
180 check_stop_unlink(m
, unit
, test_path
, NULL
);
183 static void test_path_unit(Manager
*m
) {
184 const char *test_path
= "/tmp/test-path_unit";
189 assert_se(manager_load_unit(m
, "path-unit.path", NULL
, NULL
, &unit
) >= 0);
190 assert_se(UNIT_VTABLE(unit
)->start(unit
) >= 0);
192 assert_se(touch(test_path
) >= 0);
194 check_stop_unlink(m
, unit
, test_path
, "path-mycustomunit.service");
197 static void test_path_directorynotempty(Manager
*m
) {
198 const char *test_path
= "/tmp/test-path_directorynotempty/";
203 assert_se(access(test_path
, F_OK
) < 0);
205 assert_se(manager_load_unit(m
, "path-directorynotempty.path", NULL
, NULL
, &unit
) >= 0);
206 assert_se(UNIT_VTABLE(unit
)->start(unit
) >= 0);
208 /* MakeDirectory default to no */
209 assert_se(access(test_path
, F_OK
) < 0);
211 assert_se(mkdir_p(test_path
, 0755) >= 0);
212 assert_se(touch(strjoina(test_path
, "test_file")) >= 0);
214 check_stop_unlink(m
, unit
, test_path
, NULL
);
217 static void test_path_makedirectory_directorymode(Manager
*m
) {
218 const char *test_path
= "/tmp/test-path_makedirectory/";
224 assert_se(access(test_path
, F_OK
) < 0);
226 assert_se(manager_load_unit(m
, "path-makedirectory.path", NULL
, NULL
, &unit
) >= 0);
227 assert_se(UNIT_VTABLE(unit
)->start(unit
) >= 0);
229 /* Check if the directory has been created */
230 assert_se(access(test_path
, F_OK
) >= 0);
232 /* Check the mode we specified with DirectoryMode=0744 */
233 assert_se(stat(test_path
, &s
) >= 0);
234 assert_se((s
.st_mode
& S_IRWXU
) == 0700);
235 assert_se((s
.st_mode
& S_IRWXG
) == 0040);
236 assert_se((s
.st_mode
& S_IRWXO
) == 0004);
238 assert_se(UNIT_VTABLE(unit
)->stop(unit
) >= 0);
239 (void) rm_rf(test_path
, REMOVE_ROOT
|REMOVE_PHYSICAL
);
242 int main(int argc
, char *argv
[]) {
243 test_function_t tests
[] = {
245 test_path_existsglob
,
249 test_path_directorynotempty
,
250 test_path_makedirectory_directorymode
,
253 test_function_t
*test
= NULL
;
256 log_parse_environment();
259 assert_se(set_unit_path(TEST_DIR
) >= 0);
261 for (test
= tests
; test
&& *test
; test
++) {
264 /* We create a clean environment for each test */