Relocate validate-modules for easier testing. (#60214)

* Relocate module validator code and tests.
* Fix validate-modules entry point and imports.
* Fix paths and test entry points.
* Fix up unit tests.
* Fix shebang and execute bit.
This commit is contained in:
Matt Clay 2019-08-07 09:34:11 -07:00 committed by GitHub
parent 164881d871
commit a93a0c8fd6
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 1904 additions and 1913 deletions

4
.github/BOTMETA.yml vendored
View file

@ -1469,11 +1469,11 @@ files:
- mattclay
keywords:
- validate-modules
test/lib/ansible_test/_data/sanity/validate-modules/schema.py:
test/lib/ansible_test/_data/sanity/validate-modules/validate_modules/schema.py:
notified:
- gundalow
- sivel
test/lib/ansible_test/_data/sanity/validate-modules/main.py:
test/lib/ansible_test/_data/sanity/validate-modules/validate_modules/main.py:
notified:
- gundalow
- sivel

View file

@ -52,7 +52,7 @@ Help
Extending validate-modules
==========================
The ``validate-modules`` tool has a `schema.py <https://github.com/ansible/ansible/blob/devel/test/lib/ansible_test/_data/sanity/validate-modules/schema.py>`_ that is used to validate the YAML blocks, such as ``DOCUMENTATION`` and ``RETURNS``.
The ``validate-modules`` tool has a `schema.py <https://github.com/ansible/ansible/blob/devel/test/lib/ansible_test/_data/sanity/validate-modules/validate_modules/schema.py>`_ that is used to validate the YAML blocks, such as ``DOCUMENTATION`` and ``RETURNS``.
Codes

File diff suppressed because it is too large Load diff

View file

@ -1,64 +0,0 @@
# This is a standalone test for the regex inside validate-modules
# It is not suitable to add to the make tests target because the
# file under test is outside the test's sys.path AND has a hyphen
# in the name making it unimportable.
#
# To execute this by hand:
# 1) cd <checkoutdir>
# 2) source hacking/env-setup
# 3) PYTHONPATH=./lib pytest -v sanity/validate-modules
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
import re
import unittest
# TYPE_REGEX = re.compile(r'.*\stype\(.*')
# TYPE_REGEX = re.compile(r'.*(if|or)\stype\(.*')
# TYPE_REGEX = re.compile(r'.*(if|or)(\s+.*|\s+)type\(.*')
# TYPE_REGEX = re.compile(r'.*(if|or)(\s+.*|\s+)type\(.*')
# TYPE_REGEX = re.compile(r'.*(if|\sor)(\s+.*|\s+)type\(.*')
# TYPE_REGEX = re.compile(r'.*(if|\sor)(\s+.*|\s+)(?<!_)type\(.*')
TYPE_REGEX = re.compile(r'.*(if|or)(\s+.*|\s+)(?<!_)(?<!str\()type\(.*')
class TestValidateModulesRegex(unittest.TestCase):
def test_type_regex(self):
# each of these examples needs to be matched or not matched
checks = [
['if type(foo) is Bar', True],
['if Bar is type(foo)', True],
['if type(foo) is not Bar', True],
['if Bar is not type(foo)', True],
['if type(foo) == Bar', True],
['if Bar == type(foo)', True],
['if type(foo)==Bar', True],
['if Bar==type(foo)', True],
['if type(foo) != Bar', True],
['if Bar != type(foo)', True],
['if type(foo)!=Bar', True],
['if Bar!=type(foo)', True],
['if foo or type(bar) != Bar', True],
['x = type(foo)', False],
["error = err.message + ' ' + str(err) + ' - ' + str(type(err))", False],
# cloud/amazon/ec2_group.py
["module.fail_json(msg='Invalid rule parameter type [%s].' % type(rule))", False],
# files/patch.py
["p = type('Params', (), module.params)", False], # files/patch.py
# system/osx_defaults.py
["if self.current_value is not None and not isinstance(self.current_value, type(self.value)):", True],
# system/osx_defaults.py
['raise OSXDefaultsException("Type mismatch. Type in defaults: " + type(self.current_value).__name__)', False],
# network/nxos/nxos_interface.py
["if get_interface_type(interface) == 'svi':", False],
]
for idc, check in enumerate(checks):
cstring = check[0]
cexpected = check[1]
match = TYPE_REGEX.match(cstring)
if cexpected and not match:
assert False, "%s should have matched" % cstring
elif not cexpected and match:
assert False, "%s should not have matched" % cstring

File diff suppressed because it is too large Load diff

View file

@ -28,7 +28,7 @@ from contextlib import contextmanager
from ansible.module_utils.six import reraise
from utils import find_executable
from .utils import find_executable
class AnsibleModuleCallError(RuntimeError):

View file

@ -126,6 +126,7 @@ class PylintTest(SanitySingleVersion):
add_context(remaining_paths, 'collection', lambda p: True)
else:
add_context(remaining_paths, 'validate-modules', filter_path('test/lib/ansible_test/_data/sanity/validate-modules/'))
add_context(remaining_paths, 'validate-modules-unit', filter_path('test/lib/ansible_test/tests/validate-modules-unit/'))
add_context(remaining_paths, 'sanity', filter_path('test/lib/ansible_test/_data/sanity/'))
add_context(remaining_paths, 'ansible-test', filter_path('test/lib/'))
add_context(remaining_paths, 'test', filter_path('test/'))

View file

@ -1,4 +1,4 @@
all: sanity unit
all: sanity unit validate-modules-unit
.PHONY: sanity
sanity:
@ -6,4 +6,8 @@ sanity:
.PHONY: unit
unit:
PYTHONPATH=../../lib/ pytest unit ${FLAGS}
PYTHONPATH=$(abspath ${CURDIR}/../..) pytest unit ${FLAGS}
.PHONY: validate-modules-unit
validate-modules-unit:
PYTHONPATH=$(abspath ${CURDIR}/../_data/sanity/validate-modules):$(abspath ${CURDIR}/../../../../lib) pytest validate-modules-unit ${FLAGS}

View file

@ -0,0 +1,43 @@
"""Tests for validate-modules regexes."""
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
import pytest
from validate_modules.main import TYPE_REGEX
@pytest.mark.parametrize('cstring,cexpected', [
['if type(foo) is Bar', True],
['if Bar is type(foo)', True],
['if type(foo) is not Bar', True],
['if Bar is not type(foo)', True],
['if type(foo) == Bar', True],
['if Bar == type(foo)', True],
['if type(foo)==Bar', True],
['if Bar==type(foo)', True],
['if type(foo) != Bar', True],
['if Bar != type(foo)', True],
['if type(foo)!=Bar', True],
['if Bar!=type(foo)', True],
['if foo or type(bar) != Bar', True],
['x = type(foo)', False],
["error = err.message + ' ' + str(err) + ' - ' + str(type(err))", False],
# cloud/amazon/ec2_group.py
["module.fail_json(msg='Invalid rule parameter type [%s].' % type(rule))", False],
# files/patch.py
["p = type('Params', (), module.params)", False], # files/patch.py
# system/osx_defaults.py
["if self.current_value is not None and not isinstance(self.current_value, type(self.value)):", True],
# system/osx_defaults.py
['raise OSXDefaultsException("Type mismatch. Type in defaults: " + type(self.current_value).__name__)', False],
# network/nxos/nxos_interface.py
["if get_interface_type(interface) == 'svi':", False],
])
def test_type_regex(cstring, cexpected): # type: (str, str) -> None
"""Check TYPE_REGEX against various examples to verify it correctly matches or does not match."""
match = TYPE_REGEX.match(cstring)
if cexpected and not match:
assert False, "%s should have matched" % cstring
elif not cexpected and match:
assert False, "%s should not have matched" % cstring