Skip to content

Commit 59ad06b

Browse files
committed
upgraded director-v0 to be compatible with both registries
1 parent ea25763 commit 59ad06b

File tree

3 files changed

+59
-24
lines changed

3 files changed

+59
-24
lines changed

packages/pytest-simcore/src/pytest_simcore/docker_registry.py

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -22,35 +22,45 @@
2222

2323
from .helpers.host import get_localhost_ip
2424

25-
log = logging.getLogger(__name__)
25+
_logger = logging.getLogger(__name__)
2626

2727

2828
@pytest.fixture(scope="session")
2929
def docker_registry(keep_docker_up: bool) -> Iterator[str]:
30+
"""sets up and runs a docker registry container locally and returns its URL"""
31+
yield from _docker_registry_impl(keep_docker_up, registry_version="3")
32+
33+
34+
@pytest.fixture(scope="session")
35+
def docker_registry_v2() -> Iterator[str]:
36+
"""sets up and runs a docker registry v2 container locally and returns its URL"""
37+
yield from _docker_registry_impl(keep_docker_up=False, registry_version="2")
38+
39+
40+
def _docker_registry_impl(keep_docker_up: bool, registry_version: str) -> Iterator[str]:
3041
"""sets up and runs a docker registry container locally and returns its URL"""
3142
# run the registry outside of the stack
3243
docker_client = docker.from_env()
3344
# try to login to private registry
3445
host = "127.0.0.1"
35-
port = 5000
46+
port = 5000 if registry_version == "3" else 5001
3647
url = f"{host}:{port}"
48+
container_name = f"pytest_registry_v{registry_version}"
49+
volume_name = f"pytest_registry_v{registry_version}_data"
50+
3751
container = None
3852
try:
3953
docker_client.login(registry=url, username="simcore")
40-
container = docker_client.containers.list(filters={"name": "pytest_registry"})[
41-
0
42-
]
54+
container = docker_client.containers.list(filters={"name": container_name})[0]
4355
print("Warning: docker registry is already up!")
4456
except Exception: # pylint: disable=broad-except
4557
container = docker_client.containers.run(
46-
"registry:3",
47-
ports={"5000": "5000"},
48-
name="pytest_registry",
58+
f"registry:{registry_version}",
59+
ports={"5000": port},
60+
name=container_name,
4961
environment=["REGISTRY_STORAGE_DELETE_ENABLED=true"],
5062
restart_policy={"Name": "always"},
51-
volumes={
52-
"pytest_registry_data": {"bind": "/var/lib/registry", "mode": "rw"}
53-
},
63+
volumes={volume_name: {"bind": "/var/lib/registry", "mode": "rw"}},
5464
detach=True,
5565
)
5666

@@ -79,9 +89,9 @@ def docker_registry(keep_docker_up: bool) -> Iterator[str]:
7989
os.environ["REGISTRY_SSL"] = "False"
8090
os.environ["REGISTRY_AUTH"] = "False"
8191
# the registry URL is how to access from the container (e.g. for accessing the API)
82-
os.environ["REGISTRY_URL"] = f"{get_localhost_ip()}:5000"
92+
os.environ["REGISTRY_URL"] = f"{get_localhost_ip()}:{port}"
8393
# the registry PATH is how the docker engine shall access the images (usually same as REGISTRY_URL but for testing)
84-
os.environ["REGISTRY_PATH"] = "127.0.0.1:5000"
94+
os.environ["REGISTRY_PATH"] = f"127.0.0.1:{port}"
8595
os.environ["REGISTRY_USER"] = "simcore"
8696
os.environ["REGISTRY_PW"] = ""
8797

@@ -124,7 +134,7 @@ def registry_settings(
124134
@tenacity.retry(
125135
wait=tenacity.wait_fixed(2),
126136
stop=tenacity.stop_after_delay(20),
127-
before_sleep=tenacity.before_sleep_log(log, logging.INFO),
137+
before_sleep=tenacity.before_sleep_log(_logger, logging.INFO),
128138
reraise=True,
129139
)
130140
def wait_till_registry_is_responsive(url: str) -> bool:

services/director/tests/unit/fixtures/fake_services.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ async def _build_and_push_image(
117117
bad_json_format: bool = False,
118118
app_settings: ApplicationSettings,
119119
) -> ServiceInRegistryInfoDict:
120-
121120
# crate image
122121
service_description = _create_service_description(service_type, name, tag)
123122
docker_labels = _create_docker_labels(
@@ -214,12 +213,13 @@ async def _build_and_push_image(
214213
)
215214

216215

217-
def _clean_registry(registry_url: str, list_of_images: list[ServiceInRegistryInfoDict]):
216+
def _clean_registry(list_of_images: list[ServiceInRegistryInfoDict]):
218217
request_headers = {"accept": "application/vnd.docker.distribution.manifest.v2+json"}
219218
for image in list_of_images:
220219
service_description = image["service_description"]
221220
# get the image digest
222221
tag = service_description["version"]
222+
registry_url = image["image_path"].split("/")[0]
223223
url = "http://{host}/v2/{name}/manifests/{tag}".format(
224224
host=registry_url, name=service_description["key"], tag=tag
225225
)
@@ -243,15 +243,13 @@ async def __call__(
243243
inter_dependent_services: bool = False,
244244
bad_json_format: bool = False,
245245
version="1.0.",
246-
) -> list[ServiceInRegistryInfoDict]:
247-
...
246+
) -> list[ServiceInRegistryInfoDict]: ...
248247

249248

250249
@pytest.fixture
251250
def push_services(
252251
docker_registry: str, app_settings: ApplicationSettings
253252
) -> Iterator[PushServicesCallable]:
254-
registry_url = docker_registry
255253
list_of_pushed_images_tags: list[ServiceInRegistryInfoDict] = []
256254
dependent_images = []
257255

@@ -262,8 +260,13 @@ async def _build_push_images_to_docker_registry(
262260
inter_dependent_services=False,
263261
bad_json_format=False,
264262
version="1.0.",
263+
override_registry_url: str | None = None,
265264
) -> list[ServiceInRegistryInfoDict]:
266265
try:
266+
registry_url = docker_registry
267+
if override_registry_url:
268+
_logger.info("Overriding registry URL with %s", override_registry_url)
269+
registry_url = override_registry_url
267270
dependent_image = None
268271
if inter_dependent_services:
269272
dependent_image = await _build_and_push_image(
@@ -317,5 +320,5 @@ async def _build_push_images_to_docker_registry(
317320
yield _build_push_images_to_docker_registry
318321

319322
_logger.info("clean registry")
320-
_clean_registry(registry_url, list_of_pushed_images_tags)
321-
_clean_registry(registry_url, dependent_images)
323+
_clean_registry(list_of_pushed_images_tags)
324+
_clean_registry(dependent_images)

services/director/tests/unit/test_registry_proxy.py

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ async def test_list_no_services_available(
2121
configure_registry_access: EnvVarsDict,
2222
app: FastAPI,
2323
):
24-
2524
computational_services = await registry_proxy.list_services(
2625
app, registry_proxy.ServiceType.COMPUTATIONAL
2726
)
@@ -116,13 +115,36 @@ async def test_list_interactive_service_dependencies(
116115
assert image_dependencies[0]["tag"] == docker_dependencies[0]["tag"]
117116

118117

118+
@pytest.fixture(
119+
params=["docker_registry", "docker_registry_v2"], ids=["registry_v3", "registry_v2"]
120+
)
121+
def configure_registry_access_both_versions(
122+
app_environment: EnvVarsDict,
123+
monkeypatch: pytest.MonkeyPatch,
124+
request: pytest.FixtureRequest,
125+
) -> EnvVarsDict:
126+
"""Parametrized fixture that tests with both registry v3 and v2 - use only for specific tests that need both"""
127+
registry_url = request.getfixturevalue(request.param)
128+
return app_environment | setenvs_from_dict(
129+
monkeypatch,
130+
envs={
131+
"REGISTRY_URL": registry_url,
132+
"REGISTRY_PATH": registry_url,
133+
"REGISTRY_SSL": False,
134+
"DIRECTOR_REGISTRY_CACHING": False,
135+
},
136+
)
137+
138+
119139
async def test_get_image_labels(
120-
configure_registry_access: EnvVarsDict,
140+
configure_registry_access_both_versions: EnvVarsDict,
121141
app: FastAPI,
122142
push_services,
123143
):
124144
images = await push_services(
125-
number_of_computational_services=1, number_of_interactive_services=1
145+
number_of_computational_services=1,
146+
number_of_interactive_services=1,
147+
override_registry_url=configure_registry_access_both_versions["REGISTRY_URL"],
126148
)
127149
images_digests = set()
128150
for image in images:

0 commit comments

Comments
 (0)