|
| 1 | +# Copyright 2018-2019 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"). |
| 4 | +# You may not use this file except in compliance with the License. |
| 5 | +# A copy of the License is located at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# or in the "license" file accompanying this file. This file is distributed |
| 10 | +# on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either |
| 11 | +# express or implied. See the License for the specific language governing |
| 12 | +# permissions and limitations under the License. |
| 13 | +from __future__ import absolute_import |
| 14 | + |
| 15 | +import logging |
| 16 | +import os |
| 17 | + |
| 18 | +import boto3 |
| 19 | +import pytest |
| 20 | +from sagemaker import LocalSession, Session |
| 21 | +from sagemaker.tensorflow import TensorFlow |
| 22 | + |
| 23 | +from test.integration import NO_P2_REGIONS, NO_P3_REGIONS |
| 24 | + |
| 25 | +logger = logging.getLogger(__name__) |
| 26 | +logging.getLogger('boto').setLevel(logging.INFO) |
| 27 | +logging.getLogger('botocore').setLevel(logging.INFO) |
| 28 | +logging.getLogger('factory.py').setLevel(logging.INFO) |
| 29 | +logging.getLogger('auth.py').setLevel(logging.INFO) |
| 30 | +logging.getLogger('connectionpool.py').setLevel(logging.INFO) |
| 31 | + |
| 32 | +SCRIPT_PATH = os.path.dirname(os.path.realpath(__file__)) |
| 33 | + |
| 34 | + |
| 35 | +def pytest_addoption(parser): |
| 36 | + parser.addoption('--docker-base-name', default='sagemaker-tensorflow-scriptmode') |
| 37 | + parser.addoption('--tag', default=None) |
| 38 | + parser.addoption('--region', default='us-west-2') |
| 39 | + parser.addoption('--framework-version', default=TensorFlow.LATEST_VERSION) |
| 40 | + parser.addoption('--processor', default='cpu', choices=['cpu', 'gpu', 'cpu,gpu']) |
| 41 | + parser.addoption('--py-version', default='3', choices=['2', '3', '2,3']) |
| 42 | + parser.addoption('--account-id', default='142577830533') |
| 43 | + parser.addoption('--instance-type', default=None) |
| 44 | + |
| 45 | + |
| 46 | +def pytest_configure(config): |
| 47 | + os.environ['TEST_PY_VERSIONS'] = config.getoption('--py-version') |
| 48 | + os.environ['TEST_PROCESSORS'] = config.getoption('--processor') |
| 49 | + |
| 50 | + |
| 51 | +@pytest.fixture(scope='session') |
| 52 | +def docker_base_name(request): |
| 53 | + return request.config.getoption('--docker-base-name') |
| 54 | + |
| 55 | + |
| 56 | +@pytest.fixture(scope='session') |
| 57 | +def region(request): |
| 58 | + return request.config.getoption('--region') |
| 59 | + |
| 60 | + |
| 61 | +@pytest.fixture(scope='session') |
| 62 | +def framework_version(request): |
| 63 | + return request.config.getoption('--framework-version') |
| 64 | + |
| 65 | + |
| 66 | +@pytest.fixture |
| 67 | +def tag(request, framework_version, processor, py_version): |
| 68 | + provided_tag = request.config.getoption('--tag') |
| 69 | + default_tag = '{}-{}-py{}'.format(framework_version, processor, py_version) |
| 70 | + return provided_tag if provided_tag is not None else default_tag |
| 71 | + |
| 72 | + |
| 73 | +@pytest.fixture(scope='session') |
| 74 | +def sagemaker_session(region): |
| 75 | + return Session(boto_session=boto3.Session(region_name=region)) |
| 76 | + |
| 77 | + |
| 78 | +@pytest.fixture(scope='session') |
| 79 | +def sagemaker_local_session(region): |
| 80 | + return LocalSession(boto_session=boto3.Session(region_name=region)) |
| 81 | + |
| 82 | + |
| 83 | +@pytest.fixture(scope='session') |
| 84 | +def account_id(request): |
| 85 | + return request.config.getoption('--account-id') |
| 86 | + |
| 87 | + |
| 88 | +@pytest.fixture |
| 89 | +def instance_type(request, processor): |
| 90 | + provided_instance_type = request.config.getoption('--instance-type') |
| 91 | + default_instance_type = 'ml.c4.xlarge' if processor == 'cpu' else 'ml.p2.xlarge' |
| 92 | + return provided_instance_type if provided_instance_type is not None else default_instance_type |
| 93 | + |
| 94 | + |
| 95 | +@pytest.fixture(autouse=True) |
| 96 | +def skip_by_device_type(request, processor): |
| 97 | + is_gpu = (processor == 'gpu') |
| 98 | + if (request.node.get_closest_marker('skip_gpu') and is_gpu) or \ |
| 99 | + (request.node.get_closest_marker('skip_cpu') and not is_gpu): |
| 100 | + pytest.skip('Skipping because running on \'{}\' instance'.format(processor)) |
| 101 | + |
| 102 | + |
| 103 | +@pytest.fixture(autouse=True) |
| 104 | +def skip_gpu_instance_restricted_regions(region, instance_type): |
| 105 | + if (region in NO_P2_REGIONS and instance_type.startswith('ml.p2')) or \ |
| 106 | + (region in NO_P3_REGIONS and instance_type.startswith('ml.p3')): |
| 107 | + pytest.skip('Skipping GPU test in region {}'.format(region)) |
| 108 | + |
| 109 | + |
| 110 | +@pytest.fixture |
| 111 | +def docker_image(docker_base_name, tag): |
| 112 | + return '{}:{}'.format(docker_base_name, tag) |
| 113 | + |
| 114 | + |
| 115 | +@pytest.fixture |
| 116 | +def ecr_image(account_id, docker_base_name, tag, region): |
| 117 | + return '{}.dkr.ecr.{}.amazonaws.com/{}:{}'.format( |
| 118 | + account_id, region, docker_base_name, tag) |
0 commit comments