Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 1 commit into from
May 6, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -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)
12 changes: 8 additions & 4 deletions lib/ansible/template/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -335,9 +335,10 @@ def __getitem__(self, key):
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

Expand All @@ -348,7 +349,10 @@ def __getitem__(self, key):
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)

Expand Down
Original file line number Diff line number Diff line change
@@ -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')
19 changes: 19 additions & 0 deletions test/integration/targets/collections/posix.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down