| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
from __future__ import absolute_import, division, print_function |
| 5 |
__metaclass__ = type |
| 6 |
|
| 7 |
import copy |
| 8 |
try: |
| 9 |
from ansible_collections.infoblox.nios_modules.tests.unit.compat import unittest |
| 10 |
except ImportError: |
| 11 |
import unittest |
| 12 |
from ansible_collections.infoblox.nios_modules.tests.unit.compat.mock import patch, MagicMock, Mock |
| 13 |
from ansible_collections.infoblox.nios_modules.plugins.module_utils import api |
| 14 |
|
| 15 |
|
| 16 |
class TestNiosApi(unittest.TestCase): |
| 17 |
|
| 18 |
def setUp(self): |
| 19 |
super(TestNiosApi, self).setUp() |
| 20 |
|
| 21 |
self.module = MagicMock(name='AnsibleModule') |
| 22 |
self.module.check_mode = False |
| 23 |
self.module.params = {'provider': None} |
| 24 |
|
| 25 |
self.mock_connector = patch('ansible_collections.infoblox.nios_modules.plugins.module_utils.api.get_connector') |
| 26 |
self.mock_connector.start() |
| 27 |
self.mock_check_type_dict = patch('ansible.module_utils.common.validation.check_type_dict') |
| 28 |
self.mock_check_type_dict_obj = self.mock_check_type_dict.start() |
| 29 |
|
| 30 |
def tearDown(self): |
| 31 |
super(TestNiosApi, self).tearDown() |
| 32 |
|
| 33 |
self.mock_connector.stop() |
| 34 |
self.mock_check_type_dict.stop() |
| 35 |
|
| 36 |
def test_get_provider_spec(self): |
| 37 |
provider_options = ['host', 'username', 'password', 'cert', 'key', 'validate_certs', 'silent_ssl_warnings', |
| 38 |
'http_request_timeout', 'http_pool_connections', |
| 39 |
'http_pool_maxsize', 'max_retries', 'wapi_version', 'max_results'] |
| 40 |
res = api.WapiBase.provider_spec |
| 41 |
self.assertIsNotNone(res) |
| 42 |
self.assertIn('provider', res) |
| 43 |
self.assertIn('options', res['provider']) |
| 44 |
returned_options = res['provider']['options'] |
| 45 |
self.assertEqual(sorted(provider_options), sorted(returned_options.keys())) |
| 46 |
|
| 47 |
def _get_wapi(self, test_object): |
| 48 |
wapi = api.WapiModule(self.module) |
| 49 |
wapi.get_object = Mock(name='get_object', return_value=test_object) |
| 50 |
wapi.create_object = Mock(name='create_object') |
| 51 |
wapi.update_object = Mock(name='update_object') |
| 52 |
wapi.delete_object = Mock(name='delete_object') |
| 53 |
return wapi |
| 54 |
|
| 55 |
def test_wapi_no_change(self): |
| 56 |
self.module.params = {'provider': None, 'state': 'present', 'name': 'default', |
| 57 |
'comment': 'test comment', 'extattrs': None} |
| 58 |
|
| 59 |
test_object = [ |
| 60 |
{ |
| 61 |
"comment": "test comment", |
| 62 |
"_ref": "networkview/ZG5zLm5ldHdvcmtfdmlldyQw:default/true", |
| 63 |
"name": 'default', |
| 64 |
"extattrs": {} |
| 65 |
} |
| 66 |
] |
| 67 |
|
| 68 |
test_spec = { |
| 69 |
"name": {"ib_req": True}, |
| 70 |
"comment": {}, |
| 71 |
"extattrs": {} |
| 72 |
} |
| 73 |
|
| 74 |
wapi = self._get_wapi(test_object) |
| 75 |
res = wapi.run('testobject', test_spec) |
| 76 |
|
| 77 |
self.assertFalse(res['changed']) |
| 78 |
|
| 79 |
def test_wapi_change(self): |
| 80 |
self.module.params = {'provider': None, 'state': 'present', 'name': 'default', |
| 81 |
'comment': 'updated comment', 'extattrs': None} |
| 82 |
ref = "networkview/ZG5zLm5ldHdvcmtfdmlldyQw:default/true" |
| 83 |
test_object = [ |
| 84 |
{ |
| 85 |
"comment": "test comment", |
| 86 |
"_ref": ref, |
| 87 |
"name": "default", |
| 88 |
"extattrs": {} |
| 89 |
} |
| 90 |
] |
| 91 |
|
| 92 |
test_spec = { |
| 93 |
"name": {"ib_req": True}, |
| 94 |
"comment": {}, |
| 95 |
"extattrs": {} |
| 96 |
} |
| 97 |
|
| 98 |
wapi = self._get_wapi(test_object) |
| 99 |
res = wapi.run('testobject', test_spec) |
| 100 |
|
| 101 |
self.assertTrue(res['changed']) |
| 102 |
wapi.update_object.assert_called_once_with(ref, {'comment': 'updated comment', 'name': 'default'}) |
| 103 |
|
| 104 |
def test_wapi_change_false(self): |
| 105 |
self.module.params = {'provider': None, 'state': 'present', 'name': 'default', |
| 106 |
'comment': 'updated comment', 'extattrs': None, 'fqdn': 'foo'} |
| 107 |
ref = "networkview/ZG5zLm5ldHdvcmtfdmlldyQw:default/true" |
| 108 |
test_object = [ |
| 109 |
{ |
| 110 |
"comment": "test comment", |
| 111 |
"_ref": ref, |
| 112 |
"name": "default", |
| 113 |
"extattrs": {} |
| 114 |
} |
| 115 |
] |
| 116 |
|
| 117 |
test_spec = { |
| 118 |
"name": {"ib_req": True}, |
| 119 |
"fqdn": {"ib_req": True, 'update': False}, |
| 120 |
"comment": {}, |
| 121 |
"extattrs": {} |
| 122 |
} |
| 123 |
|
| 124 |
wapi = self._get_wapi(test_object) |
| 125 |
res = wapi.run('testobject', test_spec) |
| 126 |
|
| 127 |
self.assertTrue(res['changed']) |
| 128 |
wapi.update_object.assert_called_once_with( |
| 129 |
ref, {'comment': 'updated comment', 'name': 'default'} |
| 130 |
) |
| 131 |
|
| 132 |
def test_wapi_extattrs_change(self): |
| 133 |
self.module.params = {'provider': None, 'state': 'present', 'name': 'default', |
| 134 |
'comment': 'test comment', 'extattrs': {'Site': 'update'}} |
| 135 |
|
| 136 |
ref = "networkview/ZG5zLm5ldHdvcmtfdmlldyQw:default/true" |
| 137 |
|
| 138 |
test_object = [{ |
| 139 |
"comment": "test comment", |
| 140 |
"_ref": ref, |
| 141 |
"name": "default", |
| 142 |
"extattrs": {'Site': {'value': 'test'}} |
| 143 |
}] |
| 144 |
|
| 145 |
test_spec = { |
| 146 |
"name": {"ib_req": True}, |
| 147 |
"comment": {}, |
| 148 |
"extattrs": {} |
| 149 |
} |
| 150 |
|
| 151 |
kwargs = copy.deepcopy(test_object[0]) |
| 152 |
kwargs['extattrs']['Site']['value'] = 'update' |
| 153 |
kwargs['name'] = 'default' |
| 154 |
del kwargs['_ref'] |
| 155 |
|
| 156 |
wapi = self._get_wapi(test_object) |
| 157 |
res = wapi.run('testobject', test_spec) |
| 158 |
|
| 159 |
self.assertTrue(res['changed']) |
| 160 |
wapi.update_object.assert_called_once_with(ref, kwargs) |
| 161 |
|
| 162 |
def test_wapi_extattrs_nochange(self): |
| 163 |
self.module.params = {'provider': None, 'state': 'present', 'name': 'default', |
| 164 |
'comment': 'test comment', 'extattrs': {'Site': 'test'}} |
| 165 |
|
| 166 |
ref = "networkview/ZG5zLm5ldHdvcmtfdmlldyQw:default/true" |
| 167 |
test_object = [{ |
| 168 |
"comment": "test comment", |
| 169 |
"_ref": ref, |
| 170 |
"name": "default", |
| 171 |
"extattrs": {'Site': {'value': 'test'}} |
| 172 |
}] |
| 173 |
|
| 174 |
test_spec = { |
| 175 |
"name": {"ib_req": True}, |
| 176 |
"comment": {}, |
| 177 |
"extattrs": {} |
| 178 |
} |
| 179 |
|
| 180 |
wapi = self._get_wapi(test_object) |
| 181 |
res = wapi.run('testobject', test_spec) |
| 182 |
|
| 183 |
self.assertFalse(res['changed']) |
| 184 |
wapi.update_object.assert_not_called() |
| 185 |
|
| 186 |
def test_wapi_create(self): |
| 187 |
self.module.params = {'provider': None, 'state': 'present', 'name': 'ansible', |
| 188 |
'comment': None, 'extattrs': None} |
| 189 |
|
| 190 |
test_object = None |
| 191 |
|
| 192 |
test_spec = { |
| 193 |
"name": {"ib_req": True}, |
| 194 |
"comment": {}, |
| 195 |
"extattrs": {} |
| 196 |
} |
| 197 |
|
| 198 |
wapi = self._get_wapi(test_object) |
| 199 |
res = wapi.run('testobject', test_spec) |
| 200 |
|
| 201 |
self.assertTrue(res['changed']) |
| 202 |
wapi.create_object.assert_called_once_with('testobject', {'name': 'ansible'}) |
| 203 |
|
| 204 |
def test_wapi_delete(self): |
| 205 |
self.module.params = {'provider': None, 'state': 'absent', 'name': 'ansible', |
| 206 |
'comment': None, 'extattrs': None} |
| 207 |
|
| 208 |
ref = "networkview/ZG5zLm5ldHdvcmtfdmlldyQw:ansible/false" |
| 209 |
|
| 210 |
test_object = [{ |
| 211 |
"comment": "test comment", |
| 212 |
"_ref": ref, |
| 213 |
"name": "ansible", |
| 214 |
"extattrs": {'Site': {'value': 'test'}} |
| 215 |
}] |
| 216 |
|
| 217 |
test_spec = { |
| 218 |
"name": {"ib_req": True}, |
| 219 |
"comment": {}, |
| 220 |
"extattrs": {} |
| 221 |
} |
| 222 |
|
| 223 |
wapi = self._get_wapi(test_object) |
| 224 |
res = wapi.run('testobject', test_spec) |
| 225 |
|
| 226 |
self.assertTrue(res['changed']) |
| 227 |
wapi.delete_object.assert_called_once_with(ref) |
| 228 |
|
| 229 |
def test_wapi_strip_network_view(self): |
| 230 |
self.module.params = {'provider': None, 'state': 'present', 'name': 'ansible', |
| 231 |
'comment': 'updated comment', 'extattrs': None, |
| 232 |
'network_view': 'default'} |
| 233 |
|
| 234 |
test_object = [{ |
| 235 |
"comment": "test comment", |
| 236 |
"_ref": "view/ZG5zLm5ldHdvcmtfdmlldyQw:ansible/true", |
| 237 |
"name": "ansible", |
| 238 |
"extattrs": {}, |
| 239 |
"network_view": "default" |
| 240 |
}] |
| 241 |
|
| 242 |
test_spec = { |
| 243 |
"name": {"ib_req": True}, |
| 244 |
"network_view": {"ib_req": True}, |
| 245 |
"comment": {}, |
| 246 |
"extattrs": {} |
| 247 |
} |
| 248 |
|
| 249 |
kwargs = test_object[0].copy() |
| 250 |
ref = kwargs.pop('_ref') |
| 251 |
kwargs['comment'] = 'updated comment' |
| 252 |
kwargs['name'] = 'ansible' |
| 253 |
del kwargs['network_view'] |
| 254 |
del kwargs['extattrs'] |
| 255 |
|
| 256 |
wapi = self._get_wapi(test_object) |
| 257 |
res = wapi.run('testobject', test_spec) |
| 258 |
|
| 259 |
self.assertTrue(res['changed']) |
| 260 |
wapi.update_object.assert_called_once_with(ref, kwargs) |