From 4bc798ed4b3a9dd54d478ee4759b27c63f6faaf0 Mon Sep 17 00:00:00 2001 From: 2mal3 <56305732+2mal3@users.noreply.github.com> Date: Sat, 22 Feb 2025 17:09:11 +0100 Subject: [PATCH] test: use fixtures for runtime dependencies --- tests/test_logic/test_main.py | 74 +++++++++++++++++++++-------------- 1 file changed, 44 insertions(+), 30 deletions(-) diff --git a/tests/test_logic/test_main.py b/tests/test_logic/test_main.py index e6c2213..98b301a 100644 --- a/tests/test_logic/test_main.py +++ b/tests/test_logic/test_main.py @@ -1,40 +1,54 @@ import asyncio import podman +import pytest from podman.domain.containers import Container -from somnus.logic import start +from somnus.logic import start, stop from somnus.config import Config +@pytest.fixture(scope="session", autouse=True) +def podman_setup(): + client = podman.PodmanClient() + + container: Container = client.containers.run( + "somnus-test", detach=True, ports={"22/tcp": 25566, "25565/tcp": 25565} + ) # type: ignore + if not client.ping(): + raise Exception("Podman service is not running") + container.wait(condition="running") + + yield + + #container.stop() + #container.remove() + + client.close() + + def test_main(): - with podman.PodmanClient() as client: - if not client.ping(): - raise Exception("Podman service is not running") - - container: Container = client.containers.run( - "somnus-test", detach=True, ports={"22/tcp": 25566, "25565/tcp": 25565} - ) - container.wait(condition="running") - - asyncio.run(run_server()) - - container.stop() - container.remove() - - -async def run_server(): - async for _ in start.start_server( - Config( - DISCORD_TOKEN="", - HOST_SERVER_HOST="localhost", - HOST_SERVER_SSH_PORT=25566, - HOST_SERVER_USER="root", - HOST_SERVER_PASSWORD="root", - MC_SERVER_START_CMD="./start.sh", - MC_SERVER_ADDRESS="25565", - DISCORD_SUPER_USER_ID="", - DEBUG=True, - ) - ): + config = Config( + DISCORD_TOKEN="", + HOST_SERVER_HOST="localhost", + HOST_SERVER_SSH_PORT=25566, + HOST_SERVER_USER="root", + HOST_SERVER_PASSWORD="root", + MC_SERVER_START_CMD="./start.sh", + MC_SERVER_ADDRESS="25565", + DISCORD_SUPER_USER_ID="", + DEBUG=True, + ) + + asyncio.run(start_server(config)) + asyncio.run(stop_server(config)) + + +async def start_server(config: Config): + async for _ in start.start_server(config): + pass + + +async def stop_server(config: Config): + async for _ in stop.stop_server(True, config): pass