Debian Codegraph @

tests/test_plugins.py

raw
1 # Copyright (c) Meta Platforms, Inc. and affiliates.
2 # SPDX-License-Identifier: LGPL-2.1-or-later
3
4 import logging
5 import os
6 from pathlib import Path
7 import sys
8 import tempfile
9 import unittest.mock
10
11 import _drgn_util.plugins
12 from _drgn_util.plugins import call_plugins
13 from tests import TestCase, modifyenv
14
15
16 class TestPlugins(TestCase):
17 def setUp(self):
18 # Clear the plugin and hook caches before each test.
19 _drgn_util.plugins._plugins = None
20 _drgn_util.plugins._hooks.clear()
21
22 # pkg_resources caches distributions on import. Delete it before each
23 # test so that it is reloaded.
24 sys.modules.pop("pkg_resources", None)
25
26 # These tests change these environment variables and sys.path, so
27 # restore them after each test.
28 self.enterContext(
29 modifyenv({"DRGN_PLUGINS": None, "DRGN_DISABLE_PLUGINS": None})
30 )
31 self.addCleanup(setattr, sys, "path", list(sys.path))
32
33 # Delete modules imported by each test so that we can reuse the same
34 # module names.
35 def restore_modules(old_modules):
36 for new_module in set(sys.modules) - old_modules:
37 sys.modules.pop(new_module, None)
38
39 self.addCleanup(restore_modules, set(sys.modules))
40
41 @staticmethod
42 def _create_plugin(dir):
43 plugin_path = Path(dir) / "test_plugin.py"
44 plugin_path.write_text(
45 """\
46 def drgn_test_hook(call_me):
47 call_me()
48 """
49 )
50 return plugin_path
51
52 @staticmethod
53 def _create_dist_info(dir, module_name="test_plugin", entry_point_name="test"):
54 dist_info_dir = Path(dir) / f"{module_name}-1.0.dist-info"
55 dist_info_dir.mkdir()
56 (dist_info_dir / "METADATA").write_text(
57 f"""\
58 Metadata-Version: 1.1
59 Name: {module_name}
60 Version: 1.0
61 """
62 )
63 (dist_info_dir / "entry_points.txt").write_text(
64 f"""\
65 [drgn.plugins]
66 {entry_point_name} = {module_name}
67 """
68 )
69
70 def test_entry_point(self):
71 with tempfile.TemporaryDirectory() as temp_dir:
72 self._create_plugin(temp_dir)
73 self._create_dist_info(temp_dir)
74 sys.path.insert(0, temp_dir)
75
76 call_me = unittest.mock.Mock()
77 call_plugins("drgn_test_hook", call_me)
78 call_me.assert_called_once()
79
80 def test_drgn_disable_plugins_envvar_all(self):
81 with tempfile.TemporaryDirectory() as temp_dir:
82 self._create_plugin(temp_dir)
83 self._create_dist_info(temp_dir)
84 sys.path.insert(0, temp_dir)
85 os.environ["DRGN_DISABLE_PLUGINS"] = "*"
86
87 call_me = unittest.mock.Mock()
88 call_plugins("drgn_test_hook", call_me)
89 call_me.assert_not_called()
90
91 def test_drgn_disable_plugins_envvar_specific(self):
92 with tempfile.TemporaryDirectory() as temp_dir:
93 self._create_plugin(temp_dir)
94 self._create_dist_info(temp_dir)
95 sys.path.insert(0, temp_dir)
96 os.environ["DRGN_DISABLE_PLUGINS"] = "foo,test"
97
98 call_me = unittest.mock.Mock()
99 call_plugins("drgn_test_hook", call_me)
100 call_me.assert_not_called()
101
102 def test_drgn_plugins_envvar_path(self):
103 with tempfile.TemporaryDirectory() as temp_dir:
104 plugin_path = self._create_plugin(temp_dir)
105 os.environ["DRGN_PLUGINS"] = f"test={plugin_path}"
106
107 call_me = unittest.mock.Mock()
108 call_plugins("drgn_test_hook", call_me)
109 call_me.assert_called_once()
110
111 def test_drgn_plugins_envvar_module(self):
112 with tempfile.TemporaryDirectory() as temp_dir:
113 plugin_path = self._create_plugin(temp_dir)
114 sys.path.insert(0, temp_dir)
115 os.environ["DRGN_PLUGINS"] = f"test={plugin_path.stem}"
116
117 call_me = unittest.mock.Mock()
118 call_plugins("drgn_test_hook", call_me)
119 call_me.assert_called_once()
120
121 def test_drgn_plugins_envvar_precedence(self):
122 with tempfile.TemporaryDirectory() as temp_dir:
123 self._create_plugin(temp_dir)
124 self._create_dist_info(temp_dir)
125 sys.path.insert(0, temp_dir)
126 os.environ["DRGN_DISABLE_PLUGINS"] = "*"
127 os.environ["DRGN_PLUGINS"] = "test"
128
129 call_me = unittest.mock.Mock()
130 call_plugins("drgn_test_hook", call_me)
131 call_me.assert_called_once()
132
133 def test_priority(self):
134 with tempfile.TemporaryDirectory() as temp_dir:
135 (Path(temp_dir) / "test_plugin1.py").write_text(
136 """\
137 def drgn_test_hook(call_me):
138 call_me(1)
139 drgn_test_hook.drgn_priority = 75
140 """
141 )
142 (Path(temp_dir) / "test_plugin2.py").write_text(
143 """\
144 def drgn_test_hook(call_me):
145 call_me(2)
146 drgn_test_hook.drgn_priority = 25
147 """
148 )
149 (Path(temp_dir) / "test_plugin3.py").write_text(
150 """\
151 def drgn_test_hook(call_me):
152 call_me(3)
153 """
154 )
155 self._create_dist_info(temp_dir, "test_plugin1", "test1")
156 self._create_dist_info(temp_dir, "test_plugin2", "test2")
157 self._create_dist_info(temp_dir, "test_plugin3", "test3")
158 sys.path.insert(0, temp_dir)
159
160 call_me = unittest.mock.Mock()
161 call_plugins("drgn_test_hook", call_me)
162 self.assertEqual(
163 call_me.call_args_list,
164 [unittest.mock.call(2), unittest.mock.call(3), unittest.mock.call(1)],
165 )
166
167 def test_plugin_exception(self):
168 with tempfile.TemporaryDirectory() as temp_dir:
169 (Path(temp_dir) / "test_plugin.py").write_text('raise Exception("foo")\n')
170 self._create_dist_info(temp_dir)
171 sys.path.insert(0, temp_dir)
172
173 with self.assertLogs(logging.getLogger("drgn.plugins"), "WARNING") as cm:
174 call_plugins("drgn_test_hook")
175 self.assertTrue(
176 any(
177 message.startswith("WARNING:drgn.plugins:failed to load 'test")
178 for message in cm.output
179 ),
180 msg=f"no match in {cm.output}",
181 )
182
183 def test_hook_exception(self):
184 with tempfile.TemporaryDirectory() as temp_dir:
185 (Path(temp_dir) / "test_plugin.py").write_text(
186 """\
187 def drgn_test_hook():
188 raise Exception("foo")
189 """
190 )
191 self._create_dist_info(temp_dir)
192 sys.path.insert(0, temp_dir)
193
194 with self.assertLogs(logging.getLogger("drgn.plugins"), "WARNING") as cm:
195 call_plugins("drgn_test_hook")
196 self.assertTrue(
197 any(
198 message.startswith(
199 "WARNING:drgn.plugins:'test' drgn_test_hook failed:"
200 )
201 for message in cm.output
202 ),
203 msg=f"no match in {cm.output}",
204 )
205
206 def test_missing_entry_point(self):
207 os.environ["DRGN_PLUGINS"] = "__non__existent__entrypoint__"
208 with self.assertLogs(logging.getLogger("drgn.plugins"), "WARNING") as cm:
209 call_plugins("drgn_test_hook")
210 self.assertTrue(
211 any(
212 message.startswith(
213 "WARNING:drgn.plugins:not found: '__non__existent__entrypoint__'"
214 )
215 for message in cm.output
216 ),
217 msg=f"no match in {cm.output}",
218 )