Skip to content

chore(main): project fixes and updates #807

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ repos:
- id: end-of-file-fixer

- repo: https://github.com/astral-sh/ruff-pre-commit
rev: 'v0.3.5'
rev: 'v0.11.5'
hooks:
- id: ruff
# Explicitly setting config to prevent Ruff from using `pyproject.toml` in sub packages.
Expand Down
6 changes: 3 additions & 3 deletions core/testcontainers/compose/__init__.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# flake8: noqa
# flake8: noqa: F401
from testcontainers.compose.compose import (
ComposeContainer,
ContainerIsNotRunning,
DockerCompose,
NoSuchPortExposed,
PublishedPort,
ComposeContainer,
DockerCompose,
)
2 changes: 1 addition & 1 deletion core/testcontainers/compose/compose.py
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ def get_config(
config_cmd.append("--no-interpolate")

cmd_output = self._run_command(cmd=config_cmd).stdout
return cast(dict[str, Any], loads(cmd_output))
return cast(dict[str, Any], loads(cmd_output)) # noqa: TC006

def get_containers(self, include_all=False) -> list[ComposeContainer]:
"""
Expand Down
22 changes: 10 additions & 12 deletions core/testcontainers/core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,11 @@ class ConnectionMode(Enum):
@property
def use_mapped_port(self) -> bool:
"""
Return true if we need to use mapped port for this connection
Return True if mapped ports should be used for this connection mode.
This is true for everything but bridge mode.
Mapped ports are used for all connection modes except 'bridge_ip'.
"""
if self == self.bridge_ip:
return False
return True
return self != ConnectionMode.bridge_ip


def get_docker_socket() -> str:
Expand Down Expand Up @@ -137,15 +135,15 @@ def timeout(self) -> int:
testcontainers_config = TestcontainersConfiguration()

__all__ = [
# the public API of this module
"testcontainers_config",
# and all the legacy things that are deprecated:
# Legacy things that are deprecated:
"MAX_TRIES",
"SLEEP_TIME",
"TIMEOUT",
"RYUK_IMAGE",
"RYUK_PRIVILEGED",
"RYUK_DISABLED",
"RYUK_DOCKER_SOCKET",
"RYUK_IMAGE",
"RYUK_PRIVILEGED",
"RYUK_RECONNECTION_TIMEOUT",
"SLEEP_TIME",
"TIMEOUT",
# Public API of this module:
"testcontainers_config",
]
7 changes: 5 additions & 2 deletions core/testcontainers/core/docker_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@
"""
port_mappings = self.client.api.port(container_id, port)
if not port_mappings:
raise ConnectionError(f"Port mapping for container {container_id} and port {port} is " "not available")
raise ConnectionError(f"Port mapping for container {container_id} and port {port} is not available")

Check warning on line 165 in core/testcontainers/core/docker_client.py

View check run for this annotation

Codecov / codecov/patch

core/testcontainers/core/docker_client.py#L165

Added line #L165 was not covered by tests
return port_mappings[0]["HostPort"]

def get_container(self, container_id: str) -> Container:
Expand Down Expand Up @@ -233,7 +233,10 @@
url = urllib.parse.urlparse(self.client.api.base_url)
except ValueError:
return "localhost"
if "http" in url.scheme or "tcp" in url.scheme and url.hostname:

is_http_scheme = "http" in url.scheme
is_tcp_scheme_with_hostname = "tcp" in url.scheme and url.hostname
if is_http_scheme or is_tcp_scheme_with_hostname:
# see https://github.com/testcontainers/testcontainers-python/issues/415
if url.hostname == "localnpipe" and utils.is_windows():
return "localhost"
Expand Down
2 changes: 1 addition & 1 deletion core/testcontainers/core/waiting_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ def wait_for_logs(
if predicate_result:
return duration
if duration > timeout:
raise TimeoutError(f"Container did not emit logs satisfying predicate in {timeout:.3f} " "seconds")
raise TimeoutError(f"Container did not emit logs satisfying predicate in {timeout:.3f} seconds")
if raise_on_exit:
wrapped.reload()
if wrapped.status not in _NOT_EXITED_STATUSES:
Expand Down
2 changes: 1 addition & 1 deletion core/testcontainers/socat/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
# flake8: noqa
# flake8: noqa: F401
from testcontainers.socat.socat import SocatContainer
2 changes: 1 addition & 1 deletion core/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@ def _check_for_image(image_short_id: str, cleaned: bool) -> None:
client = DockerClient()
images = client.client.images.list()
found = any(image.short_id.endswith(image_short_id) for image in images)
assert found is not cleaned, f'Image {image_short_id} was {"found" if cleaned else "not found"}'
assert found is not cleaned, f"Image {image_short_id} was {'found' if cleaned else 'not found'}"

return _check_for_image
2 changes: 1 addition & 1 deletion core/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ def fake_cgroup(monkeypatch: pytest.MonkeyPatch, tmp_path: Path) -> Path:
def test_get_running_container_id_empty_or_missing(fake_cgroup: Path) -> None:
# non existing does not fail but is only none
assert utils.get_running_in_container_id() is None
fake_cgroup.write_text("12:devices:/system.slice/sshd.service\n" "13:cpuset:\n")
fake_cgroup.write_text("12:devices:/system.slice/sshd.service\n13:cpuset:\n")
# missing docker does also not fail
assert utils.get_running_in_container_id() is None

Expand Down
16 changes: 5 additions & 11 deletions modules/azurite/testcontainers/azurite/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ def __init__(
self.account_name = account_name or os.environ.get("AZURITE_ACCOUNT_NAME", "devstoreaccount1")
self.account_key = account_key or os.environ.get(
"AZURITE_ACCOUNT_KEY",
"Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/" "K1SZFPTOtr/KBHBeksoGMGw==",
"Eby8vdM02xNOcqFlqUwJPLlmEtlCDXJ1OUzFT50uSRZ6IFsuFq2UVErCz4I6tq/K1SZFPTOtr/KBHBeksoGMGw==",
)

raise_for_deprecated_parameter(kwargs, "ports_to_expose", "container.with_exposed_ports")
Expand All @@ -76,28 +76,22 @@ def __init__(
def get_connection_string(self) -> str:
host_ip = self.get_container_host_ip()
connection_string = (
f"DefaultEndpointsProtocol=http;" f"AccountName={self.account_name};" f"AccountKey={self.account_key};"
f"DefaultEndpointsProtocol=http;AccountName={self.account_name};AccountKey={self.account_key};"
)

if self.blob_service_port in self.ports:
connection_string += (
f"BlobEndpoint=http://{host_ip}:"
f"{self.get_exposed_port(self.blob_service_port)}"
f"/{self.account_name};"
f"BlobEndpoint=http://{host_ip}:{self.get_exposed_port(self.blob_service_port)}/{self.account_name};"
)

if self.queue_service_port in self.ports:
connection_string += (
f"QueueEndpoint=http://{host_ip}:"
f"{self.get_exposed_port(self.queue_service_port)}"
f"/{self.account_name};"
f"QueueEndpoint=http://{host_ip}:{self.get_exposed_port(self.queue_service_port)}/{self.account_name};"
)

if self.table_service_port in self.ports:
connection_string += (
f"TableEndpoint=http://{host_ip}:"
f"{self.get_exposed_port(self.table_service_port)}"
f"/{self.account_name};"
f"TableEndpoint=http://{host_ip}:{self.get_exposed_port(self.table_service_port)}/{self.account_name};"
)

return connection_string
Expand Down
2 changes: 1 addition & 1 deletion modules/generic/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,6 @@ def _check_for_image(image_short_id: str, cleaned: bool) -> None:
client = DockerClient()
images = client.client.images.list()
found = any(image.short_id.endswith(image_short_id) for image in images)
assert found is not cleaned, f'Image {image_short_id} was {"found" if cleaned else "not found"}'
assert found is not cleaned, f"Image {image_short_id} was {'found' if cleaned else 'not found'}"

return _check_for_image
6 changes: 3 additions & 3 deletions modules/google/tests/test_google.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,9 @@ def test_datastore_container_isolation():

# Create a second container and try to fetch the entity to makesure its a different container
with DatastoreContainer() as datastore2:
assert (
datastore.get_datastore_emulator_host() != datastore2.get_datastore_emulator_host()
), "Datastore containers use the same port."
assert datastore.get_datastore_emulator_host() != datastore2.get_datastore_emulator_host(), (
"Datastore containers use the same port."
)
client2 = datastore2.get_datastore_client()
fetched_entity2 = client2.get(key)
assert fetched_entity2 is None, "Entity was found in the datastore."
2 changes: 1 addition & 1 deletion modules/k3s/testcontainers/k3s/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,6 @@ def config_yaml(self) -> str:
execution = self.get_wrapped_container().exec_run(["cat", "/etc/rancher/k3s/k3s.yaml"])
config_yaml = execution.output.decode("utf-8").replace(
f"https://127.0.0.1:{self.KUBE_SECURE_PORT}",
f"https://{self.get_container_host_ip()}:" f"{self.get_exposed_port(self.KUBE_SECURE_PORT)}",
f"https://{self.get_container_host_ip()}:{self.get_exposed_port(self.KUBE_SECURE_PORT)}",
)
return config_yaml
2 changes: 1 addition & 1 deletion modules/mqtt/testcontainers/mqtt/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def __init__(
super().__init__(image, **kwargs)
# self.password = password
# reusable client context:
self.client: Optional["Client"] = None
self.client: Optional["Client"] = None # noqa: UP037

@wait_container_is_ready()
def get_client(self) -> "Client":
Expand Down
4 changes: 3 additions & 1 deletion modules/opensearch/testcontainers/opensearch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
from testcontainers.core.utils import raise_for_deprecated_parameter
from testcontainers.core.waiting_utils import wait_container_is_ready

MIN_REQUIRED_INITIAL_ADMIN_PASSWORD = [2, 12, 0]


class OpenSearchContainer(DockerContainer):
"""
Expand Down Expand Up @@ -65,7 +67,7 @@ def __init__(

def _supports_initial_admin_password(self, image: str) -> bool:
with suppress(Exception):
return [int(n) for n in image.split(":")[-1].split(".")] >= [int(n) for n in "2.12.0".split(".")]
return [int(n) for n in image.split(":")[-1].split(".")] >= MIN_REQUIRED_INITIAL_ADMIN_PASSWORD
return False

def get_config(self) -> dict:
Expand Down
3 changes: 1 addition & 2 deletions modules/scylla/tests/test_scylla.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,7 @@ def test_docker_run_scylla():
cluster = scylla.get_cluster()
with cluster.connect() as session:
session.execute(
"CREATE KEYSPACE keyspace1 WITH replication = "
"{'class': 'SimpleStrategy', 'replication_factor': '1'};"
"CREATE KEYSPACE keyspace1 WITH replication = {'class': 'SimpleStrategy', 'replication_factor': '1'};"
)
session.execute("CREATE TABLE keyspace1.table1 (key1 int, key2 int, PRIMARY KEY (key1));")
session.execute("INSERT INTO keyspace1.table1 (key1,key2) values (1,2);")
Expand Down
Loading