Fix working directory test
[lttng-tools.git] / tests / regression / tools / working-directory / test_relayd_working_directory
1 #!/bin/bash
2 #
3 # Copyright (C) - 2018 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
4 #
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.
8 #
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
12 # details.
13 #
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
17
18 TEST_DESC="Change working directory of process"
19
20 CURDIR=$(dirname "$0")/
21 TESTDIR=$CURDIR/../../../
22
23 DIR=$(readlink -f "$TESTDIR")
24
25 NUM_TESTS=35
26
27 source $TESTDIR/utils/utils.sh
28
29 #MUST set TESTDIR before calling those functions
30 plan_tests $NUM_TESTS
31
32 print_test_banner "$TEST_DESC"
33 function test_relayd()
34 {
35 local relayd_bin_path="$DIR/../src/bin/lttng-relayd/$RELAYD_BIN"
36 local working_dir
37 local pid
38 local cwd
39
40 working_dir=$(realpath "$(mktemp -d)")
41
42 diag "Test lttng-relayd normal mode change working directory"
43
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 &
51 pid=$!
52
53 while true; do
54 cwd=$(readlink "/proc/${pid}/cwd")
55 if test "$working_dir" = "$cwd"; then
56 # Working dir for process is valid
57 break
58 fi
59 sleep 0.1
60 done
61
62 # If we are here the test passed
63 pass "Working directory changed"
64
65 RELAYD_PIDS=$pid
66 stop_lttng_relayd
67 rm -rf "$working_dir"
68 }
69
70 function test_relayd_daemon()
71 {
72 local working_dir
73 local cwd
74 local pid
75
76 working_dir=$(realpath "$(mktemp -d)")
77
78 diag "Test lttng-relayd daemon mode change working directory"
79
80 start_lttng_relayd_opt 1 "-d" "--working-directory $working_dir"
81
82 pid=$(pgrep "$RELAYD_MATCH")
83 ok $? "Found lttng-relayd"
84
85 cwd=$(readlink "/proc/${pid}/cwd")
86
87 is "$cwd" "$working_dir" "Working directory changed"
88
89 RELAYD_PIDS=$pid
90 stop_lttng_relayd
91 rm -rf "$working_dir"
92 }
93
94 function test_relayd_daemon_no_working_dir()
95 {
96 local expected_working_dir="/"
97 local cwd
98 local pid
99
100 diag "Test lttng-relayd daemon mode change working directory"
101
102 start_lttng_relayd_opt 1 "-d" ""
103
104 pid=$(pgrep "$RELAYD_MATCH")
105 ok $? "Found lttng-relayd"
106
107 cwd=$(readlink "/proc/${pid}/cwd")
108
109 is "$cwd" "$expected_working_dir" "Working directory is $expected_working_dir"
110
111 RELAYD_PIDS=$pid
112 stop_lttng_relayd
113 rm -rf "$working_dir"
114 }
115
116 function test_relayd_background()
117 {
118 local working_dir
119 local cwd
120 local pid
121
122 working_dir=$(realpath "$(mktemp -d)")
123
124 diag "Test lttng-relayd background mode change working directory"
125
126 start_lttng_relayd_opt 1 "-b" "--working-directory $working_dir"
127
128 pid=$(pgrep "$RELAYD_MATCH")
129 ok $? "Found lttng-relayd"
130
131 cwd=$(readlink "/proc/${pid}/cwd")
132
133 is "$cwd" "$working_dir" "Working directory changed"
134
135 RELAYD_PIDS=$pid
136 stop_lttng_relayd
137 rm -rf "$working_dir"
138 }
139
140 function test_relayd_background_no_working_dir()
141 {
142 local expected_working_dir="/"
143 local cwd
144 local pid
145
146 diag "Test lttng-relayd background working directory"
147
148 start_lttng_relayd_opt 1 "-b" ""
149
150 pid=$(pgrep "$RELAYD_MATCH")
151 ok $? "Found lttng-relayd"
152
153 cwd=$(readlink "/proc/${pid}/cwd")
154
155 is "$cwd" "$expected_working_dir" "Working directory is $expected_working_dir"
156
157 RELAYD_PIDS=$pid
158 stop_lttng_relayd
159 rm -rf "$working_dir"
160 }
161
162 function test_relayd_debug_permission()
163 {
164 local is_user
165
166 diag "Test lttng-relayd change working directory on non writable directory"
167
168 if [ "$(id -u)" == "0" ]; then
169 is_user=0
170 else
171 is_user=1
172 fi
173
174 skip $is_user "Skipping permission debug output test; operation can't fail as root" 6 ||
175 {
176 local output_pattern='Working directory is not writable'
177 local working_dir
178 local cwd
179 local pid
180
181 working_dir=$(realpath "$(mktemp -d)")
182
183 # Removing write access to working dir
184 okx chmod -w "$working_dir"
185
186 # Redirect the error output to a temporary file
187
188 ERROR_OUTPUT_DEST=$(mktemp)
189 start_lttng_relayd_opt 1 "-b" "-v --working-dir $working_dir"
190
191 pid=$(pgrep "$RELAYD_MATCH")
192 ok $? "Found lttng-relayd"
193
194 cwd=$(readlink "/proc/${pid}/cwd")
195 is "$cwd" "$working_dir" "Working directory changed"
196
197 grep -q "$output_pattern" "$ERROR_OUTPUT_DEST"
198 ok $? "Warning about missing write permission is present"
199
200 RELAYD_PIDS=$pid
201 stop_lttng_relayd
202 rm "$ERROR_OUTPUT_DEST"
203 rm -rf "$working_dir" "$ERROR_OUTPUT_DEST"
204 ERROR_OUTPUT_DEST=/dev/null
205 }
206 }
207
208 function test_relayd_failure()
209 {
210 local output_pattern='Failed to change working directory'
211 local relayd_bin_path="$DIR/../src/bin/lttng-relayd/$RELAYD_BIN"
212
213 local working_dir
214 local working_dir_imaginary
215 local output_dest
216 local pid
217
218 working_dir=$(realpath "$(mktemp -d)")
219 working_dir_imaginary="${working_dir}/imaginary_directory"
220 output_dest=$(mktemp)
221
222 diag "Test lttng-relayd normal mode change non-existing directory"
223
224 $relayd_bin_path -b --working-directory "$working_dir_imaginary" > "$output_dest" 2>&1
225 test $? -eq "1"
226 ok $? "Expect failure to start lttng-relayd for non-existent working directory"
227
228 pid=$(pgrep "$RELAYD_MATCH")
229 if [ -z "$pid" ]; then
230 pass "No lttng-relayd present"
231 else
232 fail "No lttng-relayd present"
233 RELAYD_PIDS=$pid
234 stop_lttng_relayd_notap
235 fi
236
237 grep -q "$output_pattern" "$output_dest"
238 ok $? "Found error message: invalid directory"
239
240 rm "$output_dest"
241 rm -rf "$working_dir"
242 }
243
244 function test_relayd_env()
245 {
246 local working_dir
247 local cwd
248 local pid
249
250 working_dir=$(mktemp -d)
251 working_dir=$(realpath "$(mktemp -d)")
252
253 diag "Test lttng-relayd change working directory from env. variable"
254
255 export LTTNG_RELAYD_WORKING_DIRECTORY=${working_dir}
256 start_lttng_relayd_opt 1 "-b" ""
257
258 pid=$(pgrep "$RELAYD_MATCH")
259 ok $? "Found lttng-relayd"
260
261 cwd=$(readlink "/proc/$pid/cwd")
262
263 is "$cwd" "$working_dir" "Working directory changed"
264
265 RELAYD_PIDS=$pid
266 stop_lttng_relayd
267 rm -rf "$working_dir"
268 unset LTTNG_RELAYD_WORKING_DIRECTORY
269 }
270
271 function test_relayd_cmdline_overwrite_env()
272 {
273 local working_dir_env
274 local working_dir_cmdline
275 local cwd
276 local pid
277
278 working_dir_env=$(realpath "$(mktemp -d)")
279 working_dir_cmdline=$(realpath "$(mktemp -d)")
280
281 diag "Test lttng-relayd change working directory command line overwrite env variable"
282
283 export LTTNG_RELAYD_WORKING_DIRECTORY=${working_dir_env}
284 start_lttng_relayd_opt 1 "-b" "--working-dir ${working_dir_cmdline}"
285
286 pid=$(pgrep "$RELAYD_MATCH")
287 ok $? "Found lttng-relayd"
288
289 cwd=$(readlink "/proc/$pid/cwd")
290
291 is "$cwd" "$working_dir_cmdline" "Working directory is the one from command line"
292
293 RELAYD_PIDS=$pid
294 stop_lttng_relayd
295 rm -rf "$working_dir_env" "$working_dir_cmdline"
296 unset LTTNG_RELAYD_WORKING_DIRECTORY
297 }
298
299 TESTS=(
300 test_relayd
301 test_relayd_daemon
302 test_relayd_daemon_no_working_dir
303 test_relayd_background
304 test_relayd_background_no_working_dir
305 test_relayd_debug_permission
306 test_relayd_failure
307 test_relayd_env
308 test_relayd_cmdline_overwrite_env
309 )
310
311 for fct_test in "${TESTS[@]}";
312 do
313 if ! ${fct_test}; then
314 break;
315 fi
316 done
This page took 0.039813 seconds and 5 git commands to generate.