| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
"""Unit Tests for consistency group module on Unity""" |
| 6 |
|
| 7 |
from __future__ import (absolute_import, division, print_function) |
| 8 |
|
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
import pytest |
| 12 |
from mock.mock import MagicMock |
| 13 |
from ansible_collections.dellemc.unity.tests.unit.plugins.module_utils.mock_consistencygroup_api \ |
| 14 |
import MockConsistenyGroupApi |
| 15 |
from ansible_collections.dellemc.unity.tests.unit.plugins.module_utils.mock_sdk_response \ |
| 16 |
import MockSDKObject |
| 17 |
from ansible_collections.dellemc.unity.tests.unit.plugins.module_utils.mock_api_exception \ |
| 18 |
import MockApiException |
| 19 |
from ansible_collections.dellemc.unity.plugins.module_utils.storage.dell \ |
| 20 |
import utils |
| 21 |
|
| 22 |
utils.get_logger = MagicMock() |
| 23 |
utils.get_unity_management_host_parameters = MagicMock() |
| 24 |
utils.ensure_required_libs = MagicMock() |
| 25 |
utils.get_unity_unisphere_connection = MagicMock(side_effect=[MagicMock(), |
| 26 |
MockConsistenyGroupApi.get_remote_system_conn_response()]) |
| 27 |
from ansible.module_utils import basic |
| 28 |
basic.AnsibleModule = MagicMock() |
| 29 |
|
| 30 |
from ansible_collections.dellemc.unity.plugins.modules.consistencygroup import ConsistencyGroup |
| 31 |
|
| 32 |
|
| 33 |
class TestConsistencyGroup(): |
| 34 |
|
| 35 |
get_module_args = MockConsistenyGroupApi.CONSISTENCY_GROUP_MODULE_ARGS |
| 36 |
|
| 37 |
@pytest.fixture |
| 38 |
def consistencygroup_module_mock(self): |
| 39 |
consistencygroup_module_mock = ConsistencyGroup() |
| 40 |
consistencygroup_module_mock.unity_conn = MagicMock() |
| 41 |
utils.cg = MagicMock() |
| 42 |
return consistencygroup_module_mock |
| 43 |
|
| 44 |
def test_enable_cg_replication(self, consistencygroup_module_mock): |
| 45 |
self.get_module_args.update({ |
| 46 |
'cg_name': 'lun_test_cg_source_12', |
| 47 |
'replication_params': { |
| 48 |
'destination_cg_name': 'destination_cg_1', |
| 49 |
'replication_mode': 'asynchronous', |
| 50 |
'rpo': 60, |
| 51 |
'replication_type': 'remote', |
| 52 |
'remote_system': { |
| 53 |
'remote_system_host': '11.111.11.11', |
| 54 |
'remote_system_verifycert': False, |
| 55 |
'remote_system_username': 'username', |
| 56 |
'remote_system_password': 'password', |
| 57 |
'remote_system_port': 1111 |
| 58 |
}, |
| 59 |
'destination_pool_name': 'pool_test_name_1', |
| 60 |
'destination_pool_id': None |
| 61 |
}, |
| 62 |
'replication_state': 'enable', |
| 63 |
'state': 'present' |
| 64 |
}) |
| 65 |
consistencygroup_module_mock.module.params = self.get_module_args |
| 66 |
cg_details = MockConsistenyGroupApi.cg_get_details_method_response() |
| 67 |
cg_object = MockConsistenyGroupApi.get_cg_object() |
| 68 |
consistencygroup_module_mock.unity_conn.get_cg = MagicMock(return_value=cg_object) |
| 69 |
consistencygroup_module_mock.get_details = MagicMock(side_effect=[ |
| 70 |
cg_details, |
| 71 |
MockConsistenyGroupApi.get_cg_replication_dependent_response('cg_replication_enabled_details')]) |
| 72 |
cg_object.get_id = MagicMock(return_value=cg_details['id']) |
| 73 |
utils.cg.UnityConsistencyGroup.get = MagicMock(return_value=cg_object) |
| 74 |
cg_object.check_cg_is_replicated = MagicMock(return_value=False) |
| 75 |
consistencygroup_module_mock.unity_conn.get_remote_system = \ |
| 76 |
MagicMock(return_value=MockConsistenyGroupApi.get_cg_replication_dependent_response('remote_system')) |
| 77 |
utils.UnityStorageResource = MagicMock(return_value=MockSDKObject({})) |
| 78 |
cg_object.replicate_cg_with_dst_resource_provisioning = MagicMock(return_value=None) |
| 79 |
consistencygroup_module_mock.perform_module_operation() |
| 80 |
assert consistencygroup_module_mock.module.exit_json.call_args[1]['consistency_group_details']['cg_replication_enabled'] is True |
| 81 |
assert consistencygroup_module_mock.module.exit_json.call_args[1]['changed'] is True |
| 82 |
|
| 83 |
def test_enable_cg_replication_negative_1(self, consistencygroup_module_mock): |
| 84 |
self.get_module_args.update({ |
| 85 |
'cg_name': 'lun_test_cg_source_12', |
| 86 |
'replication_params': { |
| 87 |
'destination_cg_name': '', |
| 88 |
'replication_mode': 'asynchronous', |
| 89 |
'rpo': 60, |
| 90 |
'replication_type': 'local', |
| 91 |
'destination_pool_name': None, |
| 92 |
'destination_pool_id': 'pool_test_1' |
| 93 |
}, |
| 94 |
'replication_state': 'enable', |
| 95 |
'state': 'present' |
| 96 |
}) |
| 97 |
consistencygroup_module_mock.module.params = self.get_module_args |
| 98 |
cg_details = MockConsistenyGroupApi.cg_get_details_method_response() |
| 99 |
cg_object = MockConsistenyGroupApi.get_cg_object() |
| 100 |
consistencygroup_module_mock.unity_conn.get_cg = MagicMock(return_value=cg_object) |
| 101 |
consistencygroup_module_mock.get_details = MagicMock(side_effect=[ |
| 102 |
cg_details, |
| 103 |
MockConsistenyGroupApi.get_cg_replication_dependent_response('cg_replication_enabled_details')]) |
| 104 |
cg_object.get_id = MagicMock(return_value=cg_details['id']) |
| 105 |
utils.cg.UnityConsistencyGroup.get = MagicMock(return_value=cg_object) |
| 106 |
cg_object.check_cg_is_replicated = MagicMock(return_value=False) |
| 107 |
consistencygroup_module_mock.unity_conn.get_remote_system = \ |
| 108 |
MagicMock(return_value=MockConsistenyGroupApi.get_cg_replication_dependent_response('remote_system')) |
| 109 |
utils.UnityStorageResource = MagicMock(return_value=MockSDKObject({})) |
| 110 |
cg_object.replicate_cg_with_dst_resource_provisioning = MagicMock(return_value=None) |
| 111 |
consistencygroup_module_mock.perform_module_operation() |
| 112 |
assert consistencygroup_module_mock.module.fail_json.call_args[1]['msg'] == \ |
| 113 |
MockConsistenyGroupApi.get_cg_replication_dependent_response('destination_cg_name_validation') |
| 114 |
|
| 115 |
def test_enable_cg_replication_negative_2(self, consistencygroup_module_mock): |
| 116 |
self.get_module_args.update({ |
| 117 |
'cg_name': 'lun_test_cg_source_12', |
| 118 |
'replication_params': { |
| 119 |
'destination_cg_name': 'destination_cg_1', |
| 120 |
'replication_mode': 'asynchronous', |
| 121 |
'rpo': 60, |
| 122 |
'replication_type': 'remote', |
| 123 |
'remote_system': { |
| 124 |
'remote_system_host': '11.111.11.11', |
| 125 |
'remote_system_verifycert': False, |
| 126 |
'remote_system_username': 'username', |
| 127 |
'remote_system_password': 'password', |
| 128 |
'remote_system_port': 1111 |
| 129 |
}, |
| 130 |
'destination_pool_name': None, |
| 131 |
'destination_pool_id': 'pool_test_1' |
| 132 |
}, |
| 133 |
'replication_state': 'enable', |
| 134 |
'state': 'present' |
| 135 |
}) |
| 136 |
consistencygroup_module_mock.module.params = self.get_module_args |
| 137 |
cg_details = MockConsistenyGroupApi.cg_get_details_method_response() |
| 138 |
cg_object = MockConsistenyGroupApi.get_cg_object() |
| 139 |
consistencygroup_module_mock.unity_conn.get_cg = MagicMock(return_value=cg_object) |
| 140 |
consistencygroup_module_mock.get_details = MagicMock(side_effect=[ |
| 141 |
cg_details, |
| 142 |
MockConsistenyGroupApi.get_cg_replication_dependent_response('cg_replication_enabled_details')]) |
| 143 |
cg_object.get_id = MagicMock(return_value=cg_details['id']) |
| 144 |
utils.cg.UnityConsistencyGroup.get = MagicMock(return_value=cg_object) |
| 145 |
cg_object.check_cg_is_replicated = MagicMock(return_value=False) |
| 146 |
consistencygroup_module_mock.unity_conn.get_remote_system = MagicMock(side_effect=MockApiException) |
| 147 |
consistencygroup_module_mock.perform_module_operation() |
| 148 |
assert consistencygroup_module_mock.module.fail_json.call_args[1]['msg'] == \ |
| 149 |
MockConsistenyGroupApi.get_cg_replication_dependent_response('enable_cg_exception') |
| 150 |
|
| 151 |
def test_disable_cg_replication(self, consistencygroup_module_mock): |
| 152 |
self.get_module_args.update({ |
| 153 |
'cg_name': 'lun_test_cg_source_12', |
| 154 |
'replication_state': 'disable', |
| 155 |
'state': 'present' |
| 156 |
}) |
| 157 |
consistencygroup_module_mock.module.params = self.get_module_args |
| 158 |
cg_details = MockConsistenyGroupApi.cg_get_details_method_response() |
| 159 |
cg_object = MockConsistenyGroupApi.get_cg_object() |
| 160 |
consistencygroup_module_mock.unity_conn.get_cg = MagicMock(return_value=cg_object) |
| 161 |
consistencygroup_module_mock.get_details = MagicMock(side_effect=[ |
| 162 |
MockConsistenyGroupApi.get_cg_replication_dependent_response('cg_replication_enabled_details'), |
| 163 |
cg_details]) |
| 164 |
cg_object.get_id = MagicMock(return_value=cg_details['id']) |
| 165 |
utils.cg.UnityConsistencyGroup.get = MagicMock(return_value=cg_object) |
| 166 |
cg_object.check_cg_is_replicated = MagicMock(return_value=True) |
| 167 |
repl_session = MockConsistenyGroupApi.get_cg_replication_dependent_response('replication_session') |
| 168 |
repl_session.delete = MagicMock(return_value=None) |
| 169 |
consistencygroup_module_mock.unity_conn.get_replication_session = \ |
| 170 |
MagicMock(return_value=[repl_session]) |
| 171 |
consistencygroup_module_mock.perform_module_operation() |
| 172 |
assert consistencygroup_module_mock.module.exit_json.call_args[1]['consistency_group_details']['cg_replication_enabled'] is False |
| 173 |
assert consistencygroup_module_mock.module.exit_json.call_args[1]['changed'] is True |
| 174 |
|
| 175 |
def test_disable_cg_replication_throws_exception(self, consistencygroup_module_mock): |
| 176 |
self.get_module_args.update({ |
| 177 |
'cg_name': 'lun_test_cg_source_12', |
| 178 |
'replication_state': 'disable', |
| 179 |
'state': 'present' |
| 180 |
}) |
| 181 |
consistencygroup_module_mock.module.params = self.get_module_args |
| 182 |
cg_details = MockConsistenyGroupApi.cg_get_details_method_response() |
| 183 |
cg_object = MockConsistenyGroupApi.get_cg_object() |
| 184 |
consistencygroup_module_mock.unity_conn.get_cg = MagicMock(return_value=cg_object) |
| 185 |
consistencygroup_module_mock.get_details = MagicMock(side_effect=[ |
| 186 |
MockConsistenyGroupApi.get_cg_replication_dependent_response('cg_replication_enabled_details'), |
| 187 |
cg_details]) |
| 188 |
cg_object.get_id = MagicMock(return_value=cg_details['id']) |
| 189 |
utils.cg.UnityConsistencyGroup.get = MagicMock(return_value=cg_object) |
| 190 |
cg_object.check_cg_is_replicated = MagicMock(side_effect=MockApiException) |
| 191 |
consistencygroup_module_mock.perform_module_operation() |
| 192 |
assert consistencygroup_module_mock.module.fail_json.call_args[1]['msg'] == \ |
| 193 |
MockConsistenyGroupApi.get_cg_replication_dependent_response('disable_cg_exception') |