Skip to content

Commit

Permalink
Adding support for docker: protocol for DockerImage.from_string met…
Browse files Browse the repository at this point in the history
…hod. (#333)

This method correctly processes image names that start with `docker:[/]*` protocol specification.
  • Loading branch information
sergey-serebryakov authored Aug 4, 2023
1 parent 07231a2 commit 1c63408
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import json
import logging
import typing as t
from enum import Enum
Expand Down Expand Up @@ -77,6 +76,10 @@ def from_string(cls, name: str) -> "DockerImage":
Returns:
DockerImage instance with parsed components.
"""
# Remove protocol if present
if name.startswith("docker:"):
name = name[7:].lstrip("/")

# Split into parts that are separated by "/".
parts: t.List[str] = name.strip().split("/")

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
from unittest import TestCase

import semver
from mlcube_singularity.singularity_client import Client, DockerImage, Runtime, Version
from mlcube_singularity.singularity_client import (Client, DockerImage,
Runtime, Version)


class TestSingularityRunner(TestCase):
Expand Down Expand Up @@ -165,13 +166,16 @@ def test_docker_image_from_string(self) -> None:
)

def test_docker_image_to_string(self) -> None:
names = [
names: t.List[t.Union[t.Tuple[str, str], str]] = [
"LOCATION-docker.pkg.dev/PROJECT-ID/REPOSITORY/IMAGE",
"LOCATION-docker.pkg.dev/PROJECT-ID/REPOSITORY/IMAGE:TAG",
"LOCATION-docker.pkg.dev/PROJECT-ID/REPOSITORY/IMAGE@IMG-DIGEST",
"USERNAME/REPOSITORY:TAG",
"mlcommons/hello_world:0.0.1",
("docker:mlcommons/hello_world:0.0.1", "mlcommons/hello_world:0.0.1"),
("docker:///mlcommons/hello_world:0.0.1", "mlcommons/hello_world:0.0.1"),
"mlcommons/hello_world@sha256:9b77d4cb97f8dcf14ac137bf65185fc8980578",
]
for name in names:
self.assertEqual(str(DockerImage.from_string(name)), name)
name_in, name_out = (name, name) if isinstance(name, str) else (name[0], name[1])
self.assertEqual(str(DockerImage.from_string(name_in)), name_out)

0 comments on commit 1c63408

Please sign in to comment.