ansible-test - Fix import test for collections.

pull/76514/head
Matt Clay 3 years ago
parent 0df60f3b61
commit e56e47faa7

@ -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.

@ -1150,7 +1150,8 @@ def _does_collection_support_ansible_version(requirement_string, ansible_version
def _configure_collection_loader(): def _configure_collection_loader():
if AnsibleCollectionConfig.collection_finder: 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 return
finder = _AnsibleCollectionFinder(C.config.get_config_value('COLLECTIONS_PATHS'), C.config.get_config_value('COLLECTIONS_SCAN_SYS_PATH')) finder = _AnsibleCollectionFinder(C.config.get_config_value('COLLECTIONS_PATHS'), C.config.get_config_value('COLLECTIONS_SCAN_SYS_PATH'))

@ -15,6 +15,8 @@ EXAMPLES = '''#'''
RETURN = '''#''' RETURN = '''#'''
from ansible.plugins.lookup import LookupBase 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: try:
import demo import demo

@ -15,6 +15,8 @@ EXAMPLES = '''#'''
RETURN = '''#''' RETURN = '''#'''
from ansible.plugins.lookup import LookupBase 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: try:
import demo import demo

@ -114,6 +114,11 @@ def main():
# do not support collection loading when not testing a collection # do not support collection loading when not testing a collection
collection_loader = None collection_loader = None
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 # 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])) list(map(sys.modules.pop, [m for m in sys.modules if m.partition('.')[0] == ansible.__name__ and m != vendor_module_name]))
@ -437,7 +442,7 @@ def main():
try: try:
yield yield
finally: finally:
if import_type == 'plugin': if import_type == 'plugin' and not collection_loader:
from ansible.utils.collection_loader._collection_finder import _AnsibleCollectionFinder from ansible.utils.collection_loader._collection_finder import _AnsibleCollectionFinder
_AnsibleCollectionFinder._remove() # pylint: disable=protected-access _AnsibleCollectionFinder._remove() # pylint: disable=protected-access
@ -486,6 +491,11 @@ def main():
with warnings.catch_warnings(): with warnings.catch_warnings():
warnings.simplefilter('error') warnings.simplefilter('error')
if collection_loader and import_type == 'plugin':
warnings.filterwarnings(
"ignore",
"AnsibleCollectionFinder has already been configured")
if sys.version_info[0] == 2: if sys.version_info[0] == 2:
warnings.filterwarnings( warnings.filterwarnings(
"ignore", "ignore",

Loading…
Cancel
Save