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 c555931c412..ba31c6dff05 100644 --- a/lib/ansible/plugins/loader.py +++ b/lib/ansible/plugins/loader.py @@ -1150,7 +1150,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 8a39eb66ff9..ba3dbdc58cd 100644 --- a/test/lib/ansible_test/_util/target/sanity/import/importer.py +++ b/test/lib/ansible_test/_util/target/sanity/import/importer.py @@ -114,8 +114,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 @@ -437,7 +442,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 @@ -486,6 +491,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",