diff --git a/tests/test_docker_build.py b/tests/test_docker_build.py new file mode 100644 index 00000000..bc788c74 --- /dev/null +++ b/tests/test_docker_build.py @@ -0,0 +1,89 @@ +# tests/test_docker_build.py +import os +import shutil +import subprocess +import time + +import pytest +import requests + + +IMAGE_NAME = "manual_slop:test" +CONTAINER_NAME = "manual_slop_test_container" +WEB_PORT = 18080 +HOOK_PORT = 18999 + + +@pytest.mark.docker +def test_docker_image_builds(tmp_path): + """Build the Docker image. Slow; opt-in.""" + if os.environ.get("RUN_DOCKER_TEST") != "1": + pytest.skip("Set RUN_DOCKER_TEST=1 to enable") + if not _docker_available(): + pytest.skip("Docker not available in this environment") + + repo_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + result = subprocess.run( + ["docker", "build", "-t", IMAGE_NAME, "."], + cwd=repo_root, + capture_output=True, text=True, timeout=600, + ) + assert result.returncode == 0, f"Docker build failed: {result.stderr}" + + +@pytest.mark.docker +def test_docker_container_starts_and_responds(): + """Run the container, verify web and hook endpoints respond.""" + if os.environ.get("RUN_DOCKER_TEST") != "1": + pytest.skip("Set RUN_DOCKER_TEST=1 to enable") + if not _docker_available(): + pytest.skip("Docker not available in this environment") + + _cleanup_container() + + repo_root = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + result = subprocess.run( + [ + "docker", "run", "-d", + "--name", CONTAINER_NAME, + "-p", f"{WEB_PORT}:8080", + "-p", f"{HOOK_PORT}:8999", + IMAGE_NAME, + ], + cwd=repo_root, + capture_output=True, text=True, timeout=30, + ) + assert result.returncode == 0, f"Docker run failed: {result.stderr}" + + try: + start = time.time() + ready = False + while time.time() - start < 90: + try: + r = requests.get(f"http://127.0.0.1:{HOOK_PORT}/status", timeout=1) + if r.status_code == 200: + ready = True + break + except (requests.ConnectionError, requests.Timeout): + pass + time.sleep(1) + assert ready, "Container hook API did not respond within 90s" + + r = requests.get(f"http://127.0.0.1:{WEB_PORT}/", timeout=5) + assert r.status_code == 200 + body = r.content.lower() + assert b" bool: + return shutil.which("docker") is not None + + +def _cleanup_container() -> None: + subprocess.run( + ["docker", "rm", "-f", CONTAINER_NAME], + capture_output=True, + ) \ No newline at end of file