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
30 #MUST set TESTDIR before calling those functions
33 print_test_banner
"$TEST_DESC"
34 function test_relayd
()
36 local relayd_bin_path
="$DIR/../src/bin/lttng-relayd/$RELAYD_BIN"
41 working_dir
=$
(mktemp
-d)
43 diag
"Test lttng-relayd normal mode change working directory"
45 # There is no rendez-vous mechanism that can guarantee the good timing
46 # to check if the workdir directory was changed.
47 # In the case of lttng-sessiond this would be achieved using the
48 # --sig-parent option but lttng-relayd does not have this feature yet.
49 # Fall back on using polling of the value and unblock when the value is
50 # the one we expect. In case of a failure the test will hang.
51 $relayd_bin_path --working-directory "$working_dir" > /dev
/null
2>&1 &
55 cwd
=$
(readlink
"/proc/${pid}/cwd")
56 if test "$working_dir" = "$cwd"; then
57 # Working dir for process is valid
63 # If we are here the test passed
64 pass
"Working directory changed"
70 function test_relayd_daemon
()
76 working_dir
=$
(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"
93 function test_relayd_daemon_no_working_dir
()
95 local expected_working_dir
="/"
99 diag
"Test lttng-relayd daemon mode change working directory"
101 start_lttng_relayd_opt
1 "-d" ""
103 pid
=$
(pgrep
"$RELAYD_MATCH")
104 ok $?
"Found lttng-relayd"
106 cwd
=$
(readlink
"/proc/${pid}/cwd")
108 is
"$cwd" "$expected_working_dir" "Working directory is $expected_working_dir"
111 rm -rf "$working_dir"
114 function test_relayd_background
()
120 working_dir
=$
(mktemp
-d)
122 diag
"Test lttng-relayd background mode change working directory"
124 start_lttng_relayd_opt
1 "-b" "--working-directory $working_dir"
126 pid
=$
(pgrep
"$RELAYD_MATCH")
127 ok $?
"Found lttng-relayd"
129 cwd
=$
(readlink
"/proc/${pid}/cwd")
131 is
"$cwd" "$working_dir" "Working directory changed"
134 rm -rf "$working_dir"
137 function test_relayd_background_no_working_dir
()
139 local expected_working_dir
="/"
143 diag
"Test lttng-relayd background working directory"
145 start_lttng_relayd_opt
1 "-b" ""
147 pid
=$
(pgrep
"$RELAYD_MATCH")
148 ok $?
"Found lttng-relayd"
150 cwd
=$
(readlink
"/proc/${pid}/cwd")
152 is
"$cwd" "$expected_working_dir" "Working directory is $expected_working_dir"
155 rm -rf "$working_dir"
158 function test_relayd_debug_permission
()
160 local output_pattern
='Working directory \".*\" is not writable'
165 working_dir
=$
(mktemp
-d)
167 diag
"Test lttng-relayd change working directory on non writable directory"
169 # Removing write access to working dir
170 okx
chmod -w "$working_dir"
172 # Redirect the error output to a temporary file
173 ERROR_OUTPUT_DEST
=$
(mktemp
)
174 start_lttng_relayd_opt
1 "-b" "-v --working-dir $working_dir"
176 pid
=$
(pgrep
"$RELAYD_MATCH")
177 ok $?
"Found lttng-relayd"
179 cwd
=$
(readlink
"/proc/${pid}/cwd")
180 is
"$cwd" "$working_dir" "Working directory changed"
182 grep -q "$output_pattern" "$ERROR_OUTPUT_DEST"
183 ok $?
"Warning about missing write permission is present"
186 rm "$ERROR_OUTPUT_DEST"
187 rm -rf "$working_dir" "$ERROR_OUTPUT_DEST"
188 ERROR_OUTPUT_DEST
=/dev
/null
191 function test_relayd_failure
()
193 local output_pattern
='Failed to change working directory to'
194 local relayd_bin_path
="$DIR/../src/bin/lttng-relayd/$RELAYD_BIN"
197 local working_dir_imaginary
201 working_dir
="$(mktemp -d)"
202 working_dir_imaginary
="${working_dir}/imaginary_directory"
203 output_dest
=$
(mktemp
)
205 diag
"Test lttng-relayd normal mode change non-existing directory"
207 $relayd_bin_path -b --working-directory "$working_dir_imaginary" > "$output_dest" 2>&1
209 ok $?
"Expect failure to start lttng-relayd for non-existent working directory"
211 pid
=$
(pgrep
"$RELAYD_MATCH")
212 if [ -z "$pid" ]; then
213 pass
"No lttng-relayd present"
215 fail
"No lttng-relayd present"
216 stop_lttng_relayd_notap
219 grep -q "$output_pattern" "$output_dest"
220 ok $?
"Found error message: invalid directory"
223 rm -rf "$working_dir"
229 test_relayd_daemon_no_working_dir
230 test_relayd_background
231 test_relayd_background_no_working_dir
232 test_relayd_debug_permission
236 for fct_test
in "${TESTS[@]}";
238 if ! ${fct_test}; then