|
1 | 1 | import sys |
2 | 2 | import os |
3 | | -import unittest |
4 | 3 | from unittest.mock import patch |
5 | 4 |
|
6 | 5 | from proxy_worker.utils.dependency import DependencyManager |
7 | | -from tests.utils import testutils |
8 | 6 |
|
9 | 7 |
|
10 | | -class TestDependency(unittest.TestCase): |
11 | | - |
12 | | - @patch("proxy_worker.utils.dependency.DependencyManager._get_cx_deps_path", |
13 | | - return_value="/mock/cx/site-packages") |
14 | | - @patch("proxy_worker.utils.dependency.DependencyManager._get_cx_working_dir", |
15 | | - return_value="/mock/cx") |
16 | | - @patch("proxy_worker.utils.dependency.DependencyManager._get_worker_deps_path", |
17 | | - return_value="/mock/worker") |
18 | | - @patch("proxy_worker.utils.dependency.logger") |
19 | | - def test_use_worker_dependencies(mock_logger, mock_worker, mock_cx_dir, |
20 | | - mock_cx_deps): |
21 | | - sys.path = ["/mock/cx/site-packages", "/mock/cx", "/original"] |
22 | | - |
23 | | - DependencyManager.initialize() |
24 | | - DependencyManager.use_worker_dependencies() |
25 | | - |
26 | | - assert sys.path[0] == "/mock/worker" |
27 | | - assert "/mock/cx/site-packages" not in sys.path |
28 | | - assert "/mock/cx" not in sys.path |
29 | | - |
30 | | - mock_logger.info.assert_any_call( |
31 | | - 'Applying use_worker_dependencies:' |
32 | | - ' worker_dependencies: %s,' |
33 | | - ' customer_dependencies: %s,' |
34 | | - ' working_directory: %s', |
35 | | - "/mock/worker", "/mock/cx/site-packages", "/mock/cx" |
36 | | - ) |
37 | | - |
38 | | - @patch("proxy_worker.utils.dependency.DependencyManager._get_cx_deps_path", |
39 | | - return_value="/mock/cx/site-packages") |
40 | | - @patch("proxy_worker.utils.dependency.DependencyManager._get_worker_deps_path", |
41 | | - return_value="/mock/worker") |
42 | | - @patch("proxy_worker.utils.dependency.DependencyManager._get_cx_working_dir", |
43 | | - return_value="/mock/cx") |
44 | | - @patch("proxy_worker.utils.dependency.DependencyManager.is_in_linux_consumption", |
45 | | - return_value=False) |
46 | | - @patch("proxy_worker.utils.dependency.is_envvar_true", return_value=False) |
47 | | - @patch("proxy_worker.utils.dependency.logger") |
48 | | - def test_prioritize_customer_dependencies(mock_logger, mock_env, mock_linux, |
49 | | - mock_cx_dir, mock_worker, mock_cx_deps): |
50 | | - sys.path = ["/mock/worker", "/some/old/path"] |
51 | | - |
52 | | - DependencyManager.initialize() |
53 | | - DependencyManager.prioritize_customer_dependencies("/override/cx") |
54 | | - |
55 | | - assert sys.path[0] == "/mock/cx/site-packages" |
56 | | - assert sys.path[1] == "/mock/worker" |
57 | | - expected_path = os.path.abspath("/override/cx") |
58 | | - assert expected_path in sys.path |
59 | | - |
60 | | - assert any( |
61 | | - "Finished prioritize_customer_dependencies" in str(call[0][0]) |
62 | | - for call in mock_logger.info.call_args_list |
63 | | - ) |
64 | | - |
65 | | - |
66 | | -class TestProtobufImports(unittest.TestCase): |
67 | | - def setUp(self): |
68 | | - self._patch_environ = patch.dict('os.environ', os.environ.copy()) |
69 | | - self._patch_sys_path = patch('sys.path', []) |
70 | | - self._patch_importer_cache = patch.dict('sys.path_importer_cache', {}) |
71 | | - self._patch_modules = patch.dict('sys.modules', {}) |
72 | | - self._customer_func_path = os.path.abspath( |
73 | | - os.path.join( |
74 | | - testutils.UNIT_TESTS_ROOT, 'resources', 'customer_func_path' |
75 | | - ) |
76 | | - ) |
77 | | - self._worker_deps_path = os.path.abspath( |
78 | | - os.path.join( |
79 | | - testutils.UNIT_TESTS_ROOT, 'resources', 'worker_deps_path' |
80 | | - ) |
81 | | - ) |
82 | | - self._customer_deps_path = os.path.abspath( |
83 | | - os.path.join( |
84 | | - testutils.UNIT_TESTS_ROOT, 'resources', 'customer_deps_path' |
85 | | - ) |
86 | | - ) |
87 | | - |
88 | | - self._patch_environ.start() |
89 | | - self._patch_sys_path.start() |
90 | | - self._patch_importer_cache.start() |
91 | | - self._patch_modules.start() |
92 | | - |
93 | | - def tearDown(self): |
94 | | - self._patch_environ.stop() |
95 | | - self._patch_sys_path.stop() |
96 | | - self._patch_importer_cache.stop() |
97 | | - self._patch_modules.stop() |
98 | | - DependencyManager.cx_deps_path = '' |
99 | | - DependencyManager.cx_working_dir = '' |
100 | | - DependencyManager.worker_deps_path = '' |
101 | | - |
102 | | - @unittest.skipIf(sys.version_info.minor != 13, |
103 | | - "The worker brings different protobuf versions" |
104 | | - "between 3.13 and 3.14.") |
105 | | - def test_newrelic_protobuf_import_scenario_worker_deps_313(self): |
106 | | - # https://github.com/Azure/azure-functions-python-worker/issues/1339 |
107 | | - # newrelic checks if protobuf has been imported and based on the |
108 | | - # version it finds, imports a specific pb2 file. |
109 | | - |
110 | | - # protobuf is brought through the worker's deps. |
111 | | - # Setup paths |
112 | | - DependencyManager.worker_deps_path = self._worker_deps_path |
113 | | - DependencyManager.cx_deps_path = "" # No customer deps |
114 | | - DependencyManager.cx_working_dir = self._customer_func_path |
115 | | - |
116 | | - DependencyManager.prioritize_customer_dependencies() |
117 | | - |
118 | | - # protobuf v5 is found |
119 | | - from google.protobuf import __version__ |
120 | | - |
121 | | - protobuf_version = tuple(int(v) for v in __version__.split(".")) |
122 | | - self.assertIsNotNone(protobuf_version) |
123 | | - self.assertEqual(protobuf_version[0], 5) |
124 | | - |
125 | | - @unittest.skipIf(sys.version_info.minor != 13, |
126 | | - "The worker brings different protobuf versions" |
127 | | - "between 3.13 and 3.14.") |
128 | | - def test_newrelic_protobuf_import_scenario_user_deps_313(self): |
129 | | - # https://github.com/Azure/azure-functions-python-worker/issues/1339 |
130 | | - # newrelic checks if protobuf has been imported and based on the |
131 | | - # version it finds, imports a specific pb2 file. |
132 | | - |
133 | | - # protobuf is brought through the user's deps. |
134 | | - # Setup paths |
135 | | - DependencyManager.worker_deps_path = self._worker_deps_path |
136 | | - DependencyManager.cx_deps_path = self._customer_deps_path |
137 | | - DependencyManager.cx_working_dir = self._customer_func_path |
138 | | - |
139 | | - DependencyManager.prioritize_customer_dependencies() |
140 | | - |
141 | | - # protobuf is found from worker deps, but newrelic won't find it |
142 | | - from google.protobuf import __version__ |
143 | | - |
144 | | - protobuf_version = tuple(int(v) for v in __version__.split(".")) |
145 | | - self.assertIsNotNone(protobuf_version) |
146 | | - |
147 | | - # newrelic tries to import protobuf v3 |
148 | | - self.assertEqual(protobuf_version[0], 3) |
149 | | - |
150 | | - # newrelic tries to import protobuf 5 |
151 | | - self.assertNotEqual(protobuf_version[0], 5) |
| 8 | +@patch("proxy_worker.utils.dependency.DependencyManager._get_cx_deps_path", |
| 9 | + return_value="/mock/cx/site-packages") |
| 10 | +@patch("proxy_worker.utils.dependency.DependencyManager._get_cx_working_dir", |
| 11 | + return_value="/mock/cx") |
| 12 | +@patch("proxy_worker.utils.dependency.DependencyManager._get_worker_deps_path", |
| 13 | + return_value="/mock/worker") |
| 14 | +@patch("proxy_worker.utils.dependency.logger") |
| 15 | +def test_use_worker_dependencies(mock_logger, mock_worker, mock_cx_dir, |
| 16 | + mock_cx_deps): |
| 17 | + sys.path = ["/mock/cx/site-packages", "/mock/cx", "/original"] |
| 18 | + |
| 19 | + DependencyManager.initialize() |
| 20 | + DependencyManager.use_worker_dependencies() |
| 21 | + |
| 22 | + assert sys.path[0] == "/mock/worker" |
| 23 | + assert "/mock/cx/site-packages" not in sys.path |
| 24 | + assert "/mock/cx" not in sys.path |
| 25 | + |
| 26 | + mock_logger.info.assert_any_call( |
| 27 | + 'Applying use_worker_dependencies:' |
| 28 | + ' worker_dependencies: %s,' |
| 29 | + ' customer_dependencies: %s,' |
| 30 | + ' working_directory: %s', |
| 31 | + "/mock/worker", "/mock/cx/site-packages", "/mock/cx" |
| 32 | + ) |
| 33 | + |
| 34 | + |
| 35 | +@patch("proxy_worker.utils.dependency.DependencyManager._get_cx_deps_path", |
| 36 | + return_value="/mock/cx/site-packages") |
| 37 | +@patch("proxy_worker.utils.dependency.DependencyManager._get_worker_deps_path", |
| 38 | + return_value="/mock/worker") |
| 39 | +@patch("proxy_worker.utils.dependency.DependencyManager._get_cx_working_dir", |
| 40 | + return_value="/mock/cx") |
| 41 | +@patch("proxy_worker.utils.dependency.DependencyManager.is_in_linux_consumption", |
| 42 | + return_value=False) |
| 43 | +@patch("proxy_worker.utils.dependency.is_envvar_true", return_value=False) |
| 44 | +@patch("proxy_worker.utils.dependency.logger") |
| 45 | +def test_prioritize_customer_dependencies(mock_logger, mock_env, mock_linux, |
| 46 | + mock_cx_dir, mock_worker, mock_cx_deps): |
| 47 | + sys.path = ["/mock/worker", "/some/old/path"] |
| 48 | + |
| 49 | + DependencyManager.initialize() |
| 50 | + DependencyManager.prioritize_customer_dependencies("/override/cx") |
| 51 | + |
| 52 | + assert sys.path[0] == "/mock/cx/site-packages" |
| 53 | + assert sys.path[1] == "/mock/worker" |
| 54 | + expected_path = os.path.abspath("/override/cx") |
| 55 | + assert expected_path in sys.path |
| 56 | + |
| 57 | + assert any( |
| 58 | + "Finished prioritize_customer_dependencies" in str(call[0][0]) |
| 59 | + for call in mock_logger.info.call_args_list |
| 60 | + ) |
0 commit comments