2017-08-19 02:21:11 +02:00
|
|
|
"""Sanity test using validate-modules."""
|
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
|
|
|
|
2018-01-11 22:36:08 +01:00
|
|
|
import collections
|
2017-08-19 02:21:11 +02:00
|
|
|
import json
|
2018-01-11 22:36:08 +01:00
|
|
|
import os
|
2017-08-19 02:21:11 +02:00
|
|
|
|
2019-07-11 22:03:49 +02:00
|
|
|
import lib.types as t
|
|
|
|
|
2017-08-19 02:21:11 +02:00
|
|
|
from lib.sanity import (
|
|
|
|
SanitySingleVersion,
|
|
|
|
SanityMessage,
|
|
|
|
SanityFailure,
|
|
|
|
SanitySuccess,
|
|
|
|
SanitySkipped,
|
|
|
|
)
|
|
|
|
|
|
|
|
from lib.util import (
|
|
|
|
SubprocessError,
|
|
|
|
display,
|
2018-09-20 08:20:27 +02:00
|
|
|
read_lines_without_comments,
|
2019-07-10 07:50:15 +02:00
|
|
|
INSTALL_ROOT,
|
2017-08-19 02:21:11 +02:00
|
|
|
)
|
|
|
|
|
2019-07-11 07:00:34 +02:00
|
|
|
from lib.util_common import (
|
|
|
|
run_command,
|
|
|
|
)
|
|
|
|
|
2017-08-19 02:21:11 +02:00
|
|
|
from lib.ansible_util import (
|
|
|
|
ansible_environment,
|
|
|
|
)
|
|
|
|
|
|
|
|
from lib.config import (
|
|
|
|
SanityConfig,
|
|
|
|
)
|
|
|
|
|
2018-01-11 22:36:08 +01:00
|
|
|
from lib.test import (
|
|
|
|
calculate_confidence,
|
|
|
|
calculate_best_confidence,
|
|
|
|
)
|
|
|
|
|
|
|
|
VALIDATE_SKIP_PATH = 'test/sanity/validate-modules/skip.txt'
|
|
|
|
VALIDATE_IGNORE_PATH = 'test/sanity/validate-modules/ignore.txt'
|
|
|
|
|
2019-03-05 17:26:06 +01:00
|
|
|
UNSUPPORTED_PYTHON_VERSIONS = (
|
|
|
|
'2.6',
|
|
|
|
'2.7',
|
|
|
|
)
|
|
|
|
|
2017-08-19 02:21:11 +02:00
|
|
|
|
|
|
|
class ValidateModulesTest(SanitySingleVersion):
|
|
|
|
"""Sanity test using validate-modules."""
|
|
|
|
def test(self, args, targets):
|
|
|
|
"""
|
|
|
|
:type args: SanityConfig
|
|
|
|
:type targets: SanityTargets
|
2018-04-04 03:53:53 +02:00
|
|
|
:rtype: TestResult
|
2017-08-19 02:21:11 +02:00
|
|
|
"""
|
2019-03-05 17:26:06 +01:00
|
|
|
if args.python_version in UNSUPPORTED_PYTHON_VERSIONS:
|
|
|
|
display.warning('Skipping validate-modules on unsupported Python version %s.' % args.python_version)
|
|
|
|
return SanitySkipped(self.name)
|
|
|
|
|
2019-07-10 07:50:15 +02:00
|
|
|
skip_paths = read_lines_without_comments(VALIDATE_SKIP_PATH, optional=True)
|
2018-02-20 17:51:01 +01:00
|
|
|
skip_paths_set = set(skip_paths)
|
|
|
|
|
2017-08-19 02:21:11 +02:00
|
|
|
env = ansible_environment(args, color=False)
|
|
|
|
|
2018-02-20 17:51:01 +01:00
|
|
|
paths = sorted([i.path for i in targets.include if i.module and i.path not in skip_paths_set])
|
2017-08-19 02:21:11 +02:00
|
|
|
|
|
|
|
if not paths:
|
|
|
|
return SanitySkipped(self.name)
|
|
|
|
|
|
|
|
cmd = [
|
2018-03-14 19:35:59 +01:00
|
|
|
args.python_executable,
|
2019-07-10 07:50:15 +02:00
|
|
|
os.path.join(INSTALL_ROOT, 'test/sanity/validate-modules/validate-modules'),
|
2017-08-19 02:21:11 +02:00
|
|
|
'--format', 'json',
|
2018-01-12 00:41:53 +01:00
|
|
|
'--arg-spec',
|
2017-08-19 02:21:11 +02:00
|
|
|
] + paths
|
|
|
|
|
2018-01-11 22:36:08 +01:00
|
|
|
invalid_ignores = []
|
|
|
|
|
2019-07-10 07:50:15 +02:00
|
|
|
ignore_entries = read_lines_without_comments(VALIDATE_IGNORE_PATH, optional=True)
|
2019-07-11 22:03:49 +02:00
|
|
|
ignore = collections.defaultdict(dict) # type: t.Dict[str, t.Dict[str, int]]
|
2018-09-20 08:20:27 +02:00
|
|
|
line = 0
|
|
|
|
|
|
|
|
for ignore_entry in ignore_entries:
|
|
|
|
line += 1
|
2018-01-11 22:36:08 +01:00
|
|
|
|
2018-09-20 08:20:27 +02:00
|
|
|
if not ignore_entry:
|
|
|
|
continue
|
2018-01-11 22:36:08 +01:00
|
|
|
|
2018-09-20 08:20:27 +02:00
|
|
|
if ' ' not in ignore_entry:
|
|
|
|
invalid_ignores.append((line, 'Invalid syntax'))
|
|
|
|
continue
|
2018-01-11 22:36:08 +01:00
|
|
|
|
2018-09-20 08:20:27 +02:00
|
|
|
path, code = ignore_entry.split(' ', 1)
|
2018-01-11 22:36:08 +01:00
|
|
|
|
2018-09-20 08:20:27 +02:00
|
|
|
ignore[path][code] = line
|
2018-01-11 22:36:08 +01:00
|
|
|
|
2017-08-19 02:21:11 +02:00
|
|
|
if args.base_branch:
|
|
|
|
cmd.extend([
|
|
|
|
'--base-branch', args.base_branch,
|
|
|
|
])
|
|
|
|
else:
|
|
|
|
display.warning('Cannot perform module comparison against the base branch. Base branch not detected when running locally.')
|
|
|
|
|
|
|
|
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 not in (0, 3):
|
|
|
|
raise SubprocessError(cmd=cmd, status=status, stderr=stderr, stdout=stdout)
|
|
|
|
|
|
|
|
if args.explain:
|
|
|
|
return SanitySuccess(self.name)
|
|
|
|
|
|
|
|
messages = json.loads(stdout)
|
|
|
|
|
2018-01-11 22:36:08 +01:00
|
|
|
errors = []
|
2017-08-19 02:21:11 +02:00
|
|
|
|
|
|
|
for filename in messages:
|
|
|
|
output = messages[filename]
|
|
|
|
|
|
|
|
for item in output['errors']:
|
2018-01-11 22:36:08 +01:00
|
|
|
errors.append(SanityMessage(
|
2017-08-19 02:21:11 +02:00
|
|
|
path=filename,
|
|
|
|
line=int(item['line']) if 'line' in item else 0,
|
|
|
|
column=int(item['column']) if 'column' in item else 0,
|
|
|
|
level='error',
|
|
|
|
code='E%s' % item['code'],
|
|
|
|
message=item['msg'],
|
|
|
|
))
|
|
|
|
|
2018-01-11 22:36:08 +01:00
|
|
|
filtered = []
|
|
|
|
|
|
|
|
for error in errors:
|
|
|
|
if error.code in ignore[error.path]:
|
2019-07-11 22:03:49 +02:00
|
|
|
ignore[error.path][error.code] = 0 # error ignored, clear line number of ignore entry to track usage
|
2018-01-11 22:36:08 +01:00
|
|
|
else:
|
|
|
|
filtered.append(error) # error not ignored
|
|
|
|
|
|
|
|
errors = filtered
|
|
|
|
|
|
|
|
for invalid_ignore in invalid_ignores:
|
|
|
|
errors.append(SanityMessage(
|
|
|
|
code='A201',
|
|
|
|
message=invalid_ignore[1],
|
|
|
|
path=VALIDATE_IGNORE_PATH,
|
|
|
|
line=invalid_ignore[0],
|
|
|
|
column=1,
|
|
|
|
confidence=calculate_confidence(VALIDATE_IGNORE_PATH, line, args.metadata) if args.metadata.changes else None,
|
|
|
|
))
|
|
|
|
|
2018-09-20 08:20:27 +02:00
|
|
|
line = 0
|
|
|
|
|
2018-01-11 22:36:08 +01:00
|
|
|
for path in skip_paths:
|
|
|
|
line += 1
|
|
|
|
|
2018-09-20 08:20:27 +02:00
|
|
|
if not path:
|
|
|
|
continue
|
|
|
|
|
2018-01-11 22:36:08 +01:00
|
|
|
if not os.path.exists(path):
|
|
|
|
# Keep files out of the list which no longer exist in the repo.
|
|
|
|
errors.append(SanityMessage(
|
|
|
|
code='A101',
|
|
|
|
message='Remove "%s" since it does not exist' % path,
|
|
|
|
path=VALIDATE_SKIP_PATH,
|
|
|
|
line=line,
|
|
|
|
column=1,
|
|
|
|
confidence=calculate_best_confidence(((VALIDATE_SKIP_PATH, line), (path, 0)), args.metadata) if args.metadata.changes else None,
|
|
|
|
))
|
|
|
|
|
2019-06-24 19:55:16 +02:00
|
|
|
for path in sorted(ignore.keys()):
|
|
|
|
if os.path.exists(path):
|
|
|
|
continue
|
|
|
|
|
|
|
|
for line in sorted(ignore[path].values()):
|
|
|
|
# Keep files out of the list which no longer exist in the repo.
|
|
|
|
errors.append(SanityMessage(
|
|
|
|
code='A101',
|
|
|
|
message='Remove "%s" since it does not exist' % path,
|
|
|
|
path=VALIDATE_IGNORE_PATH,
|
|
|
|
line=line,
|
|
|
|
column=1,
|
|
|
|
confidence=calculate_best_confidence(((VALIDATE_IGNORE_PATH, line), (path, 0)), args.metadata) if args.metadata.changes else None,
|
|
|
|
))
|
|
|
|
|
2018-01-11 22:36:08 +01:00
|
|
|
for path in paths:
|
|
|
|
if path not in ignore:
|
|
|
|
continue
|
|
|
|
|
|
|
|
for code in ignore[path]:
|
|
|
|
line = ignore[path][code]
|
|
|
|
|
|
|
|
if not line:
|
|
|
|
continue
|
|
|
|
|
|
|
|
errors.append(SanityMessage(
|
|
|
|
code='A102',
|
|
|
|
message='Remove since "%s" passes "%s" test' % (path, code),
|
|
|
|
path=VALIDATE_IGNORE_PATH,
|
|
|
|
line=line,
|
|
|
|
column=1,
|
|
|
|
confidence=calculate_best_confidence(((VALIDATE_IGNORE_PATH, line), (path, 0)), args.metadata) if args.metadata.changes else None,
|
|
|
|
))
|
|
|
|
|
|
|
|
if errors:
|
|
|
|
return SanityFailure(self.name, messages=errors)
|
2017-08-19 02:21:11 +02:00
|
|
|
|
|
|
|
return SanitySuccess(self.name)
|