|
| 1 | +import time |
| 2 | +import requests |
| 3 | +from urllib.parse import urljoin |
| 4 | +from urllib3.util.retry import Retry |
| 5 | +from requests.adapters import HTTPAdapter |
| 6 | + |
| 7 | +import pytest |
| 8 | + |
| 9 | +pytest_plugins = ["docker_compose"] |
| 10 | + |
| 11 | + |
| 12 | +@pytest.fixture(scope="module") |
| 13 | +def wait_for_api(module_scoped_container_getter): |
| 14 | + """Wait for the api from my_api_service to become responsive""" |
| 15 | + request_session = requests.Session() |
| 16 | + retries = Retry(total=5, |
| 17 | + backoff_factor=0.1, |
| 18 | + status_forcelist=[500, 502, 503, 504]) |
| 19 | + request_session.mount('http://', HTTPAdapter(max_retries=retries)) |
| 20 | + |
| 21 | + service = module_scoped_container_getter.get("my_api_service").network_info[0] |
| 22 | + api_url = "http://%s:%s/" % (service.hostname, service.host_port) |
| 23 | + assert request_session.get(api_url) |
| 24 | + |
| 25 | + start = time.time() |
| 26 | + while 'Exit' not in module_scoped_container_getter.get("my_short_lived_service").human_readable_state: |
| 27 | + if time.time() - start >= 5: |
| 28 | + raise RuntimeError( |
| 29 | + 'my_short_lived_service should spin up, echo "Echoing" and ' |
| 30 | + 'then shut down, since it still running something went wrong' |
| 31 | + ) |
| 32 | + time.sleep(.5) |
| 33 | + |
| 34 | + return request_session, api_url |
| 35 | + |
| 36 | + |
| 37 | +@pytest.mark.compose_file_path |
| 38 | +def test_read_all(wait_for_api): |
| 39 | + request_session, api_url = wait_for_api |
| 40 | + assert len(request_session.get(urljoin(api_url, 'items/all')).json()) == 0 |
| 41 | + |
| 42 | + |
| 43 | +if __name__ == '__main__': |
| 44 | + pytest.main(['-m', 'compose_file_path', '--docker-compose', './my_network/docker-compose.yml', '--docker-compose-no-build']) |
0 commit comments