[stable-2.9] Address fixme and handle filter/test errors for collections better (#68047)

* Address fixme and handle fitler/test errors for collections better. Fixes #66721

* Re-arrange code
(cherry picked from commit ee6413a)

Co-authored-by: Matt Martz <matt@sivel.net>
pull/69271/head
Matt Martz 5 years ago committed by Matt Clay
parent ff3275f9bd
commit 6c2b372d1b

@ -0,0 +1,4 @@
bugfixes:
- collections - Handle errors better for filters and tests in collections,
where a non-existent collection is specified, or importing the plugin
results in an exception (https://github.com/ansible/ansible/issues/66721)

@ -335,9 +335,10 @@ class JinjaPluginIntercept(MutableMapping):
if not acr: if not acr:
raise KeyError('invalid plugin name: {0}'.format(key)) raise KeyError('invalid plugin name: {0}'.format(key))
# FIXME: error handling for bogus plugin name, bogus impl, bogus filter/test try:
pkg = import_module(acr.n_python_package_name) pkg = import_module(acr.n_python_package_name)
except ImportError:
raise KeyError()
parent_prefix = acr.collection parent_prefix = acr.collection
@ -348,7 +349,10 @@ class JinjaPluginIntercept(MutableMapping):
if ispkg: if ispkg:
continue continue
try:
plugin_impl = self._pluginloader.get(module_name) plugin_impl = self._pluginloader.get(module_name)
except Exception as e:
raise TemplateSyntaxError(to_native(e), 0)
method_map = getattr(plugin_impl, self._method_map_name) method_map = getattr(plugin_impl, self._method_map_name)

@ -0,0 +1,13 @@
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
class FilterModule(object):
def filters(self):
return {
'broken': lambda x: 'broken',
}
raise Exception('This is a broken filter plugin')

@ -95,6 +95,25 @@
- lookup('testns.testcoll.mylookup2') == 'mylookup2_from_user_dir' - lookup('testns.testcoll.mylookup2') == 'mylookup2_from_user_dir'
- lookup('testns.testcoll.lookup_subdir.my_subdir_lookup') == 'subdir_lookup_from_user_dir' - lookup('testns.testcoll.lookup_subdir.my_subdir_lookup') == 'subdir_lookup_from_user_dir'
- debug:
msg: "{{ 'foo'|testns.testbroken.broken }}"
register: result
ignore_errors: true
- assert:
that:
- |
'This is a broken filter plugin.' in result.msg
- debug:
msg: "{{ 'foo'|missing.collection.filter }}"
register: result
ignore_errors: true
- assert:
that:
- result is failed
# ensure that the synthetic ansible.builtin collection limits to builtin plugins, that ansible.legacy loads overrides # ensure that the synthetic ansible.builtin collection limits to builtin plugins, that ansible.legacy loads overrides
# from legacy plugin dirs, and that a same-named plugin loaded from a real collection is not masked by the others # from legacy plugin dirs, and that a same-named plugin loaded from a real collection is not masked by the others
- hosts: testhost - hosts: testhost

Loading…
Cancel
Save