diff options
author | Matt Clay <matt@mystile.com> | 2021-12-08 15:14:13 -0800 |
---|---|---|
committer | Matt Clay <matt@mystile.com> | 2021-12-08 15:53:38 -0800 |
commit | e56e47faa7f22f1de4079ed5494daf49ba7efbf6 (patch) | |
tree | e7cbed93e950ad81ba1481e7492a6ffe3f41fae6 | |
parent | 0df60f3b617e1cf6144a76512573c9e54ef4ce7c (diff) | |
download | ansible-e56e47faa7f22f1de4079ed5494daf49ba7efbf6.tar.gz |
ansible-test - Fix import test for collections.
5 files changed, 24 insertions, 4 deletions
diff --git a/changelogs/fragments/ansible-test-import-collections.yml b/changelogs/fragments/ansible-test-import-collections.yml new file mode 100644 index 0000000000..99c4bd8b15 --- /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 c555931c41..ba31c6dff0 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 7b688f9f19..f59b9091aa 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 cfe2fe95ae..22b4236a86 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 8a39eb66ff..ba3dbdc58c 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", |