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