| 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 import unittest |
| 17 |
from ansible_collections.netapp.cloudmanager.tests.unit.compat.mock import patch |
| 18 |
import ansible_collections.netapp.cloudmanager.plugins.module_utils.netapp as netapp_utils |
| 19 |
from ansible_collections.netapp.cloudmanager.plugins.modules.na_cloudmanager_aggregate \ |
| 20 |
import NetAppCloudmanagerAggregate as my_module |
| 21 |
|
| 22 |
if not netapp_utils.HAS_REQUESTS and sys.version_info < (3, 5): |
| 23 |
pytestmark = pytest.mark.skip('skipping as missing required imports on 2.6 and 2.7') |
| 24 |
|
| 25 |
|
| 26 |
def set_module_args(args): |
| 27 |
'''prepare arguments so that they will be picked up during module creation''' |
| 28 |
args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) |
| 29 |
basic._ANSIBLE_ARGS = to_bytes(args) # pylint: disable=protected-access |
| 30 |
|
| 31 |
|
| 32 |
class AnsibleExitJson(Exception): |
| 33 |
'''Exception class to be raised by module.exit_json and caught by the test case''' |
| 34 |
|
| 35 |
|
| 36 |
class AnsibleFailJson(Exception): |
| 37 |
'''Exception class to be raised by module.fail_json and caught by the test case''' |
| 38 |
|
| 39 |
|
| 40 |
def exit_json(*args, **kwargs): # pylint: disable=unused-argument |
| 41 |
'''function to patch over exit_json; package return data into an exception''' |
| 42 |
if 'changed' not in kwargs: |
| 43 |
kwargs['changed'] = False |
| 44 |
raise AnsibleExitJson(kwargs) |
| 45 |
|
| 46 |
|
| 47 |
def fail_json(*args, **kwargs): # pylint: disable=unused-argument |
| 48 |
'''function to patch over fail_json; package return data into an exception''' |
| 49 |
kwargs['failed'] = True |
| 50 |
raise AnsibleFailJson(kwargs) |
| 51 |
|
| 52 |
|
| 53 |
class MockCMConnection(): |
| 54 |
''' Mock response of http connections ''' |
| 55 |
|
| 56 |
def __init__(self, kind=None, parm1=None): |
| 57 |
self.type = kind |
| 58 |
self.parm1 = parm1 |
| 59 |
self.xml_in = None |
| 60 |
self.xml_out = None |
| 61 |
|
| 62 |
|
| 63 |
class TestMyModule(unittest.TestCase): |
| 64 |
''' a group of related Unit Tests ''' |
| 65 |
|
| 66 |
def setUp(self): |
| 67 |
self.mock_module_helper = patch.multiple(basic.AnsibleModule, |
| 68 |
exit_json=exit_json, |
| 69 |
fail_json=fail_json) |
| 70 |
self.mock_module_helper.start() |
| 71 |
self.addCleanup(self.mock_module_helper.stop) |
| 72 |
|
| 73 |
def set_default_args_pass_check(self): |
| 74 |
return dict({ |
| 75 |
'state': 'present', |
| 76 |
'name': 'TestA', |
| 77 |
'working_environment_id': 'VsaWorkingEnvironment-abcdefg12345', |
| 78 |
'client_id': 'Nw4Q2O1kdnLtvhwegGalFnodEHUfPJWh', |
| 79 |
'number_of_disks': 2, |
| 80 |
'disk_size_size': 100, |
| 81 |
'disk_size_unit': 'GB', |
| 82 |
'refresh_token': 'myrefresh_token', |
| 83 |
}) |
| 84 |
|
| 85 |
def set_args_create_cloudmanager_aggregate(self): |
| 86 |
return dict({ |
| 87 |
'state': 'present', |
| 88 |
'name': 'Dummyname', |
| 89 |
'working_environment_id': 'VsaWorkingEnvironment-abcdefg12345', |
| 90 |
'client_id': 'Nw4Q2O1kdnLtvhwegGalFnodEHUfPJWh', |
| 91 |
'number_of_disks': 2, |
| 92 |
'disk_size_size': 100, |
| 93 |
'disk_size_unit': 'GB', |
| 94 |
'refresh_token': 'myrefresh_token', |
| 95 |
}) |
| 96 |
|
| 97 |
def set_args_create_cloudmanager_aggregate_by_workingenv_name(self): |
| 98 |
return dict({ |
| 99 |
'state': 'present', |
| 100 |
'name': 'Dummyname', |
| 101 |
'working_environment_name': 'wkone', |
| 102 |
'client_id': 'Nw4Q2O1kdnLtvhwegGalFnodEHUfPJWh', |
| 103 |
'number_of_disks': 2, |
| 104 |
'disk_size_size': 100, |
| 105 |
'disk_size_unit': 'GB', |
| 106 |
'refresh_token': 'myrefresh_token', |
| 107 |
}) |
| 108 |
|
| 109 |
def set_args_delete_cloudmanager_aggregate(self): |
| 110 |
return dict({ |
| 111 |
'state': 'absent', |
| 112 |
'name': 'Dummyname', |
| 113 |
'working_environment_id': 'VsaWorkingEnvironment-abcdefg12345', |
| 114 |
'client_id': 'Nw4Q2O1kdnLtvhwegGalFnodEHUfPJWh', |
| 115 |
'number_of_disks': 2, |
| 116 |
'disk_size_size': 100, |
| 117 |
'disk_size_unit': 'GB', |
| 118 |
'refresh_token': 'myrefresh_token', |
| 119 |
}) |
| 120 |
|
| 121 |
def set_args_delete_cloudmanager_aggregate_by_workingenv_name(self): |
| 122 |
return dict({ |
| 123 |
'state': 'absent', |
| 124 |
'name': 'Dummyname', |
| 125 |
'client_id': 'Nw4Q2O1kdnLtvhwegGalFnodEHUfPJWh', |
| 126 |
'number_of_disks': 2, |
| 127 |
'disk_size_size': 100, |
| 128 |
'disk_size_unit': 'GB', |
| 129 |
'refresh_token': 'myrefresh_token', |
| 130 |
}) |
| 131 |
|
| 132 |
def set_args_update_cloudmanager_aggregate(self): |
| 133 |
return dict({ |
| 134 |
'state': 'present', |
| 135 |
'name': 'TestCMAggregate', |
| 136 |
'working_environment_id': 'VsaWorkingEnvironment-abcdefg12345', |
| 137 |
'client_id': 'Nw4Q2O1kdnLtvhwegGalFnodEHUfPJWh', |
| 138 |
'number_of_disks': 3, |
| 139 |
'disk_size_size': 100, |
| 140 |
'disk_size_unit': 'GB', |
| 141 |
'refresh_token': 'myrefresh_token', |
| 142 |
}) |
| 143 |
|
| 144 |
def test_module_fail_when_required_args_missing(self): |
| 145 |
''' required arguments are reported as errors ''' |
| 146 |
with pytest.raises(AnsibleFailJson) as exc: |
| 147 |
set_module_args({}) |
| 148 |
my_module() |
| 149 |
print('Info: %s' % exc.value.args[0]['msg']) |
| 150 |
|
| 151 |
@patch('ansible_collections.netapp.cloudmanager.plugins.module_utils.netapp.CloudManagerRestAPI.get_token') |
| 152 |
def test_module_fail_when_required_args_present(self, get_token): |
| 153 |
''' required arguments are reported as errors ''' |
| 154 |
with pytest.raises(AnsibleExitJson) as exc: |
| 155 |
set_module_args(self.set_default_args_pass_check()) |
| 156 |
get_token.return_value = 'test', 'test' |
| 157 |
my_module() |
| 158 |
exit_json(changed=True, msg="TestCase Fail when required ars are present") |
| 159 |
assert exc.value.args[0]['changed'] |
| 160 |
|
| 161 |
@patch('ansible_collections.netapp.cloudmanager.plugins.module_utils.netapp.CloudManagerRestAPI.get_token') |
| 162 |
@patch( |
| 163 |
'ansible_collections.netapp.cloudmanager.plugins.modules.na_cloudmanager_aggregate.NetAppCloudmanagerAggregate.get_aggregate') |
| 164 |
@patch('ansible_collections.netapp.cloudmanager.plugins.module_utils.netapp.CloudManagerRestAPI.post') |
| 165 |
def test_create_cloudmanager_aggregate_pass(self, get_post_api, get_aggregate_api, get_token): |
| 166 |
set_module_args(self.set_args_create_cloudmanager_aggregate()) |
| 167 |
get_token.return_value = 'test', 'test' |
| 168 |
my_obj = my_module() |
| 169 |
my_obj.rest_api.api_root_path = "my_root_path" |
| 170 |
|
| 171 |
get_aggregate_api.return_value = None |
| 172 |
get_post_api.return_value = None, None, None |
| 173 |
|
| 174 |
with pytest.raises(AnsibleExitJson) as exc: |
| 175 |
my_obj.apply() |
| 176 |
print('Info: test_create_cloudmanager_aggregate: %s' % repr(exc.value)) |
| 177 |
assert exc.value.args[0]['changed'] |
| 178 |
|
| 179 |
@patch('ansible_collections.netapp.cloudmanager.plugins.module_utils.netapp.CloudManagerRestAPI.get_token') |
| 180 |
@patch( |
| 181 |
'ansible_collections.netapp.cloudmanager.plugins.modules.na_cloudmanager_aggregate.NetAppCloudmanagerAggregate.get_aggregate') |
| 182 |
@patch('ansible_collections.netapp.cloudmanager.plugins.module_utils.netapp.CloudManagerRestAPI.delete') |
| 183 |
def test_delete_cloudmanager_aggregate_pass(self, get_delete_api, get_aggregate_api, get_token): |
| 184 |
set_module_args(self.set_args_delete_cloudmanager_aggregate()) |
| 185 |
get_token.return_value = 'test', 'test' |
| 186 |
my_obj = my_module() |
| 187 |
my_obj.rest_api.api_root_path = "my_root_path" |
| 188 |
|
| 189 |
my_aggregate = { |
| 190 |
'name': 'Dummyname', |
| 191 |
'state': 'online', |
| 192 |
'working_environment_id': 'VsaWorkingEnvironment-abcdefg12345', |
| 193 |
'client_id': 'Nw4Q2O1kdnLtvhwegGalFnodEHUfPJWh', |
| 194 |
'refresh_token': 'myrefresh_token', |
| 195 |
'disks': [{'device': 'xvdh vol-313', 'position': 'data', 'vmDiskProperties': None, |
| 196 |
'ownerNode': 'testAWSa-01', 'name': 'testAWSa-01-i-12h'}, |
| 197 |
{'device': 'xvdi vol-314', 'position': 'data', 'vmDiskProperties': None, |
| 198 |
'ownerNode': 'testAWSa-01', 'name': 'testAWSa-01-i-12i'}], |
| 199 |
'homeNode': 'testAWSa-01', |
| 200 |
} |
| 201 |
get_aggregate_api.return_value = my_aggregate |
| 202 |
get_delete_api.return_value = 'Aggregated Deleted', None, None |
| 203 |
|
| 204 |
with pytest.raises(AnsibleExitJson) as exc: |
| 205 |
my_obj.apply() |
| 206 |
print('Info: test_delete_cloudmanager_aggregate: %s' % repr(exc.value)) |
| 207 |
assert exc.value.args[0]['changed'] |
| 208 |
|
| 209 |
@patch('ansible_collections.netapp.cloudmanager.plugins.module_utils.netapp.CloudManagerRestAPI.get_token') |
| 210 |
@patch('ansible_collections.netapp.cloudmanager.plugins.modules.na_cloudmanager_aggregate.NetAppCloudmanagerAggregate.get_aggregate') |
| 211 |
@patch('ansible_collections.netapp.cloudmanager.plugins.module_utils.netapp.CloudManagerRestAPI.post') |
| 212 |
def test_update_cloudmanager_aggregate_pass(self, get_post_api, get_aggregate_api, get_token): |
| 213 |
set_module_args(self.set_args_update_cloudmanager_aggregate()) |
| 214 |
get_token.return_value = 'test', 'test' |
| 215 |
my_obj = my_module() |
| 216 |
my_obj.rest_api.api_root_path = "my_root_path" |
| 217 |
|
| 218 |
my_aggregate = { |
| 219 |
'name': 'Dummyname', |
| 220 |
'state': 'online', |
| 221 |
'working_environment_id': 'VsaWorkingEnvironment-abcdefg12345', |
| 222 |
'client_id': 'Nw4Q2O1kdnLtvhwegGalFnodEHUfPJWh', |
| 223 |
'refresh_token': 'myrefresh_token', |
| 224 |
'disks': [{'device': 'xvdh vol-313', 'position': 'data', 'vmDiskProperties': None, |
| 225 |
'ownerNode': 'testAWSa-01', 'name': 'testAWSa-01-i-12h'}, |
| 226 |
{'device': 'xvdi vol-314', 'position': 'data', 'vmDiskProperties': None, |
| 227 |
'ownerNode': 'testAWSa-01', 'name': 'testAWSa-01-i-12i'}], |
| 228 |
'homeNode': 'testAWSa-01', |
| 229 |
} |
| 230 |
get_aggregate_api.return_value = my_aggregate |
| 231 |
get_post_api.return_value = None, None, None |
| 232 |
|
| 233 |
with pytest.raises(AnsibleExitJson) as exc: |
| 234 |
my_obj.apply() |
| 235 |
print('Info: test_update_cloudmanager_aggregate: %s' % repr(exc.value)) |
| 236 |
assert exc.value.args[0]['changed'] |
| 237 |
|
| 238 |
@patch('ansible_collections.netapp.cloudmanager.plugins.module_utils.netapp.CloudManagerRestAPI.get_token') |
| 239 |
@patch('ansible_collections.netapp.cloudmanager.plugins.modules.na_cloudmanager_aggregate.NetAppCloudmanagerAggregate.get_aggregate') |
| 240 |
@patch('ansible_collections.netapp.cloudmanager.plugins.module_utils.netapp_module.NetAppModule.get_working_environment_details_by_name') |
| 241 |
@patch('ansible_collections.netapp.cloudmanager.plugins.module_utils.netapp.CloudManagerRestAPI.post') |
| 242 |
def test_create_cloudmanager_aggregate_by_workingenv_name_pass(self, get_post_api, get_we, get_aggregate_api, get_token): |
| 243 |
data = self.set_args_create_cloudmanager_aggregate_by_workingenv_name() |
| 244 |
get_token.return_value = 'test', 'test' |
| 245 |
my_we = { |
| 246 |
'name': 'test', |
| 247 |
'publicId': 'test', |
| 248 |
'cloudProviderName': 'Amazon'} |
| 249 |
get_we.return_value = my_we, None |
| 250 |
data['working_environment_id'] = my_we['publicId'] |
| 251 |
set_module_args(data) |
| 252 |
my_obj = my_module() |
| 253 |
my_obj.rest_api.api_root_path = "my_root_path" |
| 254 |
get_aggregate_api.return_value = None |
| 255 |
get_post_api.return_value = None, None, None |
| 256 |
|
| 257 |
with pytest.raises(AnsibleExitJson) as exc: |
| 258 |
my_obj.apply() |
| 259 |
print('Info: test_create_cloudmanager_aggregate: %s' % repr(exc.value)) |
| 260 |
assert exc.value.args[0]['changed'] |
| 261 |
|
| 262 |
@patch('ansible_collections.netapp.cloudmanager.plugins.module_utils.netapp.CloudManagerRestAPI.get_token') |
| 263 |
@patch('ansible_collections.netapp.cloudmanager.plugins.modules.na_cloudmanager_aggregate.NetAppCloudmanagerAggregate.get_aggregate') |
| 264 |
@patch('ansible_collections.netapp.cloudmanager.plugins.module_utils.netapp_module.NetAppModule.get_working_environment_details_by_name') |
| 265 |
@patch('ansible_collections.netapp.cloudmanager.plugins.module_utils.netapp.CloudManagerRestAPI.delete') |
| 266 |
def test_delete_cloudmanager_aggregate_by_workingenv_name_pass(self, get_delete_api, get_we, get_aggregate_api, get_token): |
| 267 |
data = self.set_args_delete_cloudmanager_aggregate_by_workingenv_name() |
| 268 |
my_we = { |
| 269 |
'name': 'test', |
| 270 |
'publicId': 'test', |
| 271 |
'cloudProviderName': 'Amazon'} |
| 272 |
get_we.return_value = my_we, None |
| 273 |
data['working_environment_id'] = my_we['publicId'] |
| 274 |
set_module_args(data) |
| 275 |
get_token.return_value = 'test', 'test' |
| 276 |
my_obj = my_module() |
| 277 |
my_obj.rest_api.api_root_path = "my_root_path" |
| 278 |
|
| 279 |
my_aggregate = { |
| 280 |
'name': 'Dummyname', |
| 281 |
'state': 'online', |
| 282 |
'working_environment_id': 'VsaWorkingEnvironment-abcdefg12345', |
| 283 |
'client_id': 'Nw4Q2O1kdnLtvhwegGalFnodEHUfPJWh', |
| 284 |
'refresh_token': 'myrefresh_token', |
| 285 |
'disks': [{'device': 'xvdh vol-313', 'position': 'data', 'vmDiskProperties': None, |
| 286 |
'ownerNode': 'testAWSa-01', 'name': 'testAWSa-01-i-12h'}, |
| 287 |
{'device': 'xvdi vol-314', 'position': 'data', 'vmDiskProperties': None, |
| 288 |
'ownerNode': 'testAWSa-01', 'name': 'testAWSa-01-i-12i'}], |
| 289 |
'homeNode': 'testAWSa-01', |
| 290 |
} |
| 291 |
get_aggregate_api.return_value = my_aggregate |
| 292 |
get_delete_api.return_value = 'Aggregated Deleted', None, None |
| 293 |
|
| 294 |
with pytest.raises(AnsibleExitJson) as exc: |
| 295 |
my_obj.apply() |
| 296 |
print('Info: test_delete_cloudmanager_aggregate: %s' % repr(exc.value)) |
| 297 |
assert exc.value.args[0]['changed'] |