Skip to content

Commit 54605ce

Browse files
committed
Add e2e pytest test for serve command
- Migrate to e2e pytest the `test/system/040-serve.bats` bat test. - Create a container registry fixture to test interactions with private registries. - The tests cover a wide range of functionalities, including: - Validation of various CLI arguments and flags. - Generation of Quadlet and Kubernetes files. - Serving with different APIs and RAG. - Error handling for invalid inputs. Signed-off-by: Roberto Majadas <[email protected]>
1 parent 499b2bc commit 54605ce

File tree

3 files changed

+888
-0
lines changed

3 files changed

+888
-0
lines changed

test/e2e/conftest.py

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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 = (
31+
f"{os.environ.get('PODMAN_TEST_IMAGE_REGISTRY', 'quay.io')}/"
32+
f"{os.environ.get('PODMAN_TEST_IMAGE_USER', 'libpod')}/registry:2.8.2"
33+
)
34+
35+
with TemporaryDirectory() as temp_dir:
36+
work_dir = Path(temp_dir)
37+
htpasswd_file = work_dir / "htpasswd"
38+
trusted_certs_dir = work_dir / "trusted-registry-cert-dir"
39+
trusted_certs_dir.mkdir()
40+
41+
# Generate Certificates
42+
subprocess.run(
43+
f"openssl req -newkey rsa:4096 -nodes -sha256 "
44+
f"-keyout {work_dir.as_posix()}/domain.key -x509 -days 2 "
45+
f"-out {work_dir.as_posix()}/domain.crt "
46+
f"-subj \"/C=US/ST=Foo/L=Bar/O=Red Hat, Inc./CN=localhost\" "
47+
"-addext \"subjectAltName=DNS:localhost\"",
48+
shell=True,
49+
check=True,
50+
stderr=subprocess.DEVNULL,
51+
stdout=subprocess.DEVNULL,
52+
)
53+
54+
# Copy domain.crt to trusted_certs_dir
55+
shutil.copy(work_dir / "domain.crt", trusted_certs_dir)
56+
57+
# Create htpasswd file
58+
subprocess.run(
59+
f"htpasswd -Bbn {registry_username} {registry_password} > {htpasswd_file.as_posix()}",
60+
shell=True,
61+
check=True,
62+
)
63+
64+
# Start the registry
65+
subprocess.run(
66+
[
67+
# fmt: off
68+
ramalama_container_engine, "run", "-d", "--rm",
69+
"--name", registry_name,
70+
"-p", f"{registry_port}:5000",
71+
"-v", f"{work_dir.as_posix()}:/auth:Z",
72+
"-e", "REGISTRY_AUTH=htpasswd",
73+
"-e", "REGISTRY_AUTH_HTPASSWD_REALM='Registry Realm'",
74+
"-e", "REGISTRY_AUTH_HTPASSWD_PATH=/auth/htpasswd",
75+
"-e", "REGISTRY_HTTP_TLS_CERTIFICATE=/auth/domain.crt",
76+
"-e", "REGISTRY_HTTP_TLS_KEY=/auth/domain.key",
77+
registry_image,
78+
# fmt: on
79+
],
80+
check=True,
81+
)
82+
time.sleep(2)
83+
84+
try:
85+
yield Registry(
86+
username=registry_username,
87+
password=registry_password,
88+
url=f"oci://localhost:{registry_port}",
89+
host="localhost",
90+
port=registry_port,
91+
)
92+
finally:
93+
subprocess.run([ramalama_container_engine, "stop", registry_name], check=False)

0 commit comments

Comments
 (0)