BACKPORT: Tests: fix: test_relayd_working_directory fails as root
[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
65 stop_lttng_relayd
b6df17eb 66 rm -rf "$working_dir"
11f9a85f
JR
67}
68
69function 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
92function 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
113function 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
136function 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
157function test_relayd_debug_permission()
158{
50bb83e4 159 local is_user
11f9a85f
JR
160
161 diag "Test lttng-relayd change working directory on non writable directory"
162
50bb83e4
JG
163 if [ "$(id -u)" == "0" ]; then
164 is_user=0
165 else
166 is_user=1
167 fi
11f9a85f 168
50bb83e4
JG
169 skip $is_user "Skipping permission debug output test; operation can't fail as root" 6 ||
170 {
171 local output_pattern='Working directory \".*\" is not writable'
172 local working_dir
173 local cwd
174 local pid
11f9a85f 175
50bb83e4 176 working_dir=$(realpath "$(mktemp -d)")
11f9a85f 177
50bb83e4
JG
178 # Removing write access to working dir
179 okx chmod -w "$working_dir"
b6df17eb 180
50bb83e4 181 # Redirect the error output to a temporary file
11f9a85f 182
50bb83e4
JG
183 ERROR_OUTPUT_DEST=$(mktemp) start_lttng_relayd_opt 1 "-b" "-v --working-dir $working_dir"
184
185 pid=$(pgrep "$RELAYD_MATCH")
186 ok $? "Found lttng-relayd"
187
188 cwd=$(readlink "/proc/${pid}/cwd")
189 is "$cwd" "$working_dir" "Working directory changed"
190
191 grep -q "$output_pattern" "$ERROR_OUTPUT_DEST"
192 ok $? "Warning about missing write permission is present"
193
194 stop_lttng_relayd
195 rm "$ERROR_OUTPUT_DEST"
196 rm -rf "$working_dir" "$ERROR_OUTPUT_DEST"
197 ERROR_OUTPUT_DEST=/dev/null
198 }
11f9a85f
JR
199}
200
201function test_relayd_failure()
202{
b6df17eb 203 local output_pattern='Failed to change working directory to'
11f9a85f
JR
204 local relayd_bin_path="$DIR/../src/bin/lttng-relayd/$RELAYD_BIN"
205
b6df17eb
JR
206 local working_dir
207 local working_dir_imaginary
208 local output_dest
209 local pid
210
f108e23f 211 working_dir=$(realpath "$(mktemp -d)")
b6df17eb
JR
212 working_dir_imaginary="${working_dir}/imaginary_directory"
213 output_dest=$(mktemp)
214
11f9a85f
JR
215 diag "Test lttng-relayd normal mode change non-existing directory"
216
b6df17eb 217 $relayd_bin_path -b --working-directory "$working_dir_imaginary" > "$output_dest" 2>&1
11f9a85f
JR
218 test $? -eq "1"
219 ok $? "Expect failure to start lttng-relayd for non-existent working directory"
220
b6df17eb
JR
221 pid=$(pgrep "$RELAYD_MATCH")
222 if [ -z "$pid" ]; then
11f9a85f
JR
223 pass "No lttng-relayd present"
224 else
225 fail "No lttng-relayd present"
226 stop_lttng_relayd_notap
227 fi
228
b6df17eb
JR
229 grep -q "$output_pattern" "$output_dest"
230 ok $? "Found error message: invalid directory"
11f9a85f 231
b6df17eb
JR
232 rm "$output_dest"
233 rm -rf "$working_dir"
11f9a85f
JR
234}
235
c7cc870e
JR
236function test_relayd_env()
237{
b6df17eb
JR
238 local working_dir
239 local cwd
240 local pid
241
242 working_dir=$(mktemp -d)
f108e23f 243 working_dir=$(realpath "$(mktemp -d)")
c7cc870e
JR
244
245 diag "Test lttng-relayd change working directory from env. variable"
246
247 export LTTNG_RELAYD_WORKING_DIRECTORY=${working_dir}
248 start_lttng_relayd_opt 1 "-b" ""
249
b6df17eb
JR
250 pid=$(pgrep "$RELAYD_MATCH")
251 ok $? "Found lttng-relayd"
c7cc870e 252
b6df17eb
JR
253 cwd=$(readlink "/proc/$pid/cwd")
254
255 is "$cwd" "$working_dir" "Working directory changed"
c7cc870e
JR
256
257 stop_lttng_relayd
b6df17eb 258 rm -rf "$working_dir"
c7cc870e
JR
259 unset LTTNG_RELAYD_WORKING_DIRECTORY
260}
261
262function test_relayd_cmdline_overwrite_env()
263{
b6df17eb
JR
264 local working_dir_env
265 local working_dir_cmdline
266 local cwd
267 local pid
268
f108e23f
JR
269 working_dir_env=$(realpath "$(mktemp -d)")
270 working_dir_cmdline=$(realpath "$(mktemp -d)")
c7cc870e
JR
271
272 diag "Test lttng-relayd change working directory command line overwrite env variable"
273
274 export LTTNG_RELAYD_WORKING_DIRECTORY=${working_dir_env}
275 start_lttng_relayd_opt 1 "-b" "--working-dir ${working_dir_cmdline}"
276
b6df17eb
JR
277 pid=$(pgrep "$RELAYD_MATCH")
278 ok $? "Found lttng-relayd"
c7cc870e 279
b6df17eb
JR
280 cwd=$(readlink "/proc/$pid/cwd")
281
282 is "$cwd" "$working_dir_cmdline" "Working directory is the one from command line"
c7cc870e
JR
283
284 stop_lttng_relayd
b6df17eb 285 rm -rf "$working_dir_env" "$working_dir_cmdline"
c7cc870e
JR
286 unset LTTNG_RELAYD_WORKING_DIRECTORY
287}
b6df17eb 288
11f9a85f
JR
289TESTS=(
290 test_relayd
291 test_relayd_daemon
292 test_relayd_daemon_no_working_dir
293 test_relayd_background
294 test_relayd_background_no_working_dir
295 test_relayd_debug_permission
296 test_relayd_failure
c7cc870e
JR
297 test_relayd_env
298 test_relayd_cmdline_overwrite_env
11f9a85f
JR
299)
300
b6df17eb 301for fct_test in "${TESTS[@]}";
11f9a85f 302do
b6df17eb 303 if ! ${fct_test}; then
11f9a85f
JR
304 break;
305 fi
306done
This page took 0.04413 seconds and 5 git commands to generate.