Skip to content

Commit

Permalink
fix(client): support version only copy (#1531)
Browse files Browse the repository at this point in the history
  • Loading branch information
jialeicui authored Nov 17, 2022
1 parent 8d9df95 commit 4c9e8ba
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 9 deletions.
9 changes: 7 additions & 2 deletions client/starwhale/base/uricomponents/resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,11 @@ def _parse_with_type(self, typ: ResourceType, uri: str) -> None:
raise Exception(f"invalid uri {uri} when parse with type {typ}")

if len(parts) == 1:
self.name = parts[0]
try:
# try version first
self._parse_by_version(parts[0])
except NoMatchException:
self.name = parts[0]
elif len(parts) == 2:
if parts[0] != "version":
# name/version
Expand Down Expand Up @@ -146,7 +150,8 @@ def _parse_by_version(self, ver: str) -> None:
raise NoMatchException(ver, list(m))
else:
# TODO use api to check if it is a name or version
...
# assume is is name for now
self.name = ver

@property
def instance(self) -> Instance:
Expand Down
47 changes: 40 additions & 7 deletions client/tests/base/uricomponents/test_resource.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,20 +59,33 @@ def test_with_full_uri(self, mock_parse: MagicMock) -> None:
assert r.typ == ResourceType.dataset
assert mock_parse.call_args(uri)

@patch("starwhale.base.uricomponents.resource.Project.parse")
def test_with_full_uri_no_version(self, mock_parse: MagicMock) -> None:
ins = mock_parse.return_value
ins.path = "dataset/mnist"
@patch("starwhale.utils.config.load_swcli_config")
def test_with_full_uri_no_version(self, mock_conf: MagicMock) -> None:
mock_conf.return_value = {
"current_instance": "local",
"instances": {
"local": {"uri": "local", "current_project": "self"},
},
"storage": {"root": "/root"},
}
uri = "local/project/self/dataset/mnist"
r = Resource(uri)
assert r.name == "mnist"
assert r.version is None
assert r.typ == ResourceType.dataset
assert mock_parse.call_args(uri)

@patch("starwhale.base.uricomponents.resource.glob")
def test_version_only_with_project(self, mock_glob: MagicMock) -> None:
@patch("starwhale.utils.config.load_swcli_config")
def test_version_only(self, mock_conf: MagicMock, mock_glob: MagicMock) -> None:
mock_conf.return_value = {
"current_instance": "local",
"instances": {
"local": {"uri": "local", "current_project": "self"},
},
}
mock_glob.return_value = ["/root/project/self/runtime/mnist/fo/foo"]

# with project
project = Mock(spec=Project)
project.instance = MockLocalInstance()
project.name = "self"
Expand All @@ -82,6 +95,13 @@ def test_version_only_with_project(self, mock_glob: MagicMock) -> None:
assert r.typ == ResourceType.runtime
assert r.to_uri().raw == "local/project/self/runtime/mnist/version/foo"

# without project
r = Resource("foo")
assert r.name == "mnist"
assert r.version == "foo"
assert r.typ == ResourceType.runtime
assert r.to_uri().raw == "local/project/self/runtime/mnist/version/foo"

@patch("starwhale.base.uricomponents.resource.glob")
@patch("starwhale.base.uricomponents.resource.Project.parse")
def test_version_only_no_match(
Expand Down Expand Up @@ -146,8 +166,21 @@ def __eq__(self, other: Resource):
with self.assertRaises(Exception):
Resource("https://foo.com/projects/1/model") # model missing the tail 's'

def test_short_uri(self) -> None:
@patch("starwhale.utils.config.load_swcli_config")
def test_short_uri(self, load_conf: MagicMock) -> None:
load_conf.return_value = {
"instances": {
"foo": {"uri": "https://foo.com"},
"bar": {"uri": "https://bar.com"},
"local": {"uri": "local"},
},
}
p = Resource("local/project/self/mnist", typ=ResourceType.runtime)
assert p.name == "mnist"
assert p.project.name == "self"
assert p.instance.alias == "local"

p = Resource("cloud://bar/project/self/mnist", typ=ResourceType.runtime)
assert p.name == "mnist"
assert p.project.name == "self"
assert p.instance.alias == "bar"

0 comments on commit 4c9e8ba

Please sign in to comment.