Skip to content

Commit

Permalink
[pre-commit.ci] auto fixes from pre-commit.com hooks
Browse files Browse the repository at this point in the history
for more information, see https://pre-commit.ci
  • Loading branch information
pre-commit-ci[bot] committed Feb 28, 2025
1 parent aa948e0 commit ad29ffd
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 25 deletions.
76 changes: 52 additions & 24 deletions tests/norun/test_registry.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@
import shutil
import socket
import subprocess
from tempfile import TemporaryDirectory
from base64 import b64encode
import time
import bcrypt
from base64 import b64encode
from pathlib import Path
from tempfile import TemporaryDirectory

import bcrypt
import pytest
import requests

Expand Down Expand Up @@ -87,7 +87,9 @@ def registry(host_ip):
port = get_free_port()
username = "user"
password = secrets.token_hex(16)
bcrypted_pw = bcrypt.hashpw(password.encode("utf-8"), bcrypt.gensalt(rounds=12)).decode("utf-8")
bcrypted_pw = bcrypt.hashpw(
password.encode("utf-8"), bcrypt.gensalt(rounds=12)
).decode("utf-8")

# We put our password here, and mount it into the container.
# put it in current dir than in /tmp because on macos, current dir is likely to
Expand All @@ -100,12 +102,22 @@ def registry(host_ip):
registry_image = "registry:3.0.0-rc.3"
subprocess.check_call(["docker", "pull", registry_image])

cmd = ["docker", "run", "--rm",
"-e", "REGISTRY_AUTH=htpasswd",
"-e", "REGISTRY_AUTH_HTPASSWD_REALM=basic",
"-e", "REGISTRY_AUTH_HTPASSWD_PATH=/opt/htpasswd/htpasswd.conf",
"--mount", f"type=bind,src={htpasswd_dir},dst=/opt/htpasswd",
"-p", f"{port}:5000", registry_image]
cmd = [
"docker",
"run",
"--rm",
"-e",
"REGISTRY_AUTH=htpasswd",
"-e",
"REGISTRY_AUTH_HTPASSWD_REALM=basic",
"-e",
"REGISTRY_AUTH_HTPASSWD_PATH=/opt/htpasswd/htpasswd.conf",
"--mount",
f"type=bind,src={htpasswd_dir},dst=/opt/htpasswd",
"-p",
f"{port}:5000",
registry_image,
]
proc = subprocess.Popen(cmd)
health_url = f"http://{host_ip}:{port}/v2"
# Wait for the registry to actually come up
Expand Down Expand Up @@ -144,19 +156,24 @@ def test_registry_explicit_creds(registry, dind):
os.environ["DOCKER_HOST"] = docker_host
os.environ["DOCKER_CERT_PATH"] = str(cert_dir / "client")
os.environ["DOCKER_TLS_VERIFY"] = "1"
os.environ["CONTAINER_ENGINE_REGISTRY_CREDENTIALS"] = json.dumps({
"registry": f"http://{registry_host}",
"username": username,
"password": password
})
os.environ["CONTAINER_ENGINE_REGISTRY_CREDENTIALS"] = json.dumps(
{
"registry": f"http://{registry_host}",
"username": username,
"password": password,
}
)
r2d.start()


proc = subprocess.run(["docker", "manifest", "inspect", "--insecure", image_name])
proc = subprocess.run(
["docker", "manifest", "inspect", "--insecure", image_name]
)
assert proc.returncode == 0

# Validate that we didn't leak our registry creds into existing docker config
docker_config_path = Path(os.environ.get("DOCKER_CONFIG", "~/.docker/config.json")).expanduser()
docker_config_path = Path(
os.environ.get("DOCKER_CONFIG", "~/.docker/config.json")
).expanduser()
if docker_config_path.exists():
# Just check that our randomly generated password is not in this file
# Can this cause a conflict? Sure, if there's a different randomly generated password in here
Expand Down Expand Up @@ -184,15 +201,26 @@ def test_registry_no_explicit_creds(registry, dind):
os.environ["DOCKER_CERT_PATH"] = str(cert_dir / "client")
os.environ["DOCKER_TLS_VERIFY"] = "1"
with TemporaryDirectory() as d:
(Path(d) / "config.json").write_text(json.dumps(
({"auths":{f"http://{registry_host}":{"auth":b64encode(f"{username}:{password}".encode()).decode()}}})
))
(Path(d) / "config.json").write_text(
json.dumps(
{
"auths": {
f"http://{registry_host}": {
"auth": b64encode(
f"{username}:{password}".encode()
).decode()
}
}
}
)
)
os.environ["DOCKER_CONFIG"] = d
r2d.start()


proc = subprocess.run(["docker", "manifest", "inspect", "--insecure", image_name])
proc = subprocess.run(
["docker", "manifest", "inspect", "--insecure", image_name]
)
assert proc.returncode == 0
finally:
os.environ.clear()
os.environ.update(old_environ)
os.environ.update(old_environ)
1 change: 0 additions & 1 deletion tests/unit/test_argumentvalidation.py
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,6 @@ def test_invalid_container_port_protocol_mapping_fail(temp_cwd):
assert not validate_arguments(builddir, args_list, "Port specification")



def test_docker_no_build_success(temp_cwd):
"""
Test to check if r2d succeeds with --no-build argument with not being able to connect to docker daemon
Expand Down

0 comments on commit ad29ffd

Please sign in to comment.