forked from testcontainers/testcontainers-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_server.py
More file actions
53 lines (43 loc) · 1.98 KB
/
test_server.py
File metadata and controls
53 lines (43 loc) · 1.98 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import re
from pathlib import Path
from typing import Optional
import pytest
from httpx import get
from testcontainers.core.waiting_utils import wait_for_logs
from testcontainers.core.image import DockerImage
from testcontainers.generic import ServerContainer
TEST_DIR = Path(__file__).parent
@pytest.mark.parametrize("test_image_cleanup", [True, False])
@pytest.mark.parametrize("test_image_tag", [None, "custom-image:test"])
def test_server_container(test_image_tag: Optional[str], test_image_cleanup: bool, check_for_image, port=9000):
with (
DockerImage(
path=TEST_DIR / "samples/python_server",
tag=test_image_tag,
clean_up=test_image_cleanup,
#
) as docker_image,
ServerContainer(port=port, image=docker_image) as srv,
):
image_short_id = docker_image.short_id
image_build_logs = docker_image.get_logs()
# check if dict is in any of the logs
assert {"stream": f"Step 2/3 : EXPOSE {port}"} in image_build_logs, "Image logs mismatch"
assert (port, None) in srv.ports.items(), "Port mismatch"
with pytest.raises(NotImplementedError):
srv.get_api_url()
test_url = srv._create_connection_url()
assert re.match(r"http://localhost:\d+", test_url), "Connection URL mismatch"
check_for_image(image_short_id, test_image_cleanup)
def test_server_container_no_port():
with pytest.raises(TypeError):
with ServerContainer(path="./modules/generic/tests/samples/python_server", tag="test-srv:latest"):
pass
def test_like_doctest():
with DockerImage(path=TEST_DIR / "samples/python_server", tag="test-srv:latest") as image:
with ServerContainer(port=9000, image=image) as srv:
url = srv._create_connection_url()
response = get(f"{url}", timeout=5)
assert response.status_code == 200, "Response status code is not 200"
delay = wait_for_logs(srv, "GET / HTTP/1.1")
print(delay)