tests: make --port-filename optional in lttng_live_server.py
[babeltrace.git] / tests / data / plugins / src.ctf.lttng-live / lttng_live_server.py
index 45246b9bd5d6198134affefa9326117f5edf96f6..078b31fc24394a4fdf2cd242c055b18ead20eccd 100644 (file)
@@ -7,7 +7,6 @@
 
 import os
 import re
-import sys
 import socket
 import struct
 import logging
@@ -24,10 +23,6 @@ from typing import Any, Callable  # noqa: F401
 # isort: on
 
 
-class UnexpectedInput(RuntimeError):
-    pass
-
-
 # An entry within the index of an LTTng data stream.
 class _LttngDataStreamIndexEntry:
     def __init__(
@@ -569,7 +564,7 @@ class _LttngLiveViewerProtocolCodec:
                 version, tracing_session_id
             )
         else:
-            raise UnexpectedInput("Unknown command type {}".format(cmd_type))
+            raise RuntimeError("Unknown command type {}".format(cmd_type))
 
     def _pack(self, fmt: str, *args: Any):
         # Force network byte order
@@ -1398,7 +1393,7 @@ class _LttngLiveViewerSession:
 
     def _get_tracing_session_state(self, tracing_session_id: int):
         if tracing_session_id not in self._ts_states:
-            raise UnexpectedInput(
+            raise RuntimeError(
                 "Unknown tracing session ID {}".format(tracing_session_id)
             )
 
@@ -1433,7 +1428,7 @@ class _LttngLiveViewerSession:
         cmd_type = type(cmd)
 
         if cmd_type not in self._command_handlers:
-            raise UnexpectedInput(
+            raise RuntimeError(
                 "Unexpected command: cmd-cls-name={}".format(cmd.__class__.__name__)
             )
 
@@ -1448,7 +1443,7 @@ class _LttngLiveViewerSession:
         info = ts_state.tracing_session_descriptor.info
 
         if ts_state.is_attached:
-            raise UnexpectedInput(
+            raise RuntimeError(
                 "Cannot attach to tracing session `{}`: viewer is already attached".format(
                     info.name
                 )
@@ -1469,7 +1464,7 @@ class _LttngLiveViewerSession:
         info = ts_state.tracing_session_descriptor.info
 
         if not ts_state.is_attached:
-            raise UnexpectedInput(
+            raise RuntimeError(
                 "Cannot detach to tracing session `{}`: viewer is not attached".format(
                     info.name
                 )
@@ -1629,7 +1624,7 @@ class LttngLiveServer:
     def __init__(
         self,
         port: Optional[int],
-        port_filename: str,
+        port_filename: Optional[str],
         tracing_session_descriptors: Iterable[LttngTracingSessionDescriptor],
         max_query_data_response_size: Optional[int],
     ):
@@ -1669,7 +1664,11 @@ class LttngLiveServer:
         # Port 0: OS assigns an unused port
         serv_addr = ("localhost", port if port is not None else 0)
         self._sock.bind(serv_addr)
-        self._write_port_to_file(port_filename)
+
+        if port_filename is not None:
+            self._write_port_to_file(port_filename)
+
+        print("Listening on port {}".format(self._server_port))
 
         try:
             self._listen()
@@ -1692,7 +1691,7 @@ class LttngLiveServer:
                 logging.info("Client closed connection.")
 
                 if data:
-                    raise UnexpectedInput(
+                    raise RuntimeError(
                         "Client closed connection after having sent {} command bytes.".format(
                             len(data)
                         )
@@ -1707,7 +1706,7 @@ class LttngLiveServer:
             try:
                 cmd = self._codec.decode(data)
             except struct.error as exc:
-                raise UnexpectedInput("Malformed command: {}".format(exc)) from exc
+                raise RuntimeError("Malformed command: {}".format(exc)) from exc
 
             if cmd is not None:
                 logging.info(
@@ -1731,7 +1730,7 @@ class LttngLiveServer:
         cmd = self._recv_command()
 
         if type(cmd) is not _LttngLiveViewerConnectCommand:
-            raise UnexpectedInput(
+            raise RuntimeError(
                 'First command is not "connect": cmd-cls-name={}'.format(
                     cmd.__class__.__name__
                 )
@@ -1905,7 +1904,6 @@ if __name__ == "__main__":
     parser.add_argument(
         "--port-filename",
         help="The final port file. This file is present when the server is ready to receive connection.",
-        required=True,
     )
     parser.add_argument(
         "--max-query-data-response-size",
@@ -1931,21 +1929,14 @@ if __name__ == "__main__":
     )
 
     args = parser.parse_args(args=remaining_args)
-    try:
-        sessions_filename = args.sessions_filename  # type: str
-        trace_path_prefix = args.trace_path_prefix  # type: str | None
-        sessions = _session_descriptors_from_path(
-            sessions_filename,
-            trace_path_prefix,
-        )
+    sessions_filename = args.sessions_filename  # type: str
+    trace_path_prefix = args.trace_path_prefix  # type: str | None
+    sessions = _session_descriptors_from_path(
+        sessions_filename,
+        trace_path_prefix,
+    )
 
-        port = args.port  # type: int | None
-        port_filename = args.port_filename  # type: str
-        max_query_data_response_size = (
-            args.max_query_data_response_size
-        )  # type: int | None
-        LttngLiveServer(port, port_filename, sessions, max_query_data_response_size)
-    except UnexpectedInput as exc:
-        logging.error(str(exc))
-        print(exc, file=sys.stderr)
-        sys.exit(1)
+    port = args.port  # type: int | None
+    port_filename = args.port_filename  # type: str | None
+    max_query_data_response_size = args.max_query_data_response_size  # type: int | None
+    LttngLiveServer(port, port_filename, sessions, max_query_data_response_size)
This page took 0.025579 seconds and 4 git commands to generate.