|
| 1 | +import asyncio |
| 2 | +import unittest |
| 3 | +from unittest.mock import patch, MagicMock |
| 4 | + |
| 5 | +from azure_functions_worker import protos |
| 6 | +from tests.unittests.test_dispatcher import FUNCTION_APP_DIRECTORY |
| 7 | +from tests.utils import testutils |
| 8 | + |
| 9 | + |
| 10 | +class TestOpenTelemetry(unittest.TestCase): |
| 11 | + |
| 12 | + def setUp(self): |
| 13 | + self.loop = asyncio.new_event_loop() |
| 14 | + asyncio.set_event_loop(self.loop) |
| 15 | + self.dispatcher = testutils.create_dummy_dispatcher() |
| 16 | + |
| 17 | + def tearDown(self): |
| 18 | + self.loop.close() |
| 19 | + |
| 20 | + def test_update_opentelemetry_status_import_error(self): |
| 21 | + # Patch the built-in import mechanism |
| 22 | + with patch('builtins.__import__', side_effect=ImportError): |
| 23 | + self.dispatcher.update_opentelemetry_status() |
| 24 | + # Verify that otel_libs_available is set to False due to ImportError |
| 25 | + self.assertFalse(self.dispatcher.otel_libs_available) |
| 26 | + |
| 27 | + @patch('builtins.__import__') |
| 28 | + def test_update_opentelemetry_status_success( |
| 29 | + self, mock_imports): |
| 30 | + mock_imports.return_value = MagicMock() |
| 31 | + self.dispatcher.update_opentelemetry_status() |
| 32 | + self.assertTrue(self.dispatcher.otel_libs_available) |
| 33 | + |
| 34 | + @patch('builtins.__import__') |
| 35 | + def test_init_request_otel_capability_enabled( |
| 36 | + self, mock_imports): |
| 37 | + mock_imports.return_value = MagicMock() |
| 38 | + |
| 39 | + init_request = protos.StreamingMessage( |
| 40 | + worker_init_request=protos.WorkerInitRequest( |
| 41 | + host_version="2.3.4", |
| 42 | + function_app_directory=str(FUNCTION_APP_DIRECTORY) |
| 43 | + ) |
| 44 | + ) |
| 45 | + |
| 46 | + init_response = self.loop.run_until_complete( |
| 47 | + self.dispatcher._handle__worker_init_request(init_request)) |
| 48 | + |
| 49 | + self.assertEqual(init_response.worker_init_response.result.status, |
| 50 | + protos.StatusResult.Success) |
| 51 | + |
| 52 | + # Verify that WorkerOpenTelemetryEnabled capability is set to _TRUE |
| 53 | + capabilities = init_response.worker_init_response.capabilities |
| 54 | + self.assertIn("WorkerOpenTelemetryEnabled", capabilities) |
| 55 | + self.assertEqual(capabilities["WorkerOpenTelemetryEnabled"], "true") |
| 56 | + |
| 57 | + def test_init_request_otel_capability_disabled(self): |
| 58 | + |
| 59 | + init_request = protos.StreamingMessage( |
| 60 | + worker_init_request=protos.WorkerInitRequest( |
| 61 | + host_version="2.3.4", |
| 62 | + function_app_directory=str(FUNCTION_APP_DIRECTORY) |
| 63 | + ) |
| 64 | + ) |
| 65 | + |
| 66 | + init_response = self.loop.run_until_complete( |
| 67 | + self.dispatcher._handle__worker_init_request(init_request)) |
| 68 | + |
| 69 | + self.assertEqual(init_response.worker_init_response.result.status, |
| 70 | + protos.StatusResult.Success) |
| 71 | + |
| 72 | + capabilities = init_response.worker_init_response.capabilities |
| 73 | + self.assertNotIn("WorkerOpenTelemetryEnabled", capabilities) |
0 commit comments