Commit | Line | Data |
---|---|---|
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 | |
15 | SH_TAP=1 | |
16 | ||
75e396f6 | 17 | if [ -n "${BT_TESTS_SRCDIR:-}" ]; then |
584af91e PP |
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" | |
2b763e29 | 40 | trace_dir="$BT_CTF_TRACES_PATH" |
584af91e | 41 | |
a0baab4a | 42 | if [ "$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}") | |
46 | else | |
47 | trace_dir_native="${trace_dir}" | |
48 | fi | |
49 | ||
584af91e | 50 | lttng_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 SM |
59 | diag "$BT_TESTS_PYTHON_BIN $server_script ${server_args[*]}" |
60 | run_python "$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 | ||
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" | |
286074cc SM |
84 | local cli_stdout_file="$2" |
85 | local cli_stderr_file="$3" | |
86 | local port_file="$4" | |
87 | shift 4 | |
88 | local server_args=("$@") | |
584af91e PP |
89 | |
90 | local i | |
91 | local ret | |
92 | local port | |
93 | local cli_args | |
94 | local server_pid_file | |
95 | local server_retcode_file | |
286074cc SM |
96 | |
97 | server_args+=(--port-file "$port_file" --trace-path-prefix "$trace_dir_native") | |
a01934b7 SM |
98 | server_pid_file="$(mktemp -t test_live_server_pid.XXXXXX)" |
99 | server_retcode_file="$(mktemp -t test_live_server_ret.XXXXX)" | |
584af91e PP |
100 | |
101 | diag "Starting LTTng live server mockup" | |
102 | ||
103 | # This starts the server, which eventually writes its listening | |
104 | # port number to the `$port_file` file. The lttng_live_server() | |
105 | # function itself writes the server's PID to the | |
106 | # `$server_pid_file` file. When the server exits, | |
107 | # lttng_live_server() writes its return code to the | |
108 | # `$server_retcode_file` file. | |
286074cc | 109 | lttng_live_server "$server_pid_file" "$server_retcode_file" "${server_args[@]}" & |
584af91e PP |
110 | |
111 | # Get port number | |
112 | i=0 | |
113 | while [ ! -s "$port_file" ]; do | |
114 | sleep .1 | |
115 | ||
116 | # Timeout of 30 seconds | |
117 | if [ "$i" -eq "300" ]; then | |
118 | # too long, kill it | |
119 | kill_lttng_live_server "$server_pid_file" | |
120 | wait | |
121 | rm -f "$server_pid_file" | |
122 | rm -f "$server_retcode_file" | |
123 | return 1 | |
124 | fi | |
125 | ||
126 | i=$((i + 1)) | |
127 | done | |
128 | ||
129 | port=$(<"$port_file") | |
130 | ||
131 | diag "LTTng live port is $port" | |
132 | ||
133 | cli_args=${cli_args_template//@PORT@/$port} | |
134 | ||
db4cb7f1 SM |
135 | # Split argument string by spaces into an array. |
136 | IFS=' ' read -ra cli_args <<< "$cli_args" | |
137 | ||
138 | if ! bt_cli "$cli_stdout_file" "$cli_stderr_file" "${cli_args[@]}"; then | |
584af91e PP |
139 | # CLI failed: cancel everything else |
140 | kill_lttng_live_server "$server_pid_file" | |
141 | wait | |
142 | rm -f "$server_pid_file" | |
143 | rm -f "$server_retcode_file" | |
144 | return 1 | |
145 | fi | |
146 | ||
147 | # get server's return code | |
148 | i=0 | |
149 | while [ ! -s "$server_retcode_file" ]; do | |
150 | sleep .1 | |
151 | ||
152 | # Timeout of 30 seconds | |
153 | if [ "$i" -eq "300" ]; then | |
154 | # too long, kill it | |
155 | kill_lttng_live_server "$server_pid_file" | |
156 | wait | |
157 | rm -f "$server_pid_file" | |
158 | rm -f "$server_retcode_file" | |
159 | return 1 | |
160 | fi | |
161 | ||
162 | i=$((i + 1)) | |
163 | done | |
164 | ||
165 | wait | |
166 | ||
167 | ret=$(<"$server_retcode_file") | |
168 | ||
169 | rm -f "$server_pid_file" | |
170 | rm -f "$server_retcode_file" | |
171 | return "$ret" | |
172 | } | |
173 | ||
174 | run_test() { | |
175 | local test_text="$1" | |
176 | local cli_args_template="$2" | |
286074cc SM |
177 | local expected_stdout="$3" |
178 | local expected_stderr="$4" | |
179 | shift 4 | |
180 | local server_args=("$@") | |
584af91e PP |
181 | |
182 | local cli_stderr | |
183 | local cli_stdout | |
184 | local port_file | |
4079467b | 185 | local port |
584af91e | 186 | |
a01934b7 SM |
187 | cli_stderr="$(mktemp -t test_live_stderr.XXXXXX)" |
188 | cli_stdout="$(mktemp -t test_live_stdout.XXXXXX)" | |
189 | port_file="$(mktemp -t test_live_server_port.XXXXXX)" | |
584af91e | 190 | |
286074cc SM |
191 | get_cli_output_with_lttng_live_server "$cli_args_template" "$cli_stdout" \ |
192 | "$cli_stderr" "$port_file" "${server_args[@]}" | |
584af91e PP |
193 | port=$(<"$port_file") |
194 | ||
195 | bt_diff "$expected_stdout" "$cli_stdout" | |
196 | ok $? "$test_text - stdout" | |
197 | bt_diff "$expected_stderr" "$cli_stderr" | |
198 | ok $? "$test_text - stderr" | |
199 | ||
200 | rm -f "$cli_stderr" | |
201 | rm -f "$cli_stdout" | |
202 | rm -f "$port_file" | |
203 | } | |
204 | ||
205 | test_list_sessions() { | |
206 | # Test the basic listing of sessions. | |
207 | # Ensure that a multi-domain trace is seen as a single session. | |
208 | # run_test() is not used here because the port is needed to craft the | |
209 | # expected output. | |
210 | ||
211 | local port | |
212 | local port_file | |
213 | local tmp_stdout_expected | |
214 | local template_expected | |
215 | ||
216 | local test_text="CLI prints the expected session list" | |
217 | local cli_args_template="-i lttng-live net://localhost:@PORT@" | |
286074cc | 218 | local server_args=("$test_data_dir/list_sessions.json") |
584af91e PP |
219 | |
220 | template_expected=$(<"$test_data_dir/cli-list-sessions.expect") | |
a01934b7 SM |
221 | cli_stderr="$(mktemp -t test_live_list_sessions_stderr.XXXXXX)" |
222 | cli_stdout="$(mktemp -t test_live_list_sessions_stdout.XXXXXX)" | |
223 | empty_file="$(mktemp -t test_live_list_sessions_empty.XXXXXX)" | |
224 | port_file="$(mktemp -t test_live_list_sessions_server_port.XXXXXX)" | |
225 | tmp_stdout_expected="$(mktemp -t test_live_list_sessions_stdout_expected.XXXXXX)" | |
584af91e | 226 | |
286074cc SM |
227 | get_cli_output_with_lttng_live_server "$cli_args_template" "$cli_stdout" \ |
228 | "$cli_stderr" "$port_file" "${server_args[@]}" | |
584af91e PP |
229 | port=$(<"$port_file") |
230 | ||
231 | # Craft the expected output. This is necessary since the port number | |
232 | # (random) of a "relayd" is present in the output. | |
233 | template_expected=${template_expected//@PORT@/$port} | |
234 | ||
235 | echo "$template_expected" > "$tmp_stdout_expected" | |
236 | ||
237 | bt_diff "$tmp_stdout_expected" "$cli_stdout" | |
238 | ok $? "$test_text - stdout" | |
239 | bt_diff "$empty_file" "$cli_stderr" | |
240 | ok $? "$test_text - stderr" | |
241 | ||
242 | rm -f "$cli_stderr" | |
243 | rm -f "$cli_stdout" | |
244 | rm -f "$empty_file" | |
245 | rm -f "$port_file" | |
246 | rm -f "$tmp_stdout_expected" | |
247 | } | |
248 | ||
249 | test_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 PP |
255 | local expected_stdout="${test_data_dir}/cli-base.expect" |
256 | local expected_stderr | |
257 | ||
258 | # Empty file for stderr expected | |
a01934b7 | 259 | expected_stderr="$(mktemp -t test_live_base_stderr_expected.XXXXXX)" |
584af91e | 260 | |
286074cc SM |
261 | run_test "$test_text" "$cli_args_template" "$expected_stdout" \ |
262 | "$expected_stderr" "${server_args[@]}" | |
584af91e PP |
263 | |
264 | rm -f "$expected_stderr" | |
265 | } | |
266 | ||
267 | test_multi_domains() { | |
268 | # Attach and consume data from a multi-domains session with discarded | |
269 | # events. | |
270 | local test_text="CLI attach and fetch from multi-domains session - discarded events" | |
271 | local cli_args_template="-i lttng-live net://localhost:@PORT@/host/hostname/multi-domains -c sink.text.details" | |
286074cc | 272 | local server_args=("${test_data_dir}/multi_domains.json") |
2b763e29 | 273 | local expected_stdout="$test_data_dir/cli-multi-domains.expect" |
584af91e PP |
274 | local expected_stderr |
275 | ||
276 | # Empty file for stderr expected | |
a01934b7 | 277 | expected_stderr="$(mktemp -t test_live_multi_domains_stderr_expected.XXXXXX)" |
584af91e | 278 | |
286074cc SM |
279 | run_test "$test_text" "$cli_args_template" "$expected_stdout" \ |
280 | "$expected_stderr" "${server_args[@]}" | |
584af91e PP |
281 | |
282 | rm -f "$expected_stderr" | |
283 | } | |
284 | ||
285 | test_rate_limited() { | |
286 | # Attach and consume data from a multi packets ust session with no | |
287 | # discarded events. Enforce a server side limit on the stream data | |
288 | # requests size. Ensure that babeltrace respect the returned size and that | |
289 | # many requests per packet works as expected. | |
290 | # The packet size of the test trace is 4k. Limit requests to 1k. | |
291 | local test_text="CLI many requests per packet" | |
292 | local cli_args_template="-i lttng-live net://localhost:@PORT@/host/hostname/trace-with-index -c sink.text.details" | |
286074cc | 293 | local server_args=(--max-query-data-response-size 1024 "$test_data_dir/rate_limited.json") |
584af91e PP |
294 | local expected_stdout="${test_data_dir}/cli-base.expect" |
295 | local expected_stderr | |
296 | ||
297 | # Empty file for stderr expected | |
a01934b7 | 298 | expected_stderr="$(mktemp -t test_live_rate_limited_stderr_expected.XXXXXX)" |
584af91e | 299 | |
286074cc SM |
300 | run_test "$test_text" "$cli_args_template" "$expected_stdout" \ |
301 | "$expected_stderr" "${server_args[@]}" | |
584af91e PP |
302 | |
303 | rm -f "$expected_stderr" | |
304 | } | |
305 | ||
306 | test_compare_to_ctf_fs() { | |
307 | # Compare the details text sink or ctf.fs and ctf.lttng-live to ensure | |
308 | # that the trace is parsed the same way. | |
2d2a59db JR |
309 | # Do the same with the session swapped on the relayd side. This validate |
310 | # that ordering is consistent between live and ctf fs. | |
584af91e | 311 | local test_text="CLI src.ctf.fs vs src.ctf.lttng-live" |
2d2a59db | 312 | 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" |
286074cc SM |
313 | local server_args=("$test_data_dir/multi_domains.json") |
314 | local server_args_inverse=("$test_data_dir/multi_domains_inverse.json") | |
584af91e PP |
315 | local expected_stdout |
316 | local expected_stderr | |
317 | ||
a01934b7 SM |
318 | expected_stdout="$(mktemp -t test_live_compare_stdout_expected.XXXXXX)" |
319 | expected_stderr="$(mktemp -t test_live_compare_stderr_expected.XXXXXX)" | |
584af91e | 320 | |
2b763e29 | 321 | 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 |
322 | bt_remove_cr "${expected_stdout}" |
323 | bt_remove_cr "${expected_stderr}" | |
286074cc SM |
324 | run_test "$test_text" "$cli_args_template" "$expected_stdout" \ |
325 | "$expected_stderr" "${server_args[@]}" | |
2d2a59db | 326 | diag "Inverse session order from lttng-relayd" |
286074cc SM |
327 | run_test "$test_text" "$cli_args_template" "$expected_stdout" \ |
328 | "$expected_stderr" "${server_args_inverse[@]}" | |
584af91e PP |
329 | |
330 | rm -f "$expected_stdout" | |
331 | rm -f "$expected_stderr" | |
332 | } | |
333 | ||
b198ef81 JG |
334 | test_inactivity_discarded_packet() { |
335 | # Attach and consume data from a multi-packet trace with discarded | |
336 | # packets and emit an inactivity beacon during the discarded packets | |
337 | # period. | |
338 | # | |
339 | # | pkt seq:0 |<-------discarded packets------>| pkt seq:8 | | |
340 | # 0 20 121 140 | |
341 | # | |
342 | # This test was introduced to cover the following bug: | |
343 | # | |
344 | # When reading this type of trace locally, the CTF source is expected | |
345 | # to introduce a "Discarded packets" message between packets 0 and 8. | |
346 | # The timestamps of this message are [pkt0.end_ts, pkt8.begin_ts]. | |
347 | # | |
348 | # In the context of a live source, the tracer could report an inactivity | |
349 | # period during the interval of the "Discarded packets" message. | |
350 | # Those messages eventually translate into a | |
351 | # "Iterator inactivity" message with a timestamp set at the end of the | |
352 | # inactivity period. | |
353 | # | |
354 | # If the tracer reports an inactivity period that ends at a point | |
355 | # between pkt0 and pkt7 (resulting in an "Iterator inactivity" message), | |
356 | # the live source could send a "Discarded packets" message that starts | |
357 | # before the preceding "Iterator inactivity" message. This would break | |
358 | # the monotonicity constraint of the graph. | |
359 | local test_text="CLI attach and fetch from single-domains session - inactivity discarded packet" | |
360 | local cli_args_template="-i lttng-live net://localhost:@PORT@/host/hostname/7_lost_between_2_with_index -c sink.text.details" | |
286074cc | 361 | local server_args=("$test_data_dir/inactivity_discarded_packet.json") |
b198ef81 JG |
362 | local expected_stdout="$test_data_dir/inactivity_discarded_packet.expect" |
363 | local expected_stderr | |
364 | ||
365 | # Empty file for stderr expected | |
366 | expected_stderr="$(mktemp -t test_live_inactivity_discarded_packet_stderr_expected.XXXXXX)" | |
367 | ||
286074cc SM |
368 | run_test "$test_text" "$cli_args_template" "$expected_stdout" \ |
369 | "$expected_stderr" "${server_args[@]}" | |
b198ef81 JG |
370 | |
371 | rm -f "$expected_stderr" | |
372 | } | |
373 | ||
78169723 FD |
374 | test_split_metadata() { |
375 | # Consume a metadata stream sent in two parts. This testcase tests the | |
376 | # behaviour of Babeltrace when the tracing session was cleared (lttng | |
377 | # clear) but the metadata is not yet available to the relay. In such | |
378 | # cases, when asked for metadata, the relay will return the | |
379 | # `LTTNG_VIEWER_METADATA_OK` status and a data length of 0. The viewer | |
380 | # need to consider such case as a request to retry fetching metadata. | |
381 | # | |
382 | # This testcase emulates such behaviour by adding empty metadata | |
383 | # packets. | |
384 | ||
385 | local test_text="CLI attach and fetch from single-domain session - Receive metadata in two sections separated by a empty section" | |
386 | local cli_args_template="-i lttng-live net://localhost:@PORT@/host/hostname/split_metadata -c sink.text.details" | |
286074cc | 387 | local server_args=("$test_data_dir/split_metadata.json") |
78169723 FD |
388 | local expected_stdout="${test_data_dir}/split_metadata.expect" |
389 | local expected_stderr | |
390 | ||
391 | # Empty file for stderr expected | |
392 | expected_stderr="$(mktemp -t test_live_split_metadata_stderr_expected.XXXXXX)" | |
393 | ||
286074cc SM |
394 | run_test "$test_text" "$cli_args_template" "$expected_stdout" \ |
395 | "$expected_stderr" "${server_args[@]}" | |
78169723 FD |
396 | |
397 | rm -f "$expected_stderr" | |
398 | } | |
399 | ||
400 | plan_tests 16 | |
584af91e PP |
401 | |
402 | test_list_sessions | |
403 | test_base | |
404 | test_multi_domains | |
405 | test_rate_limited | |
406 | test_compare_to_ctf_fs | |
b198ef81 | 407 | test_inactivity_discarded_packet |
78169723 | 408 | test_split_metadata |