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