]>
Commit | Line | Data |
---|---|---|
f47781d8 MP |
1 | /*** |
2 | This file is part of systemd. | |
3 | ||
4 | Copyright 2014 Ronny Chevalier | |
5 | ||
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. | |
10 | ||
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. | |
15 | ||
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/>. | |
18 | ***/ | |
19 | ||
f47781d8 | 20 | #include <stdbool.h> |
db2df898 | 21 | #include <stdio.h> |
f47781d8 | 22 | |
db2df898 MP |
23 | #include "alloc-util.h" |
24 | #include "fd-util.h" | |
25 | #include "fs-util.h" | |
f47781d8 | 26 | #include "macro.h" |
db2df898 | 27 | #include "manager.h" |
f47781d8 | 28 | #include "mkdir.h" |
e3bff60a | 29 | #include "rm-rf.h" |
db2df898 MP |
30 | #include "string-util.h" |
31 | #include "strv.h" | |
4c89c718 | 32 | #include "test-helper.h" |
db2df898 MP |
33 | #include "unit.h" |
34 | #include "util.h" | |
f47781d8 MP |
35 | |
36 | typedef void (*test_function_t)(Manager *m); | |
37 | ||
38 | static int setup_test(Manager **m) { | |
39 | char **tests_path = STRV_MAKE("exists", "existsglobFOOBAR", "changed", "modified", "unit", | |
40 | "directorynotempty", "makedirectory"); | |
41 | char **test_path; | |
e3bff60a | 42 | Manager *tmp = NULL; |
f47781d8 MP |
43 | int r; |
44 | ||
45 | assert_se(m); | |
46 | ||
e3bff60a | 47 | r = manager_new(MANAGER_USER, true, &tmp); |
4c89c718 MP |
48 | if (MANAGER_SKIP_TEST(r)) { |
49 | printf("Skipping test: manager_new: %s\n", strerror(-r)); | |
f47781d8 MP |
50 | return -EXIT_TEST_SKIP; |
51 | } | |
52 | assert_se(r >= 0); | |
53 | assert_se(manager_startup(tmp, NULL, NULL) >= 0); | |
54 | ||
55 | STRV_FOREACH(test_path, tests_path) { | |
e3bff60a MP |
56 | _cleanup_free_ char *p = NULL; |
57 | ||
58 | p = strjoin("/tmp/test-path_", *test_path, NULL); | |
59 | assert_se(p); | |
60 | ||
61 | (void) rm_rf(p, REMOVE_ROOT|REMOVE_PHYSICAL); | |
f47781d8 MP |
62 | } |
63 | ||
64 | *m = tmp; | |
65 | ||
66 | return 0; | |
67 | } | |
68 | ||
69 | static void shutdown_test(Manager *m) { | |
70 | assert_se(m); | |
71 | ||
72 | manager_free(m); | |
73 | } | |
74 | ||
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; | |
79 | usec_t ts; | |
80 | usec_t timeout = 2 * USEC_PER_SEC; | |
81 | ||
82 | assert_se(m); | |
83 | assert_se(unit); | |
84 | assert_se(test_path); | |
85 | ||
86 | if (!service_name) { | |
87 | assert_se(tmp = strreplace(unit->id, ".path", ".service")); | |
88 | service_unit = manager_get_unit(m, tmp); | |
89 | } else | |
90 | service_unit = manager_get_unit(m, service_name); | |
91 | assert_se(service_unit); | |
92 | service = SERVICE(service_unit); | |
93 | ||
94 | ts = now(CLOCK_MONOTONIC); | |
e735f4d4 | 95 | /* We process events until the service related to the path has been successfully started */ |
f47781d8 MP |
96 | while(service->result != SERVICE_SUCCESS || service->state != SERVICE_START) { |
97 | usec_t n; | |
98 | int r; | |
99 | ||
100 | r = sd_event_run(m->event, 100 * USEC_PER_MSEC); | |
101 | assert_se(r >= 0); | |
102 | ||
103 | printf("%s: state = %s; result = %s \n", | |
104 | service_unit->id, | |
105 | service_state_to_string(service->state), | |
106 | service_result_to_string(service->result)); | |
107 | ||
108 | ||
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); | |
113 | exit(EXIT_FAILURE); | |
114 | } | |
115 | } | |
116 | ||
117 | assert_se(UNIT_VTABLE(unit)->stop(unit) >= 0); | |
e3bff60a | 118 | (void) rm_rf(test_path, REMOVE_ROOT|REMOVE_PHYSICAL); |
f47781d8 MP |
119 | } |
120 | ||
121 | static void test_path_exists(Manager *m) { | |
122 | const char *test_path = "/tmp/test-path_exists"; | |
123 | Unit *unit = NULL; | |
124 | ||
125 | assert_se(m); | |
126 | ||
127 | assert_se(manager_load_unit(m, "path-exists.path", NULL, NULL, &unit) >= 0); | |
128 | assert_se(UNIT_VTABLE(unit)->start(unit) >= 0); | |
129 | ||
130 | assert_se(touch(test_path) >= 0); | |
131 | ||
132 | check_stop_unlink(m, unit, test_path, NULL); | |
133 | } | |
134 | ||
135 | static void test_path_existsglob(Manager *m) { | |
136 | const char *test_path = "/tmp/test-path_existsglobFOOBAR"; | |
137 | Unit *unit = NULL; | |
138 | ||
139 | assert_se(m); | |
140 | assert_se(manager_load_unit(m, "path-existsglob.path", NULL, NULL, &unit) >= 0); | |
141 | assert_se(UNIT_VTABLE(unit)->start(unit) >= 0); | |
142 | ||
143 | assert_se(touch(test_path) >= 0); | |
144 | ||
145 | check_stop_unlink(m, unit, test_path, NULL); | |
146 | } | |
147 | ||
148 | static void test_path_changed(Manager *m) { | |
149 | const char *test_path = "/tmp/test-path_changed"; | |
150 | FILE *f; | |
151 | Unit *unit = NULL; | |
152 | ||
153 | assert_se(m); | |
154 | ||
155 | assert_se(touch(test_path) >= 0); | |
156 | ||
157 | assert_se(manager_load_unit(m, "path-changed.path", NULL, NULL, &unit) >= 0); | |
158 | assert_se(UNIT_VTABLE(unit)->start(unit) >= 0); | |
159 | ||
160 | f = fopen(test_path, "w"); | |
161 | assert_se(f); | |
162 | fclose(f); | |
163 | ||
164 | check_stop_unlink(m, unit, test_path, NULL); | |
165 | } | |
166 | ||
167 | static void test_path_modified(Manager *m) { | |
168 | _cleanup_fclose_ FILE *f = NULL; | |
169 | const char *test_path = "/tmp/test-path_modified"; | |
170 | Unit *unit = NULL; | |
171 | ||
172 | assert_se(m); | |
173 | ||
174 | assert_se(touch(test_path) >= 0); | |
175 | ||
176 | assert_se(manager_load_unit(m, "path-modified.path", NULL, NULL, &unit) >= 0); | |
177 | assert_se(UNIT_VTABLE(unit)->start(unit) >= 0); | |
178 | ||
179 | f = fopen(test_path, "w"); | |
180 | assert_se(f); | |
181 | fputs("test", f); | |
182 | ||
183 | check_stop_unlink(m, unit, test_path, NULL); | |
184 | } | |
185 | ||
186 | static void test_path_unit(Manager *m) { | |
187 | const char *test_path = "/tmp/test-path_unit"; | |
188 | Unit *unit = NULL; | |
189 | ||
190 | assert_se(m); | |
191 | ||
192 | assert_se(manager_load_unit(m, "path-unit.path", NULL, NULL, &unit) >= 0); | |
193 | assert_se(UNIT_VTABLE(unit)->start(unit) >= 0); | |
194 | ||
195 | assert_se(touch(test_path) >= 0); | |
196 | ||
197 | check_stop_unlink(m, unit, test_path, "path-mycustomunit.service"); | |
198 | } | |
199 | ||
200 | static void test_path_directorynotempty(Manager *m) { | |
201 | const char *test_path = "/tmp/test-path_directorynotempty/"; | |
202 | Unit *unit = NULL; | |
203 | ||
204 | assert_se(m); | |
205 | ||
206 | assert_se(access(test_path, F_OK) < 0); | |
207 | ||
208 | assert_se(manager_load_unit(m, "path-directorynotempty.path", NULL, NULL, &unit) >= 0); | |
209 | assert_se(UNIT_VTABLE(unit)->start(unit) >= 0); | |
210 | ||
211 | /* MakeDirectory default to no */ | |
212 | assert_se(access(test_path, F_OK) < 0); | |
213 | ||
214 | assert_se(mkdir_p(test_path, 0755) >= 0); | |
e735f4d4 | 215 | assert_se(touch(strjoina(test_path, "test_file")) >= 0); |
f47781d8 MP |
216 | |
217 | check_stop_unlink(m, unit, test_path, NULL); | |
218 | } | |
219 | ||
220 | static void test_path_makedirectory_directorymode(Manager *m) { | |
221 | const char *test_path = "/tmp/test-path_makedirectory/"; | |
222 | Unit *unit = NULL; | |
223 | struct stat s; | |
224 | ||
225 | assert_se(m); | |
226 | ||
227 | assert_se(access(test_path, F_OK) < 0); | |
228 | ||
229 | assert_se(manager_load_unit(m, "path-makedirectory.path", NULL, NULL, &unit) >= 0); | |
230 | assert_se(UNIT_VTABLE(unit)->start(unit) >= 0); | |
231 | ||
232 | /* Check if the directory has been created */ | |
233 | assert_se(access(test_path, F_OK) >= 0); | |
234 | ||
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); | |
240 | ||
241 | assert_se(UNIT_VTABLE(unit)->stop(unit) >= 0); | |
e3bff60a | 242 | (void) rm_rf(test_path, REMOVE_ROOT|REMOVE_PHYSICAL); |
f47781d8 MP |
243 | } |
244 | ||
245 | int main(int argc, char *argv[]) { | |
246 | test_function_t tests[] = { | |
247 | test_path_exists, | |
248 | test_path_existsglob, | |
249 | test_path_changed, | |
250 | test_path_modified, | |
251 | test_path_unit, | |
252 | test_path_directorynotempty, | |
253 | test_path_makedirectory_directorymode, | |
254 | NULL, | |
255 | }; | |
256 | test_function_t *test = NULL; | |
257 | Manager *m = NULL; | |
258 | ||
259 | log_parse_environment(); | |
260 | log_open(); | |
261 | ||
db2df898 | 262 | assert_se(set_unit_path(TEST_DIR "/test-path/") >= 0); |
f47781d8 MP |
263 | |
264 | for (test = tests; test && *test; test++) { | |
265 | int r; | |
266 | ||
267 | /* We create a clean environment for each test */ | |
268 | r = setup_test(&m); | |
269 | if (r < 0) | |
270 | return -r; | |
271 | ||
272 | (*test)(m); | |
273 | ||
274 | shutdown_test(m); | |
275 | } | |
276 | ||
277 | return 0; | |
278 | } |