Skip to content

Commit

Permalink
chore: set default dest_dir
Browse files Browse the repository at this point in the history
Signed-off-by: Eric Dobroveanu <[email protected]>
  • Loading branch information
Crazyglue committed Feb 18, 2025
1 parent 010ccbb commit 7fb8bef
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
1 change: 1 addition & 0 deletions clients/python/noxfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ def tests(session: Session) -> None:
"requests",
"pytest",
"pytest-asyncio",
"olot",
)
session.run(
"pytest",
Expand Down
4 changes: 3 additions & 1 deletion clients/python/src/model_registry/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
from __future__ import annotations

import os
import tempfile
from pathlib import Path
from typing import Callable, TypedDict

Expand Down Expand Up @@ -96,6 +97,7 @@ def s3_uri_from(

class BackendDefinition(TypedDict):
"""Holds the 3 core callables for a backend:
- is_available() -> bool
- pull(base_image: str, dest_dir: Path) -> None
- push(local_image_path: Path, oci_ref: str) -> None.
Expand Down Expand Up @@ -142,9 +144,9 @@ def _get_oras_backend() -> BackendDefinition:

def save_to_oci_registry(
base_image: str,
dest_dir: str | os.PathLike,
oci_ref: str,
model_files: list[os.PathLike],
dest_dir: str | os.PathLike = tempfile.mkdtemp(),
backend: str = "skopeo",
modelcard: os.PathLike | None = None,
backend_registry: BackendDict | None = DEFAULT_BACKENDS,
Expand Down
26 changes: 22 additions & 4 deletions clients/python/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,13 +90,31 @@ def test_save_to_oci_registry_with_skopeo():
model_files = [readme_file_path]
backend = "skopeo"

save_to_oci_registry(base_image, dest_dir, oci_ref, model_files, backend)
save_to_oci_registry(base_image, oci_ref, model_files, dest_dir, backend)


# These are trimmed down versions of whats found in the example specs found here: https://github.com/opencontainers/image-spec/blob/main/image-layout.md#oci-layout-file
index_json_contents = '''{
"schemaVersion": 2,
"mediaType": "application/vnd.oci.image.index.v1+json",
"manifests": [],
"annotations": {
"com.example.index.revision": "r124356"
}
}'''
oci_layout_contents = '''{"imageLayoutVersion": "1.0.0"}'''

def test_save_to_oci_registry_with_custom_backend():
is_available_mock = Mock()
is_available_mock.return_value = True
pull_mock = Mock()
push_mock = Mock()
def pull_mock_imple(base_image, dest_dir):
pathlib.Path(dest_dir).joinpath("oci-layout").write_text(oci_layout_contents)
pathlib.Path(dest_dir).joinpath("index.json").write_text(index_json_contents)

pull_mock.side_effect = pull_mock_imple


backend = "something_custom"
backend_registry = {
Expand All @@ -120,7 +138,7 @@ def test_save_to_oci_registry_with_custom_backend():

model_files = [readme_file_path]

save_to_oci_registry(base_image, dest_dir, oci_ref, model_files, backend, None, backend_registry)
save_to_oci_registry(base_image, oci_ref, model_files, dest_dir, backend, None, backend_registry)
# Ensure our mocked backend was called
is_available_mock.assert_called_once()
pull_mock.assert_called_once()
Expand All @@ -143,13 +161,13 @@ def test_save_to_oci_registry_with_custom_backend_unavailable():


with pytest.raises(ValueError) as e:
save_to_oci_registry("", "", "", "", backend, backend_registry=backend_registry)
save_to_oci_registry("", "", [], "", backend, backend_registry=backend_registry)

assert f"Backend '{backend}' is selected, but not available on the system. Ensure the dependencies for '{backend}' are installed in your environment." in str(e.value)

def test_save_to_oci_registry_backend_not_found():
backend = "non-existent"
with pytest.raises(ValueError) as e:
save_to_oci_registry("", "", "", [], backend)
save_to_oci_registry("", "", [], "", backend)

assert f"'{backend}' is not an available backend to use." in str(e.value)

0 comments on commit 7fb8bef

Please sign in to comment.