| 1 |
# (c) 2018-2019, NetApp, Inc |
| 2 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
|
| 4 |
''' unit tests for ONTAP Ansible module na_ontap_info ''' |
| 5 |
|
| 6 |
from __future__ import (absolute_import, division, print_function) |
| 7 |
__metaclass__ = type |
| 8 |
|
| 9 |
import pytest |
| 10 |
import sys |
| 11 |
|
| 12 |
from ansible_collections.netapp.ontap.tests.unit.compat.mock import patch |
| 13 |
import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils |
| 14 |
# pylint: disable=unused-import |
| 15 |
from ansible_collections.netapp.ontap.tests.unit.framework.mock_rest_and_zapi_requests import patch_request_and_invoke, register_responses |
| 16 |
from ansible_collections.netapp.ontap.tests.unit.framework.zapi_factory import build_zapi_response, zapi_error_message, zapi_responses |
| 17 |
from ansible_collections.netapp.ontap.tests.unit.plugins.module_utils.ansible_mocks import\ |
| 18 |
assert_warning_was_raised, expect_and_capture_ansible_exception, call_main, create_module, patch_ansible, print_warnings |
| 19 |
from ansible_collections.netapp.ontap.plugins.modules.na_ontap_info import NetAppONTAPGatherInfo as my_module, main as my_main |
| 20 |
from ansible_collections.netapp.ontap.plugins.modules.na_ontap_info import convert_keys as info_convert_keys, __finditem as info_finditem |
| 21 |
|
| 22 |
if not netapp_utils.has_netapp_lib(): |
| 23 |
pytestmark = pytest.mark.skip('skipping as missing required netapp_lib') |
| 24 |
|
| 25 |
if sys.version_info < (3, 11): |
| 26 |
pytestmark = pytest.mark.skip("Skipping Unit Tests on 3.11") |
| 27 |
|
| 28 |
|
| 29 |
DEFAULT_ARGS = { |
| 30 |
'hostname': 'hostname', |
| 31 |
'username': 'username', |
| 32 |
'password': 'password', |
| 33 |
'use_rest': 'use_rest', |
| 34 |
} |
| 35 |
|
| 36 |
|
| 37 |
def net_port_info(port_type): |
| 38 |
return { |
| 39 |
'attributes-list': [{ |
| 40 |
'net-port-info': { |
| 41 |
'node': 'node_0', |
| 42 |
'port': 'port_0', |
| 43 |
'broadcast_domain': 'broadcast_domain_0', |
| 44 |
'ipspace': 'ipspace_0', |
| 45 |
'port_type': port_type |
| 46 |
}}, { |
| 47 |
'net-port-info': { |
| 48 |
'node': 'node_1', |
| 49 |
'port': 'port_1', |
| 50 |
'broadcast_domain': 'broadcast_domain_1', |
| 51 |
'ipspace': 'ipspace_1', |
| 52 |
'port_type': port_type |
| 53 |
}} |
| 54 |
] |
| 55 |
} |
| 56 |
|
| 57 |
|
| 58 |
def net_ifgrp_info(id): |
| 59 |
return { |
| 60 |
'attributes': { |
| 61 |
'net-ifgrp-info': { |
| 62 |
'ifgrp-name': 'ifgrp_%d' % id, |
| 63 |
'node': 'node_%d' % id, |
| 64 |
} |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
|
| 69 |
def aggr_efficiency_info(node): |
| 70 |
attributes = { |
| 71 |
'aggregate': 'v2', |
| 72 |
} |
| 73 |
if node: |
| 74 |
attributes['node'] = node |
| 75 |
return { |
| 76 |
'attributes-list': [{ |
| 77 |
'aggr-efficiency-info': attributes |
| 78 |
}] |
| 79 |
} |
| 80 |
|
| 81 |
|
| 82 |
def lun_info(path, next=False): |
| 83 |
info = { |
| 84 |
'attributes-list': [{ |
| 85 |
'lun-info': { |
| 86 |
'serial-number': 'z6CcD+SK5mPb', |
| 87 |
'vserver': 'svm1', |
| 88 |
'path': path} |
| 89 |
}] |
| 90 |
} |
| 91 |
if next: |
| 92 |
info['next-tag'] = 'next_tag' |
| 93 |
return info |
| 94 |
|
| 95 |
|
| 96 |
list_of_one = [{'k1': 'v1'}] |
| 97 |
list_of_two = [{'k1': 'v1'}, {'k2': 'v2'}] |
| 98 |
list_of_two_dups = [{'k1': 'v1'}, {'k1': 'v2'}] |
| 99 |
|
| 100 |
|
| 101 |
ZRR = zapi_responses({ |
| 102 |
'net_port_info': build_zapi_response(net_port_info('whatever'), 2), |
| 103 |
'net_port_info_with_ifgroup': build_zapi_response(net_port_info('if_group'), 2), |
| 104 |
'net_ifgrp_info_0': build_zapi_response(net_ifgrp_info(0), 1), |
| 105 |
'net_ifgrp_info_1': build_zapi_response(net_ifgrp_info(1), 1), |
| 106 |
'list_of_one': build_zapi_response(list_of_one), |
| 107 |
'list_of_two': build_zapi_response(list_of_two), |
| 108 |
'list_of_two_dups': build_zapi_response(list_of_two_dups), |
| 109 |
'aggr_efficiency_info': build_zapi_response(aggr_efficiency_info('v1')), |
| 110 |
'aggr_efficiency_info_no_node': build_zapi_response(aggr_efficiency_info(None)), |
| 111 |
'lun_info': build_zapi_response(lun_info('p1')), |
| 112 |
'lun_info_next_2': build_zapi_response(lun_info('p2', True)), |
| 113 |
'lun_info_next_3': build_zapi_response(lun_info('p3', True)), |
| 114 |
'lun_info_next_4': build_zapi_response(lun_info('p4', True)), |
| 115 |
}) |
| 116 |
|
| 117 |
|
| 118 |
def test_module_fail_when_required_args_missing(): |
| 119 |
''' required arguments are reported as errors ''' |
| 120 |
register_responses([ |
| 121 |
]) |
| 122 |
assert 'missing required arguments: hostname' in call_main(my_main, {}, fail=True)['msg'] |
| 123 |
|
| 124 |
|
| 125 |
def test_ensure_command_called(): |
| 126 |
''' calling get_all will raise a KeyError exception ''' |
| 127 |
register_responses([ |
| 128 |
('ZAPI', 'system-get-ontapi-version', ZRR['version']), |
| 129 |
('ZAPI', 'net-interface-get-iter', ZRR['success']), |
| 130 |
]) |
| 131 |
my_obj = create_module(my_module, DEFAULT_ARGS) |
| 132 |
results = my_obj.get_all(['net_interface_info']) |
| 133 |
assert 'net_interface_info' in results |
| 134 |
|
| 135 |
|
| 136 |
def test_get_generic_get_iter(): |
| 137 |
'''calling get_generic_get_iter will return expected dict''' |
| 138 |
register_responses([ |
| 139 |
('ZAPI', 'net-port-get-iter', ZRR['net_port_info']), |
| 140 |
]) |
| 141 |
obj = create_module(my_module, DEFAULT_ARGS) |
| 142 |
result = obj.get_generic_get_iter( |
| 143 |
'net-port-get-iter', |
| 144 |
attribute='net-port-info', |
| 145 |
key_fields=('node', 'port'), |
| 146 |
query={'max-records': '1024'} |
| 147 |
) |
| 148 |
assert result.get('node_0:port_0') |
| 149 |
assert result.get('node_1:port_1') |
| 150 |
|
| 151 |
|
| 152 |
@patch('ansible_collections.netapp.ontap.plugins.modules.na_ontap_info.NetAppONTAPGatherInfo.get_all') |
| 153 |
def test_main(get_all): |
| 154 |
'''test main method - default: no state.''' |
| 155 |
register_responses([ |
| 156 |
]) |
| 157 |
get_all.side_effect = [ |
| 158 |
{'test_get_all': {'vserver_login_banner_info': 'test_vserver_login_banner_info', 'vserver_info': 'test_vserver_info'}} |
| 159 |
] |
| 160 |
results = call_main(my_main, DEFAULT_ARGS) |
| 161 |
assert 'ontap_info' in results |
| 162 |
assert 'test_get_all' in results['ontap_info'] |
| 163 |
|
| 164 |
|
| 165 |
@patch('ansible_collections.netapp.ontap.plugins.modules.na_ontap_info.NetAppONTAPGatherInfo.get_all') |
| 166 |
def test_main_with_state(get_all): |
| 167 |
'''test main method with explicit state.''' |
| 168 |
register_responses([ |
| 169 |
]) |
| 170 |
module_args = {'state': 'some_state'} |
| 171 |
get_all.side_effect = [ |
| 172 |
{'test_get_all': {'vserver_login_banner_info': 'test_vserver_login_banner_info', 'vserver_info': 'test_vserver_info'}} |
| 173 |
] |
| 174 |
results = call_main(my_main, DEFAULT_ARGS, module_args) |
| 175 |
assert 'ontap_info' in results |
| 176 |
assert 'test_get_all' in results['ontap_info'] |
| 177 |
print_warnings() |
| 178 |
assert_warning_was_raised("option 'state' is deprecated.") |
| 179 |
|
| 180 |
|
| 181 |
def test_get_ifgrp_info_no_ifgrp(): |
| 182 |
'''test get_ifgrp_info with empty ifgrp_info''' |
| 183 |
register_responses([ |
| 184 |
('ZAPI', 'net-port-get-iter', ZRR['net_port_info']), |
| 185 |
]) |
| 186 |
obj = create_module(my_module, DEFAULT_ARGS) |
| 187 |
result = obj.get_ifgrp_info() |
| 188 |
assert result == {} |
| 189 |
|
| 190 |
|
| 191 |
def test_get_ifgrp_info_with_ifgrp(): |
| 192 |
'''test get_ifgrp_info with empty ifgrp_info''' |
| 193 |
register_responses([ |
| 194 |
('ZAPI', 'net-port-get-iter', ZRR['net_port_info_with_ifgroup']), |
| 195 |
('ZAPI', 'net-port-ifgrp-get', ZRR['net_ifgrp_info_0']), |
| 196 |
('ZAPI', 'net-port-ifgrp-get', ZRR['net_ifgrp_info_1']), |
| 197 |
]) |
| 198 |
obj = create_module(my_module, DEFAULT_ARGS) |
| 199 |
results = obj.get_ifgrp_info() |
| 200 |
assert results.get('node_0:ifgrp_0') |
| 201 |
assert results.get('node_1:ifgrp_1') |
| 202 |
|
| 203 |
|
| 204 |
def test_ontapi_error(): |
| 205 |
'''test ontapi will raise zapi error''' |
| 206 |
register_responses([ |
| 207 |
('ZAPI', 'system-get-ontapi-version', ZRR['error']), |
| 208 |
]) |
| 209 |
obj = create_module(my_module, DEFAULT_ARGS) |
| 210 |
error = zapi_error_message('Error calling API system-get-ontapi-version') |
| 211 |
assert error in expect_and_capture_ansible_exception(obj.ontapi, 'fail')['msg'] |
| 212 |
|
| 213 |
|
| 214 |
def test_call_api_error(): |
| 215 |
'''test call_api will raise zapi error''' |
| 216 |
register_responses([ |
| 217 |
('ZAPI', 'security-key-manager-key-get-iter', ZRR['error']), |
| 218 |
('ZAPI', 'lun-get-iter', ZRR['error_missing_api']), |
| 219 |
('ZAPI', 'nvme-get-iter', ZRR['error']), |
| 220 |
]) |
| 221 |
obj = create_module(my_module, DEFAULT_ARGS) |
| 222 |
# 1 error is ignored |
| 223 |
assert obj.call_api('security-key-manager-key-get-iter') == (None, None) |
| 224 |
# 2 missing API (cluster admin API not visible at vserver level) |
| 225 |
error = zapi_error_message('Error invalid API. Most likely running a cluster level API as vserver', 13005) |
| 226 |
assert error in expect_and_capture_ansible_exception(obj.call_api, 'fail', 'lun-get-iter')['msg'] |
| 227 |
# 3 API error |
| 228 |
error = zapi_error_message('Error calling API nvme-get-iter') |
| 229 |
assert error in expect_and_capture_ansible_exception(obj.call_api, 'fail', 'nvme-get-iter')['msg'] |
| 230 |
|
| 231 |
|
| 232 |
def test_get_generic_get_iter_key_error(): |
| 233 |
register_responses([ |
| 234 |
('ZAPI', 'lun-get-iter', ZRR['lun_info']), |
| 235 |
('ZAPI', 'lun-get-iter', ZRR['lun_info']), |
| 236 |
('ZAPI', 'lun-get-iter', ZRR['lun_info']), |
| 237 |
('ZAPI', 'lun-get-iter', ZRR['lun_info']), |
| 238 |
]) |
| 239 |
obj = create_module(my_module, DEFAULT_ARGS) |
| 240 |
keys = 'single_key' |
| 241 |
error = "Error: key 'single_key' not found for lun-get-iter, got:" |
| 242 |
assert error in expect_and_capture_ansible_exception(obj.get_generic_get_iter, 'fail', 'lun-get-iter', None, keys)['msg'] |
| 243 |
keys = ('key1', 'path') |
| 244 |
error = "Error: key 'key1' not found for lun-get-iter, got:" |
| 245 |
assert error in expect_and_capture_ansible_exception(obj.get_generic_get_iter, 'fail', 'lun-get-iter', None, keys)['msg'] |
| 246 |
# ignoring key_errors |
| 247 |
module_args = {'continue_on_error': 'key_error'} |
| 248 |
obj = create_module(my_module, DEFAULT_ARGS, module_args) |
| 249 |
keys = 'single_key' |
| 250 |
missing_key = 'Error_1_key_not_found_%s' % keys |
| 251 |
results = obj.get_generic_get_iter('lun-get-iter', None, keys) |
| 252 |
assert missing_key in results |
| 253 |
keys = ('key1', 'path') |
| 254 |
missing_key = 'Error_1_key_not_found_%s' % keys[0] |
| 255 |
results = obj.get_generic_get_iter('lun-get-iter', None, keys) |
| 256 |
assert missing_key in results |
| 257 |
|
| 258 |
|
| 259 |
def test_find_item(): |
| 260 |
'''test __find_item return expected key value''' |
| 261 |
register_responses([ |
| 262 |
]) |
| 263 |
obj = {"A": 1, "B": {"C": {"D": 2}}} |
| 264 |
key = "D" |
| 265 |
result = info_finditem(obj, key) |
| 266 |
assert result == 2 |
| 267 |
obj = {"A": 1, "B": {"C": {"D": None}}} |
| 268 |
result = info_finditem(obj, key) |
| 269 |
assert result == "None" |
| 270 |
|
| 271 |
|
| 272 |
def test_subset_return_all_complete(): |
| 273 |
''' Check all returns all of the entries if version is high enough ''' |
| 274 |
register_responses([ |
| 275 |
]) |
| 276 |
version = '170' # change this if new ZAPIs are supported |
| 277 |
obj = create_module(my_module, DEFAULT_ARGS) |
| 278 |
subset = obj.get_subset(['all'], version) |
| 279 |
assert set(obj.info_subsets.keys()) == subset |
| 280 |
|
| 281 |
|
| 282 |
def test_subset_return_all_partial(): |
| 283 |
''' Check all returns a subset of the entries if version is low enough ''' |
| 284 |
register_responses([ |
| 285 |
]) |
| 286 |
version = '120' # low enough so that some ZAPIs are not supported |
| 287 |
obj = create_module(my_module, DEFAULT_ARGS) |
| 288 |
subset = obj.get_subset(['all'], version) |
| 289 |
all_keys = obj.info_subsets.keys() |
| 290 |
assert set(all_keys) > subset |
| 291 |
supported_keys = filter(lambda key: obj.info_subsets[key]['min_version'] <= version, all_keys) |
| 292 |
assert set(supported_keys) == subset |
| 293 |
|
| 294 |
|
| 295 |
def test_subset_return_one(): |
| 296 |
''' Check single entry returns one ''' |
| 297 |
register_responses([ |
| 298 |
]) |
| 299 |
version = '120' # low enough so that some ZAPIs are not supported |
| 300 |
obj = create_module(my_module, DEFAULT_ARGS) |
| 301 |
subset = obj.get_subset(['net_interface_info'], version) |
| 302 |
assert len(subset) == 1 |
| 303 |
|
| 304 |
|
| 305 |
def test_subset_return_multiple(): |
| 306 |
''' Check that more than one entry returns the same number ''' |
| 307 |
register_responses([ |
| 308 |
]) |
| 309 |
version = '120' # low enough so that some ZAPIs are not supported |
| 310 |
obj = create_module(my_module, DEFAULT_ARGS) |
| 311 |
subset_entries = ['net_interface_info', 'net_port_info'] |
| 312 |
subset = obj.get_subset(subset_entries, version) |
| 313 |
assert len(subset) == len(subset_entries) |
| 314 |
|
| 315 |
|
| 316 |
def test_subset_return_bad(): |
| 317 |
''' Check that a bad subset entry will error out ''' |
| 318 |
register_responses([ |
| 319 |
]) |
| 320 |
version = '120' # low enough so that some ZAPIs are not supported |
| 321 |
obj = create_module(my_module, DEFAULT_ARGS) |
| 322 |
error = 'Bad subset: my_invalid_subset' |
| 323 |
assert error in expect_and_capture_ansible_exception(obj.get_subset, 'fail', ['net_interface_info', 'my_invalid_subset'], version)['msg'] |
| 324 |
|
| 325 |
|
| 326 |
def test_subset_return_unsupported(): |
| 327 |
''' Check that a new subset entry will error out on an older system ''' |
| 328 |
register_responses([ |
| 329 |
]) |
| 330 |
version = '120' # low enough so that some ZAPIs are not supported |
| 331 |
key = 'nvme_info' # only supported starting at 140 |
| 332 |
obj = create_module(my_module, DEFAULT_ARGS) |
| 333 |
error = 'Remote system at version %s does not support %s' % (version, key) |
| 334 |
assert error in expect_and_capture_ansible_exception(obj.get_subset, 'fail', ['net_interface_info', key], version)['msg'] |
| 335 |
|
| 336 |
|
| 337 |
def test_subset_return_none(): |
| 338 |
''' Check usable subset can be empty ''' |
| 339 |
register_responses([ |
| 340 |
]) |
| 341 |
version = '!' # lower then 0, so that no ZAPI is supported |
| 342 |
obj = create_module(my_module, DEFAULT_ARGS) |
| 343 |
subset = obj.get_subset(['all'], version) |
| 344 |
assert len(subset) == 0 |
| 345 |
|
| 346 |
|
| 347 |
def test_subset_return_all_expect_one(): |
| 348 |
''' Check !x returns all of the entries except x if version is high enough ''' |
| 349 |
register_responses([ |
| 350 |
]) |
| 351 |
version = '170' # change this if new ZAPIs are supported |
| 352 |
obj = create_module(my_module, DEFAULT_ARGS) |
| 353 |
subset = obj.get_subset(['!net_interface_info'], version) |
| 354 |
assert len(obj.info_subsets.keys()) == len(subset) + 1 |
| 355 |
subset.add('net_interface_info') |
| 356 |
assert set(obj.info_subsets.keys()) == subset |
| 357 |
|
| 358 |
|
| 359 |
def test_subset_return_all_expect_three(): |
| 360 |
''' Check !x,!y,!z returns all of the entries except x, y, z if version is high enough ''' |
| 361 |
register_responses([ |
| 362 |
]) |
| 363 |
version = '170' # change this if new ZAPIs are supported |
| 364 |
obj = create_module(my_module, DEFAULT_ARGS) |
| 365 |
subset = obj.get_subset(['!net_interface_info', '!nvme_info', '!ontap_version'], version) |
| 366 |
assert len(obj.info_subsets.keys()) == len(subset) + 3 |
| 367 |
subset.update(['net_interface_info', 'nvme_info', 'ontap_version']) |
| 368 |
assert set(obj.info_subsets.keys()) == subset |
| 369 |
|
| 370 |
|
| 371 |
def test_subset_return_none_with_exclusion(): |
| 372 |
''' Check usable subset can be empty with !x ''' |
| 373 |
register_responses([ |
| 374 |
]) |
| 375 |
version = '!' # lower then 0, so that no ZAPI is supported |
| 376 |
key = 'net_interface_info' |
| 377 |
obj = create_module(my_module, DEFAULT_ARGS) |
| 378 |
error = 'Remote system at version %s does not support %s' % (version, key) |
| 379 |
assert error in expect_and_capture_ansible_exception(obj.get_subset, 'fail', ['!' + key], version)['msg'] |
| 380 |
|
| 381 |
|
| 382 |
def test_get_generic_get_iter_flatten_list_of_one(): |
| 383 |
'''calling get_generic_get_iter will return expected dict''' |
| 384 |
register_responses([ |
| 385 |
('ZAPI', 'list_of_one', ZRR['list_of_one']), |
| 386 |
]) |
| 387 |
obj = create_module(my_module, DEFAULT_ARGS) |
| 388 |
result = obj.get_generic_get_iter( |
| 389 |
'list_of_one', |
| 390 |
attributes_list_tag=None, |
| 391 |
) |
| 392 |
print(ZRR['list_of_one'][0].to_string()) |
| 393 |
print(result) |
| 394 |
assert isinstance(result, dict) |
| 395 |
assert result.get('k1') == 'v1' |
| 396 |
|
| 397 |
|
| 398 |
def test_get_generic_get_iter_flatten_list_of_two(): |
| 399 |
'''calling get_generic_get_iter will return expected dict''' |
| 400 |
register_responses([ |
| 401 |
('ZAPI', 'list_of_two', ZRR['list_of_two']), |
| 402 |
]) |
| 403 |
obj = create_module(my_module, DEFAULT_ARGS) |
| 404 |
result = obj.get_generic_get_iter( |
| 405 |
'list_of_two', |
| 406 |
attributes_list_tag=None, |
| 407 |
) |
| 408 |
print(result) |
| 409 |
assert isinstance(result, dict) |
| 410 |
assert result.get('k1') == 'v1' |
| 411 |
assert result.get('k2') == 'v2' |
| 412 |
|
| 413 |
|
| 414 |
def test_get_generic_get_iter_flatten_list_of_two_dups(): |
| 415 |
'''calling get_generic_get_iter will return expected dict''' |
| 416 |
register_responses([ |
| 417 |
('ZAPI', 'list_of_two_dups', ZRR['list_of_two_dups']), |
| 418 |
]) |
| 419 |
obj = create_module(my_module, DEFAULT_ARGS) |
| 420 |
result = obj.get_generic_get_iter( |
| 421 |
'list_of_two_dups', |
| 422 |
attributes_list_tag=None, |
| 423 |
) |
| 424 |
assert isinstance(result, list) |
| 425 |
assert result[0].get('k1') == 'v1' |
| 426 |
assert result[1].get('k1') == 'v2' |
| 427 |
|
| 428 |
|
| 429 |
def test_check_underscore(): |
| 430 |
''' Check warning is recorded if '_' is found in key ''' |
| 431 |
register_responses([ |
| 432 |
]) |
| 433 |
test_dict = dict( |
| 434 |
bad_key='something' |
| 435 |
) |
| 436 |
test_dict['good-key'] = [dict( |
| 437 |
other_bad_key=dict( |
| 438 |
yet_another_bad_key=1 |
| 439 |
), |
| 440 |
somekey=dict( |
| 441 |
more_bad_key=2 |
| 442 |
) |
| 443 |
)] |
| 444 |
obj = create_module(my_module, DEFAULT_ARGS) |
| 445 |
obj.check_for___in_keys(test_dict) |
| 446 |
print('Info: %s' % repr(obj.warnings)) |
| 447 |
for key in ['bad_key', 'other_bad_key', 'yet_another_bad_key', 'more_bad_key']: |
| 448 |
msg = "Underscore in ZAPI tag: %s, do you mean '-'?" % key |
| 449 |
assert msg in obj.warnings |
| 450 |
obj.warnings.remove(msg) |
| 451 |
# make sure there is no extra warnings (eg we found and removed all of them) |
| 452 |
assert obj.warnings == list() |
| 453 |
|
| 454 |
|
| 455 |
def d2us(astr): |
| 456 |
return str.replace(astr, '-', '_') |
| 457 |
|
| 458 |
|
| 459 |
def test_convert_keys_string(): |
| 460 |
''' no conversion ''' |
| 461 |
register_responses([ |
| 462 |
]) |
| 463 |
key = 'a-b-c' |
| 464 |
assert info_convert_keys(key) == key |
| 465 |
|
| 466 |
|
| 467 |
def test_convert_keys_tuple(): |
| 468 |
''' no conversion ''' |
| 469 |
register_responses([ |
| 470 |
]) |
| 471 |
key = 'a-b-c' |
| 472 |
anobject = (key, key) |
| 473 |
assert info_convert_keys(anobject) == anobject |
| 474 |
|
| 475 |
|
| 476 |
def test_convert_keys_list(): |
| 477 |
''' no conversion ''' |
| 478 |
register_responses([ |
| 479 |
]) |
| 480 |
key = 'a-b-c' |
| 481 |
anobject = [key, key] |
| 482 |
assert info_convert_keys(anobject) == anobject |
| 483 |
|
| 484 |
|
| 485 |
def test_convert_keys_simple_dict(): |
| 486 |
''' conversion of keys ''' |
| 487 |
register_responses([ |
| 488 |
]) |
| 489 |
key = 'a-b-c' |
| 490 |
anobject = {key: 1} |
| 491 |
assert list(info_convert_keys(anobject).keys())[0] == d2us(key) |
| 492 |
|
| 493 |
|
| 494 |
def test_convert_keys_list_of_dict(): |
| 495 |
''' conversion of keys ''' |
| 496 |
register_responses([ |
| 497 |
]) |
| 498 |
key = 'a-b-c' |
| 499 |
anobject = [{key: 1}, {key: 2}] |
| 500 |
converted = info_convert_keys(anobject) |
| 501 |
for adict in converted: |
| 502 |
for akey in adict: |
| 503 |
assert akey == d2us(key) |
| 504 |
|
| 505 |
|
| 506 |
def test_set_error_flags_error_n(): |
| 507 |
''' Check set_error__flags return correct dict ''' |
| 508 |
register_responses([ |
| 509 |
]) |
| 510 |
module_args = {'continue_on_error': ['never', 'whatever']} |
| 511 |
msg = "never needs to be the only keyword in 'continue_on_error' option." |
| 512 |
assert call_main(my_main, DEFAULT_ARGS, module_args, fail=True)['msg'] == msg |
| 513 |
|
| 514 |
|
| 515 |
def test_set_error_flags_error_a(): |
| 516 |
''' Check set_error__flags return correct dict ''' |
| 517 |
register_responses([ |
| 518 |
]) |
| 519 |
module_args = {'continue_on_error': ['whatever', 'always']} |
| 520 |
print('Info: %s' % call_main(my_main, DEFAULT_ARGS, module_args, fail=True)['msg']) |
| 521 |
msg = "always needs to be the only keyword in 'continue_on_error' option." |
| 522 |
assert call_main(my_main, DEFAULT_ARGS, module_args, fail=True)['msg'] == msg |
| 523 |
|
| 524 |
|
| 525 |
def test_set_error_flags_error_u(): |
| 526 |
''' Check set_error__flags return correct dict ''' |
| 527 |
register_responses([ |
| 528 |
]) |
| 529 |
module_args = {'continue_on_error': ['whatever', 'else']} |
| 530 |
|
| 531 |
print('Info: %s' % call_main(my_main, DEFAULT_ARGS, module_args, fail=True)['msg']) |
| 532 |
msg = "whatever is not a valid keyword in 'continue_on_error' option." |
| 533 |
assert call_main(my_main, DEFAULT_ARGS, module_args, fail=True)['msg'] == msg |
| 534 |
|
| 535 |
|
| 536 |
def test_set_error_flags_1_flag(): |
| 537 |
''' Check set_error__flags return correct dict ''' |
| 538 |
register_responses([ |
| 539 |
]) |
| 540 |
module_args = {'continue_on_error': ['missing_vserver_api_error']} |
| 541 |
obj = create_module(my_module, DEFAULT_ARGS, module_args, 'vserver') |
| 542 |
assert not obj.error_flags['missing_vserver_api_error'] |
| 543 |
assert obj.error_flags['rpc_error'] |
| 544 |
assert obj.error_flags['other_error'] |
| 545 |
|
| 546 |
|
| 547 |
def test_set_error_flags_2_flags(): |
| 548 |
''' Check set_error__flags return correct dict ''' |
| 549 |
register_responses([ |
| 550 |
]) |
| 551 |
module_args = {'continue_on_error': ['missing_vserver_api_error', 'rpc_error']} |
| 552 |
obj = create_module(my_module, DEFAULT_ARGS, module_args, 'vserver') |
| 553 |
assert not obj.error_flags['missing_vserver_api_error'] |
| 554 |
assert not obj.error_flags['rpc_error'] |
| 555 |
assert obj.error_flags['other_error'] |
| 556 |
|
| 557 |
|
| 558 |
def test_set_error_flags_3_flags(): |
| 559 |
''' Check set_error__flags return correct dict ''' |
| 560 |
register_responses([ |
| 561 |
]) |
| 562 |
module_args = {'continue_on_error': ['missing_vserver_api_error', 'rpc_error', 'other_error']} |
| 563 |
obj = create_module(my_module, DEFAULT_ARGS, module_args, 'vserver') |
| 564 |
assert not obj.error_flags['missing_vserver_api_error'] |
| 565 |
assert not obj.error_flags['rpc_error'] |
| 566 |
assert not obj.error_flags['other_error'] |
| 567 |
|
| 568 |
|
| 569 |
def test_get_subset_missing_key(): |
| 570 |
'''calling aggr_efficiency_info with missing key''' |
| 571 |
register_responses([ |
| 572 |
('ZAPI', 'aggr-efficiency-get-iter', ZRR['aggr_efficiency_info']), |
| 573 |
('ZAPI', 'aggr-efficiency-get-iter', ZRR['aggr_efficiency_info_no_node']), |
| 574 |
]) |
| 575 |
obj = create_module(my_module, DEFAULT_ARGS) |
| 576 |
call = obj.info_subsets['aggr_efficiency_info'] |
| 577 |
info = call['method'](**call['kwargs']) |
| 578 |
print(info) |
| 579 |
assert 'v1:v2' in info |
| 580 |
call = obj.info_subsets['aggr_efficiency_info'] |
| 581 |
info = call['method'](**call['kwargs']) |
| 582 |
print(info) |
| 583 |
assert 'key_not_present:v2' in info |
| 584 |
|
| 585 |
|
| 586 |
def test_get_lun_with_serial(): |
| 587 |
'''calling lun_info with serial-number key''' |
| 588 |
register_responses([ |
| 589 |
('ZAPI', 'system-get-ontapi-version', ZRR['success']), |
| 590 |
('ZAPI', 'lun-get-iter', ZRR['lun_info']), |
| 591 |
# no records |
| 592 |
('ZAPI', 'system-get-ontapi-version', ZRR['success']), |
| 593 |
('ZAPI', 'lun-get-iter', ZRR['no_records']), |
| 594 |
]) |
| 595 |
obj = create_module(my_module, DEFAULT_ARGS) |
| 596 |
info = obj.get_all(['lun_info']) |
| 597 |
print(info) |
| 598 |
assert 'lun_info' in info |
| 599 |
lun_info = info['lun_info']['svm1:p1'] |
| 600 |
assert lun_info['serial_number'] == 'z6CcD+SK5mPb' |
| 601 |
assert lun_info['serial_hex'] == '7a364363442b534b356d5062' |
| 602 |
assert lun_info['naa_id'] == 'naa.600a0980' + '7a364363442b534b356d5062' |
| 603 |
# no records |
| 604 |
info = obj.get_all(['lun_info']) |
| 605 |
assert 'lun_info' in info |
| 606 |
assert info['lun_info'] is None |
| 607 |
# error |
| 608 |
|
| 609 |
|
| 610 |
def test_get_nothing(): |
| 611 |
'''calling with !all''' |
| 612 |
register_responses([ |
| 613 |
('ZAPI', 'system-get-ontapi-version', ZRR['success']), |
| 614 |
]) |
| 615 |
obj = create_module(my_module, DEFAULT_ARGS) |
| 616 |
info = obj.get_all(['!all']) |
| 617 |
print(info) |
| 618 |
assert info == {'ontap_version': '0', 'ontapi_version': '0'} |
| 619 |
|
| 620 |
|
| 621 |
def test_deprecation_ontap_version(): |
| 622 |
'''calling ontap_version''' |
| 623 |
register_responses([ |
| 624 |
('ZAPI', 'system-get-ontapi-version', ZRR['success']), |
| 625 |
('ZAPI', 'system-get-ontapi-version', ZRR['success']), |
| 626 |
]) |
| 627 |
obj = create_module(my_module, DEFAULT_ARGS) |
| 628 |
info = obj.get_all(['ontap_version']) |
| 629 |
assert info |
| 630 |
assert 'deprecation_warning' in info |
| 631 |
assert info['deprecation_warning'] == 'ontap_version is deprecated, please use ontapi_version' |
| 632 |
|
| 633 |
|
| 634 |
def test_help(): |
| 635 |
'''calling help''' |
| 636 |
register_responses([ |
| 637 |
('ZAPI', 'system-get-ontapi-version', ZRR['success']), |
| 638 |
]) |
| 639 |
obj = create_module(my_module, DEFAULT_ARGS) |
| 640 |
info = obj.get_all(['help']) |
| 641 |
assert info |
| 642 |
assert 'help' in info |
| 643 |
|
| 644 |
|
| 645 |
def test_desired_attributes(): |
| 646 |
'''desired_attributes option''' |
| 647 |
register_responses([ |
| 648 |
('ZAPI', 'system-get-ontapi-version', ZRR['success']), |
| 649 |
('ZAPI', 'lun-get-iter', ZRR['success']), |
| 650 |
('ZAPI', 'system-get-ontapi-version', ZRR['success']), |
| 651 |
]) |
| 652 |
module_args = {'desired_attributes': {'attr': 'value'}} |
| 653 |
obj = create_module(my_module, DEFAULT_ARGS, module_args) |
| 654 |
info = obj.get_all(['lun_info']) |
| 655 |
assert 'lun_info' in info |
| 656 |
assert info['lun_info'] is None |
| 657 |
error = 'desired_attributes option is only supported with a single subset' |
| 658 |
assert error in expect_and_capture_ansible_exception(obj.get_all, 'fail', ['ontapi_version', 'ontap_system_version'])['msg'] |
| 659 |
|
| 660 |
|
| 661 |
def test_query(): |
| 662 |
'''query option''' |
| 663 |
register_responses([ |
| 664 |
('ZAPI', 'system-get-ontapi-version', ZRR['success']), |
| 665 |
('ZAPI', 'system-get-ontapi-version', ZRR['success']), |
| 666 |
('ZAPI', 'system-get-ontapi-version', ZRR['success']), |
| 667 |
]) |
| 668 |
module_args = {'query': {'attr': 'value', 'a_b': 'val'}} |
| 669 |
obj = create_module(my_module, DEFAULT_ARGS, module_args) |
| 670 |
info = obj.get_all(['ontapi_version']) |
| 671 |
assert info == {'ontap_version': '0', 'ontapi_version': '0', 'module_warnings': ["Underscore in ZAPI tag: a_b, do you mean '-'?"]} |
| 672 |
error = 'query option is only supported with a single subset' |
| 673 |
assert error in expect_and_capture_ansible_exception(obj.get_all, 'fail', ['ontapi_version', 'ontap_system_version'])['msg'] |
| 674 |
|
| 675 |
|
| 676 |
@patch('ansible_collections.netapp.ontap.plugins.modules.na_ontap_info.NetAppONTAPGatherInfo.get_all') |
| 677 |
def test_get_all_with_summary(mock_get_all): |
| 678 |
'''all and summary''' |
| 679 |
register_responses([ |
| 680 |
]) |
| 681 |
module_args = {'summary': True, 'gather_subset': []} |
| 682 |
mock_get_all.return_value = {'a_info': {'1': '1.1'}, 'b_info': {'2': '2.2'}} |
| 683 |
info = call_main(my_main, DEFAULT_ARGS, module_args) |
| 684 |
assert info |
| 685 |
assert 'ontap_info' in info |
| 686 |
assert info['ontap_info'] == {'a_info': {'1': None}.keys(), 'b_info': {'2': None}.keys()} |
| 687 |
|
| 688 |
|
| 689 |
def test_repeated_get(): |
| 690 |
'''query option''' |
| 691 |
register_responses([ |
| 692 |
('ZAPI', 'system-get-ontapi-version', ZRR['success']), |
| 693 |
('ZAPI', 'lun-get-iter', ZRR['lun_info_next_2']), |
| 694 |
('ZAPI', 'lun-get-iter', ZRR['lun_info_next_3']), |
| 695 |
('ZAPI', 'lun-get-iter', ZRR['lun_info_next_4']), |
| 696 |
('ZAPI', 'lun-get-iter', ZRR['lun_info']), |
| 697 |
]) |
| 698 |
module_args = {'query': {}} |
| 699 |
obj = create_module(my_module, DEFAULT_ARGS, module_args) |
| 700 |
info = obj.get_all(['lun_info']) |
| 701 |
assert info |
| 702 |
assert 'lun_info' in info |
| 703 |
assert len(info['lun_info']) == 4 |
| 704 |
|
| 705 |
|
| 706 |
def test_repeated_get_error(): |
| 707 |
'''query option''' |
| 708 |
register_responses([ |
| 709 |
('ZAPI', 'lun-get-iter', ZRR['lun_info_next_2']), |
| 710 |
('ZAPI', 'lun-get-iter', ZRR['lun_info']), |
| 711 |
]) |
| 712 |
module_args = {'query': {}} |
| 713 |
obj = create_module(my_module, DEFAULT_ARGS, module_args) |
| 714 |
error = "'next-tag' is not expected for this API" |
| 715 |
assert error in expect_and_capture_ansible_exception(obj.call_api, 'fail', 'lun-get-iter', attributes_list_tag=None)['msg'] |
| 716 |
|
| 717 |
|
| 718 |
def test_attribute_error(): |
| 719 |
register_responses([ |
| 720 |
('ZAPI', 'system-get-ontapi-version', ZRR['success']), |
| 721 |
('ZAPI', 'license-v2-list-info', ZRR['no_records']), |
| 722 |
]) |
| 723 |
module_args = {'gather_subset': ['license_info'], 'vserver': 'svm'} |
| 724 |
error = "Error: attribute 'licenses' not found for license-v2-list-info, got:" |
| 725 |
assert error in call_main(my_main, DEFAULT_ARGS, module_args, fail=True)['msg'] |
| 726 |
|
| 727 |
|
| 728 |
def test_continue_on_error(): |
| 729 |
register_responses([ |
| 730 |
('ZAPI', 'system-get-ontapi-version', ZRR['success']), |
| 731 |
('ZAPI', 'license-v2-list-info', ZRR['error']), |
| 732 |
('ZAPI', 'system-get-ontapi-version', ZRR['success']), |
| 733 |
('ZAPI', 'license-v2-list-info', ZRR['error']), |
| 734 |
]) |
| 735 |
module_args = {'gather_subset': ['license_info'], 'vserver': 'svm'} |
| 736 |
error = zapi_error_message('Error calling API license-v2-list-info') |
| 737 |
assert error in call_main(my_main, DEFAULT_ARGS, module_args, fail=True)['msg'] |
| 738 |
module_args = {'gather_subset': ['license_info'], 'vserver': 'svm', 'continue_on_error': 'always'} |
| 739 |
info = call_main(my_main, DEFAULT_ARGS, module_args) |
| 740 |
error = {'error': zapi_error_message('Error calling API license-v2-list-info')} |
| 741 |
assert info is not None |
| 742 |
assert info['ontap_info']['license_info'] == error |