Debian Codegraph @

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

raw
1 # (c) 2021, NetApp, Inc
2 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
3
4 ''' unit test for ONTAP disks Ansible module '''
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 set_module_args, \
14 AnsibleFailJson, AnsibleExitJson, patch_ansible
15
16 from ansible_collections.netapp.ontap.plugins.modules.na_ontap_partitions \
17 import NetAppOntapPartitions as my_module # module under test
18
19 if not netapp_utils.has_netapp_lib():
20 pytestmark = pytest.mark.skip('skipping as missing required netapp_lib')
21
22
23 def default_args():
24 args = {
25 'disk_type': 'SAS',
26 'partitioning_method': 'root_data',
27 'partition_type': 'data',
28 'partition_count': 13,
29 'hostname': '10.10.10.10',
30 'username': 'username',
31 'password': 'password',
32 'node': 'node1',
33 'use_rest': 'always'
34 }
35 return args
36
37
38 # REST API canned responses when mocking send_request
39 SRR = {
40 # common responses
41 'is_rest': (200, dict(version=dict(generation=9, major=9, minor=0, full='dummy')), None),
42 'is_rest_9_8': (200, dict(version=dict(generation=9, major=8, minor=0, full='dummy')), None),
43 'is_zapi': (400, {}, "Unreachable"),
44 'empty_good': (200, {}, None),
45 'zero_record': (200, dict(records=[], num_records=0), None),
46 'one_record_uuid': (200, dict(records=[dict(uuid='a1b2c3')], num_records=1), None),
47 'end_of_sequence': (500, None, "Unexpected call to send_request"),
48 'generic_error': (400, None, "Expected error"),
49 'owned_partitions_record': (200, {
50 "records": [
51 {
52 "partition": "1.0.0.P1",
53 "container_type": "spare",
54 "partitioning_method": "root_data",
55 "is_root": False,
56 "disk_type": "sas",
57 "home_node_name": "fas2552-rtp-13-02",
58 "owner_node_name": "fas2552-rtp-13-02"
59 },
60 {
61 "partition": "1.0.2.P1",
62 "container_type": "spare",
63 "partitioning_method": "root_data",
64 "is_root": False,
65 "disk_type": "sas",
66 "home_node_name": "fas2552-rtp-13-02",
67 "owner_node_name": "fas2552-rtp-13-02"
68 },
69 {
70 "partition": "1.0.4.P1",
71 "container_type": "spare",
72 "partitioning_method": "root_data",
73 "is_root": False,
74 "disk_type": "sas",
75 "home_node_name": "fas2552-rtp-13-02",
76 "owner_node_name": "fas2552-rtp-13-02"
77 }
78 ],
79 "num_records": 3
80 }, None),
81
82 'unassigned_partitions_record': (200, {
83 "records": [
84 {
85 "partition": "1.0.25.P1",
86 "container_type": "spare",
87 "partitioning_method": "root_data",
88 "is_root": False,
89 "disk_type": "sas",
90 "home_node_name": "fas2552-rtp-13-02",
91 "owner_node_name": "fas2552-rtp-13-02"
92 },
93 {
94 "partition": "1.0.27.P1",
95 "container_type": "spare",
96 "partitioning_method": "root_data",
97 "is_root": False,
98 "disk_type": "sas",
99 "home_node_name": "fas2552-rtp-13-02",
100 "owner_node_name": "fas2552-rtp-13-02"
101 },
102 ],
103 "num_records": 2
104 }, None),
105
106 'unassigned_disks_record': (200, {
107 "records": [
108 {
109 'name': '1.0.27',
110 'type': 'sas',
111 'container_type': 'unassigned',
112 'home_node': {'name': 'node1'}},
113 {
114 'name': '1.0.28',
115 'type': 'sas',
116 'container_type': 'unassigned',
117 'home_node': {'name': 'node1'}}
118 ],
119 'num_records': 2}, None),
120
121 'home_spare_disk_info_record': (200, {
122 'records': [],
123 'num_records': 2}, None),
124
125 'spare_partitions_record': (200, {
126 "records": [
127 {
128 "partition": "1.0.0.P1",
129 "container_type": "spare",
130 "partitioning_method": "root_data",
131 "is_root": False,
132 "disk_type": "sas",
133 "home_node_name": "fas2552-rtp-13-02",
134 "owner_node_name": "fas2552-rtp-13-02"
135 },
136 {
137 "partition": "1.0.1.P1",
138 "container_type": "spare",
139 "partitioning_method": "root_data",
140 "is_root": False,
141 "disk_type": "sas",
142 "home_node_name": "fas2552-rtp-13-02",
143 "owner_node_name": "fas2552-rtp-13-02"
144 }
145 ], 'num_records': 2
146 }, None),
147
148 'partner_spare_partitions_record': (200, {
149 "records": [
150 {
151 "partition": "1.0.1.P1",
152 "container_type": "spare",
153 "partitioning_method": "root_data",
154 "is_root": False,
155 "disk_type": "sas",
156 "home_node_name": "node2",
157 "owner_node_name": "node2"
158 },
159 {
160 "partition": "1.0.3.P1",
161 "container_type": "spare",
162 "partitioning_method": "root_data",
163 "is_root": False,
164 "disk_type": "sas",
165 "home_node_name": "node2",
166 "owner_node_name": "node2"
167 },
168 {
169 "partition": "1.0.5.P1",
170 "container_type": "spare",
171 "partitioning_method": "root_data",
172 "is_root": False,
173 "disk_type": "sas",
174 "home_node_name": "node2",
175 "owner_node_name": "node2"
176 },
177 {
178 "partition": "1.0.23.P1",
179 "container_type": "spare",
180 "partitioning_method": "root_data",
181 "is_root": False,
182 "disk_type": "sas",
183 "home_node_name": "node2",
184 "owner_node_name": "node2"
185 }
186 ], "num_records": 4
187 }, None),
188
189 'partner_node_name_record': (200, {
190 'records': [
191 {
192 'uuid': 'c345c182-a6a0-11eb-af7b-00a0984839de',
193 'name': 'node2',
194 'ha': {
195 'partners': [
196 {'name': 'node1'}
197 ]
198 }
199 }
200 ], 'num_records': 1
201 }, None),
202
203 'partner_spare_disks_record': (200, {
204 'records': [
205 {
206 'name': '1.0.22',
207 'type': 'sas',
208 'container_type': 'spare',
209 'home_node': {'name': 'node2'}
210 },
211 {
212 'name': '1.0.20',
213 'type': 'sas',
214 'container_type': 'spare',
215 'home_node': {'name': 'node2'}
216 },
217 {
218 'name': '1.0.18',
219 'type': 'sas',
220 'container_type': 'spare',
221 'home_node': {'name': 'node2'}
222 },
223 {
224 'name': '1.0.16',
225 'type': 'sas',
226 'container_type': 'spare',
227 'home_node': {'name': 'node2'}
228 }
229 ], 'num_records': 4
230 }, None),
231
232 'adp2_owned_partitions_record': (200, {
233 "records": [
234 {
235 "partition": "1.0.0.P1",
236 "container_type": "spare",
237 "partitioning_method": "root_data1_data2",
238 "is_root": False,
239 "disk_type": "ssd",
240 "home_node_name": "aff300-rtp-2b",
241 "owner_node_name": "aff300-rtp-2b"
242 },
243 {
244 "partition": "1.0.1.P1",
245 "container_type": "spare",
246 "partitioning_method": "root_data1_data2",
247 "is_root": False,
248 "disk_type": "ssd",
249 "home_node_name": "aff300-rtp-2b",
250 "owner_node_name": "aff300-rtp-2b"
251 },
252 {
253 "partition": "1.0.23.P1",
254 "container_type": "spare",
255 "partitioning_method": "root_data1_data2",
256 "is_root": False,
257 "disk_type": "ssd",
258 "home_node_name": "aff300-rtp-2b",
259 "owner_node_name": "aff300-rtp-2b"
260 }
261 ], "num_records": 3
262 }, None),
263 }
264
265
266 def test_rest_missing_arguments(patch_ansible): # pylint: disable=redefined-outer-name,unused-argument ##WHAT DOES THIS METHOD DO
267 ''' create scope '''
268 args = dict(default_args())
269 del args['hostname']
270 set_module_args(args)
271 with pytest.raises(AnsibleFailJson) as exc:
272 my_module()
273 msg = 'missing required arguments: hostname'
274 assert exc.value.args[0]['msg'] == msg
275
276
277 # get unassigned partitions
278 @patch('ansible_collections.netapp.ontap.plugins.module_utils.netapp.OntapRestAPI.send_request')
279 def test_rest_assign_unassigned_disks(mock_request, patch_ansible): # pylint: disable=redefined-outer-name,unused-argument
280 ''' Steal disks from partner node and assign them to the requested node '''
281 args = dict(default_args())
282 args['partition_count'] = 5
283 set_module_args(args)
284 mock_request.side_effect = [
285 SRR['is_rest'],
286 SRR['owned_partitions_record'],
287 SRR['home_spare_disk_info_record'],
288 SRR['unassigned_partitions_record'],
289 SRR['unassigned_disks_record'],
290 SRR['partner_node_name_record'],
291 SRR['partner_spare_partitions_record'],
292 SRR['partner_spare_disks_record'],
293 SRR['empty_good'], # assign
294 SRR['end_of_sequence']
295 ]
296 my_obj = my_module()
297 with pytest.raises(AnsibleExitJson) as exc:
298 my_obj.apply()
299 assert exc.value.args[0]['changed'] is True
300 print(mock_request.mock_calls)
301 assert len(mock_request.mock_calls) == 6
302
303
304 # assign unassigned partitions + steal 2 partner spare partitions
305 @patch('ansible_collections.netapp.ontap.plugins.module_utils.netapp.OntapRestAPI.send_request')
306 def test_rest_assign_unassigned_and_partner_spare_disks(mock_request, patch_ansible): # pylint: disable=redefined-outer-name,unused-argument
307 ''' Steal disks from partner node and assign them to the requested node '''
308 args = dict(default_args())
309 args['partition_count'] = 7
310 set_module_args(args)
311 mock_request.side_effect = [
312 SRR['is_rest'],
313 SRR['owned_partitions_record'],
314 SRR['home_spare_disk_info_record'],
315 SRR['unassigned_partitions_record'],
316 SRR['unassigned_disks_record'],
317 SRR['partner_node_name_record'],
318 SRR['partner_spare_partitions_record'],
319 SRR['partner_spare_disks_record'],
320 SRR['empty_good'], # unassign
321 SRR['empty_good'], # assign
322 SRR['end_of_sequence']
323 ]
324 my_obj = my_module()
325 with pytest.raises(AnsibleExitJson) as exc:
326 my_obj.apply()
327 assert exc.value.args[0]['changed'] is True
328 print(mock_request.mock_calls)
329 assert len(mock_request.mock_calls) == 9
330
331
332 # assign unassigned partitions + steal 2 partner spare partitions
333 @patch('ansible_collections.netapp.ontap.plugins.module_utils.netapp.OntapRestAPI.send_request')
334 def test_rest_assign_unassigned_and_partner_spare_partitions_and_disks(mock_request, patch_ansible): # pylint: disable=redefined-outer-name,unused-argument
335 ''' Steal disks from partner node and assign them to the requested node '''
336 args = dict(default_args())
337 args['partition_count'] = 6
338 set_module_args(args)
339 mock_request.side_effect = [
340 SRR['is_rest'],
341 SRR['owned_partitions_record'],
342 SRR['home_spare_disk_info_record'],
343 SRR['unassigned_partitions_record'],
344 SRR['unassigned_disks_record'],
345 SRR['partner_node_name_record'],
346 SRR['partner_spare_partitions_record'],
347 SRR['partner_spare_disks_record'],
348 SRR['empty_good'], # unassign
349 SRR['empty_good'], # assign
350 SRR['end_of_sequence']
351 ]
352 my_obj = my_module()
353 with pytest.raises(AnsibleExitJson) as exc:
354 my_obj.apply()
355 assert exc.value.args[0]['changed'] is True
356 print(mock_request.mock_calls)
357 assert len(mock_request.mock_calls) == 8
358
359
360 # Should unassign partitions
361 @patch('ansible_collections.netapp.ontap.plugins.module_utils.netapp.OntapRestAPI.send_request')
362 def test_rest_unassign(mock_request, patch_ansible): # pylint: disable=redefined-outer-name,unused-argument
363 ''' Steal disks from partner node and assign them to the requested node '''
364 args = dict(default_args())
365 args['partition_count'] = 2 # change this number to be less than currently assigned partions to the node
366 set_module_args(args)
367 mock_request.side_effect = [
368 SRR['is_rest'],
369 SRR['owned_partitions_record'],
370 SRR['unassigned_partitions_record'],
371 SRR['spare_partitions_record'],
372 SRR['empty_good'], # unassign
373 SRR['end_of_sequence']
374 ]
375 my_obj = my_module()
376 with pytest.raises(AnsibleExitJson) as exc:
377 my_obj.apply()
378 assert exc.value.args[0]['changed'] is True
379 print(mock_request.mock_calls)
380 assert len(mock_request.mock_calls) == 5
381
382
383 @patch('ansible_collections.netapp.ontap.plugins.module_utils.netapp.OntapRestAPI.send_request')
384 def test_rest_no_action(mock_request, patch_ansible): # pylint: disable=redefined-outer-name,unused-argument
385 ''' disk_count matches arguments, do nothing '''
386 args = dict(default_args())
387 args['partition_count'] = 3
388 set_module_args(args)
389 mock_request.side_effect = [
390 SRR['is_rest'],
391 SRR['owned_partitions_record'],
392 SRR['unassigned_partitions_record'],
393 SRR['end_of_sequence']
394 ]
395 my_obj = my_module()
396 with pytest.raises(AnsibleExitJson) as exc:
397 my_obj.apply()
398 assert exc.value.args[0]['changed'] is False
399 print(mock_request.mock_calls)
400 assert len(mock_request.mock_calls) == 3
401
402
403 # ADP2
404 @patch('ansible_collections.netapp.ontap.plugins.module_utils.netapp.OntapRestAPI.send_request')
405 def test_rest_assign_unassigned_disks_adp2(mock_request, patch_ansible): # pylint: disable=redefined-outer-name,unused-argument
406 ''' Steal disks from partner node and assign them to the requested node '''
407 args = dict(default_args())
408 args['partitioning_method'] = 'root_data1_data2'
409 args['partition_type'] = 'data1'
410 args['partition_count'] = 5 # change this dependant on data1 partitions
411 set_module_args(args)
412 mock_request.side_effect = [
413 SRR['is_rest'],
414 SRR['adp2_owned_partitions_record'],
415 SRR['home_spare_disk_info_record'],
416 SRR['unassigned_partitions_record'],
417 SRR['unassigned_disks_record'],
418 SRR['partner_node_name_record'],
419 SRR['partner_spare_partitions_record'],
420 SRR['partner_spare_disks_record'],
421 SRR['empty_good'], # assign
422 SRR['end_of_sequence']
423 ]
424 my_obj = my_module()
425 with pytest.raises(AnsibleExitJson) as exc:
426 my_obj.apply()
427 assert exc.value.args[0]['changed'] is True
428 print(mock_request.mock_calls)
429 assert len(mock_request.mock_calls) == 6
430
431
432 # assign unassigned partitions + steal 2 partner spare partitions
433 @patch('ansible_collections.netapp.ontap.plugins.module_utils.netapp.OntapRestAPI.send_request')
434 def test_rest_assign_unassigned_and_partner_spare_disks_adp2(mock_request, patch_ansible): # pylint: disable=redefined-outer-name,unused-argument
435 ''' Steal disks from partner node and assign them to the requested node '''
436 args = dict(default_args())
437 args['partitioning_method'] = 'root_data1_data2'
438 args['partition_type'] = 'data1'
439 args['partition_count'] = 7 # data1 partitions
440 set_module_args(args)
441 mock_request.side_effect = [
442 SRR['is_rest'],
443 SRR['adp2_owned_partitions_record'],
444 SRR['home_spare_disk_info_record'],
445 SRR['unassigned_partitions_record'],
446 SRR['unassigned_disks_record'],
447 SRR['partner_node_name_record'],
448 SRR['partner_spare_partitions_record'],
449 SRR['partner_spare_disks_record'],
450 SRR['empty_good'], # unassign
451 SRR['empty_good'], # assign
452 SRR['end_of_sequence']
453 ]
454 my_obj = my_module()
455 with pytest.raises(AnsibleExitJson) as exc:
456 my_obj.apply()
457 assert exc.value.args[0]['changed'] is True
458 print(mock_request.mock_calls)
459 assert len(mock_request.mock_calls) == 9
460
461
462 # assign unassigned partitions + steal 2 partner spare partitions
463 @patch('ansible_collections.netapp.ontap.plugins.module_utils.netapp.OntapRestAPI.send_request')
464 def test_rest_assign_unassigned_and_partner_spare_partitions_and_disks_adp2(mock_request, patch_ansible):
465 ''' Steal disks from partner node and assign them to the requested node '''
466 args = dict(default_args())
467 args['partitioning_method'] = 'root_data1_data2'
468 args['partition_type'] = 'data1'
469 args['partition_count'] = 6 # change this dependant on data1 partitions
470 set_module_args(args)
471 mock_request.side_effect = [
472 SRR['is_rest'],
473 SRR['adp2_owned_partitions_record'],
474 SRR['home_spare_disk_info_record'],
475 SRR['unassigned_partitions_record'],
476 SRR['unassigned_disks_record'],
477 SRR['partner_node_name_record'],
478 SRR['partner_spare_partitions_record'],
479 SRR['partner_spare_disks_record'],
480 SRR['empty_good'], # unassign
481 SRR['empty_good'], # assign
482 SRR['end_of_sequence']
483 ]
484 my_obj = my_module()
485 with pytest.raises(AnsibleExitJson) as exc:
486 my_obj.apply()
487 assert exc.value.args[0]['changed'] is True
488 print(mock_request.mock_calls)
489 assert len(mock_request.mock_calls) == 8
490
491
492 # Should unassign partitions
493 @patch('ansible_collections.netapp.ontap.plugins.module_utils.netapp.OntapRestAPI.send_request')
494 def test_rest_unassign_adp2(mock_request, patch_ansible): # pylint: disable=redefined-outer-name,unused-argument
495 ''' Steal disks from partner node and assign them to the requested node '''
496 args = dict(default_args())
497 args['partitioning_method'] = 'root_data1_data2'
498 args['partition_type'] = 'data1'
499 args['partition_count'] = 2 # change this number to be less than currently assigned partions to the node
500 set_module_args(args)
501 mock_request.side_effect = [
502 SRR['is_rest'],
503 SRR['adp2_owned_partitions_record'],
504 SRR['unassigned_partitions_record'],
505 SRR['spare_partitions_record'],
506 SRR['empty_good'], # unassign
507 # SRR['empty_good'], # assign
508 SRR['end_of_sequence']
509 ]
510 my_obj = my_module()
511 with pytest.raises(AnsibleExitJson) as exc:
512 my_obj.apply()
513 assert exc.value.args[0]['changed'] is True
514 print(mock_request.mock_calls)
515 assert len(mock_request.mock_calls) == 5