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-08-06 23:43:29 +02:00
|
|
|
from .. import types as t
|
2019-07-30 20:10:55 +02:00
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from ..sanity import (
|
2017-08-19 02:21:11 +02:00
|
|
|
SanityMultipleVersion,
|
|
|
|
SanityMessage,
|
|
|
|
SanityFailure,
|
|
|
|
SanitySuccess,
|
2019-08-28 18:10:17 +02:00
|
|
|
SanitySkipped,
|
2019-08-06 02:40:00 +02:00
|
|
|
SANITY_ROOT,
|
2019-07-30 20:10:55 +02:00
|
|
|
)
|
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from ..target import (
|
2019-07-30 20:10:55 +02:00
|
|
|
TestTarget,
|
2017-08-19 02:21:11 +02:00
|
|
|
)
|
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from ..util import (
|
2017-08-19 02:21:11 +02:00
|
|
|
SubprocessError,
|
|
|
|
remove_tree,
|
2018-02-28 00:05:39 +01:00
|
|
|
display,
|
2018-09-21 07:20:08 +02:00
|
|
|
parse_to_list_of_dict,
|
2019-07-16 01:20:03 +02:00
|
|
|
is_subdir,
|
2019-08-28 18:10:17 +02:00
|
|
|
generate_pip_command,
|
|
|
|
find_python,
|
2017-08-19 02:21:11 +02:00
|
|
|
)
|
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from ..util_common import (
|
2019-07-11 07:00:34 +02:00
|
|
|
intercept_command,
|
|
|
|
run_command,
|
2019-08-28 08:40:06 +02:00
|
|
|
ResultType,
|
2019-07-11 07:00:34 +02:00
|
|
|
)
|
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from ..ansible_util import (
|
2017-08-19 02:21:11 +02:00
|
|
|
ansible_environment,
|
|
|
|
)
|
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from ..executor import (
|
2017-08-19 02:21:11 +02:00
|
|
|
generate_pip_install,
|
|
|
|
)
|
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from ..config import (
|
2017-08-19 02:21:11 +02:00
|
|
|
SanityConfig,
|
|
|
|
)
|
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from ..coverage_util import (
|
2019-07-23 04:24:48 +02:00
|
|
|
coverage_context,
|
|
|
|
)
|
|
|
|
|
2019-08-28 18:10:17 +02:00
|
|
|
from ..venv import (
|
|
|
|
create_virtual_environment,
|
|
|
|
)
|
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from ..data import (
|
2019-07-23 04:24:48 +02:00
|
|
|
data_context,
|
|
|
|
)
|
|
|
|
|
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-08-28 18:10:17 +02:00
|
|
|
capture_pip = args.verbosity < 2
|
|
|
|
|
2020-05-26 18:42:06 +02:00
|
|
|
python = find_python(python_version)
|
|
|
|
|
2019-08-28 18:10:17 +02:00
|
|
|
if python_version.startswith('2.') and args.requirements:
|
|
|
|
# hack to make sure that virtualenv is available under Python 2.x
|
|
|
|
# on Python 3.x we can use the built-in venv
|
2020-05-26 18:42:06 +02:00
|
|
|
pip = generate_pip_command(python)
|
2019-08-28 18:10:17 +02:00
|
|
|
run_command(args, generate_pip_install(pip, 'sanity.import', packages=['virtualenv']), capture=capture_pip)
|
|
|
|
|
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)
|
|
|
|
|
2019-08-28 08:40:06 +02:00
|
|
|
temp_root = os.path.join(ResultType.TMP.path, 'sanity', 'import')
|
|
|
|
|
2017-08-19 02:21:11 +02:00
|
|
|
# create a clean virtual environment to minimize the available imports beyond the python standard library
|
2019-08-28 08:40:06 +02:00
|
|
|
virtual_environment_path = os.path.join(temp_root, 'minimal-py%s' % python_version.replace('.', ''))
|
2017-08-19 02:21:11 +02:00
|
|
|
virtual_environment_bin = os.path.join(virtual_environment_path, 'bin')
|
|
|
|
|
|
|
|
remove_tree(virtual_environment_path)
|
|
|
|
|
2019-08-28 18:10:17 +02:00
|
|
|
if not create_virtual_environment(args, python_version, virtual_environment_path):
|
|
|
|
display.warning("Skipping sanity test '%s' on Python %s due to missing virtual environment support." % (self.name, python_version))
|
|
|
|
return SanitySkipped(self.name, python_version)
|
2017-08-19 02:21:11 +02:00
|
|
|
|
|
|
|
# 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')
|
2020-05-26 18:42:06 +02:00
|
|
|
yaml_to_json_path = os.path.join(virtual_environment_bin, 'yaml_to_json.py')
|
2017-08-19 02:21:11 +02:00
|
|
|
if not args.explain:
|
2019-08-06 02:40:00 +02:00
|
|
|
os.symlink(os.path.abspath(os.path.join(SANITY_ROOT, 'import', 'importer.py')), importer_path)
|
2020-05-26 18:42:06 +02:00
|
|
|
os.symlink(os.path.abspath(os.path.join(SANITY_ROOT, 'import', 'yaml_to_json.py')), yaml_to_json_path)
|
2017-08-19 02:21:11 +02:00
|
|
|
|
|
|
|
# activate the virtual environment
|
|
|
|
env['PATH'] = '%s:%s' % (virtual_environment_bin, env['PATH'])
|
|
|
|
|
2019-08-28 08:40:06 +02:00
|
|
|
env.update(
|
2019-10-21 19:56:41 +02:00
|
|
|
SANITY_TEMP_PATH=ResultType.TMP.path,
|
2019-08-28 08:40:06 +02:00
|
|
|
)
|
|
|
|
|
2019-10-21 19:56:41 +02:00
|
|
|
if data_context().content.collection:
|
|
|
|
env.update(
|
|
|
|
SANITY_COLLECTION_FULL_NAME=data_context().content.collection.full_name,
|
2020-05-26 18:42:06 +02:00
|
|
|
SANITY_EXTERNAL_PYTHON=python,
|
2019-10-21 19:56:41 +02:00
|
|
|
)
|
|
|
|
|
2019-08-28 18:10:17 +02:00
|
|
|
virtualenv_python = os.path.join(virtual_environment_bin, 'python')
|
|
|
|
virtualenv_pip = generate_pip_command(virtualenv_python)
|
|
|
|
|
2017-08-19 02:21:11 +02:00
|
|
|
# make sure coverage is available in the virtual environment if needed
|
|
|
|
if args.coverage:
|
2019-08-28 18:10:17 +02:00
|
|
|
run_command(args, generate_pip_install(virtualenv_pip, 'sanity.import', packages=['setuptools']), env=env, capture=capture_pip)
|
|
|
|
run_command(args, generate_pip_install(virtualenv_pip, 'sanity.import', packages=['coverage']), env=env, capture=capture_pip)
|
|
|
|
|
2020-03-01 20:25:58 +01:00
|
|
|
try:
|
|
|
|
# In some environments pkg_resources is installed as a separate pip package which needs to be removed.
|
|
|
|
# For example, using Python 3.8 on Ubuntu 18.04 a virtualenv is created with only pip and setuptools.
|
|
|
|
# However, a venv is created with an additional pkg-resources package which is independent of setuptools.
|
|
|
|
# Making sure pkg-resources is removed preserves the import test consistency between venv and virtualenv.
|
|
|
|
# Additionally, in the above example, the pyparsing package vendored with pkg-resources is out-of-date and generates deprecation warnings.
|
|
|
|
# Thus it is important to remove pkg-resources to prevent system installed packages from generating deprecation warnings.
|
|
|
|
run_command(args, virtualenv_pip + ['uninstall', '--disable-pip-version-check', '-y', 'pkg-resources'], env=env, capture=capture_pip)
|
|
|
|
except SubprocessError:
|
|
|
|
pass
|
|
|
|
|
2019-08-28 18:10:17 +02:00
|
|
|
run_command(args, virtualenv_pip + ['uninstall', '--disable-pip-version-check', '-y', 'setuptools'], env=env, capture=capture_pip)
|
|
|
|
run_command(args, virtualenv_pip + ['uninstall', '--disable-pip-version-check', '-y', 'pip'], env=env, capture=capture_pip)
|
2017-08-19 02:21:11 +02:00
|
|
|
|
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 = []
|
|
|
|
|
|
|
|
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
|
|
|
|
2019-08-28 08:40:06 +02:00
|
|
|
relative_temp_root = os.path.relpath(temp_root, data_context().content.root) + os.path.sep
|
|
|
|
|
2017-08-19 02:21:11 +02:00
|
|
|
results = [SanityMessage(
|
|
|
|
message=r['message'],
|
2019-08-28 08:40:06 +02:00
|
|
|
path=os.path.relpath(r['path'], relative_temp_root) if r['path'].startswith(relative_temp_root) else r['path'],
|
2017-08-19 02:21:11 +02:00
|
|
|
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)
|