3 # Copyright (C) 2018 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
5 # SPDX-License-Identifier: LGPL-2.1-only
7 TEST_DESC
="Change working directory of process"
9 CURDIR
=$
(dirname "$0")/
10 TESTDIR
=$CURDIR/..
/..
/..
/
12 DIR
=$
(readlink
-f "$TESTDIR")
16 source $TESTDIR/utils
/utils.sh
18 #MUST set TESTDIR before calling those functions
21 print_test_banner
"$TEST_DESC"
22 function test_relayd
()
24 local relayd_bin_path
="$DIR/../src/bin/lttng-relayd/$RELAYD_BIN"
29 working_dir
=$
(realpath
"$(mktemp -d)")
31 diag
"Test lttng-relayd normal mode change working directory"
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 &
43 cwd
=$
(readlink
"/proc/${pid}/cwd")
44 if test "$working_dir" = "$cwd"; then
45 # Working dir for process is valid
51 # If we are here the test passed
52 pass
"Working directory changed"
58 function test_relayd_daemon
()
64 working_dir
=$
(realpath
"$(mktemp -d)")
66 diag
"Test lttng-relayd daemon mode change working directory"
68 start_lttng_relayd_opt
1 "-d" "--working-directory $working_dir"
70 pid
=$
(pgrep
"$RELAYD_MATCH")
71 ok $?
"Found lttng-relayd"
73 cwd
=$
(readlink
"/proc/${pid}/cwd")
75 is
"$cwd" "$working_dir" "Working directory changed"
81 function test_relayd_daemon_no_working_dir
()
83 local expected_working_dir
="/"
87 diag
"Test lttng-relayd daemon mode change working directory"
89 start_lttng_relayd_opt
1 "-d" ""
91 pid
=$
(pgrep
"$RELAYD_MATCH")
92 ok $?
"Found lttng-relayd"
94 cwd
=$
(readlink
"/proc/${pid}/cwd")
96 is
"$cwd" "$expected_working_dir" "Working directory is $expected_working_dir"
102 function test_relayd_background
()
108 working_dir
=$
(realpath
"$(mktemp -d)")
110 diag
"Test lttng-relayd background mode change working directory"
112 start_lttng_relayd_opt
1 "-b" "--working-directory $working_dir"
114 pid
=$
(pgrep
"$RELAYD_MATCH")
115 ok $?
"Found lttng-relayd"
117 cwd
=$
(readlink
"/proc/${pid}/cwd")
119 is
"$cwd" "$working_dir" "Working directory changed"
122 rm -rf "$working_dir"
125 function test_relayd_background_no_working_dir
()
127 local expected_working_dir
="/"
131 diag
"Test lttng-relayd background working directory"
133 start_lttng_relayd_opt
1 "-b" ""
135 pid
=$
(pgrep
"$RELAYD_MATCH")
136 ok $?
"Found lttng-relayd"
138 cwd
=$
(readlink
"/proc/${pid}/cwd")
140 is
"$cwd" "$expected_working_dir" "Working directory is $expected_working_dir"
143 rm -rf "$working_dir"
146 function test_relayd_debug_permission
()
148 local output_pattern
='Working directory \".*\" is not writable'
153 working_dir
=$
(realpath
"$(mktemp -d)")
155 diag
"Test lttng-relayd change working directory on non writable directory"
157 # Removing write access to working dir
158 okx
chmod -w "$working_dir"
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"
164 pid
=$
(pgrep
"$RELAYD_MATCH")
165 ok $?
"Found lttng-relayd"
167 cwd
=$
(readlink
"/proc/${pid}/cwd")
168 is
"$cwd" "$working_dir" "Working directory changed"
170 grep -q "$output_pattern" "$ERROR_OUTPUT_DEST"
171 ok $?
"Warning about missing write permission is present"
174 rm "$ERROR_OUTPUT_DEST"
175 rm -rf "$working_dir" "$ERROR_OUTPUT_DEST"
176 ERROR_OUTPUT_DEST
=/dev
/null
179 function test_relayd_failure
()
181 local output_pattern
='Failed to change working directory to'
182 local relayd_bin_path
="$DIR/../src/bin/lttng-relayd/$RELAYD_BIN"
185 local working_dir_imaginary
189 working_dir
=$
(realpath
"$(mktemp -d)")
190 working_dir_imaginary
="${working_dir}/imaginary_directory"
191 output_dest
=$
(mktemp
)
193 diag
"Test lttng-relayd normal mode change non-existing directory"
195 $relayd_bin_path -b --working-directory "$working_dir_imaginary" > "$output_dest" 2>&1
197 ok $?
"Expect failure to start lttng-relayd for non-existent working directory"
199 pid
=$
(pgrep
"$RELAYD_MATCH")
200 if [ -z "$pid" ]; then
201 pass
"No lttng-relayd present"
203 fail
"No lttng-relayd present"
204 stop_lttng_relayd_notap
207 grep -q "$output_pattern" "$output_dest"
208 ok $?
"Found error message: invalid directory"
211 rm -rf "$working_dir"
214 function test_relayd_env
()
220 working_dir
=$
(mktemp
-d)
221 working_dir
=$
(realpath
"$(mktemp -d)")
223 diag
"Test lttng-relayd change working directory from env. variable"
225 export LTTNG_RELAYD_WORKING_DIRECTORY
=${working_dir}
226 start_lttng_relayd_opt
1 "-b" ""
228 pid
=$
(pgrep
"$RELAYD_MATCH")
229 ok $?
"Found lttng-relayd"
231 cwd
=$
(readlink
"/proc/$pid/cwd")
233 is
"$cwd" "$working_dir" "Working directory changed"
236 rm -rf "$working_dir"
237 unset LTTNG_RELAYD_WORKING_DIRECTORY
240 function test_relayd_cmdline_overwrite_env
()
242 local working_dir_env
243 local working_dir_cmdline
247 working_dir_env
=$
(realpath
"$(mktemp -d)")
248 working_dir_cmdline
=$
(realpath
"$(mktemp -d)")
250 diag
"Test lttng-relayd change working directory command line overwrite env variable"
252 export LTTNG_RELAYD_WORKING_DIRECTORY
=${working_dir_env}
253 start_lttng_relayd_opt
1 "-b" "--working-dir ${working_dir_cmdline}"
255 pid
=$
(pgrep
"$RELAYD_MATCH")
256 ok $?
"Found lttng-relayd"
258 cwd
=$
(readlink
"/proc/$pid/cwd")
260 is
"$cwd" "$working_dir_cmdline" "Working directory is the one from command line"
263 rm -rf "$working_dir_env" "$working_dir_cmdline"
264 unset LTTNG_RELAYD_WORKING_DIRECTORY
270 test_relayd_daemon_no_working_dir
271 test_relayd_background
272 test_relayd_background_no_working_dir
273 test_relayd_debug_permission
276 test_relayd_cmdline_overwrite_env
279 for fct_test
in "${TESTS[@]}";
281 if ! ${fct_test}; then