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
|
|
|
|
|
|
|
import json
|
2018-01-11 22:36:08 +01:00
|
|
|
import os
|
2017-08-19 02:21:11 +02:00
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from .. import types as t
|
2019-07-26 08:29:23 +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,
|
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,
|
|
|
|
display,
|
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,
|
2020-05-13 22:58:09 +02:00
|
|
|
get_collection_detail,
|
|
|
|
CollectionDetailError,
|
2017-08-19 02:21:11 +02:00
|
|
|
)
|
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from ..config import (
|
2017-08-19 02:21:11 +02:00
|
|
|
SanityConfig,
|
|
|
|
)
|
|
|
|
|
2020-04-26 03:55:39 +02:00
|
|
|
from ..ci import (
|
|
|
|
get_ci_provider,
|
|
|
|
)
|
|
|
|
|
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 ValidateModulesTest(SanitySingleVersion):
|
|
|
|
"""Sanity test using validate-modules."""
|
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 'A100'
|
|
|
|
|
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 target.module]
|
|
|
|
|
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
|
|
|
"""
|
|
|
|
env = ansible_environment(args, color=False)
|
|
|
|
|
2019-07-26 08:29:23 +02:00
|
|
|
settings = self.load_processor(args)
|
2019-07-23 22:31:54 +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
|
|
|
|
2020-05-13 22:58:09 +02:00
|
|
|
python = find_python(python_version)
|
|
|
|
|
2017-08-19 02:21:11 +02:00
|
|
|
cmd = [
|
2020-05-13 22:58:09 +02:00
|
|
|
python,
|
2019-08-06 02:40:00 +02:00
|
|
|
os.path.join(SANITY_ROOT, '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
|
|
|
|
|
2019-08-16 21:28:34 +02:00
|
|
|
if data_context().content.collection:
|
|
|
|
cmd.extend(['--collection', data_context().content.collection.directory])
|
2020-05-13 22:58:09 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
collection_detail = get_collection_detail(args, python)
|
|
|
|
|
|
|
|
if collection_detail.version:
|
|
|
|
cmd.extend(['--collection-version', collection_detail.version])
|
|
|
|
else:
|
|
|
|
display.warning('Skipping validate-modules collection version checks since no collection version was found.')
|
|
|
|
except CollectionDetailError as ex:
|
|
|
|
display.warning('Skipping validate-modules collection version checks since collection detail loading failed: %s' % ex.reason)
|
2017-08-19 02:21:11 +02:00
|
|
|
else:
|
2020-04-26 03:55:39 +02:00
|
|
|
base_branch = args.base_branch or get_ci_provider().get_base_branch()
|
|
|
|
|
|
|
|
if base_branch:
|
2019-08-28 18:10:17 +02:00
|
|
|
cmd.extend([
|
2020-04-26 03:55:39 +02:00
|
|
|
'--base-branch', base_branch,
|
2019-08-28 18:10:17 +02:00
|
|
|
])
|
|
|
|
else:
|
2020-04-26 03:55:39 +02:00
|
|
|
display.warning('Cannot perform module comparison against the base branch because the base branch was not detected.')
|
2017-08-19 02:21:11 +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 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',
|
2019-08-28 23:12:56 +02:00
|
|
|
code='%s' % item['code'],
|
2017-08-19 02:21:11 +02:00
|
|
|
message=item['msg'],
|
|
|
|
))
|
|
|
|
|
2019-07-23 22:31:54 +02:00
|
|
|
errors = settings.process_errors(errors, paths)
|
2018-01-11 22:36:08 +01:00
|
|
|
|
|
|
|
if errors:
|
|
|
|
return SanityFailure(self.name, messages=errors)
|
2017-08-19 02:21:11 +02:00
|
|
|
|
|
|
|
return SanitySuccess(self.name)
|