|
8 | 8 |
|
9 | 9 | import gevent.monkey |
10 | 10 |
|
| 11 | +import opentelemetry.sdk.extension.aws.resource.ec2 as ec2_resource |
| 12 | +import opentelemetry.sdk.extension.aws.resource.eks as eks_resource |
11 | 13 | from amazon.opentelemetry.distro.patches._instrumentation_patch import ( |
12 | 14 | AWS_GEVENT_PATCH_MODULES, |
13 | 15 | apply_instrumentation_patches, |
@@ -116,6 +118,8 @@ def _run_patch_mechanism_tests(self): |
116 | 118 | """ |
117 | 119 | self._test_botocore_installed_flag() |
118 | 120 | self._reset_mocks() |
| 121 | + self._test_resource_detector_patches() |
| 122 | + self._reset_mocks() |
119 | 123 |
|
120 | 124 | def _test_unpatched_botocore_instrumentation(self): |
121 | 125 | # Kinesis |
@@ -352,6 +356,53 @@ def _test_patched_bedrock_agent_instrumentation(self): |
352 | 356 | self.assertEqual(len(bedrock_agent_success_attributes), 1) |
353 | 357 | self.assertEqual(bedrock_agent_success_attributes[attribute_tuple[0]], attribute_tuple[1]) |
354 | 358 |
|
| 359 | + def _test_resource_detector_patches(self): |
| 360 | + """Test that resource detector patches are applied and work correctly""" |
| 361 | + # Test that the functions were patched |
| 362 | + self.assertIsNotNone(ec2_resource._aws_http_request) |
| 363 | + self.assertIsNotNone(eks_resource._aws_http_request) |
| 364 | + |
| 365 | + # Test EC2 patched function |
| 366 | + with patch("amazon.opentelemetry.distro.patches._resource_detector_patches.urlopen") as mock_urlopen: |
| 367 | + mock_response = MagicMock() |
| 368 | + mock_response.read.return_value = b'{"test": "ec2-data"}' |
| 369 | + mock_urlopen.return_value.__enter__.return_value = mock_response |
| 370 | + |
| 371 | + result = ec2_resource._aws_http_request("GET", "/test/path", {"X-Test": "header"}) |
| 372 | + self.assertEqual(result, '{"test": "ec2-data"}') |
| 373 | + |
| 374 | + # Verify the request was made correctly |
| 375 | + args, kwargs = mock_urlopen.call_args |
| 376 | + request = args[0] |
| 377 | + self.assertEqual(request.full_url, "http://169.254.169.254/test/path") |
| 378 | + self.assertEqual(request.headers, {"X-test": "header"}) |
| 379 | + self.assertEqual(kwargs["timeout"], 5) |
| 380 | + |
| 381 | + # Test EKS patched function |
| 382 | + with patch("amazon.opentelemetry.distro.patches._resource_detector_patches.urlopen") as mock_urlopen, patch( |
| 383 | + "amazon.opentelemetry.distro.patches._resource_detector_patches.ssl.create_default_context" |
| 384 | + ) as mock_ssl: |
| 385 | + mock_response = MagicMock() |
| 386 | + mock_response.read.return_value = b'{"test": "eks-data"}' |
| 387 | + mock_urlopen.return_value.__enter__.return_value = mock_response |
| 388 | + |
| 389 | + mock_context = MagicMock() |
| 390 | + mock_ssl.return_value = mock_context |
| 391 | + |
| 392 | + result = eks_resource._aws_http_request("GET", "/api/v1/test", "Bearer token123") |
| 393 | + self.assertEqual(result, '{"test": "eks-data"}') |
| 394 | + |
| 395 | + # Verify the request was made correctly |
| 396 | + args, kwargs = mock_urlopen.call_args |
| 397 | + request = args[0] |
| 398 | + self.assertEqual(request.full_url, "https://kubernetes.default.svc/api/v1/test") |
| 399 | + self.assertEqual(request.headers, {"Authorization": "Bearer token123"}) |
| 400 | + self.assertEqual(kwargs["timeout"], 5) |
| 401 | + self.assertEqual(kwargs["context"], mock_context) |
| 402 | + |
| 403 | + # Verify SSL context was created with correct CA file |
| 404 | + mock_ssl.assert_called_once_with(cafile="/var/run/secrets/kubernetes.io/serviceaccount/ca.crt") |
| 405 | + |
355 | 406 | def _reset_mocks(self): |
356 | 407 | for method_patch in self.method_patches.values(): |
357 | 408 | method_patch.reset_mock() |
|
0 commit comments