From a103f81513c2fa631c41e7a8d095cd9cc0873d3d Mon Sep 17 00:00:00 2001 From: Matt Martz Date: Tue, 1 Mar 2016 14:20:57 -0600 Subject: [PATCH] Validate DOCUMENTATION schema --- ansible_testing/modules.py | 24 +++++++++++++++++++++++- ansible_testing/schema.py | 27 +++++++++++++++++++++++++++ 2 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 ansible_testing/schema.py diff --git a/ansible_testing/modules.py b/ansible_testing/modules.py index ee9a83f1aca..ecece1ef131 100644 --- a/ansible_testing/modules.py +++ b/ansible_testing/modules.py @@ -18,6 +18,8 @@ from ansible.module_utils import basic as module_utils_basic from ansible.plugins import module_loader from ansible.utils.module_docs import BLACKLIST_MODULES, get_docstring +from schema import doc_schema, option_schema + from utils import CaptureStd, find_globals import yaml @@ -345,6 +347,26 @@ class ModuleValidator(Validator): return docs + def _validate_docs_schema(self, doc): + errors = [] + try: + doc_schema(doc) + except Exception as e: + errors.extend(e.errors) + + for key, option in doc.get('options', {}).iteritems(): + try: + option_schema(option) + except Exception as e: + for error in e.errors: + error.path[:0] = ['options', key] + errors.extend(e.errors) + + for error in errors: + path = [str(p) for p in error.path] + self.errors.append('DOCUMENTATION.%s: %s' % + ('.'.join(path), error.error_message)) + def _validate_docs(self): doc_info = self._get_docs() try: @@ -378,6 +400,7 @@ class ModuleValidator(Validator): self.errors.append('Unknown DOCUMENTATION error, see ' 'TRACE') + self._validate_docs_schema(doc) self._check_version_added(doc) self._check_for_new_args(doc) @@ -451,7 +474,6 @@ class ModuleValidator(Validator): 'TRACE') return - try: mod_version_added = StrictVersion( str(existing_doc.get('version_added', '0.0')) diff --git a/ansible_testing/schema.py b/ansible_testing/schema.py new file mode 100644 index 00000000000..76d0df7fdfa --- /dev/null +++ b/ansible_testing/schema.py @@ -0,0 +1,27 @@ +from voluptuous import ALLOW_EXTRA, Any, Required, Schema + +option_schema = Schema( + { + Required('description'): Any(basestring, [basestring]), + 'required': bool, + 'choices': Any(list, basestring), + 'aliases': list, + 'version_added': Any(basestring, float) + }, + extra=ALLOW_EXTRA +) + +doc_schema = Schema( + { + Required('module'): basestring, + 'short_description': Any(basestring, [basestring]), + 'description': Any(basestring, [basestring]), + 'version_added': Any(basestring, float), + 'author': Any(None, basestring, [basestring]), + 'notes': Any(None, [basestring]), + 'requirements': [basestring], + 'options': Any(None, dict), + 'extends_documentation_fragment': Any(basestring, [basestring]) + }, + extra=ALLOW_EXTRA +)