Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ADD]Integration and Unit test cases for extensible attribute. #246

Merged
merged 5 commits into from
Aug 29, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/ansible-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ jobs:
- name: Generate coverage report
run: |
if [ "${{ matrix.ansible-version }}" == "devel" ]; then
pip install coverage==7.5.3;
pip install coverage==7.6.1;
elif [ "${{ matrix.ansible-version }}" == "stable-2.15" ]; then
pip install coverage==6.5.0;
fi
Expand Down Expand Up @@ -161,7 +161,7 @@ jobs:
echo $ANSIBLE_NIOSSIM_CONTAINER
ansible-test integration -v --color --retry-on-error --continue-on-error --diff --python ${{ matrix.python-version }} --docker --coverage
env:
ANSIBLE_NIOSSIM_CONTAINER: quay.io/ansible/nios-test-container:4.0.0
ANSIBLE_NIOSSIM_CONTAINER: quay.io/ansible/nios-test-container:5.0.0
working-directory: /home/runner/.ansible/collections/ansible_collections/infoblox/nios_modules/

# ansible-test support producing code coverage date
Expand Down
20 changes: 20 additions & 0 deletions playbooks/create_extensible_attribute.yaml
Original file line number Diff line number Diff line change
@@ -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 }}"
1 change: 0 additions & 1 deletion plugins/modules/nios_dtc_lbdn.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
3 changes: 3 additions & 0 deletions tests/integration/targets/nios_extensible_attribute/aliases
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
shippable/cloud/group1
cloud/nios
destructive
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
---
testcase: "*"
test_items: []
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
dependencies:
- prepare_nios_tests
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
- include_tasks: nios_extensible_attribute.yaml
Original file line number Diff line number Diff line change
@@ -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"
158 changes: 158 additions & 0 deletions tests/unit/plugins/modules/test_extensible_attribute.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,158 @@
# 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.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)
Loading