diff --git a/.github/workflows/ansible-test.yml b/.github/workflows/ansible-test.yml index ad01212..e062a88 100644 --- a/.github/workflows/ansible-test.yml +++ b/.github/workflows/ansible-test.yml @@ -37,15 +37,7 @@ jobs: - stable-2.17 - stable-2.18 - devel - # Ansible-test on various stable branches does not yet work well with cgroups v2. - # Since ubuntu-latest now uses Ubuntu 22.04, we need to fall back to the ubuntu-20.04 - # image for these stable branches. The list of branches where this is necessary will - # shrink over time, check out https://github.com/ansible-collections/news-for-maintainers/issues/28 - # for the latest list. - runs-on: >- - ${{ contains(fromJson( - '["stable-2.9", "stable-2.10", "stable-2.11"]' - ), matrix.ansible) && 'ubuntu-20.04' || 'ubuntu-latest' }} + runs-on: ubuntu-latest steps: - name: Perform sanity testing uses: felixfontein/ansible-test-gh-action@main @@ -58,17 +50,10 @@ 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: - # Ansible-test on various stable branches does not yet work well with cgroups v2. - # Since ubuntu-latest now uses Ubuntu 22.04, we need to fall back to the ubuntu-20.04 - # image for these stable branches. The list of branches where this is necessary will - # shrink over time, check out https://github.com/ansible-collections/news-for-maintainers/issues/28 - # for the latest list. - runs-on: >- - ${{ contains(fromJson( - '["stable-2.9", "stable-2.10", "stable-2.11"]' - ), matrix.ansible) && 'ubuntu-20.04' || 'ubuntu-latest' }} + runs-on: ubuntu-latest name: Units (Ⓐ${{ matrix.ansible }}) strategy: # As soon as the first unit test fails, cancel the others to free up the CI queue @@ -101,17 +86,10 @@ 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: - # Ansible-test on various stable branches does not yet work well with cgroups v2. - # Since ubuntu-latest now uses Ubuntu 22.04, we need to fall back to the ubuntu-20.04 - # image for these stable branches. The list of branches where this is necessary will - # shrink over time, check out https://github.com/ansible-collections/news-for-maintainers/issues/28 - # for the latest list. - runs-on: >- - ${{ contains(fromJson( - '["stable-2.9", "stable-2.10", "stable-2.11"]' - ), matrix.ansible) && 'ubuntu-20.04' || 'ubuntu-latest' }} + runs-on: ubuntu-latest name: I (Ⓐ${{ matrix.ansible }}+py${{ matrix.python }}) strategy: fail-fast: false @@ -175,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/.github/workflows/ee.yml b/.github/workflows/ee.yml index cd6341f..32c0c50 100644 --- a/.github/workflows/ee.yml +++ b/.github/workflows/ee.yml @@ -68,6 +68,7 @@ jobs: uses: actions/checkout@v4 with: path: ansible_collections/${{ env.NAMESPACE }}/${{ env.COLLECTION_NAME }} + persist-credentials: false - name: Set up Python uses: actions/setup-python@v5 @@ -101,7 +102,7 @@ jobs: - name: Create files for building execution environment run: | - COLLECTION_FILENAME="$(ls "${{ env.NAMESPACE }}-${{ env.COLLECTION_NAME }}"-*.tar.gz)" + COLLECTION_FILENAME="$(ls "${NAMESPACE}-${COLLECTION_NAME}"-*.tar.gz)" # EE config cat > execution-environment.yml <v2\.20\.0 +- v2\.20\.1 - Release Summary +- v2\.20\.0 + - Release Summary - Minor Changes - v2\.19\.0 - - Release Summary + - Release Summary - Minor Changes - v2\.18\.0 - - Release Summary + - Release Summary - Minor Changes - Deprecated Features - Bugfixes - v2\.17\.0 - - Release Summary + - Release Summary - Minor Changes - v2\.16\.0 - - Release Summary + - Release Summary - Minor Changes - v2\.15\.0 - - Release Summary + - Release Summary - Minor Changes - v2\.14\.0 - - Release Summary + - Release Summary - Minor Changes - v2\.13\.0 - - Release Summary + - Release Summary - Minor Changes - Bugfixes - v2\.12\.0 - - Release Summary + - Release Summary - Minor Changes - v2\.11\.0 - - Release Summary + - Release Summary - Minor Changes - v2\.10\.0 - - Release Summary + - Release Summary - Minor Changes - Bugfixes - v2\.9\.0 - - Release Summary + - Release Summary - Minor Changes - Bugfixes - v2\.8\.3 - - Release Summary + - Release Summary - Known Issues - v2\.8\.2 - - Release Summary + - Release Summary - Bugfixes - v2\.8\.1 - - Release Summary + - Release Summary - Bugfixes - v2\.8\.0 - - Release Summary + - Release Summary - Minor Changes - Bugfixes - v2\.7\.0 - - Release Summary + - Release Summary - Minor Changes - Bugfixes - v2\.6\.0 - - Release Summary + - Release Summary - Minor Changes - Bugfixes - v2\.5\.0 - - Release Summary + - Release Summary - Minor Changes - Bugfixes - v2\.4\.0 - - Release Summary + - Release Summary - Minor Changes - Bugfixes - Known Issues - v2\.3\.1 - - Release Summary + - Release Summary - Known Issues - v2\.3\.0 - - Release Summary + - Release Summary - Minor Changes - Bugfixes - v2\.2\.1 - - Release Summary + - Release Summary - Bugfixes - v2\.2\.0 - - Release Summary + - Release Summary - Minor Changes - Bugfixes - New Modules - v2\.1\.0 - - Release Summary + - Release Summary - Minor Changes - Bugfixes - New Modules - v2\.0\.0 - - Release Summary + - Release Summary - Minor Changes - Breaking Changes / Porting Guide - Bugfixes - New Plugins - Filter - v1\.2\.0 - - Release Summary + - Release Summary - Minor Changes - Bugfixes - v1\.1\.0 - - Release Summary + - Release Summary - Minor Changes - v1\.0\.1 - - Release Summary + - Release Summary - Bugfixes - v1\.0\.0 - - Release Summary + - Release Summary - Bugfixes - v0\.1\.1 - - Release Summary + - Release Summary - Bugfixes - v0\.1\.0 - - Release Summary + - Release Summary - Minor Changes + +## v2\.20\.1 + + +### Release Summary + +This is the final community\.routeros 2\.x\.y release\. + +Please upgrade to community\.routeros 3\.x\.y as soon as possible to receive further bugfixes\. + +Thanks to everyone who contributed to community\.routeros 2\.x\.y releases\! + ## v2\.20\.0 - + ### Release Summary Feature release\. @@ -139,7 +153,7 @@ Feature release\. ## v2\.19\.0 - + ### Release Summary Feature release\. @@ -156,7 +170,7 @@ Feature release\. ## v2\.18\.0 - + ### Release Summary Feature release\. @@ -185,7 +199,7 @@ Feature release\. ## v2\.17\.0 - + ### Release Summary Feature release\. @@ -200,7 +214,7 @@ Feature release\. ## v2\.16\.0 - + ### Release Summary Feature release\. @@ -214,7 +228,7 @@ Feature release\. ## v2\.15\.0 - + ### Release Summary Feature release\. @@ -236,7 +250,7 @@ Feature release\. ## v2\.14\.0 - + ### Release Summary Feature release\. @@ -251,7 +265,7 @@ Feature release\. ## v2\.13\.0 - + ### Release Summary Bugfix and feature release\. @@ -270,7 +284,7 @@ Bugfix and feature release\. ## v2\.12\.0 - + ### Release Summary Feature release\. @@ -289,7 +303,7 @@ Feature release\. ## v2\.11\.0 - + ### Release Summary Feature and bugfix release\. @@ -309,7 +323,7 @@ Feature and bugfix release\. ## v2\.10\.0 - + ### Release Summary Bugfix and feature release\. @@ -346,7 +360,7 @@ Bugfix and feature release\. ## v2\.9\.0 - + ### Release Summary Bugfix and feature release\. @@ -365,7 +379,7 @@ Bugfix and feature release\. ## v2\.8\.3 - + ### Release Summary Maintenance release with updated documentation\. @@ -386,7 +400,7 @@ for the rendered HTML version of the documentation of the latest release\. ## v2\.8\.2 - + ### Release Summary Bugfix release\. @@ -399,7 +413,7 @@ Bugfix release\. ## v2\.8\.1 - + ### Release Summary Bugfix release\. @@ -412,7 +426,7 @@ Bugfix release\. ## v2\.8\.0 - + ### Release Summary Bugfix and feature release\. @@ -437,7 +451,7 @@ Bugfix and feature release\. ## v2\.7\.0 - + ### Release Summary Bugfix and feature release\. @@ -455,7 +469,7 @@ Bugfix and feature release\. ## v2\.6\.0 - + ### Release Summary Regular bugfix and feature release\. @@ -475,7 +489,7 @@ Regular bugfix and feature release\. ## v2\.5\.0 - + ### Release Summary Feature and bugfix release\. @@ -493,7 +507,7 @@ Feature and bugfix release\. ## v2\.4\.0 - + ### Release Summary Feature release improving the api\* modules\. @@ -537,7 +551,7 @@ Feature release improving the api\* modules\. ## v2\.3\.1 - + ### Release Summary Maintenance release with improved documentation\. @@ -550,7 +564,7 @@ Maintenance release with improved documentation\. ## v2\.3\.0 - + ### Release Summary Feature and bugfix release\. @@ -570,7 +584,7 @@ Feature and bugfix release\. ## v2\.2\.1 - + ### Release Summary Bugfix release\. @@ -584,7 +598,7 @@ Bugfix release\. ## v2\.2\.0 - + ### Release Summary New feature release\. @@ -608,7 +622,7 @@ New feature release\. ## v2\.1\.0 - + ### Release Summary Feature and bugfix release with new modules\. @@ -637,7 +651,7 @@ Feature and bugfix release with new modules\. ## v2\.0\.0 - + ### Release Summary A new major release with breaking changes in the behavior of community\.routeros\.api and community\.routeros\.command\. @@ -678,7 +692,7 @@ A new major release with breaking changes in the behavior of community\.ro ## v1\.2\.0 - + ### Release Summary Bugfix and feature release\. @@ -699,7 +713,7 @@ Bugfix and feature release\. ## v1\.1\.0 - + ### Release Summary This release allow dashes in usernames for SSH\-based modules\. @@ -713,7 +727,7 @@ This release allow dashes in usernames for SSH\-based modules\. ## v1\.0\.1 - + ### Release Summary Maintenance release with a bugfix for api\. @@ -726,7 +740,7 @@ Maintenance release with a bugfix for api\. ## v1\.0\.0 - + ### Release Summary This is the first production \(non\-prerelease\) release of community\.routeros\. @@ -739,7 +753,7 @@ This is the first production \(non\-prerelease\) release of community\.rou ## v0\.1\.1 - + ### Release Summary Small improvements and bugfixes over the initial release\. @@ -752,7 +766,7 @@ Small improvements and bugfixes over the initial release\. ## v0\.1\.0 - + ### Release Summary The community\.routeros continues the work on the Ansible RouterOS modules from their state in community\.network 1\.2\.0\. The changes listed here are thus relative to the modules community\.network\.routeros\_\*\. diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 108831a..7a3b0a5 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -4,6 +4,18 @@ Community RouterOS Release Notes .. contents:: Topics +v2.20.1 +======= + +Release Summary +--------------- + +This is the final community.routeros 2.x.y release. + +Please upgrade to community.routeros 3.x.y as soon as possible to receive further bugfixes. + +Thanks to everyone who contributed to community.routeros 2.x.y releases! + v2.20.0 ======= diff --git a/README.md b/README.md index a773b72..b11b3b1 100644 --- a/README.md +++ b/README.md @@ -5,7 +5,8 @@ SPDX-License-Identifier: GPL-3.0-or-later --> # Community RouterOS Collection -[![CI](https://github.com/ansible-collections/community.routeros/workflows/CI/badge.svg?event=push)](https://github.com/ansible-collections/community.routeros/actions) +[![Documentation](https://img.shields.io/badge/docs-brightgreen.svg)](https://docs.ansible.com/ansible/latest/collections/community/routeros/) +[![CI](https://github.com/ansible-collections/community.routeros/actions/workflows/ansible-test.yml/badge.svg?branch=stable-2)](https://github.com/ansible-collections/community.routeros/actions) [![Codecov](https://img.shields.io/codecov/c/github/ansible-collections/community.routeros)](https://codecov.io/gh/ansible-collections/community.routeros) [![REUSE status](https://api.reuse.software/badge/github.com/ansible-collections/community.routeros)](https://api.reuse.software/info/github.com/ansible-collections/community.routeros) @@ -53,7 +54,7 @@ Browsing the [**latest** collection documentation](https://docs.ansible.com/ansi Browsing the [**devel** collection documentation](https://docs.ansible.com/ansible/devel/collections/community/routeros) shows docs for the _latest version released on Galaxy_. -We also separately publish [**latest commit** collection documentation](https://ansible-collections.github.io/community.routeros/branch/main/) which shows docs for the _latest commit in the `main` branch_. +We also separately publish [**latest commit** collection documentation](https://ansible-collections.github.io/community.routeros/branch/stable-2/) which shows docs for the _latest commit in the `stable-2` branch_. If you use the Ansible package and do not update collections independently, use **latest**. If you install or update this collection directly from Galaxy, use **devel**. If you are looking to contribute, use **latest commit**. @@ -183,7 +184,7 @@ See [Ansible's dev guide](https://docs.ansible.com/ansible/devel/dev_guide/devel ## Release notes -See the [collection's changelog](https://github.com/ansible-collections/community.routeros/blob/main/CHANGELOG.md). +See the [collection's changelog](https://github.com/ansible-collections/community.routeros/blob/stable-2/CHANGELOG.md). ## Roadmap @@ -203,8 +204,8 @@ We plan to regularly release minor and patch versions, whenever new features are This collection is primarily licensed and distributed as a whole under the GNU General Public License v3.0 or later. -See [LICENSES/GPL-3.0-or-later.txt](https://github.com/ansible-collections/community.routeros/blob/main/COPYING) for the full text. +See [LICENSES/GPL-3.0-or-later.txt](https://github.com/ansible-collections/community.routeros/blob/stable-2/COPYING) for the full text. -Parts of the collection are licensed under the [BSD 2-Clause license](https://github.com/ansible-collections/community.routeros/blob/main/LICENSES/BSD-2-Clause.txt). +Parts of the collection are licensed under the [BSD 2-Clause license](https://github.com/ansible-collections/community.routeros/blob/stable-2/LICENSES/BSD-2-Clause.txt). All files have a machine readable `SDPX-License-Identifier:` comment denoting its respective license(s) or an equivalent entry in an accompanying `.license` file. Only changelog fragments (which will not be part of a release) are covered by a blanket statement in `.reuse/dep5`. This conforms to the [REUSE specification](https://reuse.software/spec/). diff --git a/changelogs/changelog.yaml b/changelogs/changelog.yaml index 7185a2c..0ca7cbd 100644 --- a/changelogs/changelog.yaml +++ b/changelogs/changelog.yaml @@ -795,3 +795,18 @@ releases: - 322-add-l2tp-client-interface-configuration.yml - 323-add-ros-7.16-parameters.yml release_date: '2024-10-17' + 2.20.1: + changes: + release_summary: 'This is the final community.routeros 2.x.y release. + + + Please upgrade to community.routeros 3.x.y as soon as possible to receive + further bugfixes. + + + Thanks to everyone who contributed to community.routeros 2.x.y releases! + + ' + fragments: + - 2.20.1.yml + release_date: '2025-04-19' diff --git a/galaxy.yml b/galaxy.yml index 7c40eda..4c73628 100644 --- a/galaxy.yml +++ b/galaxy.yml @@ -7,7 +7,7 @@ namespace: community name: routeros -version: 2.20.0 +version: 2.20.1 readme: README.md authors: - Egor Zaitsev (github.com/heuels) diff --git a/plugins/module_utils/_api_data.py b/plugins/module_utils/_api_data.py index 847f5b1..e4a5182 100644 --- a/plugins/module_utils/_api_data.py +++ b/plugins/module_utils/_api_data.py @@ -5142,7 +5142,7 @@ PATHS = { 'protocol': KeyInfo(default='all'), 'src-address': KeyInfo(), 'src-port': KeyInfo(default='any'), - # The template field can't really be changed once the item is + # The template field ca not really be changed once the item is # created. This config captures the behavior best as it can # i.e. template=yes is shown, template=no is hidden. 'template': KeyInfo(can_disable=True, remove_value=False), diff --git a/plugins/module_utils/api.py b/plugins/module_utils/api.py index 6c276d7..aa3aa2e 100644 --- a/plugins/module_utils/api.py +++ b/plugins/module_utils/api.py @@ -77,7 +77,7 @@ def _ros_api_connect(module, username, password, host, port, use_tls, force_no_c elif not validate_cert_hostname: ctx.check_hostname = False else: - # Since librouteros doesn't pass server_hostname, + # Since librouteros does not pass server_hostname, # we have to do this ourselves: def wrap_context(*args, **kwargs): kwargs.pop('server_hostname', None) diff --git a/plugins/modules/api.py b/plugins/modules/api.py index 4857a3c..469b9ea 100644 --- a/plugins/modules/api.py +++ b/plugins/modules/api.py @@ -65,7 +65,7 @@ options: type: str query: description: - - Query given path for selected query attributes from RouterOS aip. + - Query given path for selected query attributes from RouterOS API. - WHERE is key word which extend query. WHERE format is key operator value - with spaces. - WHERE valid operators are V(==) or V(eq), V(!=) or V(not), V(>) or V(more), V(<) or V(less). - Example path V(ip address) and query V(.id address) will return only C(.id) and C(address) for all items in V(ip address) path. diff --git a/tests/integration/targets/filter_quoting/tasks/main.yml b/tests/integration/targets/filter_quoting/tasks/main.yml index e7a2d29..c80af59 100644 --- a/tests/integration/targets/filter_quoting/tasks/main.yml +++ b/tests/integration/targets/filter_quoting/tasks/main.yml @@ -22,7 +22,7 @@ assert: that: - >- - result.msg == "Unexpected end of string during escaped parameter" + "Unexpected end of string during escaped parameter" in result.msg - name: "Test quote_argument filter" assert: 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 4cfdeef..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 @@ -34,8 +35,8 @@ class TestRouterosApiModule(ModuleTestCase): def test_module_fail_when_required_args_missing(self): with self.assertRaises(AnsibleFailJson) as exc: - set_module_args({}) - self.module.main() + with set_module_args({}): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['failed'], True) @@ -43,8 +44,8 @@ class TestRouterosApiModule(ModuleTestCase): @patch('ansible_collections.community.routeros.plugins.modules.api.ROS_api_module.api_add_path', new=fake_ros_api.path) def test_api_path(self): with self.assertRaises(AnsibleExitJson) as exc: - set_module_args(self.config_module_args.copy()) - self.module.main() + with set_module_args(self.config_module_args.copy()): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], False) @@ -54,8 +55,8 @@ class TestRouterosApiModule(ModuleTestCase): with self.assertRaises(AnsibleExitJson) as exc: module_args = self.config_module_args.copy() module_args['add'] = "name=unit_test_brige" - set_module_args(module_args) - self.module.main() + with set_module_args(module_args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], True) @@ -65,8 +66,8 @@ class TestRouterosApiModule(ModuleTestCase): with self.assertRaises(AnsibleFailJson) as exc: module_args = self.config_module_args.copy() module_args['add'] = "name=unit_test_brige_exist" - set_module_args(module_args) - self.module.main() + with set_module_args(module_args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['failed'], True) @@ -77,8 +78,8 @@ class TestRouterosApiModule(ModuleTestCase): with self.assertRaises(AnsibleExitJson) as exc: module_args = self.config_module_args.copy() module_args['remove'] = "*A1" - set_module_args(module_args) - self.module.main() + with set_module_args(module_args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], True) @@ -88,8 +89,8 @@ class TestRouterosApiModule(ModuleTestCase): with self.assertRaises(AnsibleFailJson) as exc: module_args = self.config_module_args.copy() module_args['remove'] = "*A2" - set_module_args(module_args) - self.module.main() + with set_module_args(module_args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['failed'], True) @@ -100,8 +101,8 @@ class TestRouterosApiModule(ModuleTestCase): with self.assertRaises(AnsibleExitJson) as exc: module_args = self.config_module_args.copy() module_args['cmd'] = "add name=unit_test_brige_arbitrary" - set_module_args(module_args) - self.module.main() + with set_module_args(module_args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], False) @@ -111,8 +112,8 @@ class TestRouterosApiModule(ModuleTestCase): with self.assertRaises(AnsibleFailJson) as exc: module_args = self.config_module_args.copy() module_args['cmd'] = "add NONE_EXIST=unit_test_brige_arbitrary" - set_module_args(module_args) - self.module.main() + with set_module_args(module_args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['failed'], True) @@ -123,8 +124,8 @@ class TestRouterosApiModule(ModuleTestCase): with self.assertRaises(AnsibleExitJson) as exc: module_args = self.config_module_args.copy() module_args['update'] = ".id=*A1 name=unit_test_brige" - set_module_args(module_args) - self.module.main() + with set_module_args(module_args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], True) @@ -134,8 +135,8 @@ class TestRouterosApiModule(ModuleTestCase): with self.assertRaises(AnsibleFailJson) as exc: module_args = self.config_module_args.copy() module_args['update'] = ".id=*A2 name=unit_test_brige" - set_module_args(module_args) - self.module.main() + with set_module_args(module_args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['failed'], True) @@ -146,8 +147,8 @@ class TestRouterosApiModule(ModuleTestCase): with self.assertRaises(AnsibleExitJson) as exc: module_args = self.config_module_args.copy() module_args['query'] = ".id name" - set_module_args(module_args) - self.module.main() + with set_module_args(module_args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], False) @@ -162,8 +163,8 @@ class TestRouterosApiModule(ModuleTestCase): with self.assertRaises(AnsibleExitJson) as exc: module_args = self.config_module_args.copy() module_args['query'] = ".id other" - set_module_args(module_args) - self.module.main() + with set_module_args(module_args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], False) @@ -174,8 +175,8 @@ class TestRouterosApiModule(ModuleTestCase): with self.assertRaises(AnsibleExitJson) as exc: module_args = self.config_module_args.copy() module_args['query'] = ".id name WHERE name == dummy_bridge_A2" - set_module_args(module_args) - self.module.main() + with set_module_args(module_args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], False) @@ -188,8 +189,8 @@ class TestRouterosApiModule(ModuleTestCase): with self.assertRaises(AnsibleExitJson) as exc: module_args = self.config_module_args.copy() module_args['query'] = ".id name WHERE name != dummy_bridge_A2" - set_module_args(module_args) - self.module.main() + with set_module_args(module_args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], False) @@ -204,8 +205,8 @@ class TestRouterosApiModule(ModuleTestCase): module_args['extended_query'] = { 'attributes': ['.id', 'name'], } - set_module_args(module_args) - self.module.main() + with set_module_args(module_args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], False) @@ -222,8 +223,8 @@ class TestRouterosApiModule(ModuleTestCase): module_args['extended_query'] = { 'attributes': ['.id', 'other'], } - set_module_args(module_args) - self.module.main() + with set_module_args(module_args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], False) @@ -243,8 +244,8 @@ class TestRouterosApiModule(ModuleTestCase): }, ], } - set_module_args(module_args) - self.module.main() + with set_module_args(module_args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], False) @@ -266,8 +267,8 @@ class TestRouterosApiModule(ModuleTestCase): }, ], } - set_module_args(module_args) - self.module.main() + with set_module_args(module_args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], False) @@ -298,8 +299,8 @@ class TestRouterosApiModule(ModuleTestCase): }, ], } - set_module_args(module_args) - self.module.main() + with set_module_args(module_args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], False) diff --git a/tests/unit/plugins/modules/test_api_facts.py b/tests/unit/plugins/modules/test_api_facts.py index 64985f8..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 @@ -437,8 +438,8 @@ class TestRouterosApiFactsModule(ModuleTestCase): def test_module_fail_when_required_args_missing(self): with self.assertRaises(AnsibleFailJson) as exc: - set_module_args({}) - self.module.main() + with set_module_args({}): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['failed'], True) @@ -447,8 +448,8 @@ class TestRouterosApiFactsModule(ModuleTestCase): with self.assertRaises(AnsibleFailJson) as exc: module_args = self.config_module_args.copy() module_args['gather_subset'] = ['!foobar'] - set_module_args(module_args) - self.module.main() + with set_module_args(module_args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['failed'], True) @@ -456,8 +457,8 @@ class TestRouterosApiFactsModule(ModuleTestCase): def test_full_run(self): with self.assertRaises(AnsibleExitJson) as exc: - set_module_args(self.config_module_args.copy()) - self.module.main() + with set_module_args(self.config_module_args.copy()): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], False) 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 2f47af4..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 @@ -117,8 +118,8 @@ class TestRouterosApiFindAndModifyModule(ModuleTestCase): def test_module_fail_when_required_args_missing(self): with self.assertRaises(AnsibleFailJson) as exc: - set_module_args({}) - self.module.main() + with set_module_args({}): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['failed'], True) @@ -136,8 +137,8 @@ class TestRouterosApiFindAndModifyModule(ModuleTestCase): 'comment': 'bar', }, }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['failed'], True) @@ -155,8 +156,8 @@ class TestRouterosApiFindAndModifyModule(ModuleTestCase): 'comment': 'bar', }, }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['failed'], True) @@ -173,8 +174,8 @@ class TestRouterosApiFindAndModifyModule(ModuleTestCase): '!comment': None, }, }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['failed'], True) @@ -190,8 +191,8 @@ class TestRouterosApiFindAndModifyModule(ModuleTestCase): '!comment': 'gone', }, }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['failed'], True) @@ -212,8 +213,8 @@ class TestRouterosApiFindAndModifyModule(ModuleTestCase): }, 'require_matches_min': 10, }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['failed'], True) @@ -234,8 +235,8 @@ class TestRouterosApiFindAndModifyModule(ModuleTestCase): }, 'require_matches_min': 10, }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['failed'], True) @@ -256,8 +257,8 @@ class TestRouterosApiFindAndModifyModule(ModuleTestCase): }, 'require_matches_max': 1, }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['failed'], True) @@ -277,8 +278,8 @@ class TestRouterosApiFindAndModifyModule(ModuleTestCase): 'name': 'bam', }, }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], False) @@ -303,8 +304,8 @@ class TestRouterosApiFindAndModifyModule(ModuleTestCase): 'require_matches_min': 2, 'allow_no_matches': True, }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], False) @@ -325,8 +326,8 @@ class TestRouterosApiFindAndModifyModule(ModuleTestCase): 'values': { }, }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], False) @@ -349,8 +350,8 @@ class TestRouterosApiFindAndModifyModule(ModuleTestCase): 'comment': None, }, }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], False) @@ -374,8 +375,8 @@ class TestRouterosApiFindAndModifyModule(ModuleTestCase): }, '_ansible_diff': True, }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], True) @@ -450,8 +451,8 @@ class TestRouterosApiFindAndModifyModule(ModuleTestCase): 'comment': None, }, }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], True) @@ -502,8 +503,8 @@ class TestRouterosApiFindAndModifyModule(ModuleTestCase): 'comment': '', }, }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], True) @@ -553,8 +554,8 @@ class TestRouterosApiFindAndModifyModule(ModuleTestCase): '!comment': None, }, }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], True) @@ -606,8 +607,8 @@ class TestRouterosApiFindAndModifyModule(ModuleTestCase): '!connection-state': None, }, }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], True) diff --git a/tests/unit/plugins/modules/test_api_info.py b/tests/unit/plugins/modules/test_api_info.py index 1c9d014..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 @@ -41,8 +42,8 @@ class TestRouterosApiInfoModule(ModuleTestCase): def test_module_fail_when_required_args_missing(self): with self.assertRaises(AnsibleFailJson) as exc: - set_module_args({}) - self.module.main() + with set_module_args({}): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['failed'], True) @@ -53,8 +54,8 @@ class TestRouterosApiInfoModule(ModuleTestCase): args.update({ 'path': 'something invalid' }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['failed'], True) @@ -68,8 +69,8 @@ class TestRouterosApiInfoModule(ModuleTestCase): args.update({ 'path': 'ip dns static' }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], False) @@ -93,8 +94,8 @@ class TestRouterosApiInfoModule(ModuleTestCase): args.update({ 'path': 'caps-man aaa', }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], False) @@ -122,8 +123,8 @@ class TestRouterosApiInfoModule(ModuleTestCase): 'path': 'caps-man aaa', 'hide_defaults': False, }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], False) @@ -155,8 +156,8 @@ class TestRouterosApiInfoModule(ModuleTestCase): 'path': 'caps-man aaa', 'unfiltered': True, }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], False) @@ -189,8 +190,8 @@ class TestRouterosApiInfoModule(ModuleTestCase): 'path': 'ip firewall filter', 'handle_disabled': 'exclamation', }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], False) @@ -278,8 +279,8 @@ class TestRouterosApiInfoModule(ModuleTestCase): 'path': 'ip firewall filter', 'handle_disabled': 'null-value', }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], False) @@ -367,8 +368,8 @@ class TestRouterosApiInfoModule(ModuleTestCase): 'path': 'ip firewall filter', 'handle_disabled': 'omit', }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], False) @@ -402,8 +403,8 @@ class TestRouterosApiInfoModule(ModuleTestCase): 'handle_disabled': 'omit', 'include_dynamic': True, }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], False) @@ -460,8 +461,8 @@ class TestRouterosApiInfoModule(ModuleTestCase): 'path': 'interface list', 'handle_disabled': 'omit', }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], False) @@ -513,8 +514,8 @@ class TestRouterosApiInfoModule(ModuleTestCase): 'handle_disabled': 'omit', 'include_builtin': True, }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], False) @@ -605,8 +606,8 @@ class TestRouterosApiInfoModule(ModuleTestCase): 'path': 'ip dhcp-server lease', 'handle_disabled': 'omit', }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], False) @@ -688,8 +689,8 @@ class TestRouterosApiInfoModule(ModuleTestCase): args.update({ 'path': 'interface gre', }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], False) @@ -776,8 +777,8 @@ class TestRouterosApiInfoModule(ModuleTestCase): 'handle_disabled': 'omit', 'hide_defaults': False, }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], False) @@ -847,8 +848,8 @@ class TestRouterosApiInfoModule(ModuleTestCase): 'handle_disabled': 'omit', 'restrict': [], }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], False) @@ -899,8 +900,8 @@ class TestRouterosApiInfoModule(ModuleTestCase): 'values': ['forward'], }], }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], False) @@ -970,8 +971,8 @@ class TestRouterosApiInfoModule(ModuleTestCase): }, ], }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], False) diff --git a/tests/unit/plugins/modules/test_api_modify.py b/tests/unit/plugins/modules/test_api_modify.py index f4aa777..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 @@ -318,8 +319,8 @@ class TestRouterosApiModifyModule(ModuleTestCase): def test_module_fail_when_required_args_missing(self): with self.assertRaises(AnsibleFailJson) as exc: - set_module_args({}) - self.module.main() + with set_module_args({}): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['failed'], True) @@ -331,8 +332,8 @@ class TestRouterosApiModifyModule(ModuleTestCase): 'path': 'something invalid', 'data': [], }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['failed'], True) @@ -348,8 +349,8 @@ class TestRouterosApiModifyModule(ModuleTestCase): 'foo': 'bar', }], }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['failed'], True) @@ -366,8 +367,8 @@ class TestRouterosApiModifyModule(ModuleTestCase): '!comment': None, }], }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['failed'], True) @@ -383,8 +384,8 @@ class TestRouterosApiModifyModule(ModuleTestCase): '!disabled': None, }], }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['failed'], True) @@ -400,8 +401,8 @@ class TestRouterosApiModifyModule(ModuleTestCase): '!comment': 'foo', }], }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['failed'], True) @@ -416,8 +417,8 @@ class TestRouterosApiModifyModule(ModuleTestCase): 'name': None, }], }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['failed'], True) @@ -432,8 +433,8 @@ class TestRouterosApiModifyModule(ModuleTestCase): 'interface': 'eth0', }], }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['failed'], True) @@ -448,8 +449,8 @@ class TestRouterosApiModifyModule(ModuleTestCase): 'address': '192.168.88.1', }], }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['failed'], True) @@ -466,8 +467,8 @@ class TestRouterosApiModifyModule(ModuleTestCase): 'address': '192.168.88.1', }], }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['failed'], True) @@ -498,8 +499,8 @@ class TestRouterosApiModifyModule(ModuleTestCase): }, ], }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], False) @@ -533,8 +534,8 @@ class TestRouterosApiModifyModule(ModuleTestCase): 'handle_absent_entries': 'remove', 'handle_entries_content': 'remove', }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], False) @@ -556,8 +557,8 @@ class TestRouterosApiModifyModule(ModuleTestCase): }, ], }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], False) @@ -591,8 +592,8 @@ class TestRouterosApiModifyModule(ModuleTestCase): }, ], }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], True) @@ -656,8 +657,8 @@ class TestRouterosApiModifyModule(ModuleTestCase): }, ], }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], True) @@ -722,8 +723,8 @@ class TestRouterosApiModifyModule(ModuleTestCase): ], '_ansible_check_mode': True, }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], True) @@ -785,8 +786,8 @@ class TestRouterosApiModifyModule(ModuleTestCase): 'handle_absent_entries': 'remove', 'handle_entries_content': 'remove', }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], True) @@ -844,8 +845,8 @@ class TestRouterosApiModifyModule(ModuleTestCase): 'handle_entries_content': 'remove', '_ansible_check_mode': True, }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], True) @@ -903,8 +904,8 @@ class TestRouterosApiModifyModule(ModuleTestCase): 'handle_absent_entries': 'remove', 'handle_entries_content': 'remove', }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], True) @@ -963,8 +964,8 @@ class TestRouterosApiModifyModule(ModuleTestCase): 'handle_entries_content': 'remove', '_ansible_check_mode': True, }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], True) @@ -1017,8 +1018,8 @@ class TestRouterosApiModifyModule(ModuleTestCase): 'handle_absent_entries': 'remove', 'handle_entries_content': 'remove', }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], True) @@ -1077,8 +1078,8 @@ class TestRouterosApiModifyModule(ModuleTestCase): 'handle_entries_content': 'remove', '_ansible_check_mode': True, }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], True) @@ -1128,8 +1129,8 @@ class TestRouterosApiModifyModule(ModuleTestCase): 'handle_absent_entries': 'remove', 'handle_entries_content': 'remove', }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], True) @@ -1164,8 +1165,8 @@ class TestRouterosApiModifyModule(ModuleTestCase): 'handle_entries_content': 'remove', '_ansible_check_mode': True, }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], True) @@ -1212,8 +1213,8 @@ class TestRouterosApiModifyModule(ModuleTestCase): 'handle_entries_content': 'remove', 'ensure_order': True, }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], True) @@ -1285,8 +1286,8 @@ class TestRouterosApiModifyModule(ModuleTestCase): 'ensure_order': True, '_ansible_check_mode': True, }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], True) @@ -1340,8 +1341,8 @@ class TestRouterosApiModifyModule(ModuleTestCase): }, ], }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], False) @@ -1363,8 +1364,8 @@ class TestRouterosApiModifyModule(ModuleTestCase): ], 'handle_entries_content': 'remove', }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], False) @@ -1386,8 +1387,8 @@ class TestRouterosApiModifyModule(ModuleTestCase): }, ], }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], True) @@ -1426,8 +1427,8 @@ class TestRouterosApiModifyModule(ModuleTestCase): ], '_ansible_check_mode': True, }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], True) @@ -1466,8 +1467,8 @@ class TestRouterosApiModifyModule(ModuleTestCase): ], 'handle_entries_content': 'remove', }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], True) @@ -1507,8 +1508,8 @@ class TestRouterosApiModifyModule(ModuleTestCase): 'handle_entries_content': 'remove', '_ansible_check_mode': True, }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], True) @@ -1551,8 +1552,8 @@ class TestRouterosApiModifyModule(ModuleTestCase): }, ], }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], False) @@ -1587,8 +1588,8 @@ class TestRouterosApiModifyModule(ModuleTestCase): 'handle_absent_entries': 'remove', 'handle_entries_content': 'remove', }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], False) @@ -1621,8 +1622,8 @@ class TestRouterosApiModifyModule(ModuleTestCase): 'handle_absent_entries': 'remove', 'handle_entries_content': 'remove', }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], True) @@ -1676,8 +1677,8 @@ class TestRouterosApiModifyModule(ModuleTestCase): 'handle_entries_content': 'remove', '_ansible_check_mode': True, }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], True) @@ -1729,8 +1730,8 @@ class TestRouterosApiModifyModule(ModuleTestCase): 'handle_entries_content': 'remove', 'ensure_order': True, }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], True) @@ -1785,8 +1786,8 @@ class TestRouterosApiModifyModule(ModuleTestCase): 'ensure_order': True, '_ansible_check_mode': True, }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], True) @@ -1847,8 +1848,8 @@ class TestRouterosApiModifyModule(ModuleTestCase): 'handle_entries_content': 'remove', 'ensure_order': True, }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], False) @@ -1879,8 +1880,8 @@ class TestRouterosApiModifyModule(ModuleTestCase): 'handle_entries_content': 'remove', 'ensure_order': True, }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], False) @@ -1911,8 +1912,8 @@ class TestRouterosApiModifyModule(ModuleTestCase): 'handle_entries_content': 'remove', 'ensure_order': True, }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], True) @@ -1969,8 +1970,8 @@ class TestRouterosApiModifyModule(ModuleTestCase): 'handle_absent_entries': 'remove', 'ensure_order': True, }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], False) @@ -2005,8 +2006,8 @@ class TestRouterosApiModifyModule(ModuleTestCase): 'handle_absent_entries': 'remove', 'ensure_order': True, }) - set_module_args(args) - self.module.main() + with set_module_args(args): + self.module.main() result = exc.exception.args[0] self.assertEqual(result['changed'], False) diff --git a/tests/unit/plugins/modules/test_command.py b/tests/unit/plugins/modules/test_command.py index 3fc5865..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 @@ -47,54 +48,54 @@ class TestRouterosCommandModule(TestRouterosModule): self.run_commands.side_effect = load_from_file def test_command_simple(self): - set_module_args(dict(commands=['/system resource print'])) - result = self.execute_module(changed=True) + with set_module_args(dict(commands=['/system resource print'])): + result = self.execute_module(changed=True) self.assertEqual(len(result['stdout']), 1) self.assertTrue('platform: "MikroTik"' in result['stdout'][0]) def test_command_multiple(self): - set_module_args(dict(commands=['/system resource print', '/system resource print'])) - result = self.execute_module(changed=True) + with set_module_args(dict(commands=['/system resource print', '/system resource print'])): + result = self.execute_module(changed=True) self.assertEqual(len(result['stdout']), 2) self.assertTrue('platform: "MikroTik"' in result['stdout'][0]) def test_command_wait_for(self): wait_for = 'result[0] contains "MikroTik"' - set_module_args(dict(commands=['/system resource print'], wait_for=wait_for)) - self.execute_module(changed=True) + with set_module_args(dict(commands=['/system resource print'], wait_for=wait_for)): + self.execute_module(changed=True) def test_command_wait_for_fails(self): wait_for = 'result[0] contains "test string"' - set_module_args(dict(commands=['/system resource print'], wait_for=wait_for)) - self.execute_module(failed=True) + with set_module_args(dict(commands=['/system resource print'], wait_for=wait_for)): + self.execute_module(failed=True) self.assertEqual(self.run_commands.call_count, 10) def test_command_retries(self): wait_for = 'result[0] contains "test string"' - set_module_args(dict(commands=['/system resource print'], wait_for=wait_for, retries=2)) - self.execute_module(failed=True) + with set_module_args(dict(commands=['/system resource print'], wait_for=wait_for, retries=2)): + self.execute_module(failed=True) self.assertEqual(self.run_commands.call_count, 2) def test_command_match_any(self): wait_for = ['result[0] contains "MikroTik"', 'result[0] contains "test string"'] - set_module_args(dict(commands=['/system resource print'], wait_for=wait_for, match='any')) - self.execute_module(changed=True) + with set_module_args(dict(commands=['/system resource print'], wait_for=wait_for, match='any')): + self.execute_module(changed=True) def test_command_match_all(self): wait_for = ['result[0] contains "MikroTik"', 'result[0] contains "RB1100"'] - set_module_args(dict(commands=['/system resource print'], wait_for=wait_for, match='all')) - self.execute_module(changed=True) + with set_module_args(dict(commands=['/system resource print'], wait_for=wait_for, match='all')): + self.execute_module(changed=True) def test_command_match_all_failure(self): wait_for = ['result[0] contains "MikroTik"', 'result[0] contains "test string"'] commands = ['/system resource print', '/system resource print'] - set_module_args(dict(commands=commands, wait_for=wait_for, match='all')) - self.execute_module(failed=True) + with set_module_args(dict(commands=commands, wait_for=wait_for, match='all')): + self.execute_module(failed=True) def test_command_wait_for_2(self): wait_for = 'result[0] contains "wireless"' - set_module_args(dict(commands=['/system package print'], wait_for=wait_for)) - self.execute_module(changed=True) + with set_module_args(dict(commands=['/system package print'], wait_for=wait_for)): + self.execute_module(changed=True) diff --git a/tests/unit/plugins/modules/test_facts.py b/tests/unit/plugins/modules/test_facts.py index 322fdda..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 @@ -39,8 +40,8 @@ class TestRouterosFactsModule(TestRouterosModule): self.run_commands.side_effect = load_from_file def test_facts_default(self): - set_module_args(dict(gather_subset='default')) - result = self.execute_module() + with set_module_args(dict(gather_subset='default')): + result = self.execute_module() self.assertEqual( result['ansible_facts']['ansible_net_hostname'], 'MikroTik' ) @@ -61,8 +62,8 @@ class TestRouterosFactsModule(TestRouterosModule): ) def test_facts_hardware(self): - set_module_args(dict(gather_subset='hardware')) - result = self.execute_module() + with set_module_args(dict(gather_subset='hardware')): + result = self.execute_module() self.assertEqual( result['ansible_facts']['ansible_net_spacefree_mb'], 64921.6 ) @@ -77,8 +78,8 @@ class TestRouterosFactsModule(TestRouterosModule): ) def test_facts_config(self): - set_module_args(dict(gather_subset='config')) - result = self.execute_module() + with set_module_args(dict(gather_subset='config')): + result = self.execute_module() self.assertIsInstance( result['ansible_facts']['ansible_net_config'], str ) @@ -88,8 +89,8 @@ class TestRouterosFactsModule(TestRouterosModule): ) def test_facts_interfaces(self): - set_module_args(dict(gather_subset='interfaces')) - result = self.execute_module() + with set_module_args(dict(gather_subset='interfaces')): + result = self.execute_module() self.assertIn( result['ansible_facts']['ansible_net_all_ipv4_addresses'][0], ['10.37.129.3', '10.37.0.0', '192.168.88.1'] ) @@ -118,8 +119,8 @@ class TestRouterosFactsModule(TestRouterosModule): self.assertEqual(result, None) def test_facts_routing(self): - set_module_args(dict(gather_subset='routing')) - result = self.execute_module() + with set_module_args(dict(gather_subset='routing')): + result = self.execute_module() self.assertIn( result['ansible_facts']['ansible_net_bgp_peer']['iBGP_BRAS.TYRMA']['name'], ['iBGP_BRAS.TYRMA'] ) diff --git a/tests/unit/plugins/modules/utils.py b/tests/unit/plugins/modules/utils.py deleted file mode 100644 index 419eef1..0000000 --- a/tests/unit/plugins/modules/utils.py +++ /dev/null @@ -1,54 +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 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 - - -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 - - args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) - basic._ANSIBLE_ARGS = to_bytes(args) - - -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)