3 # Copyright (C) - 2018 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
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.
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
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
18 TEST_DESC
="Change working directory of process"
20 CURDIR
=$
(dirname "$0")/
21 TESTDIR
=$CURDIR/..
/..
/..
/
23 DIR
=$
(readlink
-f "$TESTDIR")
27 source $TESTDIR/utils
/utils.sh
29 #MUST set TESTDIR before calling those functions
32 print_test_banner
"$TEST_DESC"
33 function test_relayd
()
35 local relayd_bin_path
="$DIR/../src/bin/lttng-relayd/$RELAYD_BIN"
40 working_dir
=$
(realpath
"$(mktemp -d)")
42 diag
"Test lttng-relayd normal mode change working directory"
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.
50 $relayd_bin_path --working-directory "$working_dir" > /dev
/null
2>&1 &
54 cwd
=$
(readlink
"/proc/${pid}/cwd")
55 if test "$working_dir" = "$cwd"; then
56 # Working dir for process is valid
62 # If we are here the test passed
63 pass
"Working directory changed"
69 function test_relayd_daemon
()
75 working_dir
=$
(realpath
"$(mktemp -d)")
77 diag
"Test lttng-relayd daemon mode change working directory"
79 start_lttng_relayd_opt
1 "-d" "--working-directory $working_dir"
81 pid
=$
(pgrep
"$RELAYD_MATCH")
82 ok $?
"Found lttng-relayd"
84 cwd
=$
(readlink
"/proc/${pid}/cwd")
86 is
"$cwd" "$working_dir" "Working directory changed"
92 function test_relayd_daemon_no_working_dir
()
94 local expected_working_dir
="/"
98 diag
"Test lttng-relayd daemon mode change working directory"
100 start_lttng_relayd_opt
1 "-d" ""
102 pid
=$
(pgrep
"$RELAYD_MATCH")
103 ok $?
"Found lttng-relayd"
105 cwd
=$
(readlink
"/proc/${pid}/cwd")
107 is
"$cwd" "$expected_working_dir" "Working directory is $expected_working_dir"
110 rm -rf "$working_dir"
113 function test_relayd_background
()
119 working_dir
=$
(realpath
"$(mktemp -d)")
121 diag
"Test lttng-relayd background mode change working directory"
123 start_lttng_relayd_opt
1 "-b" "--working-directory $working_dir"
125 pid
=$
(pgrep
"$RELAYD_MATCH")
126 ok $?
"Found lttng-relayd"
128 cwd
=$
(readlink
"/proc/${pid}/cwd")
130 is
"$cwd" "$working_dir" "Working directory changed"
133 rm -rf "$working_dir"
136 function test_relayd_background_no_working_dir
()
138 local expected_working_dir
="/"
142 diag
"Test lttng-relayd background working directory"
144 start_lttng_relayd_opt
1 "-b" ""
146 pid
=$
(pgrep
"$RELAYD_MATCH")
147 ok $?
"Found lttng-relayd"
149 cwd
=$
(readlink
"/proc/${pid}/cwd")
151 is
"$cwd" "$expected_working_dir" "Working directory is $expected_working_dir"
154 rm -rf "$working_dir"
157 function test_relayd_debug_permission
()
161 diag
"Test lttng-relayd change working directory on non writable directory"
163 if [ "$(id -u)" == "0" ]; then
169 skip
$is_user "Skipping permission debug output test; operation can't fail as root" 6 ||
171 local output_pattern
='Working directory \".*\" is not writable'
176 working_dir
=$
(realpath
"$(mktemp -d)")
178 # Removing write access to working dir
179 okx
chmod -w "$working_dir"
181 # Redirect the error output to a temporary file
183 ERROR_OUTPUT_DEST
=$
(mktemp
)
184 start_lttng_relayd_opt
1 "-b" "-v --working-dir $working_dir"
186 pid
=$
(pgrep
"$RELAYD_MATCH")
187 ok $?
"Found lttng-relayd"
189 cwd
=$
(readlink
"/proc/${pid}/cwd")
190 is
"$cwd" "$working_dir" "Working directory changed"
192 grep -q "$output_pattern" "$ERROR_OUTPUT_DEST"
193 ok $?
"Warning about missing write permission is present"
196 rm "$ERROR_OUTPUT_DEST"
197 rm -rf "$working_dir" "$ERROR_OUTPUT_DEST"
198 ERROR_OUTPUT_DEST
=/dev
/null
202 function test_relayd_failure
()
204 local output_pattern
='Failed to change working directory to'
205 local relayd_bin_path
="$DIR/../src/bin/lttng-relayd/$RELAYD_BIN"
208 local working_dir_imaginary
212 working_dir
=$
(realpath
"$(mktemp -d)")
213 working_dir_imaginary
="${working_dir}/imaginary_directory"
214 output_dest
=$
(mktemp
)
216 diag
"Test lttng-relayd normal mode change non-existing directory"
218 $relayd_bin_path -b --working-directory "$working_dir_imaginary" > "$output_dest" 2>&1
220 ok $?
"Expect failure to start lttng-relayd for non-existent working directory"
222 pid
=$
(pgrep
"$RELAYD_MATCH")
223 if [ -z "$pid" ]; then
224 pass
"No lttng-relayd present"
226 fail
"No lttng-relayd present"
227 stop_lttng_relayd_notap
230 grep -q "$output_pattern" "$output_dest"
231 ok $?
"Found error message: invalid directory"
234 rm -rf "$working_dir"
237 function test_relayd_env
()
243 working_dir
=$
(mktemp
-d)
244 working_dir
=$
(realpath
"$(mktemp -d)")
246 diag
"Test lttng-relayd change working directory from env. variable"
248 export LTTNG_RELAYD_WORKING_DIRECTORY
=${working_dir}
249 start_lttng_relayd_opt
1 "-b" ""
251 pid
=$
(pgrep
"$RELAYD_MATCH")
252 ok $?
"Found lttng-relayd"
254 cwd
=$
(readlink
"/proc/$pid/cwd")
256 is
"$cwd" "$working_dir" "Working directory changed"
259 rm -rf "$working_dir"
260 unset LTTNG_RELAYD_WORKING_DIRECTORY
263 function test_relayd_cmdline_overwrite_env
()
265 local working_dir_env
266 local working_dir_cmdline
270 working_dir_env
=$
(realpath
"$(mktemp -d)")
271 working_dir_cmdline
=$
(realpath
"$(mktemp -d)")
273 diag
"Test lttng-relayd change working directory command line overwrite env variable"
275 export LTTNG_RELAYD_WORKING_DIRECTORY
=${working_dir_env}
276 start_lttng_relayd_opt
1 "-b" "--working-dir ${working_dir_cmdline}"
278 pid
=$
(pgrep
"$RELAYD_MATCH")
279 ok $?
"Found lttng-relayd"
281 cwd
=$
(readlink
"/proc/$pid/cwd")
283 is
"$cwd" "$working_dir_cmdline" "Working directory is the one from command line"
286 rm -rf "$working_dir_env" "$working_dir_cmdline"
287 unset LTTNG_RELAYD_WORKING_DIRECTORY
293 test_relayd_daemon_no_working_dir
294 test_relayd_background
295 test_relayd_background_no_working_dir
296 test_relayd_debug_permission
299 test_relayd_cmdline_overwrite_env
302 for fct_test
in "${TESTS[@]}";
304 if ! ${fct_test}; then