3 # SPDX-License-Identifier: GPL-2.0-only
5 # Copyright (C) 2019 Philippe Proulx <pproulx@efficios.com>
8 # This test validates that a `src.ctf.fs` component successfully reads
9 # specific CTF traces and creates the expected messages.
11 # Such CTF traces to open either exist (in `tests/ctf-traces/`)
12 # or are generated by this test using local trace generators.
16 if [ "x${BT_TESTS_SRCDIR:-}" != "x" ]; then
17 UTILSSH
="$BT_TESTS_SRCDIR/utils/utils.sh"
19 UTILSSH
="$(dirname "$0")/../../utils/utils.sh"
22 # shellcheck source=../../utils/utils.sh
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 -- -$$
34 # Ensure that background child jobs are killed on SIGINT/SIGTERM
35 trap cleanup SIGINT SIGTERM
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"
41 if [ "$BT_TESTS_OS_TYPE" = "mingw" ]; then
42 # Same as the above, but in Windows form (C:\foo\bar) instead of Unix form
44 trace_dir_native
=$
(cygpath
-w "${trace_dir}")
46 trace_dir_native
="${trace_dir}"
52 local retcode_file
="$3"
53 local server_args
="$4"
54 local server_script
="$test_data_dir/lttng_live_server.py"
57 echo "$server_args" |
xargs "$BT_TESTS_PYTHON_BIN" "$server_script" \
58 --port-file "$port_file" \
59 --trace-path-prefix "$trace_dir_native" &
64 # wait for server to exit
67 # write return code to file
68 echo $?
> "$retcode_file"
71 kill_lttng_live_server
() {
74 if [ ! -s "$pid_file" ]; then
78 kill -9 "$(cat "$pid_file")"
81 get_cli_output_with_lttng_live_server
() {
82 local cli_args_template
="$1"
83 local sessions_file
="$2"
84 local cli_stdout_file
="$3"
85 local cli_stderr_file
="$4"
93 local server_retcode_file
95 server_pid_file
="$(mktemp -t test_live_server_pid.XXXXXX)"
96 server_retcode_file
="$(mktemp -t test_live_server_ret.XXXXX)"
98 diag
"Starting LTTng live server mockup"
100 # This starts the server, which eventually writes its listening
101 # port number to the `$port_file` file. The lttng_live_server()
102 # function itself writes the server's PID to the
103 # `$server_pid_file` file. When the server exits,
104 # lttng_live_server() writes its return code to the
105 # `$server_retcode_file` file.
106 lttng_live_server
"$port_file" "$server_pid_file" \
107 "$server_retcode_file" "$sessions_file" &
111 while [ ! -s "$port_file" ]; do
114 # Timeout of 30 seconds
115 if [ "$i" -eq "300" ]; then
117 kill_lttng_live_server
"$server_pid_file"
119 rm -f "$server_pid_file"
120 rm -f "$server_retcode_file"
127 port
=$
(<"$port_file")
129 diag
"LTTng live port is $port"
131 cli_args
=${cli_args_template//@PORT@/$port}
133 # Split argument string by spaces into an array.
134 IFS
=' ' read -ra cli_args
<<< "$cli_args"
136 if ! bt_cli
"$cli_stdout_file" "$cli_stderr_file" "${cli_args[@]}"; then
137 # CLI failed: cancel everything else
138 kill_lttng_live_server
"$server_pid_file"
140 rm -f "$server_pid_file"
141 rm -f "$server_retcode_file"
145 # get server's return code
147 while [ ! -s "$server_retcode_file" ]; do
150 # Timeout of 30 seconds
151 if [ "$i" -eq "300" ]; then
153 kill_lttng_live_server
"$server_pid_file"
155 rm -f "$server_pid_file"
156 rm -f "$server_retcode_file"
165 ret
=$
(<"$server_retcode_file")
167 rm -f "$server_pid_file"
168 rm -f "$server_retcode_file"
174 local cli_args_template
="$2"
175 local server_args
="$3"
176 local expected_stdout
="$4"
177 local expected_stderr
="$5"
184 cli_stderr
="$(mktemp -t test_live_stderr.XXXXXX)"
185 cli_stdout
="$(mktemp -t test_live_stdout.XXXXXX)"
186 port_file
="$(mktemp -t test_live_server_port.XXXXXX)"
188 get_cli_output_with_lttng_live_server
"$cli_args_template" "$server_args" "$cli_stdout" "$cli_stderr" "$port_file"
189 port
=$
(<"$port_file")
191 bt_diff
"$expected_stdout" "$cli_stdout"
192 ok $?
"$test_text - stdout"
193 bt_diff
"$expected_stderr" "$cli_stderr"
194 ok $?
"$test_text - stderr"
201 test_list_sessions
() {
202 # Test the basic listing of sessions.
203 # Ensure that a multi-domain trace is seen as a single session.
204 # run_test() is not used here because the port is needed to craft the
209 local tmp_stdout_expected
210 local template_expected
212 local test_text
="CLI prints the expected session list"
213 local cli_args_template
="-i lttng-live net://localhost:@PORT@"
214 local sessions_file
="$test_data_dir/list_sessions.json"
215 local server_args
="--sessions-filename '$sessions_file'"
217 template_expected
=$
(<"$test_data_dir/cli-list-sessions.expect")
218 cli_stderr
="$(mktemp -t test_live_list_sessions_stderr.XXXXXX)"
219 cli_stdout
="$(mktemp -t test_live_list_sessions_stdout.XXXXXX)"
220 empty_file
="$(mktemp -t test_live_list_sessions_empty.XXXXXX)"
221 port_file
="$(mktemp -t test_live_list_sessions_server_port.XXXXXX)"
222 tmp_stdout_expected
="$(mktemp -t test_live_list_sessions_stdout_expected.XXXXXX)"
224 get_cli_output_with_lttng_live_server
"$cli_args_template" "$server_args" "$cli_stdout" "$cli_stderr" "$port_file"
225 port
=$
(<"$port_file")
227 # Craft the expected output. This is necessary since the port number
228 # (random) of a "relayd" is present in the output.
229 template_expected
=${template_expected//@PORT@/$port}
231 echo "$template_expected" > "$tmp_stdout_expected"
233 bt_diff
"$tmp_stdout_expected" "$cli_stdout"
234 ok $?
"$test_text - stdout"
235 bt_diff
"$empty_file" "$cli_stderr"
236 ok $?
"$test_text - stderr"
242 rm -f "$tmp_stdout_expected"
246 # Attach and consume data from a multi packets ust session with no
248 local test_text
="CLI attach and fetch from single-domains session - no discarded events"
249 local cli_args_template
="-i lttng-live net://localhost:@PORT@/host/hostname/trace-with-index -c sink.text.details"
250 local sessions_file
="$test_data_dir/base.json"
251 local server_args
="--sessions-filename '$sessions_file'"
252 local expected_stdout
="${test_data_dir}/cli-base.expect"
253 local expected_stderr
255 # Empty file for stderr expected
256 expected_stderr
="$(mktemp -t test_live_base_stderr_expected.XXXXXX)"
258 run_test
"$test_text" "$cli_args_template" "$server_args" "$expected_stdout" "$expected_stderr"
260 rm -f "$expected_stderr"
263 test_multi_domains
() {
264 # Attach and consume data from a multi-domains session with discarded
266 local test_text
="CLI attach and fetch from multi-domains session - discarded events"
267 local cli_args_template
="-i lttng-live net://localhost:@PORT@/host/hostname/multi-domains -c sink.text.details"
268 local sessions_file
="${test_data_dir}/multi_domains.json"
269 local server_args
="--sessions-filename '$sessions_file'"
270 local expected_stdout
="$test_data_dir/cli-multi-domains.expect"
271 local expected_stderr
273 # Empty file for stderr expected
274 expected_stderr
="$(mktemp -t test_live_multi_domains_stderr_expected.XXXXXX)"
276 run_test
"$test_text" "$cli_args_template" "$server_args" "$expected_stdout" "$expected_stderr"
278 rm -f "$expected_stderr"
281 test_rate_limited
() {
282 # Attach and consume data from a multi packets ust session with no
283 # discarded events. Enforce a server side limit on the stream data
284 # requests size. Ensure that babeltrace respect the returned size and that
285 # many requests per packet works as expected.
286 # The packet size of the test trace is 4k. Limit requests to 1k.
287 local test_text
="CLI many requests per packet"
288 local cli_args_template
="-i lttng-live net://localhost:@PORT@/host/hostname/trace-with-index -c sink.text.details"
289 local sessions_file
="$test_data_dir/rate_limited.json"
290 local server_args
="--max-query-data-response-size 1024 --sessions-filename '$sessions_file'"
291 local expected_stdout
="${test_data_dir}/cli-base.expect"
292 local expected_stderr
294 # Empty file for stderr expected
295 expected_stderr
="$(mktemp -t test_live_rate_limited_stderr_expected.XXXXXX)"
297 run_test
"$test_text" "$cli_args_template" "$server_args" "$expected_stdout" "$expected_stderr"
299 rm -f "$expected_stderr"
302 test_compare_to_ctf_fs
() {
303 # Compare the details text sink or ctf.fs and ctf.lttng-live to ensure
304 # that the trace is parsed the same way.
305 # Do the same with the session swapped on the relayd side. This validate
306 # that ordering is consistent between live and ctf fs.
307 local test_text
="CLI src.ctf.fs vs src.ctf.lttng-live"
308 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"
309 local sessions_file
="$test_data_dir/multi_domains.json"
310 local sessions_file_inverse
="$test_data_dir/multi_domains_inverse.json"
311 local server_args
="--sessions-filename '$sessions_file'"
312 local server_args_inverse
="--sessions-filename '$sessions_file_inverse'"
313 local expected_stdout
314 local expected_stderr
316 expected_stdout
="$(mktemp -t test_live_compare_stdout_expected.XXXXXX)"
317 expected_stderr
="$(mktemp -t test_live_compare_stderr_expected.XXXXXX)"
319 bt_cli
"$expected_stdout" "$expected_stderr" "${trace_dir}/succeed/multi-domains" -c sink.text.details
--params "with-trace-name=false,with-stream-name=false"
320 bt_remove_cr
"${expected_stdout}"
321 bt_remove_cr
"${expected_stderr}"
322 run_test
"$test_text" "$cli_args_template" "$server_args" "$expected_stdout" "$expected_stderr"
323 diag
"Inverse session order from lttng-relayd"
324 run_test
"$test_text" "$cli_args_template" "$server_args_inverse" "$expected_stdout" "$expected_stderr"
326 rm -f "$expected_stdout"
327 rm -f "$expected_stderr"
330 test_inactivity_discarded_packet
() {
331 # Attach and consume data from a multi-packet trace with discarded
332 # packets and emit an inactivity beacon during the discarded packets
335 # | pkt seq:0 |<-------discarded packets------>| pkt seq:8 |
338 # This test was introduced to cover the following bug:
340 # When reading this type of trace locally, the CTF source is expected
341 # to introduce a "Discarded packets" message between packets 0 and 8.
342 # The timestamps of this message are [pkt0.end_ts, pkt8.begin_ts].
344 # In the context of a live source, the tracer could report an inactivity
345 # period during the interval of the "Discarded packets" message.
346 # Those messages eventually translate into a
347 # "Iterator inactivity" message with a timestamp set at the end of the
350 # If the tracer reports an inactivity period that ends at a point
351 # between pkt0 and pkt7 (resulting in an "Iterator inactivity" message),
352 # the live source could send a "Discarded packets" message that starts
353 # before the preceding "Iterator inactivity" message. This would break
354 # the monotonicity constraint of the graph.
355 local test_text
="CLI attach and fetch from single-domains session - inactivity discarded packet"
356 local cli_args_template
="-i lttng-live net://localhost:@PORT@/host/hostname/7_lost_between_2_with_index -c sink.text.details"
357 local sessions_file
="$test_data_dir/inactivity_discarded_packet.json"
358 local server_args
="--sessions-filename '$sessions_file'"
359 local expected_stdout
="$test_data_dir/inactivity_discarded_packet.expect"
360 local expected_stderr
362 # Empty file for stderr expected
363 expected_stderr
="$(mktemp -t test_live_inactivity_discarded_packet_stderr_expected.XXXXXX)"
365 run_test
"$test_text" "$cli_args_template" "$server_args" "$expected_stdout" "$expected_stderr"
367 rm -f "$expected_stderr"
370 test_split_metadata
() {
371 # Consume a metadata stream sent in two parts. This testcase tests the
372 # behaviour of Babeltrace when the tracing session was cleared (lttng
373 # clear) but the metadata is not yet available to the relay. In such
374 # cases, when asked for metadata, the relay will return the
375 # `LTTNG_VIEWER_METADATA_OK` status and a data length of 0. The viewer
376 # need to consider such case as a request to retry fetching metadata.
378 # This testcase emulates such behaviour by adding empty metadata
381 local test_text
="CLI attach and fetch from single-domain session - Receive metadata in two sections separated by a empty section"
382 local cli_args_template
="-i lttng-live net://localhost:@PORT@/host/hostname/split_metadata -c sink.text.details"
383 local sessions_file
="$test_data_dir/split_metadata.json"
384 local server_args
="--sessions-filename '$sessions_file'"
385 local expected_stdout
="${test_data_dir}/split_metadata.expect"
386 local expected_stderr
388 # Empty file for stderr expected
389 expected_stderr
="$(mktemp -t test_live_split_metadata_stderr_expected.XXXXXX)"
391 run_test
"$test_text" "$cli_args_template" "$server_args" "$expected_stdout" "$expected_stderr"
393 rm -f "$expected_stderr"
402 test_compare_to_ctf_fs
403 test_inactivity_discarded_packet