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"
70 function test_relayd_daemon
()
76 working_dir
=$
(realpath
"$(mktemp -d)")
78 diag
"Test lttng-relayd daemon mode change working directory"
80 start_lttng_relayd_opt
1 "-d" "--working-directory $working_dir"
82 pid
=$
(pgrep
"$RELAYD_MATCH")
83 ok $?
"Found lttng-relayd"
85 cwd
=$
(readlink
"/proc/${pid}/cwd")
87 is
"$cwd" "$working_dir" "Working directory changed"
94 function test_relayd_daemon_no_working_dir
()
96 local expected_working_dir
="/"
100 diag
"Test lttng-relayd daemon mode change working directory"
102 start_lttng_relayd_opt
1 "-d" ""
104 pid
=$
(pgrep
"$RELAYD_MATCH")
105 ok $?
"Found lttng-relayd"
107 cwd
=$
(readlink
"/proc/${pid}/cwd")
109 is
"$cwd" "$expected_working_dir" "Working directory is $expected_working_dir"
113 rm -rf "$working_dir"
116 function test_relayd_background
()
122 working_dir
=$
(realpath
"$(mktemp -d)")
124 diag
"Test lttng-relayd background mode change working directory"
126 start_lttng_relayd_opt
1 "-b" "--working-directory $working_dir"
128 pid
=$
(pgrep
"$RELAYD_MATCH")
129 ok $?
"Found lttng-relayd"
131 cwd
=$
(readlink
"/proc/${pid}/cwd")
133 is
"$cwd" "$working_dir" "Working directory changed"
137 rm -rf "$working_dir"
140 function test_relayd_background_no_working_dir
()
142 local expected_working_dir
="/"
146 diag
"Test lttng-relayd background working directory"
148 start_lttng_relayd_opt
1 "-b" ""
150 pid
=$
(pgrep
"$RELAYD_MATCH")
151 ok $?
"Found lttng-relayd"
153 cwd
=$
(readlink
"/proc/${pid}/cwd")
155 is
"$cwd" "$expected_working_dir" "Working directory is $expected_working_dir"
159 rm -rf "$working_dir"
162 function test_relayd_debug_permission
()
166 diag
"Test lttng-relayd change working directory on non writable directory"
168 if [ "$(id -u)" == "0" ]; then
174 skip
$is_user "Skipping permission debug output test; operation can't fail as root" 6 ||
176 local output_pattern
='Working directory is not writable'
181 working_dir
=$
(realpath
"$(mktemp -d)")
183 # Removing write access to working dir
184 okx
chmod -w "$working_dir"
186 # Redirect the error output to a temporary file
188 ERROR_OUTPUT_DEST
=$
(mktemp
)
189 start_lttng_relayd_opt
1 "-b" "-v --working-dir $working_dir"
191 pid
=$
(pgrep
"$RELAYD_MATCH")
192 ok $?
"Found lttng-relayd"
194 cwd
=$
(readlink
"/proc/${pid}/cwd")
195 is
"$cwd" "$working_dir" "Working directory changed"
197 grep -q "$output_pattern" "$ERROR_OUTPUT_DEST"
198 ok $?
"Warning about missing write permission is present"
202 rm "$ERROR_OUTPUT_DEST"
203 rm -rf "$working_dir" "$ERROR_OUTPUT_DEST"
204 ERROR_OUTPUT_DEST
=/dev
/null
208 function test_relayd_failure
()
210 local output_pattern
='Failed to change working directory'
211 local relayd_bin_path
="$DIR/../src/bin/lttng-relayd/$RELAYD_BIN"
214 local working_dir_imaginary
218 working_dir
=$
(realpath
"$(mktemp -d)")
219 working_dir_imaginary
="${working_dir}/imaginary_directory"
220 output_dest
=$
(mktemp
)
222 diag
"Test lttng-relayd normal mode change non-existing directory"
224 $relayd_bin_path -b --working-directory "$working_dir_imaginary" > "$output_dest" 2>&1
226 ok $?
"Expect failure to start lttng-relayd for non-existent working directory"
228 pid
=$
(pgrep
"$RELAYD_MATCH")
229 if [ -z "$pid" ]; then
230 pass
"No lttng-relayd present"
232 fail
"No lttng-relayd present"
234 stop_lttng_relayd_notap
237 grep -q "$output_pattern" "$output_dest"
238 ok $?
"Found error message: invalid directory"
241 rm -rf "$working_dir"
244 function test_relayd_env
()
250 working_dir
=$
(mktemp
-d)
251 working_dir
=$
(realpath
"$(mktemp -d)")
253 diag
"Test lttng-relayd change working directory from env. variable"
255 export LTTNG_RELAYD_WORKING_DIRECTORY
=${working_dir}
256 start_lttng_relayd_opt
1 "-b" ""
258 pid
=$
(pgrep
"$RELAYD_MATCH")
259 ok $?
"Found lttng-relayd"
261 cwd
=$
(readlink
"/proc/$pid/cwd")
263 is
"$cwd" "$working_dir" "Working directory changed"
267 rm -rf "$working_dir"
268 unset LTTNG_RELAYD_WORKING_DIRECTORY
271 function test_relayd_cmdline_overwrite_env
()
273 local working_dir_env
274 local working_dir_cmdline
278 working_dir_env
=$
(realpath
"$(mktemp -d)")
279 working_dir_cmdline
=$
(realpath
"$(mktemp -d)")
281 diag
"Test lttng-relayd change working directory command line overwrite env variable"
283 export LTTNG_RELAYD_WORKING_DIRECTORY
=${working_dir_env}
284 start_lttng_relayd_opt
1 "-b" "--working-dir ${working_dir_cmdline}"
286 pid
=$
(pgrep
"$RELAYD_MATCH")
287 ok $?
"Found lttng-relayd"
289 cwd
=$
(readlink
"/proc/$pid/cwd")
291 is
"$cwd" "$working_dir_cmdline" "Working directory is the one from command line"
295 rm -rf "$working_dir_env" "$working_dir_cmdline"
296 unset LTTNG_RELAYD_WORKING_DIRECTORY
302 test_relayd_daemon_no_working_dir
303 test_relayd_background
304 test_relayd_background_no_working_dir
305 test_relayd_debug_permission
308 test_relayd_cmdline_overwrite_env
311 for fct_test
in "${TESTS[@]}";
313 if ! ${fct_test}; then