From 2b763e2981a27a45f9a24e0dc74c96dbf40ffaf0 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Galarneau?= Date: Tue, 2 Jun 2020 15:35:55 -0400 Subject: [PATCH] Tests: src.ctf.lttng-live: use JSON description for sessions MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit In preparation for a follow-up commit introducing live beacons to insert within a stream's indexes, transition from the current session description string format to a JSON-based description. Signed-off-by: Jérémie Galarneau Change-Id: I08642f95888550de94bd7bddcf4b1e2e875b1a25 Reviewed-on: https://review.lttng.org/c/babeltrace/+/3605 Reviewed-by: Philippe Proulx Tested-by: jenkins --- .../data/plugins/src.ctf.lttng-live/base.json | 14 ++++ .../src.ctf.lttng-live/list_sessions.json | 38 +++++++++ .../src.ctf.lttng-live/lttng_live_server.py | 83 ++++++++++++++----- .../src.ctf.lttng-live/multi_domains.json | 26 ++++++ .../multi_domains_inverse.json | 26 ++++++ .../src.ctf.lttng-live/rate_limited.json | 14 ++++ tests/plugins/src.ctf.lttng-live/test_live | 34 +++++--- 7 files changed, 202 insertions(+), 33 deletions(-) create mode 100644 tests/data/plugins/src.ctf.lttng-live/base.json create mode 100644 tests/data/plugins/src.ctf.lttng-live/list_sessions.json create mode 100644 tests/data/plugins/src.ctf.lttng-live/multi_domains.json create mode 100644 tests/data/plugins/src.ctf.lttng-live/multi_domains_inverse.json create mode 100644 tests/data/plugins/src.ctf.lttng-live/rate_limited.json diff --git a/tests/data/plugins/src.ctf.lttng-live/base.json b/tests/data/plugins/src.ctf.lttng-live/base.json new file mode 100644 index 00000000..967645b5 --- /dev/null +++ b/tests/data/plugins/src.ctf.lttng-live/base.json @@ -0,0 +1,14 @@ +[ + { + "name": "trace-with-index", + "id": 2, + "hostname": "hostname", + "live-timer-freq": 1, + "client-count": 0, + "traces": [ + { + "path": "succeed/trace-with-index/" + } + ] + } +] diff --git a/tests/data/plugins/src.ctf.lttng-live/list_sessions.json b/tests/data/plugins/src.ctf.lttng-live/list_sessions.json new file mode 100644 index 00000000..0c0091ca --- /dev/null +++ b/tests/data/plugins/src.ctf.lttng-live/list_sessions.json @@ -0,0 +1,38 @@ +[ + { + "name": "multi-domains", + "id": 0, + "hostname": "hostname", + "live-timer-freq": 1, + "client-count": 0, + "traces": [ + { + "path": "succeed/multi-domains/ust/" + } + ] + }, + { + "name": "multi-domains", + "id": 1, + "hostname": "hostname", + "live-timer-freq": 1, + "client-count": 0, + "traces": [ + { + "path": "succeed/multi-domains/kernel/" + } + ] + }, + { + "name": "trace-with-index", + "id": 2, + "hostname": "hostname", + "live-timer-freq": 1, + "client-count": 0, + "traces": [ + { + "path": "succeed/trace-with-index/" + } + ] + } +] diff --git a/tests/data/plugins/src.ctf.lttng-live/lttng_live_server.py b/tests/data/plugins/src.ctf.lttng-live/lttng_live_server.py index 77b005ca..6b674666 100644 --- a/tests/data/plugins/src.ctf.lttng-live/lttng_live_server.py +++ b/tests/data/plugins/src.ctf.lttng-live/lttng_live_server.py @@ -13,6 +13,7 @@ import socket import struct import sys import tempfile +import json class UnexpectedInput(RuntimeError): @@ -1369,19 +1370,59 @@ class LttngTracingSessionDescriptor: return self._info -def _tracing_session_descriptors_from_arg(string): - # Format is: - # NAME,ID,HOSTNAME,FREQ,CLIENTS,TRACEPATH[,TRACEPATH]... - parts = string.split(',') - name = parts[0] - tracing_session_id = int(parts[1]) - hostname = parts[2] - live_timer_freq = int(parts[3]) - client_count = int(parts[4]) - traces = [LttngTrace(path) for path in parts[5:]] - return LttngTracingSessionDescriptor( - name, tracing_session_id, hostname, live_timer_freq, client_count, traces - ) +def _session_descriptors_from_path(sessions_filename, trace_path_prefix): + # File format is: + # + # [ + # { + # "name": "my-session", + # "id": 17, + # "hostname": "myhost", + # "live-timer-freq": 1000000, + # "client-count": 23, + # "traces": [ + # { + # "path": "lol" + # }, + # { + # "path": "meow/mix" + # } + # ] + # } + # ] + with open(sessions_filename, 'r') as sessions_file: + params = json.load(sessions_file) + + sessions = [] + + for session in params: + name = session['name'] + tracing_session_id = session['id'] + hostname = session['hostname'] + live_timer_freq = session['live-timer-freq'] + client_count = session['client-count'] + traces = [] + + for trace in session['traces']: + path = trace['path'] + + if not os.path.isabs(path): + path = os.path.join(trace_path_prefix, path) + + traces.append(LttngTrace(path)) + + sessions.append( + LttngTracingSessionDescriptor( + name, + tracing_session_id, + hostname, + live_timer_freq, + client_count, + traces, + ) + ) + + return sessions def _loglevel_parser(string): @@ -1418,11 +1459,12 @@ if __name__ == '__main__': help='The maximum size of control data response in bytes', ) parser.add_argument( - 'sessions', - nargs="+", - metavar="SESSION", - type=_tracing_session_descriptors_from_arg, - help='A session configuration. There is no space after comma. Format is: NAME,ID,HOSTNAME,FREQ,CLIENTS,TRACEPATH[,TRACEPATH]....', + '--trace-path-prefix', + type=str, + help='Prefix to prepend to the trace paths of session configurations', + ) + parser.add_argument( + '--sessions-filename', type=str, help='Path to a session configuration file', ) parser.add_argument( '-h', @@ -1434,9 +1476,10 @@ if __name__ == '__main__': args = parser.parse_args(args=remaining_args) try: - LttngLiveServer( - args.port_filename, args.sessions, args.max_query_data_response_size + sessions = _session_descriptors_from_path( + args.sessions_filename, args.trace_path_prefix ) + LttngLiveServer(args.port_filename, sessions, args.max_query_data_response_size) except UnexpectedInput as exc: logging.error(str(exc)) print(exc, file=sys.stderr) diff --git a/tests/data/plugins/src.ctf.lttng-live/multi_domains.json b/tests/data/plugins/src.ctf.lttng-live/multi_domains.json new file mode 100644 index 00000000..155cacf9 --- /dev/null +++ b/tests/data/plugins/src.ctf.lttng-live/multi_domains.json @@ -0,0 +1,26 @@ +[ + { + "name": "multi-domains", + "id": 0, + "hostname": "hostname", + "live-timer-freq": 1, + "client-count": 0, + "traces": [ + { + "path": "succeed/multi-domains/kernel/" + } + ] + }, + { + "name": "multi-domains", + "id": 1, + "hostname": "hostname", + "live-timer-freq": 1, + "client-count": 0, + "traces": [ + { + "path": "succeed/multi-domains/ust/" + } + ] + } +] diff --git a/tests/data/plugins/src.ctf.lttng-live/multi_domains_inverse.json b/tests/data/plugins/src.ctf.lttng-live/multi_domains_inverse.json new file mode 100644 index 00000000..71755022 --- /dev/null +++ b/tests/data/plugins/src.ctf.lttng-live/multi_domains_inverse.json @@ -0,0 +1,26 @@ +[ + { + "name": "multi-domains", + "id": 0, + "hostname": "hostname", + "live-timer-freq": 1, + "client-count": 0, + "traces": [ + { + "path": "succeed/multi-domains/ust/" + } + ] + }, + { + "name": "multi-domains", + "id": 1, + "hostname": "hostname", + "live-timer-freq": 1, + "client-count": 0, + "traces": [ + { + "path": "succeed/multi-domains/kernel/" + } + ] + } +] diff --git a/tests/data/plugins/src.ctf.lttng-live/rate_limited.json b/tests/data/plugins/src.ctf.lttng-live/rate_limited.json new file mode 100644 index 00000000..d7022b10 --- /dev/null +++ b/tests/data/plugins/src.ctf.lttng-live/rate_limited.json @@ -0,0 +1,14 @@ +[ + { + "name": "trace-with-index", + "id": 0, + "hostname": "hostname", + "live-timer-freq": 1, + "client-count": 0, + "traces": [ + { + "path": "succeed/trace-with-index/" + } + ] + } +] diff --git a/tests/plugins/src.ctf.lttng-live/test_live b/tests/plugins/src.ctf.lttng-live/test_live index 58a58514..9b44ec21 100755 --- a/tests/plugins/src.ctf.lttng-live/test_live +++ b/tests/plugins/src.ctf.lttng-live/test_live @@ -8,7 +8,7 @@ # This test validates that a `src.ctf.fs` component successfully reads # specific CTF traces and creates the expected messages. # -# Such CTF traces to open either exist (in `tests/ctf-traces/succeed`) +# Such CTF traces to open either exist (in `tests/ctf-traces/`) # or are generated by this test using local trace generators. SH_TAP=1 @@ -36,7 +36,7 @@ trap cleanup SIGINT SIGTERM this_dir_relative="plugins/src.ctf.lttng-live" test_data_dir="$BT_TESTS_DATADIR/$this_dir_relative" -trace_dir="$BT_CTF_TRACES_PATH/succeed" +trace_dir="$BT_CTF_TRACES_PATH" if [ "$BT_OS_TYPE" = "mingw" ]; then # Same as the above, but in Windows form (C:\foo\bar) instead of Unix form @@ -54,7 +54,9 @@ lttng_live_server() { local server_script="$test_data_dir/lttng_live_server.py" # start server - echo "$server_args" | xargs "$BT_TESTS_PYTHON_BIN" "$server_script" --port-file "$port_file" & + echo "$server_args" | xargs "$BT_TESTS_PYTHON_BIN" "$server_script" \ + --port-file "$port_file" \ + --trace-path-prefix "$trace_dir_native" & # write PID to file echo $! > "$pid_file" @@ -78,7 +80,7 @@ kill_lttng_live_server() { get_cli_output_with_lttng_live_server() { local cli_args_template="$1" - local server_args="$2" + local sessions_file="$2" local cli_stdout_file="$3" local cli_stderr_file="$4" local port_file="$5" @@ -102,7 +104,7 @@ get_cli_output_with_lttng_live_server() { # lttng_live_server() writes its return code to the # `$server_retcode_file` file. lttng_live_server "$port_file" "$server_pid_file" \ - "$server_retcode_file" "$server_args" & + "$server_retcode_file" "$sessions_file" & # Get port number i=0 @@ -209,7 +211,8 @@ test_list_sessions() { local test_text="CLI prints the expected session list" local cli_args_template="-i lttng-live net://localhost:@PORT@" - 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/' " + local sessions_file="$test_data_dir/list_sessions.json" + local server_args="--sessions-filename '$sessions_file'" template_expected=$(<"$test_data_dir/cli-list-sessions.expect") cli_stderr="$(mktemp -t test_live_list_sessions_stderr.XXXXXX)" @@ -244,7 +247,8 @@ test_base() { # discarded events. local test_text="CLI attach and fetch from single-domains session - no discarded events" local cli_args_template="-i lttng-live net://localhost:@PORT@/host/hostname/trace-with-index -c sink.text.details" - local server_args="'trace-with-index,0,hostname,1,0,${trace_dir_native}/trace-with-index/'" + local sessions_file="$test_data_dir/base.json" + local server_args="--sessions-filename '$sessions_file'" local expected_stdout="${test_data_dir}/cli-base.expect" local expected_stderr @@ -261,8 +265,9 @@ test_multi_domains() { # events. local test_text="CLI attach and fetch from multi-domains session - discarded events" local cli_args_template="-i lttng-live net://localhost:@PORT@/host/hostname/multi-domains -c sink.text.details" - local server_args="'multi-domains,0,hostname,1,0,${trace_dir_native}/multi-domains/kernel/,${trace_dir_native}/multi-domains/ust/'" - local expected_stdout="${test_data_dir}/cli-multi-domains.expect" + local sessions_file="${test_data_dir}/multi_domains.json" + local server_args="--sessions-filename '$sessions_file'" + local expected_stdout="$test_data_dir/cli-multi-domains.expect" local expected_stderr # Empty file for stderr expected @@ -281,7 +286,8 @@ test_rate_limited() { # The packet size of the test trace is 4k. Limit requests to 1k. local test_text="CLI many requests per packet" local cli_args_template="-i lttng-live net://localhost:@PORT@/host/hostname/trace-with-index -c sink.text.details" - local server_args="--max-query-data-response-size 1024 'trace-with-index,0,hostname,1,0,${trace_dir_native}/trace-with-index/'" + local sessions_file="$test_data_dir/rate_limited.json" + local server_args="--max-query-data-response-size 1024 --sessions-filename '$sessions_file'" local expected_stdout="${test_data_dir}/cli-base.expect" local expected_stderr @@ -300,15 +306,17 @@ test_compare_to_ctf_fs() { # that ordering is consistent between live and ctf fs. local test_text="CLI src.ctf.fs vs src.ctf.lttng-live" 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" - 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/'" - 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/'" + local sessions_file="$test_data_dir/multi_domains.json" + local sessions_file_inverse="$test_data_dir/multi_domains_inverse.json" + local server_args="--sessions-filename '$sessions_file'" + local server_args_inverse="--sessions-filename '$sessions_file_inverse'" local expected_stdout local expected_stderr expected_stdout="$(mktemp -t test_live_compare_stdout_expected.XXXXXX)" expected_stderr="$(mktemp -t test_live_compare_stderr_expected.XXXXXX)" - bt_cli "$expected_stdout" "$expected_stderr" "${trace_dir}/multi-domains" -c sink.text.details --params "with-trace-name=false,with-stream-name=false" + bt_cli "$expected_stdout" "$expected_stderr" "${trace_dir}/succeed/multi-domains" -c sink.text.details --params "with-trace-name=false,with-stream-name=false" bt_remove_cr "${expected_stdout}" bt_remove_cr "${expected_stderr}" run_test "$test_text" "$cli_args_template" "$server_args" "$expected_stdout" "$expected_stderr" -- 2.34.1