import os import shutil import subprocess import time import urllib.request import pytest def podman_available(): return shutil.which("podman") is not None def wait_for_http(url, timeout=30): end = time.time() + timeout last_exc = None while time.time() < end: try: with urllib.request.urlopen(url, timeout=3) as r: body = r.read().decode('utf-8', errors='ignore') return r.getcode(), body except Exception as e: last_exc = e time.sleep(0.5) raise RuntimeError(f"HTTP check failed after {timeout}s: {last_exc}") @pytest.mark.skipif(not podman_available(), reason="Podman is not available on this runner") @pytest.mark.integration def test_build_run_and_cleanup_podman(): image_tag = "scenar-creator:latest" container_name = "scenar-creator-test" port = int(os.environ.get('SCENAR_TEST_PORT', '8080')) # Ensure podman machine is running (macOS/Windows) if not subprocess.run(["podman", "machine", "info"], capture_output=True).returncode == 0: print("Starting podman machine...") subprocess.run(["podman", "machine", "start"], check=True) time.sleep(2) # Build image subprocess.run(["podman", "build", "-t", image_tag, "."], check=True) # Ensure no leftover container subprocess.run(["podman", "rm", "-f", container_name], check=False) try: # Run container subprocess.run([ "podman", "run", "-d", "--name", container_name, "-p", f"{port}:8080", image_tag ], check=True) # Wait for HTTP and verify content code, body = wait_for_http(f"http://127.0.0.1:{port}/") assert code == 200 assert "Vytvoření Scénáře" in body or "Scenar" in body or "Vytvoření" in body finally: # Cleanup container and image subprocess.run(["podman", "rm", "-f", container_name], check=False) subprocess.run(["podman", "rmi", image_tag], check=False)