|
| 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 | +import pytest |
| 15 | +from mock import Mock |
| 16 | +from sagemaker.jumpstart.curated_hub.curated_hub import CuratedHub |
| 17 | + |
| 18 | +REGION = "us-east-1" |
| 19 | +ACCOUNT_ID = "123456789123" |
| 20 | +HUB_NAME = "mock-hub-name" |
| 21 | + |
| 22 | + |
| 23 | +@pytest.fixture() |
| 24 | +def sagemaker_session(): |
| 25 | + boto_mock = Mock(name="boto_session") |
| 26 | + sagemaker_session_mock = Mock( |
| 27 | + name="sagemaker_session", boto_session=boto_mock, boto_region_name=REGION |
| 28 | + ) |
| 29 | + sagemaker_session_mock._client_config.user_agent = ( |
| 30 | + "Boto3/1.9.69 Python/3.6.5 Linux/4.14.77-70.82.amzn1.x86_64 Botocore/1.12.69 Resource" |
| 31 | + ) |
| 32 | + sagemaker_session_mock.account_id.return_value = ACCOUNT_ID |
| 33 | + return sagemaker_session_mock |
| 34 | + |
| 35 | + |
| 36 | +# @pytest.fixture() |
| 37 | +# def sagemaker_session(): |
| 38 | +# boto_mock = Mock(name="boto_session", region_name=REGION) |
| 39 | +# session_mock = Mock( |
| 40 | +# name="sagemaker_session", |
| 41 | +# boto_session=boto_mock, |
| 42 | +# boto_region_name=REGION, |
| 43 | +# config=None, |
| 44 | +# local_mode=False, |
| 45 | +# default_bucket_prefix=None, |
| 46 | +# ) |
| 47 | +# session_mock.return_value.sagemkaer_client = Mock(name="sagemaker_client") |
| 48 | +# session_mock.sts_client.get_caller_identity = Mock(return_value={"Account": ACCOUNT_ID}) |
| 49 | +# create_hub = {"HubArn": "arn:aws:sagemaker:us-east-1:123456789123:hub/mock-hub-name"} |
| 50 | +# session_mock.sagemaker_client.create_hub = Mock(return_value=create_hub) |
| 51 | +# print(session_mock.sagemaker_client) |
| 52 | +# return session_mock |
| 53 | + |
| 54 | + |
| 55 | +def test_instantiates(sagemaker_session): |
| 56 | + hub = CuratedHub(hub_name=HUB_NAME, region=REGION, sagemaker_session=sagemaker_session) |
| 57 | + assert hub.hub_name == HUB_NAME |
| 58 | + assert hub.region == "us-east-1" |
| 59 | + assert hub._sagemaker_session == sagemaker_session |
| 60 | + |
| 61 | + |
| 62 | +def test_instantiates_handles_conflicting_regions(sagemaker_session): |
| 63 | + conflicting_region = "us-east-2" |
| 64 | + |
| 65 | + with pytest.raises(ValueError): |
| 66 | + CuratedHub( |
| 67 | + hub_name=HUB_NAME, region=conflicting_region, sagemaker_session=sagemaker_session |
| 68 | + ) |
| 69 | + |
| 70 | + |
| 71 | +@pytest.mark.parametrize( |
| 72 | + ("hub_name,hub_description,hub_bucket_name,hub_display_name,hub_search_keywords,tags"), |
| 73 | + [ |
| 74 | + pytest.param("MockHub1", "this is my sagemaker hub", None, None, None, None), |
| 75 | + pytest.param( |
| 76 | + "MockHub2", |
| 77 | + "this is my sagemaker hub two", |
| 78 | + None, |
| 79 | + "DisplayMockHub2", |
| 80 | + ["mock", "hub", "123"], |
| 81 | + [{"Key": "tag-key-1", "Value": "tag-value-1"}], |
| 82 | + ), |
| 83 | + ], |
| 84 | +) |
| 85 | +def test_create_with_no_bucket_name( |
| 86 | + sagemaker_session, |
| 87 | + hub_name, |
| 88 | + hub_description, |
| 89 | + hub_bucket_name, |
| 90 | + hub_display_name, |
| 91 | + hub_search_keywords, |
| 92 | + tags, |
| 93 | +): |
| 94 | + create_hub = {"HubArn": f"arn:aws:sagemaker:us-east-1:123456789123:hub/{hub_name}"} |
| 95 | + sagemaker_session.create_hub = Mock(return_value=create_hub) |
| 96 | + hub = CuratedHub(hub_name=hub_name, region=REGION, sagemaker_session=sagemaker_session) |
| 97 | + request = { |
| 98 | + "hub_name": hub_name, |
| 99 | + "hub_description": hub_description, |
| 100 | + "hub_bucket_name": "sagemaker-hubs-us-east-1-123456789123", |
| 101 | + "hub_display_name": hub_display_name, |
| 102 | + "hub_search_keywords": hub_search_keywords, |
| 103 | + "tags": tags, |
| 104 | + } |
| 105 | + response = hub.create( |
| 106 | + description=hub_description, |
| 107 | + display_name=hub_display_name, |
| 108 | + bucket_name=hub_bucket_name, |
| 109 | + search_keywords=hub_search_keywords, |
| 110 | + tags=tags, |
| 111 | + ) |
| 112 | + sagemaker_session.create_hub.assert_called_with(**request) |
| 113 | + assert response == {"HubArn": f"arn:aws:sagemaker:us-east-1:123456789123:hub/{hub_name}"} |
| 114 | + |
| 115 | + |
| 116 | +@pytest.mark.parametrize( |
| 117 | + ("hub_name,hub_description,hub_bucket_name,hub_display_name,hub_search_keywords,tags"), |
| 118 | + [ |
| 119 | + pytest.param("MockHub1", "this is my sagemaker hub", "mock-bucket-123", None, None, None), |
| 120 | + pytest.param( |
| 121 | + "MockHub2", |
| 122 | + "this is my sagemaker hub two", |
| 123 | + "mock-bucket-123", |
| 124 | + "DisplayMockHub2", |
| 125 | + ["mock", "hub", "123"], |
| 126 | + [{"Key": "tag-key-1", "Value": "tag-value-1"}], |
| 127 | + ), |
| 128 | + ], |
| 129 | +) |
| 130 | +def test_create_with_bucket_name( |
| 131 | + sagemaker_session, |
| 132 | + hub_name, |
| 133 | + hub_description, |
| 134 | + hub_bucket_name, |
| 135 | + hub_display_name, |
| 136 | + hub_search_keywords, |
| 137 | + tags, |
| 138 | +): |
| 139 | + create_hub = {"HubArn": f"arn:aws:sagemaker:us-east-1:123456789123:hub/{hub_name}"} |
| 140 | + sagemaker_session.create_hub = Mock(return_value=create_hub) |
| 141 | + hub = CuratedHub(hub_name=hub_name, region=REGION, sagemaker_session=sagemaker_session) |
| 142 | + request = { |
| 143 | + "hub_name": hub_name, |
| 144 | + "hub_description": hub_description, |
| 145 | + "hub_bucket_name": hub_bucket_name, |
| 146 | + "hub_display_name": hub_display_name, |
| 147 | + "hub_search_keywords": hub_search_keywords, |
| 148 | + "tags": tags, |
| 149 | + } |
| 150 | + response = hub.create( |
| 151 | + description=hub_description, |
| 152 | + display_name=hub_display_name, |
| 153 | + bucket_name=hub_bucket_name, |
| 154 | + search_keywords=hub_search_keywords, |
| 155 | + tags=tags, |
| 156 | + ) |
| 157 | + sagemaker_session.create_hub.assert_called_with(**request) |
| 158 | + assert response == {"HubArn": f"arn:aws:sagemaker:us-east-1:123456789123:hub/{hub_name}"} |
0 commit comments