| 1 |
# (c) 2020, 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 |
|
| 10 |
from ansible_collections.netapp.ontap.tests.unit.compat.mock import patch |
| 11 |
# pylint: disable=unused-import |
| 12 |
from ansible_collections.netapp.ontap.tests.unit.plugins.module_utils.ansible_mocks import\ |
| 13 |
call_main, create_module, expect_and_capture_ansible_exception, patch_ansible |
| 14 |
import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils |
| 15 |
from ansible_collections.netapp.ontap.tests.unit.framework.mock_rest_and_zapi_requests import\ |
| 16 |
patch_request_and_invoke, register_responses |
| 17 |
from ansible_collections.netapp.ontap.tests.unit.framework.zapi_factory import build_zapi_error, build_zapi_response, zapi_error_message, zapi_responses |
| 18 |
|
| 19 |
from ansible_collections.netapp.ontap.plugins.modules.na_ontap_lun import NetAppOntapLUN as my_module, main as my_main # 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 |
|
| 25 |
def lun_info(name, next_tag=None): |
| 26 |
info = { |
| 27 |
'num-records': 1, |
| 28 |
'attributes-list': [{ |
| 29 |
'lun_info': { |
| 30 |
'path': "/what/ever/%s" % name, |
| 31 |
'size': 5368709120, |
| 32 |
'is-space-alloc-enabled': "false", |
| 33 |
'is-space-reservation-enabled': "true", |
| 34 |
'multiprotocol-type': 'linux', |
| 35 |
'qos-policy-group': 'qospol', |
| 36 |
'qos-adaptive-policy-group': 'qosadppol', |
| 37 |
} |
| 38 |
}] |
| 39 |
} |
| 40 |
if next_tag: |
| 41 |
info['next-tag'] = next_tag |
| 42 |
return info |
| 43 |
|
| 44 |
|
| 45 |
ZRR = zapi_responses({ |
| 46 |
'lun_info': build_zapi_response(lun_info('lun_name')), |
| 47 |
'lun_info_from': build_zapi_response(lun_info('lun_from_name')), |
| 48 |
'lun_info_with_tag': build_zapi_response(lun_info('lun_name', 'more to come')), |
| 49 |
'error_9042': build_zapi_error(9042, 'new size == old size, more or less'), |
| 50 |
}) |
| 51 |
|
| 52 |
|
| 53 |
DEFAULT_ARGS = { |
| 54 |
'hostname': 'hostname', |
| 55 |
'username': 'username', |
| 56 |
'password': 'password', |
| 57 |
'use_rest': 'use_rest', |
| 58 |
'name': 'lun_name', |
| 59 |
'vserver': 'lunsvm_name', |
| 60 |
} |
| 61 |
|
| 62 |
|
| 63 |
@patch('ansible_collections.netapp.ontap.plugins.module_utils.netapp.HAS_NETAPP_LIB', False) |
| 64 |
def test_module_fail_when_netapp_lib_missing(): |
| 65 |
''' required lib missing ''' |
| 66 |
module_args = { |
| 67 |
'use_rest': 'never', |
| 68 |
} |
| 69 |
assert 'Error: the python NetApp-Lib module is required. Import error: None' in call_main(my_main, DEFAULT_ARGS, module_args, fail=True)['msg'] |
| 70 |
|
| 71 |
|
| 72 |
def test_module_fail_when_required_args_missing(): |
| 73 |
''' required arguments are reported as errors ''' |
| 74 |
register_responses([ |
| 75 |
]) |
| 76 |
module_args = { |
| 77 |
'use_rest': 'never' |
| 78 |
} |
| 79 |
print('Info: %s' % call_main(my_main, {}, module_args, fail=True)['msg']) |
| 80 |
|
| 81 |
|
| 82 |
def test_create_error_missing_param(): |
| 83 |
''' Test if create throws an error if required param 'destination_vserver' is not specified''' |
| 84 |
register_responses([ |
| 85 |
('ZAPI', 'lun-get-iter', ZRR['no_records']), |
| 86 |
]) |
| 87 |
module_args = { |
| 88 |
'use_rest': 'never', |
| 89 |
} |
| 90 |
msg = "Error: 'flexvol_name' option is required when using ZAPI." |
| 91 |
assert msg == call_main(my_main, DEFAULT_ARGS, module_args, fail=True)['msg'] |
| 92 |
module_args['flexvol_name'] = 'xxx' |
| 93 |
msg = 'size is a required parameter for create.' |
| 94 |
assert msg == call_main(my_main, DEFAULT_ARGS, module_args, fail=True)['msg'] |
| 95 |
|
| 96 |
|
| 97 |
def test_successful_create(): |
| 98 |
''' Test successful create ''' |
| 99 |
register_responses([ |
| 100 |
('ZAPI', 'lun-get-iter', ZRR['no_records']), |
| 101 |
('ZAPI', 'lun-create-by-size', ZRR['success']), |
| 102 |
# second create |
| 103 |
('ZAPI', 'lun-get-iter', ZRR['no_records']), |
| 104 |
('ZAPI', 'lun-create-by-size', ZRR['success']), |
| 105 |
]) |
| 106 |
module_args = { |
| 107 |
'use_rest': 'never', |
| 108 |
'comment': 'some comment', |
| 109 |
'flexvol_name': 'vol_name', |
| 110 |
'qos_adaptive_policy_group': 'new_adaptive_pol', |
| 111 |
'size': 5, |
| 112 |
'space_allocation': False, |
| 113 |
'space_reserve': False, |
| 114 |
} |
| 115 |
assert call_main(my_main, DEFAULT_ARGS, module_args)['changed'] |
| 116 |
module_args = { |
| 117 |
'use_rest': 'never', |
| 118 |
'comment': 'some comment', |
| 119 |
'flexvol_name': 'vol_name', |
| 120 |
'os_type': 'windows', |
| 121 |
'qos_policy_group': 'new_pol', |
| 122 |
'size': 5, |
| 123 |
'space_allocation': False, |
| 124 |
'space_reserve': False, |
| 125 |
} |
| 126 |
assert call_main(my_main, DEFAULT_ARGS, module_args)['changed'] |
| 127 |
|
| 128 |
|
| 129 |
def test_create_rename_idempotency(): |
| 130 |
''' Test create idempotency ''' |
| 131 |
register_responses([ |
| 132 |
('ZAPI', 'lun-get-iter', ZRR['lun_info']), |
| 133 |
]) |
| 134 |
module_args = { |
| 135 |
'use_rest': 'never', |
| 136 |
'flexvol_name': 'vol_name', |
| 137 |
'size': 5, |
| 138 |
} |
| 139 |
assert not call_main(my_main, DEFAULT_ARGS, module_args)['changed'] |
| 140 |
|
| 141 |
|
| 142 |
def test_delete_lun(): |
| 143 |
''' Test delete and idempotency ''' |
| 144 |
register_responses([ |
| 145 |
('ZAPI', 'lun-get-iter', ZRR['lun_info']), |
| 146 |
('ZAPI', 'lun-destroy', ZRR['success']), |
| 147 |
# idempotency |
| 148 |
('ZAPI', 'lun-get-iter', ZRR['no_records']), |
| 149 |
]) |
| 150 |
module_args = { |
| 151 |
'use_rest': 'never', |
| 152 |
'flexvol_name': 'vol_name', |
| 153 |
'state': 'absent', |
| 154 |
} |
| 155 |
assert call_main(my_main, DEFAULT_ARGS, module_args)['changed'] |
| 156 |
module_args |
| 157 |
assert not call_main(my_main, DEFAULT_ARGS, module_args)['changed'] |
| 158 |
|
| 159 |
|
| 160 |
def test_delete_lun_no_input(): |
| 161 |
''' Nothing to delete! ''' |
| 162 |
register_responses([ |
| 163 |
]) |
| 164 |
module_args = { |
| 165 |
'use_rest': 'never', |
| 166 |
'state': 'absent', |
| 167 |
} |
| 168 |
msg = "Error: 'flexvol_name' option is required when using ZAPI." |
| 169 |
assert msg == call_main(my_main, DEFAULT_ARGS, module_args, fail=True)['msg'] |
| 170 |
|
| 171 |
|
| 172 |
def test_successful_resize(): |
| 173 |
''' Test successful resize ''' |
| 174 |
register_responses([ |
| 175 |
('ZAPI', 'lun-get-iter', ZRR['lun_info']), |
| 176 |
('ZAPI', 'lun-resize', ZRR['success']), |
| 177 |
('ZAPI', 'lun-get-iter', ZRR['lun_info']), |
| 178 |
('ZAPI', 'lun-resize', ZRR['error_9042']), |
| 179 |
]) |
| 180 |
module_args = { |
| 181 |
'use_rest': 'never', |
| 182 |
'flexvol_name': 'vol_name', |
| 183 |
'size': 7 |
| 184 |
} |
| 185 |
assert call_main(my_main, DEFAULT_ARGS, module_args)['changed'] |
| 186 |
assert not call_main(my_main, DEFAULT_ARGS, module_args)['changed'] |
| 187 |
|
| 188 |
|
| 189 |
def test_successful_modify(): |
| 190 |
''' Test successful modify ''' |
| 191 |
register_responses([ |
| 192 |
('ZAPI', 'lun-get-iter', ZRR['lun_info']), |
| 193 |
('ZAPI', 'lun-set-comment', ZRR['success']), |
| 194 |
('ZAPI', 'lun-set-qos-policy-group', ZRR['success']), |
| 195 |
('ZAPI', 'lun-set-space-alloc', ZRR['success']), |
| 196 |
# second call |
| 197 |
('ZAPI', 'lun-get-iter', ZRR['lun_info']), |
| 198 |
('ZAPI', 'lun-set-comment', ZRR['success']), |
| 199 |
('ZAPI', 'lun-set-qos-policy-group', ZRR['success']), |
| 200 |
('ZAPI', 'lun-set-space-reservation-info', ZRR['success']), |
| 201 |
]) |
| 202 |
module_args = { |
| 203 |
'use_rest': 'never', |
| 204 |
'comment': 'some comment', |
| 205 |
'flexvol_name': 'vol_name', |
| 206 |
'qos_policy_group': 'new_pol', |
| 207 |
'space_allocation': True, |
| 208 |
} |
| 209 |
assert call_main(my_main, DEFAULT_ARGS, module_args)['changed'] |
| 210 |
module_args = { |
| 211 |
'use_rest': 'never', |
| 212 |
'comment': 'some comment', |
| 213 |
'flexvol_name': 'vol_name', |
| 214 |
'qos_adaptive_policy_group': 'new_adaptive_pol', |
| 215 |
'space_allocation': False, |
| 216 |
'space_reserve': False, |
| 217 |
} |
| 218 |
assert call_main(my_main, DEFAULT_ARGS, module_args)['changed'] |
| 219 |
|
| 220 |
|
| 221 |
def test_negative_modify(): |
| 222 |
''' Test successful modify ''' |
| 223 |
register_responses([ |
| 224 |
('ZAPI', 'lun-get-iter', ZRR['lun_info']), |
| 225 |
]) |
| 226 |
module_args = { |
| 227 |
'use_rest': 'never', |
| 228 |
'flexvol_name': 'vol_name', |
| 229 |
'comment': 'some comment', |
| 230 |
'os_type': 'windows', |
| 231 |
} |
| 232 |
assert call_main(my_main, DEFAULT_ARGS, module_args, fail=True)['msg'] == 'os_type cannot be modified: current: linux, desired: windows' |
| 233 |
|
| 234 |
|
| 235 |
def test_successful_rename(): |
| 236 |
''' Test successful create ''' |
| 237 |
register_responses([ |
| 238 |
('ZAPI', 'lun-get-iter', ZRR['no_records']), |
| 239 |
('ZAPI', 'lun-get-iter', ZRR['lun_info_from']), |
| 240 |
('ZAPI', 'lun-move', ZRR['success']), |
| 241 |
]) |
| 242 |
module_args = { |
| 243 |
'use_rest': 'never', |
| 244 |
'flexvol_name': 'vol_name', |
| 245 |
'from_name': 'lun_from_name' |
| 246 |
} |
| 247 |
assert call_main(my_main, DEFAULT_ARGS, module_args)['changed'] |
| 248 |
|
| 249 |
|
| 250 |
def test_failed_rename(): |
| 251 |
''' Test failed rename ''' |
| 252 |
register_responses([ |
| 253 |
('ZAPI', 'lun-get-iter', ZRR['no_records']), |
| 254 |
('ZAPI', 'lun-get-iter', ZRR['no_records']), |
| 255 |
]) |
| 256 |
module_args = { |
| 257 |
'use_rest': 'never', |
| 258 |
'flexvol_name': 'vol_name', |
| 259 |
'from_name': 'lun_from_name' |
| 260 |
} |
| 261 |
msg = 'Error renaming lun: lun_from_name does not exist' |
| 262 |
assert msg == call_main(my_main, DEFAULT_ARGS, module_args, fail=True)['msg'] |
| 263 |
|
| 264 |
|
| 265 |
def test_zapi_errors(): |
| 266 |
register_responses([ |
| 267 |
# get error |
| 268 |
('ZAPI', 'lun-get-iter', ZRR['error']), |
| 269 |
# error on next tag |
| 270 |
('ZAPI', 'lun-get-iter', ZRR['lun_info_with_tag']), |
| 271 |
('ZAPI', 'lun-get-iter', ZRR['lun_info_with_tag']), |
| 272 |
('ZAPI', 'lun-get-iter', ZRR['error']), |
| 273 |
# create error |
| 274 |
('ZAPI', 'lun-create-by-size', ZRR['error']), |
| 275 |
# resize error |
| 276 |
('ZAPI', 'lun-resize', ZRR['error']), |
| 277 |
# rename error |
| 278 |
('ZAPI', 'lun-move', ZRR['error']), |
| 279 |
# modify error |
| 280 |
('ZAPI', 'lun-set-space-reservation-info', ZRR['error']), |
| 281 |
# delete error |
| 282 |
('ZAPI', 'lun-destroy', ZRR['error']), |
| 283 |
]) |
| 284 |
module_args = { |
| 285 |
'use_rest': 'never', |
| 286 |
'flexvol_name': 'vol_name', |
| 287 |
} |
| 288 |
my_obj = create_module(my_module, DEFAULT_ARGS, module_args) |
| 289 |
msg = 'Error fetching luns for vol_name' |
| 290 |
assert zapi_error_message(msg) == expect_and_capture_ansible_exception(my_obj.get_luns, 'fail')['msg'] |
| 291 |
assert zapi_error_message(msg) == expect_and_capture_ansible_exception(my_obj.get_luns, 'fail')['msg'] |
| 292 |
|
| 293 |
my_obj.parameters['size'] = 123456 |
| 294 |
msg = 'Error provisioning lun lun_name of size 123456' |
| 295 |
assert zapi_error_message(msg) == expect_and_capture_ansible_exception(my_obj.create_lun, 'fail')['msg'] |
| 296 |
|
| 297 |
msg = 'Error resizing lun path' |
| 298 |
assert zapi_error_message(msg) == expect_and_capture_ansible_exception(my_obj.resize_lun, 'fail', 'path')['msg'] |
| 299 |
|
| 300 |
my_obj.parameters.pop('size') |
| 301 |
msg = 'Error moving lun old_path' |
| 302 |
assert zapi_error_message(msg) == expect_and_capture_ansible_exception(my_obj.rename_lun, 'fail', 'old_path', 'new_path')['msg'] |
| 303 |
|
| 304 |
msg = 'Error setting lun option space_reserve' |
| 305 |
assert zapi_error_message(msg) == expect_and_capture_ansible_exception(my_obj.modify_lun, 'fail', 'path', {'space_reserve': True})['msg'] |
| 306 |
|
| 307 |
msg = 'Error deleting lun path' |
| 308 |
assert zapi_error_message(msg) == expect_and_capture_ansible_exception(my_obj.delete_lun, 'fail', 'path')['msg'] |