2017-08-19 00:21:11 +00:00
|
|
|
"""Sanity test using validate-modules."""
|
2019-07-12 06:46:20 +00:00
|
|
|
from __future__ import (absolute_import, division, print_function)
|
|
|
|
__metaclass__ = type
|
2017-08-19 00:21:11 +00:00
|
|
|
|
|
|
|
import json
|
2018-01-11 21:36:08 +00:00
|
|
|
import os
|
2017-08-19 00:21:11 +00:00
|
|
|
|
2019-07-26 06:29:23 +00:00
|
|
|
import lib.types as t
|
|
|
|
|
2017-08-19 00:21:11 +00:00
|
|
|
from lib.sanity import (
|
|
|
|
SanitySingleVersion,
|
|
|
|
SanityMessage,
|
|
|
|
SanityFailure,
|
|
|
|
SanitySuccess,
|
2019-07-30 18:10:55 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
from lib.target import (
|
|
|
|
TestTarget,
|
2017-08-19 00:21:11 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
from lib.util import (
|
|
|
|
SubprocessError,
|
|
|
|
display,
|
2019-07-23 02:24:48 +00:00
|
|
|
ANSIBLE_ROOT,
|
2019-07-26 23:46:52 +00:00
|
|
|
find_python,
|
2017-08-19 00:21:11 +00:00
|
|
|
)
|
|
|
|
|
2019-07-11 05:00:34 +00:00
|
|
|
from lib.util_common import (
|
|
|
|
run_command,
|
|
|
|
)
|
|
|
|
|
2017-08-19 00:21:11 +00:00
|
|
|
from lib.ansible_util import (
|
|
|
|
ansible_environment,
|
|
|
|
)
|
|
|
|
|
|
|
|
from lib.config import (
|
|
|
|
SanityConfig,
|
|
|
|
)
|
|
|
|
|
2019-07-23 02:24:48 +00:00
|
|
|
from lib.data import (
|
|
|
|
data_context,
|
|
|
|
)
|
|
|
|
|
2017-08-19 00:21:11 +00:00
|
|
|
|
|
|
|
class ValidateModulesTest(SanitySingleVersion):
|
|
|
|
"""Sanity test using validate-modules."""
|
2019-07-26 14:53:53 +00:00
|
|
|
@property
|
2019-07-26 06:29:23 +00: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 'A100'
|
|
|
|
|
2019-07-30 18:10:55 +00: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 target.module]
|
|
|
|
|
2019-07-26 23:46:52 +00:00
|
|
|
def test(self, args, targets, python_version):
|
2017-08-19 00:21:11 +00:00
|
|
|
"""
|
|
|
|
:type args: SanityConfig
|
|
|
|
:type targets: SanityTargets
|
2019-07-26 23:46:52 +00:00
|
|
|
:type python_version: str
|
2018-04-04 01:53:53 +00:00
|
|
|
:rtype: TestResult
|
2017-08-19 00:21:11 +00:00
|
|
|
"""
|
2019-07-23 02:24:48 +00:00
|
|
|
if data_context().content.is_ansible:
|
|
|
|
ignore_codes = ()
|
|
|
|
else:
|
|
|
|
ignore_codes = ((
|
|
|
|
'E502', # only ansible content requires __init__.py for module subdirectories
|
|
|
|
))
|
|
|
|
|
2017-08-19 00:21:11 +00:00
|
|
|
env = ansible_environment(args, color=False)
|
|
|
|
|
2019-07-26 06:29:23 +00:00
|
|
|
settings = self.load_processor(args)
|
2019-07-23 20:31:54 +00:00
|
|
|
|
2019-07-30 18:10:55 +00:00
|
|
|
paths = [target.path for target in targets.include]
|
2017-08-19 00:21:11 +00:00
|
|
|
|
|
|
|
cmd = [
|
2019-07-26 23:46:52 +00:00
|
|
|
find_python(python_version),
|
2019-07-23 02:24:48 +00:00
|
|
|
os.path.join(ANSIBLE_ROOT, 'test/sanity/validate-modules/validate-modules'),
|
2017-08-19 00:21:11 +00:00
|
|
|
'--format', 'json',
|
2018-01-11 23:41:53 +00:00
|
|
|
'--arg-spec',
|
2017-08-19 00:21:11 +00:00
|
|
|
] + paths
|
|
|
|
|
|
|
|
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 21:36:08 +00:00
|
|
|
errors = []
|
2017-08-19 00:21:11 +00:00
|
|
|
|
|
|
|
for filename in messages:
|
|
|
|
output = messages[filename]
|
|
|
|
|
|
|
|
for item in output['errors']:
|
2018-01-11 21:36:08 +00:00
|
|
|
errors.append(SanityMessage(
|
2017-08-19 00:21:11 +00: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'],
|
|
|
|
))
|
|
|
|
|
2019-07-23 02:24:48 +00:00
|
|
|
errors = [error for error in errors if error.code not in ignore_codes]
|
2019-07-23 20:31:54 +00:00
|
|
|
errors = settings.process_errors(errors, paths)
|
2018-01-11 21:36:08 +00:00
|
|
|
|
|
|
|
if errors:
|
|
|
|
return SanityFailure(self.name, messages=errors)
|
2017-08-19 00:21:11 +00:00
|
|
|
|
|
|
|
return SanitySuccess(self.name)
|