Debian Codegraph @

ansible_collections/netapp/ontap/tests/unit/plugins/modules/test_na_ontap_quotas.py

raw
1 # (c) 2019-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 ONTAP Ansible module: na_ontap_quotas '''
5
6 from __future__ import (absolute_import, division, print_function)
7 __metaclass__ = type
8
9 import pytest
10
11 from ansible_collections.netapp.ontap.tests.unit.compat.mock import patch
12 import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils
13 # pylint: disable=unused-import
14 from ansible_collections.netapp.ontap.tests.unit.plugins.module_utils.ansible_mocks import assert_no_warnings, \
15 assert_warning_was_raised, call_main, patch_ansible, create_module, create_and_apply, expect_and_capture_ansible_exception, print_warnings
16 from ansible_collections.netapp.ontap.tests.unit.framework.mock_rest_and_zapi_requests import 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_responses
18 from ansible_collections.netapp.ontap.tests.unit.framework.rest_factory import rest_responses
19
20 from ansible_collections.netapp.ontap.plugins.modules.na_ontap_quotas \
21 import NetAppONTAPQuotas as my_module, main as my_main
22
23 # if not netapp_utils.has_netapp_lib():
24 # pytestmark = pytest.mark.skip('skipping as missing required netapp_lib')
25
26
27 SRR = rest_responses({
28 # module specific responses
29 'quota_record': (
30 200,
31 {
32 "records": [
33 {
34 "svm": {
35 "uuid": "671aa46e-11ad-11ec-a267-005056b30cfa",
36 "name": "ansible"
37 },
38 "files": {
39 "hard_limit": "100",
40 "soft_limit": "80"
41 },
42 "qtree": {
43 "id": "1",
44 "name": "qt1"
45 },
46 "space": {
47 "hard_limit": "1222800",
48 "soft_limit": "51200"
49 },
50 "type": "user",
51 "user_mapping": False,
52 "users": [{"name": "quota_user"}],
53 "uuid": "264a9e0b-2e03-11e9-a610-005056a7b72d",
54 "volume": {"name": "fv", "uuid": "264a9e0b-2e03-11e9-a610-005056a7b72da"},
55 "target": {
56 "name": "20:05:00:50:56:b3:0c:fa"
57 },
58 },
59 {
60 "svm": {
61 "uuid": "671aa46e-11ad-11ec-a267-005056b30cfa",
62 "name": "ansible"
63 },
64 "files": {
65 "hard_limit": "100",
66 "soft_limit": "80"
67 },
68 "qtree": {
69 "id": "1",
70 "name": "qt1"
71 },
72 "space": {
73 "hard_limit": "1222800",
74 "soft_limit": "51200"
75 },
76 "type": "user",
77 "user_mapping": False,
78 "users": [{"id": "757"}],
79 "uuid": "264a9e0b-2e03-11e9-a610-005056a7b72d",
80 "volume": {"name": "fv", "uuid": "264a9e0b-2e03-11e9-a610-005056a7b72da"},
81 "target": {
82 "name": "20:05:00:50:56:b3:0c:fa"
83 },
84 }
85 ],
86 "num_records": 1
87 }, None
88 ),
89 'quota_record_0_empty_limits': (200, {"records": [{
90 "svm": {"name": "ansible"},
91 "files": {"hard_limit": 0},
92 "qtree": {"id": "1", "name": "qt1"},
93 "space": {"hard_limit": 0},
94 "type": "user",
95 "user_mapping": False,
96 "users": [{"name": "quota_user"}],
97 "uuid": "264a9e0b-2e03-11e9-a610-005056a7b72d",
98 "volume": {"name": "fv", "uuid": "264a9e0b-2e03-11e9-a610-005056a7b72da"},
99 "target": {"name": "20:05:00:50:56:b3:0c:fa"},
100 }], "num_records": 1}, None),
101 'quota_status': (
102 200,
103 {
104 "records": [
105 {
106 "quota": {"state": "off"}
107 }
108 ],
109 "num_records": 1
110 }, None
111 ),
112 'quota_on': (
113 200,
114 {
115 "records": [
116 {
117 "quota": {"state": "on"}
118 }
119 ],
120 "num_records": 1
121 }, None
122 ),
123 "no_record": (
124 200,
125 {"num_records": 0},
126 None),
127 "error_5308572": (409, None, {'code': 5308572, 'message': 'Expected delete error'}),
128 "error_5308569": (409, None, {'code': 5308569, 'message': 'Expected delete error'}),
129 "error_5308568": (409, None, {'code': 5308568, 'message': 'Expected create error'}),
130 "error_5308571": (409, None, {'code': 5308571, 'message': 'Expected create error'}),
131 "error_5308567": (409, None, {'code': 5308567, 'message': 'Expected modify error'}),
132 'error_rest': (404, None, {"message": "temporarily locked from changes", "code": "4", "target": "uuid"}),
133 "volume_uuid": (200, {"records": [{
134 'uuid': 'sdgthfd'
135 }], 'num_records': 1}, None),
136 'job_info': (200, {
137 "job": {
138 "uuid": "d78811c1-aebc-11ec-b4de-005056b30cfa",
139 "_links": {"self": {"href": "/api/cluster/jobs/d78811c1-aebc-11ec-b4de-005056b30cfa"}}
140 }}, None),
141 'job_not_found': (404, "", {"message": "entry doesn't exist", "code": "4", "target": "uuid"})
142 })
143
144
145 quota_policy = {
146 'num-records': 1,
147 'attributes-list': {'quota-entry': {'volume': 'ansible', 'policy-name': 'policy_name', 'perform-user-mapping': 'true',
148 'file-limit': '-', 'disk-limit': '-', 'quota-target': '/vol/ansible',
149 'soft-file-limit': '-', 'soft-disk-limit': '-', 'threshold': '-'}},
150 }
151
152 quota_policies = {
153 'num-records': 2,
154 'attributes-list': [{'quota-policy-info': {'policy-name': 'p1'}},
155 {'quota-policy-info': {'policy-name': 'p2'}}],
156 }
157
158 ZRR = zapi_responses({
159 'quota_policy': build_zapi_response(quota_policy, 1),
160 'quota_on': build_zapi_response({'status': 'on'}, 1),
161 'quota_off': build_zapi_response({'status': 'off'}, 1),
162 'quota_policies': build_zapi_response(quota_policies, 1),
163 'quota_fail': build_zapi_error('TEST', 'This exception is from the unit test'),
164 'quota_fail_13001': build_zapi_error('13001', 'success'),
165 'quota_fail_14958': build_zapi_error('14958', 'No valid quota rules found'),
166 })
167
168
169 DEFAULT_ARGS = {
170 'hostname': 'hostname',
171 'username': 'username',
172 'password': 'password',
173 'volume': 'ansible',
174 'vserver': 'ansible',
175 'quota_target': '/vol/ansible',
176 'type': 'user',
177 'use_rest': 'never'
178 }
179
180
181 @pytest.mark.skipif(not netapp_utils.has_netapp_lib(), reason="skipping as missing required netapp_lib")
182 def test_module_fail_when_required_args_missing():
183 error = create_module(my_module, fail=True)['msg']
184 assert 'missing required arguments:' in error
185
186
187 @pytest.mark.skipif(not netapp_utils.has_netapp_lib(), reason="skipping as missing required netapp_lib")
188 def test_ensure_get_called():
189 register_responses([
190 ('ZAPI', 'quota-list-entries-iter', ZRR['empty']),
191 ])
192 my_obj = create_module(my_module, DEFAULT_ARGS)
193 quotas = my_obj.get_quotas()
194 print('QUOTAS', quotas)
195 assert quotas is None
196
197
198 @pytest.mark.skipif(not netapp_utils.has_netapp_lib(), reason="skipping as missing required netapp_lib")
199 def test_ensure_get_quota_not_called():
200 args = dict(DEFAULT_ARGS)
201 args.pop('quota_target')
202 args.pop('type')
203 my_obj = create_module(my_module, args)
204 assert my_obj.get_quotas() is None
205
206
207 @pytest.mark.skipif(not netapp_utils.has_netapp_lib(), reason="skipping as missing required netapp_lib")
208 def test_ensure_get_called_existing():
209 register_responses([
210 ('ZAPI', 'quota-list-entries-iter', ZRR['quota_policy']),
211 ])
212 my_obj = create_module(my_module, DEFAULT_ARGS)
213 quotas = my_obj.get_quotas()
214 print('QUOTAS', quotas)
215 assert quotas
216
217
218 @pytest.mark.skipif(not netapp_utils.has_netapp_lib(), reason="skipping as missing required netapp_lib")
219 def test_successful_create():
220 ''' creating quota and testing idempotency '''
221 register_responses([
222 ('ZAPI', 'quota-list-entries-iter', ZRR['no_records']),
223 ('ZAPI', 'quota-status', ZRR['quota_on']),
224 ('ZAPI', 'quota-set-entry', ZRR['success']),
225 ('ZAPI', 'quota-resize', ZRR['success']),
226 ('ZAPI', 'quota-list-entries-iter', ZRR['quota_policy']),
227 ('ZAPI', 'quota-status', ZRR['quota_on']),
228 ])
229 module_args = {
230 'file_limit': '3',
231 'disk_limit': '4',
232 'perform_user_mapping': False,
233 'policy': 'policy',
234 'soft_file_limit': '3',
235 'soft_disk_limit': '4',
236 'threshold': '10',
237 }
238 assert create_and_apply(my_module, DEFAULT_ARGS, module_args)['changed']
239 assert not create_and_apply(my_module, DEFAULT_ARGS)['changed']
240
241
242 @pytest.mark.skipif(not netapp_utils.has_netapp_lib(), reason="skipping as missing required netapp_lib")
243 def test_successful_delete():
244 ''' deleting quota and testing idempotency '''
245 register_responses([
246 ('ZAPI', 'quota-list-entries-iter', ZRR['quota_policy']),
247 ('ZAPI', 'quota-status', ZRR['quota_on']),
248 ('ZAPI', 'quota-delete-entry', ZRR['success']),
249 ('ZAPI', 'quota-resize', ZRR['success']),
250 ('ZAPI', 'quota-list-entries-iter', ZRR['no_records']),
251 ('ZAPI', 'quota-status', ZRR['quota_on']),
252 ])
253 module_args = {
254 'policy': 'policy',
255 'state': 'absent'
256 }
257 assert create_and_apply(my_module, DEFAULT_ARGS, module_args)['changed']
258 assert not create_and_apply(my_module, DEFAULT_ARGS, module_args)['changed']
259
260
261 @pytest.mark.skipif(not netapp_utils.has_netapp_lib(), reason="skipping as missing required netapp_lib")
262 @patch('time.sleep')
263 def test_successful_modify(dont_sleep):
264 ''' modifying quota and testing idempotency '''
265 register_responses([
266 ('ZAPI', 'quota-list-entries-iter', ZRR['quota_policy']),
267 ('ZAPI', 'quota-status', ZRR['quota_on']),
268 ('ZAPI', 'quota-modify-entry', ZRR['success']),
269 ('ZAPI', 'quota-off', ZRR['success']),
270 ('ZAPI', 'quota-on', ZRR['success']),
271 ])
272 module_args = {
273 'activate_quota_on_change': 'reinitialize',
274 'file_limit': '3',
275 'policy': 'policy',
276 }
277 assert create_and_apply(my_module, DEFAULT_ARGS, module_args)['changed']
278
279
280 @pytest.mark.skipif(not netapp_utils.has_netapp_lib(), reason="skipping as missing required netapp_lib")
281 def test_quota_on_off():
282 ''' quota set on or off '''
283 register_responses([
284 ('ZAPI', 'quota-list-entries-iter', ZRR['quota_policy']),
285 ('ZAPI', 'quota-status', ZRR['quota_off']),
286 ('ZAPI', 'quota-list-entries-iter', ZRR['quota_policy']),
287 ('ZAPI', 'quota-status', ZRR['quota_on']),
288 ('ZAPI', 'quota-off', ZRR['success']),
289 ])
290 module_args = {'set_quota_status': False}
291 assert not create_and_apply(my_module, DEFAULT_ARGS, module_args)['changed']
292 assert create_and_apply(my_module, DEFAULT_ARGS, module_args)['changed']
293
294
295 @pytest.mark.skipif(not netapp_utils.has_netapp_lib(), reason="skipping as missing required netapp_lib")
296 def test_if_all_methods_catch_exception():
297 register_responses([
298 ('ZAPI', 'quota-status', ZRR['quota_fail']),
299 ('ZAPI', 'quota-list-entries-iter', ZRR['quota_fail']),
300 ('ZAPI', 'quota-set-entry', ZRR['quota_fail']),
301 ('ZAPI', 'quota-delete-entry', ZRR['quota_fail']),
302 ('ZAPI', 'quota-modify-entry', ZRR['quota_fail']),
303 ('ZAPI', 'quota-on', ZRR['quota_fail']),
304 ('ZAPI', 'quota-policy-get-iter', ZRR['quota_fail']),
305 ('ZAPI', 'quota-resize', ZRR['quota_fail']),
306 ])
307 my_obj = create_module(my_module, DEFAULT_ARGS)
308 assert 'Error fetching quotas status info' in expect_and_capture_ansible_exception(my_obj.get_quota_status, 'fail')['msg']
309 assert 'Error fetching quotas info' in expect_and_capture_ansible_exception(my_obj.get_quotas, 'fail')['msg']
310 assert 'Error adding/modifying quota entry' in expect_and_capture_ansible_exception(my_obj.quota_entry_set, 'fail')['msg']
311 assert 'Error deleting quota entry' in expect_and_capture_ansible_exception(my_obj.quota_entry_delete, 'fail')['msg']
312 assert 'Error modifying quota entry' in expect_and_capture_ansible_exception(my_obj.quota_entry_modify, 'fail', {})['msg']
313 assert 'Error setting quota-on for ansible' in expect_and_capture_ansible_exception(my_obj.on_or_off_quota, 'fail', 'quota-on')['msg']
314 assert 'Error fetching quota policies' in expect_and_capture_ansible_exception(my_obj.get_quota_policies, 'fail')['msg']
315 assert 'Error setting quota-resize for ansible:' in expect_and_capture_ansible_exception(my_obj.resize_quota, 'fail')['msg']
316
317
318 @pytest.mark.skipif(not netapp_utils.has_netapp_lib(), reason="skipping as missing required netapp_lib")
319 def test_get_quota_policies():
320 register_responses([
321 ('ZAPI', 'quota-policy-get-iter', ZRR['quota_policies']),
322 ])
323 my_obj = create_module(my_module, DEFAULT_ARGS)
324 policies = my_obj.get_quota_policies()
325 assert len(policies) == 2
326
327
328 @pytest.mark.skipif(not netapp_utils.has_netapp_lib(), reason="skipping as missing required netapp_lib")
329 def test_debug_quota_get_error_fail():
330 register_responses([
331 ('ZAPI', 'quota-policy-get-iter', ZRR['quota_policies']),
332 ('ZAPI', 'quota-list-entries-iter', ZRR['quota_policy']),
333 ('ZAPI', 'quota-list-entries-iter', ZRR['quota_policy']),
334 ])
335 my_obj = create_module(my_module, DEFAULT_ARGS)
336 error = expect_and_capture_ansible_exception(my_obj.debug_quota_get_error, 'fail', 'dummy error')['msg']
337 assert error.startswith('Error fetching quotas info: dummy error - current vserver policies: ')
338
339
340 @pytest.mark.skipif(not netapp_utils.has_netapp_lib(), reason="skipping as missing required netapp_lib")
341 def test_debug_quota_get_error_success():
342 register_responses([
343 ('ZAPI', 'quota-policy-get-iter', ZRR['quota_policy']),
344 ('ZAPI', 'quota-list-entries-iter', ZRR['quota_policy']),
345 ])
346 my_obj = create_module(my_module, DEFAULT_ARGS)
347 quotas = my_obj.debug_quota_get_error('dummy error')
348 print('QUOTAS', quotas)
349 assert quotas
350
351
352 @pytest.mark.skipif(not netapp_utils.has_netapp_lib(), reason="skipping as missing required netapp_lib")
353 def test_get_no_quota_retry_on_13001():
354 register_responses([
355 ('ZAPI', 'quota-list-entries-iter', ZRR['quota_fail_13001']),
356 ])
357 module_args = {'policy': 'policy'}
358 my_obj = create_module(my_module, DEFAULT_ARGS, module_args)
359 error = expect_and_capture_ansible_exception(my_obj.get_quotas, 'fail')['msg']
360 assert error.startswith('Error fetching quotas info for policy policy')
361
362
363 @pytest.mark.skipif(not netapp_utils.has_netapp_lib(), reason="skipping as missing required netapp_lib")
364 def test_get_quota_retry_on_13001():
365 register_responses([
366 ('ZAPI', 'quota-list-entries-iter', ZRR['quota_fail_13001']),
367 ('ZAPI', 'quota-policy-get-iter', ZRR['quota_policy']),
368 ('ZAPI', 'quota-list-entries-iter', ZRR['quota_policy']),
369 ])
370 my_obj = create_module(my_module, DEFAULT_ARGS)
371 quotas = my_obj.get_quotas()
372 print('QUOTAS', quotas)
373 assert quotas
374
375
376 @pytest.mark.skipif(not netapp_utils.has_netapp_lib(), reason="skipping as missing required netapp_lib")
377 def test_resize_warning():
378 ''' warning as resize is not allowed if all rules were deleted '''
379 register_responses([
380 ('ZAPI', 'quota-resize', ZRR['quota_fail_14958']),
381 ])
382 my_obj = create_module(my_module, DEFAULT_ARGS)
383 my_obj.resize_quota('delete')
384 assert_warning_was_raised('Last rule deleted, but quota is on as resize is not allowed.')
385
386
387 @pytest.mark.skipif(not netapp_utils.has_netapp_lib(), reason="skipping as missing required netapp_lib")
388 def test_quota_on_warning():
389 ''' warning as quota-on is not allowed if all rules were deleted '''
390 register_responses([
391 ('ZAPI', 'quota-on', ZRR['quota_fail_14958']),
392 ])
393 my_obj = create_module(my_module, DEFAULT_ARGS)
394 my_obj.on_or_off_quota('quota-on', 'delete')
395 print_warnings()
396 assert_warning_was_raised('Last rule deleted, quota is off.')
397
398
399 @pytest.mark.skipif(not netapp_utils.has_netapp_lib(), reason="skipping as missing required netapp_lib")
400 def test_convert_size_format():
401 module_args = {'disk_limit': '10MB'}
402 my_obj = create_module(my_module, DEFAULT_ARGS, module_args)
403 assert my_obj.convert_to_kb_or_bytes('disk_limit')
404 print(my_obj.parameters)
405 assert my_obj.parameters['disk_limit'] == '10240'
406 my_obj.parameters['disk_limit'] = '10'
407 assert my_obj.convert_to_kb_or_bytes('disk_limit')
408 print(my_obj.parameters)
409 assert my_obj.parameters['disk_limit'] == '10'
410 my_obj.parameters['disk_limit'] = '10tB'
411 assert my_obj.convert_to_kb_or_bytes('disk_limit')
412 print(my_obj.parameters)
413 assert my_obj.parameters['disk_limit'] == str(10 * 1024 * 1024 * 1024)
414 my_obj.parameters['disk_limit'] = ''
415 assert not my_obj.convert_to_kb_or_bytes('disk_limit')
416 print(my_obj.parameters)
417 assert my_obj.parameters['disk_limit'] == ''
418
419
420 def test_error_convert_size_format():
421 module_args = {
422 'disk_limit': '10MBi',
423 'quota_target': ''
424 }
425 error = create_module(my_module, DEFAULT_ARGS, module_args, fail=True)['msg']
426 assert error.startswith('disk_limit input string is not a valid size format')
427 module_args = {
428 'soft_disk_limit': 'MBi',
429 'quota_target': ''
430 }
431 error = create_module(my_module, DEFAULT_ARGS, module_args, fail=True)['msg']
432 assert error.startswith('soft_disk_limit input string is not a valid size format')
433 module_args = {
434 'soft_disk_limit': '10MB10',
435 'quota_target': ''
436 }
437 error = create_module(my_module, DEFAULT_ARGS, module_args, fail=True)['msg']
438 assert error.startswith('soft_disk_limit input string is not a valid size format')
439
440
441 @pytest.mark.skipif(not netapp_utils.has_netapp_lib(), reason="skipping as missing required netapp_lib")
442 def test_has_netapp_lib(has_netapp_lib):
443 has_netapp_lib.return_value = False
444 assert call_main(my_main, DEFAULT_ARGS, fail=True)['msg'] == 'Error: the python NetApp-Lib module is required. Import error: None'
445
446
447 @pytest.mark.skipif(not netapp_utils.has_netapp_lib(), reason="skipping as missing required netapp_lib")
448 def create_from_main():
449 register_responses([
450 ('ZAPI', 'quota-list-entries-iter', ZRR['no_records']),
451 ('ZAPI', 'quota-status', ZRR['quota_on']),
452 ('ZAPI', 'quota-set-entry', ZRR['success']),
453 ])
454 assert call_main(my_main, DEFAULT_ARGS)['changed']
455
456
457 ARGS_REST = {
458 'hostname': 'test',
459 'username': 'test_user',
460 'password': 'test_pass!',
461 'use_rest': 'always',
462 'volume': 'ansible',
463 'vserver': 'ansible',
464 'quota_target': 'quota_user',
465 'qtree': 'qt1',
466 'type': 'user'
467 }
468
469
470 def test_rest_error_get():
471 '''Test error rest get'''
472 register_responses([
473 ('GET', 'cluster', SRR['is_rest']),
474 ('GET', 'storage/quota/rules', SRR['generic_error']),
475 ])
476 error = create_and_apply(my_module, ARGS_REST, fail=True)['msg']
477 assert 'Error on getting quota rule info' in error
478
479
480 def test_rest_successful_create():
481 '''Test successful rest create'''
482 register_responses([
483 ('GET', 'cluster', SRR['is_rest']),
484 ('GET', 'storage/quota/rules', SRR['empty_records']),
485 ('GET', 'storage/volumes', SRR['quota_status']),
486 ('POST', 'storage/quota/rules', SRR['empty_good']),
487 ])
488 assert create_and_apply(my_module, ARGS_REST)
489
490
491 def test_rest_successful_create_default_user_quota_rule():
492 '''Test successful rest create default quota rule'''
493 register_responses([
494 ('GET', 'cluster', SRR['is_rest']),
495 ('GET', 'storage/quota/rules', SRR['empty_records']),
496 ('GET', 'storage/volumes', SRR['quota_status']),
497 ('POST', 'storage/quota/rules', SRR['empty_good']),
498 ])
499 module_args = {
500 "quota_target": "",
501 "type": "user"
502 }
503 assert create_and_apply(my_module, ARGS_REST, module_args)
504
505
506 def test_rest_successful_create_userid():
507 '''Test successful rest create with users.id with idempotency check'''
508 register_responses([
509 ('GET', 'cluster', SRR['is_rest']),
510 ('GET', 'storage/quota/rules', SRR['empty_records']),
511 ('GET', 'storage/volumes', SRR['quota_status']),
512 ('POST', 'storage/quota/rules', SRR['empty_good']),
513
514 ('GET', 'cluster', SRR['is_rest']),
515 ('GET', 'storage/quota/rules', SRR['quota_record']),
516 ('GET', 'storage/volumes', SRR['quota_status']),
517 ])
518 module_args = {
519 "quota_target": "757",
520 }
521 assert create_and_apply(my_module, ARGS_REST, module_args)
522 assert create_and_apply(my_module, ARGS_REST, module_args)['changed'] is False
523
524
525 @patch('time.sleep')
526 def test_rest_successful_create_job_error(sleep):
527 '''Test successful rest create'''
528 register_responses([
529 ('GET', 'cluster', SRR['is_rest']),
530 ('GET', 'storage/quota/rules', SRR['empty_records']),
531 ('GET', 'storage/volumes', SRR['quota_status']),
532 ('POST', 'storage/quota/rules', SRR['job_info']),
533 ('GET', 'cluster/jobs/d78811c1-aebc-11ec-b4de-005056b30cfa', SRR['job_not_found']),
534 ('GET', 'cluster/jobs/d78811c1-aebc-11ec-b4de-005056b30cfa', SRR['job_not_found']),
535 ('GET', 'cluster/jobs/d78811c1-aebc-11ec-b4de-005056b30cfa', SRR['job_not_found']),
536 ('GET', 'cluster/jobs/d78811c1-aebc-11ec-b4de-005056b30cfa', SRR['job_not_found']),
537 ('GET', 'storage/volumes', SRR['volume_uuid'])
538 ])
539 assert create_and_apply(my_module, ARGS_REST)
540 print_warnings()
541 assert_warning_was_raised('Ignoring job status, assuming success.')
542
543
544 def test_rest_error_create():
545 '''Test error rest create'''
546 register_responses([
547 ('GET', 'cluster', SRR['is_rest']),
548 ('GET', 'storage/quota/rules', SRR['empty_records']),
549 ('GET', 'storage/volumes', SRR['quota_status']),
550 ('POST', 'storage/quota/rules', SRR['generic_error']),
551 ])
552 error = create_and_apply(my_module, ARGS_REST, fail=True)['msg']
553 assert 'Error on creating quotas rule:' in error
554
555
556 def test_delete_rest():
557 ''' Test delete with rest API'''
558 register_responses([
559 ('GET', 'cluster', SRR['is_rest']),
560 ('GET', 'storage/quota/rules', SRR['quota_record']),
561 ('GET', 'storage/volumes', SRR['quota_status']),
562 ('DELETE', 'storage/quota/rules/264a9e0b-2e03-11e9-a610-005056a7b72d', SRR['empty_good']),
563 ])
564 module_args = {
565 'state': 'absent'
566 }
567 assert create_and_apply(my_module, ARGS_REST, module_args)
568
569
570 def test_error_delete_rest():
571 ''' Test error delete with rest API'''
572 register_responses([
573 ('GET', 'cluster', SRR['is_rest']),
574 ('GET', 'storage/quota/rules', SRR['quota_record']),
575 ('GET', 'storage/volumes', SRR['quota_status']),
576 ('DELETE', 'storage/quota/rules/264a9e0b-2e03-11e9-a610-005056a7b72d', SRR['generic_error']),
577 ])
578 module_args = {
579 'state': 'absent'
580 }
581 error = create_and_apply(my_module, ARGS_REST, module_args, fail=True)['msg']
582 assert 'Error on deleting quotas rule:' in error
583
584
585 def test_modify_files_limit_rest():
586 ''' Test modify with rest API'''
587 register_responses([
588 ('GET', 'cluster', SRR['is_rest']),
589 ('GET', 'storage/quota/rules', SRR['quota_record']),
590 ('GET', 'storage/volumes', SRR['quota_on']),
591 ('PATCH', 'storage/quota/rules/264a9e0b-2e03-11e9-a610-005056a7b72d', SRR['empty_good']),
592 ])
593 module_args = {
594 "file_limit": "122", "soft_file_limit": "90"
595 }
596 assert create_and_apply(my_module, ARGS_REST, module_args)
597
598
599 def test_modify_space_limit_rest():
600 ''' Test modify with rest API'''
601 register_responses([
602 ('GET', 'cluster', SRR['is_rest']),
603 ('GET', 'storage/quota/rules', SRR['quota_record']),
604 ('GET', 'storage/volumes', SRR['quota_on']),
605 ('PATCH', 'storage/quota/rules/264a9e0b-2e03-11e9-a610-005056a7b72d', SRR['empty_good']),
606 ])
607 module_args = {
608 "disk_limit": "1024", "soft_disk_limit": "80"
609 }
610 assert create_and_apply(my_module, ARGS_REST, module_args)
611
612
613 def test_modify_rest_error():
614 ''' Test negative modify with rest API'''
615 register_responses([
616 ('GET', 'cluster', SRR['is_rest']),
617 ('GET', 'storage/quota/rules', SRR['quota_record']),
618 ('GET', 'storage/volumes', SRR['quota_status']),
619 ('PATCH', 'storage/quota/rules/264a9e0b-2e03-11e9-a610-005056a7b72d', SRR['generic_error']),
620 ])
621 module_args = {
622 'perform_user_mapping': True
623 }
624 error = create_and_apply(my_module, ARGS_REST, module_args, fail=True)['msg']
625 assert 'Error on modifying quotas rule:' in error
626
627
628 @patch('time.sleep')
629 def test_modify_rest_temporary_locked_error(sleep):
630 ''' Test negative modify with rest API'''
631 register_responses([
632 ('GET', 'cluster', SRR['is_rest']),
633 ('GET', 'storage/quota/rules', SRR['quota_record']),
634 ('GET', 'storage/volumes', SRR['quota_status']),
635 # wait for 60s if we get temporary locl error.
636 ('PATCH', 'storage/quota/rules/264a9e0b-2e03-11e9-a610-005056a7b72d', SRR['error_rest']),
637 ('PATCH', 'storage/quota/rules/264a9e0b-2e03-11e9-a610-005056a7b72d', SRR['error_rest']),
638 ('PATCH', 'storage/quota/rules/264a9e0b-2e03-11e9-a610-005056a7b72d', SRR['success']),
639
640 ('GET', 'cluster', SRR['is_rest']),
641 ('GET', 'storage/quota/rules', SRR['quota_record']),
642 ('GET', 'storage/volumes', SRR['quota_status']),
643 # error persist even after 60s
644 ('PATCH', 'storage/quota/rules/264a9e0b-2e03-11e9-a610-005056a7b72d', SRR['error_rest']),
645 ('PATCH', 'storage/quota/rules/264a9e0b-2e03-11e9-a610-005056a7b72d', SRR['error_rest']),
646 ('PATCH', 'storage/quota/rules/264a9e0b-2e03-11e9-a610-005056a7b72d', SRR['error_rest']),
647
648 # wait 60s in create for temporary locked error.
649 ('GET', 'cluster', SRR['is_rest']),
650 ('GET', 'storage/quota/rules', SRR['empty_records']),
651 ('GET', 'storage/volumes', SRR['quota_status']),
652 ('POST', 'storage/quota/rules', SRR['error_rest']),
653 ('POST', 'storage/quota/rules', SRR['success']),
654 ])
655 module_args = {
656 'perform_user_mapping': True
657 }
658 assert create_and_apply(my_module, ARGS_REST, module_args)['changed']
659 assert 'Error on modifying quotas rule:' in create_and_apply(my_module, ARGS_REST, module_args, fail=True)['msg']
660 assert create_and_apply(my_module, ARGS_REST, module_args)['changed']
661
662
663 def test_rest_successful_create_idempotency():
664 '''Test successful rest create'''
665 register_responses([
666 ('GET', 'cluster', SRR['is_rest']),
667 ('GET', 'storage/quota/rules', SRR['quota_record']),
668 ('GET', 'storage/volumes', SRR['quota_status']),
669 ('GET', 'cluster', SRR['is_rest']),
670 ('GET', 'storage/quota/rules', SRR['quota_record_0_empty_limits']),
671 ('GET', 'storage/volumes', SRR['quota_status']),
672 ('GET', 'cluster', SRR['is_rest']),
673 ('GET', 'storage/quota/rules', SRR['quota_record_0_empty_limits']),
674 ('GET', 'storage/volumes', SRR['quota_status'])
675 ])
676 assert create_and_apply(my_module, ARGS_REST)['changed'] is False
677 module_args = {
678 "disk_limit": "0", "soft_disk_limit": "-", "file_limit": 0, "soft_file_limit": "-"
679 }
680 assert create_and_apply(my_module, ARGS_REST, module_args)['changed'] is False
681 module_args = {
682 "disk_limit": "0", "soft_disk_limit": "-1", "file_limit": "0", "soft_file_limit": "-1"
683 }
684 assert create_and_apply(my_module, ARGS_REST, module_args)['changed'] is False
685
686
687 def test_rest_successful_delete_idempotency():
688 '''Test successful rest delete'''
689 register_responses([
690 ('GET', 'cluster', SRR['is_rest']),
691 ('GET', 'storage/quota/rules', SRR['empty_records']),
692 ('GET', 'storage/volumes', SRR['quota_status']),
693 ])
694 module_args = {'use_rest': 'always', 'state': 'absent'}
695 assert create_and_apply(my_module, ARGS_REST, module_args)['changed'] is False
696
697
698 def test_modify_quota_status_rest():
699 ''' Test modify quota status and error with rest API'''
700 register_responses([
701 ('GET', 'cluster', SRR['is_rest']),
702 ('GET', 'storage/quota/rules', SRR['quota_record']),
703 ('GET', 'storage/volumes', SRR['quota_status']),
704 ('PATCH', 'storage/volumes/264a9e0b-2e03-11e9-a610-005056a7b72da', SRR['empty_good'])
705 ])
706 module_args = {"set_quota_status": "on"}
707 assert create_and_apply(my_module, ARGS_REST, module_args)
708
709
710 @pytest.mark.skipif(not netapp_utils.has_netapp_lib(), reason="skipping as missing required netapp_lib")
711 def test_error_convert_size_format_rest():
712 register_responses([
713 ('GET', 'cluster', SRR['is_rest']),
714 ('GET', 'cluster', SRR['is_rest']),
715 ('GET', 'cluster', SRR['is_rest']),
716 ])
717 module_args = {
718 'disk_limit': '10MBi',
719 'quota_target': ''
720 }
721 error = create_module(my_module, ARGS_REST, module_args, fail=True)['msg']
722 assert error.startswith('disk_limit input string is not a valid size format')
723 module_args = {
724 'soft_disk_limit': 'MBi',
725 'quota_target': ''
726 }
727 error = create_module(my_module, ARGS_REST, module_args, fail=True)['msg']
728 assert error.startswith('soft_disk_limit input string is not a valid size format')
729 module_args = {
730 'soft_disk_limit': '10MB10',
731 'quota_target': ''
732 }
733 error = create_module(my_module, ARGS_REST, module_args, fail=True)['msg']
734 assert error.startswith('soft_disk_limit input string is not a valid size format')
735
736
737 @pytest.mark.skipif(not netapp_utils.has_netapp_lib(), reason="skipping as missing required netapp_lib")
738 def test_convert_size_format_rest():
739 module_args = {'disk_limit': '10MB'}
740 my_obj = create_module(my_module, DEFAULT_ARGS, module_args)
741 assert my_obj.convert_to_kb_or_bytes('disk_limit')
742 print(my_obj.parameters)
743 assert my_obj.parameters['disk_limit'] == '10240'
744 my_obj.parameters['disk_limit'] = '10'
745 assert my_obj.convert_to_kb_or_bytes('disk_limit')
746 print(my_obj.parameters)
747 assert my_obj.parameters['disk_limit'] == '10'
748 my_obj.parameters['disk_limit'] = '10tB'
749 assert my_obj.convert_to_kb_or_bytes('disk_limit')
750 print(my_obj.parameters)
751 assert my_obj.parameters['disk_limit'] == str(10 * 1024 * 1024 * 1024)
752 my_obj.parameters['disk_limit'] = ''
753 assert not my_obj.convert_to_kb_or_bytes('disk_limit')
754 print(my_obj.parameters)
755 assert my_obj.parameters['disk_limit'] == ''
756
757
758 def test_warning_rest_delete_5308572():
759 register_responses([
760 ('GET', 'cluster', SRR['is_rest']),
761 ('GET', 'storage/quota/rules', SRR['quota_record']),
762 ('GET', 'storage/volumes', SRR['quota_status']),
763 ('DELETE', 'storage/quota/rules/264a9e0b-2e03-11e9-a610-005056a7b72d', SRR['error_5308572'])
764 ])
765 assert create_and_apply(my_module, ARGS_REST, {'state': 'absent'})['changed']
766 # assert 'Error on deleting quotas rule:' in error
767 msg = "Quota policy rule delete opertation succeeded. However the rule is still being enforced. To stop enforcing, "\
768 "reinitialize(disable and enable again) the quota for volume ansible in SVM ansible."
769 assert_warning_was_raised(msg)
770
771
772 @patch('time.sleep')
773 def test_no_warning_rest_delete_5308572(sleep):
774 register_responses([
775 ('GET', 'cluster', SRR['is_rest']),
776 ('GET', 'storage/quota/rules', SRR['quota_record']),
777 ('GET', 'storage/volumes', SRR['quota_on']),
778 ('DELETE', 'storage/quota/rules/264a9e0b-2e03-11e9-a610-005056a7b72d', SRR['error_5308572']),
779 ('PATCH', 'storage/volumes/264a9e0b-2e03-11e9-a610-005056a7b72da', SRR['success']),
780 ('PATCH', 'storage/volumes/264a9e0b-2e03-11e9-a610-005056a7b72da', SRR['success'])
781 ])
782 assert create_and_apply(my_module, ARGS_REST, {'state': 'absent', 'activate_quota_on_change': 'reinitialize'})['changed']
783 assert_no_warnings()
784
785
786 def test_warning_rest_delete_5308569():
787 register_responses([
788 ('GET', 'cluster', SRR['is_rest']),
789 ('GET', 'storage/quota/rules', SRR['quota_record']),
790 ('GET', 'storage/volumes', SRR['quota_status']),
791 ('DELETE', 'storage/quota/rules/264a9e0b-2e03-11e9-a610-005056a7b72d', SRR['error_5308569'])
792 ])
793 assert create_and_apply(my_module, ARGS_REST, {'state': 'absent'})['changed']
794 # assert 'Error on deleting quotas rule:' in error
795 msg = "Quota policy rule delete opertation succeeded. However quota resize failed due to an internal error. To make quotas active, "\
796 "reinitialize(disable and enable again) the quota for volume ansible in SVM ansible."
797 assert_warning_was_raised(msg)
798
799
800 @patch('time.sleep')
801 def test_no_warning_rest_delete_5308569(sleep):
802 register_responses([
803 ('GET', 'cluster', SRR['is_rest']),
804 ('GET', 'storage/quota/rules', SRR['quota_record']),
805 ('GET', 'storage/volumes', SRR['quota_on']),
806 ('DELETE', 'storage/quota/rules/264a9e0b-2e03-11e9-a610-005056a7b72d', SRR['error_5308569']),
807 ('PATCH', 'storage/volumes/264a9e0b-2e03-11e9-a610-005056a7b72da', SRR['success']),
808 ('PATCH', 'storage/volumes/264a9e0b-2e03-11e9-a610-005056a7b72da', SRR['success'])
809 ])
810 assert create_and_apply(my_module, ARGS_REST, {'state': 'absent', 'activate_quota_on_change': 'reinitialize'})['changed']
811 assert_no_warnings()
812
813
814 def test_warning_rest_create_5308568():
815 register_responses([
816 ('GET', 'cluster', SRR['is_rest']),
817 ('GET', 'storage/quota/rules', SRR['empty_records']),
818 ('GET', 'storage/volumes', SRR['quota_status']),
819 ('POST', 'storage/quota/rules', SRR['error_5308568']),
820 ('GET', 'storage/volumes', SRR['volume_uuid'])
821 ])
822 assert create_and_apply(my_module, ARGS_REST)['changed']
823 msg = "Quota policy rule create opertation succeeded. However quota resize failed due to an internal error. To make quotas active, "\
824 "reinitialize(disable and enable again) the quota for volume ansible in SVM ansible."
825 assert_warning_was_raised(msg)
826
827
828 @patch('time.sleep')
829 def test_no_warning_rest_create_5308568(sleep):
830 register_responses([
831 ('GET', 'cluster', SRR['is_rest']),
832 ('GET', 'storage/quota/rules', SRR['empty_records']),
833 ('GET', 'storage/volumes', SRR['quota_on']),
834 ('POST', 'storage/quota/rules', SRR['error_5308568']),
835 ('GET', 'storage/volumes', SRR['volume_uuid']),
836 ('PATCH', 'storage/volumes/sdgthfd', SRR['success']),
837 ('PATCH', 'storage/volumes/sdgthfd', SRR['success'])
838 ])
839 assert create_and_apply(my_module, ARGS_REST, {'activate_quota_on_change': 'reinitialize'})['changed']
840 assert_no_warnings()
841
842
843 def test_warning_rest_create_5308571():
844 register_responses([
845 ('GET', 'cluster', SRR['is_rest']),
846 ('GET', 'storage/quota/rules', SRR['empty_records']),
847 ('GET', 'storage/volumes', SRR['quota_status']),
848 ('POST', 'storage/quota/rules', SRR['error_5308571']),
849 ('GET', 'storage/volumes', SRR['volume_uuid'])
850 ])
851 assert create_and_apply(my_module, ARGS_REST)['changed']
852 msg = "Quota policy rule create opertation succeeded. but quota resize is skipped. To make quotas active, "\
853 "reinitialize(disable and enable again) the quota for volume ansible in SVM ansible."
854 assert_warning_was_raised(msg)
855
856
857 @patch('time.sleep')
858 def test_no_warning_rest_create_5308571(sleep):
859 register_responses([
860 ('GET', 'cluster', SRR['is_rest']),
861 ('GET', 'storage/quota/rules', SRR['empty_records']),
862 ('GET', 'storage/volumes', SRR['quota_on']),
863 ('POST', 'storage/quota/rules', SRR['error_5308568']),
864 ('GET', 'storage/volumes', SRR['volume_uuid']),
865 ('PATCH', 'storage/volumes/sdgthfd', SRR['success']),
866 ('PATCH', 'storage/volumes/sdgthfd', SRR['success'])
867 ])
868 assert create_and_apply(my_module, ARGS_REST, {'activate_quota_on_change': 'reinitialize'})['changed']
869 assert_no_warnings()
870
871
872 def test_warning_rest_modify_5308567():
873 register_responses([
874 ('GET', 'cluster', SRR['is_rest']),
875 ('GET', 'storage/quota/rules', SRR['quota_record']),
876 ('GET', 'storage/volumes', SRR['quota_on']),
877 ('PATCH', 'storage/quota/rules/264a9e0b-2e03-11e9-a610-005056a7b72d', SRR['error_5308567']),
878 ])
879 module_args = {"soft_file_limit": "100"}
880 assert create_and_apply(my_module, ARGS_REST, module_args)
881 msg = "Quota policy rule modify opertation succeeded. However quota resize failed due to an internal error. To make quotas active, "\
882 "reinitialize(disable and enable again) the quota for volume ansible in SVM ansible."
883 assert_warning_was_raised(msg)
884
885
886 @patch('time.sleep')
887 def test_no_warning_rest_modify_5308567(sleep):
888 register_responses([
889 ('GET', 'cluster', SRR['is_rest']),
890 ('GET', 'storage/quota/rules', SRR['quota_record']),
891 ('GET', 'storage/volumes', SRR['quota_on']),
892 ('PATCH', 'storage/quota/rules/264a9e0b-2e03-11e9-a610-005056a7b72d', SRR['error_5308567']),
893 ('PATCH', 'storage/volumes/264a9e0b-2e03-11e9-a610-005056a7b72da', SRR['success']),
894 ('PATCH', 'storage/volumes/264a9e0b-2e03-11e9-a610-005056a7b72da', SRR['success'])
895 ])
896 module_args = {"soft_file_limit": "100", 'activate_quota_on_change': 'reinitialize'}
897 assert create_and_apply(my_module, ARGS_REST, module_args)['changed']
898 assert_no_warnings()
899
900
901 def test_if_all_methods_catch_exception_rest():
902 register_responses([
903 ('GET', 'cluster', SRR['is_rest_9_10_1']),
904 ('GET', 'storage/quota/rules', SRR['generic_error']),
905 ('GET', 'storage/volumes', SRR['generic_error']),
906 ('GET', 'storage/volumes', SRR['generic_error']),
907 ('GET', 'storage/volumes', SRR['empty_records']),
908 ('POST', 'storage/quota/rules', SRR['generic_error']),
909 ('DELETE', 'storage/quota/rules/abdcdef', SRR['generic_error']),
910 ('PATCH', 'storage/quota/rules/abdcdef', SRR['generic_error']),
911 ('PATCH', 'storage/volumes/ghijklmn', SRR['generic_error']),
912 ('GET', 'cluster', SRR['is_rest_9_10_1']),
913
914 ])
915 my_obj = create_module(my_module, ARGS_REST)
916 my_obj.quota_uuid = 'abdcdef'
917 my_obj.volume_uuid = 'ghijklmn'
918 assert 'Error on getting quota rule info' in expect_and_capture_ansible_exception(my_obj.get_quotas_rest, 'fail')['msg']
919 assert 'Error on getting quota status info' in expect_and_capture_ansible_exception(my_obj.get_quota_status_or_volume_id_rest, 'fail')['msg']
920 assert 'Error on getting volume' in expect_and_capture_ansible_exception(my_obj.get_quota_status_or_volume_id_rest, 'fail', True)['msg']
921 assert 'does not exist' in expect_and_capture_ansible_exception(my_obj.get_quota_status_or_volume_id_rest, 'fail', True)['msg']
922 assert 'Error on creating quotas rule' in expect_and_capture_ansible_exception(my_obj.quota_entry_set_rest, 'fail')['msg']
923 assert 'Error on deleting quotas rule' in expect_and_capture_ansible_exception(my_obj.quota_entry_delete_rest, 'fail')['msg']
924 assert 'Error on modifying quotas rule' in expect_and_capture_ansible_exception(my_obj.quota_entry_modify_rest, 'fail', {})['msg']
925 assert 'Error setting quota-on for ansible' in expect_and_capture_ansible_exception(my_obj.on_or_off_quota_rest, 'fail', 'quota-on')['msg']
926 error = "Error: Qtree cannot be specified for a tree type rule"
927 assert error in create_module(my_module, ARGS_REST, {'qtree': 'qtree1', 'type': 'tree'}, fail=True)['msg']