Skip to content

Commit 5f18b40

Browse files
committed
add resource detector patch unit tests for code coverage threshold
1 parent 80c3296 commit 5f18b40

File tree

1 file changed

+51
-0
lines changed

1 file changed

+51
-0
lines changed

aws-opentelemetry-distro/tests/amazon/opentelemetry/distro/test_instrumentation_patch.py

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88

99
import gevent.monkey
1010

11+
import opentelemetry.sdk.extension.aws.resource.ec2 as ec2_resource
12+
import opentelemetry.sdk.extension.aws.resource.eks as eks_resource
1113
from amazon.opentelemetry.distro.patches._instrumentation_patch import (
1214
AWS_GEVENT_PATCH_MODULES,
1315
apply_instrumentation_patches,
@@ -116,6 +118,8 @@ def _run_patch_mechanism_tests(self):
116118
"""
117119
self._test_botocore_installed_flag()
118120
self._reset_mocks()
121+
self._test_resource_detector_patches()
122+
self._reset_mocks()
119123

120124
def _test_unpatched_botocore_instrumentation(self):
121125
# Kinesis
@@ -352,6 +356,53 @@ def _test_patched_bedrock_agent_instrumentation(self):
352356
self.assertEqual(len(bedrock_agent_success_attributes), 1)
353357
self.assertEqual(bedrock_agent_success_attributes[attribute_tuple[0]], attribute_tuple[1])
354358

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+
355406
def _reset_mocks(self):
356407
for method_patch in self.method_patches.values():
357408
method_patch.reset_mock()

0 commit comments

Comments
 (0)