|
| 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 unittest |
| 15 | +import pytest |
| 16 | +import requests |
| 17 | +from unittest.mock import Mock, patch, MagicMock |
| 18 | +import sagemaker |
| 19 | +from sagemaker.telemetry.constants import Feature |
| 20 | +from sagemaker.telemetry.telemetry_logging import ( |
| 21 | + _send_telemetry_request, |
| 22 | + _telemetry_emitter, |
| 23 | + _construct_url, |
| 24 | + _get_accountId, |
| 25 | + _requests_helper, |
| 26 | + _get_region_or_default, |
| 27 | + OS_NAME_VERSION, |
| 28 | + PYTHON_VERSION, |
| 29 | +) |
| 30 | +from sagemaker.user_agent import SDK_VERSION, process_studio_metadata_file |
| 31 | +from sagemaker.serve.utils.exceptions import ModelBuilderException, LocalModelOutOfMemoryException |
| 32 | + |
| 33 | +MOCK_SESSION = Mock() |
| 34 | +MOCK_EXCEPTION = LocalModelOutOfMemoryException("mock raise ex") |
| 35 | +MOCK_FEATURE = Feature.SDK_DEFAULTS |
| 36 | +MOCK_FUNC_NAME = "Mock.local_session.create_model" |
| 37 | +MOCK_ENDPOINT_ARN = "arn:aws:sagemaker:us-west-2:123456789012:endpoint/test" |
| 38 | + |
| 39 | + |
| 40 | +class LocalSagemakerClientMock: |
| 41 | + def __init__(self): |
| 42 | + self.sagemaker_session = MOCK_SESSION |
| 43 | + |
| 44 | + @_telemetry_emitter(MOCK_FEATURE, MOCK_FUNC_NAME) |
| 45 | + def mock_create_model(self, mock_exception_func=None): |
| 46 | + if mock_exception_func: |
| 47 | + mock_exception_func() |
| 48 | + |
| 49 | + |
| 50 | +class TestTelemetryLogging(unittest.TestCase): |
| 51 | + @patch("sagemaker.telemetry.telemetry_logging._requests_helper") |
| 52 | + @patch("sagemaker.telemetry.telemetry_logging._get_accountId") |
| 53 | + def test_log_sucessfully(self, mock_get_accountId, mock_request_helper): |
| 54 | + """Test to check if the telemetry logging is successful""" |
| 55 | + MOCK_SESSION.boto_session.region_name = "us-west-2" |
| 56 | + mock_get_accountId.return_value = "testAccountId" |
| 57 | + _send_telemetry_request("someStatus", "1", MOCK_SESSION) |
| 58 | + mock_request_helper.assert_called_with( |
| 59 | + "https://sm-pysdk-t-us-west-2.s3.us-west-2.amazonaws.com/" |
| 60 | + "telemetry?x-accountId=testAccountId&x-status=someStatus&x-feature=1", |
| 61 | + 2, |
| 62 | + ) |
| 63 | + |
| 64 | + @patch("sagemaker.telemetry.telemetry_logging._get_accountId") |
| 65 | + def test_log_handle_exception(self, mock_get_accountId): |
| 66 | + """Test to check if the exception is handled while logging telemetry""" |
| 67 | + mock_get_accountId.side_effect = Exception("Internal error") |
| 68 | + _send_telemetry_request("someStatus", "1", MOCK_SESSION) |
| 69 | + self.assertRaises(Exception) |
| 70 | + |
| 71 | + @patch("sagemaker.telemetry.telemetry_logging._get_accountId") |
| 72 | + @patch("sagemaker.telemetry.telemetry_logging._get_region_or_default") |
| 73 | + def test_send_telemetry_request_success(self, mock_get_region, mock_get_accountId): |
| 74 | + """Test to check the _send_telemetry_request function with success status""" |
| 75 | + mock_get_accountId.return_value = "testAccountId" |
| 76 | + mock_get_region.return_value = "us-west-2" |
| 77 | + |
| 78 | + with patch( |
| 79 | + "sagemaker.telemetry.telemetry_logging._requests_helper" |
| 80 | + ) as mock_requests_helper: |
| 81 | + mock_requests_helper.return_value = None |
| 82 | + _send_telemetry_request(1, [1, 2], MagicMock(), None, None, "extra_info") |
| 83 | + mock_requests_helper.assert_called_with( |
| 84 | + "https://sm-pysdk-t-us-west-2.s3.us-west-2.amazonaws.com/" |
| 85 | + "telemetry?x-accountId=testAccountId&x-status=1&x-feature=1,2&x-extra=extra_info", |
| 86 | + 2, |
| 87 | + ) |
| 88 | + |
| 89 | + @patch("sagemaker.telemetry.telemetry_logging._get_accountId") |
| 90 | + @patch("sagemaker.telemetry.telemetry_logging._get_region_or_default") |
| 91 | + def test_send_telemetry_request_failure(self, mock_get_region, mock_get_accountId): |
| 92 | + """Test to check the _send_telemetry_request function with failure status""" |
| 93 | + mock_get_accountId.return_value = "testAccountId" |
| 94 | + mock_get_region.return_value = "us-west-2" |
| 95 | + |
| 96 | + with patch( |
| 97 | + "sagemaker.telemetry.telemetry_logging._requests_helper" |
| 98 | + ) as mock_requests_helper: |
| 99 | + mock_requests_helper.return_value = None |
| 100 | + _send_telemetry_request( |
| 101 | + 0, [1, 2], MagicMock(), "failure_reason", "failure_type", "extra_info" |
| 102 | + ) |
| 103 | + mock_requests_helper.assert_called_with( |
| 104 | + "https://sm-pysdk-t-us-west-2.s3.us-west-2.amazonaws.com/" |
| 105 | + "telemetry?x-accountId=testAccountId&x-status=0&x-feature=1,2" |
| 106 | + "&x-failureReason=failure_reason&x-failureType=failure_type&x-extra=extra_info", |
| 107 | + 2, |
| 108 | + ) |
| 109 | + |
| 110 | + @patch("sagemaker.telemetry.telemetry_logging._send_telemetry_request") |
| 111 | + @patch("sagemaker.telemetry.telemetry_logging.resolve_value_from_config") |
| 112 | + def test_telemetry_emitter_decorator_no_call_when_disabled( |
| 113 | + self, mock_resolve_config, mock_send_telemetry_request |
| 114 | + ): |
| 115 | + """Test to check if the _telemetry_emitter decorator is not called when telemetry is disabled""" |
| 116 | + mock_resolve_config.return_value = True |
| 117 | + |
| 118 | + assert not mock_send_telemetry_request.called |
| 119 | + |
| 120 | + @patch("sagemaker.telemetry.telemetry_logging._send_telemetry_request") |
| 121 | + @patch("sagemaker.telemetry.telemetry_logging.resolve_value_from_config") |
| 122 | + def test_telemetry_emitter_decorator_success( |
| 123 | + self, mock_resolve_config, mock_send_telemetry_request |
| 124 | + ): |
| 125 | + """Test to verify the _telemetry_emitter decorator with success status""" |
| 126 | + mock_resolve_config.return_value = False |
| 127 | + mock_local_client = LocalSagemakerClientMock() |
| 128 | + mock_local_client.sagemaker_session.endpoint_arn = MOCK_ENDPOINT_ARN |
| 129 | + mock_local_client.mock_create_model() |
| 130 | + app_type = process_studio_metadata_file() |
| 131 | + |
| 132 | + args = mock_send_telemetry_request.call_args.args |
| 133 | + latency = str(args[5]).split("latency=")[1] |
| 134 | + expected_extra_str = ( |
| 135 | + f"{MOCK_FUNC_NAME}" |
| 136 | + f"&x-sdkVersion={SDK_VERSION}" |
| 137 | + f"&x-env={PYTHON_VERSION}" |
| 138 | + f"&x-sys={OS_NAME_VERSION}" |
| 139 | + f"&x-platform={app_type}" |
| 140 | + f"&x-endpointArn={MOCK_ENDPOINT_ARN}" |
| 141 | + f"&x-latency={latency}" |
| 142 | + ) |
| 143 | + |
| 144 | + mock_send_telemetry_request.assert_called_once_with( |
| 145 | + 1, [1, 2], MOCK_SESSION, None, None, expected_extra_str |
| 146 | + ) |
| 147 | + |
| 148 | + @patch("sagemaker.telemetry.telemetry_logging._send_telemetry_request") |
| 149 | + @patch("sagemaker.telemetry.telemetry_logging.resolve_value_from_config") |
| 150 | + def test_telemetry_emitter_decorator_handle_exception_success( |
| 151 | + self, mock_resolve_config, mock_send_telemetry_request |
| 152 | + ): |
| 153 | + """Test to verify the _telemetry_emitter decorator when function emits exception""" |
| 154 | + mock_resolve_config.return_value = False |
| 155 | + mock_local_client = LocalSagemakerClientMock() |
| 156 | + mock_local_client.sagemaker_session.endpoint_arn = MOCK_ENDPOINT_ARN |
| 157 | + app_type = process_studio_metadata_file() |
| 158 | + |
| 159 | + mock_exception = Mock() |
| 160 | + mock_exception_obj = MOCK_EXCEPTION |
| 161 | + mock_exception.side_effect = mock_exception_obj |
| 162 | + |
| 163 | + with self.assertRaises(ModelBuilderException) as _: |
| 164 | + mock_local_client.mock_create_model(mock_exception) |
| 165 | + |
| 166 | + args = mock_send_telemetry_request.call_args.args |
| 167 | + latency = str(args[5]).split("latency=")[1] |
| 168 | + expected_extra_str = ( |
| 169 | + f"{MOCK_FUNC_NAME}" |
| 170 | + f"&x-sdkVersion={SDK_VERSION}" |
| 171 | + f"&x-env={PYTHON_VERSION}" |
| 172 | + f"&x-sys={OS_NAME_VERSION}" |
| 173 | + f"&x-platform={app_type}" |
| 174 | + f"&x-endpointArn={MOCK_ENDPOINT_ARN}" |
| 175 | + f"&x-latency={latency}" |
| 176 | + ) |
| 177 | + |
| 178 | + mock_send_telemetry_request.assert_called_once_with( |
| 179 | + 0, |
| 180 | + [1, 2], |
| 181 | + MOCK_SESSION, |
| 182 | + str(mock_exception_obj), |
| 183 | + mock_exception_obj.__class__.__name__, |
| 184 | + expected_extra_str, |
| 185 | + ) |
| 186 | + |
| 187 | + def test_construct_url_with_failure_reason_and_extra_info(self): |
| 188 | + """Test to verify the _construct_url function with failure reason and extra info""" |
| 189 | + mock_accountId = "testAccountId" |
| 190 | + mock_status = 0 |
| 191 | + mock_feature = "1,2" |
| 192 | + mock_failure_reason = str(MOCK_EXCEPTION) |
| 193 | + mock_failure_type = MOCK_EXCEPTION.__class__.__name__ |
| 194 | + mock_extra_info = "mock_extra_info" |
| 195 | + mock_region = "us-west-2" |
| 196 | + |
| 197 | + resulted_url = _construct_url( |
| 198 | + accountId=mock_accountId, |
| 199 | + region=mock_region, |
| 200 | + status=mock_status, |
| 201 | + feature=mock_feature, |
| 202 | + failure_reason=mock_failure_reason, |
| 203 | + failure_type=mock_failure_type, |
| 204 | + extra_info=mock_extra_info, |
| 205 | + ) |
| 206 | + |
| 207 | + expected_base_url = ( |
| 208 | + f"https://sm-pysdk-t-{mock_region}.s3.{mock_region}.amazonaws.com/telemetry?" |
| 209 | + f"x-accountId={mock_accountId}" |
| 210 | + f"&x-status={mock_status}" |
| 211 | + f"&x-feature={mock_feature}" |
| 212 | + f"&x-failureReason={mock_failure_reason}" |
| 213 | + f"&x-failureType={mock_failure_type}" |
| 214 | + f"&x-extra={mock_extra_info}" |
| 215 | + ) |
| 216 | + self.assertEqual(resulted_url, expected_base_url) |
| 217 | + |
| 218 | + @patch("sagemaker.telemetry.telemetry_logging.requests.get") |
| 219 | + def test_requests_helper_success(self, mock_requests_get): |
| 220 | + """Test to verify the _requests_helper function with success status""" |
| 221 | + mock_response = MagicMock() |
| 222 | + mock_response.status_code = 200 |
| 223 | + mock_requests_get.return_value = mock_response |
| 224 | + url = "https://example.com" |
| 225 | + timeout = 10 |
| 226 | + |
| 227 | + response = _requests_helper(url, timeout) |
| 228 | + |
| 229 | + mock_requests_get.assert_called_once_with(url, timeout) |
| 230 | + self.assertEqual(response, mock_response) |
| 231 | + |
| 232 | + @patch("sagemaker.telemetry.telemetry_logging.requests.get") |
| 233 | + def test_requests_helper_exception(self, mock_requests_get): |
| 234 | + """Test to verify the _requests_helper function with exception""" |
| 235 | + mock_requests_get.side_effect = requests.exceptions.RequestException("Error making request") |
| 236 | + url = "https://example.com" |
| 237 | + timeout = 10 |
| 238 | + |
| 239 | + response = _requests_helper(url, timeout) |
| 240 | + |
| 241 | + mock_requests_get.assert_called_once_with(url, timeout) |
| 242 | + self.assertIsNone(response) |
| 243 | + |
| 244 | + def test_get_accountId_success(self): |
| 245 | + """Test to verify the _get_accountId function with success status""" |
| 246 | + boto_mock = MagicMock(name="boto_session") |
| 247 | + boto_mock.client("sts").get_caller_identity.return_value = {"Account": "testAccountId"} |
| 248 | + session = sagemaker.Session(boto_session=boto_mock) |
| 249 | + account_id = _get_accountId(session) |
| 250 | + |
| 251 | + self.assertEqual(account_id, "testAccountId") |
| 252 | + |
| 253 | + def test_get_accountId_exception(self): |
| 254 | + """Test to verify the _get_accountId function with exception""" |
| 255 | + sts_client_mock = MagicMock() |
| 256 | + sts_client_mock.side_effect = Exception("Error creating STS client") |
| 257 | + boto_mock = MagicMock(name="boto_session") |
| 258 | + boto_mock.client("sts").get_caller_identity.return_value = sts_client_mock |
| 259 | + session = sagemaker.Session(boto_session=boto_mock) |
| 260 | + |
| 261 | + with pytest.raises(Exception) as exception: |
| 262 | + account_id = _get_accountId(session) |
| 263 | + assert account_id is None |
| 264 | + assert "Error creating STS client" in str(exception) |
| 265 | + |
| 266 | + def test_get_region_or_default_success(self): |
| 267 | + """Test to verify the _get_region_or_default function with success status""" |
| 268 | + mock_session = MagicMock() |
| 269 | + mock_session.boto_session = MagicMock(region_name="us-east-1") |
| 270 | + |
| 271 | + region = _get_region_or_default(mock_session) |
| 272 | + |
| 273 | + assert region == "us-east-1" |
| 274 | + |
| 275 | + def test_get_region_or_default_exception(self): |
| 276 | + """Test to verify the _get_region_or_default function with exception""" |
| 277 | + mock_session = MagicMock() |
| 278 | + mock_session.boto_session = MagicMock() |
| 279 | + mock_session.boto_session.region_name.side_effect = Exception("Error creating boto session") |
| 280 | + |
| 281 | + with pytest.raises(Exception) as exception: |
| 282 | + region = _get_region_or_default(mock_session) |
| 283 | + assert region == "us-west-2" |
| 284 | + assert "Error creating boto session" in str(exception) |
0 commit comments