Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix import sanity test for collections. #59484

Merged
merged 4 commits into from
Jul 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
16 changes: 16 additions & 0 deletions test/runner/lib/sanity/import.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,22 @@ def test(self, args, targets, python_version):

os.symlink(os.path.join(ANSIBLE_ROOT, 'lib/ansible/module_utils'), ansible_link)

if data_context().content.collection:
# inject just enough Ansible code for the collections loader to work on all supported Python versions
# the __init__.py files are needed only for Python 2.x
# the empty modules directory is required for the collection loader to generate the synthetic packages list

make_dirs(os.path.join(ansible_path, 'utils'))
with open(os.path.join(ansible_path, 'utils/__init__.py'), 'w'):
pass

os.symlink(os.path.join(ANSIBLE_ROOT, 'lib/ansible/utils/collection_loader.py'), os.path.join(ansible_path, 'utils/collection_loader.py'))
os.symlink(os.path.join(ANSIBLE_ROOT, 'lib/ansible/utils/singleton.py'), os.path.join(ansible_path, 'utils/singleton.py'))

make_dirs(os.path.join(ansible_path, 'modules'))
with open(os.path.join(ansible_path, 'modules/__init__.py'), 'w'):
pass

# activate the virtual environment
env['PATH'] = '%s:%s' % (virtual_environment_bin, env['PATH'])
env['PYTHONPATH'] = python_path
Expand Down
9 changes: 9 additions & 0 deletions test/sanity/import/importer.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@
import ansible.module_utils.basic
import ansible.module_utils.common.removed

try:
from ansible.utils.collection_loader import AnsibleCollectionLoader
except ImportError:
AnsibleCollectionLoader = None


class ImporterAnsibleModuleException(Exception):
"""Exception thrown during initialization of ImporterAnsibleModule."""
Expand All @@ -49,6 +54,10 @@ def main():
base_dir = os.getcwd()
messages = set()

if AnsibleCollectionLoader:
# allow importing code from collections
sys.meta_path.insert(0, AnsibleCollectionLoader())

for path in sys.argv[1:] or sys.stdin.read().splitlines():
test_python_module(path, base_dir, messages, False)
test_python_module(path, base_dir, messages, True)
Expand Down