| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
from __future__ import (absolute_import, division, print_function) |
| 18 |
__metaclass__ = type |
| 19 |
|
| 20 |
|
| 21 |
from ansible_collections.infoblox.nios_modules.plugins.modules import nios_member |
| 22 |
from ansible_collections.infoblox.nios_modules.plugins.module_utils import api |
| 23 |
from ansible_collections.infoblox.nios_modules.tests.unit.compat.mock import patch, MagicMock, Mock |
| 24 |
from .test_nios_module import TestNiosModule, load_fixture |
| 25 |
|
| 26 |
|
| 27 |
class TestNiosMemberModule(TestNiosModule): |
| 28 |
|
| 29 |
module = nios_member |
| 30 |
|
| 31 |
def setUp(self): |
| 32 |
super(TestNiosMemberModule, self).setUp() |
| 33 |
self.module = MagicMock(name='ansible_collections.infoblox.nios_modules.plugins.modules.nios_member.WapiModule') |
| 34 |
self.module.check_mode = False |
| 35 |
self.module.params = {'provider': None} |
| 36 |
self.mock_wapi = patch('ansible_collections.infoblox.nios_modules.plugins.modules.nios_member.WapiModule') |
| 37 |
self.exec_command = self.mock_wapi.start() |
| 38 |
self.mock_wapi_run = patch('ansible_collections.infoblox.nios_modules.plugins.modules.nios_member.WapiModule.run') |
| 39 |
self.load_config = self.mock_wapi_run.start() |
| 40 |
|
| 41 |
def tearDown(self): |
| 42 |
super(TestNiosMemberModule, self).tearDown() |
| 43 |
self.mock_wapi.stop() |
| 44 |
self.mock_wapi_run.stop() |
| 45 |
|
| 46 |
def load_fixtures(self, commands=None): |
| 47 |
self.exec_command.return_value = (0, load_fixture('nios_result.txt').strip(), None) |
| 48 |
self.load_config.return_value = dict(diff=None, session='session') |
| 49 |
|
| 50 |
def _get_wapi(self, test_object): |
| 51 |
wapi = api.WapiModule(self.module) |
| 52 |
wapi.get_object = Mock(name='get_object', return_value=test_object) |
| 53 |
wapi.create_object = Mock(name='create_object') |
| 54 |
wapi.update_object = Mock(name='update_object') |
| 55 |
wapi.delete_object = Mock(name='delete_object') |
| 56 |
return wapi |
| 57 |
|
| 58 |
def test_nios_member_create(self): |
| 59 |
self.module.params = {'provider': None, 'state': 'present', 'host_name': 'test_member', |
| 60 |
'vip_setting': {'address': '192.168.1.110', 'subnet_mask': '255.255.255.0', 'gateway': '192.168.1.1'}, |
| 61 |
'config_addr_type': 'IPV4', 'platform': 'VNIOS', 'comment': None, 'extattrs': None} |
| 62 |
|
| 63 |
test_object = None |
| 64 |
test_spec = { |
| 65 |
"host_name": {"ib_req": True}, |
| 66 |
"vip_setting": {}, |
| 67 |
"config_addr_type": {}, |
| 68 |
"platform": {}, |
| 69 |
"comment": {}, |
| 70 |
"extattrs": {} |
| 71 |
} |
| 72 |
|
| 73 |
wapi = self._get_wapi(test_object) |
| 74 |
res = wapi.run('testobject', test_spec) |
| 75 |
|
| 76 |
self.assertTrue(res['changed']) |
| 77 |
wapi.create_object.assert_called_once_with('testobject', {'host_name': 'test_member', |
| 78 |
'vip_setting': {'address': '192.168.1.110', 'subnet_mask': '255.255.255.0', |
| 79 |
'gateway': '192.168.1.1'}, |
| 80 |
'config_addr_type': 'IPV4', 'platform': 'VNIOS'}) |
| 81 |
|
| 82 |
def test_nios_member_update(self): |
| 83 |
self.module.params = {'provider': None, 'state': 'present', 'host_name': 'test_member', |
| 84 |
'vip_setting': {'address': '192.168.1.110', 'subnet_mask': '255.255.255.0', 'gateway': '192.168.1.1'}, |
| 85 |
'config_addr_type': 'IPV4', 'platform': 'VNIOS', 'comment': 'updated comment', 'extattrs': None} |
| 86 |
ref = "member/b25lLnZpcnR1YWxfbm9kZSQ3:member01.ansible-dev.com" |
| 87 |
test_object = [ |
| 88 |
{ |
| 89 |
"comment": "Created with Ansible", |
| 90 |
"_ref": ref, |
| 91 |
"config_addr_type": "IPV4", |
| 92 |
"host_name": "member01.ansible-dev.com", |
| 93 |
"platform": "VNIOS", |
| 94 |
"service_type_configuration": "ALL_V4", |
| 95 |
"vip_setting": |
| 96 |
{ |
| 97 |
"address": "192.168.1.100", |
| 98 |
"dscp": 0, |
| 99 |
"gateway": "192.168.1.1", |
| 100 |
"primary": True, |
| 101 |
"subnet_mask": "255.255.255.0", |
| 102 |
"use_dscp": False |
| 103 |
} |
| 104 |
} |
| 105 |
] |
| 106 |
|
| 107 |
test_spec = { |
| 108 |
"host_name": {"ib_req": True}, |
| 109 |
"vip_setting": {}, |
| 110 |
"config_addr_type": {}, |
| 111 |
"platform": {}, |
| 112 |
"comment": {}, |
| 113 |
"extattrs": {} |
| 114 |
} |
| 115 |
|
| 116 |
wapi = self._get_wapi(test_object) |
| 117 |
res = wapi.run('testobject', test_spec) |
| 118 |
|
| 119 |
self.assertTrue(res['changed']) |
| 120 |
wapi.update_object.assert_called_once_with( |
| 121 |
ref, |
| 122 |
{'comment': 'updated comment', 'host_name': 'test_member', |
| 123 |
'vip_setting': {'address': '192.168.1.110', 'subnet_mask': '255.255.255.0', 'gateway': '192.168.1.1'}, |
| 124 |
'config_addr_type': 'IPV4', 'platform': 'VNIOS'} |
| 125 |
) |
| 126 |
|
| 127 |
def test_nios_member_remove(self): |
| 128 |
self.module.params = {'provider': None, 'state': 'absent', 'host_name': 'test_member', |
| 129 |
'vip_setting': {'address': '192.168.1.110', 'subnet_mask': '255.255.255.0', 'gateway': '192.168.1.1'}, |
| 130 |
'config_addr_type': 'IPV4', 'platform': 'VNIOS', 'comment': 'updated comment', 'extattrs': None} |
| 131 |
|
| 132 |
ref = "member/b25lLnZpcnR1YWxfbm9kZSQ3:member01.ansible-dev.com" |
| 133 |
|
| 134 |
test_object = [ |
| 135 |
{ |
| 136 |
"comment": "Created with Ansible", |
| 137 |
"_ref": "member/b25lLnZpcnR1YWxfbm9kZSQ3:member01.ansible-dev.com", |
| 138 |
"config_addr_type": "IPV4", |
| 139 |
"host_name": "member01.ansible-dev.com", |
| 140 |
"platform": "VNIOS", |
| 141 |
"service_type_configuration": "ALL_V4", |
| 142 |
"vip_setting": |
| 143 |
{ |
| 144 |
"address": "192.168.1.100", |
| 145 |
"dscp": 0, |
| 146 |
"gateway": "192.168.1.1", |
| 147 |
"primary": True, |
| 148 |
"subnet_mask": "255.255.255.0", |
| 149 |
"use_dscp": False |
| 150 |
} |
| 151 |
} |
| 152 |
] |
| 153 |
|
| 154 |
test_spec = { |
| 155 |
"host_name": {"ib_req": True}, |
| 156 |
"vip_setting": {}, |
| 157 |
"config_addr_type": {}, |
| 158 |
"platform": {}, |
| 159 |
"comment": {}, |
| 160 |
"extattrs": {} |
| 161 |
} |
| 162 |
|
| 163 |
wapi = self._get_wapi(test_object) |
| 164 |
res = wapi.run('testobject', test_spec) |
| 165 |
|
| 166 |
self.assertTrue(res['changed']) |
| 167 |
wapi.delete_object.assert_called_once_with(ref) |