Skip to content

Commit

Permalink
Transitions to use pytest-docker for managed services
Browse files Browse the repository at this point in the history
  • Loading branch information
stumpylog committed Oct 9, 2024
1 parent 0d5215c commit bc9bcce
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 56 deletions.
32 changes: 2 additions & 30 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,6 @@ jobs:
steps:
-
uses: actions/checkout@v4
-
name: Start containers
run: |
docker compose --file ${GITHUB_WORKSPACE}/.docker/docker-compose.ci-test.yml pull --quiet
docker compose --file ${GITHUB_WORKSPACE}/.docker/docker-compose.ci-test.yml up --detach
echo "Wait for container to be started"
sleep 5
docker inspect gotenberg-client-test-server
-
name: Install poppler-utils
run: |
Expand All @@ -97,20 +89,13 @@ jobs:
name: Run tests
run: |
hatch test --cover --python ${{ matrix.python-version }}
ls -ahl .
-
name: Upload coverage to Codecov
if: matrix.python-version == '3.10'
uses: codecov/codecov-action@v4
with:
# not required for public repos, but intermittently fails otherwise
token: ${{ secrets.CODECOV_TOKEN }}
-
name: Stop containers
if: always()
run: |
docker compose --file ${GITHUB_WORKSPACE}/.docker/docker-compose.ci-test.yml logs
docker compose --file ${GITHUB_WORKSPACE}/.docker/docker-compose.ci-test.yml down

test-edge:
name: Test Gotenberg :edge
Expand All @@ -119,17 +104,11 @@ jobs:
contents: read
needs:
- lint
env:
GOTENBERG_CLIENT_EDGE_TEST: 1
steps:
-
uses: actions/checkout@v4
-
name: Start containers
run: |
docker compose --file ${GITHUB_WORKSPACE}/.docker/docker-compose.ci-test-edge.yml pull --quiet
docker compose --file ${GITHUB_WORKSPACE}/.docker/docker-compose.ci-test-edge.yml up --detach
echo "Wait for container to be started"
sleep 5
docker inspect gotenberg-client-test-edge-server
-
name: Install poppler-utils
run: |
Expand All @@ -154,13 +133,6 @@ jobs:
name: Run tests
run: |
hatch test --cover --python 3.11
ls -ahl .
-
name: Stop containers
if: always()
run: |
docker compose --file ${GITHUB_WORKSPACE}/.docker/docker-compose.ci-test-edge.yml logs
docker compose --file ${GITHUB_WORKSPACE}/.docker/docker-compose.ci-test-edge.yml down
build:
name: Build
Expand Down
1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ extra-dependencies = [
"pytest-httpx ~= 0.22; python_version < '3.9'",
"pikepdf",
"python-magic",
"pytest-docker ~= 3.1",
]
extra-args = [ "--maxprocesses=8", "--pythonwarnings=all" ]

Expand Down
70 changes: 66 additions & 4 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,83 @@
from typing import Generator
from typing import Union

import httpx
import pytest

from gotenberg_client import GotenbergClient
from gotenberg_client import SingleFileResponse
from gotenberg_client import ZipFileResponse

logger = logging.getLogger("gotenberg-client.tests")


def is_responsive(url):
try:
response = httpx.get(url)
except httpx.HTTPError:
logger.exception("Error connecting to service")
return False
else:
return response.status_code == httpx.codes.OK


@pytest.fixture(scope="session")
def docker_compose_file() -> Path:
if "GOTENBERG_CLIENT_EDGE_TEST" in os.environ:
return Path(__file__).parent / "docker" / "docker-compose.ci-test-edge.yml"
else:
return Path(__file__).parent / "docker" / "docker-compose.ci-test.yml"


@pytest.fixture(scope="session")
def gotenberg_service_name() -> str:
if "GOTENBERG_CLIENT_EDGE_TEST" in os.environ:
return "gotenberg-client-test-edge-server"
else:
return "gotenberg-client-test-edge-server"


@pytest.fixture(scope="session")
def webserver_service_name() -> str:
if "GOTENBERG_CLIENT_EDGE_TEST" in os.environ:
return "nginx-webserver-edge"
else:
return "nginx-webserver"


@pytest.fixture(scope="session")
def gotenberg_host() -> str:
return os.getenv("GOTENBERG_URL", "http://localhost:3000")
def gotenberg_host(docker_services, docker_ip: str, gotenberg_service_name: str) -> str:
def is_responsive(url):
import httpx

try:
response = httpx.get(url)
except httpx.HTTPError:
logger.exception("Error connecting to service")
return False
else:
return response.status_code == httpx.codes.OK

url = f"http://{docker_ip}:{docker_services.port_for(gotenberg_service_name, 3000)}"

docker_services.wait_until_responsive(
timeout=30.0,
pause=1,
check=lambda: is_responsive(url),
)
return url


@pytest.fixture(scope="session")
def web_server_host() -> str:
return os.getenv("WEBSERVER_HOST", "http://localhost:8888")
def web_server_host(docker_services, docker_ip: str, webserver_service_name: str) -> str:
url = f"http://{docker_ip}:{docker_services.port_for(webserver_service_name, 80)}"

docker_services.wait_until_responsive(
timeout=30.0,
pause=1,
check=lambda: is_responsive(url),
)
return url


@pytest.fixture(scope="session")
Expand Down
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,24 +2,19 @@
# Can be used locally or by the CI to start the necessary container with the
# correct networking for the tests

version: "3"
services:
gotenberg-client-test-edge-server:
image: docker.io/gotenberg/gotenberg:edge
hostname: gotenberg-client-test-edge-server
container_name: gotenberg-client-test-edge-server
network_mode: host
restart: unless-stopped
ports:
- "3000/tcp"
command:
- "gotenberg"
- "--log-level=info"
- "--log-format=text"
nginx-webserver:
nginx-webserver-edge:
image: docker.io/nginx:1-alpine
hostname: nginx-webserver
container_name: nginx-webserver
ports:
- "8888:80"
- "80/tcp"
restart: unless-stopped
volumes:
- ./content:/usr/share/nginx/html:ro
Original file line number Diff line number Diff line change
@@ -1,27 +1,19 @@
# docker-compose file for running testing with gotenberg container
# Can be used locally or by the CI to start the necessary container with the
# correct networking for the tests

version: "3"
services:
gotenberg-client-test-server:
gotenberg-client-test-edge-server:
image: docker.io/gotenberg/gotenberg:8.11.0
hostname: gotenberg-client-test-server
container_name: gotenberg-client-test-server
network_mode: host
restart: unless-stopped
ports:
- "3000/tcp"
command:
- "gotenberg"
- "--log-level=warn"
- "--log-level=info"
- "--log-format=text"
nginx-webserver:
image: docker.io/nginx:1-alpine
hostname: nginx-webserver
container_name: nginx-webserver
ports:
- "8888:80"
- "80/tcp"
restart: unless-stopped
environment:
NGINX_ENTRYPOINT_QUIET_LOGS: 1
volumes:
- ./content:/usr/share/nginx/html:ro

0 comments on commit bc9bcce

Please sign in to comment.