2017-08-19 02:21:11 +02:00
|
|
|
"""Sanity test for ansible-doc."""
|
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-05-23 15:48:22 +02:00
|
|
|
import collections
|
|
|
|
import os
|
2018-01-17 19:53:03 +01:00
|
|
|
import re
|
|
|
|
|
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 (
|
2019-07-23 22:31:54 +02:00
|
|
|
SanitySingleVersion,
|
2017-08-19 02:21:11 +02:00
|
|
|
SanityFailure,
|
|
|
|
SanitySuccess,
|
2018-01-17 19:53:03 +01:00
|
|
|
SanityMessage,
|
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,
|
|
|
|
display,
|
2019-07-30 20:10:55 +02:00
|
|
|
is_subdir,
|
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,
|
|
|
|
)
|
|
|
|
|
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,
|
|
|
|
)
|
|
|
|
|
2019-08-06 23:43:29 +02:00
|
|
|
from ..coverage_util import (
|
2019-07-23 04:24:48 +02:00
|
|
|
coverage_context,
|
|
|
|
)
|
|
|
|
|
2017-08-19 02:21:11 +02:00
|
|
|
|
2019-07-23 22:31:54 +02:00
|
|
|
class AnsibleDocTest(SanitySingleVersion):
|
2017-08-19 02:21:11 +02:00
|
|
|
"""Sanity test for ansible-doc."""
|
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."""
|
2019-01-24 02:03:47 +01:00
|
|
|
# This should use documentable plugins from constants instead
|
2018-05-23 15:48:22 +02:00
|
|
|
plugin_type_blacklist = set([
|
|
|
|
# not supported by ansible-doc
|
|
|
|
'action',
|
2019-01-24 02:03:47 +01:00
|
|
|
'doc_fragments',
|
2018-05-23 15:48:22 +02:00
|
|
|
'filter',
|
2019-07-30 20:10:55 +02:00
|
|
|
'module_utils',
|
2018-05-23 15:48:22 +02:00
|
|
|
'netconf',
|
|
|
|
'terminal',
|
|
|
|
'test',
|
|
|
|
])
|
|
|
|
|
2019-07-30 20:10:55 +02:00
|
|
|
plugin_paths = [plugin_path for plugin_type, plugin_path in data_context().content.plugin_paths.items() if plugin_type not in plugin_type_blacklist]
|
|
|
|
|
|
|
|
return [target for target in targets
|
|
|
|
if os.path.splitext(target.path)[1] == '.py'
|
|
|
|
and os.path.basename(target.path) != '__init__.py'
|
|
|
|
and any(is_subdir(target.path, path) for path in plugin_paths)
|
|
|
|
]
|
|
|
|
|
|
|
|
def test(self, args, targets, python_version):
|
|
|
|
"""
|
|
|
|
:type args: SanityConfig
|
|
|
|
:type targets: SanityTargets
|
|
|
|
:type python_version: str
|
|
|
|
:rtype: TestResult
|
|
|
|
"""
|
|
|
|
settings = self.load_processor(args)
|
2017-08-19 02:21:11 +02:00
|
|
|
|
2019-07-30 20:10:55 +02:00
|
|
|
paths = [target.path for target in targets.include]
|
2018-05-23 15:48:22 +02:00
|
|
|
|
|
|
|
doc_targets = collections.defaultdict(list)
|
|
|
|
target_paths = collections.defaultdict(dict)
|
|
|
|
|
2019-07-30 20:10:55 +02:00
|
|
|
remap_types = dict(
|
|
|
|
modules='module',
|
|
|
|
)
|
2018-05-23 15:48:22 +02:00
|
|
|
|
2019-07-30 20:10:55 +02:00
|
|
|
for plugin_type, plugin_path in data_context().content.plugin_paths.items():
|
|
|
|
plugin_type = remap_types.get(plugin_type, plugin_type)
|
2018-05-23 15:48:22 +02:00
|
|
|
|
2019-07-30 20:10:55 +02:00
|
|
|
for plugin_file_path in [target.name for target in targets.include if is_subdir(target.path, plugin_path)]:
|
|
|
|
plugin_name = os.path.splitext(os.path.basename(plugin_file_path))[0]
|
2018-05-23 15:48:22 +02:00
|
|
|
|
2019-07-30 20:10:55 +02:00
|
|
|
if plugin_name.startswith('_'):
|
|
|
|
plugin_name = plugin_name[1:]
|
2017-11-21 02:01:09 +01:00
|
|
|
|
2019-07-30 20:10:55 +02:00
|
|
|
doc_targets[plugin_type].append(data_context().content.prefix + plugin_name)
|
|
|
|
target_paths[plugin_type][data_context().content.prefix + plugin_name] = plugin_file_path
|
2017-08-19 02:21:11 +02:00
|
|
|
|
|
|
|
env = ansible_environment(args, color=False)
|
2018-05-23 15:48:22 +02:00
|
|
|
error_messages = []
|
|
|
|
|
|
|
|
for doc_type in sorted(doc_targets):
|
|
|
|
cmd = ['ansible-doc', '-t', doc_type] + sorted(doc_targets[doc_type])
|
|
|
|
|
|
|
|
try:
|
2019-07-23 04:24:48 +02:00
|
|
|
with coverage_context(args):
|
2019-07-27 01:46:52 +02:00
|
|
|
stdout, stderr = intercept_command(args, cmd, target_name='ansible-doc', env=env, capture=True, python_version=python_version)
|
2019-07-23 04:24:48 +02:00
|
|
|
|
2018-05-23 15:48:22 +02:00
|
|
|
status = 0
|
|
|
|
except SubprocessError as ex:
|
|
|
|
stdout = ex.stdout
|
|
|
|
stderr = ex.stderr
|
|
|
|
status = ex.status
|
2017-08-19 02:21:11 +02:00
|
|
|
|
2018-05-23 15:48:22 +02:00
|
|
|
if status:
|
|
|
|
summary = u'%s' % SubprocessError(cmd=cmd, status=status, stderr=stderr)
|
2019-07-23 22:31:54 +02:00
|
|
|
return SanityFailure(self.name, summary=summary)
|
2018-01-17 19:53:03 +01:00
|
|
|
|
2018-05-23 15:48:22 +02:00
|
|
|
if stdout:
|
|
|
|
display.info(stdout.strip(), verbosity=3)
|
2017-08-19 02:21:11 +02:00
|
|
|
|
2019-09-10 09:29:34 +02:00
|
|
|
if stderr:
|
|
|
|
# ignore removed module/plugin warnings
|
|
|
|
stderr = re.sub(r'\[WARNING\]: [^ ]+ [^ ]+ has been removed\n', '', stderr).strip()
|
|
|
|
|
2018-05-23 15:48:22 +02:00
|
|
|
if stderr:
|
|
|
|
summary = u'Output on stderr from ansible-doc is considered an error.\n\n%s' % SubprocessError(cmd, stderr=stderr)
|
2019-07-23 22:31:54 +02:00
|
|
|
return SanityFailure(self.name, summary=summary)
|
|
|
|
|
2019-08-26 23:02:55 +02:00
|
|
|
if args.explain:
|
|
|
|
return SanitySuccess(self.name)
|
|
|
|
|
2019-07-23 22:31:54 +02:00
|
|
|
error_messages = settings.process_errors(error_messages, paths)
|
2017-08-19 02:21:11 +02:00
|
|
|
|
2018-05-23 15:48:22 +02:00
|
|
|
if error_messages:
|
2019-07-23 22:31:54 +02:00
|
|
|
return SanityFailure(self.name, messages=error_messages)
|
2017-08-19 02:21:11 +02:00
|
|
|
|
2019-07-23 22:31:54 +02:00
|
|
|
return SanitySuccess(self.name)
|