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:
Matt Martz 2020-03-23 14:47:58 -05:00 committed by GitHub
parent 6369591b11
commit ee6413af47
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 44 additions and 4 deletions

View file

@ -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)

View file

@ -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)

View file

@ -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')

View file

@ -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