Skip to content

Commit

Permalink
add runtime dockerize ut
Browse files Browse the repository at this point in the history
  • Loading branch information
tianweidut committed Sep 16, 2022
1 parent 4ab8594 commit a843977
Showing 1 changed file with 132 additions and 1 deletion.
133 changes: 132 additions & 1 deletion client/tests/core/test_runtime.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,12 @@
from starwhale.utils.error import UnExpectedConfigFieldError
from starwhale.utils.config import SWCliConfigMixed
from starwhale.core.runtime.view import RuntimeTermView
from starwhale.core.runtime.model import Runtime, RuntimeConfig, StandaloneRuntime
from starwhale.core.runtime.model import (
Runtime,
_TEMPLATE_DIR,
RuntimeConfig,
StandaloneRuntime,
)


class StandaloneRuntimeTestCase(TestCase):
Expand Down Expand Up @@ -575,3 +580,129 @@ def test_lock_conda(self, m_output: MagicMock, m_call: MagicMock) -> None:
"/tmp/conda",
"--file",
]

def get_mock_manifest(self) -> t.Dict[str, t.Any]:
return {
"name": "rttest",
"version": "112233",
"base_image": "ghcr.io/star-whale/starwhale:latest-cuda11.4",
"dependencies": {
"conda_files": [],
"conda_pkgs": [],
"pip_pkgs": ["numpy"],
"pip_files": ["requirements-sw-lock.txt"],
"local_packaged_env": False,
},
"configs": {
"conda": {"channels": ["conda-forge"]},
"docker": {"image": "ghcr.io/star-whale/runtime/pytorch"},
"pip": {
"extra_index_url": ["https://pypi.doubanio.com/simple"],
"index_url": "https://pypi.tuna.tsinghua.edu.cn/simple",
"trusted_host": ["pypi.tuna.tsinghua.edu.cn", "pypi.doubanio.com"],
},
},
"artifacts": {
"dependencies": ["dependencies/requirements-sw-lock.txt"],
"files": [
{
"_swrt_dest": "files/bin/prepare.sh",
"dest": "bin/prepare.sh",
"name": "prepare",
"post": "bash bin/prepare.sh",
"pre": "ls bin/prepare.sh",
"src": "scripts/prepare.sh",
}
],
"runtime_yaml": "runtime.yaml",
"wheels": ["wheels/dummy-0.0.0-py3-none-any.whl"],
},
"environment": {
"arch": ["noarch"],
"auto_lock_dependencies": False,
"lock": {
"env_name": "",
"env_prefix_path": "",
"shell": {
"python_env": "conda",
"python_version": "3.8.13",
"use_conda": True,
"use_venv": False,
},
"starwhale_version": "0.0.0.dev0",
"system": "Linux",
"use_shell_detection": True,
},
"mode": "venv",
"python": "3.8",
},
}

@patch("starwhale.utils.docker.check_call")
def test_dockerize(self, m_check: MagicMock) -> None:
self.fs.add_real_directory(_TEMPLATE_DIR)
name = "rttest"
version = "112233"
image = "docker.io/t1/t2"
uri = URI(f"{name}/version/{version}", expected_type=URIType.RUNTIME)
manifest = self.get_mock_manifest()
manifest["name"] = name
manifest["version"] = version
manifest["configs"]["docker"]["image"] = image

sr = StandaloneRuntime(uri)
ensure_dir(sr.store.snapshot_workdir)
ensure_file(sr.store.manifest_path, content=yaml.safe_dump(manifest))
sr.dockerize(
tags=["t1", "t2"],
platforms=[SupportArch.AMD64],
push=True,
dry_run=False,
use_starwhale_builder=True,
reset_qemu_static=True,
)

dockerfile_path = sr.store.export_dir / "docker" / "Dockerfile"
dockerignore_path = sr.store.snapshot_workdir / ".dockerignore"
assert dockerfile_path.exists()
assert dockerignore_path.exists()
dockerfile_content = dockerfile_path.read_text()
assert f"BASE_IMAGE={manifest['base_image']}" in dockerfile_content
assert f"starwhale_runtime_version={version}" in dockerfile_content

assert m_check.call_count == 3
assert m_check.call_args_list[0][0][0] == [
"docker",
"run",
"--rm",
"--privileged",
"multiarch/qemu-user-static",
"--reset",
"-p",
"-yes",
]
assert m_check.call_args_list[1][0][0] == [
"docker",
"buildx",
"inspect",
"--builder",
"starwhale-multiarch-runtime-builder",
]

build_cmd = " ".join(m_check.call_args_list[2][0][0])
assert "--builder starwhale-multiarch-runtime-builder" in build_cmd
assert "--platform linux/amd64" in build_cmd
assert "--tag t1" in build_cmd
assert "--tag t2" in build_cmd
assert f"--tag {image}:{version}" in build_cmd
assert "--push" in build_cmd
assert f"--file {dockerfile_path}" in build_cmd

RuntimeTermView(f"{name}/version/{version}").dockerize(
tags=[],
push=False,
platforms=[SupportArch.ARM64],
dry_run=False,
use_starwhale_builder=False,
reset_qemu_static=False,
)

0 comments on commit a843977

Please sign in to comment.