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