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