Fix plugin names when loading all plugins. (#59950)

* Fix plugin names when loading all plugins.

Add an integration test to verify plugin __package__ and __name__ are correct.

* Make sure filter and test names are unique.

* Remove __package__ test.

On Python 2.x __package__ is not set, but it is on Python 3.x.
pull/60305/head
Matt Clay 5 years ago committed by Matt Davis
parent d3624cf4a4
commit 3777c2e93d

@ -678,7 +678,13 @@ class PluginLoader:
if path not in self._module_cache:
try:
module = self._load_module_source(name, path)
if self.subdir in ('filter_plugins', 'test_plugins'):
# filter and test plugin files can contain multiple plugins
# they must have a unique python module name to prevent them from shadowing each other
full_name = '{0}_{1}'.format(abs(hash(path)), basename)
else:
full_name = basename
module = self._load_module_source(full_name, path)
self._load_config_defs(basename, module, path)
except Exception as e:
display.warning("Skipping plugin (%s) as it seems to be invalid: %s" % (path, to_text(e)))

@ -0,0 +1,15 @@
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
def filter_name(a):
return __name__
class FilterModule(object):
def filters(self):
filters = {
'filter_name': filter_name,
}
return filters

@ -0,0 +1,9 @@
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
from ansible.plugins.lookup import LookupBase
class LookupModule(LookupBase):
def run(self, terms, variables, **kwargs):
return [__name__]

@ -0,0 +1,11 @@
- set_fact:
filter_name: "{{ 1 | filter_name }}"
lookup_name: "{{ lookup('lookup_name') }}"
test_name_ok: "{{ 1 is test_name_ok }}"
- assert:
that:
# filter names are prefixed with a unique hash value to prevent shadowing of other plugins
- filter_name | regex_search('^ansible\.plugins\.filter\.[0-9]+_test_filter$')
- lookup_name == 'ansible.plugins.lookup.lookup_name'
- test_name_ok

@ -0,0 +1,16 @@
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
import re
def test_name_ok(value):
# test names are prefixed with a unique hash value to prevent shadowing of other plugins
return bool(re.match(r'^ansible\.plugins\.test\.[0-9]+_test_test$', __name__))
class TestModule:
def tests(self):
return {
'test_name_ok': test_name_ok,
}
Loading…
Cancel
Save