Debian Codegraph @

ansible_collections/netapp/cloudmanager/tests/unit/plugins/modules/test_na_cloudmanager_info.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 tests ONTAP Ansible module: '''
5
6 from __future__ import (absolute_import, division, print_function)
7
8 __metaclass__ = type
9
10 import json
11 import sys
12 import pytest
13
14 from ansible.module_utils import basic
15 from ansible.module_utils._text import to_bytes
16 from ansible_collections.netapp.cloudmanager.tests.unit.compat.mock import patch
17 import ansible_collections.netapp.cloudmanager.plugins.module_utils.netapp as netapp_utils
18 from ansible_collections.netapp.cloudmanager.plugins.modules.na_cloudmanager_info \
19 import NetAppCloudmanagerInfo as my_module
20
21 if not netapp_utils.HAS_REQUESTS and sys.version_info < (3, 5):
22 pytestmark = pytest.mark.skip('skipping as missing required imports on 2.6 and 2.7')
23
24
25 def set_module_args(args):
26 '''prepare arguments so that they will be picked up during module creation'''
27 args = json.dumps({'ANSIBLE_MODULE_ARGS': args})
28 basic._ANSIBLE_ARGS = to_bytes(args) # pylint: disable=protected-access
29
30
31 class AnsibleExitJson(Exception):
32 '''Exception class to be raised by module.exit_json and caught by the test case'''
33
34
35 class AnsibleFailJson(Exception):
36 '''Exception class to be raised by module.fail_json and caught by the test case'''
37
38
39 def exit_json(*args, **kwargs): # pylint: disable=unused-argument
40 '''function to patch over exit_json; package return data into an exception'''
41 if 'changed' not in kwargs:
42 kwargs['changed'] = False
43 raise AnsibleExitJson(kwargs)
44
45
46 def fail_json(*args, **kwargs): # pylint: disable=unused-argument
47 '''function to patch over fail_json; package return data into an exception'''
48 kwargs['failed'] = True
49 raise AnsibleFailJson(kwargs)
50
51
52 class MockCMConnection():
53 ''' Mock response of http connections '''
54
55 def __init__(self, kind=None, parm1=None):
56 self.type = kind
57 self.parm1 = parm1
58 self.xml_in = None
59 self.xml_out = None
60
61
62 # using pytest natively, without unittest.TestCase
63 @pytest.fixture
64 def patch_ansible():
65 with patch.multiple(basic.AnsibleModule,
66 exit_json=exit_json,
67 fail_json=fail_json) as mocks:
68 yield mocks
69
70
71 def set_default_args_pass_check(patch_ansible):
72 return dict({
73 'client_id': 'Nw4Q2O1kdnLtvhwegGalFnodEHUfPJWh',
74 'refresh_token': 'myrefresh_token',
75 })
76
77
78 def set_args_get_cloudmanager_working_environments_info():
79 args = {
80 'client_id': 'Nw4Q2O1kdnLtvhwegGalFnodEHUfPJWh',
81 'refresh_token': 'myrefresh_token',
82 'gather_subsets': ['working_environments_info']
83 }
84 return args
85
86
87 def set_args_get_cloudmanager_aggregates_info():
88 args = {
89 'client_id': 'Nw4Q2O1kdnLtvhwegGalFnodEHUfPJWh',
90 'refresh_token': 'myrefresh_token',
91 'gather_subsets': ['working_environments_info']
92 }
93 return args
94
95
96 def set_args_get_accounts_info():
97 args = {
98 'client_id': 'Nw4Q2O1kdnLtvhwegGalFnodEHUfPJWh',
99 'refresh_token': 'myrefresh_token',
100 'gather_subsets': ['accounts_info']
101 }
102 return args
103
104
105 def test_module_fail_when_required_args_missing(patch_ansible):
106 ''' required arguments are reported as errors '''
107 with pytest.raises(AnsibleFailJson) as exc:
108 set_module_args({})
109 my_module()
110 print('Info: %s' % exc.value.args[0]['msg'])
111
112
113 @patch('ansible_collections.netapp.cloudmanager.plugins.module_utils.netapp.CloudManagerRestAPI.get_token')
114 @patch('ansible_collections.netapp.cloudmanager.plugins.module_utils.netapp_module.NetAppModule.get_working_environments_info')
115 def test_get_working_environments_info(working_environments_info, get_token, patch_ansible):
116 args = dict(set_args_get_cloudmanager_working_environments_info())
117 set_module_args(args)
118 get_token.return_value = 'token_type', 'token'
119 working_environments_info.return_value = {
120 "azureVsaWorkingEnvironments": [
121 {
122 "name": "testazure",
123 "cloudProviderName": "Azure",
124 "creatorUserEmail": "samlp|NetAppSAML|testuser",
125 "isHA": False,
126 "publicId": "VsaWorkingEnvironment-az123456",
127 "tenantId": "Tenant-2345",
128 "workingEnvironmentType": "VSA",
129 }
130 ],
131 "gcpVsaWorkingEnvironments": [],
132 "onPremWorkingEnvironments": [],
133 "vsaWorkingEnvironments": [
134 {
135 "name": "testAWS",
136 "cloudProviderName": "Amazon",
137 "creatorUserEmail": "samlp|NetAppSAML|testuser",
138 "isHA": False,
139 "publicId": "VsaWorkingEnvironment-aws12345",
140 "tenantId": "Tenant-2345",
141 "workingEnvironmentType": "VSA",
142 },
143 {
144 "name": "testAWSHA",
145 "cloudProviderName": "Amazon",
146 "creatorUserEmail": "samlp|NetAppSAML|testuser",
147 "isHA": True,
148 "publicId": "VsaWorkingEnvironment-awsha345",
149 "tenantId": "Tenant-2345",
150 "workingEnvironmentType": "VSA",
151 }
152 ]
153 }, None
154 my_obj = my_module()
155 my_obj.rest_api.api_root_path = "my_root_path"
156
157 with pytest.raises(AnsibleExitJson) as exc:
158 my_obj.apply()
159 print('Info: test_create_cloudmanager_info: %s' % repr(exc.value))
160 assert not exc.value.args[0]['changed']
161
162
163 @patch('ansible_collections.netapp.cloudmanager.plugins.module_utils.netapp.CloudManagerRestAPI.get_token')
164 @patch(
165 'ansible_collections.netapp.cloudmanager.plugins.modules.na_cloudmanager_info.NetAppCloudmanagerInfo.get_aggregates_info')
166 def test_get_aggregates_info(aggregates_info, get_token, patch_ansible):
167 args = dict(set_args_get_cloudmanager_aggregates_info())
168 set_module_args(args)
169 get_token.return_value = 'token_type', 'token'
170 aggregates_info.return_value = {
171 "azureVsaWorkingEnvironments": {
172 "VsaWorkingEnvironment-az123456": [
173 {
174 "availableCapacity": {
175 "size": 430.0,
176 "unit": "GB"
177 },
178 "disks": [
179 {
180 "device": "LUN 3.1",
181 "name": "testazure-01-1",
182 "ownerNode": "testazure-01",
183 "position": "data",
184 }
185 ],
186 "encryptionType": "notEncrypted",
187 "homeNode": "testazure-01",
188 "isRoot": False,
189 "name": "aggr1",
190 "ownerNode": "testazure-01",
191 "providerVolumes": [
192 {
193 "device": "1",
194 "diskType": "Premium_LRS",
195 "encrypted": False,
196 "instanceId": "testazureid",
197 "name": "testazuredatadisk1",
198 "size": {
199 "size": 500.0,
200 "unit": "GB"
201 },
202 "state": "available"
203 }
204 ],
205 "sidlEnabled": False,
206 "snaplockType": "non_snaplock",
207 "state": "online",
208 "totalCapacity": {
209 "size": 500.0,
210 "unit": "GB"
211 },
212 "usedCapacity": {
213 "size": 70.0,
214 "unit": "GB"
215 },
216 "volumes": [
217 {
218 "isClone": False,
219 "name": "svm_testazure_root",
220 "rootVolume": True,
221 "thinProvisioned": True,
222 "totalSize": {
223 "size": 1.0,
224 "unit": "GB"
225 },
226 "usedSize": {
227 "size": 0.000339508056640625,
228 "unit": "GB"
229 }
230 },
231 {
232 "isClone": False,
233 "name": "azv1",
234 "rootVolume": False,
235 "thinProvisioned": True,
236 "totalSize": {
237 "size": 500.0,
238 "unit": "GB"
239 },
240 "usedSize": {
241 "size": 0.0,
242 "unit": "GB"
243 }
244 }
245 ]
246 },
247 ]
248 },
249 "gcpVsaWorkingEnvironments": {},
250 "onPremWorkingEnvironments": {},
251 "vsaWorkingEnvironments": {
252 "VsaWorkingEnvironment-aws12345": [
253 {
254 "availableCapacity": {
255 "size": 430.0,
256 "unit": "GB"
257 },
258 "disks": [
259 {
260 "device": "xvdh vol-381",
261 "name": "testAWSHA-01-i-196h",
262 "ownerNode": "testAWSHA-01",
263 "position": "data",
264 },
265 {
266 "device": "xvdh vol-382",
267 "name": "testAWSHA-01-i-195h",
268 "ownerNode": "testAWSHA-01",
269 "position": "data",
270 }
271 ],
272 "encryptionType": "cloudEncrypted",
273 "homeNode": "testAWSHA-01",
274 "isRoot": False,
275 "name": "aggr1",
276 "ownerNode": "testAWSHA-01",
277 "providerVolumes": [
278 {
279 "device": "/dev/xvdh",
280 "diskType": "gp2",
281 "encrypted": True,
282 "id": "vol-381",
283 "instanceId": "i-196",
284 "name": "vol-381",
285 "size": {
286 "size": 500.0,
287 "unit": "GB"
288 },
289 "state": "in-use"
290 },
291 {
292 "device": "/dev/xvdh",
293 "diskType": "gp2",
294 "encrypted": True,
295 "id": "vol-382",
296 "instanceId": "i-195",
297 "name": "vol-382",
298 "size": {
299 "size": 500.0,
300 "unit": "GB"
301 },
302 "state": "in-use"
303 }
304 ],
305 "sidlEnabled": True,
306 "snaplockType": "non_snaplock",
307 "state": "online",
308 "totalCapacity": {
309 "size": 500.0,
310 "unit": "GB"
311 },
312 "usedCapacity": {
313 "size": 70.0,
314 "unit": "GB"
315 },
316 "volumes": [
317 {
318 "isClone": False,
319 "name": "svm_testAWSHA_root",
320 "rootVolume": True,
321 "thinProvisioned": True,
322 "totalSize": {
323 "size": 1.0,
324 "unit": "GB"
325 },
326 "usedSize": {
327 "size": 0.000339508056640625,
328 "unit": "GB"
329 }
330 },
331 {
332 "isClone": False,
333 "name": "vha",
334 "rootVolume": False,
335 "thinProvisioned": True,
336 "totalSize": {
337 "size": 100.0,
338 "unit": "GB"
339 },
340 "usedSize": {
341 "size": 0.0,
342 "unit": "GB"
343 }
344 }
345 ]
346 }
347 ],
348 "VsaWorkingEnvironment-awsha345": [
349 {
350 "availableCapacity": {
351 "size": 430.0,
352 "unit": "GB"
353 },
354 "disks": [
355 {
356 "device": "xvdg vol-369",
357 "name": "testAWS-01-i-190g",
358 "ownerNode": "testAWS-01",
359 "position": "data",
360 }
361 ],
362 "encryptionType": "cloudEncrypted",
363 "homeNode": "testAWS-01",
364 "isRoot": False,
365 "name": "aggr1",
366 "ownerNode": "testAWS-01",
367 "providerVolumes": [
368 {
369 "device": "/dev/xvdg",
370 "diskType": "gp2",
371 "encrypted": True,
372 "id": "vol-369",
373 "instanceId": "i-190",
374 "name": "vol-369",
375 "size": {
376 "size": 500.0,
377 "unit": "GB"
378 },
379 "state": "in-use"
380 }
381 ],
382 "sidlEnabled": True,
383 "snaplockType": "non_snaplock",
384 "state": "online",
385 "totalCapacity": {
386 "size": 500.0,
387 "unit": "GB"
388 },
389 "usedCapacity": {
390 "size": 70.0,
391 "unit": "GB"
392 },
393 "volumes": [
394 {
395 "isClone": False,
396 "name": "svm_testAWS_root",
397 "rootVolume": True,
398 "thinProvisioned": True,
399 "totalSize": {
400 "size": 1.0,
401 "unit": "GB"
402 },
403 "usedSize": {
404 "size": 0.000339508056640625,
405 "unit": "GB"
406 }
407 },
408 {
409 "isClone": False,
410 "name": "v1",
411 "rootVolume": False,
412 "thinProvisioned": True,
413 "totalSize": {
414 "size": 100.0,
415 "unit": "GB"
416 },
417 "usedSize": {
418 "size": 0.0,
419 "unit": "GB"
420 }
421 }
422 ]
423 },
424 {
425 "availableCapacity": {
426 "size": 86.0,
427 "unit": "GB"
428 },
429 "disks": [
430 {
431 "device": "xvdh vol-371",
432 "name": "testAWS-01-i-190h",
433 "ownerNode": "testAWS-01",
434 "position": "data",
435 }
436 ],
437 "encryptionType": "cloudEncrypted",
438 "homeNode": "testAWS-01",
439 "isRoot": False,
440 "name": "aggr2",
441 "ownerNode": "testAWS-01",
442 "providerVolumes": [
443 {
444 "device": "/dev/xvdh",
445 "diskType": "gp2",
446 "encrypted": True,
447 "id": "vol-371",
448 "instanceId": "i-190",
449 "name": "vol-371",
450 "size": {
451 "size": 100.0,
452 "unit": "GB"
453 },
454 "state": "in-use"
455 }
456 ],
457 "sidlEnabled": True,
458 "snaplockType": "non_snaplock",
459 "state": "online",
460 "totalCapacity": {
461 "size": 100.0,
462 "unit": "GB"
463 },
464 "usedCapacity": {
465 "size": 0.0,
466 "unit": "GB"
467 },
468 "volumes": []
469 }
470 ]
471 }
472 }
473 my_obj = my_module()
474 my_obj.rest_api.api_root_path = "my_root_path"
475
476 with pytest.raises(AnsibleExitJson) as exc:
477 my_obj.apply()
478 print('Info: test_create_cloudmanager_info: %s' % repr(exc.value))
479 assert not exc.value.args[0]['changed']
480
481
482 @patch('ansible_collections.netapp.cloudmanager.plugins.module_utils.netapp.CloudManagerRestAPI.get_token')
483 @patch('ansible_collections.netapp.cloudmanager.plugins.module_utils.netapp_module.NetAppModule.get_accounts_info')
484 def test_get_accounts_info(accounts_info, get_token, patch_ansible):
485 args = dict(set_args_get_accounts_info())
486 set_module_args(args)
487 get_token.return_value = 'token_type', 'token'
488 accounts_info.return_value = {
489 "awsAccounts": [
490 {
491 "accessKey": "1",
492 "accountId": "123456789011",
493 "accountName": "tami",
494 "accountType": "AWS_KEYS",
495 "publicId": "CloudProviderAccount-Ekj6L9QX",
496 "subscriptionId": "hackExp10Days",
497 "vsaList": []
498 },
499 {
500 "accessKey": "",
501 "accountId": "123456789011",
502 "accountName": "Instance Profile",
503 "accountType": "INSTANCE_PROFILE",
504 "occmRole": "occmRole",
505 "publicId": "InstanceProfile",
506 "subscriptionId": "hackExp10Days",
507 "vsaList": [
508 {
509 "name": "CVO_AWSCluster",
510 "publicId": "VsaWorkingEnvironment-9m3I6i3I",
511 "workingEnvironmentType": "AWS"
512 },
513 {
514 "name": "testAWS1",
515 "publicId": "VsaWorkingEnvironment-JCzkA9OX",
516 "workingEnvironmentType": "AWS"
517 },
518 ]
519 }
520 ],
521 "azureAccounts": [
522 {
523 "accountName": "AzureKeys",
524 "accountType": "AZURE_KEYS",
525 "applicationId": "1",
526 "publicId": "CloudProviderAccount-T84ceMYu",
527 "tenantId": "1",
528 "vsaList": [
529 {
530 "name": "testAZURE",
531 "publicId": "VsaWorkingEnvironment-jI0tbceH",
532 "workingEnvironmentType": "AZURE"
533 },
534 {
535 "name": "test",
536 "publicId": "VsaWorkingEnvironment-00EnDcfB",
537 "workingEnvironmentType": "AZURE"
538 },
539 ]
540 },
541 {
542 "accountName": "s",
543 "accountType": "AZURE_KEYS",
544 "applicationId": "1",
545 "publicId": "CloudProviderAccount-XxbN95dj",
546 "tenantId": "1",
547 "vsaList": []
548 }
549 ],
550 "gcpStorageAccounts": [],
551 "nssAccounts": [
552 {
553 "accountName": "TESTCLOUD2",
554 "accountType": "NSS_KEYS",
555 "nssUserName": "TESTCLOUD2",
556 "publicId": "be2f3cac-352a-46b9-a341-a446c35b61c9",
557 "vsaList": [
558 {
559 "name": "testAWS",
560 "publicId": "VsaWorkingEnvironment-3txYJOsX",
561 "workingEnvironmentType": "AWS"
562 },
563 {
564 "name": "testAZURE",
565 "publicId": "VsaWorkingEnvironment-jI0tbceH",
566 "workingEnvironmentType": "AZURE"
567 },
568 ]
569 },
570 {
571 "accountName": "ntapitdemo",
572 "accountType": "NSS_KEYS",
573 "nssUserName": "ntapitdemo",
574 "publicId": "01e43a7d-cfc9-4682-aa12-15374ce81638",
575 "vsaList": [
576 {
577 "name": "test",
578 "publicId": "VsaWorkingEnvironment-00EnDcfB",
579 "workingEnvironmentType": "AZURE"
580 }
581 ]
582 }
583 ]
584 }, None
585 my_obj = my_module()
586 my_obj.rest_api.api_root_path = "my_root_path"
587
588 with pytest.raises(AnsibleExitJson) as exc:
589 my_obj.apply()
590 print('Info: test_create_cloudmanager_info: %s' % repr(exc.value))
591 assert not exc.value.args[0]['changed']