|
1 | 1 | import pytest |
2 | | -from unittest.mock import patch, MagicMock |
| 2 | +from unittest.mock import patch, MagicMock, Mock |
3 | 3 | import subprocess |
4 | 4 | from typing import Tuple |
5 | 5 |
|
|
8 | 8 | get_region_and_account_from_current_context, |
9 | 9 | _send_telemetry_request, |
10 | 10 | _hyperpod_telemetry_emitter, |
| 11 | + _requests_helper, |
| 12 | + _construct_url, |
11 | 13 | DEFAULT_AWS_REGION, |
12 | 14 | FEATURE_TO_CODE, |
13 | 15 | ) |
14 | 16 | from sagemaker.hyperpod.common.telemetry.constants import Feature, Status |
| 17 | +import requests |
| 18 | +import logging |
15 | 19 |
|
16 | 20 | # Test data |
17 | 21 | MOCK_CONTEXTS = { |
@@ -199,3 +203,117 @@ def sample_function(succeed: bool): |
199 | 203 | # Check failure call |
200 | 204 | failure_call = mock_telemetry.call_args_list[1] |
201 | 205 | assert failure_call[0][0] == Status.FAILURE |
| 206 | + |
| 207 | + |
| 208 | +# Test _requests_helper |
| 209 | +def test_requests_helper_success(): |
| 210 | + """Test successful request""" |
| 211 | + with patch("requests.get") as mock_get: |
| 212 | + # Setup mock response |
| 213 | + mock_response = Mock() |
| 214 | + mock_response.status_code = 200 |
| 215 | + mock_get.return_value = mock_response |
| 216 | + |
| 217 | + # Make request |
| 218 | + response = _requests_helper("https://test.com", 2) |
| 219 | + |
| 220 | + # Verify |
| 221 | + assert response == mock_response |
| 222 | + mock_get.assert_called_once_with("https://test.com", 2) |
| 223 | + |
| 224 | + |
| 225 | +def test_requests_helper_with_invalid_url(caplog): |
| 226 | + """Test requests helper with invalid URL""" |
| 227 | + with patch("requests.get") as mock_get: |
| 228 | + # Set up the mock to raise InvalidURL |
| 229 | + mock_get.side_effect = requests.exceptions.InvalidURL("Invalid URL") |
| 230 | + |
| 231 | + # Capture logs at DEBUG level |
| 232 | + with caplog.at_level(logging.DEBUG): |
| 233 | + response = _requests_helper("invalid://url", 2) |
| 234 | + |
| 235 | + # Verify response is None |
| 236 | + assert response is None |
| 237 | + |
| 238 | + # Verify log message |
| 239 | + assert "Request exception: Invalid URL" in caplog.text |
| 240 | + |
| 241 | + |
| 242 | +def test_construct_url_basic(): |
| 243 | + """Test basic URL construction""" |
| 244 | + url = _construct_url( |
| 245 | + accountId="123456789012", |
| 246 | + region="us-west-2", |
| 247 | + status="SUCCESS", |
| 248 | + feature="TEST", |
| 249 | + failure_reason=None, |
| 250 | + failure_type=None, |
| 251 | + extra_info=None, |
| 252 | + ) |
| 253 | + |
| 254 | + expected = ( |
| 255 | + "https://sm-pysdk-t-us-west-2.s3.us-west-2.amazonaws.com/telemetry?" |
| 256 | + "x-accountId=123456789012&x-status=SUCCESS&x-feature=TEST" |
| 257 | + ) |
| 258 | + assert url == expected |
| 259 | + |
| 260 | + |
| 261 | +def test_construct_url_with_failure(): |
| 262 | + """Test URL construction with failure information""" |
| 263 | + url = _construct_url( |
| 264 | + accountId="123456789012", |
| 265 | + region="us-west-2", |
| 266 | + status="FAILURE", |
| 267 | + feature="TEST", |
| 268 | + failure_reason="Test failed", |
| 269 | + failure_type="TestError", |
| 270 | + extra_info=None, |
| 271 | + ) |
| 272 | + |
| 273 | + expected = ( |
| 274 | + "https://sm-pysdk-t-us-west-2.s3.us-west-2.amazonaws.com/telemetry?" |
| 275 | + "x-accountId=123456789012&x-status=FAILURE&x-feature=TEST" |
| 276 | + "&x-failureReason=Test failed&x-failureType=TestError" |
| 277 | + ) |
| 278 | + assert url == expected |
| 279 | + |
| 280 | + |
| 281 | +def test_construct_url_with_extra_info(): |
| 282 | + """Test URL construction with extra information""" |
| 283 | + url = _construct_url( |
| 284 | + accountId="123456789012", |
| 285 | + region="us-west-2", |
| 286 | + status="SUCCESS", |
| 287 | + feature="TEST", |
| 288 | + failure_reason=None, |
| 289 | + failure_type=None, |
| 290 | + extra_info="additional=info", |
| 291 | + ) |
| 292 | + |
| 293 | + expected = ( |
| 294 | + "https://sm-pysdk-t-us-west-2.s3.us-west-2.amazonaws.com/telemetry?" |
| 295 | + "x-accountId=123456789012&x-status=SUCCESS&x-feature=TEST" |
| 296 | + "&x-extra=additional=info" |
| 297 | + ) |
| 298 | + assert url == expected |
| 299 | + |
| 300 | + |
| 301 | +def test_construct_url_all_parameters(): |
| 302 | + """Test URL construction with all parameters""" |
| 303 | + url = _construct_url( |
| 304 | + accountId="123456789012", |
| 305 | + region="us-west-2", |
| 306 | + status="FAILURE", |
| 307 | + feature="TEST", |
| 308 | + failure_reason="Test failed", |
| 309 | + failure_type="TestError", |
| 310 | + extra_info="additional=info", |
| 311 | + ) |
| 312 | + |
| 313 | + expected = ( |
| 314 | + "https://sm-pysdk-t-us-west-2.s3.us-west-2.amazonaws.com/telemetry?" |
| 315 | + "x-accountId=123456789012&x-status=FAILURE&x-feature=TEST" |
| 316 | + "&x-failureReason=Test failed&x-failureType=TestError" |
| 317 | + "&x-extra=additional=info" |
| 318 | + ) |
| 319 | + assert url == expected |
0 commit comments