| 1 |
# Copyright (c) 2018-2025 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.py - REST features ''' |
| 5 |
from __future__ import (absolute_import, division, print_function) |
| 6 |
__metaclass__ = type |
| 7 |
|
| 8 |
import os.path |
| 9 |
import pytest |
| 10 |
import sys |
| 11 |
import tempfile |
| 12 |
|
| 13 |
from ansible.module_utils import basic |
| 14 |
from ansible_collections.netapp.ontap.tests.unit.compat.mock import call, patch |
| 15 |
|
| 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, create_module, expect_and_capture_ansible_exception, patch_ansible, print_warnings |
| 19 |
from ansible_collections.netapp.ontap.tests.unit.framework.mock_rest_and_zapi_requests import patch_request_and_invoke, register_responses |
| 20 |
from ansible_collections.netapp.ontap.tests.unit.framework.rest_factory import rest_responses |
| 21 |
import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils |
| 22 |
|
| 23 |
if not netapp_utils.HAS_REQUESTS and sys.version_info < (2, 7): |
| 24 |
pytestmark = pytest.mark.skip('Skipping Unit Tests on 2.6 as requests is not available') |
| 25 |
|
| 26 |
VERSION = {'version': { |
| 27 |
'full': '9.8.45', |
| 28 |
'generation': 9, |
| 29 |
'major': 8, |
| 30 |
'minor': 45 |
| 31 |
}} |
| 32 |
|
| 33 |
SRR = rest_responses({ |
| 34 |
'vservers_with_admin': (200, { |
| 35 |
'records': [ |
| 36 |
{'vserver': 'vserver1', 'type': 'data '}, |
| 37 |
{'vserver': 'vserver2', 'type': 'data '}, |
| 38 |
{'vserver': 'cserver', 'type': 'admin'} |
| 39 |
]}, None), |
| 40 |
'vservers_without_admin': (200, { |
| 41 |
'records': [ |
| 42 |
{'vserver': 'vserver1', 'type': 'data '}, |
| 43 |
{'vserver': 'vserver2', 'type': 'data '}, |
| 44 |
]}, None), |
| 45 |
'vservers_single': (200, { |
| 46 |
'records': [ |
| 47 |
{'vserver': 'single', 'type': 'data '}, |
| 48 |
]}, None), |
| 49 |
'vservers_empty': (200, {}, None), |
| 50 |
'vservers_error': (200, { |
| 51 |
'records': [ |
| 52 |
{'vserver': 'single', 'type': 'data '}, |
| 53 |
]}, 'some error'), |
| 54 |
'nodes': (200, { |
| 55 |
'records': [ |
| 56 |
VERSION, |
| 57 |
{'node': 'node2', 'version': 'version'}, |
| 58 |
]}, None), |
| 59 |
'precluster_error': (400, {}, {'message': 'are available in precluster.'}), |
| 60 |
}) |
| 61 |
|
| 62 |
DEFAULT_ARGS = { |
| 63 |
'hostname': 'test', |
| 64 |
'username': 'test_user', |
| 65 |
'password': 'test_pass!', |
| 66 |
'cert_filepath': None, |
| 67 |
'key_filepath': None, |
| 68 |
} |
| 69 |
|
| 70 |
CERT_ARGS = { |
| 71 |
'hostname': 'test', |
| 72 |
'cert_filepath': 'test_pem.pem', |
| 73 |
'key_filepath': 'test_key.key' |
| 74 |
} |
| 75 |
|
| 76 |
|
| 77 |
class MockONTAPModule: |
| 78 |
def __init__(self): |
| 79 |
self.module = basic.AnsibleModule(netapp_utils.na_ontap_host_argument_spec()) |
| 80 |
|
| 81 |
|
| 82 |
def create_restapi_object(default_args, module_args=None): |
| 83 |
module = create_module(MockONTAPModule, default_args, module_args) |
| 84 |
return netapp_utils.OntapRestAPI(module.module) |
| 85 |
|
| 86 |
|
| 87 |
def test_write_to_file(): |
| 88 |
''' check error and debug logs can be written to disk ''' |
| 89 |
rest_api = create_restapi_object(DEFAULT_ARGS) |
| 90 |
# logging an error also add a debug record |
| 91 |
rest_api.log_error(404, '404 error') |
| 92 |
print(rest_api.errors) |
| 93 |
print(rest_api.debug_logs) |
| 94 |
# logging a debug record only |
| 95 |
rest_api.log_debug(501, '501 error') |
| 96 |
print(rest_api.errors) |
| 97 |
print(rest_api.debug_logs) |
| 98 |
|
| 99 |
try: |
| 100 |
tempdir = tempfile.TemporaryDirectory() |
| 101 |
filepath = os.path.join(tempdir.name, 'log.txt') |
| 102 |
except AttributeError: |
| 103 |
# python 2.7 does not support tempfile.TemporaryDirectory |
| 104 |
# we're taking a small chance that there is a race condition |
| 105 |
filepath = '/tmp/deleteme354.txt' |
| 106 |
rest_api.write_debug_log_to_file(filepath=filepath, append=False) |
| 107 |
with open(filepath, 'r') as log: |
| 108 |
lines = log.readlines() |
| 109 |
assert len(lines) == 4 |
| 110 |
assert lines[0].strip() == 'Debug: 404' |
| 111 |
assert lines[2].strip() == 'Debug: 501' |
| 112 |
|
| 113 |
# Idempotent, as append is False |
| 114 |
rest_api.write_debug_log_to_file(filepath=filepath, append=False) |
| 115 |
with open(filepath, 'r') as log: |
| 116 |
lines = log.readlines() |
| 117 |
assert len(lines) == 4 |
| 118 |
assert lines[0].strip() == 'Debug: 404' |
| 119 |
assert lines[2].strip() == 'Debug: 501' |
| 120 |
|
| 121 |
# Duplication, as append is True |
| 122 |
rest_api.write_debug_log_to_file(filepath=filepath, append=True) |
| 123 |
with open(filepath, 'r') as log: |
| 124 |
lines = log.readlines() |
| 125 |
assert len(lines) == 8 |
| 126 |
assert lines[0].strip() == 'Debug: 404' |
| 127 |
assert lines[2].strip() == 'Debug: 501' |
| 128 |
assert lines[4].strip() == 'Debug: 404' |
| 129 |
assert lines[6].strip() == 'Debug: 501' |
| 130 |
|
| 131 |
rest_api.write_errors_to_file(filepath=filepath, append=False) |
| 132 |
with open(filepath, 'r') as log: |
| 133 |
lines = log.readlines() |
| 134 |
assert len(lines) == 1 |
| 135 |
assert lines[0].strip() == 'Error: 404 error' |
| 136 |
|
| 137 |
# Idempotent, as append is False |
| 138 |
rest_api.write_errors_to_file(filepath=filepath, append=False) |
| 139 |
with open(filepath, 'r') as log: |
| 140 |
lines = log.readlines() |
| 141 |
assert len(lines) == 1 |
| 142 |
assert lines[0].strip() == 'Error: 404 error' |
| 143 |
|
| 144 |
# Duplication, as append is True |
| 145 |
rest_api.write_errors_to_file(filepath=filepath, append=True) |
| 146 |
with open(filepath, 'r') as log: |
| 147 |
lines = log.readlines() |
| 148 |
assert len(lines) == 2 |
| 149 |
assert lines[0].strip() == 'Error: 404 error' |
| 150 |
assert lines[1].strip() == 'Error: 404 error' |
| 151 |
|
| 152 |
# Empty data |
| 153 |
rest_api.write_to_file(tag='atag', filepath=filepath, append=False) |
| 154 |
with open(filepath, 'r') as log: |
| 155 |
lines = log.readlines() |
| 156 |
assert len(lines) == 1 |
| 157 |
assert lines[0].strip() == 'atag' |
| 158 |
|
| 159 |
builtins = 'builtins' if sys.version_info > (3, 0) else '__builtin__' |
| 160 |
|
| 161 |
with patch('%s.open' % builtins) as mock_open: |
| 162 |
mock_open.side_effect = KeyError('Open error') |
| 163 |
exc = expect_and_capture_ansible_exception(rest_api.write_to_file, KeyError, tag='atag') |
| 164 |
assert str(exc) == 'Open error' |
| 165 |
print(mock_open.mock_calls) |
| 166 |
assert call('/tmp/ontap_log', 'a') in mock_open.mock_calls |
| 167 |
|
| 168 |
|
| 169 |
def test_is_rest_true(): |
| 170 |
''' is_rest is expected to return True ''' |
| 171 |
register_responses([ |
| 172 |
('GET', 'cluster', SRR['is_rest']), |
| 173 |
]) |
| 174 |
rest_api = create_restapi_object(DEFAULT_ARGS) |
| 175 |
is_rest = rest_api.is_rest() |
| 176 |
print(rest_api.errors) |
| 177 |
print(rest_api.debug_logs) |
| 178 |
assert is_rest |
| 179 |
|
| 180 |
|
| 181 |
def test_is_rest_false(): |
| 182 |
''' is_rest is expected to return False ''' |
| 183 |
register_responses([ |
| 184 |
('GET', 'cluster', SRR['is_zapi']), |
| 185 |
]) |
| 186 |
args = {'use_rest': 'auto'} |
| 187 |
rest_api = create_restapi_object(DEFAULT_ARGS, args) |
| 188 |
is_rest = rest_api.is_rest() |
| 189 |
print(rest_api.errors) |
| 190 |
print(rest_api.debug_logs) |
| 191 |
assert not is_rest |
| 192 |
assert rest_api.errors[0] == SRR['is_zapi'][2] |
| 193 |
assert rest_api.debug_logs[0][0] == SRR['is_zapi'][0] # status_code |
| 194 |
assert rest_api.debug_logs[0][1] == SRR['is_zapi'][2] # error |
| 195 |
|
| 196 |
|
| 197 |
def test_is_rest_false_9_5(): |
| 198 |
''' is_rest is expected to return False ''' |
| 199 |
register_responses([ |
| 200 |
('GET', 'cluster', SRR['is_rest_95']), |
| 201 |
]) |
| 202 |
args = {'use_rest': 'auto'} |
| 203 |
rest_api = create_restapi_object(DEFAULT_ARGS, args) |
| 204 |
is_rest = rest_api.is_rest() |
| 205 |
print(rest_api.errors) |
| 206 |
print(rest_api.debug_logs) |
| 207 |
assert not is_rest |
| 208 |
assert not rest_api.errors |
| 209 |
assert not rest_api.debug_logs |
| 210 |
|
| 211 |
|
| 212 |
def test_is_rest_true_9_6(): |
| 213 |
''' is_rest is expected to return False ''' |
| 214 |
register_responses([ |
| 215 |
('GET', 'cluster', SRR['is_rest_96']), |
| 216 |
]) |
| 217 |
rest_api = create_restapi_object(DEFAULT_ARGS) |
| 218 |
is_rest = rest_api.is_rest() |
| 219 |
print(rest_api.errors) |
| 220 |
print(rest_api.debug_logs) |
| 221 |
assert is_rest |
| 222 |
assert not rest_api.errors |
| 223 |
assert not rest_api.debug_logs |
| 224 |
|
| 225 |
|
| 226 |
def test_fail_has_password_and_cert(): |
| 227 |
''' failure case in auth_method ''' |
| 228 |
args = dict(DEFAULT_ARGS) |
| 229 |
del args['username'] |
| 230 |
msg = 'Error: username and password have to be provided together' |
| 231 |
assert expect_and_capture_ansible_exception(create_restapi_object, 'fail', args)['msg'] == msg |
| 232 |
|
| 233 |
|
| 234 |
def test_has_username_password(): |
| 235 |
''' auth_method reports expected value ''' |
| 236 |
rest_api = create_restapi_object(DEFAULT_ARGS) |
| 237 |
assert rest_api.auth_method == 'speedy_basic_auth' |
| 238 |
|
| 239 |
|
| 240 |
def test_has_cert_no_key(): |
| 241 |
''' auth_method reports expected value ''' |
| 242 |
args = dict(CERT_ARGS) |
| 243 |
del args['key_filepath'] |
| 244 |
rest_api = create_restapi_object(args) |
| 245 |
assert rest_api.auth_method == 'single_cert' |
| 246 |
|
| 247 |
|
| 248 |
def test_has_cert_and_key(): |
| 249 |
''' auth_method reports expected value ''' |
| 250 |
rest_api = create_restapi_object(CERT_ARGS) |
| 251 |
assert rest_api.auth_method == 'cert_key' |
| 252 |
|
| 253 |
|
| 254 |
def test_get_cserver(): |
| 255 |
''' using REST to get cserver - not sure if it's needed ''' |
| 256 |
register_responses([ |
| 257 |
('GET', 'private/cli/vserver', SRR['vservers_with_admin']), |
| 258 |
('GET', 'private/cli/vserver', SRR['vservers_without_admin']), |
| 259 |
('GET', 'private/cli/vserver', SRR['vservers_single']), |
| 260 |
('GET', 'private/cli/vserver', SRR['vservers_empty']), |
| 261 |
('GET', 'private/cli/vserver', SRR['vservers_error']), |
| 262 |
]) |
| 263 |
rest_api = create_restapi_object(DEFAULT_ARGS) |
| 264 |
assert netapp_utils.get_cserver(rest_api, is_rest=True) == 'cserver' |
| 265 |
assert netapp_utils.get_cserver(rest_api, is_rest=True) is None |
| 266 |
assert netapp_utils.get_cserver(rest_api, is_rest=True) == 'single' |
| 267 |
assert netapp_utils.get_cserver(rest_api, is_rest=True) is None |
| 268 |
assert netapp_utils.get_cserver(rest_api, is_rest=True) is None |
| 269 |
|
| 270 |
|
| 271 |
def test_ontaprestapi_init(): |
| 272 |
module_args = {'http_port': 123} |
| 273 |
rest_api = create_restapi_object(DEFAULT_ARGS) |
| 274 |
assert rest_api.url == 'https://%s/api/' % DEFAULT_ARGS['hostname'] |
| 275 |
rest_api = create_restapi_object(DEFAULT_ARGS, module_args) |
| 276 |
assert rest_api.url == 'https://%s:%d/api/' % (DEFAULT_ARGS['hostname'], module_args['http_port']) |
| 277 |
|
| 278 |
|
| 279 |
@patch('logging.basicConfig') |
| 280 |
def test_ontaprestapi_logging(mock_config): |
| 281 |
create_restapi_object(DEFAULT_ARGS) |
| 282 |
assert not mock_config.mock_calls |
| 283 |
module_args = {'feature_flags': {'trace_apis': True}} |
| 284 |
create_restapi_object(DEFAULT_ARGS, module_args) |
| 285 |
assert len(mock_config.mock_calls) == 1 |
| 286 |
|
| 287 |
|
| 288 |
def test_requires_ontap_9_6(): |
| 289 |
rest_api = create_restapi_object(DEFAULT_ARGS) |
| 290 |
assert rest_api.requires_ontap_9_6('module_name') == 'module_name only supports REST, and requires ONTAP 9.6 or later.' |
| 291 |
|
| 292 |
|
| 293 |
def test_requires_ontap_version(): |
| 294 |
rest_api = create_restapi_object(DEFAULT_ARGS) |
| 295 |
assert rest_api.requires_ontap_version('module_name', '9.1.2') == 'module_name only supports REST, and requires ONTAP 9.1.2 or later.' |
| 296 |
|
| 297 |
|
| 298 |
def test_options_require_ontap_version(): |
| 299 |
rest_api = create_restapi_object(DEFAULT_ARGS) |
| 300 |
base = 'using %s requires ONTAP 9.1.2 or later and REST must be enabled' |
| 301 |
msg = base % 'option_name' |
| 302 |
msg_m = base % "any of ['op1', 'op2', 'op3']" |
| 303 |
assert rest_api.options_require_ontap_version('option_name', '9.1.2') == '%s.' % msg |
| 304 |
assert rest_api.options_require_ontap_version('option_name', '9.1.2', use_rest=True) == '%s - using REST.' % msg |
| 305 |
assert rest_api.options_require_ontap_version('option_name', '9.1.2', use_rest=False) == '%s - using ZAPI.' % msg |
| 306 |
assert rest_api.options_require_ontap_version(['op1', 'op2', 'op3'], '9.1.2') == '%s.' % msg_m |
| 307 |
rest_api.set_version(VERSION) |
| 308 |
assert rest_api.options_require_ontap_version(['option_name'], '9.1.2') == '%s - ONTAP version: %s.' % (msg, VERSION['version']['full']) |
| 309 |
assert rest_api.options_require_ontap_version(['op1', 'op2', 'op3'], '9.1.2', use_rest=True) ==\ |
| 310 |
'%s - ONTAP version: %s - using REST.' % (msg_m, VERSION['version']['full']) |
| 311 |
|
| 312 |
|
| 313 |
def test_meets_rest_minimum_version(): |
| 314 |
rest_api = create_restapi_object(DEFAULT_ARGS) |
| 315 |
rest_api.set_version(VERSION) |
| 316 |
assert rest_api.meets_rest_minimum_version(True, VERSION['version']['generation'], VERSION['version']['major']) |
| 317 |
assert rest_api.meets_rest_minimum_version(True, VERSION['version']['generation'], VERSION['version']['major'] - 1) |
| 318 |
assert not rest_api.meets_rest_minimum_version(True, VERSION['version']['generation'], VERSION['version']['major'] + 1) |
| 319 |
assert not rest_api.meets_rest_minimum_version(True, VERSION['version']['generation'], VERSION['version']['major'], VERSION['version']['minor'] + 1) |
| 320 |
|
| 321 |
|
| 322 |
def test_fail_if_not_rest_minimum_version(): |
| 323 |
register_responses([ |
| 324 |
('GET', 'cluster', SRR['is_rest_96']), |
| 325 |
('GET', 'cluster', SRR['is_rest_96']), |
| 326 |
('GET', 'cluster', SRR['generic_error']), |
| 327 |
('GET', 'cluster', SRR['is_rest_96']), |
| 328 |
('GET', 'cluster', SRR['is_rest_96']), |
| 329 |
]) |
| 330 |
rest_api = create_restapi_object(DEFAULT_ARGS) |
| 331 |
rest_api.use_rest = 'never' |
| 332 |
# validate consistency bug in fail_if_not_rest_minimum_version |
| 333 |
assert expect_and_capture_ansible_exception(rest_api.fail_if_not_rest_minimum_version, 'fail', 'module_name', 9, 6)['msg'] ==\ |
| 334 |
'Error: REST is required for this module, found: "use_rest: never".' |
| 335 |
# never |
| 336 |
rest_api = create_restapi_object(DEFAULT_ARGS, {'use_rest': 'never'}) |
| 337 |
assert expect_and_capture_ansible_exception(rest_api.fail_if_not_rest_minimum_version, 'fail', 'module_name', 9, 6)['msg'] ==\ |
| 338 |
'Error: REST is required for this module, found: "use_rest: never".' |
| 339 |
# REST error |
| 340 |
rest_api = create_restapi_object(DEFAULT_ARGS, {'use_rest': 'auto'}) |
| 341 |
assert expect_and_capture_ansible_exception(rest_api.fail_if_not_rest_minimum_version, 'fail', 'module_name', 9, 6)['msg'] ==\ |
| 342 |
'Error using REST for version, error: Expected error. Error using REST for version, status_code: 400.' |
| 343 |
# version mismatch |
| 344 |
assert expect_and_capture_ansible_exception(rest_api.fail_if_not_rest_minimum_version, 'fail', 'module_name', 9, 7)['msg'] ==\ |
| 345 |
'Error: module_name only supports REST, and requires ONTAP 9.7.0 or later. Found: 9.6.0.' |
| 346 |
# version match |
| 347 |
assert rest_api.fail_if_not_rest_minimum_version('module_name', 9, 6) is None |
| 348 |
|
| 349 |
|
| 350 |
def test_check_required_library(): |
| 351 |
rest_api = create_restapi_object(DEFAULT_ARGS) |
| 352 |
msg = 'Failed to import the required Python library (requests)' |
| 353 |
with patch('ansible_collections.netapp.ontap.plugins.module_utils.netapp.HAS_REQUESTS', False): |
| 354 |
assert expect_and_capture_ansible_exception(rest_api.check_required_library, 'fail')['msg'].startswith(msg) |
| 355 |
|
| 356 |
|
| 357 |
def test_build_headers(): |
| 358 |
rest_api = create_restapi_object(DEFAULT_ARGS) |
| 359 |
app_version = 'basic.py/%s' % netapp_utils.COLLECTION_VERSION |
| 360 |
assert rest_api.build_headers() == {'X-Dot-Client-App': app_version} |
| 361 |
assert rest_api.build_headers(accept='accept') == {'X-Dot-Client-App': app_version, 'accept': 'accept'} |
| 362 |
assert rest_api.build_headers(vserver_name='vserver_name') == {'X-Dot-Client-App': app_version, 'X-Dot-SVM-Name': 'vserver_name'} |
| 363 |
assert rest_api.build_headers(vserver_uuid='vserver_uuid') == {'X-Dot-Client-App': app_version, 'X-Dot-SVM-UUID': 'vserver_uuid'} |
| 364 |
assert len(rest_api.build_headers(accept='accept', vserver_name='name', vserver_uuid='uuid')) == 4 |
| 365 |
|
| 366 |
|
| 367 |
def test_get_method(): |
| 368 |
register_responses([ |
| 369 |
('GET', 'cluster', SRR['is_rest_96']), |
| 370 |
]) |
| 371 |
assert create_restapi_object(DEFAULT_ARGS).get('cluster') == (SRR['is_rest_96'][1], None) |
| 372 |
|
| 373 |
|
| 374 |
def test_post_method(): |
| 375 |
register_responses([ |
| 376 |
('POST', 'cluster', SRR['is_rest_96']), |
| 377 |
]) |
| 378 |
assert create_restapi_object(DEFAULT_ARGS).post('cluster', None) == (SRR['is_rest_96'][1], None) |
| 379 |
|
| 380 |
|
| 381 |
def test_patch_method(): |
| 382 |
register_responses([ |
| 383 |
('PATCH', 'cluster', SRR['is_rest_96']), |
| 384 |
]) |
| 385 |
assert create_restapi_object(DEFAULT_ARGS).patch('cluster', None) == (SRR['is_rest_96'][1], None) |
| 386 |
|
| 387 |
|
| 388 |
def test_delete_method(): |
| 389 |
register_responses([ |
| 390 |
('DELETE', 'cluster', SRR['is_rest_96']), |
| 391 |
]) |
| 392 |
assert create_restapi_object(DEFAULT_ARGS).delete('cluster', None) == (SRR['is_rest_96'][1], None) |
| 393 |
|
| 394 |
|
| 395 |
def test_options_method(): |
| 396 |
register_responses([ |
| 397 |
('OPTIONS', 'cluster', SRR['is_rest_96']), |
| 398 |
]) |
| 399 |
assert create_restapi_object(DEFAULT_ARGS).options('cluster', None) == (SRR['is_rest_96'][1], None) |
| 400 |
|
| 401 |
|
| 402 |
def test_get_node_version_using_rest(): |
| 403 |
register_responses([ |
| 404 |
('GET', 'cluster/nodes', SRR['nodes']), |
| 405 |
]) |
| 406 |
assert create_restapi_object(DEFAULT_ARGS).get_node_version_using_rest() == (200, SRR['nodes'][1]['records'][0], None) |
| 407 |
|
| 408 |
|
| 409 |
def test_get_ontap_version_using_rest(): |
| 410 |
register_responses([ |
| 411 |
('GET', 'cluster', SRR['precluster_error']), |
| 412 |
('GET', 'cluster/nodes', SRR['nodes']), |
| 413 |
]) |
| 414 |
rest_api = create_restapi_object(DEFAULT_ARGS) |
| 415 |
assert rest_api.get_ontap_version_using_rest() == 200 |
| 416 |
assert rest_api.ontap_version['major'] == VERSION['version']['major'] |
| 417 |
assert rest_api.ontap_version['valid'] |
| 418 |
|
| 419 |
|
| 420 |
def test__is_rest(): |
| 421 |
if not sys.version_info > (3, 0): |
| 422 |
return |
| 423 |
rest_api = create_restapi_object(DEFAULT_ARGS) |
| 424 |
rest_api.use_rest = 'invalid' |
| 425 |
msg = "use_rest must be one of: never, always, auto. Got: 'invalid'" |
| 426 |
assert rest_api._is_rest() == (False, msg) |
| 427 |
# testing always with used_unsupported_rest_properties |
| 428 |
rest_api.use_rest = 'always' |
| 429 |
msg = "REST API currently does not support 'xyz'" |
| 430 |
assert rest_api._is_rest(used_unsupported_rest_properties=['xyz']) == (True, msg) |
| 431 |
# testing never |
| 432 |
rest_api.use_rest = 'never' |
| 433 |
assert rest_api._is_rest() == (False, None) |
| 434 |
# we need the version |
| 435 |
register_responses([ |
| 436 |
('GET', 'cluster', SRR['is_rest_96']), |
| 437 |
]) |
| 438 |
# testing always unconditionnally and with partially_supported_rest_properties |
| 439 |
rest_api.use_rest = 'always' |
| 440 |
msg = 'Error: Minimum version of ONTAP for xyz is (9, 7). Current version: (9, 6, 0).' |
| 441 |
assert rest_api._is_rest(partially_supported_rest_properties=[('xyz', (9, 7))], parameters=['xyz']) == (True, msg) |
| 442 |
# No error when version requirement is matched |
| 443 |
assert rest_api._is_rest(partially_supported_rest_properties=[('xyz', (9, 6))], parameters=['xyz']) == (True, None) |
| 444 |
# No error when parameter is not used |
| 445 |
assert rest_api._is_rest(partially_supported_rest_properties=[('abc', (9, 6))], parameters=['xyz']) == (True, None) |
| 446 |
# testing auto with used_unsupported_rest_properties |
| 447 |
rest_api.use_rest = 'auto' |
| 448 |
assert rest_api._is_rest(used_unsupported_rest_properties=['xyz']) == (False, None) |
| 449 |
# TODO: check warning |
| 450 |
|
| 451 |
|
| 452 |
def test_is_rest_supported_properties(): |
| 453 |
rest_api = create_restapi_object(DEFAULT_ARGS) |
| 454 |
rest_api.use_rest = 'always' |
| 455 |
assert expect_and_capture_ansible_exception(rest_api.is_rest_supported_properties, 'fail', ['xyz'], ['xyz'])['msg'] ==\ |
| 456 |
"REST API currently does not support 'xyz'" |
| 457 |
register_responses([ |
| 458 |
('GET', 'cluster', SRR['is_rest_96']), |
| 459 |
]) |
| 460 |
assert rest_api.is_rest_supported_properties(['abc'], ['xyz']) |
| 461 |
assert rest_api.is_rest_supported_properties(['abc'], ['xyz'], report_error=True) == (True, None) |
| 462 |
|
| 463 |
|
| 464 |
def test_is_rest_partially_supported_properties(): |
| 465 |
if not sys.version_info > (3, 0): |
| 466 |
return |
| 467 |
register_responses([ |
| 468 |
('GET', 'cluster', SRR['is_rest_97']), |
| 469 |
('GET', 'cluster', SRR['is_rest_9_9_0']), |
| 470 |
]) |
| 471 |
rest_api = create_restapi_object(DEFAULT_ARGS) |
| 472 |
rest_api.use_rest = 'auto' |
| 473 |
assert not rest_api.is_rest_supported_properties(['xyz'], None, [('xyz', (9, 8, 1))]) |
| 474 |
assert_warning_was_raised('Falling back to ZAPI because of unsupported option(s) or option value(s) "xyz" in REST require (9, 8, 1)') |
| 475 |
rest_api = create_restapi_object(DEFAULT_ARGS) |
| 476 |
rest_api.use_rest = 'auto' |
| 477 |
assert rest_api.is_rest_supported_properties(['xyz'], None, [('xyz', (9, 8, 1))]) |
| 478 |
|
| 479 |
|
| 480 |
def test_is_rest(): |
| 481 |
rest_api = create_restapi_object(DEFAULT_ARGS) |
| 482 |
# testing always with used_unsupported_rest_properties |
| 483 |
rest_api.use_rest = 'always' |
| 484 |
msg = "REST API currently does not support 'xyz'" |
| 485 |
assert rest_api.is_rest(used_unsupported_rest_properties=['xyz']) == (True, msg) |
| 486 |
register_responses([ |
| 487 |
('GET', 'cluster', SRR['is_rest_96']), |
| 488 |
]) |
| 489 |
assert rest_api.is_rest() |
| 490 |
|
| 491 |
|
| 492 |
def test_set_version(): |
| 493 |
rest_api = create_restapi_object(DEFAULT_ARGS) |
| 494 |
rest_api.set_version(VERSION) |
| 495 |
print('VERSION', rest_api.ontap_version) |
| 496 |
assert rest_api.ontap_version['generation'] == VERSION['version']['generation'] |
| 497 |
assert rest_api.ontap_version['valid'] |
| 498 |
rest_api.set_version({}) |
| 499 |
assert not rest_api.ontap_version['valid'] |
| 500 |
|
| 501 |
|
| 502 |
def test_force_ontap_version_local(): |
| 503 |
""" test get_ontap_version_from_params in isolation """ |
| 504 |
rest_api = create_restapi_object(DEFAULT_ARGS) |
| 505 |
rest_api.set_version(VERSION) |
| 506 |
print('VERSION', rest_api.ontap_version) |
| 507 |
assert rest_api.ontap_version['generation'] == VERSION['version']['generation'] |
| 508 |
# same version |
| 509 |
rest_api.force_ontap_version = VERSION['version']['full'] |
| 510 |
assert not rest_api.get_ontap_version_from_params() |
| 511 |
# different versions |
| 512 |
rest_api.force_ontap_version = '10.8.1' |
| 513 |
warning = rest_api.get_ontap_version_from_params() |
| 514 |
assert rest_api.ontap_version['generation'] != VERSION['version']['generation'] |
| 515 |
assert rest_api.ontap_version['generation'] == 10 |
| 516 |
assert 'Forcing ONTAP version to 10.8.1 but current version is 9.8.45' in warning |
| 517 |
# version could not be read |
| 518 |
rest_api.set_version({}) |
| 519 |
rest_api.force_ontap_version = '10.8' |
| 520 |
warning = rest_api.get_ontap_version_from_params() |
| 521 |
assert rest_api.ontap_version['generation'] != VERSION['version']['generation'] |
| 522 |
assert rest_api.ontap_version['generation'] == 10 |
| 523 |
assert rest_api.ontap_version['minor'] == 0 |
| 524 |
assert 'Forcing ONTAP version to 10.8, unable to read current version:' in warning |
| 525 |
|
| 526 |
|
| 527 |
def test_negative_force_ontap_version_local(): |
| 528 |
""" test get_ontap_version_from_params in isolation """ |
| 529 |
rest_api = create_restapi_object(DEFAULT_ARGS) |
| 530 |
# non numeric |
| 531 |
rest_api.force_ontap_version = '9.8P4' |
| 532 |
error = 'Error: unexpected format in force_ontap_version, expecting G.M.m or G.M, as in 9.10.1, got: 9.8P4,' |
| 533 |
assert error in expect_and_capture_ansible_exception(rest_api.get_ontap_version_from_params, 'fail')['msg'] |
| 534 |
# too short |
| 535 |
rest_api.force_ontap_version = '9' |
| 536 |
error = 'Error: unexpected format in force_ontap_version, expecting G.M.m or G.M, as in 9.10.1, got: 9,' |
| 537 |
assert error in expect_and_capture_ansible_exception(rest_api.get_ontap_version_from_params, 'fail')['msg'] |
| 538 |
# too long |
| 539 |
rest_api.force_ontap_version = '9.1.2.3' |
| 540 |
error = 'Error: unexpected format in force_ontap_version, expecting G.M.m or G.M, as in 9.10.1, got: 9.1.2.3,' |
| 541 |
assert error in expect_and_capture_ansible_exception(rest_api.get_ontap_version_from_params, 'fail')['msg'] |
| 542 |
|
| 543 |
|
| 544 |
def test_force_ontap_version_rest_call(): |
| 545 |
""" test get_ontap_version_using_rest with force_ontap_version option """ |
| 546 |
register_responses([ |
| 547 |
('GET', 'cluster', SRR['is_rest_97']), |
| 548 |
('GET', 'cluster', SRR['is_rest_9_9_0']), |
| 549 |
('GET', 'cluster', SRR['generic_error']), |
| 550 |
]) |
| 551 |
rest_api = create_restapi_object(DEFAULT_ARGS) |
| 552 |
# same version |
| 553 |
rest_api.force_ontap_version = '9.7' |
| 554 |
assert rest_api.get_ontap_version_using_rest() == 200 |
| 555 |
assert_no_warnings() |
| 556 |
# different versions |
| 557 |
rest_api.force_ontap_version = '10.8.1' |
| 558 |
assert rest_api.get_ontap_version_using_rest() == 200 |
| 559 |
assert rest_api.ontap_version['generation'] == 10 |
| 560 |
assert_warning_was_raised('Forcing ONTAP version to 10.8.1 but current version is dummy_9_9_0') |
| 561 |
# version could not be read |
| 562 |
assert rest_api.get_ontap_version_using_rest() == 200 |
| 563 |
assert_warning_was_raised('Forcing ONTAP version to 10.8.1, unable to read current version: error: Expected error, status_code: 400') |
| 564 |
assert rest_api.ontap_version['generation'] == 10 |