diff --git a/packages/pytest-simcore/src/pytest_simcore/httpbin_service.py b/packages/pytest-simcore/src/pytest_simcore/httpbin_service.py index ad44de57602..5e764d10923 100644 --- a/packages/pytest-simcore/src/pytest_simcore/httpbin_service.py +++ b/packages/pytest-simcore/src/pytest_simcore/httpbin_service.py @@ -8,31 +8,36 @@ from contextlib import suppress from typing import Iterable +import aiohttp.test_utils import docker import pytest import requests import requests.exceptions from docker.errors import APIError -from pydantic import HttpUrl +from pydantic import HttpUrl, parse_obj_as from tenacity import retry from tenacity.after import after_log from tenacity.retry import retry_if_exception_type from tenacity.stop import stop_after_delay from tenacity.wait import wait_fixed +from .helpers.utils_docker import get_localhost_ip + @pytest.fixture(scope="session") def httpbin_base_url() -> Iterable[HttpUrl]: - """Implemented since https://httpbin.org/ is not always available""" - - port = 80 - base_url = f"http://127.0.0.1:{port}" + """ + Implemented cannot rely on https://httpbin.org/ being always available + """ + ip_address = get_localhost_ip() + port = aiohttp.test_utils.unused_port() + base_url = f"http://{ip_address}:{port}" client = docker.from_env() container_name = "httpbin-fixture" try: client.containers.run( - "kennethreitz/httpbin", + image="kennethreitz/httpbin", ports={port: 80}, name=container_name, detach=True, @@ -45,7 +50,7 @@ def httpbin_base_url() -> Iterable[HttpUrl]: after=after_log(logging.getLogger(), logging.DEBUG), ) def _wait_until_httpbin_is_responsive(): - r = requests.get(f"{base_url}/get") + r = requests.get(f"{base_url}/get", timeout=2) r.raise_for_status() _wait_until_httpbin_is_responsive()