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