| 1 |
# (c) 2022, 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 Cloudmanager Ansible module: ''' |
| 5 |
|
| 6 |
from __future__ import (absolute_import, division, print_function) |
| 7 |
__metaclass__ = type |
| 8 |
import json |
| 9 |
import sys |
| 10 |
import pytest |
| 11 |
|
| 12 |
from ansible.module_utils import basic |
| 13 |
from ansible.module_utils._text import to_bytes |
| 14 |
from ansible_collections.netapp.cloudmanager.tests.unit.compat import unittest |
| 15 |
from ansible_collections.netapp.cloudmanager.tests.unit.compat.mock import patch |
| 16 |
import ansible_collections.netapp.cloudmanager.plugins.module_utils.netapp as netapp_utils |
| 17 |
from ansible_collections.netapp.cloudmanager.plugins.modules.na_cloudmanager_volume \ |
| 18 |
import NetAppCloudmanagerVolume as my_module |
| 19 |
|
| 20 |
if not netapp_utils.HAS_REQUESTS and sys.version_info < (3, 5): |
| 21 |
pytestmark = pytest.mark.skip('skipping as missing required imports on 2.6 and 2.7') |
| 22 |
|
| 23 |
|
| 24 |
def set_module_args(args): |
| 25 |
'''prepare arguments so that they will be picked up during module creation''' |
| 26 |
args = json.dumps({'ANSIBLE_MODULE_ARGS': args}) |
| 27 |
basic._ANSIBLE_ARGS = to_bytes(args) # pylint: disable=protected-access |
| 28 |
|
| 29 |
|
| 30 |
class AnsibleExitJson(Exception): |
| 31 |
'''Exception class to be raised by module.exit_json and caught by the test case''' |
| 32 |
|
| 33 |
|
| 34 |
class AnsibleFailJson(Exception): |
| 35 |
'''Exception class to be raised by module.fail_json and caught by the test case''' |
| 36 |
|
| 37 |
|
| 38 |
def exit_json(*args, **kwargs): # pylint: disable=unused-argument |
| 39 |
'''function to patch over exit_json; package return data into an exception''' |
| 40 |
if 'changed' not in kwargs: |
| 41 |
kwargs['changed'] = False |
| 42 |
raise AnsibleExitJson(kwargs) |
| 43 |
|
| 44 |
|
| 45 |
def fail_json(*args, **kwargs): # pylint: disable=unused-argument |
| 46 |
'''function to patch over fail_json; package return data into an exception''' |
| 47 |
kwargs['failed'] = True |
| 48 |
raise AnsibleFailJson(kwargs) |
| 49 |
|
| 50 |
|
| 51 |
class MockCMConnection(): |
| 52 |
''' Mock response of http connections ''' |
| 53 |
def __init__(self, kind=None, parm1=None): |
| 54 |
self.type = kind |
| 55 |
self.parm1 = parm1 |
| 56 |
self.xml_in = None |
| 57 |
self.xml_out = None |
| 58 |
|
| 59 |
|
| 60 |
class TestMyModule(unittest.TestCase): |
| 61 |
''' a group of related Unit Tests ''' |
| 62 |
|
| 63 |
def setUp(self): |
| 64 |
self.mock_module_helper = patch.multiple(basic.AnsibleModule, |
| 65 |
exit_json=exit_json, |
| 66 |
fail_json=fail_json) |
| 67 |
self.mock_module_helper.start() |
| 68 |
self.addCleanup(self.mock_module_helper.stop) |
| 69 |
|
| 70 |
def set_default_args_pass_check(self): |
| 71 |
return dict({ |
| 72 |
'state': 'present', |
| 73 |
'name': 'testvol', |
| 74 |
'working_environment_id': 'VsaWorkingEnvironment-abcdefg12345', |
| 75 |
'client_id': 'Nw4Q2O1kdnLtvhwegGalFnodEHUfPJWh', |
| 76 |
'svm_name': 'svm_justinaws', |
| 77 |
'snapshot_policy_name': 'default', |
| 78 |
'tiering_policy': 'auto', |
| 79 |
'export_policy_type': 'custom', |
| 80 |
'export_policy_ip': ["10.30.0.1/16"], |
| 81 |
'export_policy_nfs_version': ["nfs4"], |
| 82 |
'refresh_token': 'myrefresh_token', |
| 83 |
'size': 10, |
| 84 |
}) |
| 85 |
|
| 86 |
def set_default_args_with_workingenv_name_pass_check(self): |
| 87 |
return dict({ |
| 88 |
'state': 'present', |
| 89 |
'name': 'testvol', |
| 90 |
'working_environment_name': 'weone', |
| 91 |
'client_id': 'Nw4Q2O1kdnLtvhwegGalFnodEHUfPJWh', |
| 92 |
'svm_name': 'svm_justinaws', |
| 93 |
'snapshot_policy_name': 'default', |
| 94 |
'export_policy_type': 'custom', |
| 95 |
'export_policy_ip': ["10.30.0.1/16"], |
| 96 |
'export_policy_nfs_version': ["nfs4"], |
| 97 |
'refresh_token': 'myrefresh_token', |
| 98 |
'size': 10, |
| 99 |
}) |
| 100 |
|
| 101 |
def test_module_fail_when_required_args_missing(self): |
| 102 |
''' required arguments are reported as errors ''' |
| 103 |
with pytest.raises(AnsibleFailJson) as exc: |
| 104 |
set_module_args({}) |
| 105 |
my_module() |
| 106 |
print('Info: %s' % exc.value.args[0]['msg']) |
| 107 |
|
| 108 |
@patch('ansible_collections.netapp.cloudmanager.plugins.module_utils.netapp.CloudManagerRestAPI.get_token') |
| 109 |
@patch('ansible_collections.netapp.cloudmanager.plugins.modules.na_cloudmanager_volume.NetAppCloudmanagerVolume.get_volume') |
| 110 |
@patch('ansible_collections.netapp.cloudmanager.plugins.modules.na_cloudmanager_volume.NetAppCloudmanagerVolume.create_volume') |
| 111 |
@patch('ansible_collections.netapp.cloudmanager.plugins.module_utils.netapp.CloudManagerRestAPI.send_request') |
| 112 |
def test_create_volume_successfully(self, send_request, create, get, get_token): |
| 113 |
set_module_args(self.set_default_args_pass_check()) |
| 114 |
get.return_value = None |
| 115 |
create.return_value = None |
| 116 |
send_request.side_effect = [({'publicId': 'id', 'svmName': 'svm_name', 'cloudProviderName': "aws", 'isHA': False}, None, None)] |
| 117 |
get_token.return_value = ("type", "token") |
| 118 |
obj = my_module() |
| 119 |
obj.rest_api.api_root_path = "test_root_path" |
| 120 |
|
| 121 |
with pytest.raises(AnsibleExitJson) as exc: |
| 122 |
obj.apply() |
| 123 |
assert exc.value.args[0]['changed'] |
| 124 |
|
| 125 |
@patch('ansible_collections.netapp.cloudmanager.plugins.module_utils.netapp.CloudManagerRestAPI.get_token') |
| 126 |
@patch('ansible_collections.netapp.cloudmanager.plugins.modules.na_cloudmanager_volume.NetAppCloudmanagerVolume.get_volume') |
| 127 |
@patch('ansible_collections.netapp.cloudmanager.plugins.module_utils.netapp.CloudManagerRestAPI.send_request') |
| 128 |
def test_create_volume_idempotency(self, send_request, get, get_token): |
| 129 |
set_module_args(self.set_default_args_pass_check()) |
| 130 |
get.return_value = { |
| 131 |
'name': 'testvol', |
| 132 |
'snapshot_policy_name': 'default', |
| 133 |
'export_policy_type': 'custom', |
| 134 |
'export_policy_ip': ["10.30.0.1/16"], |
| 135 |
'export_policy_nfs_version': ["nfs4"], |
| 136 |
} |
| 137 |
send_request.side_effect = [({'publicId': 'id', 'svmName': 'svm_name', 'cloudProviderName': "aws", 'isHA': False}, None, None)] |
| 138 |
get_token.return_value = ("type", "token") |
| 139 |
obj = my_module() |
| 140 |
obj.rest_api.api_root_path = "test_root_path" |
| 141 |
|
| 142 |
with pytest.raises(AnsibleExitJson) as exc: |
| 143 |
obj.apply() |
| 144 |
assert not exc.value.args[0]['changed'] |
| 145 |
|
| 146 |
@patch('ansible_collections.netapp.cloudmanager.plugins.modules.na_cloudmanager_volume.NetAppCloudmanagerVolume.modify_volume') |
| 147 |
@patch('ansible_collections.netapp.cloudmanager.plugins.module_utils.netapp.CloudManagerRestAPI.get_token') |
| 148 |
@patch('ansible_collections.netapp.cloudmanager.plugins.modules.na_cloudmanager_volume.NetAppCloudmanagerVolume.get_volume') |
| 149 |
@patch('ansible_collections.netapp.cloudmanager.plugins.module_utils.netapp.CloudManagerRestAPI.send_request') |
| 150 |
def test_update_volume_successfully(self, send_request, get, get_token, modify): |
| 151 |
set_module_args(self.set_default_args_pass_check()) |
| 152 |
get.return_value = { |
| 153 |
'name': 'testvol', |
| 154 |
'snapshot_policy_name': 'default', |
| 155 |
'tiering_policy': 'snapshot_only', |
| 156 |
'export_policy_type': 'custom', |
| 157 |
'export_policy_ip': ["10.30.0.1/16"], |
| 158 |
'export_policy_nfs_version': ["nfs3", "nfs4"], |
| 159 |
} |
| 160 |
send_request.side_effect = [({'publicId': 'id', 'svmName': 'svm_name', 'cloudProviderName': "aws", 'isHA': False}, None, None)] |
| 161 |
get_token.return_value = ("type", "token") |
| 162 |
modify.return_value = None |
| 163 |
obj = my_module() |
| 164 |
obj.rest_api.api_root_path = "test_root_path" |
| 165 |
|
| 166 |
with pytest.raises(AnsibleExitJson) as exc: |
| 167 |
obj.apply() |
| 168 |
assert exc.value.args[0]['changed'] |
| 169 |
|
| 170 |
@patch('ansible_collections.netapp.cloudmanager.plugins.modules.na_cloudmanager_volume.NetAppCloudmanagerVolume.modify_volume') |
| 171 |
@patch('ansible_collections.netapp.cloudmanager.plugins.module_utils.netapp.CloudManagerRestAPI.get_token') |
| 172 |
@patch('ansible_collections.netapp.cloudmanager.plugins.modules.na_cloudmanager_volume.NetAppCloudmanagerVolume.get_volume') |
| 173 |
@patch('ansible_collections.netapp.cloudmanager.plugins.module_utils.netapp.CloudManagerRestAPI.send_request') |
| 174 |
def test_update_volume_idempotency(self, send_request, get, get_token, modify): |
| 175 |
set_module_args(self.set_default_args_pass_check()) |
| 176 |
get.return_value = { |
| 177 |
'name': 'testvol', |
| 178 |
'snapshot_policy_name': 'default', |
| 179 |
'export_policy_type': 'custom', |
| 180 |
'export_policy_ip': ["10.30.0.1/16"], |
| 181 |
'export_policy_nfs_version': ["nfs4"], |
| 182 |
} |
| 183 |
send_request.side_effect = [({'publicId': 'id', 'svmName': 'svm_name', 'cloudProviderName': "aws", 'isHA': False}, None, None)] |
| 184 |
get_token.return_value = ("type", "token") |
| 185 |
modify.return_value = None |
| 186 |
obj = my_module() |
| 187 |
obj.rest_api.api_root_path = "test_root_path" |
| 188 |
|
| 189 |
with pytest.raises(AnsibleExitJson) as exc: |
| 190 |
obj.apply() |
| 191 |
assert not exc.value.args[0]['changed'] |
| 192 |
|
| 193 |
@patch('ansible_collections.netapp.cloudmanager.plugins.module_utils.netapp.CloudManagerRestAPI.get_token') |
| 194 |
@patch('ansible_collections.netapp.cloudmanager.plugins.module_utils.netapp_module.NetAppModule.get_working_environment_details_by_name') |
| 195 |
@patch('ansible_collections.netapp.cloudmanager.plugins.modules.na_cloudmanager_volume.NetAppCloudmanagerVolume.get_volume') |
| 196 |
@patch('ansible_collections.netapp.cloudmanager.plugins.modules.na_cloudmanager_volume.NetAppCloudmanagerVolume.create_volume') |
| 197 |
@patch('ansible_collections.netapp.cloudmanager.plugins.module_utils.netapp.CloudManagerRestAPI.send_request') |
| 198 |
def test_create_volume_by_workingenv_name_successfully(self, send_request, create, get, get_we, get_token): |
| 199 |
args = self.set_default_args_with_workingenv_name_pass_check() |
| 200 |
my_we = { |
| 201 |
'name': 'test', |
| 202 |
'publicId': 'test', |
| 203 |
'cloudProviderName': 'Amazon'} |
| 204 |
get_we.return_value = my_we, None |
| 205 |
args['working_environment_id'] = my_we['publicId'] |
| 206 |
set_module_args(args) |
| 207 |
get.return_value = None |
| 208 |
create.return_value = None |
| 209 |
send_request.side_effect = [({'publicId': 'id', 'svmName': 'svm_name', 'cloudProviderName': "aws", 'isHA': False}, None, None)] |
| 210 |
get_token.return_value = ("type", "token") |
| 211 |
obj = my_module() |
| 212 |
obj.rest_api.api_root_path = "test_root_path" |
| 213 |
|
| 214 |
with pytest.raises(AnsibleExitJson) as exc: |
| 215 |
obj.apply() |
| 216 |
assert exc.value.args[0]['changed'] |