-
Notifications
You must be signed in to change notification settings - Fork 110
Expand file tree
/
Copy pathtest_loader.py
More file actions
297 lines (243 loc) · 11.7 KB
/
test_loader.py
File metadata and controls
297 lines (243 loc) · 11.7 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
# Copyright (c) Microsoft Corporation. All rights reserved.
# Licensed under the MIT License.
import asyncio
import os
import pathlib
import subprocess
import sys
import textwrap
from unittest import skipIf
from unittest.mock import Mock, patch
from azure.functions import Function
from azure.functions.decorators.retry_policy import RetryPolicy
from azure.functions.decorators.timer import TimerTrigger
from tests.utils import testutils
from azure_functions_worker import functions
from azure_functions_worker.constants import (
PYTHON_SCRIPT_FILE_NAME,
PYTHON_SCRIPT_FILE_NAME_DEFAULT,
)
from azure_functions_worker.loader import build_retry_protos
class TestLoader(testutils.WebHostTestCase):
def setUp(self) -> None:
def test_function():
return "Test"
self.test_function = test_function
self.func = Function(self.test_function, script_file="test.py")
self.function_registry = functions.Registry()
@classmethod
def get_script_dir(cls):
return testutils.UNIT_TESTS_FOLDER / 'load_functions'
def test_loader_building_fixed_retry_protos(self):
trigger = TimerTrigger(schedule="*/1 * * * * *", arg_name="mytimer",
name="mytimer")
self.func.add_trigger(trigger=trigger)
setting = RetryPolicy(strategy="fixed_delay", max_retry_count="1",
delay_interval="00:02:00")
self.func.add_setting(setting=setting)
protos = build_retry_protos(self.func)
self.assertEqual(protos.max_retry_count, 1)
self.assertEqual(protos.retry_strategy, 1) # 1 enum for fixed delay
self.assertEqual(protos.delay_interval.seconds, 120)
def test_loader_building_exponential_retry_protos(self):
trigger = TimerTrigger(schedule="*/1 * * * * *", arg_name="mytimer",
name="mytimer")
self.func.add_trigger(trigger=trigger)
setting = RetryPolicy(strategy="exponential_backoff",
max_retry_count="1",
minimum_interval="00:01:00",
maximum_interval="00:02:00")
self.func.add_setting(setting=setting)
protos = build_retry_protos(self.func)
self.assertEqual(protos.max_retry_count, 1)
self.assertEqual(protos.retry_strategy,
0) # 0 enum for exponential backoff
self.assertEqual(protos.minimum_interval.seconds, 60)
self.assertEqual(protos.maximum_interval.seconds, 120)
def test_loader_building_exponential_retry_protos_with_defaults(self):
"""Test exponential backoff without explicit min/max intervals (uses defaults)"""
trigger = TimerTrigger(schedule="*/1 * * * * *", arg_name="mytimer",
name="mytimer")
self.func.add_trigger(trigger=trigger)
setting = RetryPolicy(strategy="exponential_backoff",
max_retry_count="3")
self.func.add_setting(setting=setting)
protos = build_retry_protos(self.func)
self.assertEqual(protos.max_retry_count, 3)
self.assertEqual(protos.retry_strategy,
0) # 0 enum for exponential backoff
self.assertEqual(protos.minimum_interval.seconds, 0) # Default: 0 seconds
self.assertEqual(protos.maximum_interval.seconds, 2147483647) # Default: max int32
@patch('azure_functions_worker.logging.logger.warning')
def test_loader_retry_policy_attribute_error(self, mock_logger):
self.func = Mock()
self.func.get_settings_dict.side_effect = AttributeError('DummyError')
result = build_retry_protos(self.func)
self.assertIsNone(result)
# Check if the logged message starts with the expected string
logged_message = mock_logger.call_args[0][
0] # Get the first argument of the logger.warning call
self.assertTrue(logged_message.startswith(
'AttributeError while loading retry policy.'))
def test_loader_simple(self):
r = self.webhost.request('GET', 'simple')
self.assertEqual(r.status_code, 200)
self.assertEqual(r.text, '__app__.simple.main')
def test_loader_custom_entrypoint(self):
r = self.webhost.request('GET', 'entrypoint')
self.assertEqual(r.status_code, 200)
self.assertEqual(r.text, '__app__.entrypoint.main')
def test_loader_no_script_file(self):
r = self.webhost.request('GET', 'no_script_file')
self.assertEqual(r.status_code, 200)
self.assertEqual(r.text, '__app__.no_script_file.main')
def test_loader_subdir(self):
r = self.webhost.request('GET', 'subdir')
self.assertEqual(r.status_code, 200)
self.assertEqual(r.text, '__app__.subdir.sub.main')
def test_loader_relimport(self):
r = self.webhost.request('GET', 'relimport')
self.assertEqual(r.status_code, 200)
self.assertEqual(r.text, '__app__.relimport.relative')
def test_loader_submodule(self):
r = self.webhost.request('GET', 'submodule')
self.assertEqual(r.status_code, 200)
self.assertEqual(r.text, '__app__.submodule.sub_module.module')
def test_loader_parentmodule(self):
r = self.webhost.request('GET', 'parentmodule')
self.assertEqual(r.status_code, 200)
self.assertEqual(r.text, '__app__.parentmodule.module')
def test_loader_absolute_thirdparty(self):
"""Allow third-party package import from .python_packages
and worker_venv
"""
r = self.webhost.request('GET', 'absolute_thirdparty')
self.assertEqual(r.status_code, 200)
self.assertEqual(r.text, 'eh = azure.eventhub')
def test_loader_prioritize_customer_module(self):
"""When a module in customer code has the same name with a third-party
package, the worker should prioritize third-party package
"""
r = self.webhost.request('GET', 'name_collision')
self.assertEqual(r.status_code, 200)
self.assertRegex(r.text, r'pt.__version__ = \d+.\d+.\d+')
def test_loader_fix_customer_module_with_app_import(self):
"""When a module in customer code has the same name with a third-party
package, if customer uses "import __app__.<module>" statement,
the worker should load customer package
"""
r = self.webhost.request('GET', 'name_collision_app_import')
self.assertEqual(r.status_code, 200)
self.assertEqual(r.text, 'pt.__version__ = from.customer.code')
def test_loader_implicit_import(self):
"""Since sys.path is now fixed with script root appended,
implicit import statement is now acceptable.
"""
r = self.webhost.request('GET', 'implicit_import')
self.assertEqual(r.status_code, 200)
self.assertEqual(r.text, 's_main = simple.main')
def test_loader_module_not_found(self):
"""If a module cannot be found, should throw an exception with
trouble shooting link https://aka.ms/functions-modulenotfound
"""
r = self.webhost.request('GET', 'module_not_found')
self.assertEqual(r.status_code, 500)
def test_loader_init_should_only_invoke_outside_main_once(self):
"""Check if the code in __init__.py outside of main() function
is only executed once
"""
r = self.webhost.request('GET', 'outside_main_code_in_init')
self.assertEqual(r.status_code, 200)
self.assertEqual(r.text, 'executed count = 1')
def test_loader_main_should_only_invoke_outside_main_once(self):
"""Check if the code in main.py outside of main() function
is only executed once
"""
r = self.webhost.request('GET', 'outside_main_code_in_main')
self.assertEqual(r.status_code, 200)
self.assertEqual(r.text, 'executed count = 1')
def test_loader_outside_main_package_should_be_loaded_from_init(self):
"""Check if the package can still be loaded from __init__ module
"""
r = self.webhost.request('GET', 'load_outside_main?from=init')
self.assertEqual(r.status_code, 200)
self.assertEqual(r.text, 'OK')
def test_loader_outside_main_package_should_be_loaded_from_package(self):
"""Check if the package can still be loaded from package
"""
r = self.webhost.request('GET',
'load_outside_main?from=package')
self.assertEqual(r.status_code, 200)
self.assertEqual(r.text, 'OK')
def check_log_loader_module_not_found(self, host_out):
passed = False
exception_message = "Exception: ModuleNotFoundError: "\
"No module named 'notfound'. "\
"Cannot find module. "\
"Please check the requirements.txt file for the "\
"missing module. For more info, please refer the "\
"troubleshooting guide: "\
"https://aka.ms/functions-modulenotfound. "\
"Current sys.path: "
for log in host_out:
if exception_message in log:
passed = True
self.assertTrue(passed)
class TestPluginLoader(testutils.AsyncTestCase):
@skipIf(sys.version_info.minor <= 7, "Skipping tests <= Python 3.7")
async def test_entry_point_plugin(self):
test_binding = pathlib.Path(__file__).parent / 'test-binding'
subprocess.run([
sys.executable, '-m', 'pip',
'--disable-pip-version-check',
'install', '--quiet',
'-e', test_binding
], check=True)
# This test must be run in a subprocess so that
# pkg_resources picks up the newly installed package.
code = textwrap.dedent('''
import asyncio
from azure_functions_worker import protos
from tests.utils import testutils
async def _runner():
async with testutils.start_mockhost(
script_root='unittests/test-binding/functions') as host:
await host.init_worker()
func_id, r = await host.load_function('foo')
print(r.response.function_id == func_id)
print(r.response.result.status == protos.StatusResult.Success)
asyncio.get_event_loop().run_until_complete(_runner())
''')
try:
proc = await asyncio.create_subprocess_exec(
sys.executable, '-c', code,
stdout=asyncio.subprocess.PIPE)
stdout, stderr = await proc.communicate()
# Trimming off carriage return charater when testing on Windows
stdout_lines = [
line.replace(b'\r', b'') for line in stdout.strip().split(b'\n')
]
self.assertEqual(stdout_lines, [b'True', b'True'])
finally:
subprocess.run([
sys.executable, '-m', 'pip',
'--disable-pip-version-check',
'uninstall', '-y', '--quiet', 'foo-binding'
], check=True)
class TestConfigurableFileName(testutils.WebHostTestCase):
def setUp(self) -> None:
def test_function():
return "Test"
self.file_name = PYTHON_SCRIPT_FILE_NAME_DEFAULT
self.test_function = test_function
self.func = Function(self.test_function, script_file="function_app.py")
self.function_registry = functions.Registry()
@classmethod
def get_script_dir(cls):
return testutils.UNIT_TESTS_FOLDER / 'http_functions' / \
'http_functions_stein'
def test_correct_file_name(self):
os.environ.update({PYTHON_SCRIPT_FILE_NAME: self.file_name})
self.assertIsNotNone(os.environ.get(PYTHON_SCRIPT_FILE_NAME))
self.assertEqual(os.environ.get(PYTHON_SCRIPT_FILE_NAME),
'function_app.py')