|
| 1 | +# Copyright (c) Microsoft Corporation. All rights reserved. |
| 2 | +# Licensed under the MIT License. |
| 3 | +import typing |
| 4 | +import os |
| 5 | +from unittest.mock import patch |
| 6 | +from tests.utils import testutils |
| 7 | +from azure_functions_worker.constants import \ |
| 8 | + PYTHON_ENABLE_DEPENDENCY_LOG_FILTERING_MODULES |
| 9 | + |
| 10 | +HOST_JSON_TEMPLATE_WITH_LOGLEVEL_INFO = """\ |
| 11 | +{ |
| 12 | + "version": "2.0", |
| 13 | + "logging": { |
| 14 | + "logLevel": { |
| 15 | + "default": "Information" |
| 16 | + } |
| 17 | + } |
| 18 | +} |
| 19 | +""" |
| 20 | + |
| 21 | + |
| 22 | +class TestDependencyLoggingEnabledFunctions(testutils.WebHostTestCase): |
| 23 | + """ |
| 24 | + Tests for cx dependency logging filtering enabled case. |
| 25 | + """ |
| 26 | + |
| 27 | + @classmethod |
| 28 | + def setUpClass(cls): |
| 29 | + os_environ = os.environ.copy() |
| 30 | + # Turn on feature flag |
| 31 | + os_environ[ |
| 32 | + PYTHON_ENABLE_DEPENDENCY_LOG_FILTERING_MODULES] = 'function_app' |
| 33 | + cls._patch_environ = patch.dict('os.environ', os_environ) |
| 34 | + cls._patch_environ.start() |
| 35 | + super().setUpClass() |
| 36 | + |
| 37 | + @classmethod |
| 38 | + def tearDownClass(cls): |
| 39 | + super().tearDownClass() |
| 40 | + cls._patch_environ.stop() |
| 41 | + |
| 42 | + @classmethod |
| 43 | + def get_script_dir(cls): |
| 44 | + return testutils.UNIT_TESTS_FOLDER / 'log_filtering_functions' / \ |
| 45 | + 'dependency_logging_filter' |
| 46 | + |
| 47 | + def test_dependency_logging_filter_enabled(self): |
| 48 | + """ |
| 49 | + Verify when cx dependency logging filter is enabled, cx function |
| 50 | + dependencies logs are not captured. |
| 51 | + """ |
| 52 | + r = self.webhost.request('GET', 'default_template') |
| 53 | + self.assertEqual(r.status_code, 200) |
| 54 | + self.assertEqual(r.text, 'ok') |
| 55 | + |
| 56 | + def check_log_dependency_logging_filter_enabled(self, |
| 57 | + host_out: typing.List[ |
| 58 | + str]): |
| 59 | + self.assertIn('Python HTTP trigger function processed a request.', |
| 60 | + host_out) |
| 61 | + self.assertNotIn('logging from external library', host_out) |
| 62 | + |
| 63 | + |
| 64 | +class TestDependencyLoggingDisabledFunctions(testutils.WebHostTestCase): |
| 65 | + """ |
| 66 | + Tests for cx dependency logging filtering disabled case. |
| 67 | + """ |
| 68 | + |
| 69 | + @classmethod |
| 70 | + def setUpClass(cls): |
| 71 | + os_environ = os.environ.copy() |
| 72 | + # Turn off feature flag |
| 73 | + cls._patch_environ = patch.dict('os.environ', os_environ) |
| 74 | + cls._patch_environ.start() |
| 75 | + super().setUpClass() |
| 76 | + |
| 77 | + @classmethod |
| 78 | + def tearDownClass(cls): |
| 79 | + super().tearDownClass() |
| 80 | + cls._patch_environ.stop() |
| 81 | + |
| 82 | + @classmethod |
| 83 | + def get_script_dir(cls): |
| 84 | + return testutils.UNIT_TESTS_FOLDER / 'log_filtering_functions' / \ |
| 85 | + 'dependency_logging_filter' |
| 86 | + |
| 87 | + def test_dependency_logging_filter_disabled(self): |
| 88 | + """ |
| 89 | + Verify when cx dependency logging filter is disabled, dependencies logs |
| 90 | + are captured. |
| 91 | + """ |
| 92 | + r = self.webhost.request('GET', 'default_template') |
| 93 | + self.assertEqual(r.status_code, 200) |
| 94 | + self.assertEqual(r.text, 'ok') |
| 95 | + |
| 96 | + def check_log_dependency_logging_filter_disabled(self, |
| 97 | + host_out: typing.List[ |
| 98 | + str]): |
| 99 | + self.assertIn('Python HTTP trigger function processed a request.', |
| 100 | + host_out) |
| 101 | + self.assertIn('logging from external library', host_out) |
| 102 | + |
| 103 | + |
| 104 | +class TestDependencyLoggingEnabledBluePrintFunctions( |
| 105 | + testutils.WebHostTestCase): |
| 106 | + """ |
| 107 | + Tests for cx dependency logging filtering with blueprint func enabled case. |
| 108 | + """ |
| 109 | + |
| 110 | + @classmethod |
| 111 | + def setUpClass(cls): |
| 112 | + os_environ = os.environ.copy() |
| 113 | + # Turn off feature flag |
| 114 | + os_environ[ |
| 115 | + PYTHON_ENABLE_DEPENDENCY_LOG_FILTERING_MODULES] = 'function_app,' \ |
| 116 | + 'blueprint' |
| 117 | + cls._patch_environ = patch.dict('os.environ', os_environ) |
| 118 | + cls._patch_environ.start() |
| 119 | + super().setUpClass() |
| 120 | + |
| 121 | + @classmethod |
| 122 | + def tearDownClass(cls): |
| 123 | + super().tearDownClass() |
| 124 | + cls._patch_environ.stop() |
| 125 | + |
| 126 | + @classmethod |
| 127 | + def get_script_dir(cls): |
| 128 | + return testutils.UNIT_TESTS_FOLDER / 'log_filtering_functions' / \ |
| 129 | + 'dependency_logging_filter' |
| 130 | + |
| 131 | + def test_dependency_logging_filter_blueprint_enabled(self): |
| 132 | + """ |
| 133 | + Verify when cx dependency logging filter with blueprint func is |
| 134 | + enabled, dependencies logs |
| 135 | + are filtered out. |
| 136 | + """ |
| 137 | + r = self.webhost.request('GET', 'test_blueprint_logging') |
| 138 | + self.assertEqual(r.status_code, 200) |
| 139 | + self.assertEqual(r.text, 'ok') |
| 140 | + |
| 141 | + def check_log_dependency_logging_filter_blueprint_enabled(self, |
| 142 | + host_out: |
| 143 | + typing.List[ |
| 144 | + str]): |
| 145 | + self.assertIn('logging from blueprint', |
| 146 | + host_out) |
| 147 | + self.assertNotIn('logging from external library', host_out) |
| 148 | + |
| 149 | + |
| 150 | +class TestDependencyLoggingDisabledBluePrintFunctions( |
| 151 | + testutils.WebHostTestCase): |
| 152 | + """ |
| 153 | + Tests for cx dependency logging filtering with blueprint func disabled |
| 154 | + case. |
| 155 | + """ |
| 156 | + |
| 157 | + @classmethod |
| 158 | + def setUpClass(cls): |
| 159 | + os_environ = os.environ.copy() |
| 160 | + cls._patch_environ = patch.dict('os.environ', os_environ) |
| 161 | + cls._patch_environ.start() |
| 162 | + super().setUpClass() |
| 163 | + |
| 164 | + @classmethod |
| 165 | + def tearDownClass(cls): |
| 166 | + super().tearDownClass() |
| 167 | + cls._patch_environ.stop() |
| 168 | + |
| 169 | + @classmethod |
| 170 | + def get_script_dir(cls): |
| 171 | + return testutils.UNIT_TESTS_FOLDER / 'log_filtering_functions' / \ |
| 172 | + 'dependency_logging_filter' |
| 173 | + |
| 174 | + def test_dependency_logging_filter_disabled_blueprint(self): |
| 175 | + """ |
| 176 | + Verify when cx dependency logging filter with blueprint functions is |
| 177 | + disabled, dependencies logs are captured. |
| 178 | + """ |
| 179 | + r = self.webhost.request('GET', 'test_blueprint_logging') |
| 180 | + self.assertEqual(r.status_code, 200) |
| 181 | + self.assertEqual(r.text, 'ok') |
| 182 | + |
| 183 | + def check_log_dependency_logging_filter_disabled_blueprint(self, |
| 184 | + host_out: |
| 185 | + typing.List[ |
| 186 | + str]): |
| 187 | + self.assertIn('logging from blueprint', |
| 188 | + host_out) |
| 189 | + self.assertIn('logging from external library', host_out) |
0 commit comments