| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
from __future__ import (absolute_import, division, print_function) |
| 20 |
__metaclass__ = type |
| 21 |
|
| 22 |
|
| 23 |
from ansible_collections.infoblox.nios_modules.plugins.modules import nios_zone |
| 24 |
from ansible_collections.infoblox.nios_modules.plugins.module_utils import api |
| 25 |
from ansible_collections.infoblox.nios_modules.tests.unit.compat.mock import patch, MagicMock, Mock |
| 26 |
from .test_nios_module import TestNiosModule, load_fixture |
| 27 |
|
| 28 |
|
| 29 |
class TestNiosZoneModule(TestNiosModule): |
| 30 |
|
| 31 |
module = nios_zone |
| 32 |
|
| 33 |
def setUp(self): |
| 34 |
super(TestNiosZoneModule, self).setUp() |
| 35 |
self.module = MagicMock(name='ansible_collections.infoblox.nios_modules.plugins.modules.nios_zone.WapiModule') |
| 36 |
self.module.check_mode = False |
| 37 |
self.module.params = {'provider': None} |
| 38 |
self.mock_wapi = patch('ansible_collections.infoblox.nios_modules.plugins.modules.nios_zone.WapiModule') |
| 39 |
self.exec_command = self.mock_wapi.start() |
| 40 |
self.mock_wapi_run = patch('ansible_collections.infoblox.nios_modules.plugins.modules.nios_zone.WapiModule.run') |
| 41 |
self.load_config = self.mock_wapi_run.start() |
| 42 |
|
| 43 |
def tearDown(self): |
| 44 |
super(TestNiosZoneModule, self).tearDown() |
| 45 |
self.mock_wapi.stop() |
| 46 |
self.mock_wapi_run.stop() |
| 47 |
|
| 48 |
def _get_wapi(self, test_object): |
| 49 |
wapi = api.WapiModule(self.module) |
| 50 |
wapi.get_object = Mock(name='get_object', return_value=test_object) |
| 51 |
wapi.create_object = Mock(name='create_object') |
| 52 |
wapi.update_object = Mock(name='update_object') |
| 53 |
wapi.delete_object = Mock(name='delete_object') |
| 54 |
return wapi |
| 55 |
|
| 56 |
def load_fixtures(self, commands=None): |
| 57 |
self.exec_command.return_value = (0, load_fixture('nios_result.txt').strip(), None) |
| 58 |
self.load_config.return_value = dict(diff=None, session='session') |
| 59 |
|
| 60 |
def test_nios_zone_create(self): |
| 61 |
self.module.params = {'provider': None, 'state': 'present', 'fqdn': 'ansible.com', |
| 62 |
'comment': None, 'extattrs': None} |
| 63 |
|
| 64 |
test_object = None |
| 65 |
|
| 66 |
test_spec = { |
| 67 |
"fqdn": {"ib_req": True}, |
| 68 |
"comment": {}, |
| 69 |
"extattrs": {} |
| 70 |
} |
| 71 |
|
| 72 |
wapi = self._get_wapi(test_object) |
| 73 |
print("WAPI: ", wapi) |
| 74 |
res = wapi.run('testobject', test_spec) |
| 75 |
|
| 76 |
self.assertTrue(res['changed']) |
| 77 |
wapi.create_object.assert_called_once_with('testobject', {'fqdn': 'ansible.com'}) |
| 78 |
|
| 79 |
def test_nios_zone_remove(self): |
| 80 |
self.module.params = {'provider': None, 'state': 'absent', 'fqdn': 'ansible.com', |
| 81 |
'comment': None, 'extattrs': None} |
| 82 |
|
| 83 |
ref = "zone/ZG5zLm5ldHdvcmtfdmlldyQw:ansible/false" |
| 84 |
|
| 85 |
test_object = [{ |
| 86 |
"comment": "test comment", |
| 87 |
"_ref": ref, |
| 88 |
"fqdn": "ansible.com", |
| 89 |
"extattrs": {'Site': {'value': 'test'}} |
| 90 |
}] |
| 91 |
|
| 92 |
test_spec = { |
| 93 |
"fqdn": {"ib_req": True}, |
| 94 |
"comment": {}, |
| 95 |
"extattrs": {} |
| 96 |
} |
| 97 |
wapi = self._get_wapi(test_object) |
| 98 |
res = wapi.run('testobject', test_spec) |
| 99 |
|
| 100 |
self.assertTrue(res['changed']) |
| 101 |
wapi.delete_object.assert_called_once_with(ref) |
| 102 |
|
| 103 |
def test_nios_zone_update_comment(self): |
| 104 |
self.module.params = {'provider': None, 'state': 'present', 'fqdn': 'ansible.com', |
| 105 |
'comment': 'updated comment', 'extattrs': None} |
| 106 |
ref = "zone/ZG5zLm5ldHdvcmtfdmlldyQw:default/true" |
| 107 |
test_object = [ |
| 108 |
{ |
| 109 |
"comment": "test comment", |
| 110 |
"_ref": ref, |
| 111 |
"fqdn": "ansible.com", |
| 112 |
"extattrs": {'Site': {'value': 'test'}} |
| 113 |
} |
| 114 |
] |
| 115 |
|
| 116 |
test_spec = { |
| 117 |
"fqdn": {"ib_req": True}, |
| 118 |
"comment": {}, |
| 119 |
"extattrs": {} |
| 120 |
} |
| 121 |
|
| 122 |
wapi = self._get_wapi(test_object) |
| 123 |
res = wapi.run('testobject', test_spec) |
| 124 |
|
| 125 |
self.assertTrue(res['changed']) |
| 126 |
wapi.update_object.assert_called_once_with(ref, {'comment': 'updated comment', 'fqdn': 'ansible.com'}) |
| 127 |
|
| 128 |
def test_nios_zone_create_using_grid_primary_secondaries(self): |
| 129 |
self.module.params = {'provider': None, 'state': 'present', 'fqdn': 'ansible.com', |
| 130 |
'grid_primary': [{"name": "gridprimary.grid.com"}], |
| 131 |
'grid_secondaries': [{"name": "gridsecondary1.grid.com"}, |
| 132 |
{"name": "gridsecondary2.grid.com"}], |
| 133 |
'restart_if_needed': True, |
| 134 |
'comment': None, 'extattrs': None} |
| 135 |
|
| 136 |
test_object = None |
| 137 |
grid_spec = dict( |
| 138 |
name=dict(required=True), |
| 139 |
) |
| 140 |
test_spec = { |
| 141 |
"fqdn": {"ib_req": True}, |
| 142 |
"grid_primary": {}, |
| 143 |
"grid_secondaries": {}, |
| 144 |
"restart_if_needed": {}, |
| 145 |
"comment": {}, |
| 146 |
"extattrs": {} |
| 147 |
} |
| 148 |
|
| 149 |
wapi = self._get_wapi(test_object) |
| 150 |
print("WAPI: ", wapi) |
| 151 |
res = wapi.run('testobject', test_spec) |
| 152 |
|
| 153 |
self.assertTrue(res['changed']) |
| 154 |
wapi.create_object.assert_called_once_with('testobject', {'fqdn': 'ansible.com', |
| 155 |
"grid_primary": [{"name": "gridprimary.grid.com"}], |
| 156 |
"grid_secondaries": [{"name": "gridsecondary1.grid.com"}, |
| 157 |
{"name": "gridsecondary2.grid.com"}], |
| 158 |
"restart_if_needed": True |
| 159 |
}) |
| 160 |
|
| 161 |
def test_nios_zone_remove_using_grid_primary_secondaries(self): |
| 162 |
self.module.params = {'provider': None, 'state': 'absent', 'fqdn': 'ansible.com', |
| 163 |
'grid_primary': [{"name": "gridprimary.grid.com"}], |
| 164 |
'grid_secondaries': [{"name": "gridsecondary1.grid.com"}, |
| 165 |
{"name": "gridsecondary2.grid.com"}], |
| 166 |
'restart_if_needed': True, |
| 167 |
'comment': None, 'extattrs': None} |
| 168 |
|
| 169 |
ref = "zone/ZG5zLm5ldHdvcmtfdmlldyQw:ansible/false" |
| 170 |
|
| 171 |
test_object = [{ |
| 172 |
"comment": "test comment", |
| 173 |
"_ref": ref, |
| 174 |
"fqdn": "ansible.com", |
| 175 |
"grid_primary": [{"name": "gridprimary.grid.com"}], |
| 176 |
"grid_secondaries": [{"name": "gridsecondary1.grid.com"}, {"name": "gridsecondary2.grid.com"}], |
| 177 |
"restart_if_needed": True, |
| 178 |
"extattrs": {'Site': {'value': 'test'}} |
| 179 |
}] |
| 180 |
|
| 181 |
test_spec = { |
| 182 |
"fqdn": {"ib_req": True}, |
| 183 |
"grid_primary": {}, |
| 184 |
"grid_secondaries": {}, |
| 185 |
"restart_if_needed": {}, |
| 186 |
"comment": {}, |
| 187 |
"extattrs": {} |
| 188 |
} |
| 189 |
wapi = self._get_wapi(test_object) |
| 190 |
res = wapi.run('testobject', test_spec) |
| 191 |
|
| 192 |
self.assertTrue(res['changed']) |
| 193 |
wapi.delete_object.assert_called_once_with(ref) |
| 194 |
|
| 195 |
def test_nios_zone_create_using_name_server_group(self): |
| 196 |
self.module.params = {'provider': None, 'state': 'present', 'fqdn': 'ansible.com', |
| 197 |
'ns_group': 'examplensg', 'comment': None, 'extattrs': None} |
| 198 |
|
| 199 |
test_object = None |
| 200 |
|
| 201 |
test_spec = { |
| 202 |
"fqdn": {"ib_req": True}, |
| 203 |
"ns_group": {}, |
| 204 |
"comment": {}, |
| 205 |
"extattrs": {} |
| 206 |
} |
| 207 |
|
| 208 |
wapi = self._get_wapi(test_object) |
| 209 |
print("WAPI: ", wapi) |
| 210 |
res = wapi.run('testobject', test_spec) |
| 211 |
|
| 212 |
self.assertTrue(res['changed']) |
| 213 |
wapi.create_object.assert_called_once_with('testobject', {'fqdn': 'ansible.com', |
| 214 |
'ns_group': 'examplensg'}) |
| 215 |
|
| 216 |
def test_nios_zone_remove_using_name_server_group(self): |
| 217 |
self.module.params = {'provider': None, 'state': 'absent', 'fqdn': 'ansible.com', |
| 218 |
'ns_group': 'examplensg', 'comment': None, 'extattrs': None} |
| 219 |
|
| 220 |
ref = "zone/ZG5zLm5ldHdvcmtfdmlldyQw:ansible/false" |
| 221 |
|
| 222 |
test_object = [{ |
| 223 |
"comment": "test comment", |
| 224 |
"_ref": ref, |
| 225 |
"fqdn": "ansible.com", |
| 226 |
"ns_group": "examplensg", |
| 227 |
"extattrs": {'Site': {'value': 'test'}} |
| 228 |
}] |
| 229 |
|
| 230 |
test_spec = { |
| 231 |
"fqdn": {"ib_req": True}, |
| 232 |
"ns_group": {}, |
| 233 |
"comment": {}, |
| 234 |
"extattrs": {} |
| 235 |
} |
| 236 |
wapi = self._get_wapi(test_object) |
| 237 |
res = wapi.run('testobject', test_spec) |
| 238 |
|
| 239 |
self.assertTrue(res['changed']) |
| 240 |
wapi.delete_object.assert_called_once_with(ref) |
| 241 |
|
| 242 |
def test_nios_zone_create_using_zone_format(self): |
| 243 |
self.module.params = {'provider': None, 'state': 'present', 'fqdn': '10.10.10.in-addr.arpa', |
| 244 |
'zone_format': 'IPV4', 'comment': None, 'extattrs': None} |
| 245 |
|
| 246 |
test_object = None |
| 247 |
|
| 248 |
test_spec = { |
| 249 |
"fqdn": {"ib_req": True}, |
| 250 |
"zone_format": {}, |
| 251 |
"comment": {}, |
| 252 |
"extattrs": {} |
| 253 |
} |
| 254 |
|
| 255 |
wapi = self._get_wapi(test_object) |
| 256 |
print("WAPI: ", wapi) |
| 257 |
res = wapi.run('testobject', test_spec) |
| 258 |
|
| 259 |
self.assertTrue(res['changed']) |
| 260 |
wapi.create_object.assert_called_once_with('testobject', {'fqdn': '10.10.10.in-addr.arpa', |
| 261 |
'zone_format': 'IPV4'}) |
| 262 |
|
| 263 |
def test_nios_zone_remove_using_using_zone_format(self): |
| 264 |
self.module.params = {'provider': None, 'state': 'absent', 'fqdn': 'ansible.com', |
| 265 |
'zone_format': 'IPV4', 'comment': None, 'extattrs': None} |
| 266 |
|
| 267 |
ref = "zone/ZG5zLm5ldHdvcmtfdmlldyQw:ansible/false" |
| 268 |
|
| 269 |
test_object = [{ |
| 270 |
"comment": "test comment", |
| 271 |
"_ref": ref, |
| 272 |
"fqdn": "ansible.com", |
| 273 |
"zone_format": "IPV4", |
| 274 |
"extattrs": {'Site': {'value': 'test'}} |
| 275 |
}] |
| 276 |
|
| 277 |
test_spec = { |
| 278 |
"fqdn": {"ib_req": True}, |
| 279 |
"zone_format": {}, |
| 280 |
"comment": {}, |
| 281 |
"extattrs": {} |
| 282 |
} |
| 283 |
wapi = self._get_wapi(test_object) |
| 284 |
res = wapi.run('testobject', test_spec) |
| 285 |
|
| 286 |
self.assertTrue(res['changed']) |
| 287 |
wapi.delete_object.assert_called_once_with(ref) |