Skip to content

Commit

Permalink
[IMP] added unit testcases for logic_filter_rules
Browse files Browse the repository at this point in the history
- corrected sanity test failures.
  • Loading branch information
JchhatbarInfoblox committed Jun 26, 2024
1 parent f33e09e commit 49b9351
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 8 deletions.
15 changes: 10 additions & 5 deletions plugins/lookup/nios_next_ip.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,13 @@
EXAMPLES = """
- name: return next available IP address for network 192.168.10.0/24
ansible.builtin.set_fact:
ipaddr: "{{ lookup('infoblox.nios_modules.nios_next_ip', '192.168.10.0/24', provider={'host': 'nios01', 'username': 'admin', 'password': 'password'}) }}"
ipaddr: "{{ lookup('infoblox.nios_modules.nios_next_ip', '192.168.10.0/24',
provider={'host': 'nios01', 'username': 'admin', 'password': 'password'}) }}"
- name: return next available IP address for network 192.168.10.0/24 from DHCP range
ansible.builtin.set_fact:
ipaddr: "{{ lookup('infoblox.nios_modules.nios_next_ip', '192.168.10.0/24', use_range=true, provider={'host': 'nios01', 'username': 'admin', 'password': 'password'}) }}"
ipaddr: "{{ lookup('infoblox.nios_modules.nios_next_ip', '192.168.10.0/24',
use_range=true, provider={'host': 'nios01', 'username': 'admin', 'password': 'password'}) }}"
- name: return next available IP address for network 192.168.10.0/24 in a non-default network view
ansible.builtin.set_fact:
Expand All @@ -64,14 +66,17 @@
ipaddr: "{{ lookup('infoblox.nios_modules.nios_next_ip', '192.168.10.0/24', num=3,
provider={'host': 'nios01', 'username': 'admin', 'password': 'password'}) }}"
- name: return the next 3 available IP addresses for network 192.168.10.0/24 excluding ip addresses - ['192.168.10.1', '192.168.10.2']
- name: return the next 3 available IP addresses for network 192.168.10.0/24
excluding ip addresses - ['192.168.10.1', '192.168.10.2']
ansible.builtin.set_fact:
ipaddr: "{{ lookup('infoblox.nios_modules.nios_next_ip', '192.168.10.0/24', num=3, exclude=['192.168.10.1', '192.168.10.2'],
ipaddr: "{{ lookup('infoblox.nios_modules.nios_next_ip', '192.168.10.0/24', num=3,
exclude=['192.168.10.1', '192.168.10.2'],
provider={'host': 'nios01', 'username': 'admin', 'password': 'password'}) }}"
- name: return next available IP address for network fd30:f52:2:12::/64
ansible.builtin.set_fact:
ipaddr: "{{ lookup('infoblox.nios_modules.nios_next_ip', 'fd30:f52:2:12::/64', provider={'host': 'nios01', 'username': 'admin', 'password': 'password'}) }}"
ipaddr: "{{ lookup('infoblox.nios_modules.nios_next_ip', 'fd30:f52:2:12::/64',
provider={'host': 'nios01', 'username': 'admin', 'password': 'password'}) }}"
"""

RETURN = """
Expand Down
64 changes: 61 additions & 3 deletions tests/unit/plugins/modules/test_nios_network.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,16 @@ class TestNiosNetworkModule(TestNiosModule):

def setUp(self):
super(TestNiosNetworkModule, self).setUp()
self.module = MagicMock(name='ansible_collections.infoblox.nios_modules.plugins.modules.nios_network.WapiModule')
self.module = MagicMock(
name='ansible_collections.infoblox.nios_modules.plugins.modules.nios_network.WapiModule'
)
self.module.check_mode = False
self.module.params = {'provider': None}
self.mock_wapi = patch('ansible_collections.infoblox.nios_modules.plugins.modules.nios_network.WapiModule')
self.exec_command = self.mock_wapi.start()
self.mock_wapi_run = patch('ansible_collections.infoblox.nios_modules.plugins.modules.nios_network.WapiModule.run')
self.mock_wapi_run = patch(
'ansible_collections.infoblox.nios_modules.plugins.modules.nios_network.WapiModule.run'
)
self.mock_wapi_run.start()
self.load_config = self.mock_wapi_run.start()

Expand All @@ -58,7 +62,8 @@ def _get_wapi(self, test_object):

def test_nios_network_ipv4_create(self):
self.module.params = {'provider': None, 'state': 'present', 'network': '192.168.10.0/24',
'comment': None, 'extattrs': None}
'comment': None, 'extattrs': None, 'use_logic_filter_rules': False,
'logic_filter_rules': []}

test_object = None
test_spec = {
Expand Down Expand Up @@ -246,3 +251,56 @@ def test_nios_networkcontainer_ipv6_create(self):

self.assertTrue(res['changed'])
wapi.create_object.assert_called_once_with('testobject', {'ipv6networkcontainer': 'fe80::/64'})

def test_nios_network_ipv4_create_with_use_logic_filter_rules(self):
self.module.params = {'provider': None, 'state': 'present', 'network': '192.168.10.0/24',
'comment': None, 'extattrs': None, 'use_logic_filter_rules': True,
'logic_filter_rules': []}

test_object = None
test_spec = {
"network": {"ib_req": True},
"comment": {},
"extattrs": {},
"use_logic_filter_rules": {},
"logic_filter_rules": {}
}

wapi = self._get_wapi(test_object)
res = wapi.run('testobject', test_spec)

self.assertTrue(res['changed'])
wapi.create_object.assert_called_once_with('testobject', {'network': '192.168.10.0/24',
'use_logic_filter_rules': True,
'logic_filter_rules': []
}
)

def test_nios_network_ipv4_update_with_use_logic_filter_rules(self):
self.module.params = {'provider': None, 'state': 'present', 'network': '192.168.10.0/24',
'comment': 'updated comment', 'extattrs': None, 'use_logic_filter_rules': True,
'logic_filter_rules': []}

test_object = [
{
"comment": "test comment",
"_ref": "network/ZG5zLm5ldHdvcmtfdmlldyQw:default/true",
"network": "192.168.10.0/24",
"extattrs": {'options': {'name': 'test', 'value': 'ansible.com'}},
"use_logic_filter_rules": False,
"logic_filter_rules": []
}
]

test_spec = {
"network": {"ib_req": True},
"comment": {},
"extattrs": {},
"use_logic_filter_rules": {},
"logic_filter_rules": {}
}

wapi = self._get_wapi(test_object)
res = wapi.run('testobject', test_spec)

self.assertTrue(res['changed'])

0 comments on commit 49b9351

Please sign in to comment.