|
19 | 19 | import pytest
|
20 | 20 | import docker
|
21 | 21 |
|
22 |
| -from tests.integ import DATA_DIR |
23 | 22 | from sagemaker import utils
|
24 |
| -from sagemaker.utils import sagemaker_timestamp |
| 23 | +from sagemaker.utils import sagemaker_timestamp, _tmpdir |
25 | 24 |
|
26 | 25 | REPO_ACCOUNT_ID = "033110030271"
|
27 | 26 |
|
| 27 | +REPO_NAME = "remote-function-dummy-container" |
28 | 28 |
|
29 |
| -@pytest.fixture(scope="module") |
| 29 | +DOCKERFILE_TEMPLATE = ( |
| 30 | + "FROM public.ecr.aws/docker/library/python:{py_version}-slim\n\n" |
| 31 | + "WORKDIR /opt/ml/remote_function/\n" |
| 32 | + "COPY {source_archive} ./\n" |
| 33 | + "RUN pip3 install '{source_archive}[remote_function]'\n" |
| 34 | + "RUN rm {source_archive}\n" |
| 35 | +) |
| 36 | + |
| 37 | + |
| 38 | +@pytest.fixture(scope="package") |
30 | 39 | def dummy_container_without_error(sagemaker_session):
|
31 |
| - repository_name = "remote-function-dummy-container" |
32 |
| - docker_file_path = os.path.join(DATA_DIR, "remote_function", "containers", "without_error") |
33 |
| - ecr_uri = _build_container(sagemaker_session, repository_name, docker_file_path) |
| 40 | + # TODO: the python version should be dynamically specified instead of hardcoding |
| 41 | + ecr_uri = _build_container(sagemaker_session, "3.10") |
34 | 42 | return ecr_uri
|
35 | 43 |
|
36 | 44 |
|
37 |
| -def _build_container(sagemaker_session, repository_name, docker_file_path): |
38 |
| - """Build a dummy test container locally and push a container to an ecr repo""" |
| 45 | +@pytest.fixture(scope="package") |
| 46 | +def dummy_container_incompatible_python_runtime(sagemaker_session): |
| 47 | + ecr_uri = _build_container(sagemaker_session, "3.7") |
| 48 | + return ecr_uri |
| 49 | + |
39 | 50 |
|
40 |
| - _generate_and_move_sagemaker_sdk_tar(docker_file_path) |
| 51 | +def _build_container(sagemaker_session, py_version): |
| 52 | + """Build a dummy test container locally and push a container to an ecr repo""" |
41 | 53 |
|
42 |
| - image_tag = sagemaker_timestamp() |
| 54 | + region = sagemaker_session.boto_region_name |
| 55 | + image_tag = f"{py_version.replace('.', '-')}-{sagemaker_timestamp()}" |
43 | 56 | ecr_client = sagemaker_session.boto_session.client("ecr")
|
44 | 57 | username, password = _ecr_login(ecr_client)
|
45 | 58 |
|
46 |
| - docker_client = docker.from_env() |
47 |
| - # build docker locally |
48 |
| - image, _ = docker_client.images.build(path=docker_file_path, tag=repository_name, rm=True) |
| 59 | + with _tmpdir() as tmpdir: |
| 60 | + print("building docker image locally in ", tmpdir) |
| 61 | + print("building source archive...") |
| 62 | + source_archive = _generate_and_move_sagemaker_sdk_tar(tmpdir) |
| 63 | + with open(os.path.join(tmpdir, "Dockerfile"), "w") as file: |
| 64 | + file.writelines( |
| 65 | + DOCKERFILE_TEMPLATE.format(py_version=py_version, source_archive=source_archive) |
| 66 | + ) |
49 | 67 |
|
50 |
| - region = sagemaker_session.boto_region_name |
51 |
| - ecr_image = "" |
52 |
| - if _is_repository_exists(ecr_client, repository_name): |
| 68 | + docker_client = docker.from_env() |
| 69 | + |
| 70 | + print("building docker image...") |
| 71 | + image, build_logs = docker_client.images.build(path=tmpdir, tag=REPO_NAME, rm=True) |
| 72 | + |
| 73 | + if _is_repository_exists(ecr_client, REPO_NAME): |
53 | 74 | sts_client = sagemaker_session.boto_session.client(
|
54 | 75 | "sts", region_name=region, endpoint_url=utils.sts_regional_endpoint(region)
|
55 | 76 | )
|
56 | 77 | account_id = sts_client.get_caller_identity()["Account"]
|
57 |
| - # When the test is run locally, repo will exists in same account whose credentials are used to run the test |
| 78 | + # When the test is run locally, repo will exist in same account whose credentials are used to run the test |
58 | 79 | ecr_image = _ecr_image_uri(
|
59 |
| - account_id, sagemaker_session.boto_region_name, repository_name, image_tag |
| 80 | + account_id, sagemaker_session.boto_region_name, REPO_NAME, image_tag |
60 | 81 | )
|
61 | 82 | else:
|
62 | 83 | ecr_image = _ecr_image_uri(
|
63 | 84 | REPO_ACCOUNT_ID,
|
64 | 85 | sagemaker_session.boto_region_name,
|
65 |
| - repository_name, |
| 86 | + REPO_NAME, |
66 | 87 | image_tag,
|
67 | 88 | )
|
68 | 89 |
|
| 90 | + print("pushing image...") |
69 | 91 | image.tag(ecr_image, tag=image_tag)
|
70 | 92 | docker_client.images.push(ecr_image, auth_config={"username": username, "password": password})
|
| 93 | + |
71 | 94 | return ecr_image
|
72 | 95 |
|
73 | 96 |
|
@@ -99,7 +122,9 @@ def _generate_and_move_sagemaker_sdk_tar(destination_folder):
|
99 | 122 | """
|
100 | 123 | subprocess.run("python3 setup.py sdist", shell=True)
|
101 | 124 | dist_dir = "dist"
|
102 |
| - for item in os.listdir(dist_dir): |
103 |
| - source = os.path.join(dist_dir, item) |
104 |
| - destination = os.path.join(destination_folder, item) |
105 |
| - shutil.copy2(source, destination) |
| 125 | + source_archive = os.listdir(dist_dir)[0] |
| 126 | + source_path = os.path.join(dist_dir, source_archive) |
| 127 | + destination_path = os.path.join(destination_folder, source_archive) |
| 128 | + shutil.copy2(source_path, destination_path) |
| 129 | + |
| 130 | + return source_archive |
0 commit comments