2017-08-19 02:21:11 +02:00
|
|
|
"""Sanity test using pylint."""
|
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
|
|
|
|
2019-03-19 17:23:42 +01:00
|
|
|
import itertools
|
2017-08-19 02:21:11 +02:00
|
|
|
import json
|
|
|
|
import os
|
2017-10-26 09:21:46 +02:00
|
|
|
import datetime
|
2017-08-19 02:21:11 +02:00
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from .. import types as t
|
2019-07-10 07:50:15 +02:00
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from ..sanity import (
|
2017-08-19 02:21:11 +02:00
|
|
|
SanitySingleVersion,
|
|
|
|
SanityMessage,
|
|
|
|
SanityFailure,
|
|
|
|
SanitySuccess,
|
2019-08-06 02:40:00 +02:00
|
|
|
SANITY_ROOT,
|
2017-08-19 02:21:11 +02:00
|
|
|
)
|
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from ..target import (
|
2019-07-30 20:10:55 +02:00
|
|
|
TestTarget,
|
|
|
|
)
|
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from ..util import (
|
2017-08-19 02:21:11 +02:00
|
|
|
SubprocessError,
|
2017-09-12 01:39:34 +02:00
|
|
|
display,
|
2018-09-21 20:38:22 +02:00
|
|
|
ConfigParser,
|
2019-07-16 01:20:03 +02:00
|
|
|
is_subdir,
|
2019-07-27 01:46:52 +02:00
|
|
|
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
|
|
|
run_command,
|
|
|
|
)
|
|
|
|
|
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 ..config import (
|
2017-08-19 02:21:11 +02:00
|
|
|
SanityConfig,
|
|
|
|
)
|
|
|
|
|
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 PylintTest(SanitySingleVersion):
|
|
|
|
"""Sanity test using pylint."""
|
2019-07-26 16:53:53 +02:00
|
|
|
@property
|
2019-07-26 08:29:23 +02:00
|
|
|
def error_code(self): # type: () -> t.Optional[str]
|
|
|
|
"""Error code for ansible-test matching the format used by the underlying test program, or None if the program does not use error codes."""
|
|
|
|
return 'ansible-test'
|
|
|
|
|
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' or is_subdir(target.path, 'bin')]
|
|
|
|
|
2019-07-27 01:46:52 +02:00
|
|
|
def test(self, args, targets, python_version):
|
2017-08-19 02:21:11 +02:00
|
|
|
"""
|
|
|
|
:type args: SanityConfig
|
|
|
|
:type targets: SanityTargets
|
2019-07-27 01:46:52 +02:00
|
|
|
:type python_version: str
|
2018-04-04 03:53:53 +02:00
|
|
|
:rtype: TestResult
|
2017-08-19 02:21:11 +02:00
|
|
|
"""
|
2019-08-06 02:40:00 +02:00
|
|
|
plugin_dir = os.path.join(SANITY_ROOT, 'pylint', 'plugins')
|
2019-07-10 07:50:15 +02:00
|
|
|
plugin_names = sorted(p[0] for p in [
|
|
|
|
os.path.splitext(p) for p in os.listdir(plugin_dir)] if p[1] == '.py' and p[0] != '__init__')
|
|
|
|
|
2019-07-26 08:29:23 +02:00
|
|
|
settings = self.load_processor(args)
|
2018-01-05 19:18:55 +01: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
|
|
|
|
2019-07-23 04:24:48 +02:00
|
|
|
module_paths = [os.path.relpath(p, data_context().content.module_path).split(os.path.sep) for p in
|
|
|
|
paths if is_subdir(p, data_context().content.module_path)]
|
2019-07-16 19:47:11 +02:00
|
|
|
module_dirs = sorted(set([p[0] for p in module_paths if len(p) > 1]))
|
2018-10-15 07:59:52 +02:00
|
|
|
|
2019-03-19 17:23:42 +01:00
|
|
|
large_module_group_threshold = 500
|
|
|
|
large_module_groups = [key for key, value in
|
2019-07-16 19:47:11 +02:00
|
|
|
itertools.groupby(module_paths, lambda p: p[0] if len(p) > 1 else '') if len(list(value)) > large_module_group_threshold]
|
2019-03-19 17:23:42 +01:00
|
|
|
|
2019-07-23 04:24:48 +02:00
|
|
|
large_module_group_paths = [os.path.relpath(p, data_context().content.module_path).split(os.path.sep) for p in paths
|
|
|
|
if any(is_subdir(p, os.path.join(data_context().content.module_path, g)) for g in large_module_groups)]
|
2019-07-16 19:47:11 +02:00
|
|
|
large_module_group_dirs = sorted(set([os.path.sep.join(p[:2]) for p in large_module_group_paths if len(p) > 2]))
|
2019-03-19 17:23:42 +01:00
|
|
|
|
2018-10-15 07:59:52 +02:00
|
|
|
contexts = []
|
2017-10-26 09:21:46 +02:00
|
|
|
remaining_paths = set(paths)
|
2017-08-19 02:21:11 +02:00
|
|
|
|
2017-10-26 09:21:46 +02:00
|
|
|
def add_context(available_paths, context_name, context_filter):
|
|
|
|
"""
|
|
|
|
:type available_paths: set[str]
|
|
|
|
:type context_name: str
|
|
|
|
:type context_filter: (str) -> bool
|
|
|
|
"""
|
|
|
|
filtered_paths = set(p for p in available_paths if context_filter(p))
|
2018-10-15 07:59:52 +02:00
|
|
|
contexts.append((context_name, sorted(filtered_paths)))
|
2017-10-26 09:21:46 +02:00
|
|
|
available_paths -= filtered_paths
|
2017-08-19 02:21:11 +02:00
|
|
|
|
2018-10-15 07:59:52 +02:00
|
|
|
def filter_path(path_filter=None):
|
|
|
|
"""
|
|
|
|
:type path_filter: str
|
|
|
|
:rtype: (str) -> bool
|
|
|
|
"""
|
|
|
|
def context_filter(path_to_filter):
|
|
|
|
"""
|
|
|
|
:type path_to_filter: str
|
|
|
|
:rtype: bool
|
|
|
|
"""
|
2019-07-16 01:20:03 +02:00
|
|
|
return is_subdir(path_to_filter, path_filter)
|
2018-10-15 07:59:52 +02:00
|
|
|
|
|
|
|
return context_filter
|
|
|
|
|
2019-07-23 04:24:48 +02:00
|
|
|
for large_module_dir in large_module_group_dirs:
|
|
|
|
add_context(remaining_paths, 'modules/%s' % large_module_dir, filter_path(os.path.join(data_context().content.module_path, large_module_dir)))
|
2019-03-19 17:23:42 +01:00
|
|
|
|
2018-10-15 07:59:52 +02:00
|
|
|
for module_dir in module_dirs:
|
2019-07-23 04:24:48 +02:00
|
|
|
add_context(remaining_paths, 'modules/%s' % module_dir, filter_path(os.path.join(data_context().content.module_path, module_dir)))
|
2018-10-15 07:59:52 +02:00
|
|
|
|
2019-07-23 04:24:48 +02:00
|
|
|
add_context(remaining_paths, 'modules', filter_path(data_context().content.module_path))
|
|
|
|
add_context(remaining_paths, 'module_utils', filter_path(data_context().content.module_utils_path))
|
2019-07-16 19:47:11 +02:00
|
|
|
|
2019-07-23 04:24:48 +02:00
|
|
|
add_context(remaining_paths, 'units', filter_path(data_context().content.unit_path))
|
2019-07-16 19:47:11 +02:00
|
|
|
|
2019-07-23 04:24:48 +02:00
|
|
|
if data_context().content.collection:
|
|
|
|
add_context(remaining_paths, 'collection', lambda p: True)
|
|
|
|
else:
|
2019-08-06 23:43:29 +02:00
|
|
|
add_context(remaining_paths, 'validate-modules', filter_path('test/lib/ansible_test/_data/sanity/validate-modules/'))
|
2019-08-07 18:34:11 +02:00
|
|
|
add_context(remaining_paths, 'validate-modules-unit', filter_path('test/lib/ansible_test/tests/validate-modules-unit/'))
|
2019-08-06 23:43:29 +02:00
|
|
|
add_context(remaining_paths, 'sanity', filter_path('test/lib/ansible_test/_data/sanity/'))
|
|
|
|
add_context(remaining_paths, 'ansible-test', filter_path('test/lib/'))
|
2019-07-23 04:24:48 +02:00
|
|
|
add_context(remaining_paths, 'test', filter_path('test/'))
|
|
|
|
add_context(remaining_paths, 'hacking', filter_path('hacking/'))
|
|
|
|
add_context(remaining_paths, 'ansible', lambda p: True)
|
2017-08-19 02:21:11 +02:00
|
|
|
|
2017-10-26 09:21:46 +02:00
|
|
|
messages = []
|
|
|
|
context_times = []
|
2017-08-19 02:21:11 +02:00
|
|
|
|
2019-07-27 01:46:52 +02:00
|
|
|
python = find_python(python_version)
|
|
|
|
|
2017-10-26 09:21:46 +02:00
|
|
|
test_start = datetime.datetime.utcnow()
|
2017-08-19 02:21:11 +02:00
|
|
|
|
2018-10-15 07:59:52 +02:00
|
|
|
for context, context_paths in sorted(contexts):
|
2017-10-26 09:21:46 +02:00
|
|
|
if not context_paths:
|
|
|
|
continue
|
|
|
|
|
|
|
|
context_start = datetime.datetime.utcnow()
|
2019-07-27 01:46:52 +02:00
|
|
|
messages += self.pylint(args, context, context_paths, plugin_dir, plugin_names, python)
|
2017-10-26 09:21:46 +02:00
|
|
|
context_end = datetime.datetime.utcnow()
|
|
|
|
|
|
|
|
context_times.append('%s: %d (%s)' % (context, len(context_paths), context_end - context_start))
|
|
|
|
|
|
|
|
test_end = datetime.datetime.utcnow()
|
|
|
|
|
|
|
|
for context_time in context_times:
|
|
|
|
display.info(context_time, verbosity=4)
|
|
|
|
|
|
|
|
display.info('total: %d (%s)' % (len(paths), test_end - test_start), verbosity=4)
|
2017-08-19 02:21:11 +02:00
|
|
|
|
|
|
|
errors = [SanityMessage(
|
2017-09-09 17:59:09 +02:00
|
|
|
message=m['message'].replace('\n', ' '),
|
2017-08-19 02:21:11 +02:00
|
|
|
path=m['path'],
|
|
|
|
line=int(m['line']),
|
|
|
|
column=int(m['column']),
|
|
|
|
level=m['type'],
|
|
|
|
code=m['symbol'],
|
|
|
|
) for m in messages]
|
|
|
|
|
2018-01-24 19:22:04 +01:00
|
|
|
if args.explain:
|
|
|
|
return SanitySuccess(self.name)
|
|
|
|
|
2019-07-23 22:31:54 +02:00
|
|
|
errors = settings.process_errors(errors, paths)
|
2018-01-05 19:18:55 +01:00
|
|
|
|
2017-08-19 02:21:11 +02:00
|
|
|
if errors:
|
|
|
|
return SanityFailure(self.name, messages=errors)
|
|
|
|
|
|
|
|
return SanitySuccess(self.name)
|
2017-10-26 09:21:46 +02:00
|
|
|
|
2019-07-10 07:50:15 +02:00
|
|
|
@staticmethod
|
2019-07-27 01:46:52 +02:00
|
|
|
def pylint(
|
|
|
|
args, # type: SanityConfig
|
|
|
|
context, # type: str
|
|
|
|
paths, # type: t.List[str]
|
|
|
|
plugin_dir, # type: str
|
|
|
|
plugin_names, # type: t.List[str]
|
|
|
|
python, # type: str
|
|
|
|
): # type: (...) -> t.List[t.Dict[str, str]]
|
2019-07-10 07:50:15 +02:00
|
|
|
"""Run pylint using the config specified by the context on the specified paths."""
|
2019-08-24 08:56:33 +02:00
|
|
|
rcfile = os.path.join(SANITY_ROOT, 'pylint', 'config', context.split('/')[0] + '.cfg')
|
2017-10-26 09:21:46 +02:00
|
|
|
|
|
|
|
if not os.path.exists(rcfile):
|
2019-07-24 00:40:27 +02:00
|
|
|
if data_context().content.collection:
|
2019-08-24 08:56:33 +02:00
|
|
|
rcfile = os.path.join(SANITY_ROOT, 'pylint', 'config', 'collection.cfg')
|
2019-07-24 00:40:27 +02:00
|
|
|
else:
|
2019-08-24 08:56:33 +02:00
|
|
|
rcfile = os.path.join(SANITY_ROOT, 'pylint', 'config', 'default.cfg')
|
2017-10-26 09:21:46 +02:00
|
|
|
|
2018-09-21 20:38:22 +02:00
|
|
|
parser = ConfigParser()
|
2018-01-04 23:30:43 +01:00
|
|
|
parser.read(rcfile)
|
|
|
|
|
|
|
|
if parser.has_section('ansible-test'):
|
|
|
|
config = dict(parser.items('ansible-test'))
|
|
|
|
else:
|
|
|
|
config = dict()
|
|
|
|
|
|
|
|
disable_plugins = set(i.strip() for i in config.get('disable-plugins', '').split(',') if i)
|
2019-07-10 07:50:15 +02:00
|
|
|
load_plugins = set(plugin_names) - disable_plugins
|
2018-01-04 23:30:43 +01:00
|
|
|
|
2017-10-26 09:21:46 +02:00
|
|
|
cmd = [
|
2019-07-27 01:46:52 +02:00
|
|
|
python,
|
2018-03-14 19:35:59 +01:00
|
|
|
'-m', 'pylint',
|
2017-10-26 09:21:46 +02:00
|
|
|
'--jobs', '0',
|
|
|
|
'--reports', 'n',
|
|
|
|
'--max-line-length', '160',
|
|
|
|
'--rcfile', rcfile,
|
|
|
|
'--output-format', 'json',
|
2018-01-04 23:30:43 +01:00
|
|
|
'--load-plugins', ','.join(load_plugins),
|
2017-10-26 09:21:46 +02:00
|
|
|
] + paths
|
|
|
|
|
2019-07-16 19:47:11 +02:00
|
|
|
append_python_path = [plugin_dir]
|
|
|
|
|
2019-07-23 04:24:48 +02:00
|
|
|
if data_context().content.collection:
|
|
|
|
append_python_path.append(data_context().content.collection.root)
|
|
|
|
|
2017-10-26 09:21:46 +02:00
|
|
|
env = ansible_environment(args)
|
2019-07-16 19:47:11 +02:00
|
|
|
env['PYTHONPATH'] += os.path.pathsep + os.path.pathsep.join(append_python_path)
|
2017-10-26 09:21:46 +02:00
|
|
|
|
2019-12-05 03:07:16 +01:00
|
|
|
# expose plugin paths for use in custom plugins
|
|
|
|
env.update(dict(('ANSIBLE_TEST_%s_PATH' % k.upper(), os.path.abspath(v) + os.path.sep) for k, v in data_context().content.plugin_paths.items()))
|
|
|
|
|
2017-10-26 09:21:46 +02:00
|
|
|
if paths:
|
2018-10-15 07:59:52 +02:00
|
|
|
display.info('Checking %d file(s) in context "%s" with config: %s' % (len(paths), context, rcfile), verbosity=1)
|
|
|
|
|
2017-10-26 09:21:46 +02:00
|
|
|
try:
|
|
|
|
stdout, stderr = run_command(args, cmd, env=env, capture=True)
|
|
|
|
status = 0
|
|
|
|
except SubprocessError as ex:
|
|
|
|
stdout = ex.stdout
|
|
|
|
stderr = ex.stderr
|
|
|
|
status = ex.status
|
|
|
|
|
|
|
|
if stderr or status >= 32:
|
|
|
|
raise SubprocessError(cmd=cmd, status=status, stderr=stderr, stdout=stdout)
|
|
|
|
else:
|
|
|
|
stdout = None
|
|
|
|
|
|
|
|
if not args.explain and stdout:
|
|
|
|
messages = json.loads(stdout)
|
|
|
|
else:
|
|
|
|
messages = []
|
|
|
|
|
|
|
|
return messages
|