Move to kernel style SPDX license identifiers
[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.fs` component successfully reads
9 # specific CTF traces and creates the expected messages.
10 #
11 # Such CTF traces to open either exist (in `tests/ctf-traces/succeed`)
12 # or are generated by this test using local trace generators.
13
14 SH_TAP=1
15
16 if [ "x${BT_TESTS_SRCDIR:-}" != "x" ]; then
17 UTILSSH="$BT_TESTS_SRCDIR/utils/utils.sh"
18 else
19 UTILSSH="$(dirname "$0")/../../utils/utils.sh"
20 fi
21
22 # shellcheck source=../../utils/utils.sh
23 source "$UTILSSH"
24
25 function cleanup ()
26 {
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 -- -$$
32 }
33
34 # Ensure that background child jobs are killed on SIGINT/SIGTERM
35 trap cleanup SIGINT SIGTERM
36
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/succeed"
40
41 if [ "$BT_OS_TYPE" = "mingw" ]; then
42 # Same as the above, but in Windows form (C:\foo\bar) instead of Unix form
43 # (/c/foo/bar).
44 trace_dir_native=$(cygpath -w "${trace_dir}")
45 else
46 trace_dir_native="${trace_dir}"
47 fi
48
49 lttng_live_server() {
50 local port_file="$1"
51 local pid_file="$2"
52 local retcode_file="$3"
53 local server_args="$4"
54 local server_script="$test_data_dir/lttng_live_server.py"
55
56 # start server
57 echo "$server_args" | xargs "$BT_TESTS_PYTHON_BIN" "$server_script" --port-file "$port_file" &
58
59 # write PID to file
60 echo $! > "$pid_file"
61
62 # wait for server to exit
63 wait
64
65 # write return code to file
66 echo $? > "$retcode_file"
67 }
68
69 kill_lttng_live_server() {
70 local pid_file="$1"
71
72 if [ ! -s "$pid_file" ]; then
73 return
74 fi
75
76 kill -9 "$(cat "$pid_file")"
77 }
78
79 get_cli_output_with_lttng_live_server() {
80 local cli_args_template="$1"
81 local server_args="$2"
82 local cli_stdout_file="$3"
83 local cli_stderr_file="$4"
84 local port_file="$5"
85
86 local i
87 local ret
88 local port
89 local cli_args
90 local server_pid_file
91 local server_retcode_file
92
93 server_pid_file="$(mktemp -t test_live_server_pid.XXXXXX)"
94 server_retcode_file="$(mktemp -t test_live_server_ret.XXXXX)"
95
96 diag "Starting LTTng live server mockup"
97
98 # This starts the server, which eventually writes its listening
99 # port number to the `$port_file` file. The lttng_live_server()
100 # function itself writes the server's PID to the
101 # `$server_pid_file` file. When the server exits,
102 # lttng_live_server() writes its return code to the
103 # `$server_retcode_file` file.
104 lttng_live_server "$port_file" "$server_pid_file" \
105 "$server_retcode_file" "$server_args" &
106
107 # Get port number
108 i=0
109 while [ ! -s "$port_file" ]; do
110 sleep .1
111
112 # Timeout of 30 seconds
113 if [ "$i" -eq "300" ]; then
114 # too long, kill it
115 kill_lttng_live_server "$server_pid_file"
116 wait
117 rm -f "$server_pid_file"
118 rm -f "$server_retcode_file"
119 return 1
120 fi
121
122 i=$((i + 1))
123 done
124
125 port=$(<"$port_file")
126
127 diag "LTTng live port is $port"
128
129 cli_args=${cli_args_template//@PORT@/$port}
130
131 # Split argument string by spaces into an array.
132 IFS=' ' read -ra cli_args <<< "$cli_args"
133
134 if ! bt_cli "$cli_stdout_file" "$cli_stderr_file" "${cli_args[@]}"; then
135 # CLI failed: cancel everything else
136 kill_lttng_live_server "$server_pid_file"
137 wait
138 rm -f "$server_pid_file"
139 rm -f "$server_retcode_file"
140 return 1
141 fi
142
143 # get server's return code
144 i=0
145 while [ ! -s "$server_retcode_file" ]; do
146 sleep .1
147
148 # Timeout of 30 seconds
149 if [ "$i" -eq "300" ]; then
150 # too long, kill it
151 kill_lttng_live_server "$server_pid_file"
152 wait
153 rm -f "$server_pid_file"
154 rm -f "$server_retcode_file"
155 return 1
156 fi
157
158 i=$((i + 1))
159 done
160
161 wait
162
163 ret=$(<"$server_retcode_file")
164
165 rm -f "$server_pid_file"
166 rm -f "$server_retcode_file"
167 return "$ret"
168 }
169
170 run_test() {
171 local test_text="$1"
172 local cli_args_template="$2"
173 local server_args="$3"
174 local expected_stdout="$4"
175 local expected_stderr="$5"
176
177 local cli_stderr
178 local cli_stdout
179 local port_file
180 local port
181
182 cli_stderr="$(mktemp -t test_live_stderr.XXXXXX)"
183 cli_stdout="$(mktemp -t test_live_stdout.XXXXXX)"
184 port_file="$(mktemp -t test_live_server_port.XXXXXX)"
185
186 get_cli_output_with_lttng_live_server "$cli_args_template" "$server_args" "$cli_stdout" "$cli_stderr" "$port_file"
187 port=$(<"$port_file")
188
189 bt_diff "$expected_stdout" "$cli_stdout"
190 ok $? "$test_text - stdout"
191 bt_diff "$expected_stderr" "$cli_stderr"
192 ok $? "$test_text - stderr"
193
194 rm -f "$cli_stderr"
195 rm -f "$cli_stdout"
196 rm -f "$port_file"
197 }
198
199 test_list_sessions() {
200 # Test the basic listing of sessions.
201 # Ensure that a multi-domain trace is seen as a single session.
202 # run_test() is not used here because the port is needed to craft the
203 # expected output.
204
205 local port
206 local port_file
207 local tmp_stdout_expected
208 local template_expected
209
210 local test_text="CLI prints the expected session list"
211 local cli_args_template="-i lttng-live net://localhost:@PORT@"
212 local server_args="'multi-domains,0,hostname,1,0,${trace_dir_native}/multi-domains/ust/' 'multi-domains,1,hostname,1,0,${trace_dir_native}/multi-domains/kernel/' 'trace-with-index,2,hostname,1,0,${trace_dir_native}/trace-with-index/' "
213
214 template_expected=$(<"$test_data_dir/cli-list-sessions.expect")
215 cli_stderr="$(mktemp -t test_live_list_sessions_stderr.XXXXXX)"
216 cli_stdout="$(mktemp -t test_live_list_sessions_stdout.XXXXXX)"
217 empty_file="$(mktemp -t test_live_list_sessions_empty.XXXXXX)"
218 port_file="$(mktemp -t test_live_list_sessions_server_port.XXXXXX)"
219 tmp_stdout_expected="$(mktemp -t test_live_list_sessions_stdout_expected.XXXXXX)"
220
221 get_cli_output_with_lttng_live_server "$cli_args_template" "$server_args" "$cli_stdout" "$cli_stderr" "$port_file"
222 port=$(<"$port_file")
223
224 # Craft the expected output. This is necessary since the port number
225 # (random) of a "relayd" is present in the output.
226 template_expected=${template_expected//@PORT@/$port}
227
228 echo "$template_expected" > "$tmp_stdout_expected"
229
230 bt_diff "$tmp_stdout_expected" "$cli_stdout"
231 ok $? "$test_text - stdout"
232 bt_diff "$empty_file" "$cli_stderr"
233 ok $? "$test_text - stderr"
234
235 rm -f "$cli_stderr"
236 rm -f "$cli_stdout"
237 rm -f "$empty_file"
238 rm -f "$port_file"
239 rm -f "$tmp_stdout_expected"
240 }
241
242 test_base() {
243 # Attach and consume data from a multi packets ust session with no
244 # discarded events.
245 local test_text="CLI attach and fetch from single-domains session - no discarded events"
246 local cli_args_template="-i lttng-live net://localhost:@PORT@/host/hostname/trace-with-index -c sink.text.details"
247 local server_args="'trace-with-index,0,hostname,1,0,${trace_dir_native}/trace-with-index/'"
248 local expected_stdout="${test_data_dir}/cli-base.expect"
249 local expected_stderr
250
251 # Empty file for stderr expected
252 expected_stderr="$(mktemp -t test_live_base_stderr_expected.XXXXXX)"
253
254 run_test "$test_text" "$cli_args_template" "$server_args" "$expected_stdout" "$expected_stderr"
255
256 rm -f "$expected_stderr"
257 }
258
259 test_multi_domains() {
260 # Attach and consume data from a multi-domains session with discarded
261 # events.
262 local test_text="CLI attach and fetch from multi-domains session - discarded events"
263 local cli_args_template="-i lttng-live net://localhost:@PORT@/host/hostname/multi-domains -c sink.text.details"
264 local server_args="'multi-domains,0,hostname,1,0,${trace_dir_native}/multi-domains/kernel/,${trace_dir_native}/multi-domains/ust/'"
265 local expected_stdout="${test_data_dir}/cli-multi-domains.expect"
266 local expected_stderr
267
268 # Empty file for stderr expected
269 expected_stderr="$(mktemp -t test_live_multi_domains_stderr_expected.XXXXXX)"
270
271 run_test "$test_text" "$cli_args_template" "$server_args" "$expected_stdout" "$expected_stderr"
272
273 rm -f "$expected_stderr"
274 }
275
276 test_rate_limited() {
277 # Attach and consume data from a multi packets ust session with no
278 # discarded events. Enforce a server side limit on the stream data
279 # requests size. Ensure that babeltrace respect the returned size and that
280 # many requests per packet works as expected.
281 # The packet size of the test trace is 4k. Limit requests to 1k.
282 local test_text="CLI many requests per packet"
283 local cli_args_template="-i lttng-live net://localhost:@PORT@/host/hostname/trace-with-index -c sink.text.details"
284 local server_args="--max-query-data-response-size 1024 'trace-with-index,0,hostname,1,0,${trace_dir_native}/trace-with-index/'"
285 local expected_stdout="${test_data_dir}/cli-base.expect"
286 local expected_stderr
287
288 # Empty file for stderr expected
289 expected_stderr="$(mktemp -t test_live_rate_limited_stderr_expected.XXXXXX)"
290
291 run_test "$test_text" "$cli_args_template" "$server_args" "$expected_stdout" "$expected_stderr"
292
293 rm -f "$expected_stderr"
294 }
295
296 test_compare_to_ctf_fs() {
297 # Compare the details text sink or ctf.fs and ctf.lttng-live to ensure
298 # that the trace is parsed the same way.
299 # Do the same with the session swapped on the relayd side. This validate
300 # that ordering is consistent between live and ctf fs.
301 local test_text="CLI src.ctf.fs vs src.ctf.lttng-live"
302 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"
303 local server_args="'multi-domains,0,hostname,1,0,${trace_dir_native}/multi-domains/kernel/' 'multi-domains,1,hostname,1,0,${trace_dir_native}/multi-domains/ust/'"
304 local server_args_inverse="'multi-domains,0,hostname,1,0,${trace_dir_native}/multi-domains/ust/' 'multi-domains,1,hostname,1,0,${trace_dir_native}/multi-domains/kernel/'"
305 local expected_stdout
306 local expected_stderr
307
308 expected_stdout="$(mktemp -t test_live_compare_stdout_expected.XXXXXX)"
309 expected_stderr="$(mktemp -t test_live_compare_stderr_expected.XXXXXX)"
310
311 bt_cli "$expected_stdout" "$expected_stderr" "${trace_dir}/multi-domains" -c sink.text.details --params "with-trace-name=false,with-stream-name=false"
312 bt_remove_cr "${expected_stdout}"
313 bt_remove_cr "${expected_stderr}"
314 run_test "$test_text" "$cli_args_template" "$server_args" "$expected_stdout" "$expected_stderr"
315 diag "Inverse session order from lttng-relayd"
316 run_test "$test_text" "$cli_args_template" "$server_args_inverse" "$expected_stdout" "$expected_stderr"
317
318 rm -f "$expected_stdout"
319 rm -f "$expected_stderr"
320 }
321
322 plan_tests 12
323
324 test_list_sessions
325 test_base
326 test_multi_domains
327 test_rate_limited
328 test_compare_to_ctf_fs
This page took 0.03723 seconds and 4 git commands to generate.