From 6acce9b87dd87828b80d403a2d29a4f68f0ea004 Mon Sep 17 00:00:00 2001 From: jo Date: Wed, 29 Mar 2023 16:43:51 +0200 Subject: [PATCH] test(playout): move liq_conn fixture to conftest --- playout/tests/liquidsoap/client/conftest.py | 81 +++++++++++++++++++ .../liquidsoap/client/connection_test.py | 70 ---------------- 2 files changed, 81 insertions(+), 70 deletions(-) create mode 100644 playout/tests/liquidsoap/client/conftest.py diff --git a/playout/tests/liquidsoap/client/conftest.py b/playout/tests/liquidsoap/client/conftest.py new file mode 100644 index 000000000..22b4bd9c1 --- /dev/null +++ b/playout/tests/liquidsoap/client/conftest.py @@ -0,0 +1,81 @@ +import logging +from pathlib import Path +from random import randint +from subprocess import PIPE, STDOUT, Popen +from time import sleep + +import pytest +from libretime_shared.logging import setup_logger + +from libretime_playout.liquidsoap.client import LiquidsoapConnection + +logger = logging.getLogger(__name__) + +setup_logger("debug") + + +LIQ_SCRIPT = """ +set("log.file", false) +{settings} + +var1 = interactive.string("var1", "default") + +output.dummy(blank(id="safe_blank")) +""" + +LIQ_TELNET_SETTINGS = """ +set("server.telnet", true) +set("server.telnet.port", {telnet_port}) +""" + +LIQ_SOCKET_SETTINGS = """ +set("server.socket", true) +set("server.socket.path", "{socket_path}") +""" + + +@pytest.fixture( + name="liq_conn", + scope="session", + params=["telnet", "socket"], +) +def liq_conn_fixture(request, tmp_path_factory): + tmp_path: Path = tmp_path_factory.mktemp(__name__) + + entrypoint = tmp_path / "main.liq" + + if request.param == "telnet": + telnet_port = randint(32768, 65535) + liq_settings = LIQ_TELNET_SETTINGS.format(telnet_port=telnet_port) + elif request.param == "socket": + socket_path = entrypoint.with_name("main.sock") + liq_settings = LIQ_SOCKET_SETTINGS.format(socket_path=socket_path) + + liq_script = LIQ_SCRIPT.format(settings=liq_settings.strip()) + logger.debug(liq_script) + entrypoint.write_text(liq_script) + + # The --verbose flag seem to hang when testing in CI + with Popen( + ("liquidsoap", "--debug", str(entrypoint)), + stdout=PIPE, + stderr=STDOUT, + text=True, + ) as process: + if request.param == "telnet": + sleep(2) + elif request.param == "socket": + while process.poll() is None and not socket_path.is_socket(): + sleep(0.1) + + if process.poll() is not None: + pytest.fail(process.stdout.read()) + + if request.param == "telnet": + conn = LiquidsoapConnection(host="localhost", port=telnet_port) + elif request.param == "socket": + conn = LiquidsoapConnection(path=socket_path) + + with conn: + yield conn + process.terminate() diff --git a/playout/tests/liquidsoap/client/connection_test.py b/playout/tests/liquidsoap/client/connection_test.py index 3e20e84a9..ad02de16a 100644 --- a/playout/tests/liquidsoap/client/connection_test.py +++ b/playout/tests/liquidsoap/client/connection_test.py @@ -1,9 +1,5 @@ import logging -from pathlib import Path -from random import randint -from subprocess import PIPE, STDOUT, Popen from textwrap import dedent -from time import sleep import pytest from libretime_shared.logging import setup_logger @@ -22,72 +18,6 @@ pytestmark = pytest.mark.skipif( reason="unsupported liquidsoap >= 2.0.0", ) -LIQ_SCRIPT = """ -set("log.file", false) -{settings} - -var1 = interactive.string("var1", "default") - -output.dummy(blank(id="safe_blank")) -""" - -LIQ_TELNET_SETTINGS = """ -set("server.telnet", true) -set("server.telnet.port", {telnet_port}) -""" - -LIQ_SOCKET_SETTINGS = """ -set("server.socket", true) -set("server.socket.path", "{socket_path}") -""" - - -@pytest.fixture( - name="liq_conn", - scope="session", - params=["telnet", "socket"], -) -def liq_conn_fixture(request, tmp_path_factory): - tmp_path: Path = tmp_path_factory.mktemp(__name__) - - entrypoint = tmp_path / "main.liq" - - if request.param == "telnet": - telnet_port = randint(32768, 65535) - liq_settings = LIQ_TELNET_SETTINGS.format(telnet_port=telnet_port) - elif request.param == "socket": - socket_path = entrypoint.with_name("main.sock") - liq_settings = LIQ_SOCKET_SETTINGS.format(socket_path=socket_path) - - liq_script = LIQ_SCRIPT.format(settings=liq_settings.strip()) - logger.debug(liq_script) - entrypoint.write_text(liq_script) - - # The --verbose flag seem to hang when testing in CI - with Popen( - ("liquidsoap", "--debug", str(entrypoint)), - stdout=PIPE, - stderr=STDOUT, - text=True, - ) as process: - if request.param == "telnet": - sleep(2) - elif request.param == "socket": - while process.poll() is None and not socket_path.is_socket(): - sleep(0.1) - - if process.poll() is not None: - pytest.fail(process.stdout.read()) - - if request.param == "telnet": - conn = LiquidsoapConnection(host="localhost", port=telnet_port) - elif request.param == "socket": - conn = LiquidsoapConnection(path=socket_path) - - with conn: - yield conn - process.terminate() - def test_liq_conn_version(liq_conn: LiquidsoapConnection): liq_conn.write("version")