Debian Codegraph @

test/units/module_utils/_internal/test_deprecator.py

raw
1 from __future__ import annotations
2
3 import importlib.abc
4 import importlib.util
5
6 import ansible
7 import pathlib
8 import pytest
9
10 from ansible.module_utils._internal import _messages
11 from ansible.module_utils._internal import _deprecator
12
13
14 class FakePathLoader(importlib.abc.SourceLoader):
15 """A test loader that can fake out the code/frame paths to simulate callers of various types without relying on actual files on disk."""
16 def get_filename(self, fullname):
17 if fullname.startswith('ansible.'):
18 basepath = pathlib.Path(ansible.__file__).parent.parent
19 else:
20 basepath = '/x/y'
21
22 return f'{basepath}/{fullname.replace(".", "/")}'
23
24 def get_data(self, path):
25 return b'''
26 from ansible.module_utils._internal import _deprecator
27
28 def do_stuff():
29 return _deprecator.get_caller_plugin_info()
30 '''
31
32 def exec_module(self, module):
33 return super().exec_module(module)
34
35
36 @pytest.mark.parametrize("python_fq_name,expected_plugin_info", (
37 # legacy module callers
38 ('ansible.legacy.blah', _messages.PluginInfo(resolved_name='ansible.legacy.blah', type=_messages.PluginType.MODULE)),
39 # core callers
40 ('ansible.modules.ping', _messages.PluginInfo(resolved_name='ansible.builtin.ping', type=_messages.PluginType.MODULE)),
41 ('ansible.plugins.filter.core', _deprecator.ANSIBLE_CORE_DEPRECATOR),
42 ('ansible.plugins.test.core', _deprecator.ANSIBLE_CORE_DEPRECATOR),
43 ('ansible.nonplugin_something', _deprecator.ANSIBLE_CORE_DEPRECATOR),
44 # collections plugin callers
45 ('ansible_collections.foo.bar.plugins.modules.module_thing', _messages.PluginInfo(resolved_name='foo.bar.module_thing', type=_messages.PluginType.MODULE)),
46 ('ansible_collections.foo.bar.plugins.filter.somefilter', _messages.PluginInfo(resolved_name='foo.bar', type=None)),
47 ('ansible_collections.foo.bar.plugins.test.sometest', _messages.PluginInfo(resolved_name='foo.bar', type=None)),
48 # indeterminate callers (e.g. collection module_utils- must specify since they might be calling on behalf of another
49 ('ansible_collections.foo.bar.plugins.module_utils.something', _deprecator.INDETERMINATE_DEPRECATOR),
50 # other callers
51 ('something.else', None),
52 ('ansible_collections.foo.bar.nonplugin_something', None),
53 ))
54 def test_get_caller_plugin_info(python_fq_name: str, expected_plugin_info: _messages.PluginInfo):
55 """Validates the expected `PluginInfo` values received from various types of core/non-core/collection callers."""
56 # invoke a standalone fake loader that generates a Python module with the specified FQ python name (converted to a corresponding __file__ entry) that
57 # pretends as if it called `get_caller_plugin_info()` and returns its result
58 loader = FakePathLoader()
59 spec = importlib.util.spec_from_loader(name=python_fq_name, loader=loader)
60 mod = importlib.util.module_from_spec(spec)
61
62 loader.exec_module(mod)
63
64 pi: _messages.PluginInfo = mod.do_stuff()
65
66 assert pi == expected_plugin_info