• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

systemd / systemd / 23774132158

30 Mar 2026 09:35PM UTC coverage: 72.398% (+0.2%) from 72.208%
23774132158

push

github

daandemeyer
Only enable `NoAuto=true` for supported partitions

When `Format=empty` is set we need to check for `NoAuto` support for
the partition type, else we print a warning later in the build.

Followup for 381304a

1 of 1 new or added line in 1 file covered. (100.0%)

7091 existing lines in 92 files now uncovered.

318474 of 439892 relevant lines covered (72.4%)

1151474.34 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.18
/src/test/test-path.c
1
/* SPDX-License-Identifier: LGPL-2.1-or-later */
2

3
#include <stdlib.h>
4
#include <sys/stat.h>
5
#include <unistd.h>
6

7
#include "all-units.h"
8
#include "alloc-util.h"
9
#include "fd-util.h"
10
#include "fs-util.h"
11
#include "manager.h"
12
#include "mkdir.h"
13
#include "rm-rf.h"
14
#include "string-util.h"
15
#include "strv.h"
16
#include "tests.h"
17
#include "unit.h"
18

19
typedef void (*test_function_t)(Manager *m);
20

21
static int setup_test(Manager **m) {
7✔
22
        char **tests_path = STRV_MAKE("exists", "existsglobFOOBAR", "changed", "modified", "unit",
7✔
23
                                      "directorynotempty", "makedirectory");
24
        Manager *tmp = NULL;
7✔
25
        int r;
7✔
26

27
        assert_se(m);
7✔
28

29
        r = enter_cgroup_subroot(NULL);
7✔
30
        if (r == -ENOMEDIUM)
7✔
31
                return log_tests_skipped("cgroupfs not available");
7✔
32

33
        r = manager_new(RUNTIME_SCOPE_USER, MANAGER_TEST_RUN_BASIC, &tmp);
7✔
34
        if (manager_errno_skip_test(r))
7✔
35
                return log_tests_skipped_errno(r, "manager_new");
×
36
        assert_se(r >= 0);
7✔
37
        assert_se(manager_startup(tmp, NULL, NULL, NULL) >= 0);
7✔
38

39
        STRV_FOREACH(test_path, tests_path) {
56✔
40
                _cleanup_free_ char *p = NULL;
49✔
41

42
                p = strjoin("/tmp/test-path_", *test_path);
49✔
43
                assert_se(p);
49✔
44

45
                (void) rm_rf(p, REMOVE_ROOT|REMOVE_PHYSICAL);
49✔
46
        }
47

48
        *m = tmp;
7✔
49

50
        return 0;
7✔
51
}
52

53
static void shutdown_test(Manager *m) {
7✔
54
        assert_se(m);
7✔
55

56
        manager_free(m);
7✔
57
}
7✔
58

59
static Service *service_for_path(Manager *m, Path *path, const char *service_name) {
6✔
60
        _cleanup_free_ char *tmp = NULL;
6✔
61
        Unit *service_unit = NULL;
6✔
62

63
        assert_se(m);
6✔
64
        assert_se(path);
6✔
65

66
        if (!service_name) {
6✔
67
                assert_se(tmp = strreplace(UNIT(path)->id, ".path", ".service"));
5✔
68
                service_unit = manager_get_unit(m, tmp);
5✔
69
        } else
70
                service_unit = manager_get_unit(m, service_name);
1✔
71
        assert_se(service_unit);
6✔
72

73
        return SERVICE(service_unit);
6✔
74
}
75

76
static int _check_states(unsigned line,
25✔
77
                         Manager *m, Path *path, Service *service, PathState path_state, ServiceState service_state) {
78
        assert_se(m);
25✔
79
        assert_se(service);
25✔
80

81
        /* Silence static analyzers */
82
        assert_cc(30 * USEC_PER_SEC <= USEC_INFINITY);
25✔
83
        usec_t end = now(CLOCK_MONOTONIC) + 30 * USEC_PER_SEC;
25✔
84

85
        while (path->state != path_state || service->state != service_state ||
137✔
86
               path->result != PATH_SUCCESS || service->result != SERVICE_SUCCESS) {
188✔
87

88
                assert_se(sd_event_run(m->event, 100 * USEC_PER_MSEC) >= 0);
138✔
89

90
                usec_t n = now(CLOCK_MONOTONIC);
138✔
91
                log_info("line %u: %s: state = %s; result = %s (left: %" PRIi64 ")",
138✔
92
                         line,
93
                         UNIT(path)->id,
94
                         path_state_to_string(path->state),
95
                         path_result_to_string(path->result),
96
                         (int64_t) (end - n));
97
                log_info("line %u: %s: state = %s; result = %s",
138✔
98
                         line,
99
                         UNIT(service)->id,
100
                         service_state_to_string(service->state),
101
                         service_result_to_string(service->result));
102

103
                if (service->state == SERVICE_FAILED &&
138✔
UNCOV
104
                    (service->main_exec_status.status == EXIT_CGROUP || service->result == SERVICE_FAILURE_RESOURCES)) {
×
UNCOV
105
                        const char *ci = ci_environment();
×
106

107
                        /* On a general purpose system we may fail to start the service for reasons which are
108
                         * not under our control: permission limits, resource exhaustion, etc. Let's skip the
109
                         * test in those cases. On developer machines we require proper setup. */
UNCOV
110
                        if (!ci)
×
UNCOV
111
                                return log_notice_errno(SYNTHETIC_ERRNO(ECANCELED),
×
112
                                                        "Failed to start service %s, aborting test: %s/%s",
113
                                                        UNIT(service)->id,
114
                                                        service_state_to_string(service->state),
115
                                                        service_result_to_string(service->result));
116

117
                        /* On Salsa we can't setup cgroups so the unit always fails. The test checks if it
118
                         * can but continues if it cannot at the beginning, but on Salsa it fails here. */
UNCOV
119
                        if (streq(ci, "salsa-ci"))
×
UNCOV
120
                                exit(EXIT_TEST_SKIP);
×
121
                }
122

123
                if (n >= end) {
138✔
UNCOV
124
                        log_error("Test timeout when testing %s", UNIT(path)->id);
×
UNCOV
125
                        exit(EXIT_FAILURE);
×
126
                }
127
        }
128

129
        return 0;
130
}
131
#define check_states(...) _check_states(__LINE__, __VA_ARGS__)
132

133
static void test_path_exists(Manager *m) {
1✔
134
        const char *test_path = "/tmp/test-path_exists";
1✔
135
        Unit *unit = NULL;
1✔
136
        Path *path = NULL;
1✔
137
        Service *service = NULL;
1✔
138

139
        assert_se(m);
1✔
140

141
        assert_se(manager_load_startable_unit_or_warn(m, "path-exists.path", NULL, &unit) >= 0);
1✔
142

143
        path = PATH(unit);
1✔
144
        service = service_for_path(m, path, NULL);
1✔
145

146
        assert_se(unit_start(unit, NULL) >= 0);
1✔
147
        if (check_states(m, path, service, PATH_WAITING, SERVICE_DEAD) < 0)
1✔
UNCOV
148
                return;
×
149

150
        assert_se(touch(test_path) >= 0);
1✔
151
        if (check_states(m, path, service, PATH_RUNNING, SERVICE_RUNNING) < 0)
1✔
152
                return;
153

154
        /* Service restarts if file still exists */
155
        assert_se(unit_stop(UNIT(service)) >= 0);
1✔
156
        if (check_states(m, path, service, PATH_RUNNING, SERVICE_RUNNING) < 0)
1✔
157
                return;
158

159
        assert_se(rm_rf(test_path, REMOVE_ROOT|REMOVE_PHYSICAL) == 0);
1✔
160
        assert_se(unit_stop(UNIT(service)) >= 0);
1✔
161
        if (check_states(m, path, service, PATH_WAITING, SERVICE_DEAD) < 0)
1✔
162
                return;
163

164
        assert_se(unit_stop(unit) >= 0);
1✔
165
}
166

167
static void test_path_existsglob(Manager *m) {
1✔
168
        const char *test_path = "/tmp/test-path_existsglobFOOBAR";
1✔
169
        Unit *unit = NULL;
1✔
170
        Path *path = NULL;
1✔
171
        Service *service = NULL;
1✔
172

173
        assert_se(m);
1✔
174

175
        assert_se(manager_load_startable_unit_or_warn(m, "path-existsglob.path", NULL, &unit) >= 0);
1✔
176

177
        path = PATH(unit);
1✔
178
        service = service_for_path(m, path, NULL);
1✔
179

180
        assert_se(unit_start(unit, NULL) >= 0);
1✔
181
        if (check_states(m, path, service, PATH_WAITING, SERVICE_DEAD) < 0)
1✔
UNCOV
182
                return;
×
183

184
        assert_se(touch(test_path) >= 0);
1✔
185
        if (check_states(m, path, service, PATH_RUNNING, SERVICE_RUNNING) < 0)
1✔
186
                return;
187

188
        /* Service restarts if file still exists */
189
        assert_se(unit_stop(UNIT(service)) >= 0);
1✔
190
        if (check_states(m, path, service, PATH_RUNNING, SERVICE_RUNNING) < 0)
1✔
191
                return;
192

193
        assert_se(rm_rf(test_path, REMOVE_ROOT|REMOVE_PHYSICAL) == 0);
1✔
194
        assert_se(unit_stop(UNIT(service)) >= 0);
1✔
195
        if (check_states(m, path, service, PATH_WAITING, SERVICE_DEAD) < 0)
1✔
196
                return;
197

198
        assert_se(unit_stop(unit) >= 0);
1✔
199
}
200

201
static void test_path_changed(Manager *m) {
1✔
202
        const char *test_path = "/tmp/test-path_changed";
1✔
203
        FILE *f;
1✔
204
        Unit *unit = NULL;
1✔
205
        Path *path = NULL;
1✔
206
        Service *service = NULL;
1✔
207

208
        assert_se(m);
1✔
209

210
        assert_se(manager_load_startable_unit_or_warn(m, "path-changed.path", NULL, &unit) >= 0);
1✔
211

212
        path = PATH(unit);
1✔
213
        service = service_for_path(m, path, NULL);
1✔
214

215
        assert_se(unit_start(unit, NULL) >= 0);
1✔
216
        if (check_states(m, path, service, PATH_WAITING, SERVICE_DEAD) < 0)
1✔
UNCOV
217
                return;
×
218

219
        assert_se(touch(test_path) >= 0);
1✔
220
        if (check_states(m, path, service, PATH_RUNNING, SERVICE_RUNNING) < 0)
1✔
221
                return;
222

223
        /* Service does not restart if file still exists */
224
        assert_se(unit_stop(UNIT(service)) >= 0);
1✔
225
        if (check_states(m, path, service, PATH_WAITING, SERVICE_DEAD) < 0)
1✔
226
                return;
227

228
        f = fopen(test_path, "w");
1✔
229
        assert_se(f);
1✔
230
        fclose(f);
1✔
231

232
        if (check_states(m, path, service, PATH_RUNNING, SERVICE_RUNNING) < 0)
1✔
233
                return;
234

235
        assert_se(unit_stop(UNIT(service)) >= 0);
1✔
236
        if (check_states(m, path, service, PATH_WAITING, SERVICE_DEAD) < 0)
1✔
237
                return;
238

239
        (void) rm_rf(test_path, REMOVE_ROOT|REMOVE_PHYSICAL);
1✔
240
        assert_se(unit_stop(unit) >= 0);
1✔
241
}
242

243
static void test_path_modified(Manager *m) {
1✔
244
        _cleanup_fclose_ FILE *f = NULL;
1✔
245
        const char *test_path = "/tmp/test-path_modified";
1✔
246
        Unit *unit = NULL;
1✔
247
        Path *path = NULL;
1✔
248
        Service *service = NULL;
1✔
249

250
        assert_se(m);
1✔
251

252
        assert_se(manager_load_startable_unit_or_warn(m, "path-modified.path", NULL, &unit) >= 0);
1✔
253

254
        path = PATH(unit);
1✔
255
        service = service_for_path(m, path, NULL);
1✔
256

257
        assert_se(unit_start(unit, NULL) >= 0);
1✔
258
        if (check_states(m, path, service, PATH_WAITING, SERVICE_DEAD) < 0)
1✔
259
                return;
260

261
        assert_se(touch(test_path) >= 0);
1✔
262
        if (check_states(m, path, service, PATH_RUNNING, SERVICE_RUNNING) < 0)
1✔
263
                return;
264

265
        /* Service does not restart if file still exists */
266
        assert_se(unit_stop(UNIT(service)) >= 0);
1✔
267
        if (check_states(m, path, service, PATH_WAITING, SERVICE_DEAD) < 0)
1✔
268
                return;
269

270
        f = fopen(test_path, "w");
1✔
271
        assert_se(f);
1✔
272
        fputs("test", f);
1✔
273

274
        if (check_states(m, path, service, PATH_RUNNING, SERVICE_RUNNING) < 0)
1✔
275
                return;
276

277
        assert_se(unit_stop(UNIT(service)) >= 0);
1✔
278
        if (check_states(m, path, service, PATH_WAITING, SERVICE_DEAD) < 0)
1✔
279
                return;
280

281
        (void) rm_rf(test_path, REMOVE_ROOT|REMOVE_PHYSICAL);
1✔
282
        assert_se(unit_stop(unit) >= 0);
1✔
283
}
284

285
static void test_path_unit(Manager *m) {
1✔
286
        const char *test_path = "/tmp/test-path_unit";
1✔
287
        Unit *unit = NULL;
1✔
288
        Path *path = NULL;
1✔
289
        Service *service = NULL;
1✔
290

291
        assert_se(m);
1✔
292

293
        assert_se(manager_load_startable_unit_or_warn(m, "path-unit.path", NULL, &unit) >= 0);
1✔
294

295
        path = PATH(unit);
1✔
296
        service = service_for_path(m, path, "path-mycustomunit.service");
1✔
297

298
        assert_se(unit_start(unit, NULL) >= 0);
1✔
299
        if (check_states(m, path, service, PATH_WAITING, SERVICE_DEAD) < 0)
1✔
UNCOV
300
                return;
×
301

302
        assert_se(touch(test_path) >= 0);
1✔
303
        if (check_states(m, path, service, PATH_RUNNING, SERVICE_RUNNING) < 0)
1✔
304
                return;
305

306
        assert_se(rm_rf(test_path, REMOVE_ROOT|REMOVE_PHYSICAL) == 0);
1✔
307
        assert_se(unit_stop(UNIT(service)) >= 0);
1✔
308
        if (check_states(m, path, service, PATH_WAITING, SERVICE_DEAD) < 0)
1✔
309
                return;
310

311
        assert_se(unit_stop(unit) >= 0);
1✔
312
}
313

314
static void test_path_directorynotempty(Manager *m) {
1✔
315
        const char *test_file, *test_path = "/tmp/test-path_directorynotempty/";
1✔
316
        Unit *unit = NULL;
1✔
317
        Path *path = NULL;
1✔
318
        Service *service = NULL;
1✔
319

320
        assert_se(m);
1✔
321

322
        assert_se(manager_load_startable_unit_or_warn(m, "path-directorynotempty.path", NULL, &unit) >= 0);
1✔
323

324
        path = PATH(unit);
1✔
325
        service = service_for_path(m, path, NULL);
1✔
326

327
        assert_se(access(test_path, F_OK) < 0);
1✔
328

329
        assert_se(unit_start(unit, NULL) >= 0);
1✔
330
        if (check_states(m, path, service, PATH_WAITING, SERVICE_DEAD) < 0)
1✔
UNCOV
331
                return;
×
332

333
        /* MakeDirectory default to no */
334
        assert_se(access(test_path, F_OK) < 0);
1✔
335

336
        assert_se(mkdir_p(test_path, 0755) >= 0);
1✔
337
        test_file = strjoina(test_path, "test_file");
5✔
338
        assert_se(touch(test_file) >= 0);
1✔
339
        if (check_states(m, path, service, PATH_RUNNING, SERVICE_RUNNING) < 0)
1✔
340
                return;
341

342
        /* Service restarts if directory is still not empty */
343
        assert_se(unit_stop(UNIT(service)) >= 0);
1✔
344
        if (check_states(m, path, service, PATH_RUNNING, SERVICE_RUNNING) < 0)
1✔
345
                return;
346

347
        assert_se(rm_rf(test_path, REMOVE_ROOT|REMOVE_PHYSICAL) == 0);
1✔
348
        assert_se(unit_stop(UNIT(service)) >= 0);
1✔
349
        if (check_states(m, path, service, PATH_WAITING, SERVICE_DEAD) < 0)
1✔
350
                return;
351

352
        assert_se(unit_stop(unit) >= 0);
1✔
353
}
354

355
static void test_path_makedirectory_directorymode(Manager *m) {
1✔
356
        const char *test_path = "/tmp/test-path_makedirectory/";
1✔
357
        Unit *unit = NULL;
1✔
358
        struct stat s;
1✔
359

360
        assert_se(m);
1✔
361

362
        assert_se(manager_load_startable_unit_or_warn(m, "path-makedirectory.path", NULL, &unit) >= 0);
1✔
363

364
        assert_se(access(test_path, F_OK) < 0);
1✔
365

366
        assert_se(unit_start(unit, NULL) >= 0);
1✔
367

368
        /* Check if the directory has been created */
369
        assert_se(access(test_path, F_OK) >= 0);
1✔
370

371
        /* Check the mode we specified with DirectoryMode=0744 */
372
        assert_se(stat(test_path, &s) >= 0);
1✔
373
        assert_se((s.st_mode & S_IRWXU) == 0700);
1✔
374
        assert_se((s.st_mode & S_IRWXG) == 0040);
1✔
375
        assert_se((s.st_mode & S_IRWXO) == 0004);
1✔
376

377
        assert_se(unit_stop(unit) >= 0);
1✔
378
        (void) rm_rf(test_path, REMOVE_ROOT|REMOVE_PHYSICAL);
1✔
379
}
1✔
380

381
int main(int argc, char *argv[]) {
1✔
382
        static const test_function_t tests[] = {
1✔
383
                test_path_exists,
384
                test_path_existsglob,
385
                test_path_changed,
386
                test_path_modified,
387
                test_path_unit,
388
                test_path_directorynotempty,
389
                test_path_makedirectory_directorymode,
390
                NULL,
391
        };
392

393
        _cleanup_free_ char *test_path = NULL;
1✔
394
        _cleanup_(rm_rf_physical_and_freep) char *runtime_dir = NULL;
1✔
395

396
        umask(022);
1✔
397

398
        test_setup_logging(LOG_INFO);
1✔
399

400
        ASSERT_OK(get_testdata_dir("test-path", &test_path));
1✔
401
        ASSERT_OK(setenv_unit_path(test_path));
1✔
402
        assert_se(runtime_dir = setup_fake_runtime_dir());
1✔
403

404
        for (const test_function_t *test = tests; *test; test++) {
8✔
405
                Manager *m = NULL;
7✔
406
                int r;
7✔
407

408
                /* We create a clean environment for each test */
409
                r = setup_test(&m);
7✔
410
                if (r != 0)
7✔
UNCOV
411
                        return r;
×
412

413
                (*test)(m);
7✔
414

415
                shutdown_test(m);
7✔
416
        }
417

418
        return 0;
419
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc