-
Notifications
You must be signed in to change notification settings - Fork 372
Expand file tree
/
Copy pathserver.py
More file actions
70 lines (51 loc) · 2.38 KB
/
server.py
File metadata and controls
70 lines (51 loc) · 2.38 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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from typing import Union
import httpx
from testcontainers.core.container import DockerContainer
from testcontainers.core.exceptions import ContainerStartException
from testcontainers.core.image import DockerImage
from testcontainers.core.wait_strategies import HttpWaitStrategy
# This comment can be removed (Used for testing)
class ServerContainer(DockerContainer):
"""
Container for a generic server that is based on a custom image.
Example:
.. doctest::
>>> import httpx
>>> from testcontainers.generic import ServerContainer
>>> from testcontainers.core.waiting_utils import wait_for_logs
>>> from testcontainers.core.image import DockerImage
>>> with DockerImage(path="./modules/generic/tests/samples/python_server", tag="test-srv:latest") as image:
... with ServerContainer(port=9000, image=image) as srv:
... url = srv._create_connection_url()
... response = httpx.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")
:param port: Port to be exposed on the container.
:param image: Docker image to be used for the container.
"""
def __init__(self, port: int, image: Union[str, DockerImage]) -> None:
super().__init__(str(image))
self.internal_port = port
self.with_exposed_ports(self.internal_port)
def _connect(self) -> None:
strategy = HttpWaitStrategy(self.internal_port).for_status_code(404)
strategy.wait_until_ready(self)
def get_api_url(self) -> str:
raise NotImplementedError
def _create_connection_url(self) -> str:
if self._container is None:
raise ContainerStartException("container has not been started")
host = self.get_container_host_ip()
exposed_port = self.get_exposed_port(self.internal_port)
url = f"http://{host}:{exposed_port}"
return url
def start(self) -> "ServerContainer":
super().start()
self._connect()
return self
def stop(self, force=True, delete_volume=True) -> None:
super().stop(force, delete_volume)
def get_client(self) -> httpx.Client:
return httpx.Client(base_url=self.get_api_url())
def get_stdout(self) -> str:
return self.get_logs()[0].decode("utf-8")