Debian Codegraph @

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

raw
1 # (c) 2018-2025, 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_snapshot '''
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\
15 call_main, create_and_apply, create_module, expect_and_capture_ansible_exception, patch_ansible
16 from ansible_collections.netapp.ontap.tests.unit.framework.mock_rest_and_zapi_requests import\
17 patch_request_and_invoke, register_responses
18 from ansible_collections.netapp.ontap.tests.unit.framework.rest_factory import rest_responses
19 from ansible_collections.netapp.ontap.tests.unit.framework.zapi_factory import build_zapi_response, zapi_responses
20
21
22 from ansible_collections.netapp.ontap.plugins.modules.na_ontap_snapshot \
23 import NetAppOntapSnapshot as my_module, main as my_main
24
25 # if not netapp_utils.has_netapp_lib():
26 # pytestmark = pytest.mark.skip('skipping as missing required netapp_lib')
27
28
29 SRR = rest_responses({
30 'volume_uuid': (200,
31 {'records': [{"uuid": "test_uuid"}], 'num_records': 1}, None,
32 ),
33 'snapshot_record': (200,
34 {'records': [{"volume": {"uuid": "d9cd4ec5-c96d-11eb-9271-005056b3ef5a",
35 "name": "ansible_vol"},
36 "uuid": "343b5227-8c6b-4e79-a133-304bbf7537ce",
37 "svm": {"uuid": "b663d6f0-c96d-11eb-9271-005056b3ef5a",
38 "name": "ansible"},
39 "name": "ss1",
40 "create_time": "2021-06-10T17:24:41-04:00",
41 "comment": "123",
42 "expiry_time": "2022-02-04T14:00:00-05:00",
43 "snapmirror_label": "321",
44 "snaplock_expiry_time": "2022-02-04T14:00:00-05:00", }], 'num_records': 1}, None),
45 'create_response': (200, {'job': {'uuid': 'd0b3eefe-cd59-11eb-a170-005056b338cd',
46 '_links': {
47 'self': {'href': '/api/cluster/jobs/d0b3eefe-cd59-11eb-a170-005056b338cd'}}}},
48 None),
49 'job_response': (200, {'uuid': 'e43a40db-cd61-11eb-a170-005056b338cd',
50 'description': 'PATCH /api/storage/volumes/d9cd4ec5-c96d-11eb-9271-005056b3ef5a/'
51 'snapshots/da995362-cd61-11eb-a170-005056b338cd',
52 'state': 'success',
53 'message': 'success',
54 'code': 0,
55 'start_time': '2021-06-14T18:43:08-04:00',
56 'end_time': '2021-06-14T18:43:08-04:00',
57 'svm': {'name': 'ansible', 'uuid': 'b663d6f0-c96d-11eb-9271-005056b3ef5a',
58 '_links': {'self': {'href': '/api/svm/svms/b663d6f0-c96d-11eb-9271-005056b3ef5a'}}},
59 '_links': {'self': {'href': '/api/cluster/jobs/e43a40db-cd61-11eb-a170-005056b338cd'}}},
60 None)
61 }, allow_override=False)
62
63
64 snapshot_info = {
65 'num-records': 1,
66 'attributes-list': {
67 'snapshot-info': {
68 'comment': 'new comment',
69 'name': 'ansible',
70 'snapmirror-label': 'label12'
71 }
72 }
73 }
74
75 ZRR = zapi_responses({
76 'get_snapshot': build_zapi_response(snapshot_info)
77 })
78
79
80 DEFAULT_ARGS = {
81 'state': 'present',
82 'hostname': 'hostname',
83 'username': 'username',
84 'password': 'password',
85 'vserver': 'vserver',
86 'comment': 'test comment',
87 'snapshot': 'test_snapshot',
88 'snapmirror_label': 'test_label',
89 'volume': 'test_vol'
90 }
91
92
93 def test_module_fail_when_required_args_missing():
94 ''' required arguments are reported as errors '''
95 error = create_module(my_module, fail=True)['msg']
96 assert 'missing required arguments:' in error
97 for arg in ('hostname', 'snapshot', 'volume', 'vserver'):
98 assert arg in error
99
100
101 @pytest.mark.skipif(not netapp_utils.has_netapp_lib(), reason="skipping as missing required netapp_lib")
102 def test_ensure_get_called():
103 ''' test get_snapshot() for non-existent snapshot'''
104 register_responses([
105 ('snapshot-get-iter', ZRR['empty']),
106 ])
107 module_args = {
108 'use_rest': 'never',
109 }
110 my_obj = create_module(my_module, DEFAULT_ARGS, module_args)
111 assert my_obj.get_snapshot() is None
112
113
114 @pytest.mark.skipif(not netapp_utils.has_netapp_lib(), reason="skipping as missing required netapp_lib")
115 def test_ensure_get_called_existing():
116 ''' test get_snapshot() for existing snapshot'''
117 register_responses([
118 ('snapshot-get-iter', ZRR['get_snapshot']),
119 ])
120 module_args = {
121 'use_rest': 'never',
122 }
123 my_obj = create_module(my_module, DEFAULT_ARGS, module_args)
124 assert my_obj.get_snapshot()
125
126
127 @pytest.mark.skipif(not netapp_utils.has_netapp_lib(), reason="skipping as missing required netapp_lib")
128 def test_successful_create():
129 ''' creating snapshot and testing idempotency '''
130 register_responses([
131 ('snapshot-get-iter', ZRR['empty']),
132 ('snapshot-create', ZRR['success']),
133 ])
134 module_args = {
135 'use_rest': 'never',
136 'async_bool': True
137 }
138 assert create_and_apply(my_module, 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_successful_modify():
143 ''' modifying snapshot and testing idempotency '''
144 register_responses([
145 ('snapshot-get-iter', ZRR['get_snapshot']),
146 ('snapshot-modify-iter', ZRR['success']),
147 ('snapshot-get-iter', ZRR['get_snapshot']),
148 ])
149 module_args = {
150 'use_rest': 'never',
151 'comment': 'adding comment',
152 'snapmirror_label': 'label22',
153 }
154 assert create_and_apply(my_module, DEFAULT_ARGS, module_args)['changed']
155 module_args = {
156 'use_rest': 'never',
157 'comment': 'new comment',
158 'snapmirror_label': 'label12',
159 }
160 assert not create_and_apply(my_module, DEFAULT_ARGS, module_args)['changed']
161
162
163 @pytest.mark.skipif(not netapp_utils.has_netapp_lib(), reason="skipping as missing required netapp_lib")
164 def test_successful_rename():
165 ''' modifying snapshot and testing idempotency '''
166 register_responses([
167 ('snapshot-get-iter', ZRR['empty']),
168 ('snapshot-get-iter', ZRR['get_snapshot']),
169 ('snapshot-rename', ZRR['success']),
170 ])
171 module_args = {
172 'use_rest': 'never',
173 'from_name': 'from_snapshot',
174 'comment': 'new comment',
175 'snapmirror_label': 'label12',
176 }
177 assert create_and_apply(my_module, DEFAULT_ARGS, module_args)['changed']
178
179
180 @pytest.mark.skipif(not netapp_utils.has_netapp_lib(), reason="skipping as missing required netapp_lib")
181 def test_successful_delete():
182 ''' deleting snapshot and testing idempotency '''
183 register_responses([
184 ('snapshot-get-iter', ZRR['get_snapshot']),
185 ('snapshot-delete', ZRR['success']),
186 ('snapshot-get-iter', ZRR['empty']),
187 ])
188 module_args = {
189 'use_rest': 'never',
190 'state': 'absent',
191 'ignore_owners': True,
192 'snapshot_instance_uuid': 'uuid',
193 }
194 assert create_and_apply(my_module, DEFAULT_ARGS, module_args)['changed']
195 assert not create_and_apply(my_module, DEFAULT_ARGS, module_args)['changed']
196
197
198 @pytest.mark.skipif(not netapp_utils.has_netapp_lib(), reason="skipping as missing required netapp_lib")
199 def test_if_all_methods_catch_exception():
200 register_responses([
201 ('snapshot-get-iter', ZRR['error']),
202 ('snapshot-create', ZRR['error']),
203 ('snapshot-delete', ZRR['error']),
204 ('snapshot-modify-iter', ZRR['error']),
205 ('snapshot-rename', ZRR['error']),
206 ('GET', 'cluster', SRR['is_rest_9_9_0']), # get version
207 ('GET', 'storage/volumes/None/snapshots', SRR['generic_error']),
208 ('POST', 'storage/volumes/None/snapshots', SRR['generic_error']),
209 ('DELETE', 'storage/volumes/None/snapshots/None', SRR['generic_error']),
210 ('PATCH', 'storage/volumes/None/snapshots/None', SRR['generic_error']),
211 ('GET', 'storage/volumes', SRR['generic_error'])
212 ])
213 module_args = {
214 'use_rest': 'never',
215 'from_name': 'from_snapshot'}
216 my_obj = create_module(my_module, DEFAULT_ARGS, module_args)
217 assert 'Error fetching snapshot' in expect_and_capture_ansible_exception(my_obj.get_snapshot, 'fail')['msg']
218 assert 'Error creating snapshot test_snapshot:' in expect_and_capture_ansible_exception(my_obj.create_snapshot, 'fail')['msg']
219 assert 'Error deleting snapshot test_snapshot:' in expect_and_capture_ansible_exception(my_obj.delete_snapshot, 'fail')['msg']
220 assert 'Error modifying snapshot test_snapshot:' in expect_and_capture_ansible_exception(my_obj.modify_snapshot, 'fail')['msg']
221 assert 'Error renaming snapshot from_snapshot to test_snapshot:' in expect_and_capture_ansible_exception(my_obj.rename_snapshot, 'fail')['msg']
222 module_args = {'use_rest': 'always'}
223 my_obj = create_module(my_module, DEFAULT_ARGS, module_args)
224 assert 'Error fetching snapshot' in expect_and_capture_ansible_exception(my_obj.get_snapshot, 'fail')['msg']
225 assert 'Error when creating snapshot:' in expect_and_capture_ansible_exception(my_obj.create_snapshot, 'fail')['msg']
226 assert 'Error when deleting snapshot:' in expect_and_capture_ansible_exception(my_obj.delete_snapshot, 'fail')['msg']
227 assert 'Error when modifying snapshot:' in expect_and_capture_ansible_exception(my_obj.modify_snapshot, 'fail')['msg']
228 assert 'Error getting volume info:' in expect_and_capture_ansible_exception(my_obj.get_volume_uuid, 'fail')['msg']
229
230
231 def test_module_fail_rest_ONTAP96():
232 register_responses([
233 ('GET', 'cluster', SRR['is_rest_96']) # get version
234 ])
235 module_args = {'use_rest': 'always'}
236 msg = 'Error: Minimum version of ONTAP for snapmirror_label is (9, 7)'
237 assert msg in create_module(my_module, DEFAULT_ARGS, module_args, fail=True)['msg']
238
239
240 def test_module_fail_rest_ONTAP9_15_1():
241 register_responses([
242 ('GET', 'cluster', SRR['is_rest_9_14_1']) # get version
243 ])
244 module_args = {'use_rest': 'always', 'snaplock_expiry_time': 'expiry'}
245 msg = 'Error: Minimum version of ONTAP for snaplock_expiry_time is (9, 15, 1)'
246 assert msg in create_module(my_module, DEFAULT_ARGS, module_args, fail=True)['msg']
247
248
249 def test_rest_successfully_create():
250 register_responses([
251 ('GET', 'cluster', SRR['is_rest_9_9_0']),
252 ('GET', 'storage/volumes', SRR['volume_uuid']),
253 ('GET', 'storage/volumes/test_uuid/snapshots', SRR['empty_records']),
254 ('POST', 'storage/volumes/test_uuid/snapshots', SRR['create_response']),
255 ('GET', 'cluster/jobs/d0b3eefe-cd59-11eb-a170-005056b338cd', SRR['job_response']),
256 ])
257 module_args = {
258 'use_rest': 'always',
259 'expiry_time': 'expiry'
260 }
261 assert create_and_apply(my_module, DEFAULT_ARGS, module_args)['changed']
262
263
264 def test_rest_successfully_create_with_snaplock_expiry_time():
265 register_responses([
266 ('GET', 'cluster', SRR['is_rest_9_15_1']),
267 ('GET', 'storage/volumes', SRR['volume_uuid']),
268 ('GET', 'storage/volumes/test_uuid/snapshots', SRR['empty_records']),
269 ('POST', 'storage/volumes/test_uuid/snapshots', SRR['create_response']),
270 ('GET', 'cluster/jobs/d0b3eefe-cd59-11eb-a170-005056b338cd', SRR['job_response']),
271 ])
272 module_args = {
273 'use_rest': 'always',
274 'expiry_time': 'expiry',
275 'snaplock_expiry_time': 'expiry'
276 }
277 assert create_and_apply(my_module, DEFAULT_ARGS, module_args)['changed']
278
279
280 def test_rest_error_create_no_volume():
281 register_responses([
282 ('GET', 'cluster', SRR['is_rest_9_9_0']),
283 ('GET', 'storage/volumes', SRR['empty_records']),
284 ])
285 module_args = {'use_rest': 'always'}
286 msg = 'Error: volume test_vol not found for vserver vserver.'
287 assert msg == create_and_apply(my_module, DEFAULT_ARGS, module_args, fail=True)['msg']
288
289
290 def test_rest_successfully_modify_with_snaplock_expiry_time():
291 register_responses([
292 ('GET', 'cluster', SRR['is_rest_9_15_1']),
293 ('GET', 'storage/volumes', SRR['volume_uuid']),
294 ('GET', 'storage/volumes/test_uuid/snapshots', SRR['snapshot_record']),
295 ('PATCH', 'storage/volumes/test_uuid/snapshots/343b5227-8c6b-4e79-a133-304bbf7537ce', SRR['create_response']), # modify
296 ('GET', 'cluster/jobs/d0b3eefe-cd59-11eb-a170-005056b338cd', SRR['job_response']),
297 ])
298 module_args = {
299 'use_rest': 'always',
300 'comment': 'new comment',
301 'expiry_time': 'expiry',
302 'snaplock_expiry_time': 'expiry'
303 }
304 assert create_and_apply(my_module, DEFAULT_ARGS, module_args)['changed']
305
306
307 def test_rest_successfully_modify():
308 register_responses([
309 ('GET', 'cluster', SRR['is_rest_9_9_0']),
310 ('GET', 'storage/volumes', SRR['volume_uuid']),
311 ('GET', 'storage/volumes/test_uuid/snapshots', SRR['snapshot_record']),
312 ('PATCH', 'storage/volumes/test_uuid/snapshots/343b5227-8c6b-4e79-a133-304bbf7537ce', SRR['create_response']), # modify
313 ('GET', 'cluster/jobs/d0b3eefe-cd59-11eb-a170-005056b338cd', SRR['job_response']),
314 ])
315 module_args = {
316 'use_rest': 'always',
317 'comment': 'new comment',
318 'expiry_time': 'expiry'
319 }
320 assert create_and_apply(my_module, DEFAULT_ARGS, module_args)['changed']
321
322
323 def test_rest_successfully_rename():
324 register_responses([
325 ('GET', 'cluster', SRR['is_rest_9_9_0']),
326 ('GET', 'storage/volumes', SRR['volume_uuid']),
327 ('GET', 'storage/volumes/test_uuid/snapshots', SRR['empty_records']),
328 ('GET', 'storage/volumes/test_uuid/snapshots', SRR['snapshot_record']),
329 ('PATCH', 'storage/volumes/test_uuid/snapshots/343b5227-8c6b-4e79-a133-304bbf7537ce', SRR['create_response']), # modify
330 ('GET', 'cluster/jobs/d0b3eefe-cd59-11eb-a170-005056b338cd', SRR['job_response']),
331 ])
332 module_args = {
333 'use_rest': 'always',
334 'from_name': 'old_snapshot'}
335 assert create_and_apply(my_module, DEFAULT_ARGS, module_args)['changed']
336
337
338 def test_rest_error_rename_from_not_found():
339 register_responses([
340 ('GET', 'cluster', SRR['is_rest_9_9_0']),
341 ('GET', 'storage/volumes', SRR['volume_uuid']),
342 ('GET', 'storage/volumes/test_uuid/snapshots', SRR['empty_records']),
343 ('GET', 'storage/volumes/test_uuid/snapshots', SRR['empty_records']),
344 ])
345 module_args = {
346 'use_rest': 'always',
347 'from_name': 'old_snapshot'}
348 msg = 'Error renaming snapshot: test_snapshot - no snapshot with from_name: old_snapshot.'
349 assert msg == create_and_apply(my_module, DEFAULT_ARGS, module_args, fail=True)['msg']
350
351
352 def test_rest_successfully_delete():
353 register_responses([
354 ('GET', 'cluster', SRR['is_rest_9_9_0']),
355 ('GET', 'storage/volumes', SRR['volume_uuid']),
356 ('GET', 'storage/volumes/test_uuid/snapshots', SRR['snapshot_record']),
357 ('DELETE', 'storage/volumes/test_uuid/snapshots/343b5227-8c6b-4e79-a133-304bbf7537ce', SRR['success']),
358 ])
359 module_args = {
360 'use_rest': 'always',
361 'state': 'absent'}
362 assert create_and_apply(my_module, DEFAULT_ARGS, module_args)['changed']
363
364
365 def test_rest_error_delete():
366 register_responses([
367 ('GET', 'cluster', SRR['is_rest_9_9_0']),
368 ('GET', 'storage/volumes', SRR['volume_uuid']),
369 ('GET', 'storage/volumes/test_uuid/snapshots', SRR['snapshot_record']),
370 ('DELETE', 'storage/volumes/test_uuid/snapshots/343b5227-8c6b-4e79-a133-304bbf7537ce', SRR['generic_error']),
371 ])
372 module_args = {
373 'use_rest': 'always',
374 'state': 'absent'}
375 msg = 'Error when deleting snapshot: calling: storage/volumes/test_uuid/snapshots/343b5227-8c6b-4e79-a133-304bbf7537ce: got Expected error.'
376 assert msg == create_and_apply(my_module, DEFAULT_ARGS, module_args, fail=True)['msg']
377
378
379 def test_call_main():
380 register_responses([
381 ('GET', 'cluster', SRR['is_rest_9_9_0']),
382 ('GET', 'storage/volumes', SRR['volume_uuid']),
383 ('GET', 'storage/volumes/test_uuid/snapshots', SRR['snapshot_record']),
384 ('DELETE', 'storage/volumes/test_uuid/snapshots/343b5227-8c6b-4e79-a133-304bbf7537ce', SRR['generic_error']),
385 ])
386 module_args = {
387 'use_rest': 'always',
388 'state': 'absent'}
389 msg = 'Error when deleting snapshot: calling: storage/volumes/test_uuid/snapshots/343b5227-8c6b-4e79-a133-304bbf7537ce: got Expected error.'
390 assert msg == call_main(my_main, DEFAULT_ARGS, module_args, fail=True)['msg']
391
392
393 def test_unsupported_options():
394 module_args = {
395 'use_rest': 'always',
396 'ignore_owners': True}
397 error = "REST API currently does not support 'ignore_owners'"
398 assert error == create_module(my_module, DEFAULT_ARGS, module_args, fail=True)['msg']
399 module_args = {
400 'use_rest': 'never',
401 'expiry_time': 'any'}
402 error = "expiry_time is currently only supported with REST on Ontap 9.6 or higher"
403 assert error == create_module(my_module, DEFAULT_ARGS, module_args, fail=True)['msg']
404
405
406 @patch('ansible_collections.netapp.ontap.plugins.module_utils.netapp.has_netapp_lib')
407 def test_missing_netapp_lib(mock_has_netapp_lib):
408 module_args = {
409 'use_rest': 'never',
410 }
411 mock_has_netapp_lib.return_value = False
412 msg = 'Error: the python NetApp-Lib module is required. Import error: '
413 assert msg in create_module(my_module, DEFAULT_ARGS, module_args, fail=True)['msg']