| 1 |
"""Sanity test using yamllint.""" |
| 2 |
|
| 3 |
from __future__ import annotations |
| 4 |
|
| 5 |
import json |
| 6 |
import os |
| 7 |
import typing as t |
| 8 |
|
| 9 |
from . import ( |
| 10 |
SanitySingleVersion, |
| 11 |
SanityMessage, |
| 12 |
SanityFailure, |
| 13 |
SanitySuccess, |
| 14 |
SanityTargets, |
| 15 |
SANITY_ROOT, |
| 16 |
) |
| 17 |
|
| 18 |
from ...test import ( |
| 19 |
TestResult, |
| 20 |
) |
| 21 |
|
| 22 |
from ...target import ( |
| 23 |
TestTarget, |
| 24 |
) |
| 25 |
|
| 26 |
from ...util import ( |
| 27 |
SubprocessError, |
| 28 |
display, |
| 29 |
is_subdir, |
| 30 |
) |
| 31 |
|
| 32 |
from ...util_common import ( |
| 33 |
run_command, |
| 34 |
) |
| 35 |
|
| 36 |
from ...config import ( |
| 37 |
SanityConfig, |
| 38 |
) |
| 39 |
|
| 40 |
from ...data import ( |
| 41 |
data_context, |
| 42 |
) |
| 43 |
|
| 44 |
from ...host_configs import ( |
| 45 |
PythonConfig, |
| 46 |
) |
| 47 |
|
| 48 |
|
| 49 |
class YamllintTest(SanitySingleVersion): |
| 50 |
"""Sanity test using yamllint.""" |
| 51 |
|
| 52 |
@property |
| 53 |
def error_code(self) -> t.Optional[str]: |
| 54 |
"""Error code for ansible-test matching the format used by the underlying test program, or None if the program does not use error codes.""" |
| 55 |
return 'ansible-test' |
| 56 |
|
| 57 |
@property |
| 58 |
def require_libyaml(self) -> bool: |
| 59 |
"""True if the test requires PyYAML to have libyaml support.""" |
| 60 |
return True |
| 61 |
|
| 62 |
def filter_targets(self, targets: list[TestTarget]) -> list[TestTarget]: |
| 63 |
"""Return the given list of test targets, filtered to include only those relevant for the test.""" |
| 64 |
yaml_targets = [target for target in targets if os.path.splitext(target.path)[1] in ('.yml', '.yaml')] |
| 65 |
|
| 66 |
for plugin_type, plugin_path in sorted(data_context().content.plugin_paths.items()): |
| 67 |
if plugin_type == 'module_utils': |
| 68 |
continue |
| 69 |
|
| 70 |
yaml_targets.extend([target for target in targets if |
| 71 |
os.path.splitext(target.path)[1] == '.py' and |
| 72 |
os.path.basename(target.path) != '__init__.py' and |
| 73 |
is_subdir(target.path, plugin_path)]) |
| 74 |
|
| 75 |
return yaml_targets |
| 76 |
|
| 77 |
def test(self, args: SanityConfig, targets: SanityTargets, python: PythonConfig) -> TestResult: |
| 78 |
settings = self.load_processor(args) |
| 79 |
|
| 80 |
paths = [target.path for target in targets.include] |
| 81 |
|
| 82 |
results = self.test_paths(args, paths, python) |
| 83 |
results = settings.process_errors(results, paths) |
| 84 |
|
| 85 |
if results: |
| 86 |
return SanityFailure(self.name, messages=results) |
| 87 |
|
| 88 |
return SanitySuccess(self.name) |
| 89 |
|
| 90 |
@staticmethod |
| 91 |
def test_paths(args: SanityConfig, paths: list[str], python: PythonConfig) -> list[SanityMessage]: |
| 92 |
"""Test the specified paths using the given Python and return the results.""" |
| 93 |
cmd = [ |
| 94 |
python.path, |
| 95 |
os.path.join(SANITY_ROOT, 'yamllint', 'yamllinter.py'), |
| 96 |
] |
| 97 |
|
| 98 |
data = '\n'.join(paths) |
| 99 |
|
| 100 |
display.info(data, verbosity=4) |
| 101 |
|
| 102 |
try: |
| 103 |
stdout, stderr = run_command(args, cmd, data=data, capture=True) |
| 104 |
status = 0 |
| 105 |
except SubprocessError as ex: |
| 106 |
stdout = ex.stdout |
| 107 |
stderr = ex.stderr |
| 108 |
status = ex.status |
| 109 |
|
| 110 |
if stderr: |
| 111 |
raise SubprocessError(cmd=cmd, status=status, stderr=stderr, stdout=stdout) |
| 112 |
|
| 113 |
if args.explain: |
| 114 |
return [] |
| 115 |
|
| 116 |
results = json.loads(stdout)['messages'] |
| 117 |
|
| 118 |
results = [SanityMessage( |
| 119 |
code=r['code'], |
| 120 |
message=r['message'], |
| 121 |
path=r['path'], |
| 122 |
line=int(r['line']), |
| 123 |
column=int(r['column']), |
| 124 |
level=r['level'], |
| 125 |
) for r in results] |
| 126 |
|
| 127 |
return results |