mirror of
https://github.com/ansible-collections/community.routeros.git
synced 2025-06-21 01:25:49 +02:00
Use shared unit test utils from community.internal_test_tools (#353)
* Use shared unit test utils from community.internal_test_tools. * Make sure community.internal_test_tools is installed in CI.
This commit is contained in:
parent
ab446b4449
commit
30a79061f3
17 changed files with 25 additions and 229 deletions
3
.github/workflows/ansible-test.yml
vendored
3
.github/workflows/ansible-test.yml
vendored
|
@ -43,6 +43,7 @@ jobs:
|
|||
test-deps: >-
|
||||
git+https://github.com/ansible-collections/ansible.utils.git,main
|
||||
git+https://github.com/ansible-collections/ansible.netcommon.git,main
|
||||
git+https://github.com/ansible-collections/community.internal_test_tools.git,main
|
||||
|
||||
units:
|
||||
runs-on: ubuntu-latest
|
||||
|
@ -70,6 +71,7 @@ jobs:
|
|||
test-deps: >-
|
||||
git+https://github.com/ansible-collections/ansible.utils.git,main
|
||||
git+https://github.com/ansible-collections/ansible.netcommon.git,main
|
||||
git+https://github.com/ansible-collections/community.internal_test_tools.git,main
|
||||
|
||||
integration:
|
||||
runs-on: ubuntu-latest
|
||||
|
@ -118,5 +120,6 @@ jobs:
|
|||
test-deps: >-
|
||||
git+https://github.com/ansible-collections/ansible.utils.git,main
|
||||
git+https://github.com/ansible-collections/ansible.netcommon.git,main
|
||||
git+https://github.com/ansible-collections/community.internal_test_tools.git,main
|
||||
target-python-version: ${{ matrix.python }}
|
||||
testing-type: integration
|
||||
|
|
|
@ -1,2 +1 @@
|
|||
update-docs.py shebang
|
||||
tests/unit/compat/mock.py pylint:use-yield-from # suggested construct does not work with Python 2
|
||||
|
|
|
@ -1,2 +1 @@
|
|||
update-docs.py shebang
|
||||
tests/unit/compat/mock.py pylint:use-yield-from # suggested construct does not work with Python 2
|
||||
|
|
|
@ -1,2 +1 @@
|
|||
update-docs.py shebang
|
||||
tests/unit/compat/mock.py pylint:use-yield-from # suggested construct does not work with Python 2
|
||||
|
|
|
@ -1,20 +0,0 @@
|
|||
# Copyright (c) 2014, Toshio Kuratomi <tkuratomi@ansible.com>
|
||||
# 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__'
|
|
@ -1,109 +0,0 @@
|
|||
# Copyright (c) 2014, Toshio Kuratomi <tkuratomi@ansible.com>
|
||||
# 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
|
|
@ -1,25 +0,0 @@
|
|||
# Copyright (c) 2014, Toshio Kuratomi <tkuratomi@ansible.com>
|
||||
# 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
|
|
@ -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')
|
||||
|
|
|
@ -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
|
||||
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
||||
|
|
|
@ -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
|
||||
|
||||
|
||||
|
|
|
@ -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)
|
Loading…
Add table
Add a link
Reference in a new issue