From 362d289031f2370e6adcb9dfba1aff8b98c1c1d9 Mon Sep 17 00:00:00 2001 From: Jeenitkumar Khatri Date: Tue, 13 Aug 2024 11:25:27 +0530 Subject: [PATCH] [ADD]Integration and Unit test cases for extensible attribute. --- playbooks/create_extensible_attribute.yaml | 20 +++ plugins/modules/nios_dtc_lbdn.py | 1 - .../targets/nios_extensible_attribute/aliases | 3 + .../defaults/main.yaml | 3 + .../nios_extensible_attribute/meta/main.yaml | 2 + .../nios_extensible_attribute/tasks/main.yaml | 1 + .../tasks/nios_extensible_attribute.yaml | 147 ++++++++++++++++ .../modules/test_extensible_attribute.py | 159 ++++++++++++++++++ 8 files changed, 335 insertions(+), 1 deletion(-) create mode 100644 playbooks/create_extensible_attribute.yaml create mode 100644 tests/integration/targets/nios_extensible_attribute/aliases create mode 100644 tests/integration/targets/nios_extensible_attribute/defaults/main.yaml create mode 100644 tests/integration/targets/nios_extensible_attribute/meta/main.yaml create mode 100644 tests/integration/targets/nios_extensible_attribute/tasks/main.yaml create mode 100644 tests/integration/targets/nios_extensible_attribute/tasks/nios_extensible_attribute.yaml create mode 100644 tests/unit/plugins/modules/test_extensible_attribute.py diff --git a/playbooks/create_extensible_attribute.yaml b/playbooks/create_extensible_attribute.yaml new file mode 100644 index 00000000..399bc184 --- /dev/null +++ b/playbooks/create_extensible_attribute.yaml @@ -0,0 +1,20 @@ +--- + +- hosts: localhost + vars: + nios_provider: + host: 10.120.1.11 + username: admin + password: infoblox + + connection: local + tasks: + - name: Create INT extensible attribute + infoblox.nios_modules.nios_extensible_attribute: + name: integer_ea + type: INTEGER + default_value : 11 + comment: Created with Ansible + flags: 'I' + state: present + provider: "{{ nios_provider }}" \ No newline at end of file diff --git a/plugins/modules/nios_dtc_lbdn.py b/plugins/modules/nios_dtc_lbdn.py index bc5a79c1..fe99d0e5 100644 --- a/plugins/modules/nios_dtc_lbdn.py +++ b/plugins/modules/nios_dtc_lbdn.py @@ -183,7 +183,6 @@ def auth_zones_transform(module): else: module.fail_json( msg='auth_zone %s cannot be found.' % zone) - # epdb.serve() return zone_list def pools_transform(module): diff --git a/tests/integration/targets/nios_extensible_attribute/aliases b/tests/integration/targets/nios_extensible_attribute/aliases new file mode 100644 index 00000000..b3138dc7 --- /dev/null +++ b/tests/integration/targets/nios_extensible_attribute/aliases @@ -0,0 +1,3 @@ +shippable/cloud/group1 +cloud/nios +destructive diff --git a/tests/integration/targets/nios_extensible_attribute/defaults/main.yaml b/tests/integration/targets/nios_extensible_attribute/defaults/main.yaml new file mode 100644 index 00000000..9ef5ba51 --- /dev/null +++ b/tests/integration/targets/nios_extensible_attribute/defaults/main.yaml @@ -0,0 +1,3 @@ +--- +testcase: "*" +test_items: [] diff --git a/tests/integration/targets/nios_extensible_attribute/meta/main.yaml b/tests/integration/targets/nios_extensible_attribute/meta/main.yaml new file mode 100644 index 00000000..1b01a972 --- /dev/null +++ b/tests/integration/targets/nios_extensible_attribute/meta/main.yaml @@ -0,0 +1,2 @@ +dependencies: + - prepare_nios_tests diff --git a/tests/integration/targets/nios_extensible_attribute/tasks/main.yaml b/tests/integration/targets/nios_extensible_attribute/tasks/main.yaml new file mode 100644 index 00000000..e1442d4f --- /dev/null +++ b/tests/integration/targets/nios_extensible_attribute/tasks/main.yaml @@ -0,0 +1 @@ +- include_tasks: nios_extensible_attribute.yaml diff --git a/tests/integration/targets/nios_extensible_attribute/tasks/nios_extensible_attribute.yaml b/tests/integration/targets/nios_extensible_attribute/tasks/nios_extensible_attribute.yaml new file mode 100644 index 00000000..b5b54470 --- /dev/null +++ b/tests/integration/targets/nios_extensible_attribute/tasks/nios_extensible_attribute.yaml @@ -0,0 +1,147 @@ +- name: Clean up existing EA + infoblox.nios_modules.nios_extensible_attribute: + name: stringEA + type: STRING + state: absent + provider: "{{ nios_provider }}" + +- name: Create STRING EA + infoblox.nios_modules.nios_extensible_attribute: + name: stringEA + type: STRING + default_value: "string" + min: 1 + max: 15 + comment: Created string EA with Ansible + state: present + provider: "{{ nios_provider }}" + register: create_string_ea + +- name: Update STRING EA Comment + infoblox.nios_modules.nios_extensible_attribute: + name: stringEA + type: STRING + default_value: "string" + min: 1 + max: 15 + comment: Updated string EA with Ansible + state: present + provider: "{{ nios_provider }}" + register: update_string_ea + +- name: Create Integer EA + infoblox.nios_modules.nios_extensible_attribute: + name: IntegerEA + type: INTEGER + default_value: "15" + min: 1 + max: 15 + flags: 'I' + comment: Created string EA with Ansible + state: present + provider: "{{ nios_provider }}" + register: create_integer_ea + +- name: Update Integer EA value + infoblox.nios_modules.nios_extensible_attribute: + name: IntegerEA + type: INTEGER + default_value: "14" + min: 1 + max: 15 + flags: 'I' + comment: Created string EA with Ansible + state: present + provider: "{{ nios_provider }}" + register: update_integer_ea + +- name: Remove STRING EA Comment + infoblox.nios_modules.nios_extensible_attribute: + name: stringEA + type: STRING + default_value: "string" + min: 1 + max: 15 + state: present + provider: "{{ nios_provider }}" + register: remove_string_ea + +- name: Remove Integer EA definition + infoblox.nios_modules.nios_extensible_attribute: + name: IntegerEA + type: INTEGER + default_value: "14" + min: 1 + max: 15 + flags: 'I' + comment: Created string EA with Ansible + state: absent + provider: "{{ nios_provider }}" + register: remove_integer_ea + +- name: Create ENUM EA + infoblox.nios_modules.nios_extensible_attribute: + name: enumEA + type: ENUM + list_values: + - option1 + - option2 + default_value: "option1" + comment: Created enum EA with Ansible + state: present + provider: "{{ nios_provider }}" + register: create_enum_ea + +- name: Update ENUM EA default value + infoblox.nios_modules.nios_extensible_attribute: + name: enumEA + type: ENUM + list_values: + - option1 + - option2 + default_value: "option2" + comment: Updated enum EA default value with Ansible + state: present + provider: "{{ nios_provider }}" + register: update_enum_ea + +- name: Create DATE EA + infoblox.nios_modules.nios_extensible_attribute: + name: dateEA + type: DATE + default_value: "2023-01-01" + comment: Created date EA with Ansible + state: present + provider: "{{ nios_provider }}" + register: create_date_ea + +- name: Update DATE EA default value + infoblox.nios_modules.nios_extensible_attribute: + name: dateEA + type: DATE + default_value: "2023-12-31" + comment: Updated date EA default value with Ansible + state: present + provider: "{{ nios_provider }}" + register: update_date_ea + +- name: Check if the EA already exists + infoblox.nios_modules.nios_extensible_attribute: + name: existEA + state: present + provider: "{{ nios_provider }}" + register: ea_exists + +- name: Verify outcomes including new tests + ansible.builtin.assert: + that: + - "create_string_ea.changed" + - "update_string_ea.changed" + - "create_integer_ea.changed" + - "update_integer_ea.changed" + - "remove_string_ea.changed" + - "remove_integer_ea.changed" + - "create_enum_ea.changed" + - "update_enum_ea.changed" + - "create_date_ea.changed" + - "update_date_ea.changed" diff --git a/tests/unit/plugins/modules/test_extensible_attribute.py b/tests/unit/plugins/modules/test_extensible_attribute.py new file mode 100644 index 00000000..026819b3 --- /dev/null +++ b/tests/unit/plugins/modules/test_extensible_attribute.py @@ -0,0 +1,159 @@ +# 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_collections.infoblox.nios_modules.plugins.modules import nios_extensible_attribute +from ansible_collections.infoblox.nios_modules.plugins.module_utils import api +from ansible_collections.infoblox.nios_modules.tests.unit.compat.mock import patch, MagicMock, Mock +from .test_nios_module import TestNiosModule, load_fixture + + +class TestNiosExtensibleAttributeModule(TestNiosModule): + + module = nios_extensible_attribute + + def setUp(self): + super(TestNiosExtensibleAttributeModule, self).setUp() + self.module = MagicMock(name='ansible_collections.infoblox.nios_modules.plugins.modules.nios_extensible_attribute.WapiModule') + self.module.check_mode = False + self.module.params = {'provider': None} + self.mock_wapi = patch('ansible_collections.infoblox.nios_modules.plugins.modules.nios_extensible_attribute.WapiModule') + self.exec_command = self.mock_wapi.start() + self.mock_wapi_run = patch('ansible_collections.infoblox.nios_modules.plugins.modules.nios_extensible_attribute.WapiModule.run') + self.mock_wapi_run.start() + self.load_config = self.mock_wapi_run.start() + self.mock_check_type_dict = patch('ansible.module_utils.common.validation.check_type_dict') + self.mock_check_type_dict_obj = self.mock_check_type_dict.start() + + def tearDown(self): + super(TestNiosExtensibleAttributeModule, self).tearDown() + self.mock_wapi.stop() + self.mock_wapi_run.stop() + self.mock_check_type_dict.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_create_extensible_attribute(self): + self.module.params = { + 'name': 'my_string', + 'type': 'STRING', + 'comment': 'Created by ansible', + 'state': 'present', + 'provider': None + } + + test_object = None + + test_spec = { + "name": {"ib_req": True}, + "type": {"ib_req": True}, + "comment": {}, + } + + wapi = self._get_wapi(test_object) + print("WAPI: ", wapi.__dict__) + res = wapi.run('testobject', test_spec) + + self.assertEqual(res['changed'], True) + wapi.create_object.assert_called_once_with('testobject', {'name': 'my_string', + 'type': 'STRING', + 'comment': 'Created by ansible'}) + + def test_nios_ea_update_comment(self): + self.module.params = { + 'provider': None, + 'state': 'present', + 'name': 'testStringEA', + 'type': 'STRING', + 'flag': 'I', + 'default_value': 'test', + 'comment': 'Update comment' + } + + ref = "extensibleattributedef/b25lLmV4dGVuc2libGVfYXR0cmlidXRlc19kZWYkLlRlcnJhZm9ybSBJbnRlcm5hbCBJRA:testStringEA" + + test_object = [ + { + "comment": "test comment", + "_ref": ref, + "name": "testStringEA", + "flag": "I", + "type": "STRING", + "default_value": "test" + } + ] + + test_spec = { + "name": {"ib_req": True}, + "type": {"ib_req": True}, + "comment": {}, + "flag": {}, + "default_value": {} + } + + wapi = self._get_wapi(test_object) + res = wapi.run('testobject', test_spec) + self.assertTrue(res['changed']) + wapi.update_object.assert_called_once_with( + ref, {'comment': 'Update comment', 'type': 'STRING', 'name': 'testStringEA', 'flag': 'I', 'default_value': 'test'} + ) + + + def test_remove_extensible_attribute(self): + self.module.params = {'provider': None, 'state': 'absent', 'name': 'testStringEA', 'type': 'STRING', + 'flag': None, 'default_value': None, 'comment': None} + + ref = "extensibleattributedef/b25lLmV4dGVuc2libGVfYXR0cmlidXRlc19kZWYkLlRlcnJhZm9ybSBJbnRlcm5hbCBJRA:testStringEA" + + test_object = [ + { + "comment": "test comment", + "_ref": ref, + "name": "testStringEA", + "flag": "I", + "type": "STRING", + "default_value": "test" + } + ] + + test_spec = { + "name": {"ib_req": True}, + "type": {"ib_req": True}, + "comment": {}, + "flag": {}, + "default_value": {} + } + + wapi = self._get_wapi(test_object) + res = wapi.run('testobject', test_spec) + + self.assertTrue(res['changed']) + wapi.delete_object.assert_called_once_with(ref) \ No newline at end of file