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,
|
|
|
|
)
|
|
|
|
|
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
|
2020-06-25 00:23:55 +02:00
|
|
|
unsupported_plugin_types = set([
|
2018-05-23 15:48:22 +02:00
|
|
|
# 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
|
|
|
'terminal',
|
|
|
|
'test',
|
2021-02-13 23:05:16 +01:00
|
|
|
# The following are plugin directories not directly supported by ansible-core (and thus also not by ansible-doc)
|
|
|
|
# (https://github.com/ansible-collections/overview/blob/main/collection_requirements.rst#modules--plugins)
|
|
|
|
'plugin_utils',
|
|
|
|
'sub_plugins',
|
2018-05-23 15:48:22 +02:00
|
|
|
])
|
|
|
|
|
2020-06-25 00:23:55 +02:00
|
|
|
plugin_paths = [plugin_path for plugin_type, plugin_path in data_context().content.plugin_paths.items() if plugin_type not in unsupported_plugin_types]
|
2019-07-30 20:10:55 +02:00
|
|
|
|
|
|
|
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):
|
2020-05-29 17:52:29 +02:00
|
|
|
for format_option in [None, '--json']:
|
|
|
|
cmd = ['ansible-doc', '-t', doc_type]
|
|
|
|
if format_option is not None:
|
|
|
|
cmd.append(format_option)
|
|
|
|
cmd.extend(sorted(doc_targets[doc_type]))
|
|
|
|
|
|
|
|
try:
|
|
|
|
with coverage_context(args):
|
|
|
|
stdout, stderr = intercept_command(args, cmd, target_name='ansible-doc', env=env, capture=True, python_version=python_version)
|
|
|
|
|
|
|
|
status = 0
|
|
|
|
except SubprocessError as ex:
|
|
|
|
stdout = ex.stdout
|
|
|
|
stderr = ex.stderr
|
|
|
|
status = ex.status
|
|
|
|
|
|
|
|
if status:
|
|
|
|
summary = u'%s' % SubprocessError(cmd=cmd, status=status, stderr=stderr)
|
|
|
|
return SanityFailure(self.name, summary=summary)
|
|
|
|
|
|
|
|
if stdout:
|
|
|
|
display.info(stdout.strip(), verbosity=3)
|
|
|
|
|
|
|
|
if stderr:
|
|
|
|
# ignore removed module/plugin warnings
|
2021-01-26 23:02:08 +01:00
|
|
|
stderr = re.sub(r'\[WARNING]: [^ ]+ [^ ]+ has been removed\n', '', stderr).strip()
|
2020-05-29 17:52:29 +02:00
|
|
|
|
|
|
|
if stderr:
|
|
|
|
summary = u'Output on stderr from ansible-doc is considered an error.\n\n%s' % SubprocessError(cmd, stderr=stderr)
|
|
|
|
return SanityFailure(self.name, summary=summary)
|
2019-07-23 22:31:54 +02:00
|
|
|
|
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)
|