tests: fix shellcheck warnings in plugins/sink.ctf.fs
[babeltrace.git] / tests / plugins / src.ctf.lttng-live / test_live
1 #!/bin/bash
2 #
3 # SPDX-License-Identifier: GPL-2.0-only
4 #
5 # Copyright (C) 2019 Philippe Proulx <pproulx@efficios.com>
6 #
7
8 # This test validates that a `src.ctf.fs` component successfully reads
9 # specific CTF traces and creates the expected messages.
10 #
11 # Such CTF traces to open either exist (in `tests/ctf-traces/`)
12 # or are generated by this test using local trace generators.
13
14 SH_TAP=1
15
16 if [ -n "${BT_TESTS_SRCDIR:-}" ]; then
17 UTILSSH="$BT_TESTS_SRCDIR/utils/utils.sh"
18 else
19 UTILSSH="$(dirname "$0")/../../utils/utils.sh"
20 fi
21
22 # shellcheck source=../../utils/utils.sh
23 source "$UTILSSH"
24
25 function cleanup ()
26 {
27 # Disable trap for SIGTERM since the following kill to the
28 # pidgroup will be SIGTERM. Otherwise it loops.
29 # The '-' before the pid number ($$) indicates 'kill' to signal the
30 # whole process group.
31 trap - SIGTERM && kill -- -$$
32 }
33
34 # Ensure that background child jobs are killed on SIGINT/SIGTERM
35 trap cleanup SIGINT SIGTERM
36
37 this_dir_relative="plugins/src.ctf.lttng-live"
38 test_data_dir="$BT_TESTS_DATADIR/$this_dir_relative"
39 trace_dir="$BT_CTF_TRACES_PATH"
40
41 if [ "$BT_TESTS_OS_TYPE" = "mingw" ]; then
42 # Same as the above, but in Windows form (C:\foo\bar) instead of Unix form
43 # (/c/foo/bar).
44 trace_dir_native=$(cygpath -w "${trace_dir}")
45 else
46 trace_dir_native="${trace_dir}"
47 fi
48
49 lttng_live_server() {
50 local port_file="$1"
51 local pid_file="$2"
52 local retcode_file="$3"
53 local server_args="$4"
54 local server_script="$test_data_dir/lttng_live_server.py"
55
56 # start server
57 diag "$BT_TESTS_PYTHON_BIN $server_script --port-file $port_file --trace-path-prefix $trace_dir_native $server_args"
58 echo "$server_args" | xargs "$BT_TESTS_PYTHON_BIN" "$server_script" \
59 --port-file "$port_file" \
60 --trace-path-prefix "$trace_dir_native" &
61
62 # write PID to file
63 echo $! > "$pid_file"
64
65 # wait for server to exit
66 wait
67
68 # write return code to file
69 echo $? > "$retcode_file"
70 }
71
72 kill_lttng_live_server() {
73 local pid_file="$1"
74
75 if [ ! -s "$pid_file" ]; then
76 return
77 fi
78
79 kill -9 "$(cat "$pid_file")"
80 }
81
82 get_cli_output_with_lttng_live_server() {
83 local cli_args_template="$1"
84 local sessions_file="$2"
85 local cli_stdout_file="$3"
86 local cli_stderr_file="$4"
87 local port_file="$5"
88
89 local i
90 local ret
91 local port
92 local cli_args
93 local server_pid_file
94 local server_retcode_file
95
96 server_pid_file="$(mktemp -t test_live_server_pid.XXXXXX)"
97 server_retcode_file="$(mktemp -t test_live_server_ret.XXXXX)"
98
99 diag "Starting LTTng live server mockup"
100
101 # This starts the server, which eventually writes its listening
102 # port number to the `$port_file` file. The lttng_live_server()
103 # function itself writes the server's PID to the
104 # `$server_pid_file` file. When the server exits,
105 # lttng_live_server() writes its return code to the
106 # `$server_retcode_file` file.
107 lttng_live_server "$port_file" "$server_pid_file" \
108 "$server_retcode_file" "$sessions_file" &
109
110 # Get port number
111 i=0
112 while [ ! -s "$port_file" ]; do
113 sleep .1
114
115 # Timeout of 30 seconds
116 if [ "$i" -eq "300" ]; then
117 # too long, kill it
118 kill_lttng_live_server "$server_pid_file"
119 wait
120 rm -f "$server_pid_file"
121 rm -f "$server_retcode_file"
122 return 1
123 fi
124
125 i=$((i + 1))
126 done
127
128 port=$(<"$port_file")
129
130 diag "LTTng live port is $port"
131
132 cli_args=${cli_args_template//@PORT@/$port}
133
134 # Split argument string by spaces into an array.
135 IFS=' ' read -ra cli_args <<< "$cli_args"
136
137 if ! bt_cli "$cli_stdout_file" "$cli_stderr_file" "${cli_args[@]}"; then
138 # CLI failed: cancel everything else
139 kill_lttng_live_server "$server_pid_file"
140 wait
141 rm -f "$server_pid_file"
142 rm -f "$server_retcode_file"
143 return 1
144 fi
145
146 # get server's return code
147 i=0
148 while [ ! -s "$server_retcode_file" ]; do
149 sleep .1
150
151 # Timeout of 30 seconds
152 if [ "$i" -eq "300" ]; then
153 # too long, kill it
154 kill_lttng_live_server "$server_pid_file"
155 wait
156 rm -f "$server_pid_file"
157 rm -f "$server_retcode_file"
158 return 1
159 fi
160
161 i=$((i + 1))
162 done
163
164 wait
165
166 ret=$(<"$server_retcode_file")
167
168 rm -f "$server_pid_file"
169 rm -f "$server_retcode_file"
170 return "$ret"
171 }
172
173 run_test() {
174 local test_text="$1"
175 local cli_args_template="$2"
176 local server_args="$3"
177 local expected_stdout="$4"
178 local expected_stderr="$5"
179
180 local cli_stderr
181 local cli_stdout
182 local port_file
183 local port
184
185 cli_stderr="$(mktemp -t test_live_stderr.XXXXXX)"
186 cli_stdout="$(mktemp -t test_live_stdout.XXXXXX)"
187 port_file="$(mktemp -t test_live_server_port.XXXXXX)"
188
189 get_cli_output_with_lttng_live_server "$cli_args_template" "$server_args" "$cli_stdout" "$cli_stderr" "$port_file"
190 port=$(<"$port_file")
191
192 bt_diff "$expected_stdout" "$cli_stdout"
193 ok $? "$test_text - stdout"
194 bt_diff "$expected_stderr" "$cli_stderr"
195 ok $? "$test_text - stderr"
196
197 rm -f "$cli_stderr"
198 rm -f "$cli_stdout"
199 rm -f "$port_file"
200 }
201
202 test_list_sessions() {
203 # Test the basic listing of sessions.
204 # Ensure that a multi-domain trace is seen as a single session.
205 # run_test() is not used here because the port is needed to craft the
206 # expected output.
207
208 local port
209 local port_file
210 local tmp_stdout_expected
211 local template_expected
212
213 local test_text="CLI prints the expected session list"
214 local cli_args_template="-i lttng-live net://localhost:@PORT@"
215 local sessions_file="$test_data_dir/list_sessions.json"
216 local server_args="--sessions-filename '$sessions_file'"
217
218 template_expected=$(<"$test_data_dir/cli-list-sessions.expect")
219 cli_stderr="$(mktemp -t test_live_list_sessions_stderr.XXXXXX)"
220 cli_stdout="$(mktemp -t test_live_list_sessions_stdout.XXXXXX)"
221 empty_file="$(mktemp -t test_live_list_sessions_empty.XXXXXX)"
222 port_file="$(mktemp -t test_live_list_sessions_server_port.XXXXXX)"
223 tmp_stdout_expected="$(mktemp -t test_live_list_sessions_stdout_expected.XXXXXX)"
224
225 get_cli_output_with_lttng_live_server "$cli_args_template" "$server_args" "$cli_stdout" "$cli_stderr" "$port_file"
226 port=$(<"$port_file")
227
228 # Craft the expected output. This is necessary since the port number
229 # (random) of a "relayd" is present in the output.
230 template_expected=${template_expected//@PORT@/$port}
231
232 echo "$template_expected" > "$tmp_stdout_expected"
233
234 bt_diff "$tmp_stdout_expected" "$cli_stdout"
235 ok $? "$test_text - stdout"
236 bt_diff "$empty_file" "$cli_stderr"
237 ok $? "$test_text - stderr"
238
239 rm -f "$cli_stderr"
240 rm -f "$cli_stdout"
241 rm -f "$empty_file"
242 rm -f "$port_file"
243 rm -f "$tmp_stdout_expected"
244 }
245
246 test_base() {
247 # Attach and consume data from a multi packets ust session with no
248 # discarded events.
249 local test_text="CLI attach and fetch from single-domains session - no discarded events"
250 local cli_args_template="-i lttng-live net://localhost:@PORT@/host/hostname/trace-with-index -c sink.text.details"
251 local sessions_file="$test_data_dir/base.json"
252 local server_args="--sessions-filename '$sessions_file'"
253 local expected_stdout="${test_data_dir}/cli-base.expect"
254 local expected_stderr
255
256 # Empty file for stderr expected
257 expected_stderr="$(mktemp -t test_live_base_stderr_expected.XXXXXX)"
258
259 run_test "$test_text" "$cli_args_template" "$server_args" "$expected_stdout" "$expected_stderr"
260
261 rm -f "$expected_stderr"
262 }
263
264 test_multi_domains() {
265 # Attach and consume data from a multi-domains session with discarded
266 # events.
267 local test_text="CLI attach and fetch from multi-domains session - discarded events"
268 local cli_args_template="-i lttng-live net://localhost:@PORT@/host/hostname/multi-domains -c sink.text.details"
269 local sessions_file="${test_data_dir}/multi_domains.json"
270 local server_args="--sessions-filename '$sessions_file'"
271 local expected_stdout="$test_data_dir/cli-multi-domains.expect"
272 local expected_stderr
273
274 # Empty file for stderr expected
275 expected_stderr="$(mktemp -t test_live_multi_domains_stderr_expected.XXXXXX)"
276
277 run_test "$test_text" "$cli_args_template" "$server_args" "$expected_stdout" "$expected_stderr"
278
279 rm -f "$expected_stderr"
280 }
281
282 test_rate_limited() {
283 # Attach and consume data from a multi packets ust session with no
284 # discarded events. Enforce a server side limit on the stream data
285 # requests size. Ensure that babeltrace respect the returned size and that
286 # many requests per packet works as expected.
287 # The packet size of the test trace is 4k. Limit requests to 1k.
288 local test_text="CLI many requests per packet"
289 local cli_args_template="-i lttng-live net://localhost:@PORT@/host/hostname/trace-with-index -c sink.text.details"
290 local sessions_file="$test_data_dir/rate_limited.json"
291 local server_args="--max-query-data-response-size 1024 --sessions-filename '$sessions_file'"
292 local expected_stdout="${test_data_dir}/cli-base.expect"
293 local expected_stderr
294
295 # Empty file for stderr expected
296 expected_stderr="$(mktemp -t test_live_rate_limited_stderr_expected.XXXXXX)"
297
298 run_test "$test_text" "$cli_args_template" "$server_args" "$expected_stdout" "$expected_stderr"
299
300 rm -f "$expected_stderr"
301 }
302
303 test_compare_to_ctf_fs() {
304 # Compare the details text sink or ctf.fs and ctf.lttng-live to ensure
305 # that the trace is parsed the same way.
306 # Do the same with the session swapped on the relayd side. This validate
307 # that ordering is consistent between live and ctf fs.
308 local test_text="CLI src.ctf.fs vs src.ctf.lttng-live"
309 local cli_args_template="-i lttng-live net://localhost:@PORT@/host/hostname/multi-domains -c sink.text.details --params with-trace-name=false,with-stream-name=false"
310 local sessions_file="$test_data_dir/multi_domains.json"
311 local sessions_file_inverse="$test_data_dir/multi_domains_inverse.json"
312 local server_args="--sessions-filename '$sessions_file'"
313 local server_args_inverse="--sessions-filename '$sessions_file_inverse'"
314 local expected_stdout
315 local expected_stderr
316
317 expected_stdout="$(mktemp -t test_live_compare_stdout_expected.XXXXXX)"
318 expected_stderr="$(mktemp -t test_live_compare_stderr_expected.XXXXXX)"
319
320 bt_cli "$expected_stdout" "$expected_stderr" "${trace_dir}/succeed/multi-domains" -c sink.text.details --params "with-trace-name=false,with-stream-name=false"
321 bt_remove_cr "${expected_stdout}"
322 bt_remove_cr "${expected_stderr}"
323 run_test "$test_text" "$cli_args_template" "$server_args" "$expected_stdout" "$expected_stderr"
324 diag "Inverse session order from lttng-relayd"
325 run_test "$test_text" "$cli_args_template" "$server_args_inverse" "$expected_stdout" "$expected_stderr"
326
327 rm -f "$expected_stdout"
328 rm -f "$expected_stderr"
329 }
330
331 test_inactivity_discarded_packet() {
332 # Attach and consume data from a multi-packet trace with discarded
333 # packets and emit an inactivity beacon during the discarded packets
334 # period.
335 #
336 # | pkt seq:0 |<-------discarded packets------>| pkt seq:8 |
337 # 0 20 121 140
338 #
339 # This test was introduced to cover the following bug:
340 #
341 # When reading this type of trace locally, the CTF source is expected
342 # to introduce a "Discarded packets" message between packets 0 and 8.
343 # The timestamps of this message are [pkt0.end_ts, pkt8.begin_ts].
344 #
345 # In the context of a live source, the tracer could report an inactivity
346 # period during the interval of the "Discarded packets" message.
347 # Those messages eventually translate into a
348 # "Iterator inactivity" message with a timestamp set at the end of the
349 # inactivity period.
350 #
351 # If the tracer reports an inactivity period that ends at a point
352 # between pkt0 and pkt7 (resulting in an "Iterator inactivity" message),
353 # the live source could send a "Discarded packets" message that starts
354 # before the preceding "Iterator inactivity" message. This would break
355 # the monotonicity constraint of the graph.
356 local test_text="CLI attach and fetch from single-domains session - inactivity discarded packet"
357 local cli_args_template="-i lttng-live net://localhost:@PORT@/host/hostname/7_lost_between_2_with_index -c sink.text.details"
358 local sessions_file="$test_data_dir/inactivity_discarded_packet.json"
359 local server_args="--sessions-filename '$sessions_file'"
360 local expected_stdout="$test_data_dir/inactivity_discarded_packet.expect"
361 local expected_stderr
362
363 # Empty file for stderr expected
364 expected_stderr="$(mktemp -t test_live_inactivity_discarded_packet_stderr_expected.XXXXXX)"
365
366 run_test "$test_text" "$cli_args_template" "$server_args" "$expected_stdout" "$expected_stderr"
367
368 rm -f "$expected_stderr"
369 }
370
371 test_split_metadata() {
372 # Consume a metadata stream sent in two parts. This testcase tests the
373 # behaviour of Babeltrace when the tracing session was cleared (lttng
374 # clear) but the metadata is not yet available to the relay. In such
375 # cases, when asked for metadata, the relay will return the
376 # `LTTNG_VIEWER_METADATA_OK` status and a data length of 0. The viewer
377 # need to consider such case as a request to retry fetching metadata.
378 #
379 # This testcase emulates such behaviour by adding empty metadata
380 # packets.
381
382 local test_text="CLI attach and fetch from single-domain session - Receive metadata in two sections separated by a empty section"
383 local cli_args_template="-i lttng-live net://localhost:@PORT@/host/hostname/split_metadata -c sink.text.details"
384 local sessions_file="$test_data_dir/split_metadata.json"
385 local server_args="--sessions-filename '$sessions_file'"
386 local expected_stdout="${test_data_dir}/split_metadata.expect"
387 local expected_stderr
388
389 # Empty file for stderr expected
390 expected_stderr="$(mktemp -t test_live_split_metadata_stderr_expected.XXXXXX)"
391
392 run_test "$test_text" "$cli_args_template" "$server_args" "$expected_stdout" "$expected_stderr"
393
394 rm -f "$expected_stderr"
395 }
396
397 plan_tests 16
398
399 test_list_sessions
400 test_base
401 test_multi_domains
402 test_rate_limited
403 test_compare_to_ctf_fs
404 test_inactivity_discarded_packet
405 test_split_metadata
This page took 0.039388 seconds and 4 git commands to generate.