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

Overhaul ansible-test code coverage and injector. #53510

Merged
merged 6 commits into from
Mar 13, 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
3 changes: 2 additions & 1 deletion test/integration/inventory
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
# For script based test targets (using runme.sh) put the inventory file in the test's directory instead.

[testgroup]
testhost ansible_connection=local
# ansible_python_interpreter must be set to avoid interpreter discovery
testhost ansible_connection=local ansible_python_interpreter="{{ ansible_playbook_python }}"
2 changes: 1 addition & 1 deletion test/runner/injector/ansible
2 changes: 1 addition & 1 deletion test/runner/injector/ansible-config
2 changes: 1 addition & 1 deletion test/runner/injector/ansible-connection
2 changes: 1 addition & 1 deletion test/runner/injector/ansible-console
2 changes: 1 addition & 1 deletion test/runner/injector/ansible-doc
2 changes: 1 addition & 1 deletion test/runner/injector/ansible-galaxy
2 changes: 1 addition & 1 deletion test/runner/injector/ansible-inventory
2 changes: 1 addition & 1 deletion test/runner/injector/ansible-playbook
2 changes: 1 addition & 1 deletion test/runner/injector/ansible-pull
2 changes: 1 addition & 1 deletion test/runner/injector/ansible-vault
2 changes: 1 addition & 1 deletion test/runner/injector/importer.py
245 changes: 0 additions & 245 deletions test/runner/injector/injector.py

This file was deleted.

2 changes: 1 addition & 1 deletion test/runner/injector/pytest
1 change: 0 additions & 1 deletion test/runner/injector/python.py

This file was deleted.

63 changes: 63 additions & 0 deletions test/runner/injector/python.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/usr/bin/env python
"""Provides an entry point for python scripts and python modules on the controller with the current python interpreter and optional code coverage collection."""

import imp
import os
import sys


def main():
"""Main entry point."""
name = os.path.basename(__file__)
args = [sys.executable]

coverage_config = os.environ.get('_ANSIBLE_COVERAGE_CONFIG')
coverage_output = os.environ.get('_ANSIBLE_COVERAGE_OUTPUT')

if coverage_config:
if coverage_output:
args += ['-m', 'coverage.__main__', 'run', '--rcfile', coverage_config]
else:
try:
imp.find_module('coverage')
except ImportError:
exit('ERROR: Could not find `coverage` module. Did you use a virtualenv created without --system-site-packages or with the wrong interpreter?')

if name == 'python.py':
if sys.argv[1] == '-c':
# prevent simple misuse of python.py with -c which does not work with coverage
sys.exit('ERROR: Use `python -c` instead of `python.py -c` to avoid errors when code coverage is collected.')
elif name == 'pytest':
args += ['-m', 'pytest']
else:
args += [find_executable(name)]

args += sys.argv[1:]

os.execv(args[0], args)


def find_executable(name):
"""
:type name: str
:rtype: str
"""
path = os.environ.get('PATH', os.path.defpath)
seen = set([os.path.abspath(__file__)])

for base in path.split(os.path.pathsep):
candidate = os.path.abspath(os.path.join(base, name))

if candidate in seen:
continue

seen.add(candidate)

if os.path.exists(candidate) and os.access(candidate, os.F_OK | os.X_OK):
return candidate

raise Exception('Executable "%s" not found in path: %s' % (name, path))


if __name__ == '__main__':
main()