2017-08-19 02:21:11 +02:00
|
|
|
"""Sanity test for proper import exception handling."""
|
2019-07-12 08:46:20 +02:00
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
2017-08-19 02:21:11 +02:00
|
|
|
|
|
|
|
import os
|
|
|
|
|
2019-07-30 20:10:55 +02:00
|
|
|
import lib.types as t
|
|
|
|
|
2017-08-19 02:21:11 +02:00
|
|
|
from lib.sanity import (
|
|
|
|
SanityMultipleVersion,
|
|
|
|
SanityMessage,
|
|
|
|
SanityFailure,
|
|
|
|
SanitySuccess,
|
2019-07-30 20:10:55 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
from lib.target import (
|
|
|
|
TestTarget,
|
2017-08-19 02:21:11 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
from lib.util import (
|
|
|
|
SubprocessError,
|
|
|
|
remove_tree,
|
2018-02-28 00:05:39 +01:00
|
|
|
display,
|
2018-03-14 19:35:59 +01:00
|
|
|
find_python,
|
2018-09-21 07:20:08 +02:00
|
|
|
parse_to_list_of_dict,
|
2018-10-04 16:02:42 +02:00
|
|
|
make_dirs,
|
2019-07-16 01:20:03 +02:00
|
|
|
is_subdir,
|
2017-08-19 02:21:11 +02:00
|
|
|
)
|
|
|
|
|
2019-07-11 07:00:34 +02:00
|
|
|
from lib.util_common import (
|
|
|
|
intercept_command,
|
|
|
|
run_command,
|
|
|
|
)
|
|
|
|
|
2017-08-19 02:21:11 +02:00
|
|
|
from lib.ansible_util import (
|
|
|
|
ansible_environment,
|
|
|
|
)
|
|
|
|
|
|
|
|
from lib.executor import (
|
|
|
|
generate_pip_install,
|
|
|
|
)
|
|
|
|
|
|
|
|
from lib.config import (
|
|
|
|
SanityConfig,
|
|
|
|
)
|
|
|
|
|
2019-07-23 04:24:48 +02:00
|
|
|
from lib.coverage_util import (
|
|
|
|
coverage_context,
|
|
|
|
)
|
|
|
|
|
|
|
|
from lib.data import (
|
|
|
|
data_context,
|
|
|
|
ANSIBLE_ROOT,
|
|
|
|
)
|
|
|
|
|
2017-08-19 02:21:11 +02:00
|
|
|
|
|
|
|
class ImportTest(SanityMultipleVersion):
|
|
|
|
"""Sanity test for proper import exception handling."""
|
2019-07-30 20:10:55 +02:00
|
|
|
def filter_targets(self, targets): # type: (t.List[TestTarget]) -> t.List[TestTarget]
|
|
|
|
"""Return the given list of test targets, filtered to include only those relevant for the test."""
|
|
|
|
return [target for target in targets if os.path.splitext(target.path)[1] == '.py' and
|
|
|
|
(is_subdir(target.path, data_context().content.module_path) or is_subdir(target.path, data_context().content.module_utils_path))]
|
|
|
|
|
2017-08-19 02:21:11 +02:00
|
|
|
def test(self, args, targets, python_version):
|
|
|
|
"""
|
|
|
|
:type args: SanityConfig
|
|
|
|
:type targets: SanityTargets
|
|
|
|
:type python_version: str
|
2018-04-04 03:53:53 +02:00
|
|
|
:rtype: TestResult
|
2017-08-19 02:21:11 +02:00
|
|
|
"""
|
2019-07-26 08:29:23 +02:00
|
|
|
settings = self.load_processor(args, python_version)
|
2017-08-19 02:21:11 +02:00
|
|
|
|
2019-07-30 20:10:55 +02:00
|
|
|
paths = [target.path for target in targets.include]
|
2017-08-19 02:21:11 +02:00
|
|
|
|
|
|
|
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)
|
|
|
|
|
2018-09-04 18:28:34 +02:00
|
|
|
python = find_python(python_version)
|
|
|
|
|
|
|
|
cmd = [python, '-m', 'virtualenv', virtual_environment_path, '--python', python, '--no-setuptools', '--no-wheel']
|
2017-08-19 02:21:11 +02:00
|
|
|
|
|
|
|
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')
|
|
|
|
if not args.explain:
|
2019-07-23 04:24:48 +02:00
|
|
|
os.symlink(os.path.abspath(os.path.join(ANSIBLE_ROOT, 'test/sanity/import/importer.py')), importer_path)
|
2017-08-19 02:21:11 +02:00
|
|
|
|
2018-10-04 16:02:42 +02:00
|
|
|
# create a minimal python library
|
|
|
|
python_path = os.path.abspath('test/runner/.tox/import/lib')
|
|
|
|
ansible_path = os.path.join(python_path, 'ansible')
|
|
|
|
ansible_init = os.path.join(ansible_path, '__init__.py')
|
|
|
|
ansible_link = os.path.join(ansible_path, 'module_utils')
|
|
|
|
|
|
|
|
if not args.explain:
|
2019-07-23 04:24:48 +02:00
|
|
|
remove_tree(ansible_path)
|
|
|
|
|
2018-10-04 16:02:42 +02:00
|
|
|
make_dirs(ansible_path)
|
|
|
|
|
|
|
|
with open(ansible_init, 'w'):
|
|
|
|
pass
|
|
|
|
|
2019-07-23 04:24:48 +02:00
|
|
|
os.symlink(os.path.join(ANSIBLE_ROOT, 'lib/ansible/module_utils'), ansible_link)
|
2018-10-04 16:02:42 +02:00
|
|
|
|
2019-07-24 03:10:24 +02:00
|
|
|
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
|
|
|
|
|
2017-08-19 02:21:11 +02:00
|
|
|
# activate the virtual environment
|
|
|
|
env['PATH'] = '%s:%s' % (virtual_environment_bin, env['PATH'])
|
2018-10-04 16:02:42 +02:00
|
|
|
env['PYTHONPATH'] = python_path
|
2017-08-19 02:21:11 +02:00
|
|
|
|
|
|
|
# make sure coverage is available in the virtual environment if needed
|
|
|
|
if args.coverage:
|
2018-03-14 19:35:59 +01:00
|
|
|
run_command(args, generate_pip_install(['pip'], 'sanity.import', packages=['setuptools']), env=env)
|
|
|
|
run_command(args, generate_pip_install(['pip'], 'sanity.import', packages=['coverage']), env=env)
|
2018-02-06 23:03:23 +01:00
|
|
|
run_command(args, ['pip', 'uninstall', '--disable-pip-version-check', '-y', 'setuptools'], env=env)
|
2017-08-19 02:21:11 +02:00
|
|
|
run_command(args, ['pip', 'uninstall', '--disable-pip-version-check', '-y', 'pip'], env=env)
|
|
|
|
|
2018-02-28 00:05:39 +01:00
|
|
|
cmd = ['importer.py']
|
|
|
|
|
|
|
|
data = '\n'.join(paths)
|
|
|
|
|
|
|
|
display.info(data, verbosity=4)
|
2017-08-19 02:21:11 +02:00
|
|
|
|
|
|
|
results = []
|
|
|
|
|
2019-03-13 15:14:12 +01:00
|
|
|
virtualenv_python = os.path.join(virtual_environment_bin, 'python')
|
|
|
|
|
2017-08-19 02:21:11 +02:00
|
|
|
try:
|
2019-07-23 04:24:48 +02:00
|
|
|
with coverage_context(args):
|
|
|
|
stdout, stderr = intercept_command(args, cmd, self.name, env, capture=True, data=data, python_version=python_version,
|
|
|
|
virtualenv=virtualenv_python)
|
2017-08-19 02:21:11 +02:00
|
|
|
|
|
|
|
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>.*)$'
|
|
|
|
|
2018-09-21 07:20:08 +02:00
|
|
|
results = parse_to_list_of_dict(pattern, ex.stdout)
|
2017-08-19 02:21:11 +02:00
|
|
|
|
|
|
|
results = [SanityMessage(
|
|
|
|
message=r['message'],
|
|
|
|
path=r['path'],
|
|
|
|
line=int(r['line']),
|
|
|
|
column=int(r['column']),
|
|
|
|
) for r in results]
|
|
|
|
|
2019-07-23 22:31:54 +02:00
|
|
|
results = settings.process_errors(results, paths)
|
2017-08-19 02:21:11 +02:00
|
|
|
|
|
|
|
if results:
|
|
|
|
return SanityFailure(self.name, messages=results, python_version=python_version)
|
|
|
|
|
|
|
|
return SanitySuccess(self.name, python_version=python_version)
|