|
| 1 | +import os |
| 2 | +import random |
| 3 | +import shutil |
| 4 | +import string |
| 5 | +import subprocess |
| 6 | +import time |
| 7 | +from dataclasses import dataclass |
| 8 | +from pathlib import Path |
| 9 | +from tempfile import TemporaryDirectory |
| 10 | +from test.conftest import ramalama_container_engine |
| 11 | + |
| 12 | +import pytest |
| 13 | + |
| 14 | + |
| 15 | +@dataclass |
| 16 | +class Registry: |
| 17 | + username: str |
| 18 | + password: str |
| 19 | + url: str |
| 20 | + host: str |
| 21 | + port: int |
| 22 | + |
| 23 | + |
| 24 | +@pytest.fixture(scope="function") |
| 25 | +def container_registry(): |
| 26 | + registry_name = f"pytest-registry-{''.join(random.choices(string.ascii_letters + string.digits, k=5))}" |
| 27 | + registry_port = random.randint(64000, 65000) |
| 28 | + registry_username = "test_user" |
| 29 | + registry_password = "test_password" |
| 30 | + registry_image = "{}/{}/registry:2.8.2".format( |
| 31 | + os.environ.get('PODMAN_TEST_IMAGE_REGISTRY', 'quay.io'), os.environ.get('PODMAN_TEST_IMAGE_USER', 'libpod') |
| 32 | + ) |
| 33 | + |
| 34 | + with TemporaryDirectory() as temp_dir: |
| 35 | + work_dir = Path(temp_dir) |
| 36 | + htpasswd_file = work_dir / "htpasswd" |
| 37 | + trusted_certs_dir = work_dir / "trusted-registry-cert-dir" |
| 38 | + trusted_certs_dir.mkdir() |
| 39 | + |
| 40 | + # Generate Certificates |
| 41 | + subprocess.run( |
| 42 | + f"openssl req -newkey rsa:4096 -nodes -sha256 " |
| 43 | + f"-keyout {work_dir.as_posix()}/domain.key -x509 -days 2 " |
| 44 | + f"-out {work_dir.as_posix()}/domain.crt " |
| 45 | + f"-subj \"/C=US/ST=Foo/L=Bar/O=Red Hat, Inc./CN=localhost\" " |
| 46 | + "-addext \"subjectAltName=DNS:localhost\"", |
| 47 | + shell=True, |
| 48 | + check=True, |
| 49 | + stderr=subprocess.DEVNULL, |
| 50 | + stdout=subprocess.DEVNULL, |
| 51 | + ) |
| 52 | + |
| 53 | + # Copy domain.crt to trusted_certs_dir |
| 54 | + shutil.copy(work_dir / "domain.crt", trusted_certs_dir) |
| 55 | + |
| 56 | + # Create htpasswd file |
| 57 | + subprocess.run( |
| 58 | + f"htpasswd -Bbn {registry_username} {registry_password} > {htpasswd_file.as_posix()}", |
| 59 | + shell=True, |
| 60 | + check=True, |
| 61 | + ) |
| 62 | + |
| 63 | + # Start the registry |
| 64 | + subprocess.run( |
| 65 | + [ |
| 66 | + # fmt: off |
| 67 | + ramalama_container_engine, "run", "-d", "--rm", |
| 68 | + "--name", registry_name, |
| 69 | + "-p", f"{registry_port}:5000", |
| 70 | + "-v", f"{work_dir.as_posix()}:/auth:Z", |
| 71 | + "-e", "REGISTRY_AUTH=htpasswd", |
| 72 | + "-e", "REGISTRY_AUTH_HTPASSWD_REALM='Registry Realm'", |
| 73 | + "-e", "REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd", |
| 74 | + "-e", "REGISTRY_HTTP_TLS_CERTIFICATE=/auth/domain.crt", |
| 75 | + "-e", "REGISTRY_HTTP_TLS_KEY=/auth/domain.key", |
| 76 | + registry_image, |
| 77 | + # fmt: on |
| 78 | + ], |
| 79 | + check=True, |
| 80 | + ) |
| 81 | + time.sleep(2) |
| 82 | + |
| 83 | + try: |
| 84 | + yield Registry( |
| 85 | + username=registry_username, |
| 86 | + password=registry_password, |
| 87 | + url=f"oci://localhost:{registry_port}", |
| 88 | + host="localhost", |
| 89 | + port=registry_port, |
| 90 | + ) |
| 91 | + finally: |
| 92 | + subprocess.run([ramalama_container_engine, "stop", registry_name], check=False) |
0 commit comments