|
15 | 15 | import itertools
|
16 | 16 | import os
|
17 | 17 | import time
|
| 18 | +import requests |
18 | 19 |
|
19 | 20 | import pandas
|
20 | 21 | import pytest
|
| 22 | +import docker |
21 | 23 |
|
22 | 24 | import sagemaker
|
23 | 25 | import tests.integ
|
24 |
| -from sagemaker import AlgorithmEstimator, ModelPackage |
| 26 | +from sagemaker import AlgorithmEstimator, ModelPackage, Model |
25 | 27 | from sagemaker.serializers import CSVSerializer
|
26 | 28 | from sagemaker.tuner import IntegerParameter, HyperparameterTuner
|
27 | 29 | from sagemaker.utils import sagemaker_timestamp, _aws_partition, unique_name_from_base
|
28 | 30 | from tests.integ import DATA_DIR
|
29 | 31 | from tests.integ.timeout import timeout, timeout_and_delete_endpoint_by_name
|
30 | 32 | from tests.integ.marketplace_utils import REGION_ACCOUNT_MAP
|
| 33 | +from tests.integ.test_multidatamodel import ( |
| 34 | + _ecr_image_uri, |
| 35 | + _ecr_login, |
| 36 | + _create_repository, |
| 37 | + _delete_repository, |
| 38 | +) |
| 39 | +from tests.integ.retry import retries |
| 40 | +import logging |
31 | 41 |
|
| 42 | +logger = logging.getLogger(__name__) |
32 | 43 |
|
33 | 44 | # All these tests require a manual 1 time subscription to the following Marketplace items:
|
34 | 45 | # Algorithm: Scikit Decision Trees
|
@@ -186,6 +197,135 @@ def predict_wrapper(endpoint, session):
|
186 | 197 | print(predictor.predict(test_x.values).decode("utf-8"))
|
187 | 198 |
|
188 | 199 |
|
| 200 | +@pytest.fixture(scope="module") |
| 201 | +def iris_image(sagemaker_session): |
| 202 | + algorithm_name = unique_name_from_base("iris-classifier") |
| 203 | + ecr_image = _ecr_image_uri(sagemaker_session, algorithm_name) |
| 204 | + ecr_client = sagemaker_session.boto_session.client("ecr") |
| 205 | + username, password = _ecr_login(ecr_client) |
| 206 | + |
| 207 | + docker_client = docker.from_env() |
| 208 | + |
| 209 | + # Build and tag docker image locally |
| 210 | + path = os.path.join(DATA_DIR, "marketplace", "iris") |
| 211 | + image, build_logs = docker_client.images.build( |
| 212 | + path=path, |
| 213 | + tag=algorithm_name, |
| 214 | + rm=True, |
| 215 | + ) |
| 216 | + image.tag(ecr_image, tag="latest") |
| 217 | + _create_repository(ecr_client, algorithm_name) |
| 218 | + |
| 219 | + # Retry docker image push |
| 220 | + for _ in retries(3, "Upload docker image to ECR repo", seconds_to_sleep=10): |
| 221 | + try: |
| 222 | + docker_client.images.push( |
| 223 | + ecr_image, auth_config={"username": username, "password": password} |
| 224 | + ) |
| 225 | + break |
| 226 | + except requests.exceptions.ConnectionError: |
| 227 | + # This can happen when we try to create multiple repositories in parallel, so we retry |
| 228 | + pass |
| 229 | + |
| 230 | + yield ecr_image |
| 231 | + |
| 232 | + # Delete repository after the marketplace integration tests complete |
| 233 | + _delete_repository(ecr_client, algorithm_name) |
| 234 | + |
| 235 | + |
| 236 | +def test_create_model_package(sagemaker_session, boto_session, iris_image): |
| 237 | + MODEL_NAME = "iris-classifier-mp" |
| 238 | + # Prepare |
| 239 | + s3_bucket = sagemaker_session.default_bucket() |
| 240 | + |
| 241 | + model_name = unique_name_from_base(MODEL_NAME) |
| 242 | + model_description = "This model accepts petal length, petal width, sepal length, sepal width and predicts whether \ |
| 243 | + flower is of type setosa, versicolor, or virginica" |
| 244 | + |
| 245 | + supported_realtime_inference_instance_types = supported_batch_transform_instance_types = [ |
| 246 | + "ml.m4.xlarge" |
| 247 | + ] |
| 248 | + supported_content_types = ["text/csv", "application/json", "application/jsonlines"] |
| 249 | + supported_response_MIME_types = ["application/json", "text/csv", "application/jsonlines"] |
| 250 | + |
| 251 | + validation_input_path = "s3://" + s3_bucket + "/validation-input-csv/" |
| 252 | + validation_output_path = "s3://" + s3_bucket + "/validation-output-csv/" |
| 253 | + |
| 254 | + iam = boto_session.resource("iam") |
| 255 | + role = iam.Role("SageMakerRole").arn |
| 256 | + sm_client = boto_session.client("sagemaker") |
| 257 | + s3_client = boto_session.client("s3") |
| 258 | + s3_client.put_object( |
| 259 | + Bucket=s3_bucket, Key="validation-input-csv/input.csv", Body="5.1, 3.5, 1.4, 0.2" |
| 260 | + ) |
| 261 | + |
| 262 | + ValidationSpecification = { |
| 263 | + "ValidationRole": role, |
| 264 | + "ValidationProfiles": [ |
| 265 | + { |
| 266 | + "ProfileName": "Validation-test", |
| 267 | + "TransformJobDefinition": { |
| 268 | + "BatchStrategy": "SingleRecord", |
| 269 | + "TransformInput": { |
| 270 | + "DataSource": { |
| 271 | + "S3DataSource": { |
| 272 | + "S3DataType": "S3Prefix", |
| 273 | + "S3Uri": validation_input_path, |
| 274 | + } |
| 275 | + }, |
| 276 | + "ContentType": supported_content_types[0], |
| 277 | + }, |
| 278 | + "TransformOutput": { |
| 279 | + "S3OutputPath": validation_output_path, |
| 280 | + }, |
| 281 | + "TransformResources": { |
| 282 | + "InstanceType": supported_batch_transform_instance_types[0], |
| 283 | + "InstanceCount": 1, |
| 284 | + }, |
| 285 | + }, |
| 286 | + }, |
| 287 | + ], |
| 288 | + } |
| 289 | + |
| 290 | + # get pre-existing model artifact stored in ECR |
| 291 | + model = Model( |
| 292 | + image_uri=iris_image, |
| 293 | + model_data=validation_input_path + "input.csv", |
| 294 | + role=role, |
| 295 | + sagemaker_session=sagemaker_session, |
| 296 | + enable_network_isolation=False, |
| 297 | + ) |
| 298 | + |
| 299 | + # Call model.register() - the method under test - to create a model package |
| 300 | + model.register( |
| 301 | + supported_content_types, |
| 302 | + supported_response_MIME_types, |
| 303 | + supported_realtime_inference_instance_types, |
| 304 | + supported_batch_transform_instance_types, |
| 305 | + marketplace_cert=True, |
| 306 | + description=model_description, |
| 307 | + model_package_name=model_name, |
| 308 | + validation_specification=ValidationSpecification, |
| 309 | + ) |
| 310 | + |
| 311 | + # wait for model execution to complete |
| 312 | + time.sleep(60 * 3) |
| 313 | + |
| 314 | + # query for all model packages with the name <MODEL_NAME> |
| 315 | + response = sm_client.list_model_packages( |
| 316 | + MaxResults=10, |
| 317 | + NameContains=MODEL_NAME, |
| 318 | + SortBy="CreationTime", |
| 319 | + SortOrder="Descending", |
| 320 | + ) |
| 321 | + |
| 322 | + if len(response["ModelPackageSummaryList"]) > 0: |
| 323 | + sm_client.delete_model_package(ModelPackageName=model_name) |
| 324 | + |
| 325 | + # assert that response is non-empty |
| 326 | + assert len(response["ModelPackageSummaryList"]) > 0 |
| 327 | + |
| 328 | + |
189 | 329 | @pytest.mark.skipif(
|
190 | 330 | tests.integ.test_region() in tests.integ.NO_MARKET_PLACE_REGIONS,
|
191 | 331 | reason="Marketplace is not available in {}".format(tests.integ.test_region()),
|
|
0 commit comments