]>
Commit | Line | Data |
---|---|---|
53e1b683 | 1 | /* SPDX-License-Identifier: LGPL-2.1+ */ |
d2120590 | 2 | |
a4bc3c1d LP |
3 | #include <sched.h> |
4 | #include <signal.h> | |
d2120590 | 5 | #include <stdlib.h> |
a4bc3c1d LP |
6 | #include <sys/mount.h> |
7 | #include <sys/wait.h> | |
d2120590 LP |
8 | #include <util.h> |
9 | ||
795919ef LP |
10 | /* When we include libgen.h because we need dirname() we immediately |
11 | * undefine basename() since libgen.h defines it as a macro to the POSIX | |
12 | * version which is really broken. We prefer GNU basename(). */ | |
13 | #include <libgen.h> | |
14 | #undef basename | |
15 | ||
e2d41370 | 16 | #include "alloc-util.h" |
0cf29baa | 17 | #include "env-util.h" |
e2d41370 | 18 | #include "fileio.h" |
eca27ebb | 19 | #include "fs-util.h" |
317bb217 | 20 | #include "log.h" |
1f35a3b2 | 21 | #include "path-util.h" |
e2d41370 FB |
22 | #include "strv.h" |
23 | #include "tests.h" | |
d2120590 LP |
24 | |
25 | char* setup_fake_runtime_dir(void) { | |
26 | char t[] = "/tmp/fake-xdg-runtime-XXXXXX", *p; | |
27 | ||
28 | assert_se(mkdtemp(t)); | |
29 | assert_se(setenv("XDG_RUNTIME_DIR", t, 1) >= 0); | |
30 | assert_se(p = strdup(t)); | |
31 | ||
32 | return p; | |
33 | } | |
f853c6ef | 34 | |
e2d41370 FB |
35 | static void load_testdata_env(void) { |
36 | static bool called = false; | |
8cb10a4f | 37 | _cleanup_free_ char *s = NULL; |
e2d41370 FB |
38 | _cleanup_free_ char *envpath = NULL; |
39 | _cleanup_strv_free_ char **pairs = NULL; | |
40 | char **k, **v; | |
8cb10a4f | 41 | |
e2d41370 FB |
42 | if (called) |
43 | return; | |
44 | called = true; | |
8cb10a4f YW |
45 | |
46 | assert_se(readlink_and_make_absolute("/proc/self/exe", &s) >= 0); | |
e2d41370 | 47 | dirname(s); |
8cb10a4f | 48 | |
62a85ee0 | 49 | envpath = path_join(s, "systemd-runtest.env"); |
aa8fbc74 | 50 | if (load_env_file_pairs(NULL, envpath, &pairs) < 0) |
e2d41370 | 51 | return; |
8cb10a4f | 52 | |
e2d41370 FB |
53 | STRV_FOREACH_PAIR(k, v, pairs) |
54 | setenv(*k, *v, 0); | |
55 | } | |
56 | ||
55890a40 | 57 | const char* get_testdata_dir(void) { |
c60b6dda | 58 | const char *env; |
e2d41370 FB |
59 | |
60 | load_testdata_env(); | |
f853c6ef | 61 | |
c60b6dda MP |
62 | /* if the env var is set, use that */ |
63 | env = getenv("SYSTEMD_TEST_DATA"); | |
e2d41370 FB |
64 | if (!env) |
65 | env = SYSTEMD_TEST_DATA; | |
66 | if (access(env, F_OK) < 0) { | |
67 | fprintf(stderr, "ERROR: $SYSTEMD_TEST_DATA directory [%s] does not exist\n", env); | |
68 | exit(EXIT_FAILURE); | |
c60b6dda MP |
69 | } |
70 | ||
e2d41370 | 71 | return env; |
f853c6ef | 72 | } |
49cdae63 FB |
73 | |
74 | const char* get_catalog_dir(void) { | |
75 | const char *env; | |
76 | ||
77 | load_testdata_env(); | |
78 | ||
79 | /* if the env var is set, use that */ | |
80 | env = getenv("SYSTEMD_CATALOG_DIR"); | |
81 | if (!env) | |
82 | env = SYSTEMD_CATALOG_DIR; | |
83 | if (access(env, F_OK) < 0) { | |
84 | fprintf(stderr, "ERROR: $SYSTEMD_CATALOG_DIR directory [%s] does not exist\n", env); | |
85 | exit(EXIT_FAILURE); | |
86 | } | |
87 | return env; | |
88 | } | |
0cf29baa ZJS |
89 | |
90 | bool slow_tests_enabled(void) { | |
91 | int r; | |
92 | ||
93 | r = getenv_bool("SYSTEMD_SLOW_TESTS"); | |
94 | if (r >= 0) | |
95 | return r; | |
96 | ||
97 | if (r != -ENXIO) | |
98 | log_warning_errno(r, "Cannot parse $SYSTEMD_SLOW_TESTS, ignoring."); | |
99 | return SYSTEMD_SLOW_TESTS_DEFAULT; | |
100 | } | |
317bb217 | 101 | |
6d7c4033 ZJS |
102 | void test_setup_logging(int level) { |
103 | log_set_max_level(level); | |
104 | log_parse_environment(); | |
105 | log_open(); | |
106 | } | |
107 | ||
317bb217 ZJS |
108 | int log_tests_skipped(const char *message) { |
109 | log_notice("%s: %s, skipping tests.", | |
110 | program_invocation_short_name, message); | |
111 | return EXIT_TEST_SKIP; | |
112 | } | |
730d989a ZJS |
113 | |
114 | int log_tests_skipped_errno(int r, const char *message) { | |
115 | log_notice_errno(r, "%s: %s, skipping tests: %m", | |
116 | program_invocation_short_name, message); | |
117 | return EXIT_TEST_SKIP; | |
118 | } | |
a4bc3c1d LP |
119 | |
120 | bool have_namespaces(void) { | |
121 | siginfo_t si = {}; | |
122 | pid_t pid; | |
123 | ||
124 | /* Checks whether namespaces are available. In some cases they aren't. We do this by calling unshare(), and we | |
125 | * do so in a child process in order not to affect our own process. */ | |
126 | ||
127 | pid = fork(); | |
128 | assert_se(pid >= 0); | |
129 | ||
130 | if (pid == 0) { | |
131 | /* child */ | |
132 | if (unshare(CLONE_NEWNS) < 0) | |
133 | _exit(EXIT_FAILURE); | |
134 | ||
135 | if (mount(NULL, "/", NULL, MS_SLAVE|MS_REC, NULL) < 0) | |
136 | _exit(EXIT_FAILURE); | |
137 | ||
138 | _exit(EXIT_SUCCESS); | |
139 | } | |
140 | ||
141 | assert_se(waitid(P_PID, pid, &si, WEXITED) >= 0); | |
142 | assert_se(si.si_code == CLD_EXITED); | |
143 | ||
144 | if (si.si_status == EXIT_SUCCESS) | |
145 | return true; | |
146 | ||
147 | if (si.si_status == EXIT_FAILURE) | |
148 | return false; | |
149 | ||
150 | assert_not_reached("unexpected exit code"); | |
151 | } |