From 55b6c6870940231f0becb957aaf3f4b1fcb5be36 Mon Sep 17 00:00:00 2001 From: anagha-infoblox Date: Mon, 2 May 2022 15:20:53 +0530 Subject: [PATCH 1/9] fix unit test failure --- tests/unit/compat/__init__.py | 0 tests/unit/compat/mock.py | 120 ++++++++++++++++++++++++++++++++++ 2 files changed, 120 insertions(+) create mode 100644 tests/unit/compat/__init__.py create mode 100644 tests/unit/compat/mock.py diff --git a/tests/unit/compat/__init__.py b/tests/unit/compat/__init__.py new file mode 100644 index 00000000..e69de29b diff --git a/tests/unit/compat/mock.py b/tests/unit/compat/mock.py new file mode 100644 index 00000000..ba8455bf --- /dev/null +++ b/tests/unit/compat/mock.py @@ -0,0 +1,120 @@ +# (c) 2014, Toshio Kuratomi +# +# 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 . + +# 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 From 366d8dbf81ca9be988608e62818abc85456905c2 Mon Sep 17 00:00:00 2001 From: Anagha K H <70952486+anagha-infoblox@users.noreply.github.com> Date: Mon, 2 May 2022 16:25:24 +0530 Subject: [PATCH 2/9] Update ansible-test.yml --- .github/workflows/ansible-test.yml | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/.github/workflows/ansible-test.yml b/.github/workflows/ansible-test.yml index b4f8ea02..bd32ca1a 100644 --- a/.github/workflows/ansible-test.yml +++ b/.github/workflows/ansible-test.yml @@ -86,7 +86,9 @@ jobs: working-directory: /home/runner/.ansible/collections/ansible_collections/infoblox/nios_modules/ - name: Generate coverage report - run: ansible-test coverage xml -v --group-by command --group-by version + run: | + pip install coverage + ansible-test coverage xml -v --group-by command --group-by version working-directory: /home/runner/.ansible/collections/ansible_collections/infoblox/nios_modules/ From 2203f7b4632a9d433c4f70eaa86eac80429aa759 Mon Sep 17 00:00:00 2001 From: Anagha K H <70952486+anagha-infoblox@users.noreply.github.com> Date: Mon, 2 May 2022 16:35:08 +0530 Subject: [PATCH 3/9] Update ansible-test.yml --- .github/workflows/ansible-test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ansible-test.yml b/.github/workflows/ansible-test.yml index bd32ca1a..371bd609 100644 --- a/.github/workflows/ansible-test.yml +++ b/.github/workflows/ansible-test.yml @@ -87,7 +87,7 @@ jobs: - name: Generate coverage report run: | - pip install coverage + pip install coverage==6.3.2 ansible-test coverage xml -v --group-by command --group-by version working-directory: /home/runner/.ansible/collections/ansible_collections/infoblox/nios_modules/ From 262450b5237b6035f41337d293d3422c5f65e8e8 Mon Sep 17 00:00:00 2001 From: Anagha K H <70952486+anagha-infoblox@users.noreply.github.com> Date: Mon, 2 May 2022 16:55:15 +0530 Subject: [PATCH 4/9] Update ansible-test.yml --- .github/workflows/ansible-test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ansible-test.yml b/.github/workflows/ansible-test.yml index 371bd609..e19ffa3a 100644 --- a/.github/workflows/ansible-test.yml +++ b/.github/workflows/ansible-test.yml @@ -87,7 +87,7 @@ jobs: - name: Generate coverage report run: | - pip install coverage==6.3.2 + if [${{ matrix.ansible-version }}==devel]; then pip install coverage==6.3.2; fi ansible-test coverage xml -v --group-by command --group-by version working-directory: /home/runner/.ansible/collections/ansible_collections/infoblox/nios_modules/ From 6857ef5eb9dd2a36a56e2a6bcf32f52c828e2c95 Mon Sep 17 00:00:00 2001 From: Anagha K H <70952486+anagha-infoblox@users.noreply.github.com> Date: Mon, 2 May 2022 17:01:50 +0530 Subject: [PATCH 5/9] Update ansible-test.yml --- .github/workflows/ansible-test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ansible-test.yml b/.github/workflows/ansible-test.yml index e19ffa3a..185f8f57 100644 --- a/.github/workflows/ansible-test.yml +++ b/.github/workflows/ansible-test.yml @@ -87,7 +87,7 @@ jobs: - name: Generate coverage report run: | - if [${{ matrix.ansible-version }}==devel]; then pip install coverage==6.3.2; fi + if [ ${{ matrix.ansible-version }}==devel ]; then pip install coverage==6.3.2; fi ansible-test coverage xml -v --group-by command --group-by version working-directory: /home/runner/.ansible/collections/ansible_collections/infoblox/nios_modules/ From 224712496f41ad26d7157ed1b5c37b830c9f6bbb Mon Sep 17 00:00:00 2001 From: anagha-infoblox Date: Mon, 2 May 2022 17:38:24 +0530 Subject: [PATCH 6/9] Fixed sanity errors --- plugins/.DS_Store | Bin 10244 -> 0 bytes plugins/inventory/nios_inventory.py | 2 -- plugins/lookup/nios_lookup.py | 4 ++-- plugins/lookup/nios_next_ip.py | 4 ++-- plugins/lookup/nios_next_network.py | 3 +-- 5 files changed, 5 insertions(+), 8 deletions(-) delete mode 100644 plugins/.DS_Store diff --git a/plugins/.DS_Store b/plugins/.DS_Store deleted file mode 100644 index a3e4274c06adedb126e7bbe2d664830e59e2ac85..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 10244 zcmeHMU2GIp6h3EL;LcFmDL=Ac7uL1}ghjixYz3-re}LAX(6B8nP+4YoMmlkJX4#qD z0<~I1qYnh*Ph!-V7&I{ozL@BP@?EMSqKdBAoP5-!JLQNg+izpuhP*zi0G!DCqT0?MFR# zz@(5t9Uu@OFcSejU1}f)HfXeOpSpiH!O4s{`HX32$mfep-ZUJ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H{Y^dqBRzcw{89J*{))k({dcXb9p8Zv~UEOEy!#)J+oC3H^^r}KD$x*tb%(} zv)Hmve>3Wuy(~7v>)+fpyVtag73!z;w=xq|;)VN73h9MV#a+1GJC3IZejM)|vyiTQ jzQXNE-)~Y#(onhKe+C5m|6u<=<5$-_JMI3T_y7L@9WEnQ diff --git a/plugins/inventory/nios_inventory.py b/plugins/inventory/nios_inventory.py index 20817cf2..f23c0273 100644 --- a/plugins/inventory/nios_inventory.py +++ b/plugins/inventory/nios_inventory.py @@ -6,7 +6,6 @@ DOCUMENTATION = r''' name: nios_inventory - plugin_type: inventory author: - Will Tome (@willtome) short_description: Infoblox inventory plugin @@ -42,7 +41,6 @@ - Value can also be specified using C(INFOBLOX_PASSWORD) environment variable. type: string - secret: true env: - name: INFOBLOX_PASSWORD extattrs: diff --git a/plugins/lookup/nios_lookup.py b/plugins/lookup/nios_lookup.py index cfdecd7f..c2518870 100644 --- a/plugins/lookup/nios_lookup.py +++ b/plugins/lookup/nios_lookup.py @@ -9,7 +9,7 @@ DOCUMENTATION = ''' --- -lookup: nios +name: nios_lookup short_description: Query Infoblox NIOS objects version_added: "1.0.0" description: @@ -74,7 +74,7 @@ description: - The object type specified in the terms argument returned: always - type: complex + type: list contains: obj_field: description: diff --git a/plugins/lookup/nios_next_ip.py b/plugins/lookup/nios_next_ip.py index 2e77a866..8eaf03f0 100644 --- a/plugins/lookup/nios_next_ip.py +++ b/plugins/lookup/nios_next_ip.py @@ -9,7 +9,7 @@ DOCUMENTATION = ''' --- -lookup: nios_next_ip +name: nios_next_ip short_description: Return the next available IP address for a network version_added: "1.0.0" description: @@ -80,7 +80,7 @@ def run(self, terms, variables=None, **kwargs): provider = kwargs.pop('provider', {}) wapi = WapiLookup(provider) - if type(ipaddress.ip_network(network)) == ipaddress.IPv6Network: + if isinstance(ipaddress.ip_network(network)) == ipaddress.IPv6Network: network_obj = wapi.get_object('ipv6network', {'network': network}) else: network_obj = wapi.get_object('network', {'network': network}) diff --git a/plugins/lookup/nios_next_network.py b/plugins/lookup/nios_next_network.py index dc7442bd..3c4e487f 100644 --- a/plugins/lookup/nios_next_network.py +++ b/plugins/lookup/nios_next_network.py @@ -9,7 +9,7 @@ DOCUMENTATION = ''' --- -lookup: nios_next_network +name: nios_next_network short_description: Return the next available network range for a network-container version_added: "1.0.0" description: @@ -29,7 +29,6 @@ - The CIDR of the network to retrieve the next network from next available network within the specified container. Also, Requested CIDR must be specified and greater than the parent CIDR. required: True - default: 24 type: str num: description: The number of network addresses to return from network-container. From 029077a11e9677fa4718f823cf21423f1976eaac Mon Sep 17 00:00:00 2001 From: Anagha K H <70952486+anagha-infoblox@users.noreply.github.com> Date: Mon, 2 May 2022 17:48:46 +0530 Subject: [PATCH 7/9] Update ansible-test.yml --- .github/workflows/ansible-test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/ansible-test.yml b/.github/workflows/ansible-test.yml index 185f8f57..2e29d4ba 100644 --- a/.github/workflows/ansible-test.yml +++ b/.github/workflows/ansible-test.yml @@ -87,7 +87,7 @@ jobs: - name: Generate coverage report run: | - if [ ${{ matrix.ansible-version }}==devel ]; then pip install coverage==6.3.2; fi + if [ "${{ matrix.ansible-version }}" == "devel" ]; then pip install coverage==6.3.2; fi ansible-test coverage xml -v --group-by command --group-by version working-directory: /home/runner/.ansible/collections/ansible_collections/infoblox/nios_modules/ From 2b440e5f798257cf392730f565d3fe89ac93249a Mon Sep 17 00:00:00 2001 From: anagha-infoblox Date: Mon, 2 May 2022 18:09:26 +0530 Subject: [PATCH 8/9] Fixed sanity errors --- plugins/lookup/nios_lookup.py | 1 + plugins/lookup/nios_next_ip.py | 1 + plugins/lookup/nios_next_network.py | 1 + 3 files changed, 3 insertions(+) diff --git a/plugins/lookup/nios_lookup.py b/plugins/lookup/nios_lookup.py index c2518870..c33b212f 100644 --- a/plugins/lookup/nios_lookup.py +++ b/plugins/lookup/nios_lookup.py @@ -28,6 +28,7 @@ return_fields: description: The list of field names to return for the specified object. type: list + elements: str filter: description: A dict object that is used to filter the returned objects. type: dict diff --git a/plugins/lookup/nios_next_ip.py b/plugins/lookup/nios_next_ip.py index 8eaf03f0..93c5bf26 100644 --- a/plugins/lookup/nios_next_ip.py +++ b/plugins/lookup/nios_next_ip.py @@ -32,6 +32,7 @@ description: List of IP's that need to be excluded from returned IP addresses. required: false type: list + elements: str ''' EXAMPLES = """ diff --git a/plugins/lookup/nios_next_network.py b/plugins/lookup/nios_next_network.py index 3c4e487f..293dddb3 100644 --- a/plugins/lookup/nios_next_network.py +++ b/plugins/lookup/nios_next_network.py @@ -40,6 +40,7 @@ required: false default: '' type: list + elements: str ''' EXAMPLES = """ From 09e1706d24c8ab0e8beeb845a1bdff5bae1b49cb Mon Sep 17 00:00:00 2001 From: anagha-infoblox Date: Mon, 2 May 2022 18:28:48 +0530 Subject: [PATCH 9/9] Rectified a syntax error --- plugins/lookup/nios_next_ip.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/lookup/nios_next_ip.py b/plugins/lookup/nios_next_ip.py index 93c5bf26..f10203a5 100644 --- a/plugins/lookup/nios_next_ip.py +++ b/plugins/lookup/nios_next_ip.py @@ -81,7 +81,7 @@ def run(self, terms, variables=None, **kwargs): provider = kwargs.pop('provider', {}) wapi = WapiLookup(provider) - if isinstance(ipaddress.ip_network(network)) == ipaddress.IPv6Network: + if isinstance(ipaddress.ip_network(network), ipaddress.IPv6Network): network_obj = wapi.get_object('ipv6network', {'network': network}) else: network_obj = wapi.get_object('network', {'network': network})