diff options
Diffstat (limited to 'collections-debian-merged/ansible_collections/community/routeros/tests')
33 files changed, 1371 insertions, 0 deletions
diff --git a/collections-debian-merged/ansible_collections/community/routeros/tests/requirements.yml b/collections-debian-merged/ansible_collections/community/routeros/tests/requirements.yml new file mode 100644 index 00000000..a218740b --- /dev/null +++ b/collections-debian-merged/ansible_collections/community/routeros/tests/requirements.yml @@ -0,0 +1,4 @@ +integration_tests_dependencies: +- ansible.netcommon +unit_tests_dependencies: +- ansible.netcommon diff --git a/collections-debian-merged/ansible_collections/community/routeros/tests/sanity/ignore-2.10.txt b/collections-debian-merged/ansible_collections/community/routeros/tests/sanity/ignore-2.10.txt new file mode 100644 index 00000000..9d5934b3 --- /dev/null +++ b/collections-debian-merged/ansible_collections/community/routeros/tests/sanity/ignore-2.10.txt @@ -0,0 +1,5 @@ +plugins/modules/command.py validate-modules:doc-missing-type +plugins/modules/command.py validate-modules:parameter-list-no-elements +plugins/modules/command.py validate-modules:parameter-type-not-in-doc +plugins/modules/facts.py validate-modules:parameter-list-no-elements +plugins/modules/facts.py validate-modules:parameter-type-not-in-doc diff --git a/collections-debian-merged/ansible_collections/community/routeros/tests/sanity/ignore-2.11.txt b/collections-debian-merged/ansible_collections/community/routeros/tests/sanity/ignore-2.11.txt new file mode 100644 index 00000000..9d5934b3 --- /dev/null +++ b/collections-debian-merged/ansible_collections/community/routeros/tests/sanity/ignore-2.11.txt @@ -0,0 +1,5 @@ +plugins/modules/command.py validate-modules:doc-missing-type +plugins/modules/command.py validate-modules:parameter-list-no-elements +plugins/modules/command.py validate-modules:parameter-type-not-in-doc +plugins/modules/facts.py validate-modules:parameter-list-no-elements +plugins/modules/facts.py validate-modules:parameter-type-not-in-doc diff --git a/collections-debian-merged/ansible_collections/community/routeros/tests/sanity/ignore-2.9.txt b/collections-debian-merged/ansible_collections/community/routeros/tests/sanity/ignore-2.9.txt new file mode 100644 index 00000000..c1e40c7d --- /dev/null +++ b/collections-debian-merged/ansible_collections/community/routeros/tests/sanity/ignore-2.9.txt @@ -0,0 +1,3 @@ +plugins/modules/command.py validate-modules:doc-missing-type +plugins/modules/command.py validate-modules:parameter-type-not-in-doc +plugins/modules/facts.py validate-modules:parameter-type-not-in-doc diff --git a/collections-debian-merged/ansible_collections/community/routeros/tests/unit/compat/__init__.py b/collections-debian-merged/ansible_collections/community/routeros/tests/unit/compat/__init__.py new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/collections-debian-merged/ansible_collections/community/routeros/tests/unit/compat/__init__.py diff --git a/collections-debian-merged/ansible_collections/community/routeros/tests/unit/compat/builtins.py b/collections-debian-merged/ansible_collections/community/routeros/tests/unit/compat/builtins.py new file mode 100644 index 00000000..f60ee678 --- /dev/null +++ b/collections-debian-merged/ansible_collections/community/routeros/tests/unit/compat/builtins.py @@ -0,0 +1,33 @@ +# (c) 2014, Toshio Kuratomi <tkuratomi@ansible.com> +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see <http://www.gnu.org/licenses/>. + +# 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__ +except ImportError: + BUILTINS = 'builtins' +else: + BUILTINS = '__builtin__' diff --git a/collections-debian-merged/ansible_collections/community/routeros/tests/unit/compat/mock.py b/collections-debian-merged/ansible_collections/community/routeros/tests/unit/compat/mock.py new file mode 100644 index 00000000..0972cd2e --- /dev/null +++ b/collections-debian-merged/ansible_collections/community/routeros/tests/unit/compat/mock.py @@ -0,0 +1,122 @@ +# (c) 2014, Toshio Kuratomi <tkuratomi@ansible.com> +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see <http://www.gnu.org/licenses/>. + +# 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 * +except ImportError: + # Python 2 + # pylint: disable=wildcard-import,unused-wildcard-import + try: + from mock 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/collections-debian-merged/ansible_collections/community/routeros/tests/unit/compat/unittest.py b/collections-debian-merged/ansible_collections/community/routeros/tests/unit/compat/unittest.py new file mode 100644 index 00000000..98f08ad6 --- /dev/null +++ b/collections-debian-merged/ansible_collections/community/routeros/tests/unit/compat/unittest.py @@ -0,0 +1,38 @@ +# (c) 2014, Toshio Kuratomi <tkuratomi@ansible.com> +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see <http://www.gnu.org/licenses/>. + +# 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 * + except ImportError: + print('You need unittest2 installed on python2.6.x to run tests') +else: + from unittest import * diff --git a/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/__init__.py b/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/__init__.py new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/__init__.py diff --git a/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/fixtures/__init__.py b/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/fixtures/__init__.py new file mode 100644 index 00000000..e69de29b --- /dev/null +++ b/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/fixtures/__init__.py diff --git a/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/fixtures/facts/export_verbose b/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/fixtures/facts/export_verbose new file mode 100644 index 00000000..0f49fefe --- /dev/null +++ b/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/fixtures/facts/export_verbose @@ -0,0 +1,26 @@ +# sep/25/2018 10:10:52 by RouterOS 6.42.5 +# software id = 9EER-511K +# +# +# +/interface wireless security-profiles +set [ find default=yes ] supplicant-identity=MikroTik +/tool user-manager customer +set admin access=own-routers,own-users,own-profiles,own-limits,config-payment-gw +/ip address +add address=192.168.88.1/24 comment=defconf interface=ether1 network=192.168.88.0 +/ip dhcp-client +add dhcp-options=hostname,clientid disabled=no interface=ether1 +/system lcd +set contrast=0 enabled=no port=parallel type=24x4 +/system lcd page +set time disabled=yes display-time=5s +set resources disabled=yes display-time=5s +set uptime disabled=yes display-time=5s +set packets disabled=yes display-time=5s +set bits disabled=yes display-time=5s +set version disabled=yes display-time=5s +set identity disabled=yes display-time=5s +set ether1 disabled=yes display-time=5s +/tool user-manager database +set db-path=user-manager diff --git a/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/fixtures/facts/interface_print_detail_without-paging b/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/fixtures/facts/interface_print_detail_without-paging new file mode 100644 index 00000000..9ccddb29 --- /dev/null +++ b/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/fixtures/facts/interface_print_detail_without-paging @@ -0,0 +1,34 @@ +Flags: D - dynamic, X - disabled, R - running, S - slave + 0 R name="ether1" default-name="ether1" type="ether" mtu=1500 actual-mtu=1500 + mac-address=00:1C:42:36:52:90 last-link-up-time=sep/25/2018 06:30:04 + link-downs=0 + 1 R name="ether2" default-name="ether2" type="ether" mtu=1500 actual-mtu=1500 + mac-address=00:1C:42:36:52:91 last-link-up-time=sep/25/2018 06:30:04 + link-downs=0 + 2 R name="ether3" default-name="ether3" type="ether" mtu=1500 actual-mtu=1500 + mac-address=00:1C:42:36:52:92 last-link-up-time=sep/25/2018 06:30:04 + link-downs=0 + 3 R name="ether4" default-name="ether4" type="ether" mtu=1500 actual-mtu=1500 + mac-address=00:1C:42:36:52:93 last-link-up-time=sep/25/2018 06:30:04 + link-downs=0 + 4 R name="ether5" default-name="ether5" type="ether" mtu=1500 actual-mtu=1500 + mac-address=00:1C:42:36:52:94 last-link-up-time=sep/25/2018 06:30:04 + link-downs=0 + 5 R name="ether6" default-name="ether6" type="ether" mtu=1500 actual-mtu=1500 + mac-address=00:1C:42:36:52:95 last-link-up-time=sep/25/2018 06:30:04 + link-downs=0 + 6 R name="ether7" default-name="ether7" type="ether" mtu=1500 actual-mtu=1500 + mac-address=00:1C:42:36:52:96 last-link-up-time=sep/25/2018 06:30:04 + link-downs=0 + 7 R name="ether8" default-name="ether8" type="ether" mtu=1500 actual-mtu=1500 + mac-address=00:1C:42:36:52:97 last-link-up-time=sep/25/2018 06:30:04 + link-downs=0 + 8 R name="ether9" default-name="ether9" type="ether" mtu=1500 actual-mtu=1500 + mac-address=00:1C:42:36:52:98 last-link-up-time=sep/25/2018 06:30:04 + link-downs=0 + 9 R name="ether10" default-name="ether10" type="ether" mtu=1500 actual-mtu=1500 + mac-address=00:1C:42:36:52:99 last-link-up-time=sep/25/2018 06:30:04 + link-downs=0 +10 R name="pppoe" default-name="pppoe" type="ppp" mtu=1500 actual-mtu=1500 + mac-address=00:1C:42:36:52:00 last-link-up-time=sep/25/2018 06:30:04 + link-downs=0 diff --git a/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/fixtures/facts/ip_address_print_detail_without-paging b/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/fixtures/facts/ip_address_print_detail_without-paging new file mode 100644 index 00000000..d4fd2bcd --- /dev/null +++ b/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/fixtures/facts/ip_address_print_detail_without-paging @@ -0,0 +1,10 @@ +Flags: X - disabled, I - invalid, D - dynamic + 0 ;;; defconf + address=192.168.88.1/24 network=192.168.88.0 interface=ether1 + actual-interface=ether1 + + 1 D address=10.37.129.3/24 network=10.37.129.0 interface=ether1 + actual-interface=ether1 + + 2 D address=10.37.0.0/24 network=10.37.0.1 interface=pppoe + actual-interface=pppoe diff --git a/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/fixtures/facts/ip_neighbor_print_detail_without-paging b/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/fixtures/facts/ip_neighbor_print_detail_without-paging new file mode 100644 index 00000000..906dfb75 --- /dev/null +++ b/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/fixtures/facts/ip_neighbor_print_detail_without-paging @@ -0,0 +1,15 @@ + 0 interface=ether2-master address=10.37.129.3 address4=10.37.129.3 mac-address=D4:CA:6D:C6:16:4C identity="router1" platform="MikroTik" version="6.42.2 (stable)" unpack=none age=59s + uptime=3w19h11m36s software-id="1234-1234" board="RBwAPG-5HacT2HnD" interface-name="bridge" system-description="MikroTik RouterOS 6.42.2 (stable) RBwAPG-5HacT2HnD" + system-caps="" system-caps-enabled="" + + 1 interface=ether3 address=10.37.129.4 address4=10.37.129.4 mac-address=D4:CA:6D:C6:18:2F identity="router2" platform="MikroTik" version="6.42.2 (stable)" unpack=none age=54s + uptime=3w19h11m30s software-id="1234-1234" board="RBwAPG-5HacT2HnD" ipv6=no interface-name="bridge" system-description="MikroTik RouterOS 6.42.2 (stable) RBwAPG-5HacT2HnD" + system-caps="" system-caps-enabled="" + + 2 interface=ether5 address=10.37.129.5 address4=10.37.129.5 mac-address=B8:69:F4:37:F0:C8 identity="router3" platform="MikroTik" version="6.40.8 (bugfix)" unpack=none age=43s + uptime=3d14h25m31s software-id="1234-1234" board="RB960PGS" interface-name="ether1" system-description="MikroTik RouterOS 6.40.8 (bugfix) RB960PGS" system-caps="" + system-caps-enabled="" + + 3 interface=ether10 address=10.37.129.6 address4=10.37.129.6 mac-address=6C:3B:6B:A1:0B:63 identity="router4" platform="MikroTik" version="6.42.2 (stable)" unpack=none age=54s + uptime=3w6d1h11m44s software-id="1234-1234" board="RBSXTLTE3-7" interface-name="bridge" system-description="MikroTik RouterOS 6.42.2 (stable) RBSXTLTE3-7" system-caps="" + system-caps-enabled="" diff --git a/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/fixtures/facts/ip_route_print_detail_without-paging b/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/fixtures/facts/ip_route_print_detail_without-paging new file mode 100644 index 00000000..6c2e558e --- /dev/null +++ b/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/fixtures/facts/ip_route_print_detail_without-paging @@ -0,0 +1,19 @@ +Flags: X - disabled, A - active, D - dynamic, +C - connect, S - static, r - rip, b - bgp, o - ospf, m - mme, +B - blackhole, U - unreachable, P - prohibit + 0 ADC dst-address=10.10.66.0/30 pref-src=10.10.66.1 gateway=bridge1 + gateway-status=bridge1 reachable distance=0 scope=10 + routing-mark=altegro + + 2 A S dst-address=0.0.0.0/0 gateway=85.15.75.109 + gateway-status=85.15.75.109 reachable via Internet-VTK distance=1 + scope=30 target-scope=10 + + 3 ADC dst-address=10.10.1.0/30 pref-src=10.10.1.1 gateway=GRE_TYRMA + gateway-status=GRE_TYRMA reachable distance=0 scope=10 + + 4 DC dst-address=10.10.1.4/30 pref-src=10.10.1.5 gateway=RB2011 + gateway-status=RB2011 unreachable distance=255 scope=10 + + 5 ADC dst-address=10.10.2.0/30 pref-src=10.10.2.1 gateway=VLAN_SAT.ROUTER + gateway-status=VLAN_SAT.ROUTER reachable distance=0 scope=10 diff --git a/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/fixtures/facts/ipv6_address_print_detail_without-paging b/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/fixtures/facts/ipv6_address_print_detail_without-paging new file mode 100644 index 00000000..c18e9ea5 --- /dev/null +++ b/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/fixtures/facts/ipv6_address_print_detail_without-paging @@ -0,0 +1,3 @@ +Flags: X - disabled, I - invalid, D - dynamic, G - global, L - link-local + 0 DL address=fe80::21c:42ff:fe36:5290/64 from-pool="" interface=ether1 + actual-interface=ether1 eui-64=no advertise=no no-dad=no diff --git a/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/fixtures/facts/ipv6_address_print_detail_without-paging_no-ipv6 b/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/fixtures/facts/ipv6_address_print_detail_without-paging_no-ipv6 new file mode 100644 index 00000000..0ea34bc0 --- /dev/null +++ b/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/fixtures/facts/ipv6_address_print_detail_without-paging_no-ipv6 @@ -0,0 +1 @@ +bad command name address (line 1 column 7) diff --git a/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/fixtures/facts/routing_bgp_instance_print_detail_without-paging b/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/fixtures/facts/routing_bgp_instance_print_detail_without-paging new file mode 100644 index 00000000..8c560e2a --- /dev/null +++ b/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/fixtures/facts/routing_bgp_instance_print_detail_without-paging @@ -0,0 +1,10 @@ +Flags: * - default, X - disabled + 0 *X name="default" as=65530 router-id=0.0.0.0 redistribute-connected=no + redistribute-static=no redistribute-rip=no redistribute-ospf=no + redistribute-other-bgp=no out-filter="" client-to-client-reflection=yes + ignore-as-path-len=no routing-table="" + + 1 name="MAIN_AS_STARKDV" as=64520 router-id=10.10.50.1 + redistribute-connected=no redistribute-static=no redistribute-rip=no + redistribute-ospf=no redistribute-other-bgp=no out-filter="" + client-to-client-reflection=yes ignore-as-path-len=no routing-table="" diff --git a/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/fixtures/facts/routing_bgp_peer_print_detail_without-paging b/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/fixtures/facts/routing_bgp_peer_print_detail_without-paging new file mode 100644 index 00000000..1ae4f5bc --- /dev/null +++ b/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/fixtures/facts/routing_bgp_peer_print_detail_without-paging @@ -0,0 +1,13 @@ +Flags: X - disabled, E - established + 0 E name="iBGP_BRAS.TYRMA" instance=MAIN_AS_STARKDV remote-address=10.10.100.1 + remote-as=64520 tcp-md5-key="" nexthop-choice=default multihop=no + route-reflect=yes hold-time=3m ttl=default in-filter="" out-filter="" + address-families=ip,l2vpn,vpnv4 update-source=LAN_KHV + default-originate=never remove-private-as=no as-override=no passive=no + use-bfd=yes + + 1 E name="iBGP_BRAS_SAT" instance=MAIN_AS_STARKDV remote-address=10.10.50.230 + remote-as=64520 tcp-md5-key="" nexthop-choice=default multihop=no + route-reflect=yes hold-time=3m ttl=default in-filter="" out-filter="" + address-families=ip default-originate=never remove-private-as=no + as-override=no passive=no use-bfd=yes diff --git a/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/fixtures/facts/routing_bgp_vpnv4-route_print_detail_without-paging b/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/fixtures/facts/routing_bgp_vpnv4-route_print_detail_without-paging new file mode 100644 index 00000000..f64fa6d0 --- /dev/null +++ b/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/fixtures/facts/routing_bgp_vpnv4-route_print_detail_without-paging @@ -0,0 +1,7 @@ +Flags: L - label-present + 0 L route-distinguisher=64520:666 dst-address=10.10.66.8/30 gateway=10.10.100.1 + interface=GRE_TYRMA in-label=6136 out-label=6136 bgp-local-pref=100 + bgp-origin=incomplete bgp-ext-communities="RT:64520:666" + + 1 L route-distinguisher=64520:666 dst-address=10.10.66.0/30 interface=bridge1 + in-label=1790 bgp-ext-communities="RT:64520:666" diff --git a/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/fixtures/facts/routing_ospf_instance_print_detail_without-paging b/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/fixtures/facts/routing_ospf_instance_print_detail_without-paging new file mode 100644 index 00000000..96404663 --- /dev/null +++ b/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/fixtures/facts/routing_ospf_instance_print_detail_without-paging @@ -0,0 +1,10 @@ +Flags: X - disabled, * - default + 0 * name="default" router-id=10.10.50.1 distribute-default=never redistribute-connected=no + redistribute-static=no redistribute-rip=no redistribute-bgp=no redistribute-other-ospf=no + metric-default=1 metric-connected=20 metric-static=20 metric-rip=20 metric-bgp=auto + metric-other-ospf=auto in-filter=ospf-in out-filter=ospf-out + + 1 name="OSPF_ALTEGRO" router-id=10.10.66.1 distribute-default=never redistribute-connected=no + redistribute-static=no redistribute-rip=no redistribute-bgp=no redistribute-other-ospf=no + metric-default=1 metric-connected=20 metric-static=20 metric-rip=20 metric-bgp=auto + metric-other-ospf=auto in-filter=ospf-in out-filter=ospf-out routing-table=altegro diff --git a/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/fixtures/facts/routing_ospf_neighbor_print_detail_without-paging b/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/fixtures/facts/routing_ospf_neighbor_print_detail_without-paging new file mode 100644 index 00000000..d683b252 --- /dev/null +++ b/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/fixtures/facts/routing_ospf_neighbor_print_detail_without-paging @@ -0,0 +1,3 @@ +0 instance=default router-id=10.10.100.1 address=10.10.1.2 interface=GRE_TYRMA priority=1 + dr-address=0.0.0.0 backup-dr-address=0.0.0.0 state="Full" state-changes=15 ls-retransmits=0 + ls-requests=0 db-summaries=0 adjacency=6h8m46s diff --git a/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/fixtures/facts/system_identity_print_without-paging b/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/fixtures/facts/system_identity_print_without-paging new file mode 100644 index 00000000..d7dc3ff3 --- /dev/null +++ b/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/fixtures/facts/system_identity_print_without-paging @@ -0,0 +1 @@ + name: MikroTik diff --git a/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/fixtures/facts/system_resource_print_without-paging b/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/fixtures/facts/system_resource_print_without-paging new file mode 100644 index 00000000..79353f79 --- /dev/null +++ b/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/fixtures/facts/system_resource_print_without-paging @@ -0,0 +1,16 @@ + uptime: 3h28m52s + version: 6.42.5 (stable) + build-time: Jun/26/2018 12:12:08 + free-memory: 988.3MiB + total-memory: 1010.8MiB + cpu: Intel(R) + cpu-count: 2 + cpu-frequency: 2496MHz + cpu-load: 0% + free-hdd-space: 63.4GiB + total-hdd-space: 63.5GiB + write-sect-since-reboot: 4576 + write-sect-total: 4576 + architecture-name: x86 + board-name: x86 + platform: MikroTik diff --git a/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/fixtures/facts/system_routerboard_print_without-paging b/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/fixtures/facts/system_routerboard_print_without-paging new file mode 100644 index 00000000..263c9590 --- /dev/null +++ b/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/fixtures/facts/system_routerboard_print_without-paging @@ -0,0 +1,7 @@ + routerboard: yes + model: RouterBOARD 3011UiAS + serial-number: 1234567890 + firmware-type: ipq8060 + factory-firmware: 3.41 + current-firmware: 3.41 + upgrade-firmware: 6.42.2 diff --git a/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/fixtures/system_package_print b/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/fixtures/system_package_print new file mode 100644 index 00000000..3f806211 --- /dev/null +++ b/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/fixtures/system_package_print @@ -0,0 +1,106 @@ + + + + + + + + + + + + + + + + + MMM MMM KKK TTTTTTTTTTT KKK + + MMMM MMMM KKK TTTTTTTTTTT KKK + + MMM MMMM MMM III KKK KKK RRRRRR OOOOOO TTT III KKK KKK + + MMM MM MMM III KKKKK RRR RRR OOO OOO TTT III KKKKK + + MMM MMM III KKK KKK RRRRRR OOO OOO TTT III KKK KKK + + MMM MMM III KKK KKK RRR RRR OOOOOO TTT III KKK KKK + + + + MikroTik RouterOS 6.42.5 (c) 1999-2018 http://www.mikrotik.com/ + + +[?] Gives the list of available commands + +command [?] Gives help on the command and list of arguments + + + +[Tab] Completes the command/word. If the input is ambiguous, + + a second [Tab] gives possible options + + + +/ Move up to base level + +.. Move up one level + +/command Use command at the base level + +[9999B +[9999BZ [6n<[c[4l[20l[?47l[?7h[?5l[?25h[H[9999B[6n + + + +[admin@MainRouter] > +[admin@MainRouter] > /system routerboard print +[admin@MainRouter] > /system routerboard print + + routerboard: yes + model: 750GL + serial-number: 1234567890AB + firmware-type: ar7240 + factory-firmware: 3.09 + current-firmware: 6.41.2 + upgrade-firmware: 6.42.5 + + + + + +[admin@MainRouter] > +[admin@MainRouter] > /system identity print +[admin@MainRouter] > /system identity print + + name: MikroTik + + + + + +[admin@MainRouter] > +[admin@MainRouter] > /system package print +[admin@MainRouter] > /system package print + +Flags: [m[1mX[m - disabled +[m[1m # NAME VERSION SCHEDULED +[m 0 routeros-mipsbe 6.42.5 + 1 system 6.42.5 + 2 ipv6 6.42.5 + 3 wireless 6.42.5 + 4 hotspot 6.42.5 + 5 dhcp 6.42.5 + 6 mpls 6.42.5 + 7 routing 6.42.5 + 8 ppp 6.42.5 + 9 security 6.42.5 +10 advanced-tools 6.42.5 + + + + + +[admin@MainRouter] > +[admin@MainRouter] >
\ No newline at end of file diff --git a/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/fixtures/system_resource_print b/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/fixtures/system_resource_print new file mode 100644 index 00000000..63bc3beb --- /dev/null +++ b/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/fixtures/system_resource_print @@ -0,0 +1,17 @@ +[admin@RB1100test] /system resource> print + uptime: 2w1d23h34m57s + version: "5.0rc1" + free-memory: 385272KiB + total-memory: 516708KiB + cpu: "e500v2" + cpu-count: 1 + cpu-frequency: 799MHz + cpu-load: 9% + free-hdd-space: 466328KiB + total-hdd-space: 520192KiB + write-sect-since-reboot: 1411 + write-sect-total: 70625 + bad-blocks: 0.2% + architecture-name: "powerpc" + board-name: "RB1100" + platform: "MikroTik" diff --git a/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/routeros_module.py b/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/routeros_module.py new file mode 100644 index 00000000..9cad18e3 --- /dev/null +++ b/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/routeros_module.py @@ -0,0 +1,88 @@ +# (c) 2016 Red Hat Inc. +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see <http://www.gnu.org/licenses/>. + +# Make coding more python3-ish +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + +import os +import json + +from ansible_collections.community.routeros.tests.unit.plugins.modules.utils import AnsibleExitJson, AnsibleFailJson, ModuleTestCase + + +fixture_path = os.path.join(os.path.dirname(__file__), 'fixtures') +fixture_data = {} + + +def load_fixture(name): + path = os.path.join(fixture_path, name) + + if path in fixture_data: + return fixture_data[path] + + with open(path) as f: + data = f.read() + + try: + data = json.loads(data) + except Exception: + pass + + fixture_data[path] = data + return data + + +class TestRouterosModule(ModuleTestCase): + + def execute_module(self, failed=False, changed=False, commands=None, sort=True, defaults=False): + + self.load_fixtures(commands) + + if failed: + result = self.failed() + self.assertTrue(result['failed'], result) + else: + result = self.changed(changed) + self.assertEqual(result['changed'], changed, result) + + if commands is not None: + if sort: + self.assertEqual(sorted(commands), sorted(result['commands']), result['commands']) + else: + self.assertEqual(commands, result['commands'], result['commands']) + + return result + + def failed(self): + with self.assertRaises(AnsibleFailJson) as exc: + self.module.main() + + result = exc.exception.args[0] + self.assertTrue(result['failed'], result) + return result + + def changed(self, changed=False): + with self.assertRaises(AnsibleExitJson) as exc: + self.module.main() + + result = exc.exception.args[0] + self.assertEqual(result['changed'], changed, result) + return result + + def load_fixtures(self, commands=None): + pass diff --git a/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/test_api.py b/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/test_api.py new file mode 100644 index 00000000..1befeff6 --- /dev/null +++ b/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/test_api.py @@ -0,0 +1,266 @@ +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see <http://www.gnu.org/licenses/>. + +# Make coding more python3-ish +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + +import json +import pytest + +from ansible_collections.community.routeros.tests.unit.compat.mock import patch, MagicMock +from ansible_collections.community.routeros.tests.unit.plugins.modules.utils import set_module_args, basic, AnsibleExitJson, AnsibleFailJson, ModuleTestCase +from ansible_collections.community.routeros.plugins.modules import api + + +class AnsibleExitJson(Exception): + """Exception class to be raised by module.exit_json and caught by the test case""" + pass + + +class AnsibleFailJson(Exception): + """Exception class to be raised by module.fail_json and caught by the test case""" + pass + + +def exit_json(*args, **kwargs): + """function to patch over exit_json; package return data into an exception""" + if 'changed' not in kwargs: + kwargs['changed'] = False + raise AnsibleExitJson(kwargs) + + +def fail_json(*args, **kwargs): + """function to patch over fail_json; package return data into an exception""" + kwargs['failed'] = True + raise AnsibleFailJson(kwargs) + + +# fixtures +class fake_ros_api: + def __init__(self, api, path): + pass + + def path(self, api, path): + fake_bridge = [{".id": "*DC", "name": "b2", "mtu": "auto", "actual-mtu": 1500, + "l2mtu": 65535, "arp": "enabled", "arp-timeout": "auto", + "mac-address": "3A:C1:90:D6:E8:44", "protocol-mode": "rstp", + "fast-forward": "true", "igmp-snooping": "false", + "auto-mac": "true", "ageing-time": "5m", "priority": + "0x8000", "max-message-age": "20s", "forward-delay": "15s", + "transmit-hold-count": 6, "vlan-filtering": "false", + "dhcp-snooping": "false", "running": "true", "disabled": "false"}] + return fake_bridge + + def arbitrary(self, api, path): + def retr(self, *args, **kwargs): + if 'name' not in kwargs.keys(): + raise TrapError(message="no such command") + dummy_test_string = '/interface/bridge add name=unit_test_brige_arbitrary' + result = "/%s/%s add name=%s" % (path[0], path[1], kwargs['name']) + return [result] + return retr + + def add(self, name): + if name == "unit_test_brige_exist": + raise TrapError + return '*A1' + + def remove(self, id): + if id != "*A1": + raise TrapError(message="no such item (4)") + return '*A1' + + def update(self, **kwargs): + if kwargs['.id'] != "*A1" or 'name' not in kwargs.keys(): + raise TrapError(message="no such item (4)") + return ["updated: {'.id': '%s' % kwargs['.id'], 'name': '%s' % kwargs['name']}"] + + def select(self, *args): + dummy_bridge = [{".id": "*A1", "name": "dummy_bridge_A1"}, + {".id": "*A2", "name": "dummy_bridge_A2"}, + {".id": "*A3", "name": "dummy_bridge_A3"}] + + result = [] + for dummy in dummy_bridge: + found = {} + for search in args: + if search in dummy.keys(): + found[search] = dummy[search] + else: + continue + if len(found.keys()) == 2: + result.append(found) + + if result: + return result + else: + return ["no results for 'interface bridge 'query' %s" % ' '.join(args)] + + def select_where(self, api, path): + api_path = Where() + return api_path + + +class Where: + def __init__(self): + pass + + def select(self, *args): + return self + + def where(self, *args): + return ["*A1"] + + +class TrapError(Exception): + def __init__(self, message="failure: already have interface with such name"): + self.message = message + super().__init__(self.message) + + +class Key: + def __init__(self, name): + self.name = name + self.str_return() + + def str_return(self): + return str(self.name) + + +class TestRouterosApiModule(ModuleTestCase): + + def setUp(self): + librouteros = pytest.importorskip("librouteros") + self.module = api + self.module.connect = MagicMock(new=fake_ros_api) + self.module.Key = MagicMock(new=Key) + self.config_module_args = {"username": "admin", + "password": "pаss", + "hostname": "127.0.0.1", + "path": "interface bridge"} + + self.mock_module_helper = patch.multiple(basic.AnsibleModule, + exit_json=exit_json, + fail_json=fail_json) + self.mock_module_helper.start() + self.addCleanup(self.mock_module_helper.stop) + + def test_module_fail_when_required_args_missing(self): + with self.assertRaises(AnsibleFailJson): + set_module_args({}) + self.module.main() + + @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): + set_module_args(self.config_module_args) + self.module.main() + + @patch('ansible_collections.community.routeros.plugins.modules.api.ROS_api_module.api_add_path', new=fake_ros_api.arbitrary) + def test_api_add(self): + with self.assertRaises(AnsibleExitJson): + module_args = self.config_module_args.copy() + module_args['add'] = "name=unit_test_brige" + set_module_args(module_args) + self.module.main() + + @patch('ansible_collections.community.routeros.plugins.modules.api.ROS_api_module.api_add_path', new=fake_ros_api) + def test_api_add_already_exist(self): + with self.assertRaises(AnsibleExitJson): + module_args = self.config_module_args.copy() + module_args['add'] = "name=unit_test_brige_exist" + set_module_args(module_args) + self.module.main() + + @patch('ansible_collections.community.routeros.plugins.modules.api.ROS_api_module.api_add_path', new=fake_ros_api) + def test_api_remove(self): + with self.assertRaises(AnsibleExitJson): + module_args = self.config_module_args.copy() + module_args['remove'] = "*A1" + set_module_args(module_args) + self.module.main() + + @patch('ansible_collections.community.routeros.plugins.modules.api.ROS_api_module.api_add_path', new=fake_ros_api) + def test_api_remove_no_id(self): + with self.assertRaises(AnsibleExitJson): + module_args = self.config_module_args.copy() + module_args['remove'] = "*A2" + set_module_args(module_args) + self.module.main() + + @patch('ansible_collections.community.routeros.plugins.modules.api.ROS_api_module.api_add_path', new=fake_ros_api.arbitrary) + def test_api_cmd(self): + with self.assertRaises(AnsibleExitJson): + module_args = self.config_module_args.copy() + module_args['cmd'] = "add name=unit_test_brige_arbitrary" + set_module_args(module_args) + self.module.main() + + @patch('ansible_collections.community.routeros.plugins.modules.api.ROS_api_module.api_add_path', new=fake_ros_api.arbitrary) + def test_api_cmd_none_existing_cmd(self): + with self.assertRaises(AnsibleExitJson): + 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() + + @patch('ansible_collections.community.routeros.plugins.modules.api.ROS_api_module.api_add_path', new=fake_ros_api) + def test_api_update(self): + with self.assertRaises(AnsibleExitJson): + module_args = self.config_module_args.copy() + module_args['update'] = ".id=*A1 name=unit_test_brige" + set_module_args(module_args) + self.module.main() + + @patch('ansible_collections.community.routeros.plugins.modules.api.ROS_api_module.api_add_path', new=fake_ros_api) + def test_api_update_none_existing_id(self): + with self.assertRaises(AnsibleExitJson): + module_args = self.config_module_args.copy() + module_args['update'] = ".id=*A2 name=unit_test_brige" + set_module_args(module_args) + self.module.main() + + @patch('ansible_collections.community.routeros.plugins.modules.api.ROS_api_module.api_add_path', new=fake_ros_api) + def test_api_query(self): + with self.assertRaises(AnsibleExitJson): + module_args = self.config_module_args.copy() + module_args['query'] = ".id name" + set_module_args(module_args) + self.module.main() + + @patch('ansible_collections.community.routeros.plugins.modules.api.ROS_api_module.api_add_path', new=fake_ros_api) + def test_api_query_missing_key(self): + with self.assertRaises(AnsibleExitJson): + module_args = self.config_module_args.copy() + module_args['query'] = ".id other" + set_module_args(module_args) + self.module.main() + + @patch('ansible_collections.community.routeros.plugins.modules.api.ROS_api_module.api_add_path', new=fake_ros_api.select_where) + def test_api_query_and_WHERE(self): + with self.assertRaises(AnsibleExitJson): + 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() + + @patch('ansible_collections.community.routeros.plugins.modules.api.ROS_api_module.api_add_path', new=fake_ros_api.select_where) + def test_api_query_and_WHERE_no_cond(self): + with self.assertRaises(AnsibleExitJson): + 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() diff --git a/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/test_command.py b/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/test_command.py new file mode 100644 index 00000000..ab045fe5 --- /dev/null +++ b/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/test_command.py @@ -0,0 +1,113 @@ +# (c) 2016 Red Hat Inc. +# +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see <http://www.gnu.org/licenses/>. + +# Make coding more python3-ish +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + +import json + +from ansible_collections.community.routeros.tests.unit.compat.mock import patch +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 + + +class TestRouterosCommandModule(TestRouterosModule): + + module = command + + def setUp(self): + super(TestRouterosCommandModule, self).setUp() + + self.mock_run_commands = patch('ansible_collections.community.routeros.plugins.modules.command.run_commands') + self.run_commands = self.mock_run_commands.start() + + def tearDown(self): + super(TestRouterosCommandModule, self).tearDown() + self.mock_run_commands.stop() + + def load_fixtures(self, commands=None): + + def load_from_file(*args, **kwargs): + module, commands = args + output = list() + + for item in commands: + try: + obj = json.loads(item) + command = obj + except ValueError: + command = item + filename = str(command).replace(' ', '_').replace('/', '') + output.append(load_fixture(filename)) + return output + + 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() + 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() + 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() + + 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) + 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) + 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() + + 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() + + 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) + + 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() diff --git a/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/test_facts.py b/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/test_facts.py new file mode 100644 index 00000000..25fb04e2 --- /dev/null +++ b/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/test_facts.py @@ -0,0 +1,342 @@ +# This file is part of Ansible +# +# Ansible is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. +# +# Ansible is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Ansible. If not, see <http://www.gnu.org/licenses/>. + +# Make coding more python3-ish +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.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 + + +class TestRouterosFactsModule(TestRouterosModule): + + module = facts + + def setUp(self): + super(TestRouterosFactsModule, self).setUp() + self.mock_run_commands = patch('ansible_collections.community.routeros.plugins.modules.facts.run_commands') + self.run_commands = self.mock_run_commands.start() + + def tearDown(self): + super(TestRouterosFactsModule, self).tearDown() + self.mock_run_commands.stop() + + def load_fixtures(self, commands=None): + def load_from_file(*args, **kwargs): + module = args + commands = kwargs['commands'] + output = list() + + for command in commands: + filename = str(command).split(' | ')[0].replace(' ', '_') + output.append(load_fixture('facts%s' % filename)) + return output + + self.run_commands.side_effect = load_from_file + + def test_facts_default(self): + set_module_args(dict(gather_subset='default')) + result = self.execute_module() + self.assertEqual( + result['ansible_facts']['ansible_net_hostname'], 'MikroTik' + ) + self.assertEqual( + result['ansible_facts']['ansible_net_version'], '6.42.5 (stable)' + ) + self.assertEqual( + result['ansible_facts']['ansible_net_model'], 'RouterBOARD 3011UiAS' + ) + self.assertEqual( + result['ansible_facts']['ansible_net_serialnum'], '1234567890' + ) + self.assertEqual( + result['ansible_facts']['ansible_net_arch'], 'x86' + ) + self.assertEqual( + result['ansible_facts']['ansible_net_uptime'], '3h28m52s' + ) + + def test_facts_hardware(self): + set_module_args(dict(gather_subset='hardware')) + result = self.execute_module() + self.assertEqual( + result['ansible_facts']['ansible_net_spacefree_mb'], 64921.6 + ) + self.assertEqual( + result['ansible_facts']['ansible_net_spacetotal_mb'], 65024.0 + ) + self.assertEqual( + result['ansible_facts']['ansible_net_memfree_mb'], 988.3 + ) + self.assertEqual( + result['ansible_facts']['ansible_net_memtotal_mb'], 1010.8 + ) + + def test_facts_config(self): + set_module_args(dict(gather_subset='config')) + result = self.execute_module() + self.assertIsInstance( + result['ansible_facts']['ansible_net_config'], str + ) + + def test_facts_interfaces(self): + 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'] + ) + self.assertEqual( + result['ansible_facts']['ansible_net_all_ipv6_addresses'], ['fe80::21c:42ff:fe36:5290'] + ) + self.assertEqual( + result['ansible_facts']['ansible_net_all_ipv6_addresses'][0], + result['ansible_facts']['ansible_net_interfaces']['ether1']['ipv6'][0]['address'] + ) + self.assertEqual( + len(result['ansible_facts']['ansible_net_interfaces'].keys()), 11 + ) + self.assertEqual( + len(result['ansible_facts']['ansible_net_neighbors']), 4 + ) + + def test_facts_interfaces_no_ipv6(self): + fixture = load_fixture( + 'facts/ipv6_address_print_detail_without-paging_no-ipv6' + ) + interfaces = self.module.Interfaces(module=self.module) + addresses = interfaces.parse_detail(data=fixture) + result = interfaces.populate_addresses(data=addresses, family='ipv6') + + self.assertEqual(result, None) + + def test_facts_routing(self): + 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'] + ) + self.assertIn( + result['ansible_facts']['ansible_net_bgp_peer']['iBGP_BRAS.TYRMA']['instance'], ['MAIN_AS_STARKDV'] + ) + self.assertIn( + result['ansible_facts']['ansible_net_bgp_peer']['iBGP_BRAS.TYRMA']['remote-address'], ['10.10.100.1'] + ) + self.assertIn( + result['ansible_facts']['ansible_net_bgp_peer']['iBGP_BRAS.TYRMA']['remote-as'], ['64520'] + ) + self.assertIn( + result['ansible_facts']['ansible_net_bgp_peer']['iBGP_BRAS.TYRMA']['nexthop-choice'], ['default'] + ) + self.assertIn( + result['ansible_facts']['ansible_net_bgp_peer']['iBGP_BRAS.TYRMA']['multihop'], ['no'] + ) + self.assertIn( + result['ansible_facts']['ansible_net_bgp_peer']['iBGP_BRAS.TYRMA']['route-reflect'], ['yes'] + ) + self.assertIn( + result['ansible_facts']['ansible_net_bgp_peer']['iBGP_BRAS.TYRMA']['hold-time'], ['3m'] + ) + self.assertIn( + result['ansible_facts']['ansible_net_bgp_peer']['iBGP_BRAS.TYRMA']['ttl'], ['default'] + ) + self.assertIn( + result['ansible_facts']['ansible_net_bgp_peer']['iBGP_BRAS.TYRMA']['address-families'], ['ip'] + ) + self.assertIn( + result['ansible_facts']['ansible_net_bgp_peer']['iBGP_BRAS.TYRMA']['update-source'], ['LAN_KHV'] + ) + self.assertIn( + result['ansible_facts']['ansible_net_bgp_peer']['iBGP_BRAS.TYRMA']['default-originate'], ['never'] + ) + self.assertIn( + result['ansible_facts']['ansible_net_bgp_peer']['iBGP_BRAS.TYRMA']['remove-private-as'], ['no'] + ) + self.assertIn( + result['ansible_facts']['ansible_net_bgp_peer']['iBGP_BRAS.TYRMA']['as-override'], ['no'] + ) + self.assertIn( + result['ansible_facts']['ansible_net_bgp_peer']['iBGP_BRAS.TYRMA']['passive'], ['no'] + ) + self.assertIn( + result['ansible_facts']['ansible_net_bgp_peer']['iBGP_BRAS.TYRMA']['use-bfd'], ['yes'] + ) + self.assertIn( + result['ansible_facts']['ansible_net_bgp_vpnv4_route']['GRE_TYRMA']['route-distinguisher'], ['64520:666'] + ) + self.assertIn( + result['ansible_facts']['ansible_net_bgp_vpnv4_route']['GRE_TYRMA']['dst-address'], ['10.10.66.8/30'] + ) + self.assertIn( + result['ansible_facts']['ansible_net_bgp_vpnv4_route']['GRE_TYRMA']['gateway'], ['10.10.100.1'] + ) + self.assertIn( + result['ansible_facts']['ansible_net_bgp_vpnv4_route']['GRE_TYRMA']['interface'], ['GRE_TYRMA'] + ) + self.assertIn( + result['ansible_facts']['ansible_net_bgp_vpnv4_route']['GRE_TYRMA']['in-label'], ['6136'] + ) + self.assertIn( + result['ansible_facts']['ansible_net_bgp_vpnv4_route']['GRE_TYRMA']['out-label'], ['6136'] + ) + self.assertIn( + result['ansible_facts']['ansible_net_bgp_vpnv4_route']['GRE_TYRMA']['bgp-local-pref'], ['100'] + ) + self.assertIn( + result['ansible_facts']['ansible_net_bgp_vpnv4_route']['GRE_TYRMA']['bgp-origin'], ['incomplete'] + ) + self.assertIn( + result['ansible_facts']['ansible_net_bgp_vpnv4_route']['GRE_TYRMA']['bgp-ext-communities'], ['RT:64520:666'] + ) + self.assertIn( + result['ansible_facts']['ansible_net_bgp_instance']['default']['name'], ['default'] + ) + self.assertIn( + result['ansible_facts']['ansible_net_bgp_instance']['default']['as'], ['65530'] + ) + self.assertIn( + result['ansible_facts']['ansible_net_bgp_instance']['default']['router-id'], ['0.0.0.0'] + ) + self.assertIn( + result['ansible_facts']['ansible_net_bgp_instance']['default']['redistribute-connected'], ['no'] + ) + self.assertIn( + result['ansible_facts']['ansible_net_bgp_instance']['default']['redistribute-static'], ['no'] + ) + self.assertIn( + result['ansible_facts']['ansible_net_bgp_instance']['default']['redistribute-rip'], ['no'] + ) + self.assertIn( + result['ansible_facts']['ansible_net_bgp_instance']['default']['redistribute-ospf'], ['no'] + ) + self.assertIn( + result['ansible_facts']['ansible_net_bgp_instance']['default']['redistribute-other-bgp'], ['no'] + ) + self.assertIn( + result['ansible_facts']['ansible_net_bgp_instance']['default']['client-to-client-reflection'], ['yes'] + ) + self.assertIn( + result['ansible_facts']['ansible_net_bgp_instance']['default']['ignore-as-path-len'], ['no'] + ) + self.assertIn( + result['ansible_facts']['ansible_net_route']['altegro']['dst-address'], ['10.10.66.0/30'] + ) + self.assertIn( + result['ansible_facts']['ansible_net_route']['altegro']['pref-src'], ['10.10.66.1'] + ) + self.assertIn( + result['ansible_facts']['ansible_net_route']['altegro']['gateway'], ['bridge1'] + ) + self.assertIn( + result['ansible_facts']['ansible_net_route']['altegro']['gateway-status'], ['bridge1'] + ) + self.assertIn( + result['ansible_facts']['ansible_net_route']['altegro']['distance'], ['0'] + ) + self.assertIn( + result['ansible_facts']['ansible_net_route']['altegro']['scope'], ['10'] + ) + self.assertIn( + result['ansible_facts']['ansible_net_route']['altegro']['routing-mark'], ['altegro'] + ) + self.assertIn( + result['ansible_facts']['ansible_net_ospf_instance']['default']['name'], ['default'] + ) + self.assertIn( + result['ansible_facts']['ansible_net_ospf_instance']['default']['router-id'], ['10.10.50.1'] + ) + self.assertIn( + result['ansible_facts']['ansible_net_ospf_instance']['default']['distribute-default'], ['never'] + ) + self.assertIn( + result['ansible_facts']['ansible_net_ospf_instance']['default']['redistribute-connected'], ['no'] + ) + self.assertIn( + result['ansible_facts']['ansible_net_ospf_instance']['default']['redistribute-static'], ['no'] + ) + self.assertIn( + result['ansible_facts']['ansible_net_ospf_instance']['default']['redistribute-rip'], ['no'] + ) + self.assertIn( + result['ansible_facts']['ansible_net_ospf_instance']['default']['redistribute-bgp'], ['no'] + ) + self.assertIn( + result['ansible_facts']['ansible_net_ospf_instance']['default']['redistribute-other-ospf'], ['no'] + ) + self.assertIn( + result['ansible_facts']['ansible_net_ospf_instance']['default']['metric-default'], ['1'] + ) + self.assertIn( + result['ansible_facts']['ansible_net_ospf_instance']['default']['metric-connected'], ['20'] + ) + self.assertIn( + result['ansible_facts']['ansible_net_ospf_instance']['default']['metric-static'], ['20'] + ) + self.assertIn( + result['ansible_facts']['ansible_net_ospf_instance']['default']['metric-rip'], ['20'] + ) + self.assertIn( + result['ansible_facts']['ansible_net_ospf_instance']['default']['metric-bgp'], ['auto'] + ) + self.assertIn( + result['ansible_facts']['ansible_net_ospf_instance']['default']['metric-other-ospf'], ['auto'] + ) + self.assertIn( + result['ansible_facts']['ansible_net_ospf_instance']['default']['in-filter'], ['ospf-in'] + ) + self.assertIn( + result['ansible_facts']['ansible_net_ospf_instance']['default']['out-filter'], ['ospf-out'] + ) + self.assertIn( + result['ansible_facts']['ansible_net_ospf_neighbor']['default']['instance'], ['default'] + ) + self.assertIn( + result['ansible_facts']['ansible_net_ospf_neighbor']['default']['router-id'], ['10.10.100.1'] + ) + self.assertIn( + result['ansible_facts']['ansible_net_ospf_neighbor']['default']['address'], ['10.10.1.2'] + ) + self.assertIn( + result['ansible_facts']['ansible_net_ospf_neighbor']['default']['interface'], ['GRE_TYRMA'] + ) + self.assertIn( + result['ansible_facts']['ansible_net_ospf_neighbor']['default']['priority'], ['1'] + ) + self.assertIn( + result['ansible_facts']['ansible_net_ospf_neighbor']['default']['dr-address'], ['0.0.0.0'] + ) + self.assertIn( + result['ansible_facts']['ansible_net_ospf_neighbor']['default']['backup-dr-address'], ['0.0.0.0'] + ) + self.assertIn( + result['ansible_facts']['ansible_net_ospf_neighbor']['default']['state'], ['Full'] + ) + self.assertIn( + result['ansible_facts']['ansible_net_ospf_neighbor']['default']['state-changes'], ['15'] + ) + self.assertIn( + result['ansible_facts']['ansible_net_ospf_neighbor']['default']['ls-retransmits'], ['0'] + ) + self.assertIn( + result['ansible_facts']['ansible_net_ospf_neighbor']['default']['ls-requests'], ['0'] + ) + self.assertIn( + result['ansible_facts']['ansible_net_ospf_neighbor']['default']['db-summaries'], ['0'] + ) + self.assertIn( + result['ansible_facts']['ansible_net_ospf_neighbor']['default']['adjacency'], ['6h8m46s'] + ) diff --git a/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/utils.py b/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/utils.py new file mode 100644 index 00000000..89b4aa25 --- /dev/null +++ b/collections-debian-merged/ansible_collections/community/routeros/tests/unit/plugins/modules/utils.py @@ -0,0 +1,50 @@ +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._text 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) diff --git a/collections-debian-merged/ansible_collections/community/routeros/tests/unit/requirements.txt b/collections-debian-merged/ansible_collections/community/routeros/tests/unit/requirements.txt new file mode 100644 index 00000000..eac8a96b --- /dev/null +++ b/collections-debian-merged/ansible_collections/community/routeros/tests/unit/requirements.txt @@ -0,0 +1,4 @@ +unittest2 ; python_version <= '2.6' + +# requirements for api module +librouteros ; python_version >= '3.6' |