|
| 1 | +# Copyright 2021, OpenCensus Authors |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import os |
| 16 | +import sys |
| 17 | +import unittest |
| 18 | + |
| 19 | +import mock |
| 20 | + |
| 21 | +from opencensus.ext.azure.extension.azure_functions import OpenCensusExtension |
| 22 | + |
| 23 | +IS_SUPPORTED_PYTHON_VERSION = sys.version_info.major == 3 |
| 24 | + |
| 25 | +MOCK_APPINSIGHTS_KEY = 'aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee' |
| 26 | +MOCK_AZURE_EXPORTER_CONNSTRING = ( |
| 27 | + 'InstrumentationKey=11111111-2222-3333-4444-555555555555;' |
| 28 | + 'IngestionEndpoint=https://mock.in.applicationinsights.azure.com/' |
| 29 | +) |
| 30 | + |
| 31 | +unittest.skipIf( |
| 32 | + not IS_SUPPORTED_PYTHON_VERSION, |
| 33 | + 'Azure Functions only support Python 3.x' |
| 34 | +) |
| 35 | +class MockContext(object): |
| 36 | + class MockTraceContext(object): |
| 37 | + Tracestate = 'rojo=00f067aa0ba902b7' |
| 38 | + Traceparent = '00-4bf92f3577b34da6a3ce929d0e0e4736-5fd358d59f88ce45-01' |
| 39 | + |
| 40 | + trace_context = MockTraceContext() |
| 41 | + |
| 42 | +class TestAzureFunctionsExtension(unittest.TestCase): |
| 43 | + def setUp(self): |
| 44 | + self._instance = OpenCensusExtension |
| 45 | + OpenCensusExtension.init() |
| 46 | + os.environ['APPINSIGHTS_INSTRUMENTATIONKEY'] = MOCK_APPINSIGHTS_KEY |
| 47 | + |
| 48 | + def tearDown(self): |
| 49 | + if 'APPINSIGHTS_INSTRUMENTATIONKEY' in os.environ: |
| 50 | + del os.environ['APPINSIGHTS_INSTRUMENTATIONKEY'] |
| 51 | + |
| 52 | + @mock.patch('opencensus.ext.azure.extension.azure_functions' |
| 53 | + '.config_integration') |
| 54 | + def test_configure_method_should_setup_trace_integration(self, cfg_mock): |
| 55 | + self._instance.configure(['requests']) |
| 56 | + cfg_mock.trace_integrations.assert_called_once_with(['requests']) |
| 57 | + |
| 58 | + @mock.patch('opencensus.ext.azure.extension.azure_functions' |
| 59 | + '.AzureExporter') |
| 60 | + def test_configure_method_should_setup_azure_exporter( |
| 61 | + self, |
| 62 | + azure_exporter_mock |
| 63 | + ): |
| 64 | + self._instance.configure(['requests']) |
| 65 | + azure_exporter_mock.assert_called_with(connection_string=None) |
| 66 | + |
| 67 | + @mock.patch('opencensus.ext.azure.extension.azure_functions' |
| 68 | + '.AzureExporter') |
| 69 | + def test_configure_method_shouold_setup_azure_exporter_with_connstring( |
| 70 | + self, |
| 71 | + azure_exporter_mock |
| 72 | + ): |
| 73 | + self._instance.configure(['request'], MOCK_AZURE_EXPORTER_CONNSTRING) |
| 74 | + azure_exporter_mock.assert_called_with( |
| 75 | + connection_string=MOCK_AZURE_EXPORTER_CONNSTRING |
| 76 | + ) |
| 77 | + |
| 78 | + def test_pre_invocation_should_warn_if_not_configured(self): |
| 79 | + mock_context = MockContext() |
| 80 | + mock_logger = mock.Mock() |
| 81 | + self._instance.pre_invocation_app_level(mock_logger, mock_context) |
| 82 | + mock_logger.warning.assert_called_once() |
| 83 | + |
| 84 | + def test_pre_invocation_should_attach_tracer_to_context(self): |
| 85 | + # Attach a mock object to exporter |
| 86 | + self._instance._exporter = mock.Mock() |
| 87 | + |
| 88 | + # Check if the tracer is attached to mock_context |
| 89 | + mock_context = MockContext() |
| 90 | + mock_logger = mock.Mock() |
| 91 | + self._instance.pre_invocation_app_level(mock_logger, mock_context) |
| 92 | + self.assertTrue(hasattr(mock_context, 'tracer')) |
| 93 | + |
| 94 | + def test_post_invocation_should_ignore_tracer_deallocation_if_not_set(self): |
| 95 | + mock_context = MockContext() |
| 96 | + mock_logger = mock.Mock() |
| 97 | + mock_func_args = {} |
| 98 | + mock_func_ret = None |
| 99 | + self._instance.post_invocation_app_level( |
| 100 | + mock_logger, mock_context, mock_func_args, mock_func_ret |
| 101 | + ) |
| 102 | + |
| 103 | + def test_post_invocation_should_delete_tracer_from_context(self): |
| 104 | + mock_context = MockContext() |
| 105 | + mock_tracer = mock.Mock() |
| 106 | + setattr(mock_context, 'tracer', mock_tracer) |
| 107 | + mock_logger = mock.Mock() |
| 108 | + mock_func_args = {} |
| 109 | + mock_func_ret = None |
| 110 | + self._instance.post_invocation_app_level( |
| 111 | + mock_logger, mock_context, mock_func_args, mock_func_ret |
| 112 | + ) |
| 113 | + self.assertFalse(hasattr(mock_context, 'tracer')) |
0 commit comments