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 | ||
70 | pid=$(pgrep "$RELAYD_MATCH") | |
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 | ||
91 | pid=$(pgrep "$RELAYD_MATCH") | |
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 | ||
114 | pid=$(pgrep "$RELAYD_MATCH") | |
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 | ||
135 | pid=$(pgrep "$RELAYD_MATCH") | |
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 | { | |
148 | local output_pattern='Working directory \".*\" is not writable' | |
149 | local working_dir | |
150 | local cwd | |
151 | local pid | |
152 | ||
b2f4c4fa | 153 | working_dir=$(realpath "$(mktemp -d)") |
f3630ec4 JR |
154 | |
155 | diag "Test lttng-relayd change working directory on non writable directory" | |
156 | ||
157 | # Removing write access to working dir | |
158 | okx chmod -w "$working_dir" | |
159 | ||
160 | # Redirect the error output to a temporary file | |
161 | ERROR_OUTPUT_DEST=$(mktemp) | |
162 | start_lttng_relayd_opt 1 "-b" "-v --working-dir $working_dir" | |
163 | ||
164 | pid=$(pgrep "$RELAYD_MATCH") | |
165 | ok $? "Found lttng-relayd" | |
166 | ||
167 | cwd=$(readlink "/proc/${pid}/cwd") | |
168 | is "$cwd" "$working_dir" "Working directory changed" | |
169 | ||
170 | grep -q "$output_pattern" "$ERROR_OUTPUT_DEST" | |
171 | ok $? "Warning about missing write permission is present" | |
172 | ||
173 | stop_lttng_relayd | |
174 | rm "$ERROR_OUTPUT_DEST" | |
175 | rm -rf "$working_dir" "$ERROR_OUTPUT_DEST" | |
176 | ERROR_OUTPUT_DEST=/dev/null | |
177 | } | |
178 | ||
179 | function test_relayd_failure() | |
180 | { | |
181 | local output_pattern='Failed to change working directory to' | |
182 | local relayd_bin_path="$DIR/../src/bin/lttng-relayd/$RELAYD_BIN" | |
183 | ||
184 | local working_dir | |
185 | local working_dir_imaginary | |
186 | local output_dest | |
187 | local pid | |
188 | ||
b2f4c4fa | 189 | working_dir=$(realpath "$(mktemp -d)") |
f3630ec4 JR |
190 | working_dir_imaginary="${working_dir}/imaginary_directory" |
191 | output_dest=$(mktemp) | |
192 | ||
193 | diag "Test lttng-relayd normal mode change non-existing directory" | |
194 | ||
195 | $relayd_bin_path -b --working-directory "$working_dir_imaginary" > "$output_dest" 2>&1 | |
196 | test $? -eq "1" | |
197 | ok $? "Expect failure to start lttng-relayd for non-existent working directory" | |
198 | ||
199 | pid=$(pgrep "$RELAYD_MATCH") | |
200 | if [ -z "$pid" ]; then | |
201 | pass "No lttng-relayd present" | |
202 | else | |
203 | fail "No lttng-relayd present" | |
204 | stop_lttng_relayd_notap | |
205 | fi | |
206 | ||
207 | grep -q "$output_pattern" "$output_dest" | |
208 | ok $? "Found error message: invalid directory" | |
209 | ||
210 | rm "$output_dest" | |
211 | rm -rf "$working_dir" | |
212 | } | |
213 | ||
2a10de3b JR |
214 | function test_relayd_env() |
215 | { | |
216 | local working_dir | |
217 | local cwd | |
218 | local pid | |
219 | ||
220 | working_dir=$(mktemp -d) | |
b2f4c4fa | 221 | working_dir=$(realpath "$(mktemp -d)") |
2a10de3b JR |
222 | |
223 | diag "Test lttng-relayd change working directory from env. variable" | |
224 | ||
225 | export LTTNG_RELAYD_WORKING_DIRECTORY=${working_dir} | |
226 | start_lttng_relayd_opt 1 "-b" "" | |
227 | ||
228 | pid=$(pgrep "$RELAYD_MATCH") | |
229 | ok $? "Found lttng-relayd" | |
230 | ||
231 | cwd=$(readlink "/proc/$pid/cwd") | |
232 | ||
233 | is "$cwd" "$working_dir" "Working directory changed" | |
234 | ||
235 | stop_lttng_relayd | |
236 | rm -rf "$working_dir" | |
237 | unset LTTNG_RELAYD_WORKING_DIRECTORY | |
238 | } | |
239 | ||
240 | function test_relayd_cmdline_overwrite_env() | |
241 | { | |
242 | local working_dir_env | |
243 | local working_dir_cmdline | |
244 | local cwd | |
245 | local pid | |
246 | ||
b2f4c4fa JR |
247 | working_dir_env=$(realpath "$(mktemp -d)") |
248 | working_dir_cmdline=$(realpath "$(mktemp -d)") | |
2a10de3b JR |
249 | |
250 | diag "Test lttng-relayd change working directory command line overwrite env variable" | |
251 | ||
252 | export LTTNG_RELAYD_WORKING_DIRECTORY=${working_dir_env} | |
253 | start_lttng_relayd_opt 1 "-b" "--working-dir ${working_dir_cmdline}" | |
254 | ||
255 | pid=$(pgrep "$RELAYD_MATCH") | |
256 | ok $? "Found lttng-relayd" | |
257 | ||
258 | cwd=$(readlink "/proc/$pid/cwd") | |
259 | ||
260 | is "$cwd" "$working_dir_cmdline" "Working directory is the one from command line" | |
261 | ||
262 | stop_lttng_relayd | |
263 | rm -rf "$working_dir_env" "$working_dir_cmdline" | |
264 | unset LTTNG_RELAYD_WORKING_DIRECTORY | |
265 | } | |
266 | ||
f3630ec4 JR |
267 | TESTS=( |
268 | test_relayd | |
269 | test_relayd_daemon | |
270 | test_relayd_daemon_no_working_dir | |
271 | test_relayd_background | |
272 | test_relayd_background_no_working_dir | |
273 | test_relayd_debug_permission | |
274 | test_relayd_failure | |
2a10de3b JR |
275 | test_relayd_env |
276 | test_relayd_cmdline_overwrite_env | |
f3630ec4 JR |
277 | ) |
278 | ||
279 | for fct_test in "${TESTS[@]}"; | |
280 | do | |
281 | if ! ${fct_test}; then | |
282 | break; | |
283 | fi | |
284 | done |