From a9814fb3fe33d4ccfdec58e79ba0047a0a04b792 Mon Sep 17 00:00:00 2001 From: Matt Clay Date: Wed, 8 Dec 2021 15:14:13 -0800 Subject: [PATCH] [stable-2.12] ansible-test - Fix import test for collections. (cherry picked from commit e56e47faa7f22f1de4079ed5494daf49ba7efbf6) Co-authored-by: Matt Clay --- .../ansible-test-import-collections.yml | 5 +++++ lib/ansible/plugins/loader.py | 3 ++- .../ns/col/plugins/lookup/vendor1.py | 2 ++ .../ns/col/plugins/lookup/vendor2.py | 2 ++ .../_util/target/sanity/import/importer.py | 16 +++++++++++++--- 5 files changed, 24 insertions(+), 4 deletions(-) create mode 100644 changelogs/fragments/ansible-test-import-collections.yml diff --git a/changelogs/fragments/ansible-test-import-collections.yml b/changelogs/fragments/ansible-test-import-collections.yml new file mode 100644 index 00000000000..99c4bd8b15d --- /dev/null +++ b/changelogs/fragments/ansible-test-import-collections.yml @@ -0,0 +1,5 @@ +bugfixes: + - ansible-test - The ``import`` sanity test no longer reports errors about ``packaging`` being missing when testing collections. +minor_changes: + - The collection loader now reports a Python warning if an attempt is made to install the Ansible collection loader a second time. + Previously this condition was reported using an Ansible warning. diff --git a/lib/ansible/plugins/loader.py b/lib/ansible/plugins/loader.py index a80aae7c2b3..cd655814813 100644 --- a/lib/ansible/plugins/loader.py +++ b/lib/ansible/plugins/loader.py @@ -1147,7 +1147,8 @@ def _does_collection_support_ansible_version(requirement_string, ansible_version def _configure_collection_loader(): if AnsibleCollectionConfig.collection_finder: - display.warning('AnsibleCollectionFinder has already been configured') + # this must be a Python warning so that it can be filtered out by the import sanity test + warnings.warn('AnsibleCollectionFinder has already been configured') return finder = _AnsibleCollectionFinder(C.config.get_config_value('COLLECTIONS_PATHS'), C.config.get_config_value('COLLECTIONS_SCAN_SYS_PATH')) diff --git a/test/integration/targets/ansible-test/ansible_collections/ns/col/plugins/lookup/vendor1.py b/test/integration/targets/ansible-test/ansible_collections/ns/col/plugins/lookup/vendor1.py index 7b688f9f191..f59b9091aa4 100644 --- a/test/integration/targets/ansible-test/ansible_collections/ns/col/plugins/lookup/vendor1.py +++ b/test/integration/targets/ansible-test/ansible_collections/ns/col/plugins/lookup/vendor1.py @@ -15,6 +15,8 @@ EXAMPLES = '''#''' RETURN = '''#''' from ansible.plugins.lookup import LookupBase +# noinspection PyUnresolvedReferences +from ansible.plugins import loader # import the loader to verify it works when the collection loader has already been loaded try: import demo diff --git a/test/integration/targets/ansible-test/ansible_collections/ns/col/plugins/lookup/vendor2.py b/test/integration/targets/ansible-test/ansible_collections/ns/col/plugins/lookup/vendor2.py index cfe2fe95aea..22b4236a863 100644 --- a/test/integration/targets/ansible-test/ansible_collections/ns/col/plugins/lookup/vendor2.py +++ b/test/integration/targets/ansible-test/ansible_collections/ns/col/plugins/lookup/vendor2.py @@ -15,6 +15,8 @@ EXAMPLES = '''#''' RETURN = '''#''' from ansible.plugins.lookup import LookupBase +# noinspection PyUnresolvedReferences +from ansible.plugins import loader # import the loader to verify it works when the collection loader has already been loaded try: import demo diff --git a/test/lib/ansible_test/_util/target/sanity/import/importer.py b/test/lib/ansible_test/_util/target/sanity/import/importer.py index 5c84ae725ca..60255da6e4c 100644 --- a/test/lib/ansible_test/_util/target/sanity/import/importer.py +++ b/test/lib/ansible_test/_util/target/sanity/import/importer.py @@ -121,8 +121,13 @@ def main(): # do not support collection loading when not testing a collection collection_loader = None - # remove all modules under the ansible package, except the preloaded vendor module - list(map(sys.modules.pop, [m for m in sys.modules if m.partition('.')[0] == ansible.__name__ and m != vendor_module_name])) + if collection_loader and import_type == 'plugin': + # do not unload ansible code for collection plugin (not module) tests + # doing so could result in the collection loader being initialized multiple times + pass + else: + # remove all modules under the ansible package, except the preloaded vendor module + list(map(sys.modules.pop, [m for m in sys.modules if m.partition('.')[0] == ansible.__name__ and m != vendor_module_name])) if import_type == 'module': # pre-load an empty ansible package to prevent unwanted code in __init__.py from loading @@ -444,7 +449,7 @@ def main(): try: yield finally: - if import_type == 'plugin': + if import_type == 'plugin' and not collection_loader: from ansible.utils.collection_loader._collection_finder import _AnsibleCollectionFinder _AnsibleCollectionFinder._remove() # pylint: disable=protected-access @@ -493,6 +498,11 @@ def main(): with warnings.catch_warnings(): warnings.simplefilter('error') + if collection_loader and import_type == 'plugin': + warnings.filterwarnings( + "ignore", + "AnsibleCollectionFinder has already been configured") + if sys.version_info[0] == 2: warnings.filterwarnings( "ignore",