Debian Codegraph @

ansible_collections/netapp/ontap/tests/unit/plugins/module_utils/test_rest_generic.py

raw
1 # Copyright (c) 2022 NetApp
2 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
3
4 ''' unit tests for module_utils rest_generic.py - REST features '''
5 from __future__ import (absolute_import, division, print_function)
6 __metaclass__ = type
7
8 import pytest
9 import sys
10
11 from ansible.module_utils import basic
12 from ansible_collections.netapp.ontap.tests.unit.compat.mock import patch
13 # pylint: disable=unused-import
14 from ansible_collections.netapp.ontap.tests.unit.plugins.module_utils.ansible_mocks import patch_ansible, create_module
15 from ansible_collections.netapp.ontap.tests.unit.framework.mock_rest_and_zapi_requests import patch_request_and_invoke, register_responses
16 from ansible_collections.netapp.ontap.tests.unit.framework.rest_factory import rest_responses
17 import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils
18 from ansible_collections.netapp.ontap.plugins.module_utils import rest_generic
19
20 if not netapp_utils.HAS_REQUESTS and sys.version_info < (2, 7):
21 pytestmark = pytest.mark.skip('Skipping Unit Tests on 2.6 as requests is not available')
22
23 SRR = rest_responses({
24 'vservers_with_admin': (200, {
25 'records': [
26 {'vserver': 'vserver1', 'type': 'data '},
27 {'vserver': 'vserver2', 'type': 'data '},
28 {'vserver': 'cserver', 'type': 'admin'}
29 ]}, None),
30 'vservers_single': (200, {
31 'records': [
32 {'vserver': 'single', 'type': 'data '},
33 ]}, None),
34 'accepted_response': (202, {
35 'job': {
36 'uuid': 'd0b3eefe-cd59-11eb-a170-005056b338cd',
37 '_links': {'self': {'href': '/api/cluster/jobs/d0b3eefe-cd59-11eb-a170-005056b338cd'}}
38 }}, None),
39 'job_in_progress': (200, {
40 'job': {
41 'uuid': 'a1b2c3_job',
42 '_links': {'self': {'href': 'api/some_link'}}
43 }}, None),
44 'job_success': (200, {
45 'state': 'success',
46 'message': 'success_message',
47 'job': {
48 'uuid': 'a1b2c3_job',
49 '_links': {'self': {'href': 'some_link'}}
50 }}, None),
51 'job_failed': (200, {
52 'state': 'error',
53 'message': 'error_message',
54 'job': {
55 'uuid': 'a1b2c3_job',
56 '_links': {'self': {'href': 'some_link'}}
57 }}, None),
58 })
59
60 DEFAULT_ARGS = {
61 'hostname': 'test',
62 'username': 'test_user',
63 'password': 'test_pass!',
64 'cert_filepath': None,
65 'key_filepath': None,
66 }
67
68 CERT_ARGS = {
69 'hostname': 'test',
70 'cert_filepath': 'test_pem.pem',
71 'key_filepath': 'test_key.key'
72 }
73
74
75 class MockONTAPModule:
76 def __init__(self):
77 self.module = basic.AnsibleModule(netapp_utils.na_ontap_host_argument_spec())
78
79
80 def create_restapi_object(default_args, module_args=None):
81 module = create_module(MockONTAPModule, default_args, module_args)
82 return netapp_utils.OntapRestAPI(module.module)
83
84
85 def test_build_query_with_fields():
86 assert rest_generic.build_query_with_fields(None, None) is None
87 assert rest_generic.build_query_with_fields(query=None, fields=None) is None
88 assert rest_generic.build_query_with_fields(query={'aaa': 'vvv'}, fields=None) == {'aaa': 'vvv'}
89 assert rest_generic.build_query_with_fields(query=None, fields='aaa,bbb') == {'fields': 'aaa,bbb'}
90 assert rest_generic.build_query_with_fields(query={'aaa': 'vvv'}, fields='aaa,bbb') == {'aaa': 'vvv', 'fields': 'aaa,bbb'}
91
92
93 def test_build_query_with_timeout():
94 assert rest_generic.build_query_with_timeout(query=None, timeout=30) == {'return_timeout': 30}
95
96 # when timeout is 0, return_timeout is not added
97 assert rest_generic.build_query_with_timeout(query=None, timeout=0) is None
98 assert rest_generic.build_query_with_timeout(query={'aaa': 'vvv'}, timeout=0) == {'aaa': 'vvv'}
99
100 # when return_timeout is in the query, it has precedence
101 query = {'return_timeout': 55}
102 assert rest_generic.build_query_with_timeout(query, timeout=0) == query
103 assert rest_generic.build_query_with_timeout(query, timeout=20) == query
104 query = {'aaa': 'vvv', 'return_timeout': 55}
105 assert rest_generic.build_query_with_timeout(query, timeout=0) == query
106 assert rest_generic.build_query_with_timeout(query, timeout=20) == query
107
108
109 def test_successful_get_one_record_no_records_field():
110 register_responses([
111 ('GET', 'cluster', SRR['is_rest_9_10_1'])
112 ])
113 rest_api = create_restapi_object(DEFAULT_ARGS)
114 record, error = rest_generic.get_one_record(rest_api, 'cluster')
115 assert error is None
116 assert record == SRR['is_rest_9_10_1'][1]
117
118
119 def test_successful_get_one_record():
120 register_responses([
121 ('GET', 'cluster', SRR['vservers_single'])
122 ])
123 rest_api = create_restapi_object(DEFAULT_ARGS)
124 record, error = rest_generic.get_one_record(rest_api, 'cluster')
125 assert error is None
126 assert record == SRR['vservers_single'][1]['records'][0]
127
128
129 def test_successful_get_one_record_no_record():
130 register_responses([
131 ('GET', 'cluster', SRR['zero_records'])
132 ])
133 rest_api = create_restapi_object(DEFAULT_ARGS)
134 record, error = rest_generic.get_one_record(rest_api, 'cluster')
135 assert error is None
136 assert record is None
137
138
139 def test_successful_get_one_record_NN():
140 register_responses([
141 ('GET', 'cluster', SRR['is_rest_9_10_1'])
142 ])
143 rest_api = create_restapi_object(DEFAULT_ARGS)
144 record, error = rest_generic.get_one_record(rest_api, 'cluster', query=None, fields=None)
145 assert error is None
146 assert record == SRR['is_rest_9_10_1'][1]
147
148
149 def test_successful_get_one_record_NV():
150 register_responses([
151 ('GET', 'cluster', SRR['is_rest_9_10_1'])
152 ])
153 rest_api = create_restapi_object(DEFAULT_ARGS)
154 record, error = rest_generic.get_one_record(rest_api, 'cluster', query=None, fields='aaa,bbb')
155 assert error is None
156 assert record == SRR['is_rest_9_10_1'][1]
157
158
159 def test_successful_get_one_record_VN():
160 register_responses([
161 ('GET', 'cluster', SRR['is_rest_9_10_1'])
162 ])
163 rest_api = create_restapi_object(DEFAULT_ARGS)
164 record, error = rest_generic.get_one_record(rest_api, 'cluster', query={'aaa': 'value'}, fields=None)
165 assert error is None
166 assert record == SRR['is_rest_9_10_1'][1]
167
168
169 def test_successful_get_one_record_VV():
170 register_responses([
171 ('GET', 'cluster', SRR['is_rest_9_10_1'])
172 ])
173 rest_api = create_restapi_object(DEFAULT_ARGS)
174 record, error = rest_generic.get_one_record(rest_api, 'cluster', query={'aaa': 'value'}, fields='aaa,bbb')
175 assert error is None
176 assert record == SRR['is_rest_9_10_1'][1]
177
178
179 def test_error_get_one_record_empty():
180 register_responses([
181 ('GET', 'cluster', SRR['is_rest'])
182 ])
183 rest_api = create_restapi_object(DEFAULT_ARGS)
184 record, error = rest_generic.get_one_record(rest_api, 'cluster', query=None, fields=None)
185 assert error == 'calling: cluster: no response {}.'
186 assert record is None
187
188
189 def test_error_get_one_record_multiple():
190 register_responses([
191 ('GET', 'cluster', SRR['vservers_with_admin'])
192 ])
193 rest_api = create_restapi_object(DEFAULT_ARGS)
194 record, error = rest_generic.get_one_record(rest_api, 'cluster', query={'aaa': 'vvv'}, fields=None)
195 assert "calling: cluster: unexpected response {'records':" in error
196 assert "for query: {'aaa': 'vvv'}" in error
197 assert record == SRR['vservers_with_admin'][1]
198
199
200 def test_error_get_one_record_rest_error():
201 register_responses([
202 ('GET', 'cluster', SRR['generic_error'])
203 ])
204 rest_api = create_restapi_object(DEFAULT_ARGS)
205 record, error = rest_generic.get_one_record(rest_api, 'cluster', query=None, fields=None)
206 assert error == 'calling: cluster: got Expected error.'
207 assert record is None
208
209
210 def test_successful_get_0_or_more_records():
211 register_responses([
212 ('GET', 'cluster', SRR['vservers_with_admin'])
213 ])
214 rest_api = create_restapi_object(DEFAULT_ARGS)
215 records, error = rest_generic.get_0_or_more_records(rest_api, 'cluster')
216 assert error is None
217 assert records == SRR['vservers_with_admin'][1]['records']
218
219
220 def test_successful_get_0_or_more_records_NN():
221 register_responses([
222 ('GET', 'cluster', SRR['vservers_with_admin'])
223 ])
224 rest_api = create_restapi_object(DEFAULT_ARGS)
225 records, error = rest_generic.get_0_or_more_records(rest_api, 'cluster', query=None, fields=None)
226 assert error is None
227 assert records == SRR['vservers_with_admin'][1]['records']
228
229
230 def test_successful_get_0_or_more_records_NV():
231 register_responses([
232 ('GET', 'cluster', SRR['vservers_with_admin'])
233 ])
234 rest_api = create_restapi_object(DEFAULT_ARGS)
235 records, error = rest_generic.get_0_or_more_records(rest_api, 'cluster', query=None, fields='aaa,bbb')
236 assert error is None
237 assert records == SRR['vservers_with_admin'][1]['records']
238
239
240 def test_successful_get_0_or_more_records_VN():
241 register_responses([
242 ('GET', 'cluster', SRR['vservers_with_admin'])
243 ])
244 rest_api = create_restapi_object(DEFAULT_ARGS)
245 records, error = rest_generic.get_0_or_more_records(rest_api, 'cluster', query={'aaa': 'value'}, fields=None)
246 assert error is None
247 assert records == SRR['vservers_with_admin'][1]['records']
248
249
250 def test_successful_get_0_or_more_records_VV():
251 register_responses([
252 ('GET', 'cluster', SRR['vservers_with_admin'])
253 ])
254 rest_api = create_restapi_object(DEFAULT_ARGS)
255 records, error = rest_generic.get_0_or_more_records(rest_api, 'cluster', query={'aaa': 'value'}, fields='aaa,bbb')
256 assert error is None
257 assert records == SRR['vservers_with_admin'][1]['records']
258
259
260 def test_successful_get_0_or_more_records_VV_1_record():
261 register_responses([
262 ('GET', 'cluster', SRR['vservers_single'])
263 ])
264 rest_api = create_restapi_object(DEFAULT_ARGS)
265 records, error = rest_generic.get_0_or_more_records(rest_api, 'cluster', query={'aaa': 'value'}, fields='aaa,bbb')
266 assert error is None
267 assert records == SRR['vservers_single'][1]['records']
268
269
270 def test_successful_get_0_or_more_records_VV_0_record():
271 register_responses([
272 ('GET', 'cluster', SRR['zero_records'])
273 ])
274 rest_api = create_restapi_object(DEFAULT_ARGS)
275 records, error = rest_generic.get_0_or_more_records(rest_api, 'cluster', query={'aaa': 'value'}, fields='aaa,bbb')
276 assert error is None
277 assert records is None
278
279
280 def test_error_get_0_or_more_records():
281 register_responses([
282 ('GET', 'cluster', SRR['is_rest'])
283 ])
284 rest_api = create_restapi_object(DEFAULT_ARGS)
285 records, error = rest_generic.get_0_or_more_records(rest_api, 'cluster', query=None, fields=None)
286 assert error == 'calling: cluster: no response {}.'
287 assert records is None
288
289
290 def test_error_get_0_or_more_records_rest_error():
291 register_responses([
292 ('GET', 'cluster', SRR['generic_error'])
293 ])
294 rest_api = create_restapi_object(DEFAULT_ARGS)
295 records, error = rest_generic.get_0_or_more_records(rest_api, 'cluster', query=None, fields=None)
296 assert error == 'calling: cluster: got Expected error.'
297 assert records is None
298
299
300 def test_successful_post_async():
301 register_responses([
302 ('POST', 'cluster', SRR['vservers_single'])
303 ])
304 rest_api = create_restapi_object(DEFAULT_ARGS)
305 response, error = rest_generic.post_async(rest_api, 'cluster', {})
306 assert error is None
307 assert response == SRR['vservers_single'][1]
308
309
310 def test_error_post_async():
311 register_responses([
312 ('POST', 'cluster', SRR['generic_error'])
313 ])
314 rest_api = create_restapi_object(DEFAULT_ARGS)
315 response, error = rest_generic.post_async(rest_api, 'cluster', {})
316 assert error == 'calling: cluster: got Expected error.'
317 assert response is None
318
319
320 @patch('time.sleep')
321 def test_successful_post_async_with_job(dont_sleep):
322 register_responses([
323 ('POST', 'cluster', SRR['accepted_response']),
324 ('GET', 'cluster/jobs/d0b3eefe-cd59-11eb-a170-005056b338cd', SRR['job_in_progress']),
325 ('GET', 'cluster/jobs/d0b3eefe-cd59-11eb-a170-005056b338cd', SRR['job_in_progress']),
326 ('GET', 'cluster/jobs/d0b3eefe-cd59-11eb-a170-005056b338cd', SRR['job_success'])
327 ])
328 rest_api = create_restapi_object(DEFAULT_ARGS)
329 response, error = rest_generic.post_async(rest_api, 'cluster', {})
330 assert error is None
331 assert 'job_response' in response
332 assert response['job_response'] == 'success_message'
333
334
335 @patch('time.sleep')
336 def test_successful_post_async_with_job_failure(dont_sleep):
337 register_responses([
338 ('POST', 'cluster', SRR['accepted_response']),
339 ('GET', 'cluster/jobs/d0b3eefe-cd59-11eb-a170-005056b338cd', SRR['job_in_progress']),
340 ('GET', 'cluster/jobs/d0b3eefe-cd59-11eb-a170-005056b338cd', SRR['job_in_progress']),
341 ('GET', 'cluster/jobs/d0b3eefe-cd59-11eb-a170-005056b338cd', SRR['job_failed'])
342 ])
343 rest_api = create_restapi_object(DEFAULT_ARGS)
344 response, error = rest_generic.post_async(rest_api, 'cluster', {})
345 assert error is None
346 assert 'job_response' in response
347 assert response['job_response'] == 'error_message'
348
349
350 @patch('time.sleep')
351 def test_error_post_async_with_job(dont_sleep):
352 register_responses([
353 ('POST', 'cluster', SRR['accepted_response']),
354 ('GET', 'cluster/jobs/d0b3eefe-cd59-11eb-a170-005056b338cd', SRR['job_in_progress']),
355 ('GET', 'cluster/jobs/d0b3eefe-cd59-11eb-a170-005056b338cd', SRR['job_in_progress']),
356 ('GET', 'cluster/jobs/d0b3eefe-cd59-11eb-a170-005056b338cd', SRR['generic_error']),
357 ('GET', 'cluster/jobs/d0b3eefe-cd59-11eb-a170-005056b338cd', SRR['generic_error']),
358 ('GET', 'cluster/jobs/d0b3eefe-cd59-11eb-a170-005056b338cd', SRR['generic_error']),
359 ('GET', 'cluster/jobs/d0b3eefe-cd59-11eb-a170-005056b338cd', SRR['generic_error'])
360 ])
361 rest_api = create_restapi_object(DEFAULT_ARGS)
362 response, error = rest_generic.post_async(rest_api, 'cluster', {})
363 assert 'job reported error: Expected error - Expected error - Expected error - Expected error, received' in error
364 assert response == SRR['accepted_response'][1]
365
366
367 def test_successful_patch_async():
368 register_responses([
369 ('PATCH', 'cluster/uuid', SRR['vservers_single'])
370 ])
371 rest_api = create_restapi_object(DEFAULT_ARGS)
372 response, error = rest_generic.patch_async(rest_api, 'cluster', 'uuid', {})
373 assert error is None
374 assert response == SRR['vservers_single'][1]
375
376
377 def test_error_patch_async():
378 register_responses([
379 ('PATCH', 'cluster/uuid', SRR['generic_error'])
380 ])
381 rest_api = create_restapi_object(DEFAULT_ARGS)
382 response, error = rest_generic.patch_async(rest_api, 'cluster', 'uuid', {})
383 assert error == 'calling: cluster/uuid: got Expected error.'
384 assert response is None
385
386
387 @patch('time.sleep')
388 def test_successful_patch_async_with_job(dont_sleep):
389 register_responses([
390 ('PATCH', 'cluster/uuid', SRR['accepted_response']),
391 ('GET', 'cluster/jobs/d0b3eefe-cd59-11eb-a170-005056b338cd', SRR['job_in_progress']),
392 ('GET', 'cluster/jobs/d0b3eefe-cd59-11eb-a170-005056b338cd', SRR['job_success'])
393 ])
394 rest_api = create_restapi_object(DEFAULT_ARGS)
395 response, error = rest_generic.patch_async(rest_api, 'cluster', 'uuid', {})
396 assert error is None
397 assert 'job_response' in response
398 assert response['job_response'] == 'success_message'
399
400
401 @patch('time.sleep')
402 def test_successful_patch_async_with_job_failure(dont_sleep):
403 register_responses([
404 ('PATCH', 'cluster/uuid', SRR['accepted_response']),
405 ('GET', 'cluster/jobs/d0b3eefe-cd59-11eb-a170-005056b338cd', SRR['job_in_progress']),
406 ('GET', 'cluster/jobs/d0b3eefe-cd59-11eb-a170-005056b338cd', SRR['job_failed'])
407 ])
408 rest_api = create_restapi_object(DEFAULT_ARGS)
409 response, error = rest_generic.patch_async(rest_api, 'cluster', 'uuid', {})
410 assert error is None
411 assert 'job_response' in response
412 assert response['job_response'] == 'error_message'
413
414
415 @patch('time.sleep')
416 def test_error_patch_async_with_job(dont_sleep):
417 register_responses([
418 ('PATCH', 'cluster/uuid', SRR['accepted_response']),
419 ('GET', 'cluster/jobs/d0b3eefe-cd59-11eb-a170-005056b338cd', SRR['job_in_progress']),
420 ('GET', 'cluster/jobs/d0b3eefe-cd59-11eb-a170-005056b338cd', SRR['generic_error']),
421 ('GET', 'cluster/jobs/d0b3eefe-cd59-11eb-a170-005056b338cd', SRR['generic_error']),
422 ('GET', 'cluster/jobs/d0b3eefe-cd59-11eb-a170-005056b338cd', SRR['generic_error']),
423 ('GET', 'cluster/jobs/d0b3eefe-cd59-11eb-a170-005056b338cd', SRR['generic_error'])
424 ])
425 rest_api = create_restapi_object(DEFAULT_ARGS)
426 response, error = rest_generic.patch_async(rest_api, 'cluster', 'uuid', {})
427 assert 'job reported error: Expected error - Expected error - Expected error - Expected error, received' in error
428 assert response == SRR['accepted_response'][1]
429
430
431 def test_successful_delete_async():
432 register_responses([
433 ('DELETE', 'cluster/uuid', SRR['vservers_single'])
434 ])
435 rest_api = create_restapi_object(DEFAULT_ARGS)
436 response, error = rest_generic.delete_async(rest_api, 'cluster', 'uuid')
437 assert error is None
438 assert response == SRR['vservers_single'][1]
439
440
441 def test_error_delete_async():
442 register_responses([
443 ('DELETE', 'cluster/uuid', SRR['generic_error'])
444 ])
445 rest_api = create_restapi_object(DEFAULT_ARGS)
446 response, error = rest_generic.delete_async(rest_api, 'cluster', 'uuid')
447 assert error == 'calling: cluster/uuid: got Expected error.'
448 assert response is None
449
450
451 @patch('time.sleep')
452 def test_successful_delete_async_with_job(dont_sleep):
453 register_responses([
454 ('DELETE', 'cluster/uuid', SRR['accepted_response']),
455 ('GET', 'cluster/jobs/d0b3eefe-cd59-11eb-a170-005056b338cd', SRR['job_in_progress']),
456 ('GET', 'cluster/jobs/d0b3eefe-cd59-11eb-a170-005056b338cd', SRR['job_success'])
457 ])
458 rest_api = create_restapi_object(DEFAULT_ARGS)
459 response, error = rest_generic.delete_async(rest_api, 'cluster', 'uuid')
460 assert error is None
461 assert 'job_response' in response
462 assert response['job_response'] == 'success_message'
463
464
465 @patch('time.sleep')
466 def test_successful_delete_async_with_job_failure(dont_sleep):
467 register_responses([
468 ('DELETE', 'cluster/uuid', SRR['accepted_response']),
469 ('GET', 'cluster/jobs/d0b3eefe-cd59-11eb-a170-005056b338cd', SRR['job_in_progress']),
470 ('GET', 'cluster/jobs/d0b3eefe-cd59-11eb-a170-005056b338cd', SRR['job_failed'])
471 ])
472 rest_api = create_restapi_object(DEFAULT_ARGS)
473 response, error = rest_generic.delete_async(rest_api, 'cluster', 'uuid')
474 assert error is None
475 assert 'job_response' in response
476 assert response['job_response'] == 'error_message'
477
478
479 @patch('time.sleep')
480 def test_error_delete_async_with_job(dont_sleep):
481 register_responses([
482 ('DELETE', 'cluster/uuid', SRR['accepted_response']),
483 ('GET', 'cluster/jobs/d0b3eefe-cd59-11eb-a170-005056b338cd', SRR['job_in_progress']),
484 ('GET', 'cluster/jobs/d0b3eefe-cd59-11eb-a170-005056b338cd', SRR['generic_error']),
485 ('GET', 'cluster/jobs/d0b3eefe-cd59-11eb-a170-005056b338cd', SRR['generic_error']),
486 ('GET', 'cluster/jobs/d0b3eefe-cd59-11eb-a170-005056b338cd', SRR['generic_error']),
487 ('GET', 'cluster/jobs/d0b3eefe-cd59-11eb-a170-005056b338cd', SRR['generic_error'])
488 ])
489 rest_api = create_restapi_object(DEFAULT_ARGS)
490 response, error = rest_generic.delete_async(rest_api, 'cluster', 'uuid')
491 assert 'job reported error: Expected error - Expected error - Expected error - Expected error, received' in error
492 assert response == SRR['accepted_response'][1]