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