BACKPORT: Tests: fix: test_relayd_working_directory fails as root
[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 stop_lttng_relayd
66 rm -rf "$working_dir"
67 }
68
69 function test_relayd_daemon()
70 {
71 local working_dir
72 local cwd
73 local pid
74
75 working_dir=$(realpath "$(mktemp -d)")
76
77 diag "Test lttng-relayd daemon mode change working directory"
78
79 start_lttng_relayd_opt 1 "-d" "--working-directory $working_dir"
80
81 pid=$(pgrep "$RELAYD_MATCH")
82 ok $? "Found lttng-relayd"
83
84 cwd=$(readlink "/proc/${pid}/cwd")
85
86 is "$cwd" "$working_dir" "Working directory changed"
87
88 stop_lttng_relayd
89 rm -rf "$working_dir"
90 }
91
92 function test_relayd_daemon_no_working_dir()
93 {
94 local expected_working_dir="/"
95 local cwd
96 local pid
97
98 diag "Test lttng-relayd daemon mode change working directory"
99
100 start_lttng_relayd_opt 1 "-d" ""
101
102 pid=$(pgrep "$RELAYD_MATCH")
103 ok $? "Found lttng-relayd"
104
105 cwd=$(readlink "/proc/${pid}/cwd")
106
107 is "$cwd" "$expected_working_dir" "Working directory is $expected_working_dir"
108
109 stop_lttng_relayd
110 rm -rf "$working_dir"
111 }
112
113 function test_relayd_background()
114 {
115 local working_dir
116 local cwd
117 local pid
118
119 working_dir=$(realpath "$(mktemp -d)")
120
121 diag "Test lttng-relayd background mode change working directory"
122
123 start_lttng_relayd_opt 1 "-b" "--working-directory $working_dir"
124
125 pid=$(pgrep "$RELAYD_MATCH")
126 ok $? "Found lttng-relayd"
127
128 cwd=$(readlink "/proc/${pid}/cwd")
129
130 is "$cwd" "$working_dir" "Working directory changed"
131
132 stop_lttng_relayd
133 rm -rf "$working_dir"
134 }
135
136 function test_relayd_background_no_working_dir()
137 {
138 local expected_working_dir="/"
139 local cwd
140 local pid
141
142 diag "Test lttng-relayd background working directory"
143
144 start_lttng_relayd_opt 1 "-b" ""
145
146 pid=$(pgrep "$RELAYD_MATCH")
147 ok $? "Found lttng-relayd"
148
149 cwd=$(readlink "/proc/${pid}/cwd")
150
151 is "$cwd" "$expected_working_dir" "Working directory is $expected_working_dir"
152
153 stop_lttng_relayd
154 rm -rf "$working_dir"
155 }
156
157 function test_relayd_debug_permission()
158 {
159 local is_user
160
161 diag "Test lttng-relayd change working directory on non writable directory"
162
163 if [ "$(id -u)" == "0" ]; then
164 is_user=0
165 else
166 is_user=1
167 fi
168
169 skip $is_user "Skipping permission debug output test; operation can't fail as root" 6 ||
170 {
171 local output_pattern='Working directory \".*\" is not writable'
172 local working_dir
173 local cwd
174 local pid
175
176 working_dir=$(realpath "$(mktemp -d)")
177
178 # Removing write access to working dir
179 okx chmod -w "$working_dir"
180
181 # Redirect the error output to a temporary file
182
183 ERROR_OUTPUT_DEST=$(mktemp) start_lttng_relayd_opt 1 "-b" "-v --working-dir $working_dir"
184
185 pid=$(pgrep "$RELAYD_MATCH")
186 ok $? "Found lttng-relayd"
187
188 cwd=$(readlink "/proc/${pid}/cwd")
189 is "$cwd" "$working_dir" "Working directory changed"
190
191 grep -q "$output_pattern" "$ERROR_OUTPUT_DEST"
192 ok $? "Warning about missing write permission is present"
193
194 stop_lttng_relayd
195 rm "$ERROR_OUTPUT_DEST"
196 rm -rf "$working_dir" "$ERROR_OUTPUT_DEST"
197 ERROR_OUTPUT_DEST=/dev/null
198 }
199 }
200
201 function test_relayd_failure()
202 {
203 local output_pattern='Failed to change working directory to'
204 local relayd_bin_path="$DIR/../src/bin/lttng-relayd/$RELAYD_BIN"
205
206 local working_dir
207 local working_dir_imaginary
208 local output_dest
209 local pid
210
211 working_dir=$(realpath "$(mktemp -d)")
212 working_dir_imaginary="${working_dir}/imaginary_directory"
213 output_dest=$(mktemp)
214
215 diag "Test lttng-relayd normal mode change non-existing directory"
216
217 $relayd_bin_path -b --working-directory "$working_dir_imaginary" > "$output_dest" 2>&1
218 test $? -eq "1"
219 ok $? "Expect failure to start lttng-relayd for non-existent working directory"
220
221 pid=$(pgrep "$RELAYD_MATCH")
222 if [ -z "$pid" ]; then
223 pass "No lttng-relayd present"
224 else
225 fail "No lttng-relayd present"
226 stop_lttng_relayd_notap
227 fi
228
229 grep -q "$output_pattern" "$output_dest"
230 ok $? "Found error message: invalid directory"
231
232 rm "$output_dest"
233 rm -rf "$working_dir"
234 }
235
236 function test_relayd_env()
237 {
238 local working_dir
239 local cwd
240 local pid
241
242 working_dir=$(mktemp -d)
243 working_dir=$(realpath "$(mktemp -d)")
244
245 diag "Test lttng-relayd change working directory from env. variable"
246
247 export LTTNG_RELAYD_WORKING_DIRECTORY=${working_dir}
248 start_lttng_relayd_opt 1 "-b" ""
249
250 pid=$(pgrep "$RELAYD_MATCH")
251 ok $? "Found lttng-relayd"
252
253 cwd=$(readlink "/proc/$pid/cwd")
254
255 is "$cwd" "$working_dir" "Working directory changed"
256
257 stop_lttng_relayd
258 rm -rf "$working_dir"
259 unset LTTNG_RELAYD_WORKING_DIRECTORY
260 }
261
262 function test_relayd_cmdline_overwrite_env()
263 {
264 local working_dir_env
265 local working_dir_cmdline
266 local cwd
267 local pid
268
269 working_dir_env=$(realpath "$(mktemp -d)")
270 working_dir_cmdline=$(realpath "$(mktemp -d)")
271
272 diag "Test lttng-relayd change working directory command line overwrite env variable"
273
274 export LTTNG_RELAYD_WORKING_DIRECTORY=${working_dir_env}
275 start_lttng_relayd_opt 1 "-b" "--working-dir ${working_dir_cmdline}"
276
277 pid=$(pgrep "$RELAYD_MATCH")
278 ok $? "Found lttng-relayd"
279
280 cwd=$(readlink "/proc/$pid/cwd")
281
282 is "$cwd" "$working_dir_cmdline" "Working directory is the one from command line"
283
284 stop_lttng_relayd
285 rm -rf "$working_dir_env" "$working_dir_cmdline"
286 unset LTTNG_RELAYD_WORKING_DIRECTORY
287 }
288
289 TESTS=(
290 test_relayd
291 test_relayd_daemon
292 test_relayd_daemon_no_working_dir
293 test_relayd_background
294 test_relayd_background_no_working_dir
295 test_relayd_debug_permission
296 test_relayd_failure
297 test_relayd_env
298 test_relayd_cmdline_overwrite_env
299 )
300
301 for fct_test in "${TESTS[@]}";
302 do
303 if ! ${fct_test}; then
304 break;
305 fi
306 done
This page took 0.038502 seconds and 5 git commands to generate.