| 1 |
# (c) 2022-2026, 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 Ansible module: na_ontap_iscsi ''' |
| 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 |
|
| 12 |
from ansible_collections.netapp.ontap.plugins.modules.na_ontap_iscsi \ |
| 13 |
import NetAppOntapISCSI as iscsi_module # module under test |
| 14 |
# pylint: disable=unused-import |
| 15 |
from ansible_collections.netapp.ontap.tests.unit.plugins.module_utils.ansible_mocks import set_module_args, \ |
| 16 |
AnsibleFailJson, patch_ansible, create_module, create_and_apply, expect_and_capture_ansible_exception |
| 17 |
from ansible_collections.netapp.ontap.tests.unit.framework.mock_rest_and_zapi_requests import patch_request_and_invoke, \ |
| 18 |
register_responses |
| 19 |
from ansible_collections.netapp.ontap.tests.unit.framework.rest_factory import rest_responses |
| 20 |
from ansible_collections.netapp.ontap.tests.unit.framework.zapi_factory import build_zapi_response, zapi_responses |
| 21 |
|
| 22 |
|
| 23 |
# if not netapp_utils.has_netapp_lib(): |
| 24 |
# pytestmark = pytest.mark.skip('skipping as missing required netapp_lib') |
| 25 |
|
| 26 |
if not netapp_utils.HAS_REQUESTS and sys.version_info < (2, 7): |
| 27 |
pytestmark = pytest.mark.skip('Skipping Unit Tests on 2.6 as requests is not available') |
| 28 |
|
| 29 |
|
| 30 |
DEFAULT_ARGS = { |
| 31 |
"hostname": "10.10.10.10", |
| 32 |
"username": "admin", |
| 33 |
"password": "netapp1!", |
| 34 |
"validate_certs": "no", |
| 35 |
"https": "yes", |
| 36 |
"state": "present", |
| 37 |
"use_rest": "never", |
| 38 |
"vserver": "svm1", |
| 39 |
"service_state": "started" |
| 40 |
} |
| 41 |
|
| 42 |
|
| 43 |
iscsi_info_started = { |
| 44 |
'num-records': 1, |
| 45 |
'attributes-list': { |
| 46 |
'iscsi-service-info': { |
| 47 |
'is-available': 'true', |
| 48 |
'vserver': 'svm1' |
| 49 |
} |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
iscsi_info_stopped = { |
| 54 |
'num-records': 1, |
| 55 |
'attributes-list': { |
| 56 |
'iscsi-service-info': { |
| 57 |
'is-available': 'false', |
| 58 |
'vserver': 'svm1' |
| 59 |
} |
| 60 |
} |
| 61 |
} |
| 62 |
|
| 63 |
ZRR = zapi_responses({ |
| 64 |
'iscsi_started': build_zapi_response(iscsi_info_started), |
| 65 |
'iscsi_stopped': build_zapi_response(iscsi_info_stopped) |
| 66 |
}) |
| 67 |
|
| 68 |
|
| 69 |
def test_module_fail_when_required_args_missing(): |
| 70 |
''' required arguments are reported as errors ''' |
| 71 |
with pytest.raises(AnsibleFailJson) as exc: |
| 72 |
set_module_args({}) |
| 73 |
iscsi_module() |
| 74 |
print('Info: %s' % exc.value.args[0]['msg']) |
| 75 |
|
| 76 |
|
| 77 |
@pytest.mark.skipif(not netapp_utils.has_netapp_lib(), reason="skipping as missing required netapp_lib") |
| 78 |
def test_get_nonexistent_iscsi(): |
| 79 |
register_responses([ |
| 80 |
('iscsi-service-get-iter', ZRR['empty']) |
| 81 |
]) |
| 82 |
iscsi_obj = create_module(iscsi_module, DEFAULT_ARGS) |
| 83 |
result = iscsi_obj.get_iscsi() |
| 84 |
assert not result |
| 85 |
|
| 86 |
|
| 87 |
@pytest.mark.skipif(not netapp_utils.has_netapp_lib(), reason="skipping as missing required netapp_lib") |
| 88 |
def test_get_existing_iscsi(): |
| 89 |
register_responses([ |
| 90 |
('iscsi-service-get-iter', ZRR['iscsi_started']) |
| 91 |
]) |
| 92 |
iscsi_obj = create_module(iscsi_module, DEFAULT_ARGS) |
| 93 |
result = iscsi_obj.get_iscsi() |
| 94 |
assert result |
| 95 |
|
| 96 |
|
| 97 |
@pytest.mark.skipif(not netapp_utils.has_netapp_lib(), reason="skipping as missing required netapp_lib") |
| 98 |
def test_successfully_create(): |
| 99 |
register_responses([ |
| 100 |
('iscsi-service-get-iter', ZRR['empty']), |
| 101 |
('iscsi-service-create', ZRR['success']) |
| 102 |
]) |
| 103 |
assert create_and_apply(iscsi_module, DEFAULT_ARGS)['changed'] |
| 104 |
|
| 105 |
|
| 106 |
@pytest.mark.skipif(not netapp_utils.has_netapp_lib(), reason="skipping as missing required netapp_lib") |
| 107 |
def test_create_idempotency(): |
| 108 |
register_responses([ |
| 109 |
('iscsi-service-get-iter', ZRR['iscsi_started']) |
| 110 |
]) |
| 111 |
assert create_and_apply(iscsi_module, DEFAULT_ARGS)['changed'] is False |
| 112 |
|
| 113 |
|
| 114 |
@pytest.mark.skipif(not netapp_utils.has_netapp_lib(), reason="skipping as missing required netapp_lib") |
| 115 |
def test_successfully_create_stop_service(): |
| 116 |
register_responses([ |
| 117 |
('iscsi-service-get-iter', ZRR['empty']), |
| 118 |
('iscsi-service-create', ZRR['success']) |
| 119 |
]) |
| 120 |
args = {'service_state': 'stopped'} |
| 121 |
assert create_and_apply(iscsi_module, DEFAULT_ARGS, args)['changed'] |
| 122 |
|
| 123 |
|
| 124 |
@pytest.mark.skipif(not netapp_utils.has_netapp_lib(), reason="skipping as missing required netapp_lib") |
| 125 |
def test_successfully_delete_when_service_started(): |
| 126 |
register_responses([ |
| 127 |
('iscsi-service-get-iter', ZRR['iscsi_started']), |
| 128 |
('iscsi-service-stop', ZRR['success']), |
| 129 |
('iscsi-service-destroy', ZRR['success']) |
| 130 |
]) |
| 131 |
args = {'state': 'absent'} |
| 132 |
assert create_and_apply(iscsi_module, DEFAULT_ARGS, args)['changed'] |
| 133 |
|
| 134 |
|
| 135 |
@pytest.mark.skipif(not netapp_utils.has_netapp_lib(), reason="skipping as missing required netapp_lib") |
| 136 |
def test_delete_idempotent(): |
| 137 |
register_responses([ |
| 138 |
('iscsi-service-get-iter', ZRR['empty']) |
| 139 |
]) |
| 140 |
args = {'state': 'absent'} |
| 141 |
assert create_and_apply(iscsi_module, DEFAULT_ARGS, args)['changed'] is False |
| 142 |
|
| 143 |
|
| 144 |
@pytest.mark.skipif(not netapp_utils.has_netapp_lib(), reason="skipping as missing required netapp_lib") |
| 145 |
def test_start_iscsi(): |
| 146 |
register_responses([ |
| 147 |
('iscsi-service-get-iter', ZRR['iscsi_stopped']), |
| 148 |
('iscsi-service-start', ZRR['success']) |
| 149 |
]) |
| 150 |
assert create_and_apply(iscsi_module, DEFAULT_ARGS)['changed'] |
| 151 |
|
| 152 |
|
| 153 |
@pytest.mark.skipif(not netapp_utils.has_netapp_lib(), reason="skipping as missing required netapp_lib") |
| 154 |
def test_stop_iscsi(): |
| 155 |
register_responses([ |
| 156 |
('iscsi-service-get-iter', ZRR['iscsi_started']), |
| 157 |
('iscsi-service-stop', ZRR['success']) |
| 158 |
]) |
| 159 |
args = {'service_state': 'stopped'} |
| 160 |
assert create_and_apply(iscsi_module, DEFAULT_ARGS, args)['changed'] |
| 161 |
|
| 162 |
|
| 163 |
@pytest.mark.skipif(not netapp_utils.has_netapp_lib(), reason="skipping as missing required netapp_lib") |
| 164 |
def test_if_all_methods_catch_exception(): |
| 165 |
register_responses([ |
| 166 |
('iscsi-service-get-iter', ZRR['error']), |
| 167 |
('iscsi-service-create', ZRR['error']), |
| 168 |
('iscsi-service-start', ZRR['error']), |
| 169 |
('iscsi-service-stop', ZRR['error']), |
| 170 |
('iscsi-service-destroy', ZRR['error']) |
| 171 |
]) |
| 172 |
|
| 173 |
iscsi_obj = create_module(iscsi_module, DEFAULT_ARGS) |
| 174 |
|
| 175 |
error = expect_and_capture_ansible_exception(iscsi_obj.get_iscsi, 'fail')['msg'] |
| 176 |
assert 'Error finding iscsi service in svm1: NetApp API failed. Reason - 12345:synthetic error for UT purpose' in error |
| 177 |
|
| 178 |
error = expect_and_capture_ansible_exception(iscsi_obj.create_iscsi_service, 'fail')['msg'] |
| 179 |
assert 'Error creating iscsi service: NetApp API failed. Reason - 12345:synthetic error for UT purpose' in error |
| 180 |
|
| 181 |
error = expect_and_capture_ansible_exception(iscsi_obj.start_iscsi_service, 'fail')['msg'] |
| 182 |
assert 'Error starting iscsi service on vserver svm1: NetApp API failed. Reason - 12345:synthetic error for UT purpose' in error |
| 183 |
|
| 184 |
error = expect_and_capture_ansible_exception(iscsi_obj.stop_iscsi_service, 'fail')['msg'] |
| 185 |
assert 'Error Stopping iscsi service on vserver svm1: NetApp API failed. Reason - 12345:synthetic error for UT purpose' in error |
| 186 |
|
| 187 |
error = expect_and_capture_ansible_exception(iscsi_obj.delete_iscsi_service, 'fail', {'service_state': 'stopped'})['msg'] |
| 188 |
assert 'Error deleting iscsi service on vserver svm1: NetApp API failed. Reason - 12345:synthetic error for UT purpose' in error |
| 189 |
|
| 190 |
|
| 191 |
SRR = rest_responses({ |
| 192 |
'iscsi_started': (200, {"records": [ |
| 193 |
{ |
| 194 |
"svm": {"uuid": "d08434fae1-a8a8-11fg-aa26-005055fhs3e5"}, |
| 195 |
"enabled": True, |
| 196 |
'target': {'alias': 'ansibleSVM'} |
| 197 |
} |
| 198 |
], "num_records": 1}, None), |
| 199 |
'iscsi_record': (200, {"records": [ |
| 200 |
{ |
| 201 |
"svm": {"uuid": "d08434fae1-a8a8-11fg-aa26-005055fhs3e5"}, |
| 202 |
"enabled": True, |
| 203 |
'target': {'alias': 'ansibleSVM'} |
| 204 |
} |
| 205 |
], "num_records": 1}, None), |
| 206 |
'iscsi_stopped': (200, {"records": [ |
| 207 |
{ |
| 208 |
"svm": {"uuid": "d08434fae1-a8a8-11fg-aa26-005055fhs3e5"}, |
| 209 |
"enabled": False, |
| 210 |
'target': {'alias': 'ansibleSVM'} |
| 211 |
} |
| 212 |
], "num_records": 1}, None), |
| 213 |
}) |
| 214 |
|
| 215 |
|
| 216 |
ARGS_REST = { |
| 217 |
"hostname": "10.10.10.10", |
| 218 |
"username": "admin", |
| 219 |
"password": "netapp1!", |
| 220 |
"validate_certs": "no", |
| 221 |
"https": "yes", |
| 222 |
"state": "present", |
| 223 |
"use_rest": "always", |
| 224 |
"vserver": "svm1", |
| 225 |
"service_state": "started", |
| 226 |
"target_alias": "ansibleSVM" |
| 227 |
} |
| 228 |
|
| 229 |
|
| 230 |
def test_successfully_create_rest(): |
| 231 |
register_responses([ |
| 232 |
('GET', 'cluster', SRR['is_rest_9_9_0']), |
| 233 |
('GET', 'protocols/san/iscsi/services', SRR['empty_records']), |
| 234 |
('POST', 'protocols/san/iscsi/services', SRR['success']) |
| 235 |
]) |
| 236 |
assert create_and_apply(iscsi_module, ARGS_REST, {'use_rest': 'always'})['changed'] |
| 237 |
|
| 238 |
|
| 239 |
def test_create_idempotency_rest(): |
| 240 |
register_responses([ |
| 241 |
('GET', 'cluster', SRR['is_rest_9_9_0']), |
| 242 |
('GET', 'protocols/san/iscsi/services', SRR['iscsi_started']), |
| 243 |
]) |
| 244 |
assert create_and_apply(iscsi_module, ARGS_REST, {'use_rest': 'always'})['changed'] is False |
| 245 |
|
| 246 |
|
| 247 |
def test_successfully_create_stop_service_rest(): |
| 248 |
register_responses([ |
| 249 |
('GET', 'cluster', SRR['is_rest_9_9_0']), |
| 250 |
('GET', 'protocols/san/iscsi/services', SRR['empty_records']), |
| 251 |
('POST', 'protocols/san/iscsi/services', SRR['success']) |
| 252 |
]) |
| 253 |
args = {'service_state': 'stopped'} |
| 254 |
assert create_and_apply(iscsi_module, ARGS_REST, args)['changed'] |
| 255 |
|
| 256 |
|
| 257 |
def test_successfully_delete_when_service_started_rest(): |
| 258 |
register_responses([ |
| 259 |
('GET', 'cluster', SRR['is_rest_9_9_0']), |
| 260 |
('GET', 'protocols/san/iscsi/services', SRR['iscsi_started']), |
| 261 |
('PATCH', 'protocols/san/iscsi/services/d08434fae1-a8a8-11fg-aa26-005055fhs3e5', SRR['success']), |
| 262 |
('DELETE', 'protocols/san/iscsi/services/d08434fae1-a8a8-11fg-aa26-005055fhs3e5', SRR['success']), |
| 263 |
]) |
| 264 |
args = {'state': 'absent'} |
| 265 |
assert create_and_apply(iscsi_module, ARGS_REST, args)['changed'] |
| 266 |
|
| 267 |
|
| 268 |
def test_delete_idempotent_rest(): |
| 269 |
register_responses([ |
| 270 |
('GET', 'cluster', SRR['is_rest_9_9_0']), |
| 271 |
('GET', 'protocols/san/iscsi/services', SRR['empty_records']), |
| 272 |
]) |
| 273 |
args = {'state': 'absent'} |
| 274 |
assert create_and_apply(iscsi_module, ARGS_REST, args)['changed'] is False |
| 275 |
|
| 276 |
|
| 277 |
def test_start_iscsi_rest(): |
| 278 |
register_responses([ |
| 279 |
('GET', 'cluster', SRR['is_rest_9_9_0']), |
| 280 |
('GET', 'protocols/san/iscsi/services', SRR['iscsi_stopped']), |
| 281 |
('PATCH', 'protocols/san/iscsi/services/d08434fae1-a8a8-11fg-aa26-005055fhs3e5', SRR['success']), |
| 282 |
]) |
| 283 |
args = {'service_state': 'started'} |
| 284 |
assert create_and_apply(iscsi_module, ARGS_REST, args)['changed'] |
| 285 |
|
| 286 |
|
| 287 |
def test_modify_iscsi_target_alias_rest(): |
| 288 |
register_responses([ |
| 289 |
('GET', 'cluster', SRR['is_rest_9_9_0']), |
| 290 |
('GET', 'protocols/san/iscsi/services', SRR['iscsi_started']), |
| 291 |
('PATCH', 'protocols/san/iscsi/services/d08434fae1-a8a8-11fg-aa26-005055fhs3e5', SRR['success']), |
| 292 |
]) |
| 293 |
args = {"target_alias": "ansibleSVM_test"} |
| 294 |
assert create_and_apply(iscsi_module, ARGS_REST, args)['changed'] |
| 295 |
|
| 296 |
|
| 297 |
def test_modify_iscsi_target_alias_and_state_rest(): |
| 298 |
register_responses([ |
| 299 |
('GET', 'cluster', SRR['is_rest_9_9_0']), |
| 300 |
('GET', 'protocols/san/iscsi/services', SRR['iscsi_stopped']), |
| 301 |
('PATCH', 'protocols/san/iscsi/services/d08434fae1-a8a8-11fg-aa26-005055fhs3e5', SRR['success']), |
| 302 |
('PATCH', 'protocols/san/iscsi/services/d08434fae1-a8a8-11fg-aa26-005055fhs3e5', SRR['success']), |
| 303 |
]) |
| 304 |
args = {"target_alias": "ansibleSVM_test", 'service_state': 'started'} |
| 305 |
assert create_and_apply(iscsi_module, ARGS_REST, args)['changed'] |
| 306 |
|
| 307 |
|
| 308 |
def test_stop_iscsi_rest(): |
| 309 |
register_responses([ |
| 310 |
('GET', 'cluster', SRR['is_rest_9_9_0']), |
| 311 |
('GET', 'protocols/san/iscsi/services', SRR['iscsi_started']), |
| 312 |
('PATCH', 'protocols/san/iscsi/services/d08434fae1-a8a8-11fg-aa26-005055fhs3e5', SRR['success']), |
| 313 |
]) |
| 314 |
args = {'service_state': 'stopped'} |
| 315 |
assert create_and_apply(iscsi_module, ARGS_REST, args)['changed'] |
| 316 |
|
| 317 |
|
| 318 |
def test_if_all_methods_catch_exception_rest(): |
| 319 |
register_responses([ |
| 320 |
('GET', 'cluster', SRR['is_rest_9_9_0']), |
| 321 |
('GET', 'protocols/san/iscsi/services', SRR['generic_error']), |
| 322 |
('POST', 'protocols/san/iscsi/services', SRR['generic_error']), |
| 323 |
('PATCH', 'protocols/san/iscsi/services/d08434fae1-a8a8-11fg-aa26-005055fhs3e5', SRR['generic_error']), |
| 324 |
('PATCH', 'protocols/san/iscsi/services/d08434fae1-a8a8-11fg-aa26-005055fhs3e5', SRR['generic_error']), |
| 325 |
('DELETE', 'protocols/san/iscsi/services/d08434fae1-a8a8-11fg-aa26-005055fhs3e5', SRR['generic_error']) |
| 326 |
]) |
| 327 |
|
| 328 |
iscsi_obj = create_module(iscsi_module, ARGS_REST, {'use_rest': 'always'}) |
| 329 |
iscsi_obj.uuid = "d08434fae1-a8a8-11fg-aa26-005055fhs3e5" |
| 330 |
|
| 331 |
error = expect_and_capture_ansible_exception(iscsi_obj.get_iscsi_rest, 'fail')['msg'] |
| 332 |
msg = 'Error finding iscsi service in svm1: calling: protocols/san/iscsi/services: got Expected error.' |
| 333 |
assert msg in error |
| 334 |
|
| 335 |
error = expect_and_capture_ansible_exception(iscsi_obj.create_iscsi_service_rest, 'fail')['msg'] |
| 336 |
msg = 'Error creating iscsi service: calling: protocols/san/iscsi/services: got Expected error.' |
| 337 |
assert msg in error |
| 338 |
|
| 339 |
error = expect_and_capture_ansible_exception(iscsi_obj.start_or_stop_iscsi_service_rest, 'fail', 'started')['msg'] |
| 340 |
msg = 'Error starting iscsi service on vserver svm1: calling: protocols/san/iscsi/services/d08434fae1-a8a8-11fg-aa26-005055fhs3e5: got Expected error.' |
| 341 |
assert msg in error |
| 342 |
|
| 343 |
error = expect_and_capture_ansible_exception(iscsi_obj.start_or_stop_iscsi_service_rest, 'fail', 'stopped')['msg'] |
| 344 |
msg = 'Error stopping iscsi service on vserver svm1: calling: protocols/san/iscsi/services/d08434fae1-a8a8-11fg-aa26-005055fhs3e5: got Expected error.' |
| 345 |
assert msg in error |
| 346 |
|
| 347 |
error = expect_and_capture_ansible_exception(iscsi_obj.delete_iscsi_service_rest, 'fail', {'service_state': 'stopped'})['msg'] |
| 348 |
msg = 'Error deleting iscsi service on vserver svm1: calling: protocols/san/iscsi/services/d08434fae1-a8a8-11fg-aa26-005055fhs3e5: got Expected error.' |
| 349 |
assert msg in error |