tests: pass trace path prefix down in test_live
[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.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.
12 #
13 # A mock LTTng live server is used to feed data to the component.
14
15 SH_TAP=1
16
17 if [ -n "${BT_TESTS_SRCDIR:-}" ]; then
18 UTILSSH="$BT_TESTS_SRCDIR/utils/utils.sh"
19 else
20 UTILSSH="$(dirname "$0")/../../utils/utils.sh"
21 fi
22
23 # shellcheck source=../../utils/utils.sh
24 source "$UTILSSH"
25
26 function 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
36 trap cleanup SIGINT SIGTERM
37
38 this_dir_relative="plugins/src.ctf.lttng-live"
39 test_data_dir="$BT_TESTS_DATADIR/$this_dir_relative"
40 trace_dir="$BT_CTF_TRACES_PATH"
41
42 if [ "$BT_TESTS_OS_TYPE" = "mingw" ]; then
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}")
46 else
47 trace_dir_native="${trace_dir}"
48 fi
49
50 lttng_live_server() {
51 local pid_file="$1"
52 local retcode_file="$2"
53 shift 2
54 local server_args=("$@")
55
56 local server_script="$test_data_dir/lttng_live_server.py"
57
58 # start server
59 diag "$BT_TESTS_PYTHON_BIN $server_script ${server_args[*]}"
60 run_python "$BT_TESTS_PYTHON_BIN" "$server_script" "${server_args[@]}" 1>&2 &
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 cli_stdout_file="$2"
85 local cli_stderr_file="$3"
86 local port_file="$4"
87 local trace_path_prefix="$5"
88 shift 5
89 local server_args=("$@")
90
91 local i
92 local ret
93 local port
94 local cli_args
95 local server_pid_file
96 local server_retcode_file
97
98 server_args+=(--port-file "$port_file" --trace-path-prefix "$trace_path_prefix")
99 server_pid_file="$(mktemp -t test_live_server_pid.XXXXXX)"
100 server_retcode_file="$(mktemp -t test_live_server_ret.XXXXX)"
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.
110 lttng_live_server "$server_pid_file" "$server_retcode_file" "${server_args[@]}" &
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
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
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
175 run_test() {
176 local test_text="$1"
177 local cli_args_template="$2"
178 local expected_stdout="$3"
179 local expected_stderr="$4"
180 local trace_path_prefix="$5"
181 shift 5
182 local server_args=("$@")
183
184 local cli_stderr
185 local cli_stdout
186 local port_file
187 local port
188
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)"
192
193 get_cli_output_with_lttng_live_server "$cli_args_template" "$cli_stdout" \
194 "$cli_stderr" "$port_file" "$trace_path_prefix" "${server_args[@]}"
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
207 test_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@"
220 local server_args=("$test_data_dir/list_sessions.json")
221
222 template_expected=$(<"$test_data_dir/cli-list-sessions.expect")
223 cli_stderr="$(mktemp -t test_live_list_sessions_stderr.XXXXXX)"
224 cli_stdout="$(mktemp -t test_live_list_sessions_stdout.XXXXXX)"
225 empty_file="$(mktemp -t test_live_list_sessions_empty.XXXXXX)"
226 port_file="$(mktemp -t test_live_list_sessions_server_port.XXXXXX)"
227 tmp_stdout_expected="$(mktemp -t test_live_list_sessions_stdout_expected.XXXXXX)"
228
229 get_cli_output_with_lttng_live_server "$cli_args_template" "$cli_stdout" \
230 "$cli_stderr" "$port_file" "$trace_dir_native" "${server_args[@]}"
231 port=$(<"$port_file")
232
233 # Craft the expected output. This is necessary since the port number
234 # (random) of a "relayd" is present in the output.
235 template_expected=${template_expected//@PORT@/$port}
236
237 echo "$template_expected" > "$tmp_stdout_expected"
238
239 bt_diff "$tmp_stdout_expected" "$cli_stdout"
240 ok $? "$test_text - stdout"
241 bt_diff "$empty_file" "$cli_stderr"
242 ok $? "$test_text - stderr"
243
244 rm -f "$cli_stderr"
245 rm -f "$cli_stdout"
246 rm -f "$empty_file"
247 rm -f "$port_file"
248 rm -f "$tmp_stdout_expected"
249 }
250
251 test_base() {
252 # Attach and consume data from a multi packets ust session with no
253 # discarded events.
254 local test_text="CLI attach and fetch from single-domains session - no discarded events"
255 local cli_args_template="-i lttng-live net://localhost:@PORT@/host/hostname/trace-with-index -c sink.text.details"
256 local server_args=("$test_data_dir/base.json")
257 local expected_stdout="${test_data_dir}/cli-base.expect"
258 local expected_stderr
259
260 # Empty file for stderr expected
261 expected_stderr="$(mktemp -t test_live_base_stderr_expected.XXXXXX)"
262
263 run_test "$test_text" "$cli_args_template" "$expected_stdout" \
264 "$expected_stderr" "$trace_dir_native" "${server_args[@]}"
265
266 rm -f "$expected_stderr"
267 }
268
269 test_multi_domains() {
270 # Attach and consume data from a multi-domains session with discarded
271 # events.
272 local test_text="CLI attach and fetch from multi-domains session - discarded events"
273 local cli_args_template="-i lttng-live net://localhost:@PORT@/host/hostname/multi-domains -c sink.text.details"
274 local server_args=("${test_data_dir}/multi_domains.json")
275 local expected_stdout="$test_data_dir/cli-multi-domains.expect"
276 local expected_stderr
277
278 # Empty file for stderr expected
279 expected_stderr="$(mktemp -t test_live_multi_domains_stderr_expected.XXXXXX)"
280
281 run_test "$test_text" "$cli_args_template" "$expected_stdout" \
282 "$expected_stderr" "$trace_dir_native" "${server_args[@]}"
283
284 rm -f "$expected_stderr"
285 }
286
287 test_rate_limited() {
288 # Attach and consume data from a multi packets ust session with no
289 # discarded events. Enforce a server side limit on the stream data
290 # requests size. Ensure that babeltrace respect the returned size and that
291 # many requests per packet works as expected.
292 # The packet size of the test trace is 4k. Limit requests to 1k.
293 local test_text="CLI many requests per packet"
294 local cli_args_template="-i lttng-live net://localhost:@PORT@/host/hostname/trace-with-index -c sink.text.details"
295 local server_args=(--max-query-data-response-size 1024 "$test_data_dir/rate_limited.json")
296 local expected_stdout="${test_data_dir}/cli-base.expect"
297 local expected_stderr
298
299 # Empty file for stderr expected
300 expected_stderr="$(mktemp -t test_live_rate_limited_stderr_expected.XXXXXX)"
301
302 run_test "$test_text" "$cli_args_template" "$expected_stdout" \
303 "$expected_stderr" "$trace_dir_native" "${server_args[@]}"
304
305 rm -f "$expected_stderr"
306 }
307
308 test_compare_to_ctf_fs() {
309 # Compare the details text sink or ctf.fs and ctf.lttng-live to ensure
310 # that the trace is parsed the same way.
311 # Do the same with the session swapped on the relayd side. This validate
312 # that ordering is consistent between live and ctf fs.
313 local test_text="CLI src.ctf.fs vs src.ctf.lttng-live"
314 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"
315 local server_args=("$test_data_dir/multi_domains.json")
316 local server_args_inverse=("$test_data_dir/multi_domains_inverse.json")
317 local expected_stdout
318 local expected_stderr
319
320 expected_stdout="$(mktemp -t test_live_compare_stdout_expected.XXXXXX)"
321 expected_stderr="$(mktemp -t test_live_compare_stderr_expected.XXXXXX)"
322
323 bt_cli "$expected_stdout" "$expected_stderr" "${trace_dir}/succeed/multi-domains" -c sink.text.details --params "with-trace-name=false,with-stream-name=false"
324 bt_remove_cr "${expected_stdout}"
325 bt_remove_cr "${expected_stderr}"
326 run_test "$test_text" "$cli_args_template" "$expected_stdout" \
327 "$expected_stderr" "$trace_dir_native" "${server_args[@]}"
328 diag "Inverse session order from lttng-relayd"
329 run_test "$test_text" "$cli_args_template" "$expected_stdout" \
330 "$expected_stderr" "$trace_dir_native" "${server_args_inverse[@]}"
331
332 rm -f "$expected_stdout"
333 rm -f "$expected_stderr"
334 }
335
336 test_inactivity_discarded_packet() {
337 # Attach and consume data from a multi-packet trace with discarded
338 # packets and emit an inactivity beacon during the discarded packets
339 # period.
340 #
341 # | pkt seq:0 |<-------discarded packets------>| pkt seq:8 |
342 # 0 20 121 140
343 #
344 # This test was introduced to cover the following bug:
345 #
346 # When reading this type of trace locally, the CTF source is expected
347 # to introduce a "Discarded packets" message between packets 0 and 8.
348 # The timestamps of this message are [pkt0.end_ts, pkt8.begin_ts].
349 #
350 # In the context of a live source, the tracer could report an inactivity
351 # period during the interval of the "Discarded packets" message.
352 # Those messages eventually translate into a
353 # "Iterator inactivity" message with a timestamp set at the end of the
354 # inactivity period.
355 #
356 # If the tracer reports an inactivity period that ends at a point
357 # between pkt0 and pkt7 (resulting in an "Iterator inactivity" message),
358 # the live source could send a "Discarded packets" message that starts
359 # before the preceding "Iterator inactivity" message. This would break
360 # the monotonicity constraint of the graph.
361 local test_text="CLI attach and fetch from single-domains session - inactivity discarded packet"
362 local cli_args_template="-i lttng-live net://localhost:@PORT@/host/hostname/7_lost_between_2_with_index -c sink.text.details"
363 local server_args=("$test_data_dir/inactivity_discarded_packet.json")
364 local expected_stdout="$test_data_dir/inactivity_discarded_packet.expect"
365 local expected_stderr
366
367 # Empty file for stderr expected
368 expected_stderr="$(mktemp -t test_live_inactivity_discarded_packet_stderr_expected.XXXXXX)"
369
370 run_test "$test_text" "$cli_args_template" "$expected_stdout" \
371 "$expected_stderr" "$trace_dir_native" "${server_args[@]}"
372
373 rm -f "$expected_stderr"
374 }
375
376 test_split_metadata() {
377 # Consume a metadata stream sent in two parts. This testcase tests the
378 # behaviour of Babeltrace when the tracing session was cleared (lttng
379 # clear) but the metadata is not yet available to the relay. In such
380 # cases, when asked for metadata, the relay will return the
381 # `LTTNG_VIEWER_METADATA_OK` status and a data length of 0. The viewer
382 # need to consider such case as a request to retry fetching metadata.
383 #
384 # This testcase emulates such behaviour by adding empty metadata
385 # packets.
386
387 local test_text="CLI attach and fetch from single-domain session - Receive metadata in two sections separated by a empty section"
388 local cli_args_template="-i lttng-live net://localhost:@PORT@/host/hostname/split_metadata -c sink.text.details"
389 local server_args=("$test_data_dir/split_metadata.json")
390 local expected_stdout="${test_data_dir}/split_metadata.expect"
391 local expected_stderr
392
393 # Empty file for stderr expected
394 expected_stderr="$(mktemp -t test_live_split_metadata_stderr_expected.XXXXXX)"
395
396 run_test "$test_text" "$cli_args_template" "$expected_stdout" \
397 "$expected_stderr" "$trace_dir_native" "${server_args[@]}"
398
399 rm -f "$expected_stderr"
400 }
401
402 plan_tests 16
403
404 test_list_sessions
405 test_base
406 test_multi_domains
407 test_rate_limited
408 test_compare_to_ctf_fs
409 test_inactivity_discarded_packet
410 test_split_metadata
This page took 0.040156 seconds and 5 git commands to generate.