| 1 |
# (c) 2018-2022, NetApp, Inc |
| 2 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
|
| 4 |
''' unit test template for ONTAP Ansible module ''' |
| 5 |
|
| 6 |
from __future__ import (absolute_import, division, print_function) |
| 7 |
__metaclass__ = type |
| 8 |
import pytest |
| 9 |
import sys |
| 10 |
import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils |
| 11 |
# pylint: disable=unused-import |
| 12 |
from ansible_collections.netapp.ontap.tests.unit.plugins.module_utils.ansible_mocks import set_module_args, \ |
| 13 |
AnsibleFailJson, patch_ansible, create_module, create_and_apply, assert_warning_was_raised, assert_no_warnings, print_warnings |
| 14 |
from ansible_collections.netapp.ontap.tests.unit.framework.mock_rest_and_zapi_requests import patch_request_and_invoke, \ |
| 15 |
register_responses |
| 16 |
from ansible_collections.netapp.ontap.tests.unit.framework.zapi_factory import build_zapi_response, zapi_responses |
| 17 |
from ansible_collections.netapp.ontap.tests.unit.framework.rest_factory import rest_responses |
| 18 |
from ansible_collections.netapp.ontap.plugins.modules.na_ontap_igroup \ |
| 19 |
import NetAppOntapIgroup as igroup # module under test |
| 20 |
|
| 21 |
if not netapp_utils.has_netapp_lib(): |
| 22 |
pytestmark = pytest.mark.skip('skipping as missing required netapp_lib') |
| 23 |
|
| 24 |
if not netapp_utils.HAS_REQUESTS and sys.version_info < (2, 7): |
| 25 |
pytestmark = pytest.mark.skip('Skipping Unit Tests on 2.6 as requests is not available') |
| 26 |
|
| 27 |
|
| 28 |
DEFAULT_ARGS = { |
| 29 |
'vserver': 'vserver', |
| 30 |
'name': 'test', |
| 31 |
'initiator_names': 'init1', |
| 32 |
'ostype': 'linux', |
| 33 |
'initiator_group_type': 'fcp', |
| 34 |
'bind_portset': 'true', |
| 35 |
'hostname': 'hostname', |
| 36 |
'username': 'username', |
| 37 |
'password': 'password', |
| 38 |
'use_rest': 'never' |
| 39 |
} |
| 40 |
|
| 41 |
igroup_with_initiator = { |
| 42 |
'num-records': 1, |
| 43 |
'attributes-list': { |
| 44 |
'vserver': 'vserver', |
| 45 |
'initiator-group-os-type': 'linux', |
| 46 |
'initiator-group-info': { |
| 47 |
'initiators': [ |
| 48 |
{'initiator-info': {'initiator-name': 'init1'}}, |
| 49 |
{'initiator-info': {'initiator-name': 'init2'}} |
| 50 |
] |
| 51 |
} |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
igroup_without_initiator = { |
| 56 |
'num-records': 1, |
| 57 |
'attributes-list': { |
| 58 |
'initiator-group-info': {'vserver': 'test'} |
| 59 |
} |
| 60 |
} |
| 61 |
|
| 62 |
ZRR = zapi_responses({ |
| 63 |
'igroup_with_initiator_info': build_zapi_response(igroup_with_initiator), |
| 64 |
'igroup_without_initiator_info': build_zapi_response(igroup_without_initiator) |
| 65 |
}) |
| 66 |
|
| 67 |
|
| 68 |
def test_module_fail_when_required_args_missing(): |
| 69 |
''' required arguments are reported as errors ''' |
| 70 |
with pytest.raises(AnsibleFailJson) as exc: |
| 71 |
set_module_args({}) |
| 72 |
igroup() |
| 73 |
msg = 'missing required arguments:' |
| 74 |
assert msg in exc.value.args[0]['msg'] |
| 75 |
|
| 76 |
|
| 77 |
def test_get_nonexistent_igroup(): |
| 78 |
''' Test if get_igroup returns None for non-existent igroup ''' |
| 79 |
register_responses([ |
| 80 |
('igroup-get-iter', ZRR['empty']) |
| 81 |
]) |
| 82 |
igroup_obj = create_module(igroup, DEFAULT_ARGS) |
| 83 |
result = igroup_obj.get_igroup('dummy') |
| 84 |
assert result is None |
| 85 |
|
| 86 |
|
| 87 |
def test_get_existing_igroup_with_initiators(): |
| 88 |
''' Test if get_igroup returns list of existing initiators ''' |
| 89 |
register_responses([ |
| 90 |
('igroup-get-iter', ZRR['igroup_with_initiator_info']) |
| 91 |
]) |
| 92 |
igroup_obj = create_module(igroup, DEFAULT_ARGS) |
| 93 |
result = igroup_obj.get_igroup('igroup') |
| 94 |
assert DEFAULT_ARGS['initiator_names'] in result['initiator_names'] |
| 95 |
assert result['initiator_names'] == ['init1', 'init2'] |
| 96 |
|
| 97 |
|
| 98 |
def test_get_existing_igroup_without_initiators(): |
| 99 |
''' Test if get_igroup returns empty list() ''' |
| 100 |
register_responses([ |
| 101 |
('igroup-get-iter', ZRR['igroup_without_initiator_info']) |
| 102 |
]) |
| 103 |
igroup_obj = create_module(igroup, DEFAULT_ARGS) |
| 104 |
result = igroup_obj.get_igroup('igroup') |
| 105 |
assert result['initiator_names'] == [] |
| 106 |
|
| 107 |
|
| 108 |
def test_modify_initiator_calls_add_and_remove(): |
| 109 |
'''Test remove_initiator() is called followed by add_initiator() on modify operation''' |
| 110 |
register_responses([ |
| 111 |
('igroup-get-iter', ZRR['igroup_with_initiator_info']), |
| 112 |
('igroup-remove', ZRR['success']), |
| 113 |
('igroup-remove', ZRR['success']), |
| 114 |
('igroup-add', ZRR['success']) |
| 115 |
]) |
| 116 |
igroup_obj = create_and_apply(igroup, DEFAULT_ARGS, {'initiator_names': 'replacewithme'})['changed'] |
| 117 |
|
| 118 |
|
| 119 |
def test_modify_called_from_add(): |
| 120 |
'''Test remove_initiator() and add_initiator() calls modify''' |
| 121 |
register_responses([ |
| 122 |
('igroup-get-iter', ZRR['igroup_without_initiator_info']), |
| 123 |
('igroup-add', ZRR['success']) |
| 124 |
]) |
| 125 |
igroup_obj = create_and_apply(igroup, DEFAULT_ARGS, {'initiator_names': 'replacewithme'})['changed'] |
| 126 |
|
| 127 |
|
| 128 |
def test_modify_called_from_remove(): |
| 129 |
'''Test remove_initiator() and add_initiator() calls modify''' |
| 130 |
register_responses([ |
| 131 |
('igroup-get-iter', ZRR['igroup_with_initiator_info']), |
| 132 |
('igroup-remove', ZRR['success']), |
| 133 |
('igroup-remove', ZRR['success']) |
| 134 |
]) |
| 135 |
igroup_obj = create_and_apply(igroup, DEFAULT_ARGS, {'initiator_names': ''})['changed'] |
| 136 |
|
| 137 |
|
| 138 |
def test_successful_create(): |
| 139 |
''' Test successful create ''' |
| 140 |
register_responses([ |
| 141 |
('igroup-get-iter', ZRR['empty']), |
| 142 |
('igroup-create', ZRR['success']), |
| 143 |
('igroup-add', ZRR['success']) |
| 144 |
]) |
| 145 |
igroup_obj = create_and_apply(igroup, DEFAULT_ARGS)['changed'] |
| 146 |
|
| 147 |
|
| 148 |
def test_successful_delete(): |
| 149 |
''' Test successful delete ''' |
| 150 |
register_responses([ |
| 151 |
('igroup-get-iter', ZRR['igroup_with_initiator_info']), |
| 152 |
('igroup-destroy', ZRR['success']) |
| 153 |
]) |
| 154 |
igroup_obj = create_and_apply(igroup, DEFAULT_ARGS, {'state': 'absent'})['changed'] |
| 155 |
|
| 156 |
|
| 157 |
def test_successful_rename(): |
| 158 |
'''Test successful rename''' |
| 159 |
register_responses([ |
| 160 |
('igroup-get-iter', ZRR['empty']), |
| 161 |
('igroup-get-iter', ZRR['igroup_with_initiator_info']), |
| 162 |
('igroup-rename', ZRR['success']), |
| 163 |
('igroup-remove', ZRR['success']), |
| 164 |
]) |
| 165 |
args = { |
| 166 |
'from_name': 'test', |
| 167 |
'name': 'test_new' |
| 168 |
} |
| 169 |
assert create_and_apply(igroup, DEFAULT_ARGS, args)['changed'] |
| 170 |
|
| 171 |
|
| 172 |
def test_negative_modify_anything_zapi(): |
| 173 |
''' Test ZAPI option not currently supported in REST is rejected ''' |
| 174 |
register_responses([ |
| 175 |
('igroup-get-iter', ZRR['igroup_with_initiator_info']), |
| 176 |
]) |
| 177 |
args = { |
| 178 |
'vserver': 'my_vserver', |
| 179 |
'use_rest': 'never' |
| 180 |
} |
| 181 |
msg = "Error: modifying {'vserver': 'my_vserver'} is not supported in ZAPI" |
| 182 |
assert msg in create_and_apply(igroup, DEFAULT_ARGS, args, fail=True)['msg'] |
| 183 |
|
| 184 |
|
| 185 |
def test_negative_mutually_exclusive(): |
| 186 |
''' Test ZAPI option not currently supported in REST is rejected ''' |
| 187 |
args = { |
| 188 |
'use_rest': 'auto', |
| 189 |
'igroups': 'my_group' |
| 190 |
} |
| 191 |
msg = "parameters are mutually exclusive: igroups|initiator_names" |
| 192 |
assert msg in create_module(igroup, DEFAULT_ARGS, args, fail=True)['msg'] |
| 193 |
|
| 194 |
|
| 195 |
def test_negative_igroups_require_rest(): |
| 196 |
''' Test ZAPI option not currently supported in REST is rejected ''' |
| 197 |
DEFAULT_ARGS_COPY = DEFAULT_ARGS.copy() |
| 198 |
del DEFAULT_ARGS_COPY['initiator_names'] |
| 199 |
args = { |
| 200 |
'igroups': 'my_group' |
| 201 |
} |
| 202 |
msg = "requires ONTAP 9.9.1 or later and REST must be enabled" |
| 203 |
assert msg in create_module(igroup, DEFAULT_ARGS_COPY, args, fail=True)['msg'] |
| 204 |
|
| 205 |
|
| 206 |
SRR = rest_responses({ |
| 207 |
'one_igroup_record': (200, dict(records=[ |
| 208 |
dict(uuid='a1b2c3', |
| 209 |
name='test', |
| 210 |
svm=dict(name='vserver'), |
| 211 |
initiators=[{'name': 'todelete'}], |
| 212 |
protocol='fcp', |
| 213 |
os_type='aix') |
| 214 |
], num_records=1), None), |
| 215 |
'one_record_uuid': (200, dict(records=[dict(uuid='a1b2c3')], num_records=1), None) |
| 216 |
}) |
| 217 |
|
| 218 |
|
| 219 |
def test_successful_create_rest(): |
| 220 |
''' Test successful create ''' |
| 221 |
register_responses([ |
| 222 |
('GET', 'cluster', SRR['is_rest_9_9_0']), |
| 223 |
('GET', 'protocols/san/igroups', SRR['empty_records']), |
| 224 |
('POST', 'protocols/san/igroups', SRR['success']) |
| 225 |
]) |
| 226 |
assert create_and_apply(igroup, DEFAULT_ARGS, {'use_rest': 'always'})['changed'] |
| 227 |
|
| 228 |
|
| 229 |
def test_incomplete_record_rest(): |
| 230 |
''' Test successful create ''' |
| 231 |
register_responses([ |
| 232 |
('GET', 'cluster', SRR['is_rest_9_9_0']), |
| 233 |
('GET', 'protocols/san/igroups', SRR['one_record_uuid']) |
| 234 |
]) |
| 235 |
msg = "Error: unexpected igroup body:" |
| 236 |
assert msg in create_and_apply(igroup, DEFAULT_ARGS, {'use_rest': 'always'}, fail=True)['msg'] |
| 237 |
|
| 238 |
|
| 239 |
def test_successful_delete_rest(): |
| 240 |
''' Test successful delete ''' |
| 241 |
register_responses([ |
| 242 |
('GET', 'cluster', SRR['is_rest_9_9_0']), |
| 243 |
('GET', 'protocols/san/igroups', SRR['one_igroup_record']), |
| 244 |
('DELETE', 'protocols/san/igroups/a1b2c3', SRR['success']) |
| 245 |
]) |
| 246 |
args = {'state': 'absent', 'use_rest': 'always'} |
| 247 |
assert create_and_apply(igroup, DEFAULT_ARGS, args)['changed'] |
| 248 |
|
| 249 |
|
| 250 |
def test_successful_modify_rest(): |
| 251 |
''' Test successful modify ''' |
| 252 |
register_responses([ |
| 253 |
('GET', 'cluster', SRR['is_rest_9_9_0']), |
| 254 |
('GET', 'protocols/san/igroups', SRR['one_igroup_record']), |
| 255 |
('DELETE', 'protocols/san/igroups/a1b2c3/initiators/todelete', SRR['success']), |
| 256 |
('POST', 'protocols/san/igroups/a1b2c3/initiators', SRR['success']), |
| 257 |
('PATCH', 'protocols/san/igroups/a1b2c3', SRR['success']) |
| 258 |
]) |
| 259 |
assert create_and_apply(igroup, DEFAULT_ARGS, {'use_rest': 'always'})['changed'] |
| 260 |
|
| 261 |
|
| 262 |
def test_successful_modify_initiator_objects_rest(): |
| 263 |
''' Test successful modify ''' |
| 264 |
register_responses([ |
| 265 |
('GET', 'cluster', SRR['is_rest_9_9_1']), |
| 266 |
('GET', 'protocols/san/igroups', SRR['one_igroup_record']), |
| 267 |
('DELETE', 'protocols/san/igroups/a1b2c3/initiators/todelete', SRR['success']), |
| 268 |
('POST', 'protocols/san/igroups/a1b2c3/initiators', SRR['success']), |
| 269 |
('PATCH', 'protocols/san/igroups/a1b2c3', SRR['success']) |
| 270 |
]) |
| 271 |
DEFAULT_ARGS_COPY = DEFAULT_ARGS.copy() |
| 272 |
del DEFAULT_ARGS_COPY['initiator_names'] |
| 273 |
DEFAULT_ARGS_COPY['initiator_objects'] = [{'name': 'init1', 'comment': 'comment1'}] |
| 274 |
assert create_and_apply(igroup, DEFAULT_ARGS_COPY, {'use_rest': 'always'})['changed'] |
| 275 |
|
| 276 |
|
| 277 |
def test_successful_modify_initiator_objects_comment_rest(): |
| 278 |
''' Test successful modify ''' |
| 279 |
register_responses([ |
| 280 |
('GET', 'cluster', SRR['is_rest_9_9_1']), |
| 281 |
('GET', 'protocols/san/igroups', SRR['one_igroup_record']), |
| 282 |
('PATCH', 'protocols/san/igroups/a1b2c3/initiators/todelete', SRR['success']), |
| 283 |
('PATCH', 'protocols/san/igroups/a1b2c3', SRR['success']) |
| 284 |
]) |
| 285 |
DEFAULT_ARGS_COPY = DEFAULT_ARGS.copy() |
| 286 |
del DEFAULT_ARGS_COPY['initiator_names'] |
| 287 |
DEFAULT_ARGS_COPY['initiator_objects'] = [{'name': 'todelete', 'comment': 'comment1'}] |
| 288 |
assert create_and_apply(igroup, DEFAULT_ARGS_COPY, {'use_rest': 'always'})['changed'] |
| 289 |
|
| 290 |
|
| 291 |
def test_successful_modify_igroups_rest(): |
| 292 |
''' Test successful modify ''' |
| 293 |
register_responses([ |
| 294 |
('GET', 'cluster', SRR['is_rest_9_9_1']), |
| 295 |
('GET', 'protocols/san/igroups', SRR['one_igroup_record']), |
| 296 |
('DELETE', 'protocols/san/igroups/a1b2c3/initiators/todelete', SRR['success']), |
| 297 |
('POST', 'protocols/san/igroups/a1b2c3/igroups', SRR['success']), |
| 298 |
('PATCH', 'protocols/san/igroups/a1b2c3', SRR['success']) |
| 299 |
]) |
| 300 |
DEFAULT_ARGS_COPY = DEFAULT_ARGS.copy() |
| 301 |
del DEFAULT_ARGS_COPY['initiator_names'] |
| 302 |
args = { |
| 303 |
'igroups': ['test_igroup'], |
| 304 |
'use_rest': 'auto', |
| 305 |
'force_remove_initiator': True |
| 306 |
} |
| 307 |
assert create_and_apply(igroup, DEFAULT_ARGS_COPY, args)['changed'] |
| 308 |
|
| 309 |
|
| 310 |
def test_9_9_0_no_igroups_rest(): |
| 311 |
''' Test failed to use igroups ''' |
| 312 |
register_responses([ |
| 313 |
('GET', 'cluster', SRR['is_rest_9_9_0']) |
| 314 |
]) |
| 315 |
DEFAULT_ARGS_COPY = DEFAULT_ARGS.copy() |
| 316 |
del DEFAULT_ARGS_COPY['initiator_names'] |
| 317 |
args = { |
| 318 |
'igroups': ['test_igroup'], |
| 319 |
'use_rest': 'always' |
| 320 |
} |
| 321 |
msg = 'Error: using igroups requires ONTAP 9.9.1 or later and REST must be enabled - ONTAP version: 9.9.0.' |
| 322 |
assert msg in create_module(igroup, DEFAULT_ARGS_COPY, args, fail=True)['msg'] |
| 323 |
|
| 324 |
|
| 325 |
def test_successful_rename_rest(): |
| 326 |
'''Test successful rename''' |
| 327 |
register_responses([ |
| 328 |
('GET', 'cluster', SRR['is_rest_9_9_1']), |
| 329 |
('GET', 'protocols/san/igroups', SRR['empty_records']), |
| 330 |
('GET', 'protocols/san/igroups', SRR['one_igroup_record']), |
| 331 |
('DELETE', 'protocols/san/igroups/a1b2c3/initiators/todelete', SRR['success']), |
| 332 |
('POST', 'protocols/san/igroups/a1b2c3/initiators', SRR['success']), |
| 333 |
('PATCH', 'protocols/san/igroups/a1b2c3', SRR['success']) |
| 334 |
]) |
| 335 |
args = { |
| 336 |
'use_rest': 'always', |
| 337 |
'from_name': 'test', |
| 338 |
'name': 'test_new' |
| 339 |
} |
| 340 |
assert create_and_apply(igroup, DEFAULT_ARGS, args)['changed'] |
| 341 |
|
| 342 |
|
| 343 |
def test_negative_zapi_or_rest99_option(): |
| 344 |
''' Test ZAPI option not currently supported in REST is rejected ''' |
| 345 |
register_responses([ |
| 346 |
('GET', 'cluster', SRR['is_rest_9_8_0']) |
| 347 |
]) |
| 348 |
args = { |
| 349 |
'use_rest': 'always', |
| 350 |
'bind_portset': 'my_portset' |
| 351 |
} |
| 352 |
create_module(igroup, DEFAULT_ARGS, args) |
| 353 |
msg = "Warning: falling back to ZAPI: using bind_portset requires ONTAP 9.9 or later and REST must be enabled - ONTAP version: 9.8.0." |
| 354 |
print_warnings() |
| 355 |
assert_warning_was_raised(msg) |
| 356 |
|
| 357 |
|
| 358 |
def test_positive_zapi_or_rest99_option(): |
| 359 |
''' Test ZAPI option not currently supported in REST forces ZAPI calls ''' |
| 360 |
register_responses([ |
| 361 |
('GET', 'cluster', SRR['is_rest_9_8_0']) |
| 362 |
]) |
| 363 |
args = { |
| 364 |
'use_rest': 'auto', |
| 365 |
'bind_portset': 'my_portset' |
| 366 |
} |
| 367 |
create_module(igroup, DEFAULT_ARGS, args) |
| 368 |
msg = "Warning: falling back to ZAPI: using bind_portset requires ONTAP 9.9 or later and REST must be enabled - ONTAP version: 9.8.0." |
| 369 |
print_warnings() |
| 370 |
assert_warning_was_raised(msg) |
| 371 |
|
| 372 |
|
| 373 |
def test_create_rest_99(): |
| 374 |
''' Test 9.9 option works with REST ''' |
| 375 |
register_responses([ |
| 376 |
('GET', 'cluster', SRR['is_rest_9_9_0']), |
| 377 |
('GET', 'protocols/san/igroups', SRR['empty_records']), |
| 378 |
('POST', 'protocols/san/igroups', SRR['success']) |
| 379 |
]) |
| 380 |
args = { |
| 381 |
'use_rest': 'auto', |
| 382 |
'bind_portset': 'my_portset' |
| 383 |
} |
| 384 |
assert create_and_apply(igroup, DEFAULT_ARGS, args)['changed'] |
| 385 |
print_warnings |
| 386 |
assert_no_warnings() |
| 387 |
|
| 388 |
|
| 389 |
def test_negative_modify_vserver_rest(): |
| 390 |
''' Test ZAPI option not currently supported in REST is rejected ''' |
| 391 |
register_responses([ |
| 392 |
('GET', 'cluster', SRR['is_rest_9_9_0']), |
| 393 |
('GET', 'protocols/san/igroups', SRR['one_igroup_record']) |
| 394 |
]) |
| 395 |
args = { |
| 396 |
'vserver': 'my_vserver', |
| 397 |
'use_rest': 'always' |
| 398 |
} |
| 399 |
msg = "Error: modifying {'vserver': 'my_vserver'} is not supported in REST" |
| 400 |
assert msg in create_and_apply(igroup, DEFAULT_ARGS, args, fail=True)['msg'] |
| 401 |
|
| 402 |
|
| 403 |
def test_negative_igroups_require_9_9(): |
| 404 |
''' Test ZAPI option not currently supported in REST is rejected ''' |
| 405 |
register_responses([ |
| 406 |
('GET', 'cluster', SRR['is_rest_9_8_0']) |
| 407 |
]) |
| 408 |
DEFAULT_ARGS_COPY = DEFAULT_ARGS.copy() |
| 409 |
del DEFAULT_ARGS_COPY['initiator_names'] |
| 410 |
args = { |
| 411 |
'igroups': 'test_igroup', |
| 412 |
'use_rest': 'always' |
| 413 |
} |
| 414 |
msg = "requires ONTAP 9.9.1 or later and REST must be enabled" |
| 415 |
assert msg in create_module(igroup, DEFAULT_ARGS_COPY, args, fail=True)['msg'] |