| 1 |
# Copyright (c) 2018-2022 NetApp |
| 2 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
|
| 4 |
""" unit tests for module_utils netapp_module.py """ |
| 5 |
from __future__ import (absolute_import, division, print_function) |
| 6 |
import copy |
| 7 |
__metaclass__ = type |
| 8 |
|
| 9 |
import pytest |
| 10 |
import sys |
| 11 |
|
| 12 |
from ansible.module_utils import basic |
| 13 |
from ansible_collections.netapp.ontap.tests.unit.compat.mock import patch |
| 14 |
import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils |
| 15 |
from ansible_collections.netapp.ontap.plugins.module_utils.netapp_module import NetAppModule as na_helper, cmp as na_cmp |
| 16 |
# pylint: disable=unused-import |
| 17 |
from ansible_collections.netapp.ontap.tests.unit.plugins.module_utils.ansible_mocks import\ |
| 18 |
assert_no_warnings, assert_warning_was_raised, clear_warnings, patch_ansible, create_module, expect_and_capture_ansible_exception |
| 19 |
from ansible_collections.netapp.ontap.tests.unit.framework.zapi_factory import build_zapi_response |
| 20 |
from ansible_collections.netapp.ontap.tests.unit.framework import ut_utilities |
| 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 |
class MockONTAPModule(object): |
| 30 |
def __init__(self): |
| 31 |
self.module = basic.AnsibleModule(netapp_utils.na_ontap_host_argument_spec()) |
| 32 |
self.na_helper = na_helper(self.module) |
| 33 |
self.na_helper.set_parameters(self.module.params) |
| 34 |
|
| 35 |
|
| 36 |
class MockONTAPModuleV2(object): |
| 37 |
def __init__(self): |
| 38 |
self.module = basic.AnsibleModule(netapp_utils.na_ontap_host_argument_spec()) |
| 39 |
self.na_helper = na_helper(self) |
| 40 |
self.na_helper.set_parameters(self.module.params) |
| 41 |
|
| 42 |
|
| 43 |
class AnsibleFailJson(Exception): |
| 44 |
"""Exception class to be raised by module.fail_json and caught by the test case""" |
| 45 |
|
| 46 |
|
| 47 |
def create_ontap_module(args=None, version=1): |
| 48 |
if version == 2: |
| 49 |
return create_module(MockONTAPModuleV2, args) |
| 50 |
return create_module(MockONTAPModule, args) |
| 51 |
|
| 52 |
|
| 53 |
def test_get_cd_action_create(): |
| 54 |
""" validate cd_action for create """ |
| 55 |
current = None |
| 56 |
desired = {'state': 'present'} |
| 57 |
my_obj = na_helper() |
| 58 |
result = my_obj.get_cd_action(current, desired) |
| 59 |
assert result == 'create' |
| 60 |
|
| 61 |
|
| 62 |
def test_get_cd_action_delete(): |
| 63 |
""" validate cd_action for delete """ |
| 64 |
current = {'state': 'absent'} |
| 65 |
desired = {'state': 'absent'} |
| 66 |
my_obj = na_helper() |
| 67 |
result = my_obj.get_cd_action(current, desired) |
| 68 |
assert result == 'delete' |
| 69 |
|
| 70 |
|
| 71 |
def test_get_cd_action_already_exist(): |
| 72 |
""" validate cd_action for returning None """ |
| 73 |
current = {'state': 'whatever'} |
| 74 |
desired = {'state': 'present'} |
| 75 |
my_obj = na_helper() |
| 76 |
result = my_obj.get_cd_action(current, desired) |
| 77 |
assert result is None |
| 78 |
|
| 79 |
|
| 80 |
def test_get_cd_action_already_absent(): |
| 81 |
""" validate cd_action for returning None """ |
| 82 |
current = None |
| 83 |
desired = {'state': 'absent'} |
| 84 |
my_obj = na_helper() |
| 85 |
result = my_obj.get_cd_action(current, desired) |
| 86 |
assert result is None |
| 87 |
|
| 88 |
|
| 89 |
def test_get_modified_attributes_for_no_data(): |
| 90 |
""" validate modified attributes when current is None """ |
| 91 |
current = None |
| 92 |
desired = {'name': 'test'} |
| 93 |
my_obj = na_helper() |
| 94 |
result = my_obj.get_modified_attributes(current, desired) |
| 95 |
assert result == {} |
| 96 |
|
| 97 |
|
| 98 |
def test_get_modified_attributes(): |
| 99 |
""" validate modified attributes """ |
| 100 |
current = {'name': ['test', 'abcd', 'xyz', 'pqr'], 'state': 'present'} |
| 101 |
desired = {'name': ['abcd', 'abc', 'xyz', 'pqr'], 'state': 'absent'} |
| 102 |
my_obj = na_helper() |
| 103 |
result = my_obj.get_modified_attributes(current, desired) |
| 104 |
assert result == desired |
| 105 |
|
| 106 |
|
| 107 |
def test_get_modified_attributes_for_intersecting_mixed_list(): |
| 108 |
""" validate modified attributes for list diff """ |
| 109 |
current = {'name': [2, 'four', 'six', 8]} |
| 110 |
desired = {'name': ['a', 8, 'ab', 'four', 'abcd']} |
| 111 |
my_obj = na_helper() |
| 112 |
result = my_obj.get_modified_attributes(current, desired, True) |
| 113 |
assert result == {'name': ['a', 'ab', 'abcd']} |
| 114 |
|
| 115 |
|
| 116 |
def test_get_modified_attributes_for_intersecting_list(): |
| 117 |
""" validate modified attributes for list diff """ |
| 118 |
current = {'name': ['two', 'four', 'six', 'eight']} |
| 119 |
desired = {'name': ['a', 'six', 'ab', 'four', 'abc']} |
| 120 |
my_obj = na_helper() |
| 121 |
result = my_obj.get_modified_attributes(current, desired, True) |
| 122 |
assert result == {'name': ['a', 'ab', 'abc']} |
| 123 |
|
| 124 |
|
| 125 |
def test_get_modified_attributes_for_nonintersecting_list(): |
| 126 |
""" validate modified attributes for list diff """ |
| 127 |
current = {'name': ['two', 'four', 'six', 'eight']} |
| 128 |
desired = {'name': ['a', 'ab', 'abd']} |
| 129 |
my_obj = na_helper() |
| 130 |
result = my_obj.get_modified_attributes(current, desired, True) |
| 131 |
assert result == {'name': ['a', 'ab', 'abd']} |
| 132 |
|
| 133 |
|
| 134 |
def test_get_modified_attributes_for_list_of_dicts_no_data(): |
| 135 |
""" validate modified attributes for list diff """ |
| 136 |
current = None |
| 137 |
desired = {'address_blocks': [{'start': '10.20.10.40', 'size': 5}]} |
| 138 |
my_obj = na_helper() |
| 139 |
result = my_obj.get_modified_attributes(current, desired, True) |
| 140 |
assert result == {} |
| 141 |
|
| 142 |
|
| 143 |
def test_get_modified_attributes_for_intersecting_list_of_dicts(): |
| 144 |
""" validate modified attributes for list diff """ |
| 145 |
current = {'address_blocks': [{'start': '10.10.10.23', 'size': 5}, {'start': '10.10.10.30', 'size': 5}]} |
| 146 |
desired = {'address_blocks': [{'start': '10.10.10.23', 'size': 5}, {'start': '10.10.10.30', 'size': 5}, {'start': '10.20.10.40', 'size': 5}]} |
| 147 |
my_obj = na_helper() |
| 148 |
result = my_obj.get_modified_attributes(current, desired, True) |
| 149 |
assert result == {'address_blocks': [{'start': '10.20.10.40', 'size': 5}]} |
| 150 |
|
| 151 |
|
| 152 |
def test_get_modified_attributes_for_nonintersecting_list_of_dicts(): |
| 153 |
""" validate modified attributes for list diff """ |
| 154 |
current = {'address_blocks': [{'start': '10.10.10.23', 'size': 5}, {'start': '10.10.10.30', 'size': 5}]} |
| 155 |
desired = {'address_blocks': [{'start': '10.20.10.23', 'size': 5}, {'start': '10.20.10.30', 'size': 5}, {'start': '10.20.10.40', 'size': 5}]} |
| 156 |
my_obj = na_helper() |
| 157 |
result = my_obj.get_modified_attributes(current, desired, True) |
| 158 |
assert result == {'address_blocks': [{'start': '10.20.10.23', 'size': 5}, {'start': '10.20.10.30', 'size': 5}, {'start': '10.20.10.40', 'size': 5}]} |
| 159 |
|
| 160 |
|
| 161 |
def test_get_modified_attributes_for_list_diff(): |
| 162 |
""" validate modified attributes for list diff """ |
| 163 |
current = {'name': ['test', 'abcd'], 'state': 'present'} |
| 164 |
desired = {'name': ['abcd', 'abc'], 'state': 'present'} |
| 165 |
my_obj = na_helper() |
| 166 |
result = my_obj.get_modified_attributes(current, desired, True) |
| 167 |
assert result == {'name': ['abc']} |
| 168 |
|
| 169 |
|
| 170 |
def test_get_modified_attributes_for_no_change(): |
| 171 |
""" validate modified attributes for same data in current and desired """ |
| 172 |
current = {'name': 'test'} |
| 173 |
desired = {'name': 'test'} |
| 174 |
my_obj = na_helper() |
| 175 |
result = my_obj.get_modified_attributes(current, desired) |
| 176 |
assert result == {} |
| 177 |
|
| 178 |
|
| 179 |
def test_get_modified_attributes_for_an_empty_desired_list(): |
| 180 |
""" validate modified attributes for an empty desired list """ |
| 181 |
current = {'snapmirror_label': ['daily', 'weekly', 'monthly'], 'state': 'present'} |
| 182 |
desired = {'snapmirror_label': [], 'state': 'present'} |
| 183 |
my_obj = na_helper() |
| 184 |
result = my_obj.get_modified_attributes(current, desired) |
| 185 |
assert result == {'snapmirror_label': []} |
| 186 |
|
| 187 |
|
| 188 |
def test_get_modified_attributes_for_an_empty_desired_list_diff(): |
| 189 |
""" validate modified attributes for an empty desired list with diff""" |
| 190 |
current = {'snapmirror_label': ['daily', 'weekly', 'monthly'], 'state': 'present'} |
| 191 |
desired = {'snapmirror_label': [], 'state': 'present'} |
| 192 |
my_obj = na_helper() |
| 193 |
result = my_obj.get_modified_attributes(current, desired, True) |
| 194 |
assert result == {'snapmirror_label': []} |
| 195 |
|
| 196 |
|
| 197 |
def test_get_modified_attributes_for_an_empty_current_list(): |
| 198 |
""" validate modified attributes for an empty current list """ |
| 199 |
current = {'snapmirror_label': [], 'state': 'present'} |
| 200 |
desired = {'snapmirror_label': ['daily', 'weekly', 'monthly'], 'state': 'present'} |
| 201 |
my_obj = na_helper() |
| 202 |
result = my_obj.get_modified_attributes(current, desired) |
| 203 |
assert result == {'snapmirror_label': ['daily', 'weekly', 'monthly']} |
| 204 |
|
| 205 |
|
| 206 |
def test_get_modified_attributes_for_an_empty_current_list_diff(): |
| 207 |
""" validate modified attributes for an empty current list with diff""" |
| 208 |
current = {'snapmirror_label': [], 'state': 'present'} |
| 209 |
desired = {'snapmirror_label': ['daily', 'weekly', 'monthly'], 'state': 'present'} |
| 210 |
my_obj = na_helper() |
| 211 |
result = my_obj.get_modified_attributes(current, desired, True) |
| 212 |
assert result == {'snapmirror_label': ['daily', 'weekly', 'monthly']} |
| 213 |
|
| 214 |
|
| 215 |
def test_get_modified_attributes_for_empty_lists(): |
| 216 |
""" validate modified attributes for empty lists """ |
| 217 |
current = {'snapmirror_label': [], 'state': 'present'} |
| 218 |
desired = {'snapmirror_label': [], 'state': 'present'} |
| 219 |
my_obj = na_helper() |
| 220 |
result = my_obj.get_modified_attributes(current, desired) |
| 221 |
assert result == {} |
| 222 |
|
| 223 |
|
| 224 |
def test_get_modified_attributes_for_empty_lists_diff(): |
| 225 |
""" validate modified attributes for empty lists with diff """ |
| 226 |
current = {'snapmirror_label': [], 'state': 'present'} |
| 227 |
desired = {'snapmirror_label': [], 'state': 'present'} |
| 228 |
my_obj = na_helper() |
| 229 |
result = my_obj.get_modified_attributes(current, desired, True) |
| 230 |
assert result == {} |
| 231 |
|
| 232 |
|
| 233 |
def test_get_modified_attributes_equal_lists_with_duplicates(): |
| 234 |
""" validate modified attributes for equal lists with duplicates """ |
| 235 |
current = {'schedule': ['hourly', 'daily', 'daily', 'weekly', 'monthly', 'daily'], 'state': 'present'} |
| 236 |
desired = {'schedule': ['hourly', 'daily', 'daily', 'weekly', 'monthly', 'daily'], 'state': 'present'} |
| 237 |
my_obj = na_helper() |
| 238 |
result = my_obj.get_modified_attributes(current, desired, False) |
| 239 |
assert result == {} |
| 240 |
|
| 241 |
|
| 242 |
def test_get_modified_attributes_equal_lists_with_duplicates_diff(): |
| 243 |
""" validate modified attributes for equal lists with duplicates with diff """ |
| 244 |
current = {'schedule': ['hourly', 'daily', 'daily', 'weekly', 'monthly', 'daily'], 'state': 'present'} |
| 245 |
desired = {'schedule': ['hourly', 'daily', 'daily', 'weekly', 'monthly', 'daily'], 'state': 'present'} |
| 246 |
my_obj = na_helper() |
| 247 |
result = my_obj.get_modified_attributes(current, desired, True) |
| 248 |
assert result == {} |
| 249 |
|
| 250 |
|
| 251 |
def test_get_modified_attributes_for_current_list_with_duplicates(): |
| 252 |
""" validate modified attributes for current list with duplicates """ |
| 253 |
current = {'schedule': ['hourly', 'daily', 'daily', 'weekly', 'monthly', 'daily'], 'state': 'present'} |
| 254 |
desired = {'schedule': ['daily', 'daily', 'weekly', 'monthly'], 'state': 'present'} |
| 255 |
my_obj = na_helper() |
| 256 |
result = my_obj.get_modified_attributes(current, desired, False) |
| 257 |
assert result == {'schedule': ['daily', 'daily', 'weekly', 'monthly']} |
| 258 |
|
| 259 |
|
| 260 |
def test_get_modified_attributes_for_current_list_with_duplicates_diff(): |
| 261 |
""" validate modified attributes for current list with duplicates with diff """ |
| 262 |
current = {'schedule': ['hourly', 'daily', 'daily', 'weekly', 'monthly', 'daily'], 'state': 'present'} |
| 263 |
desired = {'schedule': ['daily', 'daily', 'weekly', 'monthly'], 'state': 'present'} |
| 264 |
my_obj = na_helper() |
| 265 |
result = my_obj.get_modified_attributes(current, desired, True) |
| 266 |
assert result == {'schedule': []} |
| 267 |
|
| 268 |
|
| 269 |
def test_get_modified_attributes_for_desired_list_with_duplicates(): |
| 270 |
""" validate modified attributes for desired list with duplicates """ |
| 271 |
current = {'schedule': ['daily', 'weekly', 'monthly'], 'state': 'present'} |
| 272 |
desired = {'schedule': ['hourly', 'daily', 'daily', 'weekly', 'monthly', 'daily'], 'state': 'present'} |
| 273 |
my_obj = na_helper() |
| 274 |
result = my_obj.get_modified_attributes(current, desired, False) |
| 275 |
assert result == {'schedule': ['hourly', 'daily', 'daily', 'weekly', 'monthly', 'daily']} |
| 276 |
|
| 277 |
|
| 278 |
def test_get_modified_attributes_for_desired_list_with_duplicates_diff(): |
| 279 |
""" validate modified attributes for desired list with duplicates with diff """ |
| 280 |
current = {'schedule': ['daily', 'weekly', 'monthly'], 'state': 'present'} |
| 281 |
desired = {'schedule': ['hourly', 'daily', 'daily', 'weekly', 'monthly', 'daily'], 'state': 'present'} |
| 282 |
my_obj = na_helper() |
| 283 |
result = my_obj.get_modified_attributes(current, desired, True) |
| 284 |
assert result == {'schedule': ['hourly', 'daily', 'daily']} |
| 285 |
|
| 286 |
|
| 287 |
def test_get_modified_attributes_exceptions(): |
| 288 |
""" validate exceptions """ |
| 289 |
current = {'schedule': {'name': 'weekly'}, 'state': 'present'} |
| 290 |
desired = {'schedule': 'weekly', 'state': 'present'} |
| 291 |
my_obj = create_ontap_module({'hostname': ''}) |
| 292 |
# mismatch in structure |
| 293 |
error = expect_and_capture_ansible_exception(my_obj.na_helper.get_modified_attributes, TypeError, current, desired) |
| 294 |
assert "Expecting dict, got: weekly with current: {'name': 'weekly'}" in error |
| 295 |
# mismatch in types |
| 296 |
if sys.version_info[:2] > (3, 0): |
| 297 |
# our cmp function reports an exception. But python 2.x has it's own version. |
| 298 |
desired = {'schedule': {'name': 12345}, 'state': 'present'} |
| 299 |
error = expect_and_capture_ansible_exception(my_obj.na_helper.get_modified_attributes, TypeError, current, desired) |
| 300 |
assert ("unorderable types:" in error # 3.5 |
| 301 |
or "'>' not supported between instances of 'str' and 'int'" in error) # 3.9 |
| 302 |
|
| 303 |
|
| 304 |
def test_get_modified_attributes_for_dicts(): |
| 305 |
""" validate modified attributes for dict of dicts """ |
| 306 |
current = {'schedule': {'name': 'weekly'}, 'state': 'present'} |
| 307 |
desired = {'schedule': {'name': 'daily'}, 'state': 'present'} |
| 308 |
my_obj = na_helper() |
| 309 |
result = my_obj.get_modified_attributes(current, desired, True) |
| 310 |
assert result == {'schedule': {'name': 'daily'}} |
| 311 |
|
| 312 |
|
| 313 |
def test_is_rename_action_for_empty_input(): |
| 314 |
""" validate rename action for input None """ |
| 315 |
source = None |
| 316 |
target = None |
| 317 |
my_obj = na_helper() |
| 318 |
result = my_obj.is_rename_action(source, target) |
| 319 |
assert result == source |
| 320 |
|
| 321 |
|
| 322 |
def test_is_rename_action_for_no_source(): |
| 323 |
""" validate rename action when source is None """ |
| 324 |
source = None |
| 325 |
target = 'test2' |
| 326 |
my_obj = na_helper() |
| 327 |
result = my_obj.is_rename_action(source, target) |
| 328 |
assert result is False |
| 329 |
|
| 330 |
|
| 331 |
def test_is_rename_action_for_no_target(): |
| 332 |
""" validate rename action when target is None """ |
| 333 |
source = 'test2' |
| 334 |
target = None |
| 335 |
my_obj = na_helper() |
| 336 |
result = my_obj.is_rename_action(source, target) |
| 337 |
assert result is True |
| 338 |
|
| 339 |
|
| 340 |
def test_is_rename_action(): |
| 341 |
""" validate rename action """ |
| 342 |
source = 'test' |
| 343 |
target = 'test2' |
| 344 |
my_obj = na_helper() |
| 345 |
result = my_obj.is_rename_action(source, target) |
| 346 |
assert result is False |
| 347 |
|
| 348 |
|
| 349 |
# def test_required_is_not_set_to_none(): |
| 350 |
# """ if a key is present, without a value, Ansible sets it to None """ |
| 351 |
# args = {} |
| 352 |
# args['hostname'] = None |
| 353 |
# # my_obj = create_ontap_module(args) |
| 354 |
# # msg = 'hostname requires a value, got: None' |
| 355 |
# # assert msg == expect_and_capture_ansible_exception(my_obj.na_helper.check_and_set_parameters, 'fail', my_obj.module)['msg'] |
| 356 |
# # Expect the AnsibleFailJson exception |
| 357 |
# with pytest.raises(AnsibleFailJson) as excinfo: |
| 358 |
# my_obj = create_ontap_module(args) |
| 359 |
|
| 360 |
# # Check the exception message |
| 361 |
# assert "argument 'hostname' is of type <class 'NoneType'>" in str(excinfo.value) |
| 362 |
# # force a value different than None |
| 363 |
# my_obj.module.params['hostname'] = 1 |
| 364 |
# my_params = my_obj.na_helper.check_and_set_parameters(my_obj.module) |
| 365 |
# assert set(my_params.keys()) == set(['hostname', 'https', 'validate_certs', 'use_rest']) |
| 366 |
|
| 367 |
|
| 368 |
def test_sanitize_wwn_no_action(): |
| 369 |
""" no change """ |
| 370 |
initiator = 'tEsT' |
| 371 |
expected = initiator |
| 372 |
my_obj = na_helper() |
| 373 |
result = my_obj.sanitize_wwn(initiator) |
| 374 |
assert result == expected |
| 375 |
|
| 376 |
|
| 377 |
def test_sanitize_wwn_no_action_valid_iscsi(): |
| 378 |
""" no change """ |
| 379 |
initiator = 'iqn.1995-08.com.eXaMpLe:StRiNg' |
| 380 |
expected = initiator |
| 381 |
my_obj = na_helper() |
| 382 |
result = my_obj.sanitize_wwn(initiator) |
| 383 |
assert result == expected |
| 384 |
|
| 385 |
|
| 386 |
def test_sanitize_wwn_no_action_valid_wwn(): |
| 387 |
""" no change """ |
| 388 |
initiator = '01:02:03:04:0A:0b:0C:0d' |
| 389 |
expected = initiator.lower() |
| 390 |
my_obj = na_helper() |
| 391 |
result = my_obj.sanitize_wwn(initiator) |
| 392 |
assert result == expected |
| 393 |
|
| 394 |
|
| 395 |
def test_filter_empty_dict(): |
| 396 |
""" empty dict return empty dict """ |
| 397 |
my_obj = na_helper() |
| 398 |
arg = {} |
| 399 |
result = my_obj.filter_out_none_entries(arg) |
| 400 |
assert arg == result |
| 401 |
|
| 402 |
|
| 403 |
def test_filter_empty_list(): |
| 404 |
""" empty list return empty list """ |
| 405 |
my_obj = na_helper() |
| 406 |
arg = [] |
| 407 |
result = my_obj.filter_out_none_entries(arg) |
| 408 |
assert arg == result |
| 409 |
|
| 410 |
|
| 411 |
def test_filter_typeerror_on_none(): |
| 412 |
""" empty list return empty list """ |
| 413 |
my_obj = na_helper() |
| 414 |
arg = None |
| 415 |
with pytest.raises(TypeError) as exc: |
| 416 |
my_obj.filter_out_none_entries(arg) |
| 417 |
if sys.version_info[:2] < (3, 0): |
| 418 |
# the assert fails on 2.x |
| 419 |
return |
| 420 |
msg = "unexpected type <class 'NoneType'>" |
| 421 |
assert exc.value.args[0] == msg |
| 422 |
|
| 423 |
|
| 424 |
def test_filter_typeerror_on_str(): |
| 425 |
""" empty list return empty list """ |
| 426 |
my_obj = na_helper() |
| 427 |
arg = "" |
| 428 |
with pytest.raises(TypeError) as exc: |
| 429 |
my_obj.filter_out_none_entries(arg) |
| 430 |
if sys.version_info[:2] < (3, 0): |
| 431 |
# the assert fails on 2.x |
| 432 |
return |
| 433 |
msg = "unexpected type <class 'str'>" |
| 434 |
assert exc.value.args[0] == msg |
| 435 |
|
| 436 |
|
| 437 |
def test_filter_simple_dict(): |
| 438 |
""" simple dict return simple dict """ |
| 439 |
my_obj = na_helper() |
| 440 |
arg = dict(a=None, b=1, c=None, d=2, e=3) |
| 441 |
expected = dict(b=1, d=2, e=3) |
| 442 |
result = my_obj.filter_out_none_entries(arg) |
| 443 |
assert expected == result |
| 444 |
|
| 445 |
|
| 446 |
def test_filter_simple_list(): |
| 447 |
""" simple list return simple list """ |
| 448 |
my_obj = na_helper() |
| 449 |
arg = [None, 2, 3, None, 5] |
| 450 |
expected = [2, 3, 5] |
| 451 |
result = my_obj.filter_out_none_entries(arg) |
| 452 |
assert expected == result |
| 453 |
|
| 454 |
|
| 455 |
def test_filter_dict_dict(): |
| 456 |
""" simple dict return simple dict """ |
| 457 |
my_obj = na_helper() |
| 458 |
arg = dict(a=None, b=dict(u=1, v=None, w=2), c={}, d=2, e=3) |
| 459 |
expected = dict(b=dict(u=1, w=2), d=2, e=3) |
| 460 |
result = my_obj.filter_out_none_entries(arg) |
| 461 |
assert expected == result |
| 462 |
|
| 463 |
|
| 464 |
def test_filter_list_list(): |
| 465 |
""" simple list return simple list """ |
| 466 |
my_obj = na_helper() |
| 467 |
arg = [None, [1, None, 3], 3, None, 5] |
| 468 |
expected = [[1, 3], 3, 5] |
| 469 |
result = my_obj.filter_out_none_entries(arg) |
| 470 |
assert expected == result |
| 471 |
|
| 472 |
|
| 473 |
def test_filter_dict_list_dict(): |
| 474 |
""" simple dict return simple dict """ |
| 475 |
my_obj = na_helper() |
| 476 |
arg = dict(a=None, b=[dict(u=1, v=None, w=2), 5, None, dict(x=6, y=None)], c={}, d=2, e=3) |
| 477 |
expected = dict(b=[dict(u=1, w=2), 5, dict(x=6)], d=2, e=3) |
| 478 |
result = my_obj.filter_out_none_entries(arg) |
| 479 |
assert expected == result |
| 480 |
|
| 481 |
|
| 482 |
def test_filter_list_dict_list(): |
| 483 |
""" simple list return simple list """ |
| 484 |
my_obj = na_helper() |
| 485 |
arg = [None, [1, None, 3], dict(a=None, b=[7, None, 9], c=None, d=dict(u=None, v=10)), None, 5] |
| 486 |
expected = [[1, 3], dict(b=[7, 9], d=dict(v=10)), 5] |
| 487 |
result = my_obj.filter_out_none_entries(arg) |
| 488 |
assert expected == result |
| 489 |
|
| 490 |
|
| 491 |
def test_convert_value(): |
| 492 |
""" positive tests """ |
| 493 |
my_obj = na_helper() |
| 494 |
for value, convert_to, expected in [ |
| 495 |
('any', None, 'any'), |
| 496 |
(12345, None, 12345), |
| 497 |
('12345', int, 12345), |
| 498 |
('any', str, 'any'), |
| 499 |
('true', bool, True), |
| 500 |
('false', bool, False), |
| 501 |
('online', 'bool_online', True), |
| 502 |
('any', 'bool_online', False), |
| 503 |
]: |
| 504 |
result, error = my_obj.convert_value(value, convert_to) |
| 505 |
assert error is None |
| 506 |
assert expected == result |
| 507 |
|
| 508 |
|
| 509 |
def test_convert_value_with_error(): |
| 510 |
""" negative tests """ |
| 511 |
my_obj = na_helper() |
| 512 |
for value, convert_to, expected in [ |
| 513 |
(12345, 'any', "Unexpected type:"), |
| 514 |
('any', int, "Unexpected value for int: any"), |
| 515 |
('any', bool, "Unexpected value: any received from ZAPI for boolean attribute"), |
| 516 |
]: |
| 517 |
result, error = my_obj.convert_value(value, convert_to) |
| 518 |
print(value, convert_to, result, '"%s"' % expected, '"%s"' % error) |
| 519 |
assert result is None |
| 520 |
assert expected in error |
| 521 |
|
| 522 |
|
| 523 |
def test_convert_value_with_exception(): |
| 524 |
""" negative tests """ |
| 525 |
my_obj = create_ontap_module({'hostname': ''}) |
| 526 |
expect_and_capture_ansible_exception(my_obj.na_helper.convert_value, 'fail', 'any', 'any') |
| 527 |
|
| 528 |
|
| 529 |
def get_zapi_info(): |
| 530 |
return { |
| 531 |
'a': {'b': '12345', 'bad_stuff': ['a', 'b'], 'none_stuff': None} |
| 532 |
} |
| 533 |
|
| 534 |
|
| 535 |
def get_zapi_na_element(zapi_info): |
| 536 |
na_element, valid = build_zapi_response(zapi_info) |
| 537 |
if valid != 'valid' and sys.version_info[:2] < (2, 7): |
| 538 |
pytest.skip('Skipping Unit Tests on 2.6 as netapp-lib is not available') |
| 539 |
assert valid == 'valid' |
| 540 |
return na_element |
| 541 |
|
| 542 |
|
| 543 |
def test_zapi_get_value(): |
| 544 |
na_element = get_zapi_na_element(get_zapi_info()) |
| 545 |
my_obj = na_helper() |
| 546 |
assert my_obj.zapi_get_value(na_element, ['a', 'b'], convert_to=int) == 12345 |
| 547 |
# missing key returns None if sparse dict is allowed (default) |
| 548 |
assert my_obj.zapi_get_value(na_element, ['a', 'c'], convert_to=int) is None |
| 549 |
# missing key returns 'default' - note, no conversion - if sparse dict is allowed (default) |
| 550 |
assert my_obj.zapi_get_value(na_element, ['a', 'c'], convert_to=int, default='default') == 'default' |
| 551 |
|
| 552 |
|
| 553 |
def test_zapi_get_value_with_exception(): |
| 554 |
na_element = get_zapi_na_element(get_zapi_info()) |
| 555 |
my_obj = create_ontap_module({'hostname': ''}) |
| 556 |
# KeyError |
| 557 |
error = expect_and_capture_ansible_exception(my_obj.na_helper.zapi_get_value, 'fail', na_element, ['a', 'c'], required=True)['msg'] |
| 558 |
assert 'No element by given name c.' in error |
| 559 |
|
| 560 |
|
| 561 |
def test_safe_get(): |
| 562 |
na_element = get_zapi_na_element(get_zapi_info()) |
| 563 |
my_obj = na_helper() |
| 564 |
assert my_obj.safe_get(na_element, ['a', 'b']) == '12345' |
| 565 |
assert my_obj.safe_get(na_element, ['a', 'c']) is None |
| 566 |
assert my_obj.safe_get(get_zapi_info(), ['a', 'b']) == '12345' |
| 567 |
assert my_obj.safe_get(get_zapi_info(), ['a', 'c']) is None |
| 568 |
assert my_obj.safe_get(get_zapi_info(), ['a', 'none_stuff', 'extra']) is None # TypeError on None |
| 569 |
|
| 570 |
|
| 571 |
def test_safe_get_dict_of_list(): |
| 572 |
my_obj = na_helper() |
| 573 |
my_dict = {'a': ['b', 'c', {'d': ['e']}]} |
| 574 |
assert my_obj.safe_get(my_dict, ['a', 0]) == 'b' |
| 575 |
assert my_obj.safe_get(my_dict, ['a', 2, 'd', 0]) == 'e' |
| 576 |
assert my_obj.safe_get(my_dict, ['a', 3]) is None |
| 577 |
|
| 578 |
|
| 579 |
def test_safe_get_with_exception(): |
| 580 |
na_element = get_zapi_na_element(get_zapi_info()) |
| 581 |
my_obj = create_ontap_module({'hostname': ''}) |
| 582 |
# KeyError |
| 583 |
error = expect_and_capture_ansible_exception(my_obj.na_helper.safe_get, KeyError, na_element, ['a', 'c'], allow_sparse_dict=False) |
| 584 |
assert 'No element by given name c.' in error |
| 585 |
error = expect_and_capture_ansible_exception(my_obj.na_helper.safe_get, KeyError, get_zapi_info(), ['a', 'c'], allow_sparse_dict=False) |
| 586 |
assert 'c' == error |
| 587 |
# IndexError |
| 588 |
error = expect_and_capture_ansible_exception(my_obj.na_helper.safe_get, IndexError, get_zapi_info(), ['a', 'bad_stuff', 4], allow_sparse_dict=False) |
| 589 |
print('EXC', error) |
| 590 |
if ut_utilities.is_indexerror_exception_formatted(): |
| 591 |
assert 'list index out of range' in str(error) |
| 592 |
error = expect_and_capture_ansible_exception(my_obj.na_helper.safe_get, IndexError, get_zapi_info(), ['a', 'bad_stuff', -4], allow_sparse_dict=False) |
| 593 |
print('EXC', error) |
| 594 |
if ut_utilities.is_indexerror_exception_formatted(): |
| 595 |
assert 'list index out of range' in str(error) |
| 596 |
# TypeError - not sure I can build a valid ZAPI NaElement that can give a type error, but using a dict worked. |
| 597 |
error = expect_and_capture_ansible_exception(my_obj.na_helper.safe_get, TypeError, get_zapi_info(), ['a', 'bad_stuff', 'extra'], allow_sparse_dict=False) |
| 598 |
# 'list indices must be integers, not str' with 2.7 |
| 599 |
# 'list indices must be integers or slices, not str' with 3.x |
| 600 |
assert 'list indices must be integers' in error |
| 601 |
error = expect_and_capture_ansible_exception(my_obj.na_helper.safe_get, TypeError, get_zapi_info(), ['a', 'none_stuff', 'extra'], allow_sparse_dict=False) |
| 602 |
# 'NoneType' object has no attribute '__getitem__' with 2.7 |
| 603 |
# 'NoneType' object is not subscriptable with 3.x |
| 604 |
assert "'NoneType' object " in error |
| 605 |
|
| 606 |
|
| 607 |
def test_get_value_for_bool(): |
| 608 |
my_obj = na_helper() |
| 609 |
for value, from_zapi, expected in [ |
| 610 |
(None, 'any', None), |
| 611 |
('true', True, True), |
| 612 |
('false', True, False), |
| 613 |
('any', True, False), # no error checking if key is not present |
| 614 |
(True, False, 'true'), |
| 615 |
(False, False, 'false'), |
| 616 |
('any', False, 'true'), # no error checking if key is not present |
| 617 |
]: |
| 618 |
result = my_obj.get_value_for_bool(from_zapi, value) |
| 619 |
print(value, from_zapi, result) |
| 620 |
assert result == expected |
| 621 |
|
| 622 |
|
| 623 |
def test_get_value_for_bool_with_exception(): |
| 624 |
na_element = get_zapi_na_element(get_zapi_info()) |
| 625 |
my_obj = create_ontap_module({'hostname': ''}) |
| 626 |
# Error with from_zapi=True if key is present |
| 627 |
error = expect_and_capture_ansible_exception(my_obj.na_helper.get_value_for_bool, TypeError, True, 1234, 'key') |
| 628 |
assert "expecting 'str' type for 'key': 1234" in error |
| 629 |
error = expect_and_capture_ansible_exception(my_obj.na_helper.get_value_for_bool, ValueError, True, 'any', 'key') |
| 630 |
assert "Unexpected value: 'any' received from ZAPI for boolean attribute: 'key'" == error |
| 631 |
# TypeError - expecting a bool |
| 632 |
error = expect_and_capture_ansible_exception(my_obj.na_helper.get_value_for_bool, TypeError, False, 'any', 'key') |
| 633 |
assert "expecting 'bool' type for 'key': 'any'" in error |
| 634 |
|
| 635 |
|
| 636 |
def test_get_value_for_int(): |
| 637 |
my_obj = na_helper() |
| 638 |
for value, from_zapi, expected in [ |
| 639 |
(None, 'any', None), |
| 640 |
('12345', True, 12345), |
| 641 |
(12345, True, 12345), # no error checking if key is not present |
| 642 |
(12345, False, '12345'), |
| 643 |
]: |
| 644 |
result = my_obj.get_value_for_int(from_zapi, value) |
| 645 |
print(value, from_zapi, result) |
| 646 |
assert result == expected |
| 647 |
|
| 648 |
|
| 649 |
def test_get_value_for_int_with_exception(): |
| 650 |
na_element = get_zapi_na_element(get_zapi_info()) |
| 651 |
my_obj = create_ontap_module({'hostname': ''}) |
| 652 |
# Error with from_zapi=True if key is present |
| 653 |
error = expect_and_capture_ansible_exception(my_obj.na_helper.get_value_for_int, TypeError, True, 1234, 'key') |
| 654 |
assert "expecting 'str' type for 'key': 1234" in error |
| 655 |
error = expect_and_capture_ansible_exception(my_obj.na_helper.get_value_for_int, ValueError, True, 'any', 'key') |
| 656 |
assert "invalid literal for int() with base 10: 'any'" == error |
| 657 |
# TypeError - expecting a int |
| 658 |
error = expect_and_capture_ansible_exception(my_obj.na_helper.get_value_for_int, TypeError, False, 'any', 'key') |
| 659 |
assert "expecting 'int' type for 'key': 'any'" in error |
| 660 |
|
| 661 |
|
| 662 |
def test_get_value_for_list(): |
| 663 |
my_obj = na_helper() |
| 664 |
zapi_info = { |
| 665 |
'a': [{'b': 'a1'}, {'b': 'a2'}, {'b': 'a3'}] |
| 666 |
} |
| 667 |
for from_zapi, zapi_parent, zapi_child, data, expected in [ |
| 668 |
(True, None, None, None, []), |
| 669 |
(True, get_zapi_na_element(zapi_info), None, None, [None]), |
| 670 |
(True, get_zapi_na_element(get_zapi_info()).get_child_by_name('a'), None, None, ['12345', None, None]), |
| 671 |
(True, get_zapi_na_element(zapi_info).get_child_by_name('a'), None, None, ['a1', 'a2', 'a3']), |
| 672 |
(False, 'parent', 'child', [], b'<parent/>'), |
| 673 |
(False, 'parent', 'child', ['1', '1'], b'<parent><child>1</child><child>1</child></parent>'), |
| 674 |
]: |
| 675 |
result = my_obj.get_value_for_list(from_zapi, zapi_parent, zapi_child, data) |
| 676 |
print(from_zapi, expected, result) |
| 677 |
if from_zapi: |
| 678 |
if zapi_parent: |
| 679 |
print(zapi_parent.to_string()) |
| 680 |
# ordering maybe different with 3.5 compared to 3.9 or 2.7 |
| 681 |
assert set(result) == set(expected) |
| 682 |
else: |
| 683 |
print(result.to_string()) |
| 684 |
assert result.to_string() == expected |
| 685 |
|
| 686 |
|
| 687 |
def test_zapi_get_attrs(): |
| 688 |
my_obj = na_helper() |
| 689 |
zapi_info = { |
| 690 |
'a': {'b': 'a1', 'c': 'a2', 'd': 'a3', 'int': '123'} |
| 691 |
} |
| 692 |
naelement = get_zapi_na_element(zapi_info) |
| 693 |
attr_dict = { |
| 694 |
'first': {'key_list': ['a', 'b']} |
| 695 |
} |
| 696 |
result = {} |
| 697 |
my_obj.zapi_get_attrs(naelement, attr_dict, result) |
| 698 |
assert result == {'first': 'a1'} |
| 699 |
|
| 700 |
# if element not found return None, unless omitnone is True |
| 701 |
attr_dict = { |
| 702 |
'none': {'key_list': ['a', 'z'], 'omitnone': True} |
| 703 |
} |
| 704 |
my_obj.zapi_get_attrs(naelement, attr_dict, result) |
| 705 |
assert result == {'first': 'a1'} |
| 706 |
|
| 707 |
# if element not found return None when required and omitnone are False |
| 708 |
attr_dict = { |
| 709 |
'none': {'key_list': ['a', 'z']} |
| 710 |
} |
| 711 |
my_obj.zapi_get_attrs(naelement, attr_dict, result) |
| 712 |
assert result == {'first': 'a1', 'none': None} |
| 713 |
|
| 714 |
# if element not found return default |
| 715 |
result = {} |
| 716 |
attr_dict = { |
| 717 |
'none': {'key_list': ['a', 'z'], 'default': 'some_default'} |
| 718 |
} |
| 719 |
my_obj.zapi_get_attrs(naelement, attr_dict, result) |
| 720 |
assert result == {'none': 'some_default'} |
| 721 |
|
| 722 |
# convert to int |
| 723 |
result = {} |
| 724 |
attr_dict = { |
| 725 |
'int': {'key_list': ['a', 'int'], 'convert_to': int} |
| 726 |
} |
| 727 |
my_obj.zapi_get_attrs(naelement, attr_dict, result) |
| 728 |
assert result == {'int': 123} |
| 729 |
|
| 730 |
# if element not found return None, unless required is True |
| 731 |
my_obj = create_ontap_module({'hostname': 'abc'}) |
| 732 |
attr_dict = { |
| 733 |
'none': {'key_list': ['a', 'z'], 'required': True} |
| 734 |
} |
| 735 |
# the contents of to_string() may be in a different sequence depending on the pytohn version |
| 736 |
assert expect_and_capture_ansible_exception(my_obj.na_helper.zapi_get_attrs, 'fail', naelement, attr_dict, result)['msg'].startswith(( |
| 737 |
"Error reading ['a', 'z'] from b'<results status=\"passed\"><a>", # python 3.x |
| 738 |
"Error reading ['a', 'z'] from <results status=\"passed\"><a>" # python 2.7 |
| 739 |
)) |
| 740 |
|
| 741 |
|
| 742 |
def test_set_parameters(): |
| 743 |
my_obj = na_helper() |
| 744 |
adict = dict((x, x * x) for x in range(10)) |
| 745 |
assert my_obj.set_parameters(adict) == adict |
| 746 |
assert my_obj.parameters == adict |
| 747 |
assert len(my_obj.parameters) == 10 |
| 748 |
|
| 749 |
# None values are copied |
| 750 |
adict[3] = None |
| 751 |
assert my_obj.set_parameters(adict) != adict |
| 752 |
assert my_obj.parameters != adict |
| 753 |
assert len(my_obj.parameters) == 9 |
| 754 |
|
| 755 |
|
| 756 |
def test_get_caller(): |
| 757 |
assert na_helper.get_caller(0) == 'get_caller' |
| 758 |
assert na_helper.get_caller(1) == 'test_get_caller' |
| 759 |
|
| 760 |
def one(depth): |
| 761 |
return na_helper.get_caller(depth) |
| 762 |
assert one(1) == 'one' |
| 763 |
|
| 764 |
def two(): |
| 765 |
return one(2) |
| 766 |
assert two() == 'two' |
| 767 |
|
| 768 |
def three(): |
| 769 |
return two(), one(3) |
| 770 |
assert three() == ('two', 'test_get_caller') |
| 771 |
|
| 772 |
|
| 773 |
@patch('traceback.extract_stack') |
| 774 |
def test_get_caller_2_7(mock_frame): |
| 775 |
frame = ('first', 'second', 'function_name') |
| 776 |
mock_frame.return_value = [frame] |
| 777 |
assert na_helper.get_caller(0) == 'function_name' |
| 778 |
|
| 779 |
|
| 780 |
@patch('traceback.extract_stack') |
| 781 |
def test_get_caller_bad_format(mock_frame): |
| 782 |
frame = ('first', 'second') |
| 783 |
mock_frame.return_value = [frame] |
| 784 |
assert na_helper.get_caller(0) == "Error retrieving function name: tuple index out of range - [('first', 'second')]" |
| 785 |
|
| 786 |
|
| 787 |
def test_fail_on_error(): |
| 788 |
my_obj = create_ontap_module({'hostname': 'abc'}) |
| 789 |
assert my_obj.na_helper.fail_on_error(None) is None |
| 790 |
assert expect_and_capture_ansible_exception(my_obj.na_helper.fail_on_error, 'fail', 'error_msg')['msg'] ==\ |
| 791 |
'Error in expect_and_capture_ansible_exception: error_msg' |
| 792 |
assert expect_and_capture_ansible_exception(my_obj.na_helper.fail_on_error, 'fail', 'error_msg', 'api')['msg'] ==\ |
| 793 |
'Error in expect_and_capture_ansible_exception: calling api: api: error_msg' |
| 794 |
previous_errors = ['some_errror'] |
| 795 |
exc = expect_and_capture_ansible_exception(my_obj.na_helper.fail_on_error, 'fail', 'error_msg', 'api', previous_errors=previous_errors) |
| 796 |
assert exc['msg'] == 'Error in expect_and_capture_ansible_exception: calling api: api: error_msg' |
| 797 |
assert exc['previous_errors'] == previous_errors[0] |
| 798 |
exc = expect_and_capture_ansible_exception(my_obj.na_helper.fail_on_error, 'fail', 'error_msg', 'api', True) |
| 799 |
assert exc['msg'] == 'Error in expect_and_capture_ansible_exception: calling api: api: error_msg' |
| 800 |
assert exc['stack'] |
| 801 |
delattr(my_obj.na_helper, 'ansible_module') |
| 802 |
assert expect_and_capture_ansible_exception(my_obj.na_helper.fail_on_error, AttributeError, 'error_message') ==\ |
| 803 |
"Expecting self.ansible_module to be set when reporting {'msg': 'Error in expect_and_capture_ansible_exception: error_message'}" |
| 804 |
|
| 805 |
|
| 806 |
def test_cmp(): |
| 807 |
assert na_cmp(None, 'any') == -1 |
| 808 |
# string comparison ignores case |
| 809 |
assert na_cmp('ABC', 'abc') == 0 |
| 810 |
assert na_cmp('abcd', 'abc') == 1 |
| 811 |
assert na_cmp('abd', 'abc') == 1 |
| 812 |
assert na_cmp(['abd', 'abc'], ['abc', 'abd']) == 0 |
| 813 |
# list comparison ignores case |
| 814 |
assert na_cmp(['ABD', 'abc'], ['abc', 'abd']) == 0 |
| 815 |
# but not duplicates |
| 816 |
assert na_cmp(['ABD', 'ABD', 'abc'], ['abc', 'abd']) == 1 |
| 817 |
|
| 818 |
|
| 819 |
def test_fall_back_to_zapi(): |
| 820 |
my_obj = create_ontap_module({'hostname': 'abc'}, version=2) |
| 821 |
parameters = {'use_rest': 'never'} |
| 822 |
assert my_obj.na_helper.fall_back_to_zapi(my_obj.na_helper.ansible_module, 'some message', parameters) is None |
| 823 |
assert_no_warnings() |
| 824 |
|
| 825 |
parameters = {'use_rest': 'auto'} |
| 826 |
assert my_obj.na_helper.fall_back_to_zapi(my_obj.na_helper.ansible_module, 'some message', parameters) is False |
| 827 |
assert_warning_was_raised('Falling back to ZAPI: some message') |
| 828 |
|
| 829 |
parameters = {'use_rest': 'always'} |
| 830 |
clear_warnings() |
| 831 |
assert 'Error: some message' in expect_and_capture_ansible_exception( |
| 832 |
my_obj.na_helper.fall_back_to_zapi, 'fail', my_obj.na_helper.ansible_module, 'some message', parameters)['msg'] |
| 833 |
assert_no_warnings() |
| 834 |
|
| 835 |
|
| 836 |
def test_module_deprecated(): |
| 837 |
my_obj = create_ontap_module({'hostname': 'abc'}) |
| 838 |
assert my_obj.na_helper.module_deprecated(my_obj.na_helper.ansible_module) is None |
| 839 |
assert_warning_was_raised('This module only supports ZAPI and is deprecated. ' |
| 840 |
'After upgrading to ONTAP 9.14 and beyond, ONTAPI (ZAPI) remains active for 30 days. ' |
| 841 |
'If no calls are detected, it will be automatically disabled but can be re-enabled via CLI command.') |
| 842 |
|
| 843 |
|
| 844 |
def test_module_replaces(): |
| 845 |
my_obj = create_ontap_module({'hostname': 'abc'}) |
| 846 |
new_module = 'na_ontap_new_modules' |
| 847 |
assert my_obj.na_helper.module_replaces(new_module, my_obj.na_helper.ansible_module) is None |
| 848 |
assert_warning_was_raised('netapp.ontap.%s should be used instead.' % new_module) |
| 849 |
|
| 850 |
|
| 851 |
def test_compare_chmod_value(): |
| 852 |
myobj = na_helper() |
| 853 |
assert myobj.compare_chmod_value("0777", "---rwxrwxrwx") is True |
| 854 |
assert myobj.compare_chmod_value("777", "---rwxrwxrwx") is True |
| 855 |
assert myobj.compare_chmod_value("7777", "sstrwxrwxrwx") is True |
| 856 |
assert myobj.compare_chmod_value("4555", "s--r-xr-xr-x") is True |
| 857 |
assert myobj.compare_chmod_value(None, "---rwxrwxrwx") is False |
| 858 |
assert myobj.compare_chmod_value("755", "rwxrwxrwxrwxr") is False |
| 859 |
assert myobj.compare_chmod_value("777", "---ssxrwxrwx") is False |
| 860 |
assert myobj.compare_chmod_value("7777", "rwxrwxrwxrwx") is False |
| 861 |
assert myobj.compare_chmod_value("7777", "7777") is True |
| 862 |
|
| 863 |
|
| 864 |
def test_ignore_missing_vserver_on_delete(): |
| 865 |
my_obj = create_ontap_module({'hostname': 'abc'}) |
| 866 |
assert not my_obj.na_helper.ignore_missing_vserver_on_delete('error') |
| 867 |
my_obj.na_helper.parameters['state'] = 'absent' |
| 868 |
error = 'Internal error, vserver name is required, when processing error: error_msg' |
| 869 |
assert error in expect_and_capture_ansible_exception(my_obj.na_helper.ignore_missing_vserver_on_delete, 'fail', 'error_msg')['msg'] |
| 870 |
my_obj.na_helper.parameters['vserver'] = 'svm' |
| 871 |
error = 'Internal error, error should contain "message" key, found:' |
| 872 |
assert error in expect_and_capture_ansible_exception(my_obj.na_helper.ignore_missing_vserver_on_delete, 'fail', {'error_msg': 'error'})['msg'] |
| 873 |
error = 'Internal error, error should be str or dict, found:' |
| 874 |
assert error in expect_and_capture_ansible_exception(my_obj.na_helper.ignore_missing_vserver_on_delete, 'fail', ['error_msg'])['msg'] |
| 875 |
assert not my_obj.na_helper.ignore_missing_vserver_on_delete('error') |
| 876 |
assert my_obj.na_helper.ignore_missing_vserver_on_delete({'message': 'SVM "svm" does not exist.'}) |
| 877 |
|
| 878 |
|
| 879 |
def test_remove_hal_links(): |
| 880 |
my_obj = create_ontap_module({'hostname': 'abc'}) |
| 881 |
assert my_obj.na_helper.remove_hal_links(None) is None |
| 882 |
assert my_obj.na_helper.remove_hal_links('string') is None |
| 883 |
adict = { |
| 884 |
'_links': 'whatever', |
| 885 |
'other': 'other' |
| 886 |
} |
| 887 |
# dict |
| 888 |
test_object = copy.deepcopy(adict) |
| 889 |
assert my_obj.na_helper.remove_hal_links(test_object) is None |
| 890 |
assert '_links' not in test_object |
| 891 |
# list of dicts |
| 892 |
test_object = [copy.deepcopy(adict)] * 5 |
| 893 |
assert my_obj.na_helper.remove_hal_links(test_object) is None |
| 894 |
assert all('_links' not in elem for elem in test_object) |
| 895 |
# dict of dicts |
| 896 |
test_object = {'a': copy.deepcopy(adict), 'b': copy.deepcopy(adict)} |
| 897 |
assert my_obj.na_helper.remove_hal_links(test_object) is None |
| 898 |
assert all('_links' not in value for value in test_object.values()) |
| 899 |
# list of list of dicts |
| 900 |
items = [copy.deepcopy(adict)] * 5 |
| 901 |
test_object = [items, items] |
| 902 |
assert my_obj.na_helper.remove_hal_links(test_object) is None |
| 903 |
assert all('_links' not in elem for elems in test_object for elem in elems) |