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