forked from aws/sagemaker-python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_utils.py
More file actions
322 lines (262 loc) · 11.5 KB
/
test_utils.py
File metadata and controls
322 lines (262 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
# Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"). You
# may not use this file except in compliance with the License. A copy of
# the License is located at
#
# http://aws.amazon.com/apache2.0/
#
# or in the "license" file accompanying this file. This file is
# distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF
# ANY KIND, either express or implied. See the License for the specific
# language governing permissions and limitations under the License.
from __future__ import absolute_import
from unittest.mock import patch, Mock
from sagemaker.jumpstart.types import HubArnExtractedInfo
from sagemaker.jumpstart.constants import JUMPSTART_DEFAULT_REGION_NAME
from sagemaker.jumpstart.hub import parser_utils, utils
def test_get_info_from_hub_resource_arn():
model_arn = (
"arn:aws:sagemaker:us-west-2:000000000000:hub-content/MockHub/Model/my-mock-model/1.0.2"
)
assert utils.get_info_from_hub_resource_arn(model_arn) == HubArnExtractedInfo(
partition="aws",
region="us-west-2",
account_id="000000000000",
hub_name="MockHub",
hub_content_type="Model",
hub_content_name="my-mock-model",
hub_content_version="1.0.2",
)
notebook_arn = "arn:aws:sagemaker:us-west-2:000000000000:hub-content/MockHub/Notebook/my-mock-notebook/1.0.2"
assert utils.get_info_from_hub_resource_arn(notebook_arn) == HubArnExtractedInfo(
partition="aws",
region="us-west-2",
account_id="000000000000",
hub_name="MockHub",
hub_content_type="Notebook",
hub_content_name="my-mock-notebook",
hub_content_version="1.0.2",
)
hub_arn = "arn:aws:sagemaker:us-west-2:000000000000:hub/MockHub"
assert utils.get_info_from_hub_resource_arn(hub_arn) == HubArnExtractedInfo(
partition="aws",
region="us-west-2",
account_id="000000000000",
hub_name="MockHub",
)
invalid_arn = "arn:aws:sagemaker:us-west-2:000000000000:endpoint/my-endpoint-123"
assert None is utils.get_info_from_hub_resource_arn(invalid_arn)
invalid_arn = "nonsense-string"
assert None is utils.get_info_from_hub_resource_arn(invalid_arn)
invalid_arn = ""
assert None is utils.get_info_from_hub_resource_arn(invalid_arn)
def test_construct_hub_arn_from_name():
mock_sagemaker_session = Mock()
mock_sagemaker_session.account_id.return_value = "123456789123"
mock_sagemaker_session.boto_region_name = "us-west-2"
hub_name = "my-cool-hub"
assert (
utils.construct_hub_arn_from_name(hub_name=hub_name, session=mock_sagemaker_session)
== "arn:aws:sagemaker:us-west-2:123456789123:hub/my-cool-hub"
)
assert (
utils.construct_hub_arn_from_name(
hub_name=hub_name, region="us-east-1", session=mock_sagemaker_session
)
== "arn:aws:sagemaker:us-east-1:123456789123:hub/my-cool-hub"
)
def test_construct_hub_model_arn_from_inputs():
model_name, version = "pytorch-ic-imagenet-v2", "1.0.2"
hub_arn = "arn:aws:sagemaker:us-west-2:123456789123:hub/my-mock-hub"
assert (
utils.construct_hub_model_arn_from_inputs(hub_arn, model_name, version)
== "arn:aws:sagemaker:us-west-2:123456789123:hub-content/my-mock-hub/Model/pytorch-ic-imagenet-v2/1.0.2"
)
version = "*"
assert (
utils.construct_hub_model_arn_from_inputs(hub_arn, model_name, version)
== "arn:aws:sagemaker:us-west-2:123456789123:hub-content/my-mock-hub/Model/pytorch-ic-imagenet-v2/*"
)
def test_construct_hub_model_reference_arn_from_inputs():
model_name, version = "pytorch-ic-imagenet-v2", "1.0.2"
hub_arn = "arn:aws:sagemaker:us-west-2:123456789123:hub/my-mock-hub"
hub_content_arn_prefix = "arn:aws:sagemaker:us-west-2:123456789123:hub-content/my-mock-hub"
assert (
utils.construct_hub_model_reference_arn_from_inputs(hub_arn, model_name, version)
== hub_content_arn_prefix + "/ModelReference/pytorch-ic-imagenet-v2/1.0.2"
)
version = "*"
assert (
utils.construct_hub_model_reference_arn_from_inputs(hub_arn, model_name, version)
== hub_content_arn_prefix + "/ModelReference/pytorch-ic-imagenet-v2/*"
)
def test_generate_hub_arn_for_init_kwargs():
hub_name = "my-hub-name"
hub_arn = "arn:aws:sagemaker:us-west-2:12346789123:hub/my-awesome-hub"
# Mock default session with default values
mock_default_session = Mock()
mock_default_session.account_id.return_value = "123456789123"
mock_default_session.boto_region_name = JUMPSTART_DEFAULT_REGION_NAME
# Mock custom session with custom values
mock_custom_session = Mock()
mock_custom_session.account_id.return_value = "000000000000"
mock_custom_session.boto_region_name = "us-east-2"
assert (
utils.generate_hub_arn_for_init_kwargs(hub_name, session=mock_default_session)
== "arn:aws:sagemaker:us-west-2:123456789123:hub/my-hub-name"
)
assert (
utils.generate_hub_arn_for_init_kwargs(hub_name, "us-east-1", session=mock_default_session)
== "arn:aws:sagemaker:us-east-1:123456789123:hub/my-hub-name"
)
assert (
utils.generate_hub_arn_for_init_kwargs(hub_name, "eu-west-1", mock_custom_session)
== "arn:aws:sagemaker:eu-west-1:000000000000:hub/my-hub-name"
)
assert (
utils.generate_hub_arn_for_init_kwargs(hub_name, None, mock_custom_session)
== "arn:aws:sagemaker:us-east-2:000000000000:hub/my-hub-name"
)
assert utils.generate_hub_arn_for_init_kwargs(hub_arn, session=mock_default_session) == hub_arn
assert (
utils.generate_hub_arn_for_init_kwargs(hub_arn, "us-east-1", session=mock_default_session)
== hub_arn
)
assert (
utils.generate_hub_arn_for_init_kwargs(hub_arn, "us-east-1", mock_custom_session) == hub_arn
)
assert utils.generate_hub_arn_for_init_kwargs(hub_arn, None, mock_custom_session) == hub_arn
def test_create_hub_bucket_if_it_does_not_exist_hub_arn():
mock_sagemaker_session = Mock()
mock_sagemaker_session.account_id.return_value = "123456789123"
mock_sagemaker_session.client("sts").get_caller_identity.return_value = {
"Account": "123456789123"
}
hub_arn = "arn:aws:sagemaker:us-west-2:12346789123:hub/my-awesome-hub"
# Mock custom session with custom values
mock_custom_session = Mock()
mock_custom_session.account_id.return_value = "000000000000"
mock_custom_session.boto_region_name = "us-east-2"
mock_sagemaker_session.boto_session.resource("s3").Bucket().creation_date = None
mock_sagemaker_session.boto_region_name = "us-east-1"
bucket_name = "sagemaker-hubs-us-east-1-123456789123"
created_hub_bucket_name = utils.create_hub_bucket_if_it_does_not_exist(
sagemaker_session=mock_sagemaker_session
)
mock_sagemaker_session.boto_session.resource("s3").create_bucketassert_called_once()
assert created_hub_bucket_name == bucket_name
assert utils.generate_hub_arn_for_init_kwargs(hub_arn, None, mock_custom_session) == hub_arn
def test_is_gated_bucket():
assert utils.is_gated_bucket("jumpstart-private-cache-prod-us-west-2") is True
assert utils.is_gated_bucket("jumpstart-private-cache-prod-us-east-1") is True
assert utils.is_gated_bucket("jumpstart-cache-prod-us-west-2") is False
assert utils.is_gated_bucket("") is False
def test_create_hub_bucket_if_it_does_not_exist():
mock_sagemaker_session = Mock()
mock_sagemaker_session.account_id.return_value = "123456789123"
mock_sagemaker_session.client("sts").get_caller_identity.return_value = {
"Account": "123456789123"
}
mock_sagemaker_session.boto_session.resource("s3").Bucket().creation_date = None
mock_sagemaker_session.boto_region_name = "us-east-1"
bucket_name = "sagemaker-hubs-us-east-1-123456789123"
created_hub_bucket_name = utils.create_hub_bucket_if_it_does_not_exist(
sagemaker_session=mock_sagemaker_session
)
mock_sagemaker_session.boto_session.resource("s3").create_bucketassert_called_once()
assert created_hub_bucket_name == bucket_name
@patch("sagemaker.session.Session")
def test_get_hub_model_version_success(mock_session):
hub_name = "test_hub"
hub_model_name = "test_model"
hub_model_type = "test_type"
hub_model_version = "1.0.0"
mock_session.list_hub_content_versions.return_value = {
"HubContentSummaries": [
{"HubContentVersion": "1.0.0"},
{"HubContentVersion": "1.2.3"},
{"HubContentVersion": "2.0.0"},
]
}
result = utils.get_hub_model_version(
hub_name, hub_model_name, hub_model_type, hub_model_version, mock_session
)
assert result == "1.0.0"
@patch("sagemaker.session.Session")
def test_get_hub_model_version_None(mock_session):
hub_name = "test_hub"
hub_model_name = "test_model"
hub_model_type = "test_type"
hub_model_version = None
mock_session.list_hub_content_versions.return_value = {
"HubContentSummaries": [
{"HubContentVersion": "1.0.0"},
{"HubContentVersion": "1.2.3"},
{"HubContentVersion": "2.0.0"},
]
}
result = utils.get_hub_model_version(
hub_name, hub_model_name, hub_model_type, hub_model_version, mock_session
)
assert result == "2.0.0"
@patch("sagemaker.session.Session")
def test_get_hub_model_version_wildcard_char(mock_session):
hub_name = "test_hub"
hub_model_name = "test_model"
hub_model_type = "test_type"
hub_model_version = "*"
mock_session.list_hub_content_versions.return_value = {
"HubContentSummaries": [
{"HubContentVersion": "1.0.0"},
{"HubContentVersion": "1.2.3"},
{"HubContentVersion": "2.0.0"},
]
}
result = utils.get_hub_model_version(
hub_name, hub_model_name, hub_model_type, hub_model_version, mock_session
)
assert result == "2.0.0"
def test_walk_and_apply_json():
test_json = {
"CamelCaseKey": "value",
"CamelCaseObjectKey": {
"CamelCaseObjectChildOne": "value1",
"CamelCaseObjectChildTwo": "value2",
},
"IgnoreMyChildren": {"ShouldNotBeTouchedOne": "const1", "ShouldNotBeTouchedTwo": "const2"},
"ShouldNotIgnoreMyChildren": {"NopeNope": "no"},
}
result = parser_utils.walk_and_apply_json(
test_json, parser_utils.camel_to_snake, ["ignore_my_children"]
)
assert result == {
"camel_case_key": "value",
"camel_case_object_key": {
"camel_case_object_child_one": "value1",
"camel_case_object_child_two": "value2",
},
"ignore_my_children": {
"ShouldNotBeTouchedOne": "const1",
"ShouldNotBeTouchedTwo": "const2",
},
"should_not_ignore_my_children": {"nope_nope": "no"},
}
def test_walk_and_apply_json_no_stop():
test_json = {
"CamelCaseKey": "value",
"CamelCaseObjectKey": {
"CamelCaseObjectChildOne": "value1",
"CamelCaseObjectChildTwo": "value2",
},
"CamelCaseObjectListKey": {"instance.ml.type.xlarge": [{"ShouldChangeMe": "string"}]},
}
result = parser_utils.walk_and_apply_json(test_json, parser_utils.camel_to_snake)
assert result == {
"camel_case_key": "value",
"camel_case_object_key": {
"camel_case_object_child_one": "value1",
"camel_case_object_child_two": "value2",
},
"camel_case_object_list_key": {"instance.ml.type.xlarge": [{"should_change_me": "string"}]},
}