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
=$
(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
=$
(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
=$
(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
()
159 local output_pattern
='Working directory \".*\" is not writable'
164 working_dir
=$
(mktemp
-d)
166 diag
"Test lttng-relayd change working directory on non writable directory"
168 # Removing write access to working dir
169 okx
chmod -w "$working_dir"
171 # Redirect the error output to a temporary file
172 ERROR_OUTPUT_DEST
=$
(mktemp
)
173 start_lttng_relayd_opt
1 "-b" "-v --working-dir $working_dir"
175 pid
=$
(pgrep
"$RELAYD_MATCH")
176 ok $?
"Found lttng-relayd"
178 cwd
=$
(readlink
"/proc/${pid}/cwd")
179 is
"$cwd" "$working_dir" "Working directory changed"
181 grep -q "$output_pattern" "$ERROR_OUTPUT_DEST"
182 ok $?
"Warning about missing write permission is present"
185 rm "$ERROR_OUTPUT_DEST"
186 rm -rf "$working_dir" "$ERROR_OUTPUT_DEST"
187 ERROR_OUTPUT_DEST
=/dev
/null
190 function test_relayd_failure
()
192 local output_pattern
='Failed to change working directory to'
193 local relayd_bin_path
="$DIR/../src/bin/lttng-relayd/$RELAYD_BIN"
196 local working_dir_imaginary
200 working_dir
="$(mktemp -d)"
201 working_dir_imaginary
="${working_dir}/imaginary_directory"
202 output_dest
=$
(mktemp
)
204 diag
"Test lttng-relayd normal mode change non-existing directory"
206 $relayd_bin_path -b --working-directory "$working_dir_imaginary" > "$output_dest" 2>&1
208 ok $?
"Expect failure to start lttng-relayd for non-existent working directory"
210 pid
=$
(pgrep
"$RELAYD_MATCH")
211 if [ -z "$pid" ]; then
212 pass
"No lttng-relayd present"
214 fail
"No lttng-relayd present"
215 stop_lttng_relayd_notap
218 grep -q "$output_pattern" "$output_dest"
219 ok $?
"Found error message: invalid directory"
222 rm -rf "$working_dir"
225 function test_relayd_env
()
231 working_dir
=$
(mktemp
-d)
233 diag
"Test lttng-relayd change working directory from env. variable"
235 export LTTNG_RELAYD_WORKING_DIRECTORY
=${working_dir}
236 start_lttng_relayd_opt
1 "-b" ""
238 pid
=$
(pgrep
"$RELAYD_MATCH")
239 ok $?
"Found lttng-relayd"
241 cwd
=$
(readlink
"/proc/$pid/cwd")
243 is
"$cwd" "$working_dir" "Working directory changed"
246 rm -rf "$working_dir"
247 unset LTTNG_RELAYD_WORKING_DIRECTORY
250 function test_relayd_cmdline_overwrite_env
()
252 local working_dir_env
253 local working_dir_cmdline
257 working_dir_env
=$
(mktemp
-d)
258 working_dir_cmdline
=$
(mktemp
-d)
260 diag
"Test lttng-relayd change working directory command line overwrite env variable"
262 export LTTNG_RELAYD_WORKING_DIRECTORY
=${working_dir_env}
263 start_lttng_relayd_opt
1 "-b" "--working-dir ${working_dir_cmdline}"
265 pid
=$
(pgrep
"$RELAYD_MATCH")
266 ok $?
"Found lttng-relayd"
268 cwd
=$
(readlink
"/proc/$pid/cwd")
270 is
"$cwd" "$working_dir_cmdline" "Working directory is the one from command line"
273 rm -rf "$working_dir_env" "$working_dir_cmdline"
274 unset LTTNG_RELAYD_WORKING_DIRECTORY
280 test_relayd_daemon_no_working_dir
281 test_relayd_background
282 test_relayd_background_no_working_dir
283 test_relayd_debug_permission
286 test_relayd_cmdline_overwrite_env
289 for fct_test
in "${TESTS[@]}";
291 if ! ${fct_test}; then