forked from aws/sagemaker-python-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_telemetry_logging.py
More file actions
338 lines (291 loc) · 14.5 KB
/
test_telemetry_logging.py
File metadata and controls
338 lines (291 loc) · 14.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
# 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
import unittest
import pytest
import requests
from unittest.mock import Mock, patch, MagicMock
import boto3
import sagemaker
from sagemaker.telemetry.constants import Feature
from sagemaker.telemetry.telemetry_logging import (
_send_telemetry_request,
_telemetry_emitter,
_construct_url,
_get_accountId,
_requests_helper,
_get_region_or_default,
_get_default_sagemaker_session,
OS_NAME_VERSION,
PYTHON_VERSION,
)
from sagemaker.user_agent import SDK_VERSION, process_studio_metadata_file
from sagemaker.serve.utils.exceptions import ModelBuilderException, LocalModelOutOfMemoryException
MOCK_SESSION = Mock()
MOCK_EXCEPTION = LocalModelOutOfMemoryException("mock raise ex")
MOCK_FEATURE = Feature.SDK_DEFAULTS_V2
MOCK_FUNC_NAME = "Mock.local_session.create_model"
MOCK_ENDPOINT_ARN = "arn:aws:sagemaker:us-west-2:123456789012:endpoint/test"
class LocalSagemakerClientMock:
def __init__(self):
self.sagemaker_session = MOCK_SESSION
@_telemetry_emitter(MOCK_FEATURE, MOCK_FUNC_NAME)
def mock_create_model(self, mock_exception_func=None):
if mock_exception_func:
mock_exception_func()
class TestTelemetryLogging(unittest.TestCase):
@patch("sagemaker.telemetry.telemetry_logging._requests_helper")
@patch("sagemaker.telemetry.telemetry_logging._get_accountId")
def test_log_sucessfully(self, mock_get_accountId, mock_request_helper):
"""Test to check if the telemetry logging is successful"""
MOCK_SESSION.boto_session.region_name = "us-west-2"
mock_get_accountId.return_value = "testAccountId"
_send_telemetry_request("someStatus", "1", MOCK_SESSION)
mock_request_helper.assert_called_with(
"https://sm-pysdk-t-us-west-2.s3.us-west-2.amazonaws.com/"
"telemetry?x-accountId=testAccountId&x-status=someStatus&x-feature=1",
2,
)
@patch("sagemaker.telemetry.telemetry_logging._get_accountId")
def test_log_handle_exception(self, mock_get_accountId):
"""Test to check if the exception is handled while logging telemetry"""
mock_get_accountId.side_effect = Exception("Internal error")
_send_telemetry_request("someStatus", "1", MOCK_SESSION)
self.assertRaises(Exception)
@patch("sagemaker.telemetry.telemetry_logging._get_accountId")
@patch("sagemaker.telemetry.telemetry_logging._get_region_or_default")
def test_send_telemetry_request_success(self, mock_get_region, mock_get_accountId):
"""Test to check the _send_telemetry_request function with success status"""
mock_get_accountId.return_value = "testAccountId"
mock_get_region.return_value = "us-west-2"
with patch(
"sagemaker.telemetry.telemetry_logging._requests_helper"
) as mock_requests_helper:
mock_requests_helper.return_value = None
_send_telemetry_request(1, [1, 2], MagicMock(), None, None, "extra_info")
mock_requests_helper.assert_called_with(
"https://sm-pysdk-t-us-west-2.s3.us-west-2.amazonaws.com/"
"telemetry?x-accountId=testAccountId&x-status=1&x-feature=1,2&x-extra=extra_info",
2,
)
@patch("sagemaker.telemetry.telemetry_logging._get_accountId")
@patch("sagemaker.telemetry.telemetry_logging._get_region_or_default")
def test_send_telemetry_request_failure(self, mock_get_region, mock_get_accountId):
"""Test to check the _send_telemetry_request function with failure status"""
mock_get_accountId.return_value = "testAccountId"
mock_get_region.return_value = "us-west-2"
with patch(
"sagemaker.telemetry.telemetry_logging._requests_helper"
) as mock_requests_helper:
mock_requests_helper.return_value = None
_send_telemetry_request(
0, [1, 2], MagicMock(), "failure_reason", "failure_type", "extra_info"
)
mock_requests_helper.assert_called_with(
"https://sm-pysdk-t-us-west-2.s3.us-west-2.amazonaws.com/"
"telemetry?x-accountId=testAccountId&x-status=0&x-feature=1,2"
"&x-failureReason=failure_reason&x-failureType=failure_type&x-extra=extra_info",
2,
)
@patch("sagemaker.telemetry.telemetry_logging._send_telemetry_request")
@patch("sagemaker.telemetry.telemetry_logging.resolve_value_from_config")
def test_telemetry_emitter_decorator_no_call_when_disabled(
self, mock_resolve_config, mock_send_telemetry_request
):
"""Test to check if the _telemetry_emitter decorator is not called when telemetry is disabled"""
mock_resolve_config.return_value = True
assert not mock_send_telemetry_request.called
@patch("sagemaker.telemetry.telemetry_logging._send_telemetry_request")
@patch("sagemaker.telemetry.telemetry_logging.resolve_value_from_config")
def test_telemetry_emitter_decorator_success(
self, mock_resolve_config, mock_send_telemetry_request
):
"""Test to verify the _telemetry_emitter decorator with success status"""
mock_resolve_config.return_value = False
mock_local_client = LocalSagemakerClientMock()
mock_local_client.sagemaker_session.endpoint_arn = MOCK_ENDPOINT_ARN
mock_local_client.mock_create_model()
app_type = process_studio_metadata_file()
args = mock_send_telemetry_request.call_args.args
latency = str(args[5]).split("latency=")[1]
expected_extra_str = (
f"{MOCK_FUNC_NAME}"
f"&x-sdkVersion={SDK_VERSION}"
f"&x-env={PYTHON_VERSION}"
f"&x-sys={OS_NAME_VERSION}"
f"&x-platform={app_type}"
f"&x-endpointArn={MOCK_ENDPOINT_ARN}"
f"&x-latency={latency}"
)
mock_send_telemetry_request.assert_called_once_with(
1, [1, 2], MOCK_SESSION, None, None, expected_extra_str
)
@patch("sagemaker.telemetry.telemetry_logging._send_telemetry_request")
@patch("sagemaker.telemetry.telemetry_logging.resolve_value_from_config")
def test_telemetry_emitter_decorator_handle_exception_success(
self, mock_resolve_config, mock_send_telemetry_request
):
"""Test to verify the _telemetry_emitter decorator when function emits exception"""
mock_resolve_config.return_value = False
mock_local_client = LocalSagemakerClientMock()
mock_local_client.sagemaker_session.endpoint_arn = MOCK_ENDPOINT_ARN
app_type = process_studio_metadata_file()
mock_exception = Mock()
mock_exception_obj = MOCK_EXCEPTION
mock_exception.side_effect = mock_exception_obj
with self.assertRaises(ModelBuilderException) as _:
mock_local_client.mock_create_model(mock_exception)
args = mock_send_telemetry_request.call_args.args
latency = str(args[5]).split("latency=")[1]
expected_extra_str = (
f"{MOCK_FUNC_NAME}"
f"&x-sdkVersion={SDK_VERSION}"
f"&x-env={PYTHON_VERSION}"
f"&x-sys={OS_NAME_VERSION}"
f"&x-platform={app_type}"
f"&x-endpointArn={MOCK_ENDPOINT_ARN}"
f"&x-latency={latency}"
)
mock_send_telemetry_request.assert_called_once_with(
0,
[1, 2],
MOCK_SESSION,
str(mock_exception_obj),
mock_exception_obj.__class__.__name__,
expected_extra_str,
)
def test_construct_url_with_failure_reason_and_extra_info(self):
"""Test to verify the _construct_url function with failure reason and extra info"""
mock_accountId = "testAccountId"
mock_status = 0
mock_feature = "1,2"
mock_failure_reason = str(MOCK_EXCEPTION)
mock_failure_type = MOCK_EXCEPTION.__class__.__name__
mock_extra_info = "mock_extra_info"
mock_region = "us-west-2"
resulted_url = _construct_url(
accountId=mock_accountId,
region=mock_region,
status=mock_status,
feature=mock_feature,
failure_reason=mock_failure_reason,
failure_type=mock_failure_type,
extra_info=mock_extra_info,
)
expected_base_url = (
f"https://sm-pysdk-t-{mock_region}.s3.{mock_region}.amazonaws.com/telemetry?"
f"x-accountId={mock_accountId}"
f"&x-status={mock_status}"
f"&x-feature={mock_feature}"
f"&x-failureReason={mock_failure_reason}"
f"&x-failureType={mock_failure_type}"
f"&x-extra={mock_extra_info}"
)
self.assertEqual(resulted_url, expected_base_url)
@patch("sagemaker.telemetry.telemetry_logging.requests.get")
def test_requests_helper_success(self, mock_requests_get):
"""Test to verify the _requests_helper function with success status"""
mock_response = MagicMock()
mock_response.status_code = 200
mock_requests_get.return_value = mock_response
url = "https://example.com"
timeout = 10
response = _requests_helper(url, timeout)
mock_requests_get.assert_called_once_with(url, timeout)
self.assertEqual(response, mock_response)
@patch("sagemaker.telemetry.telemetry_logging.requests.get")
def test_requests_helper_exception(self, mock_requests_get):
"""Test to verify the _requests_helper function with exception"""
mock_requests_get.side_effect = requests.exceptions.RequestException("Error making request")
url = "https://example.com"
timeout = 10
response = _requests_helper(url, timeout)
mock_requests_get.assert_called_once_with(url, timeout)
self.assertIsNone(response)
def test_get_accountId_success(self):
"""Test to verify the _get_accountId function with success status"""
boto_mock = MagicMock(name="boto_session")
boto_mock.client("sts").get_caller_identity.return_value = {"Account": "testAccountId"}
session = sagemaker.Session(boto_session=boto_mock)
account_id = _get_accountId(session)
self.assertEqual(account_id, "testAccountId")
def test_get_accountId_exception(self):
"""Test to verify the _get_accountId function with exception"""
sts_client_mock = MagicMock()
sts_client_mock.side_effect = Exception("Error creating STS client")
boto_mock = MagicMock(name="boto_session")
boto_mock.client("sts").get_caller_identity.return_value = sts_client_mock
session = sagemaker.Session(boto_session=boto_mock)
with pytest.raises(Exception) as exception:
account_id = _get_accountId(session)
assert account_id is None
assert "Error creating STS client" in str(exception)
def test_get_region_or_default_success(self):
"""Test to verify the _get_region_or_default function with success status"""
mock_session = MagicMock()
mock_session.boto_session = MagicMock(region_name="us-east-1")
region = _get_region_or_default(mock_session)
assert region == "us-east-1"
def test_get_region_or_default_exception(self):
"""Test to verify the _get_region_or_default function with exception"""
mock_session = MagicMock()
mock_session.boto_session = MagicMock()
mock_session.boto_session.region_name.side_effect = Exception("Error creating boto session")
with pytest.raises(Exception) as exception:
region = _get_region_or_default(mock_session)
assert region == "us-west-2"
assert "Error creating boto session" in str(exception)
@patch.object(boto3.Session, "region_name", "us-west-2")
def test_get_default_sagemaker_session(self):
sagemaker_session = _get_default_sagemaker_session()
assert isinstance(sagemaker_session, sagemaker.Session) is True
assert sagemaker_session.boto_session.region_name == "us-west-2"
@patch.object(boto3.Session, "region_name", None)
def test_get_default_sagemaker_session_with_no_region(self):
with self.assertRaises(ValueError) as context:
_get_default_sagemaker_session()
assert "Must setup local AWS configuration with a region supported by SageMaker." in str(
context.exception
)
@patch("sagemaker.telemetry.telemetry_logging._get_accountId")
@patch("sagemaker.telemetry.telemetry_logging._get_region_or_default")
def test_send_telemetry_request_valid_region(self, mock_get_region, mock_get_accountId):
"""Test to verify telemetry request is sent when region is valid"""
mock_get_accountId.return_value = "testAccountId"
mock_session = MagicMock()
# Test with valid region
mock_get_region.return_value = "us-east-1"
with patch(
"sagemaker.telemetry.telemetry_logging._requests_helper"
) as mock_requests_helper:
_send_telemetry_request(1, [1, 2], mock_session)
# Assert telemetry request was sent
mock_requests_helper.assert_called_once_with(
"https://sm-pysdk-t-us-east-1.s3.us-east-1.amazonaws.com/telemetry?"
"x-accountId=testAccountId&x-status=1&x-feature=1,2",
2,
)
@patch("sagemaker.telemetry.telemetry_logging._get_accountId")
@patch("sagemaker.telemetry.telemetry_logging._get_region_or_default")
def test_send_telemetry_request_invalid_region(self, mock_get_region, mock_get_accountId):
"""Test to verify telemetry request is not sent when region is invalid"""
mock_get_accountId.return_value = "testAccountId"
mock_session = MagicMock()
# Test with invalid region
mock_get_region.return_value = "invalid-region"
with patch(
"sagemaker.telemetry.telemetry_logging._requests_helper"
) as mock_requests_helper:
_send_telemetry_request(1, [1, 2], mock_session)
# Assert telemetry request was not sent
mock_requests_helper.assert_not_called()