From fc8663edc05e5fd15f9cd5dc98d1f71c9c338131 Mon Sep 17 00:00:00 2001 From: Sumit Jaiswal Date: Mon, 21 May 2018 17:04:20 +0530 Subject: [PATCH] Integration tests for Infoblox 2.5 modules (#40344) * Update nios.py * Update nios.py * Update nios.py * nios lookup errors out when there are no results #37970 Open Indentation failure issue resolved * Returning empty list instead of None In case of no results, res will be returned as an empty list instead of None (implementing ganeshrn comment) * infoblox ipv6 support changes * infoblox ipv6 support changes * for fixing pep8 errors * moving ipaddr check to utils * adding ipv6addr check * increasing space to resolve pep8 error * modified the playbook examples to valid ones * Update nios_network.py * integration tests for nios 2.5 modules * modification done in existing integration nios testcases * dns_view nios module tc * host_record nios module tc * network nios module tc * network_view nios module tc * zone nios module tc * changes to fix shippabe errors for PR 40344 * PR40344 shippable fix * PR40344 shippable fix * PR40344 shippable fix * PR40344 shippable fix * PR40344 shippable fix * PR40344 shippable fix * PR40344 shippable fix * PR40344 shippable fix * PR40344 shippable error fix * 40344 shippable fix * 40344 shippable fix * 40344 shippable fix * 40344 shippable fix * 40344 shippable fix * 40344 shippable fix * PR40344 shippable error fix for block comment should start with '# ' --- .../module_utils/net_tools/nios/api.py | 7 +- .../integration/targets/nios_dns_view/aliases | 1 + .../targets/nios_dns_view/defaults/main.yaml | 3 + .../targets/nios_dns_view/meta/main.yaml | 2 + .../targets/nios_dns_view/tasks/cli.yaml | 0 .../targets/nios_dns_view/tasks/main.yaml | 2 + .../targets/nios_dns_view/tasks/niapi.yaml | 0 .../targets/nios_host_record/aliases | 1 + .../nios_host_record/defaults/main.yaml | 3 + .../targets/nios_host_record/meta/main.yaml | 2 + .../targets/nios_host_record/tasks/cli.yaml | 22 ++ .../targets/nios_host_record/tasks/main.yaml | 2 + .../targets/nios_host_record/tasks/niapi.yaml | 0 .../tests/configure_ipv4_host_record.yaml | 0 test/integration/targets/nios_network/aliases | 1 + .../targets/nios_network/defaults/main.yaml | 0 .../targets/nios_network/meta/main.yaml | 2 + .../targets/nios_network/tasks/cli.yaml | 0 .../targets/nios_network/tasks/main.yaml | 2 + .../targets/nios_network/tasks/niapi.yaml | 0 .../targets/nios_network_view/aliases | 1 + .../nios_network_view/defaults/main.yaml | 3 + .../targets/nios_network_view/meta/main.yaml | 2 + .../targets/nios_network_view/tasks/cli.yaml | 0 .../targets/nios_network_view/tasks/main.yaml | 2 + .../nios_network_view/tasks/niapi.yaml | 0 test/integration/targets/nios_zone/aliases | 1 + .../targets/nios_zone/defaults/main.yaml | 3 + .../targets/nios_zone/meta/main.yaml | 2 + .../targets/nios_zone/tasks/cli.yaml | 0 .../targets/nios_zone/tasks/main.yaml | 2 + .../targets/nios_zone/tasks/niapi.yaml | 0 test/units/modules/net_tools/nios/__init__.py | 0 .../net_tools/nios/fixtures/nios_result.txt | 0 .../net_tools/nios/test_nios_dns_view.py | 127 ++++++++++++ .../net_tools/nios/test_nios_host_record.py | 154 ++++++++++++++ .../net_tools/nios/test_nios_module.py | 88 ++++++++ .../net_tools/nios/test_nios_network.py | 192 ++++++++++++++++++ .../net_tools/nios/test_nios_network_view.py | 156 ++++++++++++++ .../modules/net_tools/nios/test_nios_zone.py | 126 ++++++++++++ 40 files changed, 905 insertions(+), 4 deletions(-) create mode 100644 test/integration/targets/nios_dns_view/aliases create mode 100644 test/integration/targets/nios_dns_view/defaults/main.yaml create mode 100644 test/integration/targets/nios_dns_view/meta/main.yaml create mode 100644 test/integration/targets/nios_dns_view/tasks/cli.yaml create mode 100644 test/integration/targets/nios_dns_view/tasks/main.yaml create mode 100644 test/integration/targets/nios_dns_view/tasks/niapi.yaml create mode 100644 test/integration/targets/nios_host_record/aliases create mode 100644 test/integration/targets/nios_host_record/defaults/main.yaml create mode 100644 test/integration/targets/nios_host_record/meta/main.yaml create mode 100644 test/integration/targets/nios_host_record/tasks/cli.yaml create mode 100644 test/integration/targets/nios_host_record/tasks/main.yaml create mode 100644 test/integration/targets/nios_host_record/tasks/niapi.yaml create mode 100644 test/integration/targets/nios_host_record/tests/configure_ipv4_host_record.yaml create mode 100644 test/integration/targets/nios_network/aliases create mode 100644 test/integration/targets/nios_network/defaults/main.yaml create mode 100644 test/integration/targets/nios_network/meta/main.yaml create mode 100644 test/integration/targets/nios_network/tasks/cli.yaml create mode 100644 test/integration/targets/nios_network/tasks/main.yaml create mode 100644 test/integration/targets/nios_network/tasks/niapi.yaml create mode 100644 test/integration/targets/nios_network_view/aliases create mode 100644 test/integration/targets/nios_network_view/defaults/main.yaml create mode 100644 test/integration/targets/nios_network_view/meta/main.yaml create mode 100644 test/integration/targets/nios_network_view/tasks/cli.yaml create mode 100644 test/integration/targets/nios_network_view/tasks/main.yaml create mode 100644 test/integration/targets/nios_network_view/tasks/niapi.yaml create mode 100644 test/integration/targets/nios_zone/aliases create mode 100644 test/integration/targets/nios_zone/defaults/main.yaml create mode 100644 test/integration/targets/nios_zone/meta/main.yaml create mode 100644 test/integration/targets/nios_zone/tasks/cli.yaml create mode 100644 test/integration/targets/nios_zone/tasks/main.yaml create mode 100644 test/integration/targets/nios_zone/tasks/niapi.yaml create mode 100644 test/units/modules/net_tools/nios/__init__.py create mode 100644 test/units/modules/net_tools/nios/fixtures/nios_result.txt create mode 100644 test/units/modules/net_tools/nios/test_nios_dns_view.py create mode 100644 test/units/modules/net_tools/nios/test_nios_host_record.py create mode 100644 test/units/modules/net_tools/nios/test_nios_module.py create mode 100644 test/units/modules/net_tools/nios/test_nios_network.py create mode 100644 test/units/modules/net_tools/nios/test_nios_network_view.py create mode 100644 test/units/modules/net_tools/nios/test_nios_zone.py diff --git a/lib/ansible/module_utils/net_tools/nios/api.py b/lib/ansible/module_utils/net_tools/nios/api.py index 3b5384d835a..298beedac2a 100644 --- a/lib/ansible/module_utils/net_tools/nios/api.py +++ b/lib/ansible/module_utils/net_tools/nios/api.py @@ -25,8 +25,9 @@ # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE # USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. # -import os + +import os from functools import partial from ansible.module_utils.six import iteritems @@ -39,7 +40,6 @@ try: except ImportError: HAS_INFOBLOX_CLIENT = False - nios_provider_spec = { 'host': dict(), 'username': dict(), @@ -167,7 +167,6 @@ class WapiModule(WapiBase): def __init__(self, module): self.module = module provider = module.params['provider'] - try: super(WapiModule, self).__init__(provider) except Exception as exc: @@ -197,6 +196,7 @@ class WapiModule(WapiBase): :returns: a results dict ''' + state = self.module.params['state'] if state not in ('present', 'absent'): self.module.fail_json(msg='state must be one of `present`, `absent`, got `%s`' % state) @@ -300,7 +300,6 @@ class WapiModule(WapiBase): return True def compare_objects(self, current_object, proposed_object): - for key, proposed_item in iteritems(proposed_object): current_item = current_object.get(key) diff --git a/test/integration/targets/nios_dns_view/aliases b/test/integration/targets/nios_dns_view/aliases new file mode 100644 index 00000000000..89aea537d13 --- /dev/null +++ b/test/integration/targets/nios_dns_view/aliases @@ -0,0 +1 @@ +unsupported \ No newline at end of file diff --git a/test/integration/targets/nios_dns_view/defaults/main.yaml b/test/integration/targets/nios_dns_view/defaults/main.yaml new file mode 100644 index 00000000000..ebf6ffc9035 --- /dev/null +++ b/test/integration/targets/nios_dns_view/defaults/main.yaml @@ -0,0 +1,3 @@ +--- +testcase: "*" +test_items: [] \ No newline at end of file diff --git a/test/integration/targets/nios_dns_view/meta/main.yaml b/test/integration/targets/nios_dns_view/meta/main.yaml new file mode 100644 index 00000000000..9472935b7cf --- /dev/null +++ b/test/integration/targets/nios_dns_view/meta/main.yaml @@ -0,0 +1,2 @@ +dependencies: + - prepare_nios_tests \ No newline at end of file diff --git a/test/integration/targets/nios_dns_view/tasks/cli.yaml b/test/integration/targets/nios_dns_view/tasks/cli.yaml new file mode 100644 index 00000000000..e69de29bb2d diff --git a/test/integration/targets/nios_dns_view/tasks/main.yaml b/test/integration/targets/nios_dns_view/tasks/main.yaml new file mode 100644 index 00000000000..d4898c29bc2 --- /dev/null +++ b/test/integration/targets/nios_dns_view/tasks/main.yaml @@ -0,0 +1,2 @@ +--- +- { include: cli.yaml, tags: ['cli'] } \ No newline at end of file diff --git a/test/integration/targets/nios_dns_view/tasks/niapi.yaml b/test/integration/targets/nios_dns_view/tasks/niapi.yaml new file mode 100644 index 00000000000..e69de29bb2d diff --git a/test/integration/targets/nios_host_record/aliases b/test/integration/targets/nios_host_record/aliases new file mode 100644 index 00000000000..89aea537d13 --- /dev/null +++ b/test/integration/targets/nios_host_record/aliases @@ -0,0 +1 @@ +unsupported \ No newline at end of file diff --git a/test/integration/targets/nios_host_record/defaults/main.yaml b/test/integration/targets/nios_host_record/defaults/main.yaml new file mode 100644 index 00000000000..ebf6ffc9035 --- /dev/null +++ b/test/integration/targets/nios_host_record/defaults/main.yaml @@ -0,0 +1,3 @@ +--- +testcase: "*" +test_items: [] \ No newline at end of file diff --git a/test/integration/targets/nios_host_record/meta/main.yaml b/test/integration/targets/nios_host_record/meta/main.yaml new file mode 100644 index 00000000000..9472935b7cf --- /dev/null +++ b/test/integration/targets/nios_host_record/meta/main.yaml @@ -0,0 +1,2 @@ +dependencies: + - prepare_nios_tests \ No newline at end of file diff --git a/test/integration/targets/nios_host_record/tasks/cli.yaml b/test/integration/targets/nios_host_record/tasks/cli.yaml new file mode 100644 index 00000000000..1930ea2d910 --- /dev/null +++ b/test/integration/targets/nios_host_record/tasks/cli.yaml @@ -0,0 +1,22 @@ +--- +- name: collect all cli test cases + find: + paths: "{{ role_path }}/tests/cli" + patterns: "{{ testcase }}.yaml" + register: test_cases + delegate_to: localhost + +- name: set test_items + set_fact: test_items="{{ test_cases.files | map(attribute='path') | list }}" + +- name: run test cases (connection=network_cli) + include: "{{ test_case_to_run }} " + with_items: "{{ test_items }}" + loop_control: + loop_var: test_case_to_run + +- name: run test case (connection=local) + include: "{{ test_case_to_run }} ansible_connection=local" + with_first_found: "{{ test_items }}" + loop_control: + loop_var: test_case_to_run diff --git a/test/integration/targets/nios_host_record/tasks/main.yaml b/test/integration/targets/nios_host_record/tasks/main.yaml new file mode 100644 index 00000000000..d4898c29bc2 --- /dev/null +++ b/test/integration/targets/nios_host_record/tasks/main.yaml @@ -0,0 +1,2 @@ +--- +- { include: cli.yaml, tags: ['cli'] } \ No newline at end of file diff --git a/test/integration/targets/nios_host_record/tasks/niapi.yaml b/test/integration/targets/nios_host_record/tasks/niapi.yaml new file mode 100644 index 00000000000..e69de29bb2d diff --git a/test/integration/targets/nios_host_record/tests/configure_ipv4_host_record.yaml b/test/integration/targets/nios_host_record/tests/configure_ipv4_host_record.yaml new file mode 100644 index 00000000000..e69de29bb2d diff --git a/test/integration/targets/nios_network/aliases b/test/integration/targets/nios_network/aliases new file mode 100644 index 00000000000..89aea537d13 --- /dev/null +++ b/test/integration/targets/nios_network/aliases @@ -0,0 +1 @@ +unsupported \ No newline at end of file diff --git a/test/integration/targets/nios_network/defaults/main.yaml b/test/integration/targets/nios_network/defaults/main.yaml new file mode 100644 index 00000000000..e69de29bb2d diff --git a/test/integration/targets/nios_network/meta/main.yaml b/test/integration/targets/nios_network/meta/main.yaml new file mode 100644 index 00000000000..9472935b7cf --- /dev/null +++ b/test/integration/targets/nios_network/meta/main.yaml @@ -0,0 +1,2 @@ +dependencies: + - prepare_nios_tests \ No newline at end of file diff --git a/test/integration/targets/nios_network/tasks/cli.yaml b/test/integration/targets/nios_network/tasks/cli.yaml new file mode 100644 index 00000000000..e69de29bb2d diff --git a/test/integration/targets/nios_network/tasks/main.yaml b/test/integration/targets/nios_network/tasks/main.yaml new file mode 100644 index 00000000000..d4898c29bc2 --- /dev/null +++ b/test/integration/targets/nios_network/tasks/main.yaml @@ -0,0 +1,2 @@ +--- +- { include: cli.yaml, tags: ['cli'] } \ No newline at end of file diff --git a/test/integration/targets/nios_network/tasks/niapi.yaml b/test/integration/targets/nios_network/tasks/niapi.yaml new file mode 100644 index 00000000000..e69de29bb2d diff --git a/test/integration/targets/nios_network_view/aliases b/test/integration/targets/nios_network_view/aliases new file mode 100644 index 00000000000..89aea537d13 --- /dev/null +++ b/test/integration/targets/nios_network_view/aliases @@ -0,0 +1 @@ +unsupported \ No newline at end of file diff --git a/test/integration/targets/nios_network_view/defaults/main.yaml b/test/integration/targets/nios_network_view/defaults/main.yaml new file mode 100644 index 00000000000..ebf6ffc9035 --- /dev/null +++ b/test/integration/targets/nios_network_view/defaults/main.yaml @@ -0,0 +1,3 @@ +--- +testcase: "*" +test_items: [] \ No newline at end of file diff --git a/test/integration/targets/nios_network_view/meta/main.yaml b/test/integration/targets/nios_network_view/meta/main.yaml new file mode 100644 index 00000000000..9472935b7cf --- /dev/null +++ b/test/integration/targets/nios_network_view/meta/main.yaml @@ -0,0 +1,2 @@ +dependencies: + - prepare_nios_tests \ No newline at end of file diff --git a/test/integration/targets/nios_network_view/tasks/cli.yaml b/test/integration/targets/nios_network_view/tasks/cli.yaml new file mode 100644 index 00000000000..e69de29bb2d diff --git a/test/integration/targets/nios_network_view/tasks/main.yaml b/test/integration/targets/nios_network_view/tasks/main.yaml new file mode 100644 index 00000000000..d4898c29bc2 --- /dev/null +++ b/test/integration/targets/nios_network_view/tasks/main.yaml @@ -0,0 +1,2 @@ +--- +- { include: cli.yaml, tags: ['cli'] } \ No newline at end of file diff --git a/test/integration/targets/nios_network_view/tasks/niapi.yaml b/test/integration/targets/nios_network_view/tasks/niapi.yaml new file mode 100644 index 00000000000..e69de29bb2d diff --git a/test/integration/targets/nios_zone/aliases b/test/integration/targets/nios_zone/aliases new file mode 100644 index 00000000000..89aea537d13 --- /dev/null +++ b/test/integration/targets/nios_zone/aliases @@ -0,0 +1 @@ +unsupported \ No newline at end of file diff --git a/test/integration/targets/nios_zone/defaults/main.yaml b/test/integration/targets/nios_zone/defaults/main.yaml new file mode 100644 index 00000000000..ebf6ffc9035 --- /dev/null +++ b/test/integration/targets/nios_zone/defaults/main.yaml @@ -0,0 +1,3 @@ +--- +testcase: "*" +test_items: [] \ No newline at end of file diff --git a/test/integration/targets/nios_zone/meta/main.yaml b/test/integration/targets/nios_zone/meta/main.yaml new file mode 100644 index 00000000000..9472935b7cf --- /dev/null +++ b/test/integration/targets/nios_zone/meta/main.yaml @@ -0,0 +1,2 @@ +dependencies: + - prepare_nios_tests \ No newline at end of file diff --git a/test/integration/targets/nios_zone/tasks/cli.yaml b/test/integration/targets/nios_zone/tasks/cli.yaml new file mode 100644 index 00000000000..e69de29bb2d diff --git a/test/integration/targets/nios_zone/tasks/main.yaml b/test/integration/targets/nios_zone/tasks/main.yaml new file mode 100644 index 00000000000..d4898c29bc2 --- /dev/null +++ b/test/integration/targets/nios_zone/tasks/main.yaml @@ -0,0 +1,2 @@ +--- +- { include: cli.yaml, tags: ['cli'] } \ No newline at end of file diff --git a/test/integration/targets/nios_zone/tasks/niapi.yaml b/test/integration/targets/nios_zone/tasks/niapi.yaml new file mode 100644 index 00000000000..e69de29bb2d diff --git a/test/units/modules/net_tools/nios/__init__.py b/test/units/modules/net_tools/nios/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/test/units/modules/net_tools/nios/fixtures/nios_result.txt b/test/units/modules/net_tools/nios/fixtures/nios_result.txt new file mode 100644 index 00000000000..e69de29bb2d diff --git a/test/units/modules/net_tools/nios/test_nios_dns_view.py b/test/units/modules/net_tools/nios/test_nios_dns_view.py new file mode 100644 index 00000000000..f5fe86976d0 --- /dev/null +++ b/test/units/modules/net_tools/nios/test_nios_dns_view.py @@ -0,0 +1,127 @@ +# 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 + + +from ansible.modules.net_tools.nios import nios_dns_view +from ansible.module_utils.net_tools.nios import api +from ansible.compat.tests.mock import patch, MagicMock, Mock +from .test_nios_module import TestNiosModule, load_fixture + + +class TestNiosDnsViewModule(TestNiosModule): + + module = nios_dns_view + + def setUp(self): + super(TestNiosDnsViewModule, self).setUp() + self.module = MagicMock(name='ansible.modules.net_tools.nios.nios_dns_view.WapiModule') + self.module.check_mode = False + self.module.params = {'provider': None} + self.mock_wapi = patch('ansible.modules.net_tools.nios.nios_dns_view.WapiModule') + self.exec_command = self.mock_wapi.start() + self.mock_wapi_run = patch('ansible.modules.net_tools.nios.nios_dns_view.WapiModule.run') + self.mock_wapi_run.start() + self.load_config = self.mock_wapi_run.start() + + def tearDown(self): + super(TestNiosDnsViewModule, self).tearDown() + self.mock_wapi.stop() + self.mock_wapi_run.stop() + + def _get_wapi(self, test_object): + wapi = api.WapiModule(self.module) + wapi.get_object = Mock(name='get_object', return_value=test_object) + wapi.create_object = Mock(name='create_object') + wapi.update_object = Mock(name='update_object') + wapi.delete_object = Mock(name='delete_object') + return wapi + + def load_fixtures(self, commands=None): + self.exec_command.return_value = (0, load_fixture('nios_result.txt').strip(), None) + self.load_config.return_value = dict(diff=None, session='session') + + def test_nios_dns_view_create(self): + self.module.params = {'provider': None, 'state': 'present', 'name': 'ansible-dns', + 'comment': None, 'extattrs': None} + + test_object = None + + test_spec = { + "name": {"ib_req": True}, + "comment": {}, + "extattrs": {} + } + + wapi = self._get_wapi(test_object) + print("WAPI: ", wapi) + res = wapi.run('testobject', test_spec) + + self.assertTrue(res['changed']) + wapi.create_object.assert_called_once_with('testobject', {'name': 'ansible-dns'}) + + def test_nios_dns_view_update_comment(self): + self.module.params = {'provider': None, 'state': 'present', 'name': 'ansible-dns', + 'comment': 'updated comment', 'extattrs': None} + + test_object = [ + { + "comment": "test comment", + "_ref": "dnsview/ZG5zLm5ldHdvcmtfdmlldyQw:default/true", + "name": "ansible-dns", + "extattrs": {} + } + ] + + test_spec = { + "name": {"ib_req": True}, + "comment": {}, + "extattrs": {} + } + + wapi = self._get_wapi(test_object) + res = wapi.run('testobject', test_spec) + + self.assertTrue(res['changed']) + + def test_nios_dns_view_remove(self): + self.module.params = {'provider': None, 'state': 'absent', 'name': 'ansible-dns', + 'comment': None, 'extattrs': None} + + ref = "dnsview/ZG5zLm5ldHdvcmtfdmlldyQw:ansible/false" + + test_object = [{ + "comment": "test comment", + "_ref": ref, + "name": "ansible-dns", + "extattrs": {'Site': {'value': 'test'}} + }] + + test_spec = { + "name": {"ib_req": True}, + "comment": {}, + "extattrs": {} + } + + wapi = self._get_wapi(test_object) + res = wapi.run('testobject', test_spec) + + self.assertTrue(res['changed']) + wapi.delete_object.assert_called_once_with(ref) diff --git a/test/units/modules/net_tools/nios/test_nios_host_record.py b/test/units/modules/net_tools/nios/test_nios_host_record.py new file mode 100644 index 00000000000..27b8728202d --- /dev/null +++ b/test/units/modules/net_tools/nios/test_nios_host_record.py @@ -0,0 +1,154 @@ +# 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 + + +from ansible.modules.net_tools.nios import nios_host_record +from ansible.module_utils.net_tools.nios import api +from units.modules.utils import set_module_args +from ansible.compat.tests.mock import patch, MagicMock, Mock +from .test_nios_module import TestNiosModule, load_fixture + + +class TestNiosHostRecordModule(TestNiosModule): + + module = nios_host_record + + def setUp(self): + + super(TestNiosHostRecordModule, self).setUp() + self.module = MagicMock(name='ansible.modules.net_tools.nios.nios_host_record.WapiModule') + self.module.check_mode = False + self.module.params = {'provider': None} + + self.mock_wapi = patch('ansible.modules.net_tools.nios.nios_host_record.WapiModule') + self.exec_command = self.mock_wapi.start() + self.mock_wapi_run = patch('ansible.modules.net_tools.nios.nios_host_record.WapiModule.run') + self.mock_wapi_run.start() + + self.load_config = self.mock_wapi_run.start() + + def tearDown(self): + super(TestNiosHostRecordModule, self).tearDown() + self.mock_wapi.stop() + + def _get_wapi(self, test_object): + wapi = api.WapiModule(self.module) + wapi.get_object = Mock(name='get_object', return_value=test_object) + wapi.create_object = Mock(name='create_object') + wapi.update_object = Mock(name='update_object') + wapi.delete_object = Mock(name='delete_object') + return wapi + + def load_fixtures(self, commands=None): + self.exec_command.return_value = (0, load_fixture('nios_result.txt').strip(), None) + self.load_config.return_value = dict(diff=None, session='session') + + def test_nios_host_record_create(self): + self.module.params = {'provider': None, 'state': 'present', 'name': 'ansible', + 'comment': None, 'extattrs': None} + + test_object = None + + test_spec = { + "name": {"ib_req": True}, + "comment": {}, + "extattrs": {} + } + + wapi = self._get_wapi(test_object) + print("WAPI: ", wapi) + res = wapi.run('testobject', test_spec) + + self.assertTrue(res['changed']) + wapi.create_object.assert_called_once_with('testobject', {'name': 'ansible'}) + + def test_nios_host_record_remove(self): + self.module.params = {'provider': None, 'state': 'absent', 'name': 'ansible', + 'comment': None, 'extattrs': None} + + ref = "record:host/ZG5zLm5ldHdvcmtfdmlldyQw:ansible/false" + + test_object = [{ + "comment": "test comment", + "_ref": ref, + "name": "ansible", + "extattrs": {'Site': {'value': 'test'}} + }] + + test_spec = { + "name": {"ib_req": True}, + "comment": {}, + "extattrs": {} + } + + wapi = self._get_wapi(test_object) + res = wapi.run('testobject', test_spec) + self.assertTrue(res['changed']) + wapi.delete_object.assert_called_once_with(ref) + + def test_nios_host_record_update_comment(self): + self.module.params = {'provider': None, 'state': 'present', 'name': 'default', + 'comment': 'updated comment', 'extattrs': None} + + test_object = [ + { + "comment": "test comment", + "_ref": "record:host/ZG5zLm5ldHdvcmtfdmlldyQw:default/true", + "name": "default", + "extattrs": {} + } + ] + + test_spec = { + "name": {"ib_req": True}, + "comment": {}, + "extattrs": {} + } + + wapi = self._get_wapi(test_object) + res = wapi.run('testobject', test_spec) + + self.assertTrue(res['changed']) + wapi.update_object.called_once_with(test_object) + + def test_nios_host_record_update_record_name(self): + self.module.params = {'provider': None, 'state': 'present', 'name': 'default', 'old_name': 'old_default', + 'comment': 'comment', 'extattrs': None} + + test_object = [ + { + "comment": "test comment", + "_ref": "record:host/ZG5zLm5ldHdvcmtfdmlldyQw:default/true", + "name": "default", + "old_name": "old_default", + "extattrs": {} + } + ] + + test_spec = { + "name": {"ib_req": True}, + "comment": {}, + "extattrs": {} + } + + wapi = self._get_wapi(test_object) + res = wapi.run('testobject', test_spec) + + self.assertTrue(res['changed']) + wapi.update_object.called_once_with(test_object) diff --git a/test/units/modules/net_tools/nios/test_nios_module.py b/test/units/modules/net_tools/nios/test_nios_module.py new file mode 100644 index 00000000000..3ad9705c6f5 --- /dev/null +++ b/test/units/modules/net_tools/nios/test_nios_module.py @@ -0,0 +1,88 @@ +# (c) 2018 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 . + +# Make coding more python3-ish +from __future__ import (absolute_import, division, print_function) +__metaclass__ = type + +import os +import json + +from units.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: + pass + + fixture_data[path] = data + return data + + +class TestNiosModule(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/test/units/modules/net_tools/nios/test_nios_network.py b/test/units/modules/net_tools/nios/test_nios_network.py new file mode 100644 index 00000000000..eb90fa9d9d6 --- /dev/null +++ b/test/units/modules/net_tools/nios/test_nios_network.py @@ -0,0 +1,192 @@ +# 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 + + +from ansible.module_utils.net_tools.nios import api +from ansible.modules.net_tools.nios import nios_network +from ansible.compat.tests.mock import patch, MagicMock, Mock +from units.modules.utils import set_module_args +from .test_nios_module import TestNiosModule, load_fixture + + +class TestNiosNetworkModule(TestNiosModule): + + module = nios_network + + def setUp(self): + super(TestNiosNetworkModule, self).setUp() + self.module = MagicMock(name='ansible.modules.net_tools.nios.nios_network.WapiModule') + self.module.check_mode = False + self.module.params = {'provider': None} + self.mock_wapi = patch('ansible.modules.net_tools.nios.nios_network.WapiModule') + self.exec_command = self.mock_wapi.start() + self.mock_wapi_run = patch('ansible.modules.net_tools.nios.nios_network.WapiModule.run') + self.mock_wapi_run.start() + self.load_config = self.mock_wapi_run.start() + + def tearDown(self): + super(TestNiosNetworkModule, self).tearDown() + self.mock_wapi.stop() + self.mock_wapi_run.stop() + + def load_fixtures(self, commands=None): + self.exec_command.return_value = (0, load_fixture('nios_result.txt').strip(), None) + self.load_config.return_value = dict(diff=None, session='session') + + def _get_wapi(self, test_object): + wapi = api.WapiModule(self.module) + wapi.get_object = Mock(name='get_object', return_value=test_object) + wapi.create_object = Mock(name='create_object') + wapi.update_object = Mock(name='update_object') + wapi.delete_object = Mock(name='delete_object') + return wapi + + def test_nios_network_ipv4_create(self): + self.module.params = {'provider': None, 'state': 'present', 'network': '192.168.10.0/24', + 'comment': None, 'extattrs': None} + + test_object = None + test_spec = { + "network": {"ib_req": True}, + "comment": {}, + "extattrs": {} + } + + wapi = self._get_wapi(test_object) + print("WAPI: ", wapi) + res = wapi.run('testobject', test_spec) + + self.assertTrue(res['changed']) + wapi.create_object.assert_called_once_with('testobject', {'network': '192.168.10.0/24'}) + + def test_nios_network_ipv4_dhcp_update(self): + self.module.params = {'provider': None, 'state': 'present', 'network': '192.168.10.0/24', + 'comment': 'updated comment', 'extattrs': None} + + test_object = [ + { + "comment": "test comment", + "_ref": "network/ZG5zLm5ldHdvcmtfdmlldyQw:default/true", + "network": "192.168.10.0/24", + "extattrs": {'options': {'name': 'test', 'value': 'ansible.com'}} + } + ] + + test_spec = { + "network": {"ib_req": True}, + "comment": {}, + "extattrs": {} + } + + wapi = self._get_wapi(test_object) + res = wapi.run('testobject', test_spec) + + self.assertTrue(res['changed']) + + def test_nios_network_ipv6_dhcp_update(self): + self.module.params = {'provider': None, 'state': 'present', 'network': 'fe80::/64', + 'comment': 'updated comment', 'extattrs': None} + + test_object = [ + { + "comment": "test comment", + "_ref": "ipv6network/ZG5zLm5ldHdvcmtfdmlldyQw:default/true", + "network": "fe80::/64", + "extattrs": {'options': {'name': 'test', 'value': 'ansible.com'}} + } + ] + + test_spec = { + "network": {"ib_req": True}, + "comment": {}, + "extattrs": {} + } + + wapi = self._get_wapi(test_object) + res = wapi.run('testobject', test_spec) + self.assertTrue(res['changed']) + + def test_nios_network_ipv4_remove(self): + self.module.params = {'provider': None, 'state': 'absent', 'network': '192.168.10.0/24', + 'comment': None, 'extattrs': None} + + ref = "network/ZG5zLm5ldHdvcmtfdmlldyQw:ansible/false" + + test_object = [{ + "comment": "test comment", + "_ref": ref, + "network": "192.168.10.0/24", + "extattrs": {'Site': {'value': 'test'}} + }] + + test_spec = { + "network": {"ib_req": True}, + "comment": {}, + "extattrs": {} + } + + wapi = self._get_wapi(test_object) + res = wapi.run('testobject', test_spec) + + self.assertTrue(res['changed']) + wapi.delete_object.assert_called_once_with(ref) + + def test_nios_network_ipv6_create(self): + self.module.params = {'provider': None, 'state': 'present', 'network': 'fe80::/64', + 'comment': None, 'extattrs': None} + + test_object = None + + test_spec = { + "network": {"ib_req": True}, + "comment": {}, + "extattrs": {} + } + + wapi = self._get_wapi(test_object) + print("WAPI: ", wapi) + res = wapi.run('testobject', test_spec) + + self.assertTrue(res['changed']) + wapi.create_object.assert_called_once_with('testobject', {'network': 'fe80::/64'}) + + def test_nios_network_ipv6_remove(self): + self.module.params = {'provider': None, 'state': 'absent', 'network': 'fe80::/64', + 'comment': None, 'extattrs': None} + + ref = "ipv6network/ZG5zLm5ldHdvcmtfdmlldyQw:ansible/false" + + test_object = [{ + "comment": "test comment", + "_ref": ref, + "network": "fe80::/64", + "extattrs": {'Site': {'value': 'test'}} + }] + + test_spec = { + "network": {"ib_req": True}, + "comment": {}, + "extattrs": {} + } + + wapi = self._get_wapi(test_object) + res = wapi.run('testobject', test_spec) + + self.assertTrue(res['changed']) + wapi.delete_object.assert_called_once_with(ref) diff --git a/test/units/modules/net_tools/nios/test_nios_network_view.py b/test/units/modules/net_tools/nios/test_nios_network_view.py new file mode 100644 index 00000000000..472420e3ca0 --- /dev/null +++ b/test/units/modules/net_tools/nios/test_nios_network_view.py @@ -0,0 +1,156 @@ +# 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 + + +from ansible.modules.net_tools.nios import nios_network_view +from ansible.module_utils.net_tools.nios import api +from ansible.compat.tests.mock import patch, MagicMock, Mock +from .test_nios_module import TestNiosModule, load_fixture + + +class TestNiosNetworkViewModule(TestNiosModule): + + module = nios_network_view + + def setUp(self): + super(TestNiosNetworkViewModule, self).setUp() + self.module = MagicMock(name='ansible.modules.net_tools.nios.nios_network_view.WapiModule') + self.module.check_mode = False + self.module.params = {'provider': None} + self.mock_wapi = patch('ansible.modules.net_tools.nios.nios_network_view.WapiModule') + self.exec_command = self.mock_wapi.start() + self.mock_wapi_run = patch('ansible.modules.net_tools.nios.nios_network_view.WapiModule.run') + self.mock_wapi_run.start() + self.load_config = self.mock_wapi_run.start() + + def tearDown(self): + super(TestNiosNetworkViewModule, self).tearDown() + self.mock_wapi.stop() + self.mock_wapi_run.stop() + + def _get_wapi(self, test_object): + wapi = api.WapiModule(self.module) + wapi.get_object = Mock(name='get_object', return_value=test_object) + wapi.create_object = Mock(name='create_object') + wapi.update_object = Mock(name='update_object') + wapi.delete_object = Mock(name='delete_object') + return wapi + + def load_fixtures(self, commands=None): + self.exec_command.return_value = (0, load_fixture('nios_result.txt').strip(), None) + self.load_config.return_value = dict(diff=None, session='session') + + def test_nios_network_view_create(self): + self.module.params = {'provider': None, 'state': 'present', 'name': 'ansible', + 'comment': None, 'extattrs': None} + + test_object = None + test_spec = { + "name": {"ib_req": True}, + "comment": {}, + "extattrs": {}, + + } + + wapi = self._get_wapi(test_object) + print("WAPI: ", wapi) + res = wapi.run('testobject', test_spec) + + self.assertTrue(res['changed']) + wapi.create_object.assert_called_once_with('testobject', {'name': 'ansible'}) + + def test_nios_network_view_update_comment(self): + self.module.params = {'provider': None, 'state': 'present', 'name': 'default', + 'comment': 'updated comment', 'extattrs': None, 'network_view': 'default'} + + test_object = [ + { + "comment": "test comment", + "_ref": "networkview/ZG5zLm5ldHdvcmtfdmlldyQw:default/true", + "name": "default", + "extattrs": {}, + "network_view": "default" + } + ] + + test_spec = { + "name": {"ib_req": True}, + "comment": {}, + "extattrs": {} + } + + wapi = self._get_wapi(test_object) + res = wapi.run('testobject', test_spec) + + self.assertTrue(res['changed']) + wapi.update_object.called_once_with(test_object) + + def test_nios_network_view_update_name(self): + self.module.params = {'provider': None, 'state': 'present', 'name': 'default', 'old_name': 'old_default', + 'comment': 'updated comment', 'extattrs': None, 'network_view': 'default'} + + test_object = [ + { + "comment": "test comment", + "_ref": "networkview/ZG5zLm5ldHdvcmtfdmlldyQw:default/true", + "name": "default", + "old_name": "old_default", + "extattrs": {}, + "network_view": "default" + } + ] + + test_spec = { + "name": {"ib_req": True}, + "comment": {}, + "extattrs": {} + } + + wapi = self._get_wapi(test_object) + res = wapi.run('testobject', test_spec) + + self.assertTrue(res['changed']) + wapi.update_object.called_once_with(test_object) + + def test_nios_network_view_remove(self): + self.module.params = {'provider': None, 'state': 'absent', 'name': 'ansible', + 'comment': None, 'extattrs': None} + + ref = "networkview/ZG5zLm5ldHdvcmtfdmlldyQw:ansible/false" + + test_object = [{ + "comment": "test comment", + "_ref": ref, + "name": "ansible", + "extattrs": {'Site': {'value': 'test'}} + }] + + test_spec = { + "name": {"ib_req": True}, + "comment": {}, + "extattrs": {} + } + + wapi = self._get_wapi(test_object) + res = wapi.run('testobject', test_spec) + + self.assertTrue(res['changed']) + wapi.delete_object.assert_called_once_with(ref) diff --git a/test/units/modules/net_tools/nios/test_nios_zone.py b/test/units/modules/net_tools/nios/test_nios_zone.py new file mode 100644 index 00000000000..37f83455ba0 --- /dev/null +++ b/test/units/modules/net_tools/nios/test_nios_zone.py @@ -0,0 +1,126 @@ +# 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 + + +from ansible.modules.net_tools.nios import nios_zone +from ansible.module_utils.net_tools.nios import api +from ansible.compat.tests.mock import patch, MagicMock, Mock +from .test_nios_module import TestNiosModule, load_fixture + + +class TestNiosZoneModule(TestNiosModule): + + module = nios_zone + + def setUp(self): + super(TestNiosZoneModule, self).setUp() + self.module = MagicMock(name='ansible.modules.net_tools.nios.nios_zone.WapiModule') + self.module.check_mode = False + self.module.params = {'provider': None} + self.mock_wapi = patch('ansible.modules.net_tools.nios.nios_zone.WapiModule') + self.exec_command = self.mock_wapi.start() + self.mock_wapi_run = patch('ansible.modules.net_tools.nios.nios_zone.WapiModule.run') + self.mock_wapi_run.start() + self.load_config = self.mock_wapi_run.start() + + def tearDown(self): + super(TestNiosZoneModule, self).tearDown() + self.mock_wapi.stop() + self.mock_wapi_run.stop() + + def _get_wapi(self, test_object): + wapi = api.WapiModule(self.module) + wapi.get_object = Mock(name='get_object', return_value=test_object) + wapi.create_object = Mock(name='create_object') + wapi.update_object = Mock(name='update_object') + wapi.delete_object = Mock(name='delete_object') + return wapi + + def load_fixtures(self, commands=None): + self.exec_command.return_value = (0, load_fixture('nios_result.txt').strip(), None) + self.load_config.return_value = dict(diff=None, session='session') + + def test_nios_zone_create(self): + self.module.params = {'provider': None, 'state': 'present', 'name': 'ansible.com', + 'comment': None, 'extattrs': None} + + test_object = None + + test_spec = { + "name": {"ib_req": True}, + "comment": {}, + "extattrs": {} + } + + wapi = self._get_wapi(test_object) + print("WAPI: ", wapi) + res = wapi.run('testobject', test_spec) + + self.assertTrue(res['changed']) + wapi.create_object.assert_called_once_with('testobject', {'name': 'ansible.com'}) + + def test_nios_zone_remove(self): + self.module.params = {'provider': None, 'state': 'absent', 'name': 'ansible.com', + 'comment': None, 'extattrs': None} + + ref = "zone/ZG5zLm5ldHdvcmtfdmlldyQw:ansible/false" + + test_object = [{ + "comment": "test comment", + "_ref": ref, + "name": "ansible.com", + "extattrs": {'Site': {'value': 'test'}} + }] + + test_spec = { + "name": {"ib_req": True}, + "comment": {}, + "extattrs": {} + } + wapi = self._get_wapi(test_object) + res = wapi.run('testobject', test_spec) + + self.assertTrue(res['changed']) + wapi.delete_object.assert_called_once_with(ref) + + def test_nios_zone_update_comment(self): + self.module.params = {'provider': None, 'state': 'present', 'name': 'ansible.com', + 'comment': 'updated comment', 'extattrs': None} + + test_object = [ + { + "comment": "test comment", + "_ref": "zone/ZG5zLm5ldHdvcmtfdmlldyQw:default/true", + "name": "ansible.com", + "extattrs": {'Site': {'value': 'test'}} + } + ] + + test_spec = { + "name": {"ib_req": True}, + "comment": {}, + "extattrs": {} + } + + wapi = self._get_wapi(test_object) + res = wapi.run('testobject', test_spec) + + self.assertTrue(res['changed'])