Skip to content

Commit 78b6f0e

Browse files
authored
chore(core): Adds integration testing to the private registry auth feature (DOCKER_AUTH_CONFIG) (#582)
Follow up on #566 - Testing using the registry module
1 parent 8fe2d6d commit 78b6f0e

File tree

1 file changed

+83
-0
lines changed

1 file changed

+83
-0
lines changed

core/tests/test_registry.py

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
"""Integration test using login to a private registry.
2+
3+
Note: Using the testcontainers-python library to test the Docker registry.
4+
This could be considered a bad practice as it is not recommended to use the same library to test itself.
5+
However, it is a very good use case for DockerRegistryContainer and allows us to test it thoroughly.
6+
"""
7+
8+
import json
9+
import os
10+
import base64
11+
import pytest
12+
13+
from docker.errors import NotFound
14+
15+
from testcontainers.core.config import testcontainers_config
16+
from testcontainers.core.container import DockerContainer
17+
from testcontainers.core.docker_client import DockerClient
18+
from testcontainers.core.waiting_utils import wait_container_is_ready
19+
20+
from testcontainers.registry import DockerRegistryContainer
21+
22+
23+
def test_missing_on_private_registry(monkeypatch):
24+
username = "user"
25+
password = "pass"
26+
image = "hello-world"
27+
tag = "test"
28+
29+
with DockerRegistryContainer(username=username, password=password) as registry:
30+
registry_url = registry.get_registry()
31+
32+
# prepare auth config
33+
creds: bytes = base64.b64encode(f"{username}:{password}".encode("utf-8"))
34+
config = {"auths": {f"{registry_url}": {"auth": creds.decode("utf-8")}}}
35+
monkeypatch.setattr(testcontainers_config, name="docker_auth_config", value=json.dumps(config))
36+
assert testcontainers_config.docker_auth_config, "docker_auth_config not set"
37+
38+
with pytest.raises(NotFound):
39+
# Test a container with image from private registry
40+
with DockerContainer(f"{registry_url}/{image}:{tag}") as test_container:
41+
wait_container_is_ready(test_container)
42+
43+
44+
@pytest.mark.parametrize(
45+
"image,tag,username,password",
46+
[
47+
("nginx", "test", "user", "pass"),
48+
("hello-world", "latest", "new_user", "new_pass"),
49+
("alpine", "3.12", None, None),
50+
],
51+
)
52+
def test_with_private_registry(image, tag, username, password, monkeypatch):
53+
client = DockerClient().client
54+
55+
with DockerRegistryContainer(username=username, password=password) as registry:
56+
registry_url = registry.get_registry()
57+
58+
# prepare image
59+
_image = client.images.pull(image)
60+
assert _image.tag(repository=f"{registry_url}/{image}", tag=tag), "Image not tagged"
61+
62+
# login to private registry
63+
client.login(registry=registry_url, username=username, password=password)
64+
65+
# push image to private registry
66+
client.images.push(f"{registry_url}/{image}")
67+
68+
# clear local image so we will pull from private registry
69+
client.images.remove(f"{registry_url}/{image}:{tag}")
70+
71+
# prepare auth config
72+
creds: bytes = base64.b64encode(f"{username}:{password}".encode("utf-8"))
73+
config = {"auths": {f"{registry_url}": {"auth": creds.decode("utf-8")}}}
74+
monkeypatch.setattr(testcontainers_config, name="docker_auth_config", value=json.dumps(config))
75+
assert testcontainers_config.docker_auth_config, "docker_auth_config not set"
76+
77+
# Test a container with image from private registry
78+
with DockerContainer(f"{registry_url}/{image}:{tag}") as test_container:
79+
wait_container_is_ready(test_container)
80+
81+
# cleanup
82+
client.images.remove(f"{registry_url}/{image}:{tag}")
83+
client.close()

0 commit comments

Comments
 (0)