|
11 | 11 | # License for the specific language governing permissions and limitations |
12 | 12 | # under the License. |
13 | 13 | from typing import Optional |
| 14 | +from urllib.error import HTTPError |
14 | 15 | from urllib.parse import quote |
| 16 | +from urllib.request import urlopen |
15 | 17 |
|
16 | 18 | from testcontainers.core.container import DockerContainer |
17 | 19 | from testcontainers.core.exceptions import ContainerStartException |
| 20 | +from testcontainers.core.image import DockerImage |
18 | 21 | from testcontainers.core.utils import raise_for_deprecated_parameter |
19 | 22 | from testcontainers.core.waiting_utils import wait_container_is_ready |
20 | 23 |
|
@@ -79,3 +82,67 @@ def _configure(self) -> None: |
79 | 82 |
|
80 | 83 | def _transfer_seed(self) -> None: |
81 | 84 | pass |
| 85 | + |
| 86 | + |
| 87 | +class SrvContainer(DockerContainer): |
| 88 | + """ |
| 89 | + Container for a generic server that is based on a custom image. |
| 90 | +
|
| 91 | + Example: |
| 92 | +
|
| 93 | + .. doctest:: |
| 94 | +
|
| 95 | + >>> import httpx |
| 96 | + >>> from testcontainers.core.generic import SrvContainer |
| 97 | + >>> from testcontainers.core.waiting_utils import wait_for_logs |
| 98 | +
|
| 99 | + >>> with SrvContainer(path="./core/tests/image_fixtures/python_server", port=9000, tag="test-srv:latest") as srv: |
| 100 | + ... url = srv._create_connection_url() |
| 101 | + ... response = httpx.get(f"{url}", timeout=5) |
| 102 | + ... assert response.status_code == 200, "Response status code is not 200" |
| 103 | + ... delay = wait_for_logs(srv, "GET / HTTP/1.1") |
| 104 | +
|
| 105 | +
|
| 106 | + :param path: Path to the Dockerfile to build the image |
| 107 | + :param tag: Tag for the image to be built (default: None) |
| 108 | + """ |
| 109 | + |
| 110 | + def __init__(self, path: str, port: int, tag: Optional[str], image_cleanup: bool = True) -> None: |
| 111 | + self.docker_image = DockerImage(path=path, tag=tag, clean_up=image_cleanup).build() |
| 112 | + super().__init__(str(self.docker_image)) |
| 113 | + self.internal_port = port |
| 114 | + self.with_exposed_ports(self.internal_port) |
| 115 | + |
| 116 | + @wait_container_is_ready(HTTPError) |
| 117 | + def _connect(self) -> None: |
| 118 | + # noinspection HttpUrlsUsage |
| 119 | + url = self._create_connection_url() |
| 120 | + try: |
| 121 | + with urlopen(url) as r: |
| 122 | + assert b"" in r.read() |
| 123 | + except HTTPError as e: |
| 124 | + # 404 is expected, as the server may not have the specific endpoint we are looking for |
| 125 | + if e.code == 404: |
| 126 | + pass |
| 127 | + else: |
| 128 | + raise |
| 129 | + |
| 130 | + def get_api_url(self) -> str: |
| 131 | + raise NotImplementedError |
| 132 | + |
| 133 | + def _create_connection_url(self) -> str: |
| 134 | + if self._container is None: |
| 135 | + raise ContainerStartException("container has not been started") |
| 136 | + host = self.get_container_host_ip() |
| 137 | + exposed_port = self.get_exposed_port(self.internal_port) |
| 138 | + url = f"http://{host}:{exposed_port}" |
| 139 | + return url |
| 140 | + |
| 141 | + def start(self) -> "SrvContainer": |
| 142 | + super().start() |
| 143 | + self._connect() |
| 144 | + return self |
| 145 | + |
| 146 | + def stop(self, force=True, delete_volume=True) -> None: |
| 147 | + super().stop(force, delete_volume) |
| 148 | + self.docker_image.remove() |
0 commit comments