18
18
import shutil
19
19
import pytest
20
20
import docker
21
+ import re
21
22
22
23
from sagemaker .utils import sagemaker_timestamp , _tmpdir , sts_regional_endpoint
23
24
57
58
"ENV SAGEMAKER_JOB_CONDA_ENV=default_env\n "
58
59
)
59
60
60
- CONDA_YML_FILE_TEMPLATE = (
61
- "name: integ_test_env\n "
62
- "channels:\n "
63
- " - defaults\n "
64
- "dependencies:\n "
65
- " - scipy=1.7.3\n "
66
- " - pip:\n "
67
- " - /sagemaker-{sagemaker_version}.tar.gz\n "
68
- "prefix: /opt/conda/bin/conda\n "
61
+ AUTO_CAPTURE_CLIENT_DOCKER_TEMPLATE = (
62
+ "FROM public.ecr.aws/docker/library/python:{py_version}-slim\n \n "
63
+ 'SHELL ["/bin/bash", "-c"]\n '
64
+ "RUN apt-get update -y \
65
+ && apt-get install -y unzip curl\n \n "
66
+ "RUN curl -L -O 'https://github.com/conda-forge/miniforge/releases/latest/download/Mambaforge-Linux-x86_64.sh' \
67
+ && bash Mambaforge-Linux-x86_64.sh -b -p '/opt/conda' \
68
+ && /opt/conda/bin/conda init bash\n \n "
69
+ "ENV PATH $PATH:/opt/conda/bin\n "
70
+ "COPY {source_archive} ./\n "
71
+ "RUN mamba create -n auto_capture_client python={py_version} -y \
72
+ && mamba run -n auto_capture_client pip install '{source_archive}' awscli boto3\n "
73
+ "COPY test_auto_capture.py .\n "
74
+ "CMD [\" mamba\" , \" run\" , \" -n\" , \" auto_capture_client\" , \" python\" , \" test_auto_capture.py\" ]\n "
69
75
)
70
76
71
- CONDA_YML_FILE_WITH_SM_FROM_INPUT_CHANNEL = (
77
+ CONDA_YML_FILE_TEMPLATE = (
72
78
"name: integ_test_env\n "
73
79
"channels:\n "
74
80
" - defaults\n "
75
81
"dependencies:\n "
76
82
" - scipy=1.7.3\n "
77
83
" - pip:\n "
78
- " - sagemaker-2.132.1.dev0-py2.py3-none-any.whl \n "
84
+ " - / sagemaker-{sagemaker_version}.tar.gz \n "
79
85
"prefix: /opt/conda/bin/conda\n "
80
86
)
81
87
@@ -99,6 +105,12 @@ def dummy_container_with_conda(sagemaker_session):
99
105
return ecr_uri
100
106
101
107
108
+ @pytest .fixture (scope = "package" )
109
+ def auto_capture_test_container (sagemaker_session ):
110
+ ecr_uri = _build_container_locally ("3.10" , AUTO_CAPTURE_CLIENT_DOCKER_TEMPLATE )
111
+ return ecr_uri
112
+
113
+
102
114
@pytest .fixture (scope = "package" )
103
115
def conda_env_yml ():
104
116
"""Write conda yml file needed for tests"""
@@ -116,22 +128,6 @@ def conda_env_yml():
116
128
os .remove (conda_yml_file_name )
117
129
118
130
119
- @pytest .fixture (scope = "package" )
120
- def conda_yml_file_sm_from_input_channel ():
121
- """Write conda yml file needed for tests"""
122
-
123
- conda_yml_file_name = "conda_env_sm_from_input_channel.yml"
124
- conda_file_path = os .path .join (os .getcwd (), conda_yml_file_name )
125
-
126
- with open (conda_file_path , "w" ) as yml_file :
127
- yml_file .writelines (CONDA_YML_FILE_WITH_SM_FROM_INPUT_CHANNEL )
128
- yield conda_file_path
129
-
130
- # cleanup
131
- if os .path .isfile (conda_yml_file_name ):
132
- os .remove (conda_yml_file_name )
133
-
134
-
135
131
def _build_container (sagemaker_session , py_version , docker_templete ):
136
132
"""Build a dummy test container locally and push a container to an ecr repo"""
137
133
@@ -178,6 +174,23 @@ def _build_container(sagemaker_session, py_version, docker_templete):
178
174
return ecr_image
179
175
180
176
177
+ def _build_container_locally (py_version , docker_templete ):
178
+ with _tmpdir () as tmpdir :
179
+ print ("building docker image locally in " , tmpdir )
180
+ print ("building source archive..." )
181
+ source_archive = _generate_sdk_tar_with_public_version (tmpdir )
182
+ _move_auto_capture_test_file (tmpdir )
183
+ with open (os .path .join (tmpdir , "Dockerfile" ), "w" ) as file :
184
+ file .writelines (
185
+ docker_templete .format (py_version = py_version , source_archive = source_archive )
186
+ )
187
+
188
+ docker_client = docker .from_env ()
189
+
190
+ print ("building docker image..." )
191
+ image , build_logs = docker_client .images .build (path = tmpdir , tag = REPO_NAME , rm = True )
192
+ return image .id
193
+
181
194
def _is_repository_exists (ecr_client , repo_name ):
182
195
try :
183
196
ecr_client .describe_repositories (repositoryNames = [repo_name ])
@@ -212,3 +225,30 @@ def _generate_and_move_sagemaker_sdk_tar(destination_folder):
212
225
shutil .copy2 (source_path , destination_path )
213
226
214
227
return source_archive
228
+
229
+
230
+ def _generate_sdk_tar_with_public_version (destination_folder ):
231
+ with open (os .path .join (os .getcwd (), "VERSION" ), "r+" ) as version_file :
232
+ dev_sagemaker_version = version_file .readline ().strip ()
233
+ public_sagemaker_version = re .sub ("1.dev0" , "0" , dev_sagemaker_version )
234
+ version_file .seek (0 )
235
+ version_file .write (public_sagemaker_version )
236
+ version_file .truncate ()
237
+ shutil .rmtree ("dist" )
238
+
239
+ source_archive = _generate_and_move_sagemaker_sdk_tar (destination_folder )
240
+
241
+ with open (os .path .join (os .getcwd (), "VERSION" ), "r+" ) as version_file :
242
+ version_file .seek (0 )
243
+ version_file .write (dev_sagemaker_version )
244
+ version_file .truncate ()
245
+ shutil .rmtree ("dist" )
246
+
247
+ return source_archive
248
+
249
+
250
+ def _move_auto_capture_test_file (destination_folder ):
251
+ test_file_name = "test_auto_capture.py"
252
+ source_path = os .path .join (os .getcwd (), "tests" , "integ" , "sagemaker" , "remote_function" , test_file_name )
253
+ destination_path = os .path .join (destination_folder , test_file_name )
254
+ shutil .copy2 (source_path , destination_path )
0 commit comments