diff --git a/.github/workflows/ansible-test.yml b/.github/workflows/ansible-test.yml index b3c551e..e062a88 100644 --- a/.github/workflows/ansible-test.yml +++ b/.github/workflows/ansible-test.yml @@ -50,6 +50,7 @@ jobs: pre-test-cmd: |- git clone --depth=1 --single-branch https://github.com/ansible-collections/ansible.netcommon.git ../../ansible/netcommon git clone --depth=1 --single-branch https://github.com/ansible-collections/ansible.utils.git ../../ansible/utils + git clone --depth=1 --single-branch https://github.com/ansible-collections/community.internal_test_tools.git ../../community/internal_test_tools units: runs-on: ubuntu-latest @@ -85,6 +86,7 @@ jobs: pre-test-cmd: |- git clone --depth=1 --single-branch https://github.com/ansible-collections/ansible.netcommon.git ../../ansible/netcommon git clone --depth=1 --single-branch https://github.com/ansible-collections/ansible.utils.git ../../ansible/utils + git clone --depth=1 --single-branch https://github.com/ansible-collections/community.internal_test_tools.git ../../community/internal_test_tools integration: runs-on: ubuntu-latest @@ -151,5 +153,6 @@ jobs: pre-test-cmd: |- git clone --depth=1 --single-branch https://github.com/ansible-collections/ansible.netcommon.git ../../ansible/netcommon git clone --depth=1 --single-branch https://github.com/ansible-collections/ansible.utils.git ../../ansible/utils + git clone --depth=1 --single-branch https://github.com/ansible-collections/community.internal_test_tools.git ../../community/internal_test_tools target-python-version: ${{ matrix.python }} testing-type: integration diff --git a/tests/sanity/ignore-2.17.txt b/tests/sanity/ignore-2.17.txt index 0a5234b..ce635c3 100644 --- a/tests/sanity/ignore-2.17.txt +++ b/tests/sanity/ignore-2.17.txt @@ -1,2 +1 @@ update-docs.py shebang -tests/unit/compat/mock.py pylint:use-yield-from # suggested construct does not work with Python 2 diff --git a/tests/sanity/ignore-2.18.txt b/tests/sanity/ignore-2.18.txt index 0a5234b..ce635c3 100644 --- a/tests/sanity/ignore-2.18.txt +++ b/tests/sanity/ignore-2.18.txt @@ -1,2 +1 @@ update-docs.py shebang -tests/unit/compat/mock.py pylint:use-yield-from # suggested construct does not work with Python 2 diff --git a/tests/sanity/ignore-2.19.txt b/tests/sanity/ignore-2.19.txt index 0a5234b..ce635c3 100644 --- a/tests/sanity/ignore-2.19.txt +++ b/tests/sanity/ignore-2.19.txt @@ -1,2 +1 @@ update-docs.py shebang -tests/unit/compat/mock.py pylint:use-yield-from # suggested construct does not work with Python 2 diff --git a/tests/unit/compat/__init__.py b/tests/unit/compat/__init__.py deleted file mode 100644 index e69de29..0000000 diff --git a/tests/unit/compat/builtins.py b/tests/unit/compat/builtins.py deleted file mode 100644 index d548601..0000000 --- a/tests/unit/compat/builtins.py +++ /dev/null @@ -1,20 +0,0 @@ -# Copyright (c) 2014, Toshio Kuratomi -# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) -# SPDX-License-Identifier: GPL-3.0-or-later - -# Make coding more python3-ish -from __future__ import (absolute_import, division, print_function) -__metaclass__ = type - -# -# Compat for python2.7 -# - -# One unittest needs to import builtins via __import__() so we need to have -# the string that represents it -try: - import __builtin__ # noqa: F401, pylint: disable=unused-import -except ImportError: - BUILTINS = 'builtins' -else: - BUILTINS = '__builtin__' diff --git a/tests/unit/compat/mock.py b/tests/unit/compat/mock.py deleted file mode 100644 index bdbea94..0000000 --- a/tests/unit/compat/mock.py +++ /dev/null @@ -1,109 +0,0 @@ -# Copyright (c) 2014, Toshio Kuratomi -# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) -# SPDX-License-Identifier: GPL-3.0-or-later - -# Make coding more python3-ish -from __future__ import (absolute_import, division, print_function) -__metaclass__ = type - -''' -Compat module for Python3.x's unittest.mock module -''' -import sys - -# Python 2.7 - -# Note: Could use the pypi mock library on python3.x as well as python2.x. It -# is the same as the python3 stdlib mock library - -try: - # Allow wildcard import because we really do want to import all of mock's - # symbols into this compat shim - # pylint: disable=wildcard-import,unused-wildcard-import - from unittest.mock import * # noqa: F401, pylint: disable=unused-import -except ImportError: - # Python 2 - # pylint: disable=wildcard-import,unused-wildcard-import - try: - from mock import * # noqa: F401, pylint: disable=unused-import - except ImportError: - print('You need the mock library installed on python2.x to run tests') - - -# Prior to 3.4.4, mock_open cannot handle binary read_data -if sys.version_info >= (3,) and sys.version_info < (3, 4, 4): - file_spec = None - - def _iterate_read_data(read_data): - # Helper for mock_open: - # Retrieve lines from read_data via a generator so that separate calls to - # readline, read, and readlines are properly interleaved - sep = b'\n' if isinstance(read_data, bytes) else '\n' - data_as_list = [l + sep for l in read_data.split(sep)] - - if data_as_list[-1] == sep: - # If the last line ended in a newline, the list comprehension will have an - # extra entry that's just a newline. Remove this. - data_as_list = data_as_list[:-1] - else: - # If there wasn't an extra newline by itself, then the file being - # emulated doesn't have a newline to end the last line remove the - # newline that our naive format() added - data_as_list[-1] = data_as_list[-1][:-1] - - for line in data_as_list: - yield line - - def mock_open(mock=None, read_data=''): - """ - A helper function to create a mock to replace the use of `open`. It works - for `open` called directly or used as a context manager. - - The `mock` argument is the mock object to configure. If `None` (the - default) then a `MagicMock` will be created for you, with the API limited - to methods or attributes available on standard file handles. - - `read_data` is a string for the `read` methoddline`, and `readlines` of the - file handle to return. This is an empty string by default. - """ - def _readlines_side_effect(*args, **kwargs): - if handle.readlines.return_value is not None: - return handle.readlines.return_value - return list(_data) - - def _read_side_effect(*args, **kwargs): - if handle.read.return_value is not None: - return handle.read.return_value - return type(read_data)().join(_data) - - def _readline_side_effect(): - if handle.readline.return_value is not None: - while True: - yield handle.readline.return_value - for line in _data: - yield line - - global file_spec - if file_spec is None: - import _io - file_spec = list(set(dir(_io.TextIOWrapper)).union(set(dir(_io.BytesIO)))) - - if mock is None: - mock = MagicMock(name='open', spec=open) - - handle = MagicMock(spec=file_spec) - handle.__enter__.return_value = handle - - _data = _iterate_read_data(read_data) - - handle.write.return_value = None - handle.read.return_value = None - handle.readline.return_value = None - handle.readlines.return_value = None - - handle.read.side_effect = _read_side_effect - handle.readline.side_effect = _readline_side_effect() - handle.readlines.side_effect = _readlines_side_effect - - mock.return_value = handle - return mock diff --git a/tests/unit/compat/unittest.py b/tests/unit/compat/unittest.py deleted file mode 100644 index d50bab8..0000000 --- a/tests/unit/compat/unittest.py +++ /dev/null @@ -1,25 +0,0 @@ -# Copyright (c) 2014, Toshio Kuratomi -# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) -# SPDX-License-Identifier: GPL-3.0-or-later - -# Make coding more python3-ish -from __future__ import (absolute_import, division, print_function) -__metaclass__ = type - -''' -Compat module for Python2.7's unittest module -''' - -import sys - -# Allow wildcard import because we really do want to import all of -# unittests's symbols into this compat shim -# pylint: disable=wildcard-import,unused-wildcard-import -if sys.version_info < (2, 7): - try: - # Need unittest2 on python2.6 - from unittest2 import * # noqa: F401, pylint: disable=unused-import - except ImportError: - print('You need unittest2 installed on python2.6.x to run tests') -else: - from unittest import * # noqa: F401, pylint: disable=unused-import diff --git a/tests/unit/plugins/modules/routeros_module.py b/tests/unit/plugins/modules/routeros_module.py index 0ec44f7..4786da6 100644 --- a/tests/unit/plugins/modules/routeros_module.py +++ b/tests/unit/plugins/modules/routeros_module.py @@ -9,7 +9,7 @@ __metaclass__ = type import os import json -from ansible_collections.community.routeros.tests.unit.plugins.modules.utils import AnsibleExitJson, AnsibleFailJson, ModuleTestCase +from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import AnsibleExitJson, AnsibleFailJson, ModuleTestCase fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures') diff --git a/tests/unit/plugins/modules/test_api.py b/tests/unit/plugins/modules/test_api.py index 5df8430..9a03825 100644 --- a/tests/unit/plugins/modules/test_api.py +++ b/tests/unit/plugins/modules/test_api.py @@ -6,9 +6,10 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -from ansible_collections.community.routeros.tests.unit.compat.mock import patch, MagicMock +from ansible_collections.community.internal_test_tools.tests.unit.compat.mock import patch, MagicMock +from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import set_module_args, AnsibleExitJson, AnsibleFailJson, ModuleTestCase + from ansible_collections.community.routeros.tests.unit.plugins.modules.fake_api import FakeLibRouterosError, Key, Or, fake_ros_api -from ansible_collections.community.routeros.tests.unit.plugins.modules.utils import set_module_args, AnsibleExitJson, AnsibleFailJson, ModuleTestCase from ansible_collections.community.routeros.plugins.modules import api diff --git a/tests/unit/plugins/modules/test_api_facts.py b/tests/unit/plugins/modules/test_api_facts.py index f9c76be..7b019f9 100644 --- a/tests/unit/plugins/modules/test_api_facts.py +++ b/tests/unit/plugins/modules/test_api_facts.py @@ -6,9 +6,10 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -from ansible_collections.community.routeros.tests.unit.compat.mock import patch, MagicMock +from ansible_collections.community.internal_test_tools.tests.unit.compat.mock import patch, MagicMock +from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import set_module_args, AnsibleExitJson, AnsibleFailJson, ModuleTestCase + from ansible_collections.community.routeros.tests.unit.plugins.modules.fake_api import FakeLibRouterosError, Key, fake_ros_api -from ansible_collections.community.routeros.tests.unit.plugins.modules.utils import set_module_args, AnsibleExitJson, AnsibleFailJson, ModuleTestCase from ansible_collections.community.routeros.plugins.modules import api_facts diff --git a/tests/unit/plugins/modules/test_api_find_and_modify.py b/tests/unit/plugins/modules/test_api_find_and_modify.py index 2405d3a..fe93905 100644 --- a/tests/unit/plugins/modules/test_api_find_and_modify.py +++ b/tests/unit/plugins/modules/test_api_find_and_modify.py @@ -6,11 +6,12 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -from ansible_collections.community.routeros.tests.unit.compat.mock import patch, MagicMock +from ansible_collections.community.internal_test_tools.tests.unit.compat.mock import patch, MagicMock +from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import set_module_args, AnsibleExitJson, AnsibleFailJson, ModuleTestCase + from ansible_collections.community.routeros.tests.unit.plugins.modules.fake_api import ( FakeLibRouterosError, fake_ros_api, massage_expected_result_data, create_fake_path, ) -from ansible_collections.community.routeros.tests.unit.plugins.modules.utils import set_module_args, AnsibleExitJson, AnsibleFailJson, ModuleTestCase from ansible_collections.community.routeros.plugins.modules import api_find_and_modify diff --git a/tests/unit/plugins/modules/test_api_info.py b/tests/unit/plugins/modules/test_api_info.py index d20aa11..967a854 100644 --- a/tests/unit/plugins/modules/test_api_info.py +++ b/tests/unit/plugins/modules/test_api_info.py @@ -6,11 +6,12 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -from ansible_collections.community.routeros.tests.unit.compat.mock import patch, MagicMock +from ansible_collections.community.internal_test_tools.tests.unit.compat.mock import patch, MagicMock +from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import set_module_args, AnsibleExitJson, AnsibleFailJson, ModuleTestCase + from ansible_collections.community.routeros.tests.unit.plugins.modules.fake_api import ( FAKE_ROS_VERSION, FakeLibRouterosError, Key, fake_ros_api, ) -from ansible_collections.community.routeros.tests.unit.plugins.modules.utils import set_module_args, AnsibleExitJson, AnsibleFailJson, ModuleTestCase from ansible_collections.community.routeros.plugins.modules import api_info diff --git a/tests/unit/plugins/modules/test_api_modify.py b/tests/unit/plugins/modules/test_api_modify.py index 20e29e1..6edc046 100644 --- a/tests/unit/plugins/modules/test_api_modify.py +++ b/tests/unit/plugins/modules/test_api_modify.py @@ -6,11 +6,12 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -from ansible_collections.community.routeros.tests.unit.compat.mock import patch, MagicMock +from ansible_collections.community.internal_test_tools.tests.unit.compat.mock import patch, MagicMock +from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import set_module_args, AnsibleExitJson, AnsibleFailJson, ModuleTestCase + from ansible_collections.community.routeros.tests.unit.plugins.modules.fake_api import ( FAKE_ROS_VERSION, FakeLibRouterosError, fake_ros_api, massage_expected_result_data, create_fake_path, ) -from ansible_collections.community.routeros.tests.unit.plugins.modules.utils import set_module_args, AnsibleExitJson, AnsibleFailJson, ModuleTestCase from ansible_collections.community.routeros.plugins.modules import api_modify diff --git a/tests/unit/plugins/modules/test_command.py b/tests/unit/plugins/modules/test_command.py index 72fbfd6..06153f0 100644 --- a/tests/unit/plugins/modules/test_command.py +++ b/tests/unit/plugins/modules/test_command.py @@ -8,9 +8,10 @@ __metaclass__ = type import json -from ansible_collections.community.routeros.tests.unit.compat.mock import patch +from ansible_collections.community.internal_test_tools.tests.unit.compat.mock import patch +from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import set_module_args + from ansible_collections.community.routeros.plugins.modules import command -from ansible_collections.community.routeros.tests.unit.plugins.modules.utils import set_module_args from .routeros_module import TestRouterosModule, load_fixture diff --git a/tests/unit/plugins/modules/test_facts.py b/tests/unit/plugins/modules/test_facts.py index a7e2c8b..918f378 100644 --- a/tests/unit/plugins/modules/test_facts.py +++ b/tests/unit/plugins/modules/test_facts.py @@ -6,9 +6,10 @@ from __future__ import (absolute_import, division, print_function) __metaclass__ = type -from ansible_collections.community.routeros.tests.unit.compat.mock import patch +from ansible_collections.community.internal_test_tools.tests.unit.compat.mock import patch +from ansible_collections.community.internal_test_tools.tests.unit.plugins.modules.utils import set_module_args + from ansible_collections.community.routeros.plugins.modules import facts -from ansible_collections.community.routeros.tests.unit.plugins.modules.utils import set_module_args from .routeros_module import TestRouterosModule, load_fixture diff --git a/tests/unit/plugins/modules/utils.py b/tests/unit/plugins/modules/utils.py deleted file mode 100644 index 0926190..0000000 --- a/tests/unit/plugins/modules/utils.py +++ /dev/null @@ -1,57 +0,0 @@ -# Copyright (c) Ansible Project -# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) -# SPDX-License-Identifier: GPL-3.0-or-later - -from __future__ import absolute_import, division, print_function -__metaclass__ = type - -import contextlib as _contextlib -import json - -from ansible_collections.community.routeros.tests.unit.compat import unittest -from ansible_collections.community.routeros.tests.unit.compat.mock import patch -from ansible.module_utils import basic -from ansible.module_utils.common.text.converters import to_bytes - - -@_contextlib.contextmanager -def set_module_args(args): - if '_ansible_remote_tmp' not in args: - args['_ansible_remote_tmp'] = '/tmp' - if '_ansible_keep_remote_files' not in args: - args['_ansible_keep_remote_files'] = False - - serialized_args = to_bytes(json.dumps({'ANSIBLE_MODULE_ARGS': args})) - with patch.object(basic, '_ANSIBLE_ARGS', serialized_args): - yield - - -class AnsibleExitJson(Exception): - pass - - -class AnsibleFailJson(Exception): - pass - - -def exit_json(*args, **kwargs): - if 'changed' not in kwargs: - kwargs['changed'] = False - raise AnsibleExitJson(kwargs) - - -def fail_json(*args, **kwargs): - kwargs['failed'] = True - raise AnsibleFailJson(kwargs) - - -class ModuleTestCase(unittest.TestCase): - - def setUp(self): - self.mock_module = patch.multiple(basic.AnsibleModule, exit_json=exit_json, fail_json=fail_json) - self.mock_module.start() - self.mock_sleep = patch('time.sleep') - self.mock_sleep.start() - set_module_args({}) - self.addCleanup(self.mock_module.stop) - self.addCleanup(self.mock_sleep.stop)