|
| 1 | +import logging |
| 2 | +import time |
| 3 | + |
| 4 | +import httpx |
| 5 | +import pytest |
| 6 | +from typer.testing import CliRunner |
| 7 | + |
| 8 | +from ptah.cli import app |
| 9 | +from ptah.clients import Shell, get |
| 10 | +from ptah.models import OperatingSystem |
| 11 | + |
| 12 | +log = logging.getLogger(__name__) |
| 13 | + |
| 14 | +# https://stackoverflow.com/a/38609243 |
| 15 | +pytestmark = pytest.mark.e2e |
| 16 | + |
| 17 | +# https://stackoverflow.com/a/71264963 |
| 18 | +if get(OperatingSystem) != OperatingSystem.LINUX: |
| 19 | + pytest.skip(reason="unsupported", allow_module_level=True) |
| 20 | + |
| 21 | + |
| 22 | +@pytest.mark.parametrize("in_project", ["project-with-fastapi"], indirect=True) |
| 23 | +def test_build(in_project): |
| 24 | + runner = CliRunner() |
| 25 | + result = runner.invoke(app, ["build"]) |
| 26 | + assert result.exit_code == 0 |
| 27 | + assert "Building 1 Docker image" in result.stdout |
| 28 | + assert "Copying 1 manifest" in result.stdout |
| 29 | + |
| 30 | + |
| 31 | +@pytest.mark.parametrize("in_project", ["project-with-fastapi"], indirect=True) |
| 32 | +@pytest.mark.filterwarnings("ignore::pytest.PytestUnraisableExceptionWarning") |
| 33 | +def test_deploy(in_project): |
| 34 | + runner = CliRunner() |
| 35 | + result = runner.invoke(app, ["deploy"]) |
| 36 | + assert result.exit_code == 0, result.stdout |
| 37 | + |
| 38 | + |
| 39 | +@pytest.mark.timeout(20) |
| 40 | +def test_deployed_service_is_functional(): |
| 41 | + # Poor man's external liveness probe. |
| 42 | + success = False |
| 43 | + while not success: |
| 44 | + try: |
| 45 | + response = httpx.get("http://localhost:8000/probe") |
| 46 | + assert response.is_success |
| 47 | + assert "headers" in response.json() |
| 48 | + success = True |
| 49 | + except Exception as e: |
| 50 | + log.warning(e) |
| 51 | + |
| 52 | + time.sleep(1) |
| 53 | + |
| 54 | + |
| 55 | +@pytest.mark.parametrize("in_project", ["project-with-fastapi"], indirect=True) |
| 56 | +def test_nuke(in_project): |
| 57 | + runner = CliRunner() |
| 58 | + result = runner.invoke(app, ["nuke"]) |
| 59 | + assert result.exit_code == 0, result.stdout |
| 60 | + |
| 61 | + assert not get(Shell).run(["kind", "get", "clusters"]) |
0 commit comments