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
This commit is contained in:
parent
6369591b11
commit
ee6413af47
4 changed files with 44 additions and 4 deletions
|
@ -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)
|
|
@ -332,9 +332,10 @@ class JinjaPluginIntercept(MutableMapping):
|
|||
if not acr:
|
||||
raise KeyError('invalid plugin name: {0}'.format(key))
|
||||
|
||||
# FIXME: error handling for bogus plugin name, bogus impl, bogus filter/test
|
||||
|
||||
pkg = import_module(acr.n_python_package_name)
|
||||
try:
|
||||
pkg = import_module(acr.n_python_package_name)
|
||||
except ImportError:
|
||||
raise KeyError()
|
||||
|
||||
parent_prefix = acr.collection
|
||||
|
||||
|
@ -345,7 +346,10 @@ class JinjaPluginIntercept(MutableMapping):
|
|||
if ispkg:
|
||||
continue
|
||||
|
||||
plugin_impl = self._pluginloader.get(module_name)
|
||||
try:
|
||||
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)
|
||||
|
||||
|
|
|
@ -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.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
|
||||
# from legacy plugin dirs, and that a same-named plugin loaded from a real collection is not masked by the others
|
||||
- hosts: testhost
|
||||
|
|
Loading…
Reference in a new issue