|
1 | 1 | # Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
|
2 | 2 | # SPDX-License-Identifier: Apache-2.0
|
3 |
| -from unittest import TestCase |
| 3 | +import unittest |
4 | 4 | from unittest.mock import MagicMock, patch
|
5 | 5 |
|
6 | 6 | from amazon.opentelemetry.distro.patches._starlette_patches import _apply_starlette_instrumentation_patches
|
7 | 7 |
|
8 | 8 |
|
9 |
| -class TestStarlettePatch(TestCase): |
| 9 | +class TestStarlettePatch(unittest.TestCase): |
10 | 10 | """Test the Starlette instrumentation patches."""
|
11 | 11 |
|
12 |
| - @patch("amazon.opentelemetry.distro.patches._starlette_patches.AGENT_OBSERVABILITY_ENABLED", True) |
13 | 12 | @patch("amazon.opentelemetry.distro.patches._starlette_patches._logger")
|
14 | 13 | def test_starlette_patch_applied_successfully(self, mock_logger):
|
15 | 14 | """Test that the Starlette instrumentation patch is applied successfully."""
|
16 |
| - # Create a mock StarletteInstrumentor class |
17 |
| - mock_instrumentor_class = MagicMock() |
18 |
| - mock_instrumentor_class.__name__ = "StarletteInstrumentor" |
19 |
| - |
20 |
| - class MockMiddleware: |
21 |
| - def __init__(self, app, **kwargs): |
22 |
| - pass |
23 |
| - |
24 |
| - mock_middleware_class = MockMiddleware |
25 |
| - original_init = mock_middleware_class.__init__ |
26 |
| - |
27 |
| - # Create mock modules |
28 |
| - mock_starlette_module = MagicMock() |
29 |
| - mock_starlette_module.StarletteInstrumentor = mock_instrumentor_class |
30 |
| - |
31 |
| - mock_asgi_module = MagicMock() |
32 |
| - mock_asgi_module.OpenTelemetryMiddleware = mock_middleware_class |
33 |
| - |
34 |
| - # Mock the imports |
35 |
| - with patch.dict( |
36 |
| - "sys.modules", |
37 |
| - { |
38 |
| - "opentelemetry.instrumentation.starlette": mock_starlette_module, |
39 |
| - "opentelemetry.instrumentation.asgi": mock_asgi_module, |
40 |
| - }, |
41 |
| - ): |
42 |
| - # Apply the patch |
43 |
| - _apply_starlette_instrumentation_patches() |
44 |
| - |
45 |
| - # Verify the instrumentation_dependencies method was replaced |
46 |
| - self.assertTrue(hasattr(mock_instrumentor_class, "instrumentation_dependencies")) |
47 |
| - |
48 |
| - # Test the patched method returns the expected value |
49 |
| - mock_instance = MagicMock() |
50 |
| - result = mock_instrumentor_class.instrumentation_dependencies(mock_instance) |
51 |
| - self.assertEqual(result, ("starlette >= 0.13",)) |
52 |
| - |
53 |
| - self.assertNotEqual(mock_middleware_class.__init__, original_init) |
54 |
| - |
55 |
| - # Test middleware patching sets exclude flags |
56 |
| - mock_middleware_instance = MagicMock() |
57 |
| - mock_middleware_instance.exclude_receive_span = False |
58 |
| - mock_middleware_instance.exclude_send_span = False |
59 |
| - |
60 |
| - mock_middleware_class.__init__(mock_middleware_instance, "app") |
61 |
| - |
62 |
| - self.assertTrue(mock_middleware_instance.exclude_receive_span) |
63 |
| - self.assertTrue(mock_middleware_instance.exclude_send_span) |
64 |
| - |
65 |
| - # Verify logging |
66 |
| - mock_logger.debug.assert_called_once_with( |
67 |
| - "Successfully patched Starlette instrumentation_dependencies method" |
68 |
| - ) |
| 15 | + for agent_enabled in [True, False]: |
| 16 | + with self.subTest(agent_enabled=agent_enabled): |
| 17 | + with patch.dict("os.environ", {"AGENT_OBSERVABILITY_ENABLED": "true" if agent_enabled else "false"}): |
| 18 | + # Create a mock StarletteInstrumentor class |
| 19 | + mock_instrumentor_class = MagicMock() |
| 20 | + mock_instrumentor_class.__name__ = "StarletteInstrumentor" |
| 21 | + |
| 22 | + def create_middleware_class(): |
| 23 | + class MockMiddleware: |
| 24 | + def __init__(self, app, **kwargs): |
| 25 | + pass |
| 26 | + |
| 27 | + return MockMiddleware |
| 28 | + |
| 29 | + mock_middleware_class = create_middleware_class() |
| 30 | + |
| 31 | + mock_starlette_module = MagicMock() |
| 32 | + mock_starlette_module.StarletteInstrumentor = mock_instrumentor_class |
| 33 | + |
| 34 | + mock_asgi_module = MagicMock() |
| 35 | + mock_asgi_module.OpenTelemetryMiddleware = mock_middleware_class |
| 36 | + |
| 37 | + with patch.dict( |
| 38 | + "sys.modules", |
| 39 | + { |
| 40 | + "opentelemetry.instrumentation.starlette": mock_starlette_module, |
| 41 | + "opentelemetry.instrumentation.asgi": mock_asgi_module, |
| 42 | + }, |
| 43 | + ): |
| 44 | + # Apply the patch |
| 45 | + _apply_starlette_instrumentation_patches() |
| 46 | + |
| 47 | + # Verify the instrumentation_dependencies method was replaced |
| 48 | + self.assertTrue(hasattr(mock_instrumentor_class, "instrumentation_dependencies")) |
| 49 | + |
| 50 | + # Test the patched method returns the expected value |
| 51 | + mock_instance = MagicMock() |
| 52 | + result = mock_instrumentor_class.instrumentation_dependencies(mock_instance) |
| 53 | + self.assertEqual(result, ("starlette >= 0.13",)) |
| 54 | + |
| 55 | + mock_middleware_instance = MagicMock() |
| 56 | + mock_middleware_instance.exclude_receive_span = False |
| 57 | + mock_middleware_instance.exclude_send_span = False |
| 58 | + mock_middleware_class.__init__(mock_middleware_instance, "app") |
| 59 | + |
| 60 | + # Test middleware patching sets exclude flags |
| 61 | + if agent_enabled: |
| 62 | + self.assertTrue(mock_middleware_instance.exclude_receive_span) |
| 63 | + self.assertTrue(mock_middleware_instance.exclude_send_span) |
| 64 | + else: |
| 65 | + self.assertFalse(mock_middleware_instance.exclude_receive_span) |
| 66 | + self.assertFalse(mock_middleware_instance.exclude_send_span) |
| 67 | + |
| 68 | + # Verify logging |
| 69 | + mock_logger.debug.assert_called_with( |
| 70 | + "Successfully patched Starlette instrumentation_dependencies method" |
| 71 | + ) |
69 | 72 |
|
70 | 73 | @patch("amazon.opentelemetry.distro.patches._starlette_patches._logger")
|
71 | 74 | def test_starlette_patch_handles_import_error(self, mock_logger):
|
@@ -108,7 +111,7 @@ class MockStarletteInstrumentor(metaclass=ErrorMeta):
|
108 | 111 | args = mock_logger.warning.call_args[0]
|
109 | 112 | self.assertIn("Failed to apply Starlette instrumentation patches", args[0])
|
110 | 113 |
|
111 |
| - def test_starlette_patch_logs_failure_with_no_logger_patch(self): # pylint: disable=no-self-use |
| 114 | + def test_starlette_patch_logs_failure_with_no_logger_patch(self): |
112 | 115 | """Test that the patch handles exceptions gracefully without logger mock."""
|
113 | 116 | # Mock the import to fail
|
114 | 117 | with patch.dict("sys.modules", {"opentelemetry.instrumentation.starlette": None}):
|
|
0 commit comments