Skip to content

Commit 6cee898

Browse files
committed
test fixes
1 parent 6cba801 commit 6cee898

File tree

3 files changed

+21
-7
lines changed

3 files changed

+21
-7
lines changed

tests/unittests/test_handle_event.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ async def test_function_environment_reload_request(self, mock_index_function_app
136136
BASIC_FUNCTION_DIRECTORY)),
137137
properties={'host': '123',
138138
'protos': test_protos})
139+
handle_event.protos = test_protos
139140
result = await function_environment_reload_request(worker_request)
140141
self.assertEqual(result.capabilities, {})
141142
self.assertEqual(result.worker_metadata.runtime_name, "python")
@@ -152,6 +153,7 @@ async def test_function_environment_reload_request_with_streaming(
152153
self,
153154
mock_http_v2_enabled,
154155
mock_initialize_http_server):
156+
handle_event.protos = test_protos
155157
worker_request = WorkerRequest(name='function_environment_reload_request',
156158
request=Request(FunctionRequest(
157159
'hello',
@@ -172,6 +174,7 @@ async def test_function_environment_reload_request_with_streaming(
172174
return_value=True)
173175
async def test_function_environment_reload_request_with_otel(self,
174176
mock_otel_enabled):
177+
handle_event.protos = test_protos
175178
worker_request = WorkerRequest(name='function_environment_reload_request',
176179
request=Request(FunctionRequest(
177180
'hello',
@@ -189,6 +192,7 @@ async def test_function_environment_reload_request_with_otel(self,
189192
async def test_function_environment_reload_request_with_exception(self):
190193
# Even if an exception happens during indexing,
191194
# we still return success
195+
handle_event.protos = test_protos
192196
worker_request = WorkerRequest(name='function_environment_reload_request',
193197
request=Request(FunctionRequest(
194198
'hello',

tests/unittests/test_opentelemetry.py

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
from azure_functions_worker_v2.handle_event import otel_manager, worker_init_request
88
from azure_functions_worker_v2.otel import (initialize_azure_monitor,
99
update_opentelemetry_status)
10+
from azure_functions_worker_v2.logging import logger
1011
from tests.utils.constants import UNIT_TESTS_FOLDER
1112
from tests.utils.mock_classes import FunctionRequest, Request, WorkerRequest
1213
from unittest.mock import MagicMock, patch
@@ -17,12 +18,21 @@
1718

1819
class TestOpenTelemetry(unittest.TestCase):
1920

20-
@patch('builtins.__import__', side_effect=ImportError)
21-
def test_update_opentelemetry_status_import_error(self, mock_import_error):
22-
update_opentelemetry_status()
23-
# Verify that context variables are None due to ImportError
24-
self.assertIsNone(otel_manager.get_context_api())
25-
self.assertIsNone(otel_manager.get_trace_context_propagator())
21+
def test_update_opentelemetry_status_import_error(self):
22+
with patch.dict('sys.modules', {
23+
'opentelemetry': None,
24+
'opentelemetry.context': None,
25+
'opentelemetry.trace': None,
26+
'opentelemetry.trace.propagation': None,
27+
'opentelemetry.trace.propagation.tracecontext': None,
28+
}):
29+
# Verify that context variables are None due to ImportError
30+
with self.assertLogs(logger.name, 'ERROR') as cm:
31+
update_opentelemetry_status()
32+
self.assertTrue(
33+
any("Cannot import OpenTelemetry libraries."
34+
in message for message in cm.output)
35+
)
2636

2737
@patch('builtins.__import__')
2838
def test_update_opentelemetry_status_success(

tests/utils/mock_classes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ def __init__(self, name: Any):
2222
class FunctionRequest:
2323
def __init__(self, capabilities: Any,
2424
function_app_directory: Any,
25-
environment_variables: Optional[Any] = None):
25+
environment_variables: Optional[Any] = {}):
2626
self.capabilities = capabilities
2727
self.function_app_directory = function_app_directory
2828
self.environment_variables = environment_variables

0 commit comments

Comments
 (0)