tests: make --port-filename optional in lttng_live_server.py
[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 port_file="$1"
52 local pid_file="$2"
53 local retcode_file="$3"
54 local server_args="$4"
55 local server_script="$test_data_dir/lttng_live_server.py"
56
57 # start server
58 diag "$BT_TESTS_PYTHON_BIN $server_script --port-file $port_file --trace-path-prefix $trace_dir_native $server_args"
59 echo "$server_args" | run_python xargs "$BT_TESTS_PYTHON_BIN" "$server_script" \
60 --port-file "$port_file" \
61 --trace-path-prefix "$trace_dir_native" 1>&2 &
62
63 # write PID to file
64 echo $! > "$pid_file"
65
66 # wait for server to exit
67 wait
68
69 # write return code to file
70 echo $? > "$retcode_file"
71 }
72
73 kill_lttng_live_server() {
74 local pid_file="$1"
75
76 if [ ! -s "$pid_file" ]; then
77 return
78 fi
79
80 kill -9 "$(cat "$pid_file")"
81 }
82
83 get_cli_output_with_lttng_live_server() {
84 local cli_args_template="$1"
85 local sessions_file="$2"
86 local cli_stdout_file="$3"
87 local cli_stderr_file="$4"
88 local port_file="$5"
89
90 local i
91 local ret
92 local port
93 local cli_args
94 local server_pid_file
95 local server_retcode_file
96
97 server_pid_file="$(mktemp -t test_live_server_pid.XXXXXX)"
98 server_retcode_file="$(mktemp -t test_live_server_ret.XXXXX)"
99
100 diag "Starting LTTng live server mockup"
101
102 # This starts the server, which eventually writes its listening
103 # port number to the `$port_file` file. The lttng_live_server()
104 # function itself writes the server's PID to the
105 # `$server_pid_file` file. When the server exits,
106 # lttng_live_server() writes its return code to the
107 # `$server_retcode_file` file.
108 lttng_live_server "$port_file" "$server_pid_file" \
109 "$server_retcode_file" "$sessions_file" &
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
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
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"
177 local server_args="$3"
178 local expected_stdout="$4"
179 local expected_stderr="$5"
180
181 local cli_stderr
182 local cli_stdout
183 local port_file
184 local port
185
186 cli_stderr="$(mktemp -t test_live_stderr.XXXXXX)"
187 cli_stdout="$(mktemp -t test_live_stdout.XXXXXX)"
188 port_file="$(mktemp -t test_live_server_port.XXXXXX)"
189
190 get_cli_output_with_lttng_live_server "$cli_args_template" "$server_args" "$cli_stdout" "$cli_stderr" "$port_file"
191 port=$(<"$port_file")
192
193 bt_diff "$expected_stdout" "$cli_stdout"
194 ok $? "$test_text - stdout"
195 bt_diff "$expected_stderr" "$cli_stderr"
196 ok $? "$test_text - stderr"
197
198 rm -f "$cli_stderr"
199 rm -f "$cli_stdout"
200 rm -f "$port_file"
201 }
202
203 test_list_sessions() {
204 # Test the basic listing of sessions.
205 # Ensure that a multi-domain trace is seen as a single session.
206 # run_test() is not used here because the port is needed to craft the
207 # expected output.
208
209 local port
210 local port_file
211 local tmp_stdout_expected
212 local template_expected
213
214 local test_text="CLI prints the expected session list"
215 local cli_args_template="-i lttng-live net://localhost:@PORT@"
216 local sessions_file="$test_data_dir/list_sessions.json"
217 local server_args="--sessions-filename '$sessions_file'"
218
219 template_expected=$(<"$test_data_dir/cli-list-sessions.expect")
220 cli_stderr="$(mktemp -t test_live_list_sessions_stderr.XXXXXX)"
221 cli_stdout="$(mktemp -t test_live_list_sessions_stdout.XXXXXX)"
222 empty_file="$(mktemp -t test_live_list_sessions_empty.XXXXXX)"
223 port_file="$(mktemp -t test_live_list_sessions_server_port.XXXXXX)"
224 tmp_stdout_expected="$(mktemp -t test_live_list_sessions_stdout_expected.XXXXXX)"
225
226 get_cli_output_with_lttng_live_server "$cli_args_template" "$server_args" "$cli_stdout" "$cli_stderr" "$port_file"
227 port=$(<"$port_file")
228
229 # Craft the expected output. This is necessary since the port number
230 # (random) of a "relayd" is present in the output.
231 template_expected=${template_expected//@PORT@/$port}
232
233 echo "$template_expected" > "$tmp_stdout_expected"
234
235 bt_diff "$tmp_stdout_expected" "$cli_stdout"
236 ok $? "$test_text - stdout"
237 bt_diff "$empty_file" "$cli_stderr"
238 ok $? "$test_text - stderr"
239
240 rm -f "$cli_stderr"
241 rm -f "$cli_stdout"
242 rm -f "$empty_file"
243 rm -f "$port_file"
244 rm -f "$tmp_stdout_expected"
245 }
246
247 test_base() {
248 # Attach and consume data from a multi packets ust session with no
249 # discarded events.
250 local test_text="CLI attach and fetch from single-domains session - no discarded events"
251 local cli_args_template="-i lttng-live net://localhost:@PORT@/host/hostname/trace-with-index -c sink.text.details"
252 local sessions_file="$test_data_dir/base.json"
253 local server_args="--sessions-filename '$sessions_file'"
254 local expected_stdout="${test_data_dir}/cli-base.expect"
255 local expected_stderr
256
257 # Empty file for stderr expected
258 expected_stderr="$(mktemp -t test_live_base_stderr_expected.XXXXXX)"
259
260 run_test "$test_text" "$cli_args_template" "$server_args" "$expected_stdout" "$expected_stderr"
261
262 rm -f "$expected_stderr"
263 }
264
265 test_multi_domains() {
266 # Attach and consume data from a multi-domains session with discarded
267 # events.
268 local test_text="CLI attach and fetch from multi-domains session - discarded events"
269 local cli_args_template="-i lttng-live net://localhost:@PORT@/host/hostname/multi-domains -c sink.text.details"
270 local sessions_file="${test_data_dir}/multi_domains.json"
271 local server_args="--sessions-filename '$sessions_file'"
272 local expected_stdout="$test_data_dir/cli-multi-domains.expect"
273 local expected_stderr
274
275 # Empty file for stderr expected
276 expected_stderr="$(mktemp -t test_live_multi_domains_stderr_expected.XXXXXX)"
277
278 run_test "$test_text" "$cli_args_template" "$server_args" "$expected_stdout" "$expected_stderr"
279
280 rm -f "$expected_stderr"
281 }
282
283 test_rate_limited() {
284 # Attach and consume data from a multi packets ust session with no
285 # discarded events. Enforce a server side limit on the stream data
286 # requests size. Ensure that babeltrace respect the returned size and that
287 # many requests per packet works as expected.
288 # The packet size of the test trace is 4k. Limit requests to 1k.
289 local test_text="CLI many requests per packet"
290 local cli_args_template="-i lttng-live net://localhost:@PORT@/host/hostname/trace-with-index -c sink.text.details"
291 local sessions_file="$test_data_dir/rate_limited.json"
292 local server_args="--max-query-data-response-size 1024 --sessions-filename '$sessions_file'"
293 local expected_stdout="${test_data_dir}/cli-base.expect"
294 local expected_stderr
295
296 # Empty file for stderr expected
297 expected_stderr="$(mktemp -t test_live_rate_limited_stderr_expected.XXXXXX)"
298
299 run_test "$test_text" "$cli_args_template" "$server_args" "$expected_stdout" "$expected_stderr"
300
301 rm -f "$expected_stderr"
302 }
303
304 test_compare_to_ctf_fs() {
305 # Compare the details text sink or ctf.fs and ctf.lttng-live to ensure
306 # that the trace is parsed the same way.
307 # Do the same with the session swapped on the relayd side. This validate
308 # that ordering is consistent between live and ctf fs.
309 local test_text="CLI src.ctf.fs vs src.ctf.lttng-live"
310 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"
311 local sessions_file="$test_data_dir/multi_domains.json"
312 local sessions_file_inverse="$test_data_dir/multi_domains_inverse.json"
313 local server_args="--sessions-filename '$sessions_file'"
314 local server_args_inverse="--sessions-filename '$sessions_file_inverse'"
315 local expected_stdout
316 local expected_stderr
317
318 expected_stdout="$(mktemp -t test_live_compare_stdout_expected.XXXXXX)"
319 expected_stderr="$(mktemp -t test_live_compare_stderr_expected.XXXXXX)"
320
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"
322 bt_remove_cr "${expected_stdout}"
323 bt_remove_cr "${expected_stderr}"
324 run_test "$test_text" "$cli_args_template" "$server_args" "$expected_stdout" "$expected_stderr"
325 diag "Inverse session order from lttng-relayd"
326 run_test "$test_text" "$cli_args_template" "$server_args_inverse" "$expected_stdout" "$expected_stderr"
327
328 rm -f "$expected_stdout"
329 rm -f "$expected_stderr"
330 }
331
332 test_inactivity_discarded_packet() {
333 # Attach and consume data from a multi-packet trace with discarded
334 # packets and emit an inactivity beacon during the discarded packets
335 # period.
336 #
337 # | pkt seq:0 |<-------discarded packets------>| pkt seq:8 |
338 # 0 20 121 140
339 #
340 # This test was introduced to cover the following bug:
341 #
342 # When reading this type of trace locally, the CTF source is expected
343 # to introduce a "Discarded packets" message between packets 0 and 8.
344 # The timestamps of this message are [pkt0.end_ts, pkt8.begin_ts].
345 #
346 # In the context of a live source, the tracer could report an inactivity
347 # period during the interval of the "Discarded packets" message.
348 # Those messages eventually translate into a
349 # "Iterator inactivity" message with a timestamp set at the end of the
350 # inactivity period.
351 #
352 # If the tracer reports an inactivity period that ends at a point
353 # between pkt0 and pkt7 (resulting in an "Iterator inactivity" message),
354 # the live source could send a "Discarded packets" message that starts
355 # before the preceding "Iterator inactivity" message. This would break
356 # the monotonicity constraint of the graph.
357 local test_text="CLI attach and fetch from single-domains session - inactivity discarded packet"
358 local cli_args_template="-i lttng-live net://localhost:@PORT@/host/hostname/7_lost_between_2_with_index -c sink.text.details"
359 local sessions_file="$test_data_dir/inactivity_discarded_packet.json"
360 local server_args="--sessions-filename '$sessions_file'"
361 local expected_stdout="$test_data_dir/inactivity_discarded_packet.expect"
362 local expected_stderr
363
364 # Empty file for stderr expected
365 expected_stderr="$(mktemp -t test_live_inactivity_discarded_packet_stderr_expected.XXXXXX)"
366
367 run_test "$test_text" "$cli_args_template" "$server_args" "$expected_stdout" "$expected_stderr"
368
369 rm -f "$expected_stderr"
370 }
371
372 test_split_metadata() {
373 # Consume a metadata stream sent in two parts. This testcase tests the
374 # behaviour of Babeltrace when the tracing session was cleared (lttng
375 # clear) but the metadata is not yet available to the relay. In such
376 # cases, when asked for metadata, the relay will return the
377 # `LTTNG_VIEWER_METADATA_OK` status and a data length of 0. The viewer
378 # need to consider such case as a request to retry fetching metadata.
379 #
380 # This testcase emulates such behaviour by adding empty metadata
381 # packets.
382
383 local test_text="CLI attach and fetch from single-domain session - Receive metadata in two sections separated by a empty section"
384 local cli_args_template="-i lttng-live net://localhost:@PORT@/host/hostname/split_metadata -c sink.text.details"
385 local sessions_file="$test_data_dir/split_metadata.json"
386 local server_args="--sessions-filename '$sessions_file'"
387 local expected_stdout="${test_data_dir}/split_metadata.expect"
388 local expected_stderr
389
390 # Empty file for stderr expected
391 expected_stderr="$(mktemp -t test_live_split_metadata_stderr_expected.XXXXXX)"
392
393 run_test "$test_text" "$cli_args_template" "$server_args" "$expected_stdout" "$expected_stderr"
394
395 rm -f "$expected_stderr"
396 }
397
398 plan_tests 16
399
400 test_list_sessions
401 test_base
402 test_multi_domains
403 test_rate_limited
404 test_compare_to_ctf_fs
405 test_inactivity_discarded_packet
406 test_split_metadata
This page took 0.051384 seconds and 5 git commands to generate.