Skip to content

Commit 4b57208

Browse files
alexanderankinTranquility2
authored andcommitted
clean up tests, warnings, linting
1 parent 2bfc865 commit 4b57208

File tree

7 files changed

+54
-32
lines changed

7 files changed

+54
-32
lines changed

core/testcontainers/core/config.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,10 @@ class TestcontainersConfiguration:
6161

6262
@property
6363
def docker_auth_config(self):
64-
if "DOCKER_AUTH_CONFIG" in _WARNINGS:
64+
config = self._docker_auth_config
65+
if config and "DOCKER_AUTH_CONFIG" in _WARNINGS:
6566
warning(_WARNINGS.pop("DOCKER_AUTH_CONFIG"))
66-
return self._docker_auth_config
67+
return config
6768

6869
@docker_auth_config.setter
6970
def docker_auth_config(self, value: str):

core/testcontainers/core/generic.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -94,16 +94,15 @@ class ServerContainer(DockerContainer):
9494
9595
Example:
9696
97-
todo re-enable this test:
98-
.. code-block::
97+
.. doctest::
9998
10099
>>> import httpx
101100
>>> from testcontainers.core.generic import ServerContainer
102101
>>> from testcontainers.core.waiting_utils import wait_for_logs
103102
>>> from testcontainers.core.image import DockerImage
104103
105104
>>> with DockerImage(path="./core/tests/image_fixtures/python_server", tag="test-srv:latest") as image:
106-
>>> with ServerContainer(port=9000, image=image) as srv:
105+
... with ServerContainer(port=9000, image=image) as srv:
107106
... url = srv._create_connection_url()
108107
... response = httpx.get(f"{url}", timeout=5)
109108
... assert response.status_code == 200, "Response status code is not 200"

core/testcontainers/core/image.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
from typing import TYPE_CHECKING, Optional
1+
from os import PathLike
2+
from typing import TYPE_CHECKING, Optional, Union
23

34
from typing_extensions import Self
45

@@ -28,7 +29,7 @@ class DockerImage:
2829

2930
def __init__(
3031
self,
31-
path: str,
32+
path: Union[str, PathLike],
3233
docker_client_kw: Optional[dict] = None,
3334
tag: Optional[str] = None,
3435
clean_up: bool = True,
@@ -45,7 +46,7 @@ def __init__(
4546
def build(self, **kwargs) -> Self:
4647
logger.info(f"Building image from {self.path}")
4748
docker_client = self.get_docker_client()
48-
self._image, self._logs = docker_client.build(path=self.path, tag=self.tag, **kwargs)
49+
self._image, self._logs = docker_client.build(path=str(self.path), tag=self.tag, **kwargs)
4950
logger.info(f"Built image {self.short_id} with tag {self.tag}")
5051
return self
5152

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
FROM python:3
1+
FROM python:3-alpine
22
EXPOSE 9000
33
CMD ["python", "-m", "http.server", "9000"]

core/tests/test_generics.py

Lines changed: 30 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,31 @@
1-
import pytest
1+
import re
2+
from pathlib import Path
23
from typing import Optional
4+
5+
import pytest
6+
from httpx import get
7+
8+
from testcontainers.core.waiting_utils import wait_for_logs
9+
from testcontainers.core.image import DockerImage
310
from testcontainers.core.generic import ServerContainer
411

5-
import re
12+
TEST_DIR = Path(__file__).parent
613

714

815
@pytest.mark.parametrize("test_image_cleanup", [True, False])
916
@pytest.mark.parametrize("test_image_tag", [None, "custom-image:test"])
1017
def test_srv_container(test_image_tag: Optional[str], test_image_cleanup: bool, check_for_image, port=9000):
11-
with ServerContainer(
12-
path="./core/tests/image_fixtures/python_server",
13-
port=port,
14-
tag=test_image_tag,
15-
image_cleanup=test_image_cleanup,
16-
) as srv:
17-
image_short_id = srv.docker_image.short_id
18-
image_build_logs = srv.docker_image.get_logs()
18+
with (
19+
DockerImage(
20+
path=TEST_DIR / "image_fixtures/python_server",
21+
tag=test_image_tag,
22+
clean_up=test_image_cleanup,
23+
#
24+
) as docker_image,
25+
ServerContainer(port=port, image=docker_image) as srv,
26+
):
27+
image_short_id = docker_image.short_id
28+
image_build_logs = docker_image.get_logs()
1929
# check if dict is in any of the logs
2030
assert {"stream": f"Step 2/3 : EXPOSE {port}"} in image_build_logs, "Image logs mismatch"
2131
assert (port, None) in srv.ports.items(), "Port mismatch"
@@ -25,3 +35,13 @@ def test_srv_container(test_image_tag: Optional[str], test_image_cleanup: bool,
2535
assert re.match(r"http://localhost:\d+", test_url), "Connection URL mismatch"
2636

2737
check_for_image(image_short_id, test_image_cleanup)
38+
39+
40+
def test_like_doctest():
41+
with DockerImage(path=TEST_DIR / "image_fixtures/python_server", tag="test-srv:latest") as image:
42+
with ServerContainer(port=9000, image=image) as srv:
43+
url = srv._create_connection_url()
44+
response = get(f"{url}", timeout=5)
45+
assert response.status_code == 200, "Response status code is not 200"
46+
delay = wait_for_logs(srv, "GET / HTTP/1.1")
47+
print(delay)

poetry.lock

Lines changed: 4 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

pyproject.toml

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -140,19 +140,20 @@ mypy = "1.7.1"
140140
pre-commit = "^3.6"
141141
pytest = "7.4.3"
142142
pytest-cov = "4.1.0"
143-
sphinx = "^7.2.6"
144-
twine = "^4.0.2"
145-
anyio = "^4.3.0"
143+
sphinx = "7.2.6"
144+
twine = "4.0.2"
145+
anyio = "4.3.0"
146146
# for tests only
147-
psycopg2-binary = "*"
148-
pg8000 = "*"
149-
sqlalchemy = "*"
150-
psycopg = "*"
151-
cassandra-driver = "*"
147+
psycopg2-binary = "2.9.9"
148+
pg8000 = "1.30.5"
149+
sqlalchemy = "2.0.28"
150+
psycopg = "3.1.18"
151+
cassandra-driver = "3.29.1"
152152
pytest-asyncio = "0.23.5"
153153
kafka-python-ng = "^2.2.0"
154-
hvac = "*"
154+
hvac = "2.1.0"
155155
pymilvus = "2.4.3"
156+
httpx = "0.27.0"
156157

157158
[[tool.poetry.source]]
158159
name = "PyPI"

0 commit comments

Comments
 (0)