Skip to content

Commit

Permalink
Fixed linter findings, formatted code
Browse files Browse the repository at this point in the history
  • Loading branch information
max-pfeiffer committed Mar 3, 2024
1 parent eea3b16 commit 13ac882
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 13 deletions.
1 change: 1 addition & 0 deletions index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ testcontainers-python facilitates the use of Docker containers for functional an
modules/qdrant/README
modules/rabbitmq/README
modules/redis/README
modules/registry/README
modules/selenium/README
modules/weaviate/README

Expand Down
21 changes: 12 additions & 9 deletions modules/registry/testcontainers/registry/__init__.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@
import time
from io import BytesIO
from tarfile import TarFile, TarInfo
from typing import Optional
from typing import TYPE_CHECKING, Optional

import bcrypt
from requests import Response, get
from requests import get
from requests.auth import HTTPBasicAuth
from requests.exceptions import ConnectionError, ReadTimeout

from testcontainers.core.container import DockerContainer
from testcontainers.core.waiting_utils import wait_container_is_ready

if TYPE_CHECKING:
from requests import Response


class DockerRegistryContainer(DockerContainer):
# https://docs.docker.com/registry/
credentials_path: str = "/htpasswd/credentials.txt"
Expand All @@ -18,8 +23,8 @@ def __init__(
self,
image: str = "registry:2",
port: int = 5000,
username: str = None,
password: str = None,
username: Optional[str] = None,
password: Optional[str] = None,
**kwargs,
) -> None:
super().__init__(image=image, **kwargs)
Expand All @@ -34,11 +39,9 @@ def _copy_credentials(self) -> None:
self.password.encode("utf-8"),
bcrypt.gensalt(rounds=12, prefix=b"2a"),
).decode("utf-8")
content = f"{self.username}:{hashed_password}".encode("utf-8")
content: bytes = f"{self.username}:{hashed_password}".encode("utf-8") # noqa: UP012

with BytesIO() as tar_archive_object, TarFile(
fileobj=tar_archive_object, mode="w"
) as tmp_tarfile:
with BytesIO() as tar_archive_object, TarFile(fileobj=tar_archive_object, mode="w") as tmp_tarfile:
tarinfo: TarInfo = TarInfo(name=self.credentials_path)
tarinfo.size = len(content)
tarinfo.mtime = time.time()
Expand All @@ -65,7 +68,7 @@ def start(self):
else:
super().start()

self._readiness_probe()
self._readiness_probe()
return self

def get_registry(self) -> str:
Expand Down
9 changes: 5 additions & 4 deletions modules/registry/tests/test_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@


REGISTRY_USERNAME: str = "foo"
REGISTRY_PASSWORD: str ="bar"
REGISTRY_PASSWORD: str = "bar"


def test_registry():
with DockerRegistryContainer().with_bind_ports(5000, 5000) as registry_container:
Expand All @@ -16,9 +17,9 @@ def test_registry():


def test_registry_with_authentication():
with DockerRegistryContainer(
username=REGISTRY_USERNAME, password=REGISTRY_PASSWORD
).with_bind_ports(5000, 5000) as registry_container:
with DockerRegistryContainer(username=REGISTRY_USERNAME, password=REGISTRY_PASSWORD).with_bind_ports(
5000, 5000
) as registry_container:
url: str = f"http://{registry_container.get_registry()}/v2/_catalog"

response: Response = get(url, auth=HTTPBasicAuth(REGISTRY_USERNAME, REGISTRY_PASSWORD))
Expand Down

0 comments on commit 13ac882

Please sign in to comment.