|
| 1 | +# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"). You |
| 4 | +# may not use this file except in compliance with the License. A copy of |
| 5 | +# the License is located at |
| 6 | +# |
| 7 | +# http://aws.amazon.com/apache2.0/ |
| 8 | +# |
| 9 | +# or in the "license" file accompanying this file. This file is |
| 10 | +# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF |
| 11 | +# ANY KIND, either express or implied. See the License for the specific |
| 12 | +# language governing permissions and limitations under the License. |
| 13 | +from __future__ import absolute_import |
| 14 | + |
| 15 | +from unittest.mock import Mock, PropertyMock |
| 16 | + |
| 17 | +import pytest |
| 18 | + |
| 19 | +from sagemaker import Session |
| 20 | +from sagemaker.workflow.pipeline_context import PipelineSession |
| 21 | + |
| 22 | +REGION = "us-west-2" |
| 23 | +BUCKET = "my-bucket" |
| 24 | +ROLE = "DummyRole" |
| 25 | +IMAGE_URI = "fakeimage" |
| 26 | + |
| 27 | + |
| 28 | +@pytest.fixture(scope="module") |
| 29 | +def client(): |
| 30 | + """Mock client. |
| 31 | +
|
| 32 | + Considerations when appropriate: |
| 33 | +
|
| 34 | + * utilize botocore.stub.Stubber |
| 35 | + * separate runtime client from client |
| 36 | + """ |
| 37 | + client_mock = Mock() |
| 38 | + client_mock._client_config.user_agent = ( |
| 39 | + "Boto3/1.14.24 Python/3.8.5 Linux/5.4.0-42-generic Botocore/1.17.24 Resource" |
| 40 | + ) |
| 41 | + return client_mock |
| 42 | + |
| 43 | + |
| 44 | +@pytest.fixture(scope="module") |
| 45 | +def boto_session(client): |
| 46 | + role_mock = Mock() |
| 47 | + type(role_mock).arn = PropertyMock(return_value=ROLE) |
| 48 | + |
| 49 | + resource_mock = Mock() |
| 50 | + resource_mock.Role.return_value = role_mock |
| 51 | + |
| 52 | + session_mock = Mock(region_name=REGION) |
| 53 | + session_mock.resource.return_value = resource_mock |
| 54 | + session_mock.client.return_value = client |
| 55 | + |
| 56 | + return session_mock |
| 57 | + |
| 58 | + |
| 59 | +@pytest.fixture(scope="module") |
| 60 | +def pipeline_session(boto_session, client): |
| 61 | + return PipelineSession( |
| 62 | + boto_session=boto_session, |
| 63 | + sagemaker_client=client, |
| 64 | + default_bucket=BUCKET, |
| 65 | + ) |
| 66 | + |
| 67 | + |
| 68 | +@pytest.fixture(scope="module") |
| 69 | +def sagemaker_session(boto_session, client): |
| 70 | + return Session( |
| 71 | + boto_session=boto_session, |
| 72 | + sagemaker_client=client, |
| 73 | + sagemaker_runtime_client=client, |
| 74 | + default_bucket=BUCKET, |
| 75 | + ) |
0 commit comments