Skip to content

Commit

Permalink
Transitions to use pytest-docker for managed services (fixes)
Browse files Browse the repository at this point in the history
  • Loading branch information
stumpylog committed Oct 9, 2024
1 parent 01501be commit d95afff
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 47 deletions.
8 changes: 8 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ def webserver_service_name() -> str:
return "nginx-webserver"


@pytest.fixture(scope="session")
def webserver_docker_internal_url(webserver_service_name: str) -> str:
"""
The URL by which Gotenberg can access the webserver
"""
return f"http://{webserver_service_name}"


@pytest.fixture(scope="session")
def gotenberg_host(docker_services, docker_ip: str, gotenberg_service_name: str) -> str:
url = f"http://{docker_ip}:{docker_services.port_for(gotenberg_service_name, 3000)}"
Expand Down
7 changes: 6 additions & 1 deletion tests/docker/docker-compose.ci-test-edge.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
# 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

networks:
gotenberg-test-edge-net:
services:
gotenberg-client-test-edge-server:
image: docker.io/gotenberg/gotenberg:edge
networks:
- gotenberg-test-edge-net
ports:
- "3000/tcp"
command:
Expand All @@ -13,6 +16,8 @@ services:
- "--log-format=text"
nginx-webserver-edge:
image: docker.io/nginx:1-alpine
networks:
- gotenberg-test-edge-net
ports:
- "80/tcp"
volumes:
Expand Down
6 changes: 6 additions & 0 deletions tests/docker/docker-compose.ci-test.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
# 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
networks:
gotenberg-test-net:
services:
gotenberg-client-test-edge-server:
image: docker.io/gotenberg/gotenberg:8.11.0
networks:
- gotenberg-test-net
ports:
- "3000/tcp"
command:
Expand All @@ -12,6 +16,8 @@ services:
- "--log-format=text"
nginx-webserver:
image: docker.io/nginx:1-alpine
networks:
- gotenberg-test-net
ports:
- "80/tcp"
volumes:
Expand Down
45 changes: 23 additions & 22 deletions tests/test_convert_chromium_screenshots.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,11 @@
from gotenberg_client import GotenbergClient


@pytest.mark.usefixtures("web_server_host")
class TestChromiumScreenshots:
def test_basic_screenshot(self, client: GotenbergClient, web_server_host: str):
def test_basic_screenshot(self, client: GotenbergClient, webserver_docker_internal_url: str):
with client.chromium.screenshot_url() as route:
resp = route.url(web_server_host).run_with_retry()
resp = route.url(webserver_docker_internal_url).run_with_retry()

assert resp.status_code == codes.OK
assert "Content-Type" in resp.headers
Expand All @@ -26,83 +27,83 @@ def test_basic_screenshot(self, client: GotenbergClient, web_server_host: str):
def test_screenshot_formats(
self,
client: GotenbergClient,
web_server_host: str,
webserver_docker_internal_url: str,
image_format: Literal["png", "webp", "jpeg"],
):
with client.chromium.screenshot_url() as route:
resp = route.url(web_server_host).output_format(image_format).run_with_retry()
resp = route.url(webserver_docker_internal_url).output_format(image_format).run_with_retry()

assert resp.status_code == codes.OK
assert "Content-Type" in resp.headers
assert resp.headers["Content-Type"] == f"image/{image_format}"

def test_screenshot_quality_valid(self, client: GotenbergClient, web_server_host: str):
def test_screenshot_quality_valid(self, client: GotenbergClient, webserver_docker_internal_url: str):
with client.chromium.screenshot_url() as route:
resp = route.url(web_server_host).quality(80).run_with_retry()
resp = route.url(webserver_docker_internal_url).quality(80).run_with_retry()

assert resp.status_code == codes.OK
assert "Content-Type" in resp.headers
assert resp.headers["Content-Type"] == "image/png"

def test_screenshot_quality_too_low(self, client: GotenbergClient, web_server_host: str):
def test_screenshot_quality_too_low(self, client: GotenbergClient, webserver_docker_internal_url: str):
with client.chromium.screenshot_url() as route:
resp = route.url(web_server_host).quality(-10).run_with_retry()
resp = route.url(webserver_docker_internal_url).quality(-10).run_with_retry()

assert resp.status_code == codes.OK
assert "Content-Type" in resp.headers
assert resp.headers["Content-Type"] == "image/png"

def test_screenshot_quality_too_high(self, client: GotenbergClient, web_server_host: str):
def test_screenshot_quality_too_high(self, client: GotenbergClient, webserver_docker_internal_url: str):
with client.chromium.screenshot_url() as route:
resp = route.url(web_server_host).quality(101).run_with_retry()
resp = route.url(webserver_docker_internal_url).quality(101).run_with_retry()

assert resp.status_code == codes.OK
assert "Content-Type" in resp.headers
assert resp.headers["Content-Type"] == "image/png"

def test_screenshot_optimize_speed(self, client: GotenbergClient, web_server_host: str):
def test_screenshot_optimize_speed(self, client: GotenbergClient, webserver_docker_internal_url: str):
with client.chromium.screenshot_url() as route:
resp = route.url(web_server_host).optimize_speed().run_with_retry()
resp = route.url(webserver_docker_internal_url).optimize_speed().run_with_retry()

assert resp.status_code == codes.OK
assert "Content-Type" in resp.headers
assert resp.headers["Content-Type"] == "image/png"

def test_screenshot_optimize_quality(self, client: GotenbergClient, web_server_host: str):
def test_screenshot_optimize_quality(self, client: GotenbergClient, webserver_docker_internal_url: str):
with client.chromium.screenshot_url() as route:
resp = route.url(web_server_host).optimize_size().run_with_retry()
resp = route.url(webserver_docker_internal_url).optimize_size().run_with_retry()

assert resp.status_code == codes.OK
assert "Content-Type" in resp.headers
assert resp.headers["Content-Type"] == "image/png"

def test_network_idle_on(self, client: GotenbergClient, web_server_host: str):
def test_network_idle_on(self, client: GotenbergClient, webserver_docker_internal_url: str):
with client.chromium.screenshot_url() as route:
resp = route.url(web_server_host).skip_network_idle().run_with_retry()
resp = route.url(webserver_docker_internal_url).skip_network_idle().run_with_retry()

assert resp.status_code == codes.OK
assert "Content-Type" in resp.headers
assert resp.headers["Content-Type"] == "image/png"

def test_network_idle_off(self, client: GotenbergClient, web_server_host: str):
def test_network_idle_off(self, client: GotenbergClient, webserver_docker_internal_url: str):
with client.chromium.screenshot_url() as route:
resp = route.url(web_server_host).use_network_idle().run_with_retry()
resp = route.url(webserver_docker_internal_url).use_network_idle().run_with_retry()

assert resp.status_code == codes.OK
assert "Content-Type" in resp.headers
assert resp.headers["Content-Type"] == "image/png"

def test_status_codes(self, client: GotenbergClient, web_server_host: str):
def test_status_codes(self, client: GotenbergClient, webserver_docker_internal_url: str):
with client.chromium.screenshot_url() as route:
resp = route.url(web_server_host).fail_on_status_codes([499, 599]).run_with_retry()
resp = route.url(webserver_docker_internal_url).fail_on_status_codes([499, 599]).run_with_retry()

assert resp.status_code == codes.OK
assert "Content-Type" in resp.headers
assert resp.headers["Content-Type"] == "image/png"

def test_status_codes_empty(self, client: GotenbergClient, web_server_host: str):
def test_status_codes_empty(self, client: GotenbergClient, webserver_docker_internal_url: str):
with client.chromium.screenshot_url() as route:
resp = route.url(web_server_host).fail_on_status_codes([]).run_with_retry()
resp = route.url(webserver_docker_internal_url).fail_on_status_codes([]).run_with_retry()

assert resp.status_code == codes.OK
assert "Content-Type" in resp.headers
Expand Down
50 changes: 26 additions & 24 deletions tests/test_convert_chromium_url.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,18 @@
from tests.utils import verify_stream_contains


@pytest.mark.usefixtures("webserver_docker_internal_url")
class TestConvertChromiumUrlRoute:
def test_basic_convert(self, client: GotenbergClient, web_server_host: str):
def test_basic_convert(self, client: GotenbergClient, webserver_docker_internal_url: str):
with client.chromium.url_to_pdf() as route:
resp = route.url(web_server_host).run_with_retry()
resp = route.url(webserver_docker_internal_url).run_with_retry()

assert resp.status_code == codes.OK
assert "Content-Type" in resp.headers
assert resp.headers["Content-Type"] == "application/pdf"


@pytest.mark.usefixtures("webserver_docker_internal_url")
class TestConvertChromiumUrlMocked:
@pytest.mark.parametrize(
("emulation"),
Expand All @@ -30,14 +32,14 @@ class TestConvertChromiumUrlMocked:
def test_convert_orientation(
self,
client: GotenbergClient,
web_server_host: str,
webserver_docker_internal_url: str,
httpx_mock: HTTPXMock,
emulation: EmulatedMediaType,
):
httpx_mock.add_response(method="POST")

with client.chromium.url_to_pdf() as route:
_ = route.url(web_server_host).media_type(emulation).run()
_ = route.url(webserver_docker_internal_url).media_type(emulation).run()

request = httpx_mock.get_request()
verify_stream_contains(
Expand All @@ -53,14 +55,14 @@ def test_convert_orientation(
def test_convert_css_or_not_size(
self,
client: GotenbergClient,
web_server_host: str,
webserver_docker_internal_url: str,
httpx_mock: HTTPXMock,
method: str,
):
httpx_mock.add_response(method="POST")

with client.chromium.url_to_pdf() as route:
route.url(web_server_host)
route.url(webserver_docker_internal_url)
getattr(route, method)()
_ = route.run()

Expand All @@ -78,14 +80,14 @@ def test_convert_css_or_not_size(
def test_convert_background_graphics_or_not(
self,
client: GotenbergClient,
web_server_host: str,
webserver_docker_internal_url: str,
httpx_mock: HTTPXMock,
method: str,
):
httpx_mock.add_response(method="POST")

with client.chromium.url_to_pdf() as route:
route.url(web_server_host)
route.url(webserver_docker_internal_url)
getattr(route, method)()
_ = route.run()

Expand All @@ -103,14 +105,14 @@ def test_convert_background_graphics_or_not(
def test_convert_hide_background_or_not(
self,
client: GotenbergClient,
web_server_host: str,
webserver_docker_internal_url: str,
httpx_mock: HTTPXMock,
method: str,
):
httpx_mock.add_response(method="POST")

with client.chromium.url_to_pdf() as route:
route.url(web_server_host)
route.url(webserver_docker_internal_url)
getattr(route, method)()
_ = route.run()

Expand All @@ -128,14 +130,14 @@ def test_convert_hide_background_or_not(
def test_convert_fail_exceptions(
self,
client: GotenbergClient,
web_server_host: str,
webserver_docker_internal_url: str,
httpx_mock: HTTPXMock,
method: str,
):
httpx_mock.add_response(method="POST")

with client.chromium.url_to_pdf() as route:
route.url(web_server_host)
route.url(webserver_docker_internal_url)
getattr(route, method)()
_ = route.run()

Expand All @@ -149,13 +151,13 @@ def test_convert_fail_exceptions(
def test_convert_scale(
self,
client: GotenbergClient,
web_server_host: str,
webserver_docker_internal_url: str,
httpx_mock: HTTPXMock,
):
httpx_mock.add_response(method="POST")

with client.chromium.url_to_pdf() as route:
_ = route.url(web_server_host).scale(1.5).run()
_ = route.url(webserver_docker_internal_url).scale(1.5).run()

request = httpx_mock.get_request()
verify_stream_contains(
Expand All @@ -167,13 +169,13 @@ def test_convert_scale(
def test_convert_page_ranges(
self,
client: GotenbergClient,
web_server_host: str,
webserver_docker_internal_url: str,
httpx_mock: HTTPXMock,
):
httpx_mock.add_response(method="POST")

with client.chromium.url_to_pdf() as route:
_ = route.url(web_server_host).page_ranges("1-5").run()
_ = route.url(webserver_docker_internal_url).page_ranges("1-5").run()

request = httpx_mock.get_request()
verify_stream_contains(
Expand All @@ -185,13 +187,13 @@ def test_convert_page_ranges(
def test_convert_url_render_wait(
self,
client: GotenbergClient,
web_server_host: str,
webserver_docker_internal_url: str,
httpx_mock: HTTPXMock,
):
httpx_mock.add_response(method="POST")

with client.chromium.url_to_pdf() as route:
_ = route.url(web_server_host).render_wait(500).run()
_ = route.url(webserver_docker_internal_url).render_wait(500).run()

request = httpx_mock.get_request()
verify_stream_contains(
Expand All @@ -203,13 +205,13 @@ def test_convert_url_render_wait(
def test_convert_url_render_expression(
self,
client: GotenbergClient,
web_server_host: str,
webserver_docker_internal_url: str,
httpx_mock: HTTPXMock,
):
httpx_mock.add_response(method="POST")

with client.chromium.url_to_pdf() as route:
_ = route.url(web_server_host).render_expr("wait while false;").run()
_ = route.url(webserver_docker_internal_url).render_expr("wait while false;").run()

request = httpx_mock.get_request()
verify_stream_contains(
Expand All @@ -222,13 +224,13 @@ def test_convert_url_render_expression(
def test_convert_url_user_agent(
self,
client: GotenbergClient,
web_server_host: str,
webserver_docker_internal_url: str,
httpx_mock: HTTPXMock,
):
httpx_mock.add_response(method="POST")

with client.chromium.url_to_pdf() as route:
_ = route.url(web_server_host).user_agent("Firefox").run()
_ = route.url(webserver_docker_internal_url).user_agent("Firefox").run()

request = httpx_mock.get_request()
verify_stream_contains(
Expand All @@ -240,15 +242,15 @@ def test_convert_url_user_agent(
def test_convert_url_headers(
self,
client: GotenbergClient,
web_server_host: str,
webserver_docker_internal_url: str,
httpx_mock: HTTPXMock,
):
httpx_mock.add_response(method="POST")

headers = {"X-Auth-Token": "Secure"}

with client.chromium.url_to_pdf() as route:
_ = route.url(web_server_host).headers(headers).run()
_ = route.url(webserver_docker_internal_url).headers(headers).run()

request = httpx_mock.get_request()
verify_stream_contains(
Expand Down

0 comments on commit d95afff

Please sign in to comment.