mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-07-27 23:21:22 -07:00
Add sanity import test to ansible-test. (#26730)
* Add sanity import test to ansible-test. * Run sanity import test on all python versions.
This commit is contained in:
parent
74cc99fa35
commit
0b784c65b1
9 changed files with 257 additions and 4 deletions
0
test/runner/import/lib/ansible/__init__.py
Normal file
0
test/runner/import/lib/ansible/__init__.py
Normal file
1
test/runner/import/lib/ansible/module_utils
Symbolic link
1
test/runner/import/lib/ansible/module_utils
Symbolic link
|
@ -0,0 +1 @@
|
|||
../../../../../lib/ansible/module_utils
|
49
test/runner/importer.py
Executable file
49
test/runner/importer.py
Executable file
|
@ -0,0 +1,49 @@
|
|||
#!/usr/bin/env python
|
||||
"""Import the given python module(s) and report error(s) encountered."""
|
||||
|
||||
from __future__ import absolute_import, print_function
|
||||
|
||||
import imp
|
||||
import os
|
||||
import sys
|
||||
import traceback
|
||||
|
||||
|
||||
def main():
|
||||
"""Main program function."""
|
||||
base_dir = os.getcwd()
|
||||
messages = set()
|
||||
|
||||
for path in sys.argv[1:]:
|
||||
try:
|
||||
with open(path, 'r') as module_fd:
|
||||
imp.load_module('module_import_test', module_fd, os.path.abspath(path), ('.py', 'r', imp.PY_SOURCE))
|
||||
except Exception as ex: # pylint: disable=locally-disabled, broad-except
|
||||
exc_type, _, exc_tb = sys.exc_info()
|
||||
message = str(ex)
|
||||
results = list(reversed(traceback.extract_tb(exc_tb)))
|
||||
source = None
|
||||
line = None
|
||||
|
||||
for result in results:
|
||||
if result[0].startswith(base_dir):
|
||||
source = result[0][len(base_dir) + 1:].replace('test/runner/import/', '')
|
||||
line = result[1]
|
||||
break
|
||||
|
||||
if not source:
|
||||
source = path
|
||||
line = 0
|
||||
message += ' (in %s:%d)' % (results[-1][0], results[-1][1])
|
||||
|
||||
error = '%s:%d:0: %s: %s' % (source, line, exc_type.__name__, message)
|
||||
|
||||
if error not in messages:
|
||||
messages.add(error)
|
||||
print(error)
|
||||
|
||||
if messages:
|
||||
exit(10)
|
||||
|
||||
if __name__ == '__main__':
|
||||
main()
|
1
test/runner/injector/importer.py
Symbolic link
1
test/runner/injector/importer.py
Symbolic link
|
@ -0,0 +1 @@
|
|||
injector.py
|
|
@ -864,7 +864,7 @@ def compile_version(args, python_version, include, exclude):
|
|||
return TestSuccess(command, test, python_version=python_version)
|
||||
|
||||
|
||||
def intercept_command(args, cmd, target_name, capture=False, env=None, data=None, cwd=None, python_version=None):
|
||||
def intercept_command(args, cmd, target_name, capture=False, env=None, data=None, cwd=None, python_version=None, path=None):
|
||||
"""
|
||||
:type args: TestConfig
|
||||
:type cmd: collections.Iterable[str]
|
||||
|
@ -874,6 +874,7 @@ def intercept_command(args, cmd, target_name, capture=False, env=None, data=None
|
|||
:type data: str | None
|
||||
:type cwd: str | None
|
||||
:type python_version: str | None
|
||||
:type path: str | None
|
||||
:rtype: str | None, str | None
|
||||
"""
|
||||
if not env:
|
||||
|
@ -883,7 +884,7 @@ def intercept_command(args, cmd, target_name, capture=False, env=None, data=None
|
|||
inject_path = get_coverage_path(args)
|
||||
config_path = os.path.join(inject_path, 'injector.json')
|
||||
version = python_version or args.python_version
|
||||
interpreter = find_executable('python%s' % version)
|
||||
interpreter = find_executable('python%s' % version, path=path)
|
||||
coverage_file = os.path.abspath(os.path.join(inject_path, '..', 'output', '%s=%s=%s=%s=coverage' % (
|
||||
args.command, target_name, args.coverage_label or 'local-%s' % version, 'python-%s' % version)))
|
||||
|
||||
|
|
|
@ -19,6 +19,7 @@ from lib.util import (
|
|||
run_command,
|
||||
deepest_path,
|
||||
parse_to_dict,
|
||||
remove_tree,
|
||||
)
|
||||
|
||||
from lib.ansible_util import (
|
||||
|
@ -38,6 +39,7 @@ from lib.executor import (
|
|||
install_command_requirements,
|
||||
SUPPORTED_PYTHON_VERSIONS,
|
||||
intercept_command,
|
||||
generate_pip_install,
|
||||
)
|
||||
|
||||
from lib.config import (
|
||||
|
@ -668,6 +670,91 @@ def command_sanity_ansible_doc(args, targets, python_version):
|
|||
return SanitySuccess(test, python_version=python_version)
|
||||
|
||||
|
||||
def command_sanity_import(args, targets, python_version):
|
||||
"""
|
||||
:type args: SanityConfig
|
||||
:type targets: SanityTargets
|
||||
:type python_version: str
|
||||
:rtype: SanityResult
|
||||
"""
|
||||
test = 'import'
|
||||
|
||||
with open('test/sanity/import/skip.txt', 'r') as skip_fd:
|
||||
skip_paths = skip_fd.read().splitlines()
|
||||
|
||||
skip_paths_set = set(skip_paths)
|
||||
|
||||
paths = sorted(
|
||||
i.path
|
||||
for i in targets.include
|
||||
if os.path.splitext(i.path)[1] == '.py' and
|
||||
(i.path.startswith('lib/ansible/modules/') or i.path.startswith('lib/ansible/module_utils/')) and
|
||||
i.path not in skip_paths_set
|
||||
)
|
||||
|
||||
if not paths:
|
||||
return SanitySkipped(test, python_version=python_version)
|
||||
|
||||
env = ansible_environment(args, color=False)
|
||||
|
||||
# create a clean virtual environment to minimize the available imports beyond the python standard library
|
||||
virtual_environment_path = os.path.abspath('test/runner/.tox/minimal-py%s' % python_version.replace('.', ''))
|
||||
virtual_environment_bin = os.path.join(virtual_environment_path, 'bin')
|
||||
|
||||
remove_tree(virtual_environment_path)
|
||||
|
||||
cmd = ['virtualenv', virtual_environment_path, '--python', 'python%s' % python_version, '--no-setuptools', '--no-wheel']
|
||||
|
||||
if not args.coverage:
|
||||
cmd.append('--no-pip')
|
||||
|
||||
run_command(args, cmd, capture=True)
|
||||
|
||||
# add the importer to our virtual environment so it can be accessed through the coverage injector
|
||||
importer_path = os.path.join(virtual_environment_bin, 'importer.py')
|
||||
os.symlink(os.path.abspath('test/runner/importer.py'), importer_path)
|
||||
|
||||
# activate the virtual environment
|
||||
env['PATH'] = '%s:%s' % (virtual_environment_bin, env['PATH'])
|
||||
env['PYTHONPATH'] = os.path.abspath('test/runner/import/lib')
|
||||
|
||||
# make sure coverage is available in the virtual environment if needed
|
||||
if args.coverage:
|
||||
run_command(args, generate_pip_install('sanity.import', packages=['coverage']), env=env)
|
||||
run_command(args, ['pip', 'uninstall', '--disable-pip-version-check', '-y', 'pip'], env=env)
|
||||
|
||||
cmd = ['importer.py'] + paths
|
||||
|
||||
results = []
|
||||
|
||||
try:
|
||||
stdout, stderr = intercept_command(args, cmd, target_name=test, env=env, capture=True, python_version=python_version, path=env['PATH'])
|
||||
|
||||
if stdout or stderr:
|
||||
raise SubprocessError(cmd, stdout=stdout, stderr=stderr)
|
||||
except SubprocessError as ex:
|
||||
if ex.status != 10 or ex.stderr or not ex.stdout:
|
||||
raise
|
||||
|
||||
pattern = r'^(?P<path>[^:]*):(?P<line>[0-9]+):(?P<column>[0-9]+): (?P<message>.*)$'
|
||||
|
||||
results = [re.search(pattern, line).groupdict() for line in ex.stdout.splitlines()]
|
||||
|
||||
results = [SanityMessage(
|
||||
message=r['message'],
|
||||
path=r['path'],
|
||||
line=int(r['line']),
|
||||
column=int(r['column']),
|
||||
) for r in results]
|
||||
|
||||
results = [result for result in results if result.path not in skip_paths]
|
||||
|
||||
if results:
|
||||
return SanityFailure(test, messages=results, python_version=python_version)
|
||||
|
||||
return SanitySuccess(test, python_version=python_version)
|
||||
|
||||
|
||||
def collect_code_smell_tests():
|
||||
"""
|
||||
:rtype: tuple(SanityFunc)
|
||||
|
@ -771,6 +858,7 @@ SANITY_TESTS = (
|
|||
SanityFunc('rstcheck', command_sanity_rstcheck, intercept=False),
|
||||
SanityFunc('validate-modules', command_sanity_validate_modules, intercept=False),
|
||||
SanityFunc('ansible-doc', command_sanity_ansible_doc),
|
||||
SanityFunc('import', command_sanity_import),
|
||||
)
|
||||
|
||||
|
||||
|
|
|
@ -7,5 +7,6 @@ pylint
|
|||
pytest
|
||||
rstcheck
|
||||
sphinx
|
||||
virtualenv
|
||||
voluptuous
|
||||
yamllint
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue