summaryrefslogtreecommitdiffstats
path: root/test/ansible_test
diff options
context:
space:
mode:
Diffstat (limited to 'test/ansible_test')
-rw-r--r--test/ansible_test/Makefile13
-rw-r--r--test/ansible_test/unit/test_diff.py105
-rw-r--r--test/ansible_test/validate-modules-unit/test_validate_modules_regex.py43
3 files changed, 161 insertions, 0 deletions
diff --git a/test/ansible_test/Makefile b/test/ansible_test/Makefile
new file mode 100644
index 0000000..2d85e3d
--- /dev/null
+++ b/test/ansible_test/Makefile
@@ -0,0 +1,13 @@
+all: sanity unit validate-modules-unit
+
+.PHONY: sanity
+sanity:
+ $(abspath ${CURDIR}/../../bin/ansible-test) sanity test/lib/ ${FLAGS}
+
+.PHONY: unit
+unit:
+ PYTHONPATH=$(abspath ${CURDIR}/../lib) pytest unit ${FLAGS}
+
+.PHONY: validate-modules-unit
+validate-modules-unit:
+ PYTHONPATH=$(abspath ${CURDIR}/../lib/ansible_test/_util/controller/sanity/validate-modules):$(abspath ${CURDIR}/../../lib) pytest validate-modules-unit ${FLAGS}
diff --git a/test/ansible_test/unit/test_diff.py b/test/ansible_test/unit/test_diff.py
new file mode 100644
index 0000000..1f2559d
--- /dev/null
+++ b/test/ansible_test/unit/test_diff.py
@@ -0,0 +1,105 @@
+"""Tests for diff module."""
+from __future__ import (absolute_import, division, print_function)
+__metaclass__ = type
+
+import os
+import subprocess
+import pytest
+
+from ansible_test._internal.util import (
+ to_text,
+ to_bytes,
+)
+
+from ansible_test._internal.diff import (
+ parse_diff,
+ FileDiff,
+)
+
+
+def get_diff(base, head=None):
+ """Return a git diff between the base and head revision.
+ :type base: str
+ :type head: str | None
+ :rtype: list[str]
+ """
+ if not head or head == 'HEAD':
+ head = to_text(subprocess.check_output(['git', 'rev-parse', 'HEAD'])).strip()
+
+ cache = '/tmp/git-diff-cache-%s-%s.log' % (base, head)
+
+ if os.path.exists(cache):
+ with open(cache, 'rb') as cache_fd:
+ lines = to_text(cache_fd.read()).splitlines()
+ else:
+ lines = to_text(subprocess.check_output(['git', 'diff', base, head]), errors='replace').splitlines()
+
+ with open(cache, 'wb') as cache_fd:
+ cache_fd.write(to_bytes('\n'.join(lines)))
+
+ assert lines
+
+ return lines
+
+
+def get_parsed_diff(base, head=None):
+ """Return a parsed git diff between the base and head revision.
+ :type base: str
+ :type head: str | None
+ :rtype: list[FileDiff]
+ """
+ lines = get_diff(base, head)
+ items = parse_diff(lines)
+
+ assert items
+
+ for item in items:
+ assert item.headers
+ assert item.is_complete
+
+ item.old.format_lines()
+ item.new.format_lines()
+
+ for line_range in item.old.ranges:
+ assert line_range[1] >= line_range[0] > 0
+
+ for line_range in item.new.ranges:
+ assert line_range[1] >= line_range[0] > 0
+
+ return items
+
+
+RANGES_TO_TEST = (
+ ('f31421576b00f0b167cdbe61217c31c21a41ac02', 'HEAD'),
+ ('b8125ac1a61f2c7d1de821c78c884560071895f1', '32146acf4e43e6f95f54d9179bf01f0df9814217')
+)
+
+
+@pytest.mark.parametrize("base, head", RANGES_TO_TEST)
+def test_parse_diff(base, head):
+ """Integration test to verify parsing of ansible/ansible history."""
+ get_parsed_diff(base, head)
+
+
+def test_parse_delete():
+ """Integration test to verify parsing of a deleted file."""
+ commit = 'ee17b914554861470b382e9e80a8e934063e0860'
+ items = get_parsed_diff(commit + '~', commit)
+ deletes = [item for item in items if not item.new.exists]
+
+ assert len(deletes) == 1
+ assert deletes[0].old.path == 'lib/ansible/plugins/connection/nspawn.py'
+ assert deletes[0].new.path == 'lib/ansible/plugins/connection/nspawn.py'
+
+
+def test_parse_rename():
+ """Integration test to verify parsing of renamed files."""
+ commit = '16a39639f568f4dd5cb233df2d0631bdab3a05e9'
+ items = get_parsed_diff(commit + '~', commit)
+ renames = [item for item in items if item.old.path != item.new.path and item.old.exists and item.new.exists]
+
+ assert len(renames) == 2
+ assert renames[0].old.path == 'test/integration/targets/eos_eapi/tests/cli/badtransport.yaml'
+ assert renames[0].new.path == 'test/integration/targets/eos_eapi/tests/cli/badtransport.1'
+ assert renames[1].old.path == 'test/integration/targets/eos_eapi/tests/cli/zzz_reset.yaml'
+ assert renames[1].new.path == 'test/integration/targets/eos_eapi/tests/cli/zzz_reset.1'
diff --git a/test/ansible_test/validate-modules-unit/test_validate_modules_regex.py b/test/ansible_test/validate-modules-unit/test_validate_modules_regex.py
new file mode 100644
index 0000000..8c0b45c
--- /dev/null
+++ b/test/ansible_test/validate-modules-unit/test_validate_modules_regex.py
@@ -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