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