| 1 | #!/usr/bin/env python3 |
| 2 | # |
| 3 | # Copyright (C) 2024 Kienan Stewart <kstewart@efficios.com> |
| 4 | # SPDX-License-Identifier: GPL-2.0-only |
| 5 | # |
| 6 | |
| 7 | """ |
| 8 | Test for https://review.lttng.org/c/lttng-tools/+/11819 |
| 9 | |
| 10 | A live client shouldn't hang around after a session with no data has |
| 11 | been destroyed |
| 12 | """ |
| 13 | |
| 14 | import pathlib |
| 15 | import socket |
| 16 | import subprocess |
| 17 | import sys |
| 18 | import time |
| 19 | |
| 20 | # Import in-tree test utils |
| 21 | test_utils_import_path = pathlib.Path(__file__).absolute().parents[3] / "utils" |
| 22 | sys.path.append(str(test_utils_import_path)) |
| 23 | |
| 24 | import lttngtest |
| 25 | import bt2 |
| 26 | |
| 27 | tap = lttngtest.TapGenerator(1) |
| 28 | |
| 29 | |
| 30 | def test_live_hang(tap, test_env): |
| 31 | client = lttngtest.LTTngClient(test_env, log=tap.diagnostic) |
| 32 | |
| 33 | # lttng create --live |
| 34 | output = lttngtest.NetworkSessionOutputLocation( |
| 35 | "net://localhost:{}:{}/".format( |
| 36 | test_env.lttng_relayd_control_port, test_env.lttng_relayd_data_port |
| 37 | ) |
| 38 | ) |
| 39 | session = client.create_session(output=output, live=True) |
| 40 | |
| 41 | # lttng enable-event --userspace --all |
| 42 | channel = session.add_channel(lttngtest.lttngctl.TracingDomain.User) |
| 43 | channel.add_recording_rule(lttngtest.lttngctl.UserTracepointEventRule()) |
| 44 | |
| 45 | session.start() |
| 46 | test_app = test_env.launch_wait_trace_test_application(100) |
| 47 | session.stop() |
| 48 | session.clear() |
| 49 | |
| 50 | ctf_live_cc = bt2.find_plugin("ctf").source_component_classes["lttng-live"] |
| 51 | query_executor = bt2.QueryExecutor( |
| 52 | ctf_live_cc, |
| 53 | "sessions", |
| 54 | params={"url": "net://localhost:{}".format(test_env.lttng_relayd_live_port)}, |
| 55 | ) |
| 56 | |
| 57 | # wait until 'ready' |
| 58 | ready = False |
| 59 | query_result = None |
| 60 | while not ready: |
| 61 | try: |
| 62 | query_result = query_executor.query() |
| 63 | except bt2._Error: |
| 64 | time.sleep(0.1) |
| 65 | continue |
| 66 | |
| 67 | for live_session in query_result: |
| 68 | if live_session["session-name"] == session.name: |
| 69 | ready = True |
| 70 | break |
| 71 | time.sleep(0.1) |
| 72 | |
| 73 | # start live viewer |
| 74 | bt2_args = [ |
| 75 | "babeltrace2", |
| 76 | "-i", |
| 77 | "lttng-live", |
| 78 | "net://localhost:{}/host/{}/{}".format( |
| 79 | test_env.lttng_relayd_live_port, socket.gethostname(), session.name |
| 80 | ), |
| 81 | "--params=session-not-found-action=end", |
| 82 | ] |
| 83 | tap.diagnostic("Running bt2: {}".format(bt2_args)) |
| 84 | bt2_proc = subprocess.Popen(bt2_args) |
| 85 | |
| 86 | # wait until one client is connected |
| 87 | ready = False |
| 88 | while not ready: |
| 89 | try: |
| 90 | query_result = query_executor.query() |
| 91 | except bt2._Error: |
| 92 | time.sleep(0.1) |
| 93 | continue |
| 94 | for live_session in query_result: |
| 95 | if ( |
| 96 | live_session["session-name"] == session.name |
| 97 | and live_session["client-count"] == 1 |
| 98 | ): |
| 99 | ready = True |
| 100 | break |
| 101 | time.sleep(0.1) |
| 102 | |
| 103 | session.destroy() |
| 104 | |
| 105 | # assert live viewer has exited |
| 106 | stopped = False |
| 107 | try: |
| 108 | bt2_proc.wait(5) |
| 109 | stopped = True |
| 110 | except subprocess.TimeoutExpired as e: |
| 111 | tap.diagnostic("Timed out (5s) waiting for babeltrace2 to return") |
| 112 | tap.test( |
| 113 | stopped and bt2_proc.returncode == 0, "BT2 live viewer exited successfully" |
| 114 | ) |
| 115 | if not stopped: |
| 116 | bt2_proc.terminate() |
| 117 | |
| 118 | |
| 119 | with lttngtest.test_environment( |
| 120 | with_sessiond=True, log=tap.diagnostic, with_relayd=True |
| 121 | ) as test_env: |
| 122 | test_live_hang(tap, test_env) |
| 123 | |
| 124 | sys.exit(0 if tap.is_successful else 1) |