Tests: fix: test_relayd_working_directory fails as root
[lttng-tools.git] / tests / regression / tools / working-directory / test_relayd_working_directory
CommitLineData
f3630ec4
JR
1#!/bin/bash
2#
9d16b343 3# Copyright (C) 2018 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
f3630ec4 4#
9d16b343 5# SPDX-License-Identifier: LGPL-2.1-only
f3630ec4
JR
6
7TEST_DESC="Change working directory of process"
8
9CURDIR=$(dirname "$0")/
10TESTDIR=$CURDIR/../../../
11
12DIR=$(readlink -f "$TESTDIR")
13
2a10de3b 14NUM_TESTS=35
f3630ec4
JR
15
16source $TESTDIR/utils/utils.sh
17
f3630ec4
JR
18#MUST set TESTDIR before calling those functions
19plan_tests $NUM_TESTS
20
21print_test_banner "$TEST_DESC"
22function 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
b2f4c4fa 29 working_dir=$(realpath "$(mktemp -d)")
f3630ec4
JR
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
58function test_relayd_daemon()
59{
60 local working_dir
61 local cwd
62 local pid
63
b2f4c4fa 64 working_dir=$(realpath "$(mktemp -d)")
f3630ec4
JR
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
81function 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
102function test_relayd_background()
103{
104 local working_dir
105 local cwd
106 local pid
107
b2f4c4fa 108 working_dir=$(realpath "$(mktemp -d)")
f3630ec4
JR
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
125function 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
146function test_relayd_debug_permission()
147{
15da468c 148 local is_user
f3630ec4
JR
149
150 diag "Test lttng-relayd change working directory on non writable directory"
151
15da468c
JG
152 if [ "$(id -u)" == "0" ]; then
153 is_user=0
154 else
155 is_user=1
156 fi
f3630ec4 157
15da468c
JG
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
f3630ec4 164
15da468c 165 working_dir=$(realpath "$(mktemp -d)")
f3630ec4 166
15da468c
JG
167 # Removing write access to working dir
168 okx chmod -w "$working_dir"
f3630ec4 169
15da468c 170 # Redirect the error output to a temporary file
f3630ec4 171
15da468c
JG
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 }
f3630ec4
JR
188}
189
190function 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
b2f4c4fa 200 working_dir=$(realpath "$(mktemp -d)")
f3630ec4
JR
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
2a10de3b
JR
225function test_relayd_env()
226{
227 local working_dir
228 local cwd
229 local pid
230
231 working_dir=$(mktemp -d)
b2f4c4fa 232 working_dir=$(realpath "$(mktemp -d)")
2a10de3b
JR
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
251function test_relayd_cmdline_overwrite_env()
252{
253 local working_dir_env
254 local working_dir_cmdline
255 local cwd
256 local pid
257
b2f4c4fa
JR
258 working_dir_env=$(realpath "$(mktemp -d)")
259 working_dir_cmdline=$(realpath "$(mktemp -d)")
2a10de3b
JR
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
f3630ec4
JR
278TESTS=(
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
2a10de3b
JR
286 test_relayd_env
287 test_relayd_cmdline_overwrite_env
f3630ec4
JR
288)
289
290for fct_test in "${TESTS[@]}";
291do
292 if ! ${fct_test}; then
293 break;
294 fi
295done
This page took 0.038656 seconds and 5 git commands to generate.