Debian Codegraph @

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

raw
1 # (c) 2018-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_cifs '''
5
6 from __future__ import (absolute_import, division, print_function)
7 __metaclass__ = type
8 import pytest
9
10 from ansible_collections.netapp.ontap.tests.unit.compat.mock import patch
11 import ansible_collections.netapp.ontap.plugins.module_utils.netapp as netapp_utils
12 # pylint: disable=unused-import
13 from ansible_collections.netapp.ontap.tests.unit.plugins.module_utils.ansible_mocks import\
14 patch_ansible, call_main, create_module, create_and_apply, expect_and_capture_ansible_exception
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.zapi_factory import build_zapi_response, zapi_responses
17 from ansible_collections.netapp.ontap.tests.unit.framework.rest_factory import rest_responses
18
19 from ansible_collections.netapp.ontap.plugins.modules.na_ontap_cifs \
20 import NetAppONTAPCifsShare as my_module, main as my_main # module under test
21
22 # if not netapp_utils.has_netapp_lib():
23 # pytestmark = pytest.mark.skip('skipping as missing required netapp_lib')
24
25 # REST API canned responses when mocking send_request
26 SRR = rest_responses({
27 # module specific responses
28 'cifs_record': (
29 200,
30 {
31 "records": [
32 {
33 "svm": {
34 "uuid": "671aa46e-11ad-11ec-a267-005056b30cfa",
35 "name": "ansibleSVM"
36 },
37 "name": 'cifs_share_name',
38 "path": '/',
39 "comment": 'CIFS share comment',
40 "unix_symlink": 'widelink',
41 "target": {
42 "name": "20:05:00:50:56:b3:0c:fa"
43 },
44 "access_based_enumeration": True,
45 "change_notify": True,
46 "encryption": False,
47 "home_directory": True,
48 "oplocks": False,
49 "continuously_available": True,
50 "offline_files": "manual",
51 "show_snapshot": True,
52 "namespace_caching": True,
53 "allow_unencrypted_access": True,
54 "browsable": True,
55 "show_previous_versions": True,
56 "vscan_profile": "no_scan"
57 }
58 ],
59 "num_records": 1
60 }, None
61 ),
62 "no_record": (
63 200,
64 {"num_records": 0},
65 None)
66 })
67
68 cifs_record_info = {
69 'num-records': 1,
70 'attributes-list': {
71 'cifs-share': {
72 'share-name': 'cifs_share_name',
73 'path': '/test',
74 'vscan-fileop-profile': 'standard',
75 'share-properties': [{'cifs-share-properties': 'browsable'}, {'cifs-share-properties': 'show_previous_versions'}],
76 'symlink-properties': [{'cifs-share-symlink-properties': 'enable'}]
77 }
78 }
79 }
80
81 ZRR = zapi_responses({
82 'cifs_record_info': build_zapi_response(cifs_record_info)
83 })
84
85 DEFAULT_ARGS = {
86 'hostname': 'test',
87 'username': 'admin',
88 'password': 'netapp1!',
89 'name': 'cifs_share_name',
90 'path': '/test',
91 'share_properties': ['browsable', 'show-previous-versions'],
92 'symlink_properties': 'enable',
93 'vscan_fileop_profile': 'standard',
94 'vserver': 'abc',
95 'use_rest': 'never'
96 }
97
98
99 def test_module_fail_when_required_args_missing():
100 ''' required arguments are reported as errors '''
101 error = 'missing required arguments:'
102 assert error in call_main(my_main, {}, fail=True)['msg']
103
104
105 @pytest.mark.skipif(not netapp_utils.has_netapp_lib(), reason="skipping as missing required netapp_lib")
106 def test_get():
107 register_responses([
108 ('cifs-share-get-iter', ZRR['cifs_record_info'])
109 ])
110 cifs_obj = create_module(my_module, DEFAULT_ARGS)
111 result = cifs_obj.get_cifs_share()
112 assert result
113
114
115 @pytest.mark.skipif(not netapp_utils.has_netapp_lib(), reason="skipping as missing required netapp_lib")
116 def test_error_create():
117 register_responses([
118 ('cifs-share-get-iter', ZRR['empty']),
119 ('cifs-share-create', ZRR['error']),
120 ])
121 module_args = {
122 'state': 'present'
123 }
124 error = create_and_apply(my_module, DEFAULT_ARGS, fail=True)['msg']
125 assert 'Error creating cifs-share' in error
126
127
128 @pytest.mark.skipif(not netapp_utils.has_netapp_lib(), reason="skipping as missing required netapp_lib")
129 def test_create():
130 register_responses([
131 ('cifs-share-get-iter', ZRR['empty']),
132 ('cifs-share-create', ZRR['success']),
133 ])
134 module_args = {
135 'state': 'present',
136 'comment': 'some_comment'
137 }
138 assert call_main(my_main, DEFAULT_ARGS, module_args)['changed']
139
140
141 @pytest.mark.skipif(not netapp_utils.has_netapp_lib(), reason="skipping as missing required netapp_lib")
142 def test_delete():
143 register_responses([
144 ('cifs-share-get-iter', ZRR['cifs_record_info']),
145 ('cifs-share-delete', ZRR['success']),
146 ])
147 module_args = {
148 'state': 'absent'
149 }
150 assert create_and_apply(my_module, DEFAULT_ARGS, module_args)['changed']
151
152
153 @pytest.mark.skipif(not netapp_utils.has_netapp_lib(), reason="skipping as missing required netapp_lib")
154 def test_error_delete():
155 register_responses([
156 ('cifs-share-get-iter', ZRR['cifs_record_info']),
157 ('cifs-share-delete', ZRR['error']),
158 ])
159 module_args = {
160 'state': 'absent'
161 }
162 error = create_and_apply(my_module, DEFAULT_ARGS, module_args, fail=True)['msg']
163 assert 'Error deleting cifs-share' in error
164
165
166 @pytest.mark.skipif(not netapp_utils.has_netapp_lib(), reason="skipping as missing required netapp_lib")
167 def test_modify_path():
168 register_responses([
169 ('cifs-share-get-iter', ZRR['cifs_record_info']),
170 ('cifs-share-modify', ZRR['success']),
171 ])
172 module_args = {
173 'path': '//'
174 }
175 assert create_and_apply(my_module, DEFAULT_ARGS, module_args)['changed']
176
177
178 @pytest.mark.skipif(not netapp_utils.has_netapp_lib(), reason="skipping as missing required netapp_lib")
179 def test_modify_comment():
180 register_responses([
181 ('cifs-share-get-iter', ZRR['cifs_record_info']),
182 ('cifs-share-modify', ZRR['success']),
183 ])
184 module_args = {
185 'comment': 'cifs modify'
186 }
187 assert create_and_apply(my_module, DEFAULT_ARGS, module_args)['changed']
188
189
190 @pytest.mark.skipif(not netapp_utils.has_netapp_lib(), reason="skipping as missing required netapp_lib")
191 def test_modify_share_properties():
192 register_responses([
193 ('cifs-share-get-iter', ZRR['cifs_record_info']),
194 ('cifs-share-modify', ZRR['success']),
195 ])
196 module_args = {
197 'share_properties': 'oplocks'
198 }
199 assert create_and_apply(my_module, DEFAULT_ARGS, module_args)['changed']
200
201
202 @pytest.mark.skipif(not netapp_utils.has_netapp_lib(), reason="skipping as missing required netapp_lib")
203 def test_modify_symlink_properties():
204 register_responses([
205 ('cifs-share-get-iter', ZRR['cifs_record_info']),
206 ('cifs-share-modify', ZRR['success']),
207 ])
208 module_args = {
209 'symlink_properties': 'read_only'
210 }
211 assert create_and_apply(my_module, DEFAULT_ARGS, module_args)['changed']
212
213
214 @pytest.mark.skipif(not netapp_utils.has_netapp_lib(), reason="skipping as missing required netapp_lib")
215 def test_modify_vscan_fileop_profile():
216 register_responses([
217 ('cifs-share-get-iter', ZRR['cifs_record_info']),
218 ('cifs-share-modify', ZRR['success']),
219 ])
220 module_args = {
221 'vscan_fileop_profile': 'strict'
222 }
223 assert create_and_apply(my_module, DEFAULT_ARGS, module_args)['changed']
224
225
226 @pytest.mark.skipif(not netapp_utils.has_netapp_lib(), reason="skipping as missing required netapp_lib")
227 def test_error_modify():
228 register_responses([
229 ('cifs-share-get-iter', ZRR['cifs_record_info']),
230 ('cifs-share-modify', ZRR['error']),
231 ])
232 module_args = {
233 'symlink_properties': 'read'
234 }
235 error = create_and_apply(my_module, DEFAULT_ARGS, module_args, fail=True)['msg']
236 assert 'Error modifying cifs-share' in error
237
238
239 @pytest.mark.skipif(not netapp_utils.has_netapp_lib(), reason="skipping as missing required netapp_lib")
240 def test_create_idempotency():
241 register_responses([
242 ('cifs-share-get-iter', ZRR['cifs_record_info'])
243 ])
244 assert create_and_apply(my_module, DEFAULT_ARGS)['changed'] is False
245
246
247 @pytest.mark.skipif(not netapp_utils.has_netapp_lib(), reason="skipping as missing required netapp_lib")
248 def test_delete_idempotency():
249 register_responses([
250 ('cifs-share-get-iter', ZRR['empty'])
251 ])
252 module_args = {'state': 'absent'}
253 assert create_and_apply(my_module, DEFAULT_ARGS, module_args)['changed'] is False
254
255
256 @pytest.mark.skipif(not netapp_utils.has_netapp_lib(), reason="skipping as missing required netapp_lib")
257 def test_if_all_methods_catch_exception():
258 register_responses([
259 ('cifs-share-create', ZRR['error']),
260 ('cifs-share-modify', ZRR['error']),
261 ('cifs-share-delete', ZRR['error'])
262 ])
263 module_args = {}
264
265 my_obj = create_module(my_module, DEFAULT_ARGS, module_args)
266
267 error = expect_and_capture_ansible_exception(my_obj.create_cifs_share, 'fail')['msg']
268 assert 'Error creating cifs-share cifs_share_name: NetApp API failed. Reason - 12345:synthetic error for UT purpose' in error
269
270 error = expect_and_capture_ansible_exception(my_obj.modify_cifs_share, 'fail')['msg']
271 assert 'Error modifying cifs-share cifs_share_name: NetApp API failed. Reason - 12345:synthetic error for UT purpose' in error
272
273 error = expect_and_capture_ansible_exception(my_obj.delete_cifs_share, 'fail')['msg']
274 assert 'Error deleting cifs-share cifs_share_name: NetApp API failed. Reason - 12345:synthetic error for UT purpose' in error
275
276
277 ARGS_REST = {
278 'hostname': 'test',
279 'username': 'test_user',
280 'password': 'test_pass!',
281 'use_rest': 'always',
282 'vserver': 'test_vserver',
283 'name': 'cifs_share_name',
284 'path': '/',
285 'unix_symlink': 'widelink',
286 }
287
288
289 def test_options_support():
290 ''' test option support '''
291 register_responses([
292 ('GET', 'cluster', SRR['is_rest'])
293 ])
294 module_args = {
295 'show_snapshot': True,
296 'allow_unencrypted_access': True,
297 'browsable': True
298 }
299 error = 'Error: Minimum version of ONTAP'
300 assert error in create_module(my_module, ARGS_REST, module_args, fail=True)['msg']
301
302
303 def test_rest_successful_create():
304 '''Test successful rest create'''
305 register_responses([
306 ('GET', 'cluster', SRR['is_rest']),
307 ('GET', 'protocols/cifs/shares', SRR['empty_records']),
308 ('POST', 'protocols/cifs/shares', SRR['empty_good']),
309 ])
310 module_args = {
311 'comment': 'CIFS share comment',
312 'unix_symlink': 'disable'
313 }
314 assert create_and_apply(my_module, ARGS_REST, module_args)
315
316
317 def test_delete_rest():
318 ''' Test delete with rest API'''
319 register_responses([
320 ('GET', 'cluster', SRR['is_rest']),
321 ('GET', 'protocols/cifs/shares', SRR['cifs_record']),
322 ('DELETE', 'protocols/cifs/shares/671aa46e-11ad-11ec-a267-005056b30cfa', SRR['empty_good']),
323 ])
324 module_args = {
325 'state': 'absent',
326 }
327 assert create_and_apply(my_module, ARGS_REST, module_args)
328
329
330 def test_rest_error_get():
331 '''Test error rest get'''
332 register_responses([
333 ('GET', 'cluster', SRR['is_rest']),
334 ('GET', 'protocols/cifs/shares', SRR['generic_error']),
335 ])
336 error = create_and_apply(my_module, ARGS_REST, fail=True)['msg']
337 assert 'Error on fetching cifs shares: calling: protocols/cifs/shares: got Expected error.' in error
338
339
340 def test_rest_error_create():
341 '''Test error rest create'''
342 register_responses([
343 ('GET', 'cluster', SRR['is_rest']),
344 ('GET', 'protocols/cifs/shares', SRR['empty_records']),
345 ('POST', 'protocols/cifs/shares', SRR['generic_error']),
346 ])
347 error = create_and_apply(my_module, ARGS_REST, fail=True)['msg']
348 assert 'Error on creating cifs shares:' in error
349
350
351 def test_error_delete_rest():
352 ''' Test error delete with rest API'''
353 register_responses([
354 ('GET', 'cluster', SRR['is_rest']),
355 ('GET', 'protocols/cifs/shares', SRR['cifs_record']),
356 ('DELETE', 'protocols/cifs/shares/671aa46e-11ad-11ec-a267-005056b30cfa', SRR['generic_error']),
357 ])
358 module_args = {
359 'state': 'absent'
360 }
361 error = create_and_apply(my_module, ARGS_REST, module_args, fail=True)['msg']
362 assert 'Error on deleting cifs shares:' in error
363
364
365 def test_modify_cifs_share_path():
366 ''' test modify CIFS share path '''
367 register_responses([
368 ('GET', 'cluster', SRR['is_rest']),
369 ('GET', 'protocols/cifs/shares', SRR['cifs_record']),
370 ('PATCH', 'protocols/cifs/shares/671aa46e-11ad-11ec-a267-005056b30cfa/cifs_share_name', SRR['empty_good']),
371 ])
372 module_args = {
373 'path': "\\vol1"
374 }
375 assert create_and_apply(my_module, ARGS_REST, module_args)
376
377
378 def test_modify_cifs_share_comment():
379 ''' test modify CIFS share comment '''
380 register_responses([
381 ('GET', 'cluster', SRR['is_rest']),
382 ('GET', 'protocols/cifs/shares', SRR['cifs_record']),
383 ('PATCH', 'protocols/cifs/shares/671aa46e-11ad-11ec-a267-005056b30cfa/cifs_share_name', SRR['empty_good']),
384 ])
385 module_args = {
386 'comment': "cifs comment modify"
387 }
388 assert create_and_apply(my_module, ARGS_REST, module_args)
389
390
391 def test_modify_cifs_share_properties():
392 ''' test modify CIFS share properties '''
393 register_responses([
394 ('GET', 'cluster', SRR['is_rest']),
395 ('GET', 'protocols/cifs/shares', SRR['cifs_record']),
396 ('PATCH', 'protocols/cifs/shares/671aa46e-11ad-11ec-a267-005056b30cfa/cifs_share_name', SRR['empty_good']),
397 ])
398 module_args = {
399 'unix_symlink': "disable"
400 }
401 assert create_and_apply(my_module, ARGS_REST, module_args)
402
403
404 def test_modify_cifs_share_properties_2():
405 ''' test modify CIFS share properties '''
406 register_responses([
407 ('GET', 'cluster', SRR['is_rest_9_13_1']),
408 ('GET', 'protocols/cifs/shares', SRR['cifs_record']),
409 ('PATCH', 'protocols/cifs/shares/671aa46e-11ad-11ec-a267-005056b30cfa/cifs_share_name', SRR['empty_good']),
410 ])
411 module_args = {
412 "access_based_enumeration": False,
413 "change_notify": False,
414 "encryption": True,
415 "oplocks": True,
416 "continuously_available": False,
417 "show_snapshot": False,
418 "namespace_caching": False,
419 "allow_unencrypted_access": False,
420 "browsable": False,
421 "show_previous_versions": False
422 }
423 assert create_and_apply(my_module, ARGS_REST, module_args)
424
425
426 def test_modify_cifs_offline_files():
427 ''' test modify CIFS offline_files '''
428 register_responses([
429 ('GET', 'cluster', SRR['is_rest_9_13_1']),
430 ('GET', 'protocols/cifs/shares', SRR['cifs_record']),
431 ('PATCH', 'protocols/cifs/shares/671aa46e-11ad-11ec-a267-005056b30cfa/cifs_share_name', SRR['empty_good']),
432 ])
433 module_args = {
434 "offline_files": "none"
435 }
436 assert create_and_apply(my_module, ARGS_REST, module_args)
437
438
439 def test_modify_cifs_vscan_fileop_profile():
440 ''' test modify CIFS vscan_fileop_profile '''
441 register_responses([
442 ('GET', 'cluster', SRR['is_rest_9_15_1']),
443 ('GET', 'protocols/cifs/shares', SRR['cifs_record']),
444 ('PATCH', 'protocols/cifs/shares/671aa46e-11ad-11ec-a267-005056b30cfa/cifs_share_name', SRR['empty_good']),
445 ])
446 module_args = {
447 "vscan_fileop_profile": "standard"
448 }
449 assert create_and_apply(my_module, ARGS_REST, module_args)
450
451
452 def test_version_error_offline_files():
453 ''' test version error for offline_files '''
454 register_responses([
455 ('GET', 'cluster', SRR['is_rest'])
456 ])
457 module_args = {
458 "offline_files": "none"
459 }
460 error = create_module(my_module, ARGS_REST, module_args, fail=True)['msg']
461 assert 'Minimum version of ONTAP for offline_files is (9, 10, 1)' in error
462
463
464 def test_version_error_vscan_fileop_profile():
465 ''' test version error for vscan_fileop_profile '''
466 register_responses([
467 ('GET', 'cluster', SRR['is_rest'])
468 ])
469 module_args = {
470 "vscan_fileop_profile": "standard"
471 }
472 error = create_module(my_module, ARGS_REST, module_args, fail=True)['msg']
473 assert 'Minimum version of ONTAP for vscan_fileop_profile is (9, 15, 1)' in error
474
475
476 def test_error_modify_cifs_share_path():
477 ''' test modify CIFS share path error'''
478 register_responses([
479 ('GET', 'cluster', SRR['is_rest']),
480 ('GET', 'protocols/cifs/shares', SRR['cifs_record']),
481 ('PATCH', 'protocols/cifs/shares/671aa46e-11ad-11ec-a267-005056b30cfa/cifs_share_name', SRR['generic_error']),
482 ])
483 module_args = {
484 'path': "\\vol1"
485 }
486 error = create_and_apply(my_module, ARGS_REST, module_args, fail=True)['msg']
487 assert 'Error on modifying cifs shares:' in error
488
489
490 def test_error_modify_cifs_share_comment():
491 ''' test modify CIFS share comment error'''
492 register_responses([
493 ('GET', 'cluster', SRR['is_rest']),
494 ('GET', 'protocols/cifs/shares', SRR['cifs_record']),
495 ('PATCH', 'protocols/cifs/shares/671aa46e-11ad-11ec-a267-005056b30cfa/cifs_share_name', SRR['generic_error']),
496 ])
497 module_args = {
498 'comment': "cifs comment modify"
499 }
500 error = create_and_apply(my_module, ARGS_REST, module_args, fail=True)['msg']
501 assert 'Error on modifying cifs shares:' in error
502
503
504 def test_rest_successful_create_idempotency():
505 '''Test successful rest create'''
506 register_responses([
507 ('GET', 'cluster', SRR['is_rest']),
508 ('GET', 'protocols/cifs/shares', SRR['cifs_record'])
509 ])
510 module_args = {
511 'use_rest': 'always'
512 }
513 assert create_and_apply(my_module, ARGS_REST, module_args)['changed'] is False
514
515
516 def test_rest_successful_delete_idempotency():
517 '''Test successful rest delete'''
518 register_responses([
519 ('GET', 'cluster', SRR['is_rest']),
520 ('GET', 'protocols/cifs/shares', SRR['empty_records'])
521 ])
522 module_args = {'use_rest': 'always', 'state': 'absent'}
523 assert create_and_apply(my_module, ARGS_REST, module_args)['changed'] is False
524
525
526 @patch('ansible_collections.netapp.ontap.plugins.module_utils.netapp.has_netapp_lib')
527 def test_missing_netapp_lib(mock_has_netapp_lib):
528 mock_has_netapp_lib.return_value = False
529 msg = 'Error: the python NetApp-Lib module is required. Import error:'
530 assert msg in call_main(my_main, DEFAULT_ARGS, fail=True)['msg']