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

Address fixme and handle filter/test errors for collections better #68047

Merged
merged 2 commits into from Mar 23, 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
@@ -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
Expand Up @@ -337,9 +337,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 @@ -350,7 +351,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
@@ -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
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