Commit | Line | Data |
---|---|---|
f3630ec4 JR |
1 | #!/bin/bash |
2 | # | |
9d16b343 | 3 | # Copyright (C) 2018 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com> |
f3630ec4 | 4 | # |
9d16b343 | 5 | # SPDX-License-Identifier: LGPL-2.1-only |
f3630ec4 JR |
6 | |
7 | TEST_DESC="Change working directory of process" | |
8 | ||
9 | CURDIR=$(dirname "$0")/ | |
10 | TESTDIR=$CURDIR/../../../ | |
11 | ||
12 | DIR=$(readlink -f "$TESTDIR") | |
13 | ||
2a10de3b | 14 | NUM_TESTS=35 |
f3630ec4 JR |
15 | |
16 | source $TESTDIR/utils/utils.sh | |
17 | ||
f3630ec4 JR |
18 | #MUST set TESTDIR before calling those functions |
19 | plan_tests $NUM_TESTS | |
20 | ||
21 | print_test_banner "$TEST_DESC" | |
22 | function test_relayd() | |
23 | { | |
24 | local relayd_bin_path="$DIR/../src/bin/lttng-relayd/$RELAYD_BIN" | |
25 | local working_dir | |
26 | local pid | |
27 | local cwd | |
28 | ||
b2f4c4fa | 29 | working_dir=$(realpath "$(mktemp -d)") |
f3630ec4 JR |
30 | |
31 | diag "Test lttng-relayd normal mode change working directory" | |
32 | ||
33 | # There is no rendez-vous mechanism that can guarantee the good timing | |
34 | # to check if the workdir directory was changed. | |
35 | # In the case of lttng-sessiond this would be achieved using the | |
36 | # --sig-parent option but lttng-relayd does not have this feature yet. | |
37 | # Fall back on using polling of the value and unblock when the value is | |
38 | # the one we expect. In case of a failure the test will hang. | |
39 | $relayd_bin_path --working-directory "$working_dir" > /dev/null 2>&1 & | |
40 | pid=$! | |
41 | ||
42 | while true; do | |
43 | cwd=$(readlink "/proc/${pid}/cwd") | |
44 | if test "$working_dir" = "$cwd"; then | |
45 | # Working dir for process is valid | |
46 | break | |
47 | fi | |
48 | sleep 0.1 | |
49 | done | |
50 | ||
51 | # If we are here the test passed | |
52 | pass "Working directory changed" | |
53 | ||
54 | stop_lttng_relayd | |
55 | rm -rf "$working_dir" | |
56 | } | |
57 | ||
58 | function test_relayd_daemon() | |
59 | { | |
60 | local working_dir | |
61 | local cwd | |
62 | local pid | |
63 | ||
b2f4c4fa | 64 | working_dir=$(realpath "$(mktemp -d)") |
f3630ec4 JR |
65 | |
66 | diag "Test lttng-relayd daemon mode change working directory" | |
67 | ||
68 | start_lttng_relayd_opt 1 "-d" "--working-directory $working_dir" | |
69 | ||
7cb78e2f | 70 | pid=$(lttng_pgrep "$RELAYD_MATCH") |
f3630ec4 JR |
71 | ok $? "Found lttng-relayd" |
72 | ||
73 | cwd=$(readlink "/proc/${pid}/cwd") | |
74 | ||
75 | is "$cwd" "$working_dir" "Working directory changed" | |
76 | ||
77 | stop_lttng_relayd | |
78 | rm -rf "$working_dir" | |
79 | } | |
80 | ||
81 | function test_relayd_daemon_no_working_dir() | |
82 | { | |
83 | local expected_working_dir="/" | |
84 | local cwd | |
85 | local pid | |
86 | ||
87 | diag "Test lttng-relayd daemon mode change working directory" | |
88 | ||
89 | start_lttng_relayd_opt 1 "-d" "" | |
90 | ||
7cb78e2f | 91 | pid=$(lttng_pgrep "$RELAYD_MATCH") |
f3630ec4 JR |
92 | ok $? "Found lttng-relayd" |
93 | ||
94 | cwd=$(readlink "/proc/${pid}/cwd") | |
95 | ||
96 | is "$cwd" "$expected_working_dir" "Working directory is $expected_working_dir" | |
97 | ||
98 | stop_lttng_relayd | |
99 | rm -rf "$working_dir" | |
100 | } | |
101 | ||
102 | function test_relayd_background() | |
103 | { | |
104 | local working_dir | |
105 | local cwd | |
106 | local pid | |
107 | ||
b2f4c4fa | 108 | working_dir=$(realpath "$(mktemp -d)") |
f3630ec4 JR |
109 | |
110 | diag "Test lttng-relayd background mode change working directory" | |
111 | ||
112 | start_lttng_relayd_opt 1 "-b" "--working-directory $working_dir" | |
113 | ||
7cb78e2f | 114 | pid=$(lttng_pgrep "$RELAYD_MATCH") |
f3630ec4 JR |
115 | ok $? "Found lttng-relayd" |
116 | ||
117 | cwd=$(readlink "/proc/${pid}/cwd") | |
118 | ||
119 | is "$cwd" "$working_dir" "Working directory changed" | |
120 | ||
121 | stop_lttng_relayd | |
122 | rm -rf "$working_dir" | |
123 | } | |
124 | ||
125 | function test_relayd_background_no_working_dir() | |
126 | { | |
127 | local expected_working_dir="/" | |
128 | local cwd | |
129 | local pid | |
130 | ||
131 | diag "Test lttng-relayd background working directory" | |
132 | ||
133 | start_lttng_relayd_opt 1 "-b" "" | |
134 | ||
7cb78e2f | 135 | pid=$(lttng_pgrep "$RELAYD_MATCH") |
f3630ec4 JR |
136 | ok $? "Found lttng-relayd" |
137 | ||
138 | cwd=$(readlink "/proc/${pid}/cwd") | |
139 | ||
140 | is "$cwd" "$expected_working_dir" "Working directory is $expected_working_dir" | |
141 | ||
142 | stop_lttng_relayd | |
143 | rm -rf "$working_dir" | |
144 | } | |
145 | ||
146 | function test_relayd_debug_permission() | |
147 | { | |
15da468c | 148 | local is_user |
f3630ec4 JR |
149 | |
150 | diag "Test lttng-relayd change working directory on non writable directory" | |
151 | ||
15da468c JG |
152 | if [ "$(id -u)" == "0" ]; then |
153 | is_user=0 | |
154 | else | |
155 | is_user=1 | |
156 | fi | |
f3630ec4 | 157 | |
15da468c JG |
158 | skip $is_user "Skipping permission debug output test; operation can't fail as root" 6 || |
159 | { | |
160 | local output_pattern='Working directory \".*\" is not writable' | |
161 | local working_dir | |
162 | local cwd | |
163 | local pid | |
f3630ec4 | 164 | |
15da468c | 165 | working_dir=$(realpath "$(mktemp -d)") |
f3630ec4 | 166 | |
15da468c JG |
167 | # Removing write access to working dir |
168 | okx chmod -w "$working_dir" | |
f3630ec4 | 169 | |
15da468c | 170 | # Redirect the error output to a temporary file |
f3630ec4 | 171 | |
e7b7a0e3 JG |
172 | ERROR_OUTPUT_DEST=$(mktemp) |
173 | start_lttng_relayd_opt 1 "-b" "-v --working-dir $working_dir" | |
15da468c | 174 | |
7cb78e2f | 175 | pid=$(lttng_pgrep "$RELAYD_MATCH") |
15da468c JG |
176 | ok $? "Found lttng-relayd" |
177 | ||
178 | cwd=$(readlink "/proc/${pid}/cwd") | |
179 | is "$cwd" "$working_dir" "Working directory changed" | |
180 | ||
181 | grep -q "$output_pattern" "$ERROR_OUTPUT_DEST" | |
182 | ok $? "Warning about missing write permission is present" | |
183 | ||
184 | stop_lttng_relayd | |
185 | rm "$ERROR_OUTPUT_DEST" | |
186 | rm -rf "$working_dir" "$ERROR_OUTPUT_DEST" | |
187 | ERROR_OUTPUT_DEST=/dev/null | |
188 | } | |
f3630ec4 JR |
189 | } |
190 | ||
191 | function test_relayd_failure() | |
192 | { | |
193 | local output_pattern='Failed to change working directory to' | |
194 | local relayd_bin_path="$DIR/../src/bin/lttng-relayd/$RELAYD_BIN" | |
195 | ||
196 | local working_dir | |
197 | local working_dir_imaginary | |
198 | local output_dest | |
199 | local pid | |
200 | ||
b2f4c4fa | 201 | working_dir=$(realpath "$(mktemp -d)") |
f3630ec4 JR |
202 | working_dir_imaginary="${working_dir}/imaginary_directory" |
203 | output_dest=$(mktemp) | |
204 | ||
205 | diag "Test lttng-relayd normal mode change non-existing directory" | |
206 | ||
207 | $relayd_bin_path -b --working-directory "$working_dir_imaginary" > "$output_dest" 2>&1 | |
208 | test $? -eq "1" | |
209 | ok $? "Expect failure to start lttng-relayd for non-existent working directory" | |
210 | ||
7cb78e2f | 211 | pid=$(lttng_pgrep "$RELAYD_MATCH") |
f3630ec4 JR |
212 | if [ -z "$pid" ]; then |
213 | pass "No lttng-relayd present" | |
214 | else | |
215 | fail "No lttng-relayd present" | |
216 | stop_lttng_relayd_notap | |
217 | fi | |
218 | ||
219 | grep -q "$output_pattern" "$output_dest" | |
220 | ok $? "Found error message: invalid directory" | |
221 | ||
222 | rm "$output_dest" | |
223 | rm -rf "$working_dir" | |
224 | } | |
225 | ||
2a10de3b JR |
226 | function test_relayd_env() |
227 | { | |
228 | local working_dir | |
229 | local cwd | |
230 | local pid | |
231 | ||
232 | working_dir=$(mktemp -d) | |
b2f4c4fa | 233 | working_dir=$(realpath "$(mktemp -d)") |
2a10de3b JR |
234 | |
235 | diag "Test lttng-relayd change working directory from env. variable" | |
236 | ||
237 | export LTTNG_RELAYD_WORKING_DIRECTORY=${working_dir} | |
238 | start_lttng_relayd_opt 1 "-b" "" | |
239 | ||
7cb78e2f | 240 | pid=$(lttng_pgrep "$RELAYD_MATCH") |
2a10de3b JR |
241 | ok $? "Found lttng-relayd" |
242 | ||
243 | cwd=$(readlink "/proc/$pid/cwd") | |
244 | ||
245 | is "$cwd" "$working_dir" "Working directory changed" | |
246 | ||
247 | stop_lttng_relayd | |
248 | rm -rf "$working_dir" | |
249 | unset LTTNG_RELAYD_WORKING_DIRECTORY | |
250 | } | |
251 | ||
252 | function test_relayd_cmdline_overwrite_env() | |
253 | { | |
254 | local working_dir_env | |
255 | local working_dir_cmdline | |
256 | local cwd | |
257 | local pid | |
258 | ||
b2f4c4fa JR |
259 | working_dir_env=$(realpath "$(mktemp -d)") |
260 | working_dir_cmdline=$(realpath "$(mktemp -d)") | |
2a10de3b JR |
261 | |
262 | diag "Test lttng-relayd change working directory command line overwrite env variable" | |
263 | ||
264 | export LTTNG_RELAYD_WORKING_DIRECTORY=${working_dir_env} | |
265 | start_lttng_relayd_opt 1 "-b" "--working-dir ${working_dir_cmdline}" | |
266 | ||
7cb78e2f | 267 | pid=$(lttng_pgrep "$RELAYD_MATCH") |
2a10de3b JR |
268 | ok $? "Found lttng-relayd" |
269 | ||
270 | cwd=$(readlink "/proc/$pid/cwd") | |
271 | ||
272 | is "$cwd" "$working_dir_cmdline" "Working directory is the one from command line" | |
273 | ||
274 | stop_lttng_relayd | |
275 | rm -rf "$working_dir_env" "$working_dir_cmdline" | |
276 | unset LTTNG_RELAYD_WORKING_DIRECTORY | |
277 | } | |
278 | ||
f3630ec4 JR |
279 | TESTS=( |
280 | test_relayd | |
281 | test_relayd_daemon | |
282 | test_relayd_daemon_no_working_dir | |
283 | test_relayd_background | |
284 | test_relayd_background_no_working_dir | |
285 | test_relayd_debug_permission | |
286 | test_relayd_failure | |
2a10de3b JR |
287 | test_relayd_env |
288 | test_relayd_cmdline_overwrite_env | |
f3630ec4 JR |
289 | ) |
290 | ||
291 | for fct_test in "${TESTS[@]}"; | |
292 | do | |
293 | if ! ${fct_test}; then | |
294 | break; | |
295 | fi | |
296 | done |