From 462e4e7f71cd257cc9f86b2924535716489b669a Mon Sep 17 00:00:00 2001 From: Kate Case Date: Thu, 13 Oct 2022 11:01:42 -0400 Subject: [PATCH] Clean up test failures (#216) * Fix arguments-renamed * Handle changed behavior in 2.15 --- changelogs/fragments/tests.yaml | 3 +++ plugins/filter/index_of.py | 2 +- plugins/lookup/index_of.py | 2 +- .../common => plugin_utils}/index_of.py | 27 ++++++++++++------- tests/sanity/ignore-2.10.txt | 1 - tests/sanity/ignore-2.11.txt | 1 - tests/sanity/ignore-2.12.txt | 2 -- tests/sanity/ignore-2.13.txt | 2 -- tests/sanity/ignore-2.14.txt | 2 -- tests/sanity/ignore-2.9.txt | 1 - tests/unit/mock/loader.py | 10 +++---- .../plugin_utils}/test_index_of.py | 2 +- 12 files changed, 28 insertions(+), 27 deletions(-) create mode 100644 changelogs/fragments/tests.yaml rename plugins/{module_utils/common => plugin_utils}/index_of.py (90%) delete mode 100644 tests/sanity/ignore-2.10.txt delete mode 100644 tests/sanity/ignore-2.11.txt delete mode 100644 tests/sanity/ignore-2.12.txt delete mode 100644 tests/sanity/ignore-2.13.txt delete mode 100644 tests/sanity/ignore-2.14.txt delete mode 100644 tests/sanity/ignore-2.9.txt rename tests/unit/{module_utils => plugins/plugin_utils}/test_index_of.py (97%) diff --git a/changelogs/fragments/tests.yaml b/changelogs/fragments/tests.yaml new file mode 100644 index 0000000..e78eb67 --- /dev/null +++ b/changelogs/fragments/tests.yaml @@ -0,0 +1,3 @@ +--- +trivial: + - Fix various 2.15 sanity and unit tests. (https://github.com/ansible-collections/ansible.utils/pull/216) diff --git a/plugins/filter/index_of.py b/plugins/filter/index_of.py index 4c9b38a..e6ac207 100644 --- a/plugins/filter/index_of.py +++ b/plugins/filter/index_of.py @@ -303,7 +303,7 @@ from ansible.errors import AnsibleFilterError from ansible_collections.ansible.utils.plugins.module_utils.common.argspec_validate import ( AnsibleArgSpecValidator, ) -from ansible_collections.ansible.utils.plugins.module_utils.common.index_of import index_of +from ansible_collections.ansible.utils.plugins.plugin_utils.index_of import index_of try: diff --git a/plugins/lookup/index_of.py b/plugins/lookup/index_of.py index afcd7fc..ed28b7c 100644 --- a/plugins/lookup/index_of.py +++ b/plugins/lookup/index_of.py @@ -324,7 +324,7 @@ from ansible.plugins.lookup import LookupBase from ansible_collections.ansible.utils.plugins.module_utils.common.argspec_validate import ( AnsibleArgSpecValidator, ) -from ansible_collections.ansible.utils.plugins.module_utils.common.index_of import index_of +from ansible_collections.ansible.utils.plugins.plugin_utils.index_of import index_of class LookupModule(LookupBase): diff --git a/plugins/module_utils/common/index_of.py b/plugins/plugin_utils/index_of.py similarity index 90% rename from plugins/module_utils/common/index_of.py rename to plugins/plugin_utils/index_of.py index e47142e..96a90df 100644 --- a/plugins/module_utils/common/index_of.py +++ b/plugins/plugin_utils/index_of.py @@ -16,6 +16,7 @@ import json from ansible.module_utils._text import to_native from ansible.module_utils.six import integer_types, string_types +from jinja2.exceptions import TemplateSyntaxError # Note, this file can only be used on the control node @@ -99,19 +100,25 @@ def _run_test(entry, test, right, tests): if not isinstance(right, list) and test == "in": right = [right] - j2_test = tests.get(test) + # JinjaPluginIntercept.get() raises an exception instead of returning None + # in ansible-core 2.15+ + try: + j2_test = tests.get(test) + except TemplateSyntaxError: + j2_test = None + if not j2_test: msg = "{msg} Error was: the test '{test}' was not found.".format(msg=msg, test=test) _raise_error(msg) - else: - try: - if right is None: - result = j2_test(entry) - else: - result = j2_test(entry, right) - except Exception as exc: - msg = "{msg} Error was: {error}".format(msg=msg, error=to_native(exc)) - _raise_error(msg) + + try: + if right is None: + result = j2_test(entry) + else: + result = j2_test(entry, right) + except Exception as exc: + msg = "{msg} Error was: {error}".format(msg=msg, error=to_native(exc)) + _raise_error(msg) if invert: result = not result diff --git a/tests/sanity/ignore-2.10.txt b/tests/sanity/ignore-2.10.txt deleted file mode 100644 index f6a0f43..0000000 --- a/tests/sanity/ignore-2.10.txt +++ /dev/null @@ -1 +0,0 @@ -plugins/module_utils/common/index_of.py pylint:ansible-bad-module-import # file's use is limited to filter and lookups on control node diff --git a/tests/sanity/ignore-2.11.txt b/tests/sanity/ignore-2.11.txt deleted file mode 100644 index f6a0f43..0000000 --- a/tests/sanity/ignore-2.11.txt +++ /dev/null @@ -1 +0,0 @@ -plugins/module_utils/common/index_of.py pylint:ansible-bad-module-import # file's use is limited to filter and lookups on control node diff --git a/tests/sanity/ignore-2.12.txt b/tests/sanity/ignore-2.12.txt deleted file mode 100644 index fbedf6f..0000000 --- a/tests/sanity/ignore-2.12.txt +++ /dev/null @@ -1,2 +0,0 @@ -plugins/module_utils/common/index_of.py pylint:ansible-bad-module-import # file's use is limited to filter and lookups on control node -tests/unit/mock/loader.py pylint:arguments-renamed diff --git a/tests/sanity/ignore-2.13.txt b/tests/sanity/ignore-2.13.txt deleted file mode 100644 index fbedf6f..0000000 --- a/tests/sanity/ignore-2.13.txt +++ /dev/null @@ -1,2 +0,0 @@ -plugins/module_utils/common/index_of.py pylint:ansible-bad-module-import # file's use is limited to filter and lookups on control node -tests/unit/mock/loader.py pylint:arguments-renamed diff --git a/tests/sanity/ignore-2.14.txt b/tests/sanity/ignore-2.14.txt deleted file mode 100644 index fbedf6f..0000000 --- a/tests/sanity/ignore-2.14.txt +++ /dev/null @@ -1,2 +0,0 @@ -plugins/module_utils/common/index_of.py pylint:ansible-bad-module-import # file's use is limited to filter and lookups on control node -tests/unit/mock/loader.py pylint:arguments-renamed diff --git a/tests/sanity/ignore-2.9.txt b/tests/sanity/ignore-2.9.txt deleted file mode 100644 index f6a0f43..0000000 --- a/tests/sanity/ignore-2.9.txt +++ /dev/null @@ -1 +0,0 @@ -plugins/module_utils/common/index_of.py pylint:ansible-bad-module-import # file's use is limited to filter and lookups on control node diff --git a/tests/unit/mock/loader.py b/tests/unit/mock/loader.py index 2b5eb36..ebff16f 100644 --- a/tests/unit/mock/loader.py +++ b/tests/unit/mock/loader.py @@ -47,12 +47,12 @@ class DictDataLoader(DataLoader): # TODO: the real _get_file_contents returns a bytestring, so we actually convert the # unicode/text it's created with to utf-8 - def _get_file_contents(self, path): - path = to_text(path) - if path in self._file_mapping: - return (to_bytes(self._file_mapping[path]), False) + def _get_file_contents(self, file_name): + file_name = to_text(file_name) + if file_name in self._file_mapping: + return (to_bytes(self._file_mapping[file_name]), False) else: - raise AnsibleParserError("file not found: %s" % path) + raise AnsibleParserError("file not found: %s" % file_name) def path_exists(self, path): path = to_text(path) diff --git a/tests/unit/module_utils/test_index_of.py b/tests/unit/plugins/plugin_utils/test_index_of.py similarity index 97% rename from tests/unit/module_utils/test_index_of.py rename to tests/unit/plugins/plugin_utils/test_index_of.py index bdece24..bc426e4 100644 --- a/tests/unit/module_utils/test_index_of.py +++ b/tests/unit/plugins/plugin_utils/test_index_of.py @@ -14,7 +14,7 @@ import unittest from ansible.template import Templar -from ansible_collections.ansible.utils.plugins.module_utils.common.index_of import index_of +from ansible_collections.ansible.utils.plugins.plugin_utils.index_of import index_of class TestIndexOfFilter(unittest.TestCase):