Skip to content
Closed
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
8 changes: 5 additions & 3 deletions core/testcontainers/core/docker_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,11 @@ def login(self, docker_auth_config: str) -> None:
"""
Login to a docker registry using the given auth config.
"""
auth_config = parse_docker_auth_config(docker_auth_config)[0] # Only using the first auth config
login_info = self.client.login(**auth_config._asdict())
LOGGER.debug(f"logged in using {login_info}")
auth_config = parse_docker_auth_config(docker_auth_config)
if auth_config:
first_auth_config = auth_config[0] # Only using the first auth config
login_info = self.client.login(**first_auth_config._asdict())
LOGGER.debug(f"logged in using {login_info}")

def client_networks_create(self, name: str, param: dict):
labels = create_labels("", param.get("labels"))
Expand Down
2 changes: 1 addition & 1 deletion core/testcontainers/core/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ def parse_docker_auth_config(auth_config: str) -> list[DockerAuthInfo]:
"""
auth_info: list[DockerAuthInfo] = []
try:
auth_config_dict: dict = json.loads(auth_config).get("auths")
auth_config_dict: dict = json.loads(auth_config).get("auths", {})
for registry, auth in auth_config_dict.items():
auth_str = auth.get("auth")
auth_str = base64.b64decode(auth_str).decode("utf-8")
Expand Down
19 changes: 18 additions & 1 deletion core/tests/test_docker_client.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

from testcontainers.core.config import testcontainers_config as c
from testcontainers.core.container import DockerContainer
from testcontainers.core.docker_client import DockerClient
from testcontainers.core.docker_client import DockerClient, get_docker_auth_config
from testcontainers.core.utils import parse_docker_auth_config
from testcontainers.core.image import DockerImage

Expand Down Expand Up @@ -48,6 +48,23 @@ def test_docker_client_login():
mock_docker.from_env.return_value.login.assert_called_with(**{"value": "test"})


def test_docker_client_no_login_when_no_auths():
mock_docker = MagicMock(spec=docker)
mock_get_docker_auth_config = MagicMock(spec=get_docker_auth_config)
mock_get_docker_auth_config.return_value = (
'{"credHelpers":{"<aws_account_id>.dkr.ecr.<region>.amazonaws.com": "ecr-login"}}'
)

with (
mock.patch.object(c, "_docker_auth_config", "test"),
patch("testcontainers.core.docker_client.docker", mock_docker),
patch("testcontainers.core.docker_client.get_docker_auth_config", mock_get_docker_auth_config),
):
DockerClient()

mock_docker.from_env.return_value.login.assert_not_called()


def test_container_docker_client_kw():
test_kwargs = {"test_kw": "test_value"}
mock_docker = MagicMock(spec=docker)
Expand Down
7 changes: 7 additions & 0 deletions core/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,10 @@ def test_parse_docker_auth_config_multiple():
username="abc",
password="123",
)


def test_parse_docker_auth_config_with_no_auths():
auth_dict = {"credHelpers": {"<aws_account_id>.dkr.ecr.<region>.amazonaws.com": "ecr-login"}}
auth_config_json = json.dumps(auth_dict)
auth_info = parse_docker_auth_config(auth_config_json)
assert len(auth_info) == 0