Fix working directory test
[lttng-tools.git] / tests / regression / tools / working-directory / test_relayd_working_directory
CommitLineData
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
18TEST_DESC="Change working directory of process"
19
b6df17eb 20CURDIR=$(dirname "$0")/
11f9a85f
JR
21TESTDIR=$CURDIR/../../../
22
b6df17eb 23DIR=$(readlink -f "$TESTDIR")
11f9a85f 24
b6df17eb 25NUM_TESTS=35
11f9a85f
JR
26
27source $TESTDIR/utils/utils.sh
28
11f9a85f
JR
29#MUST set TESTDIR before calling those functions
30plan_tests $NUM_TESTS
31
32print_test_banner "$TEST_DESC"
33function 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
ef895a18 65 RELAYD_PIDS=$pid
11f9a85f 66 stop_lttng_relayd
b6df17eb 67 rm -rf "$working_dir"
11f9a85f
JR
68}
69
70function test_relayd_daemon()
71{
b6df17eb
JR
72 local working_dir
73 local cwd
74 local pid
75
f108e23f 76 working_dir=$(realpath "$(mktemp -d)")
11f9a85f
JR
77
78 diag "Test lttng-relayd daemon mode change working directory"
79
80 start_lttng_relayd_opt 1 "-d" "--working-directory $working_dir"
81
b6df17eb
JR
82 pid=$(pgrep "$RELAYD_MATCH")
83 ok $? "Found lttng-relayd"
84
85 cwd=$(readlink "/proc/${pid}/cwd")
11f9a85f 86
b6df17eb 87 is "$cwd" "$working_dir" "Working directory changed"
11f9a85f 88
ef895a18 89 RELAYD_PIDS=$pid
11f9a85f 90 stop_lttng_relayd
b6df17eb 91 rm -rf "$working_dir"
11f9a85f
JR
92}
93
94function test_relayd_daemon_no_working_dir()
95{
96 local expected_working_dir="/"
b6df17eb
JR
97 local cwd
98 local pid
11f9a85f
JR
99
100 diag "Test lttng-relayd daemon mode change working directory"
101
102 start_lttng_relayd_opt 1 "-d" ""
103
b6df17eb
JR
104 pid=$(pgrep "$RELAYD_MATCH")
105 ok $? "Found lttng-relayd"
11f9a85f 106
b6df17eb
JR
107 cwd=$(readlink "/proc/${pid}/cwd")
108
109 is "$cwd" "$expected_working_dir" "Working directory is $expected_working_dir"
11f9a85f 110
ef895a18 111 RELAYD_PIDS=$pid
11f9a85f 112 stop_lttng_relayd
b6df17eb 113 rm -rf "$working_dir"
11f9a85f
JR
114}
115
116function test_relayd_background()
117{
b6df17eb
JR
118 local working_dir
119 local cwd
120 local pid
121
f108e23f 122 working_dir=$(realpath "$(mktemp -d)")
11f9a85f
JR
123
124 diag "Test lttng-relayd background mode change working directory"
125
126 start_lttng_relayd_opt 1 "-b" "--working-directory $working_dir"
127
b6df17eb
JR
128 pid=$(pgrep "$RELAYD_MATCH")
129 ok $? "Found lttng-relayd"
11f9a85f 130
b6df17eb
JR
131 cwd=$(readlink "/proc/${pid}/cwd")
132
133 is "$cwd" "$working_dir" "Working directory changed"
11f9a85f 134
ef895a18 135 RELAYD_PIDS=$pid
11f9a85f 136 stop_lttng_relayd
b6df17eb 137 rm -rf "$working_dir"
11f9a85f
JR
138}
139
140function test_relayd_background_no_working_dir()
141{
142 local expected_working_dir="/"
b6df17eb
JR
143 local cwd
144 local pid
145
11f9a85f
JR
146 diag "Test lttng-relayd background working directory"
147
148 start_lttng_relayd_opt 1 "-b" ""
149
b6df17eb
JR
150 pid=$(pgrep "$RELAYD_MATCH")
151 ok $? "Found lttng-relayd"
11f9a85f 152
b6df17eb
JR
153 cwd=$(readlink "/proc/${pid}/cwd")
154
155 is "$cwd" "$expected_working_dir" "Working directory is $expected_working_dir"
11f9a85f 156
ef895a18 157 RELAYD_PIDS=$pid
11f9a85f 158 stop_lttng_relayd
b6df17eb 159 rm -rf "$working_dir"
11f9a85f
JR
160}
161
162function test_relayd_debug_permission()
163{
50bb83e4 164 local is_user
11f9a85f
JR
165
166 diag "Test lttng-relayd change working directory on non writable directory"
167
50bb83e4
JG
168 if [ "$(id -u)" == "0" ]; then
169 is_user=0
170 else
171 is_user=1
172 fi
11f9a85f 173
50bb83e4
JG
174 skip $is_user "Skipping permission debug output test; operation can't fail as root" 6 ||
175 {
ef895a18 176 local output_pattern='Working directory is not writable'
50bb83e4
JG
177 local working_dir
178 local cwd
179 local pid
11f9a85f 180
50bb83e4 181 working_dir=$(realpath "$(mktemp -d)")
11f9a85f 182
50bb83e4
JG
183 # Removing write access to working dir
184 okx chmod -w "$working_dir"
b6df17eb 185
50bb83e4 186 # Redirect the error output to a temporary file
11f9a85f 187
61db8e94
JG
188 ERROR_OUTPUT_DEST=$(mktemp)
189 start_lttng_relayd_opt 1 "-b" "-v --working-dir $working_dir"
50bb83e4
JG
190
191 pid=$(pgrep "$RELAYD_MATCH")
192 ok $? "Found lttng-relayd"
193
194 cwd=$(readlink "/proc/${pid}/cwd")
195 is "$cwd" "$working_dir" "Working directory changed"
196
197 grep -q "$output_pattern" "$ERROR_OUTPUT_DEST"
198 ok $? "Warning about missing write permission is present"
199
ef895a18 200 RELAYD_PIDS=$pid
50bb83e4
JG
201 stop_lttng_relayd
202 rm "$ERROR_OUTPUT_DEST"
203 rm -rf "$working_dir" "$ERROR_OUTPUT_DEST"
204 ERROR_OUTPUT_DEST=/dev/null
205 }
11f9a85f
JR
206}
207
208function test_relayd_failure()
209{
ef895a18 210 local output_pattern='Failed to change working directory'
11f9a85f
JR
211 local relayd_bin_path="$DIR/../src/bin/lttng-relayd/$RELAYD_BIN"
212
b6df17eb
JR
213 local working_dir
214 local working_dir_imaginary
215 local output_dest
216 local pid
217
f108e23f 218 working_dir=$(realpath "$(mktemp -d)")
b6df17eb
JR
219 working_dir_imaginary="${working_dir}/imaginary_directory"
220 output_dest=$(mktemp)
221
11f9a85f
JR
222 diag "Test lttng-relayd normal mode change non-existing directory"
223
b6df17eb 224 $relayd_bin_path -b --working-directory "$working_dir_imaginary" > "$output_dest" 2>&1
11f9a85f
JR
225 test $? -eq "1"
226 ok $? "Expect failure to start lttng-relayd for non-existent working directory"
227
b6df17eb
JR
228 pid=$(pgrep "$RELAYD_MATCH")
229 if [ -z "$pid" ]; then
11f9a85f
JR
230 pass "No lttng-relayd present"
231 else
232 fail "No lttng-relayd present"
ef895a18 233 RELAYD_PIDS=$pid
11f9a85f
JR
234 stop_lttng_relayd_notap
235 fi
236
b6df17eb
JR
237 grep -q "$output_pattern" "$output_dest"
238 ok $? "Found error message: invalid directory"
11f9a85f 239
b6df17eb
JR
240 rm "$output_dest"
241 rm -rf "$working_dir"
11f9a85f
JR
242}
243
c7cc870e
JR
244function test_relayd_env()
245{
b6df17eb
JR
246 local working_dir
247 local cwd
248 local pid
249
250 working_dir=$(mktemp -d)
f108e23f 251 working_dir=$(realpath "$(mktemp -d)")
c7cc870e
JR
252
253 diag "Test lttng-relayd change working directory from env. variable"
254
255 export LTTNG_RELAYD_WORKING_DIRECTORY=${working_dir}
256 start_lttng_relayd_opt 1 "-b" ""
257
b6df17eb
JR
258 pid=$(pgrep "$RELAYD_MATCH")
259 ok $? "Found lttng-relayd"
c7cc870e 260
b6df17eb
JR
261 cwd=$(readlink "/proc/$pid/cwd")
262
263 is "$cwd" "$working_dir" "Working directory changed"
c7cc870e 264
ef895a18 265 RELAYD_PIDS=$pid
c7cc870e 266 stop_lttng_relayd
b6df17eb 267 rm -rf "$working_dir"
c7cc870e
JR
268 unset LTTNG_RELAYD_WORKING_DIRECTORY
269}
270
271function test_relayd_cmdline_overwrite_env()
272{
b6df17eb
JR
273 local working_dir_env
274 local working_dir_cmdline
275 local cwd
276 local pid
277
f108e23f
JR
278 working_dir_env=$(realpath "$(mktemp -d)")
279 working_dir_cmdline=$(realpath "$(mktemp -d)")
c7cc870e
JR
280
281 diag "Test lttng-relayd change working directory command line overwrite env variable"
282
283 export LTTNG_RELAYD_WORKING_DIRECTORY=${working_dir_env}
284 start_lttng_relayd_opt 1 "-b" "--working-dir ${working_dir_cmdline}"
285
b6df17eb
JR
286 pid=$(pgrep "$RELAYD_MATCH")
287 ok $? "Found lttng-relayd"
c7cc870e 288
b6df17eb
JR
289 cwd=$(readlink "/proc/$pid/cwd")
290
291 is "$cwd" "$working_dir_cmdline" "Working directory is the one from command line"
c7cc870e 292
ef895a18 293 RELAYD_PIDS=$pid
c7cc870e 294 stop_lttng_relayd
b6df17eb 295 rm -rf "$working_dir_env" "$working_dir_cmdline"
c7cc870e
JR
296 unset LTTNG_RELAYD_WORKING_DIRECTORY
297}
b6df17eb 298
11f9a85f
JR
299TESTS=(
300 test_relayd
301 test_relayd_daemon
302 test_relayd_daemon_no_working_dir
303 test_relayd_background
304 test_relayd_background_no_working_dir
305 test_relayd_debug_permission
306 test_relayd_failure
c7cc870e
JR
307 test_relayd_env
308 test_relayd_cmdline_overwrite_env
11f9a85f
JR
309)
310
b6df17eb 311for fct_test in "${TESTS[@]}";
11f9a85f 312do
b6df17eb 313 if ! ${fct_test}; then
11f9a85f
JR
314 break;
315 fi
316done
This page took 0.042202 seconds and 5 git commands to generate.