|
10 | 10 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
|
11 | 11 | # License for the specific language governing permissions and limitations
|
12 | 12 | # under the License.
|
13 |
| -from typing import Optional |
| 13 | +from typing import Optional, Union |
| 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 |
|
|
29 | 32 |
|
30 | 33 | class DbContainer(DockerContainer):
|
31 | 34 | """
|
| 35 | + **DEPRECATED (for removal)** |
| 36 | +
|
32 | 37 | Generic database container.
|
33 | 38 | """
|
34 | 39 |
|
@@ -79,3 +84,69 @@ def _configure(self) -> None:
|
79 | 84 |
|
80 | 85 | def _transfer_seed(self) -> None:
|
81 | 86 | pass
|
| 87 | + |
| 88 | + |
| 89 | +class ServerContainer(DockerContainer): |
| 90 | + """ |
| 91 | + **DEPRECATED - will be moved from core to a module (stay tuned for a final/stable import location)** |
| 92 | +
|
| 93 | + Container for a generic server that is based on a custom image. |
| 94 | +
|
| 95 | + Example: |
| 96 | +
|
| 97 | + .. doctest:: |
| 98 | +
|
| 99 | + >>> import httpx |
| 100 | + >>> from testcontainers.core.generic import ServerContainer |
| 101 | + >>> from testcontainers.core.waiting_utils import wait_for_logs |
| 102 | + >>> from testcontainers.core.image import DockerImage |
| 103 | +
|
| 104 | + >>> with DockerImage(path="./core/tests/image_fixtures/python_server", tag="test-srv:latest") as image: |
| 105 | + ... with ServerContainer(port=9000, image=image) as srv: |
| 106 | + ... url = srv._create_connection_url() |
| 107 | + ... response = httpx.get(f"{url}", timeout=5) |
| 108 | + ... assert response.status_code == 200, "Response status code is not 200" |
| 109 | + ... delay = wait_for_logs(srv, "GET / HTTP/1.1") |
| 110 | +
|
| 111 | +
|
| 112 | + :param path: Path to the Dockerfile to build the image |
| 113 | + :param tag: Tag for the image to be built (default: None) |
| 114 | + """ |
| 115 | + |
| 116 | + def __init__(self, port: int, image: Union[str, DockerImage]) -> None: |
| 117 | + super().__init__(str(image)) |
| 118 | + self.internal_port = port |
| 119 | + self.with_exposed_ports(self.internal_port) |
| 120 | + |
| 121 | + @wait_container_is_ready(HTTPError) |
| 122 | + def _connect(self) -> None: |
| 123 | + # noinspection HttpUrlsUsage |
| 124 | + url = self._create_connection_url() |
| 125 | + try: |
| 126 | + with urlopen(url) as r: |
| 127 | + assert b"" in r.read() |
| 128 | + except HTTPError as e: |
| 129 | + # 404 is expected, as the server may not have the specific endpoint we are looking for |
| 130 | + if e.code == 404: |
| 131 | + pass |
| 132 | + else: |
| 133 | + raise |
| 134 | + |
| 135 | + def get_api_url(self) -> str: |
| 136 | + raise NotImplementedError |
| 137 | + |
| 138 | + def _create_connection_url(self) -> str: |
| 139 | + if self._container is None: |
| 140 | + raise ContainerStartException("container has not been started") |
| 141 | + host = self.get_container_host_ip() |
| 142 | + exposed_port = self.get_exposed_port(self.internal_port) |
| 143 | + url = f"http://{host}:{exposed_port}" |
| 144 | + return url |
| 145 | + |
| 146 | + def start(self) -> "ServerContainer": |
| 147 | + super().start() |
| 148 | + self._connect() |
| 149 | + return self |
| 150 | + |
| 151 | + def stop(self, force=True, delete_volume=True) -> None: |
| 152 | + super().stop(force, delete_volume) |
0 commit comments