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