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"
36 typedef void (*test_function_t
)(Manager
*m
);
38 static int setup_test(Manager
**m
) {
39 char **tests_path
= STRV_MAKE("exists", "existsglobFOOBAR", "changed", "modified", "unit",
40 "directorynotempty", "makedirectory");
47 r
= manager_new(MANAGER_USER
, true, &tmp
);
48 if (MANAGER_SKIP_TEST(r
)) {
49 printf("Skipping test: manager_new: %s\n", strerror(-r
));
50 return -EXIT_TEST_SKIP
;
53 assert_se(manager_startup(tmp
, NULL
, NULL
) >= 0);
55 STRV_FOREACH(test_path
, tests_path
) {
56 _cleanup_free_
char *p
= NULL
;
58 p
= strjoin("/tmp/test-path_", *test_path
, NULL
);
61 (void) rm_rf(p
, REMOVE_ROOT
|REMOVE_PHYSICAL
);
69 static void shutdown_test(Manager
*m
) {
75 static void check_stop_unlink(Manager
*m
, Unit
*unit
, const char *test_path
, const char *service_name
) {
76 _cleanup_free_
char *tmp
= NULL
;
77 Unit
*service_unit
= NULL
;
78 Service
*service
= NULL
;
80 usec_t timeout
= 2 * USEC_PER_SEC
;
87 assert_se(tmp
= strreplace(unit
->id
, ".path", ".service"));
88 service_unit
= manager_get_unit(m
, tmp
);
90 service_unit
= manager_get_unit(m
, service_name
);
91 assert_se(service_unit
);
92 service
= SERVICE(service_unit
);
94 ts
= now(CLOCK_MONOTONIC
);
95 /* We process events until the service related to the path has been successfully started */
96 while(service
->result
!= SERVICE_SUCCESS
|| service
->state
!= SERVICE_START
) {
100 r
= sd_event_run(m
->event
, 100 * USEC_PER_MSEC
);
103 printf("%s: state = %s; result = %s \n",
105 service_state_to_string(service
->state
),
106 service_result_to_string(service
->result
));
109 /* But we timeout if the service has not been started in the allocated time */
110 n
= now(CLOCK_MONOTONIC
);
111 if (ts
+ timeout
< n
) {
112 log_error("Test timeout when testing %s", unit
->id
);
117 assert_se(UNIT_VTABLE(unit
)->stop(unit
) >= 0);
118 (void) rm_rf(test_path
, REMOVE_ROOT
|REMOVE_PHYSICAL
);
121 static void test_path_exists(Manager
*m
) {
122 const char *test_path
= "/tmp/test-path_exists";
127 assert_se(manager_load_unit(m
, "path-exists.path", NULL
, NULL
, &unit
) >= 0);
128 assert_se(UNIT_VTABLE(unit
)->start(unit
) >= 0);
130 assert_se(touch(test_path
) >= 0);
132 check_stop_unlink(m
, unit
, test_path
, NULL
);
135 static void test_path_existsglob(Manager
*m
) {
136 const char *test_path
= "/tmp/test-path_existsglobFOOBAR";
140 assert_se(manager_load_unit(m
, "path-existsglob.path", NULL
, NULL
, &unit
) >= 0);
141 assert_se(UNIT_VTABLE(unit
)->start(unit
) >= 0);
143 assert_se(touch(test_path
) >= 0);
145 check_stop_unlink(m
, unit
, test_path
, NULL
);
148 static void test_path_changed(Manager
*m
) {
149 const char *test_path
= "/tmp/test-path_changed";
155 assert_se(touch(test_path
) >= 0);
157 assert_se(manager_load_unit(m
, "path-changed.path", NULL
, NULL
, &unit
) >= 0);
158 assert_se(UNIT_VTABLE(unit
)->start(unit
) >= 0);
160 f
= fopen(test_path
, "w");
164 check_stop_unlink(m
, unit
, test_path
, NULL
);
167 static void test_path_modified(Manager
*m
) {
168 _cleanup_fclose_
FILE *f
= NULL
;
169 const char *test_path
= "/tmp/test-path_modified";
174 assert_se(touch(test_path
) >= 0);
176 assert_se(manager_load_unit(m
, "path-modified.path", NULL
, NULL
, &unit
) >= 0);
177 assert_se(UNIT_VTABLE(unit
)->start(unit
) >= 0);
179 f
= fopen(test_path
, "w");
183 check_stop_unlink(m
, unit
, test_path
, NULL
);
186 static void test_path_unit(Manager
*m
) {
187 const char *test_path
= "/tmp/test-path_unit";
192 assert_se(manager_load_unit(m
, "path-unit.path", NULL
, NULL
, &unit
) >= 0);
193 assert_se(UNIT_VTABLE(unit
)->start(unit
) >= 0);
195 assert_se(touch(test_path
) >= 0);
197 check_stop_unlink(m
, unit
, test_path
, "path-mycustomunit.service");
200 static void test_path_directorynotempty(Manager
*m
) {
201 const char *test_path
= "/tmp/test-path_directorynotempty/";
206 assert_se(access(test_path
, F_OK
) < 0);
208 assert_se(manager_load_unit(m
, "path-directorynotempty.path", NULL
, NULL
, &unit
) >= 0);
209 assert_se(UNIT_VTABLE(unit
)->start(unit
) >= 0);
211 /* MakeDirectory default to no */
212 assert_se(access(test_path
, F_OK
) < 0);
214 assert_se(mkdir_p(test_path
, 0755) >= 0);
215 assert_se(touch(strjoina(test_path
, "test_file")) >= 0);
217 check_stop_unlink(m
, unit
, test_path
, NULL
);
220 static void test_path_makedirectory_directorymode(Manager
*m
) {
221 const char *test_path
= "/tmp/test-path_makedirectory/";
227 assert_se(access(test_path
, F_OK
) < 0);
229 assert_se(manager_load_unit(m
, "path-makedirectory.path", NULL
, NULL
, &unit
) >= 0);
230 assert_se(UNIT_VTABLE(unit
)->start(unit
) >= 0);
232 /* Check if the directory has been created */
233 assert_se(access(test_path
, F_OK
) >= 0);
235 /* Check the mode we specified with DirectoryMode=0744 */
236 assert_se(stat(test_path
, &s
) >= 0);
237 assert_se((s
.st_mode
& S_IRWXU
) == 0700);
238 assert_se((s
.st_mode
& S_IRWXG
) == 0040);
239 assert_se((s
.st_mode
& S_IRWXO
) == 0004);
241 assert_se(UNIT_VTABLE(unit
)->stop(unit
) >= 0);
242 (void) rm_rf(test_path
, REMOVE_ROOT
|REMOVE_PHYSICAL
);
245 int main(int argc
, char *argv
[]) {
246 test_function_t tests
[] = {
248 test_path_existsglob
,
252 test_path_directorynotempty
,
253 test_path_makedirectory_directorymode
,
256 test_function_t
*test
= NULL
;
259 log_parse_environment();
262 assert_se(set_unit_path(TEST_DIR
"/test-path/") >= 0);
264 for (test
= tests
; test
&& *test
; test
++) {
267 /* We create a clean environment for each test */