Tests: fix: tmp dir can be a symlink
[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 output_pattern='Working directory \".*\" is not writable'
160 local working_dir
161 local cwd
162 local pid
163
164 working_dir=$(realpath "$(mktemp -d)")
165
166 diag "Test lttng-relayd change working directory on non writable directory"
167
168 # Removing write access to working dir
169 okx chmod -w "$working_dir"
170
171 # Redirect the error output to a temporary file
172 ERROR_OUTPUT_DEST=$(mktemp)
173 start_lttng_relayd_opt 1 "-b" "-v --working-dir $working_dir"
174
175 pid=$(pgrep "$RELAYD_MATCH")
176 ok $? "Found lttng-relayd"
177
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"
183
184 stop_lttng_relayd
185 rm "$ERROR_OUTPUT_DEST"
186 rm -rf "$working_dir" "$ERROR_OUTPUT_DEST"
187 ERROR_OUTPUT_DEST=/dev/null
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.036323 seconds and 5 git commands to generate.