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