| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
"""Unit Tests for SDC module on PowerFlex""" |
| 6 |
|
| 7 |
from __future__ import (absolute_import, division, print_function) |
| 8 |
|
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
import pytest |
| 12 |
|
| 13 |
from ansible_collections.dellemc.powerflex.tests.unit.plugins.module_utils.libraries import initial_mock |
| 14 |
from mock.mock import MagicMock |
| 15 |
from ansible_collections.dellemc.powerflex.tests.unit.plugins.module_utils.mock_sdc_api import MockSdcApi |
| 16 |
from ansible_collections.dellemc.powerflex.tests.unit.plugins.module_utils.mock_api_exception \ |
| 17 |
import MockApiException |
| 18 |
from ansible_collections.dellemc.powerflex.plugins.module_utils.storage.dell \ |
| 19 |
import utils |
| 20 |
from ansible_collections.dellemc.powerflex.tests.unit.plugins.module_utils.mock_fail_json \ |
| 21 |
import FailJsonException, fail_json |
| 22 |
|
| 23 |
utils.get_logger = MagicMock() |
| 24 |
utils.get_powerflex_gateway_host_connection = MagicMock() |
| 25 |
utils.PowerFlexClient = MagicMock() |
| 26 |
|
| 27 |
from ansible.module_utils import basic |
| 28 |
basic.AnsibleModule = MagicMock() |
| 29 |
from ansible_collections.dellemc.powerflex.plugins.modules.sdc import PowerFlexSdc |
| 30 |
|
| 31 |
|
| 32 |
class TestPowerflexSdc(): |
| 33 |
|
| 34 |
get_module_args = MockSdcApi.COMMON_ARGS |
| 35 |
|
| 36 |
@pytest.fixture |
| 37 |
def sdc_module_mock(self, mocker): |
| 38 |
mocker.patch( |
| 39 |
MockSdcApi.MODULE_UTILS_PATH + '.PowerFlexClient', |
| 40 |
new=MockApiException) |
| 41 |
sdc_module_mock = PowerFlexSdc() |
| 42 |
sdc_module_mock.module.check_mode = False |
| 43 |
sdc_module_mock.module.fail_json = fail_json |
| 44 |
return sdc_module_mock |
| 45 |
|
| 46 |
def capture_fail_json_call(self, error_msg, sdc_module_mock): |
| 47 |
try: |
| 48 |
sdc_module_mock.perform_module_operation() |
| 49 |
except FailJsonException as fj_object: |
| 50 |
assert error_msg in fj_object.message |
| 51 |
|
| 52 |
def test_get_sdc_details(self, sdc_module_mock): |
| 53 |
self.get_module_args.update({ |
| 54 |
"sdc_name": "test_sdc", |
| 55 |
"state": "present" |
| 56 |
}) |
| 57 |
sdc_module_mock.module.params = self.get_module_args |
| 58 |
sdc_module_mock.powerflex_conn.sdc.get = MagicMock( |
| 59 |
return_value=MockSdcApi.get_sdc_details() |
| 60 |
) |
| 61 |
sdc_module_mock.perform_module_operation() |
| 62 |
sdc_module_mock.powerflex_conn.sdc.get.assert_called() |
| 63 |
|
| 64 |
def test_get_sdc_details_empty_sdc_id_exception(self, sdc_module_mock): |
| 65 |
self.get_module_args.update({ |
| 66 |
"sdc_id": " ", |
| 67 |
"state": "present" |
| 68 |
}) |
| 69 |
sdc_module_mock.module.params = self.get_module_args |
| 70 |
self.capture_fail_json_call(MockSdcApi.get_sdc_exception_response( |
| 71 |
'get_sdc_details_empty_sdc_id_exception'), sdc_module_mock) |
| 72 |
|
| 73 |
def test_get_sdc_details_with_exception(self, sdc_module_mock): |
| 74 |
self.get_module_args.update({ |
| 75 |
"sdc_id": MockSdcApi.SDC_ID, |
| 76 |
"state": "present" |
| 77 |
}) |
| 78 |
sdc_module_mock.module.params = self.get_module_args |
| 79 |
sdc_module_mock.powerflex_conn.sdc.get = MagicMock( |
| 80 |
side_effect=MockApiException) |
| 81 |
self.capture_fail_json_call(MockSdcApi.get_sdc_exception_response( |
| 82 |
'get_sdc_details_with_exception'), sdc_module_mock) |
| 83 |
|
| 84 |
def test_get_sdc_details_mapped_volumes_with_exception(self, sdc_module_mock): |
| 85 |
self.get_module_args.update({ |
| 86 |
"sdc_id": MockSdcApi.SDC_ID, |
| 87 |
"state": "present" |
| 88 |
}) |
| 89 |
sdc_module_mock.module.params = self.get_module_args |
| 90 |
sdc_module_mock.powerflex_conn.sdc.get = MagicMock( |
| 91 |
return_value=MockSdcApi.get_sdc_details() |
| 92 |
) |
| 93 |
sdc_module_mock.powerflex_conn.sdc.get_mapped_volumes = MagicMock( |
| 94 |
side_effect=MockApiException) |
| 95 |
self.capture_fail_json_call(MockSdcApi.get_sdc_exception_response( |
| 96 |
'get_sdc_details_mapped_volumes_with_exception'), sdc_module_mock) |
| 97 |
|
| 98 |
def test_modify_sdc(self, sdc_module_mock): |
| 99 |
self.get_module_args.update({ |
| 100 |
"sdc_name": "test_sdc", |
| 101 |
"performance_profile": "Compact", |
| 102 |
"state": "present" |
| 103 |
}) |
| 104 |
sdc_module_mock.module.params = self.get_module_args |
| 105 |
sdc_module_mock.powerflex_conn.sdc.get = MagicMock( |
| 106 |
return_value=MockSdcApi.get_sdc_details() |
| 107 |
) |
| 108 |
sdc_module_mock.powerflex_conn.sdc.set_performance_profile = MagicMock(return_value=True) |
| 109 |
sdc_module_mock.perform_module_operation() |
| 110 |
sdc_module_mock.powerflex_conn.sdc.set_performance_profile.assert_called() |
| 111 |
|
| 112 |
def test_modify_sdc_throws_exception(self, sdc_module_mock): |
| 113 |
self.get_module_args.update({ |
| 114 |
"sdc_name": "test_sdc", |
| 115 |
"performance_profile": "Compact", |
| 116 |
"state": "present" |
| 117 |
}) |
| 118 |
sdc_module_mock.module.params = self.get_module_args |
| 119 |
sdc_module_mock.powerflex_conn.sdc.get = MagicMock( |
| 120 |
return_value=MockSdcApi.get_sdc_details() |
| 121 |
) |
| 122 |
sdc_module_mock.powerflex_conn.sdc.set_performance_profile = MagicMock( |
| 123 |
side_effect=MockApiException) |
| 124 |
self.capture_fail_json_call(MockSdcApi.get_sdc_exception_response( |
| 125 |
'modify_sdc_throws_exception'), sdc_module_mock) |
| 126 |
|
| 127 |
def test_rename_sdc(self, sdc_module_mock): |
| 128 |
self.get_module_args.update({ |
| 129 |
"sdc_name": "test_sdc", |
| 130 |
"sdc_new_name": "test_sdc_renamed", |
| 131 |
"state": "present" |
| 132 |
}) |
| 133 |
sdc_module_mock.module.params = self.get_module_args |
| 134 |
sdc_module_mock.powerflex_conn.sdc.get = MagicMock( |
| 135 |
return_value=MockSdcApi.get_sdc_details() |
| 136 |
) |
| 137 |
sdc_module_mock.powerflex_conn.sdc.rename = MagicMock(return_value=True) |
| 138 |
sdc_module_mock.perform_module_operation() |
| 139 |
sdc_module_mock.powerflex_conn.sdc.rename.assert_called() |
| 140 |
|
| 141 |
def test_rename_sdc_empty_new_name_exception(self, sdc_module_mock): |
| 142 |
self.get_module_args.update({ |
| 143 |
"sdc_name": "test_sdc", |
| 144 |
"sdc_new_name": " ", |
| 145 |
"state": "present" |
| 146 |
}) |
| 147 |
sdc_module_mock.module.params = self.get_module_args |
| 148 |
sdc_module_mock.powerflex_conn.sdc.get = MagicMock( |
| 149 |
return_value=MockSdcApi.get_sdc_details() |
| 150 |
) |
| 151 |
self.capture_fail_json_call(MockSdcApi.get_sdc_exception_response( |
| 152 |
'rename_sdc_empty_new_name_exception'), sdc_module_mock) |
| 153 |
|
| 154 |
def test_rename_sdc_throws_exception(self, sdc_module_mock): |
| 155 |
self.get_module_args.update({ |
| 156 |
"sdc_name": "test_sdc", |
| 157 |
"sdc_new_name": "test_sdc_renamed", |
| 158 |
"state": "present" |
| 159 |
}) |
| 160 |
sdc_module_mock.module.params = self.get_module_args |
| 161 |
sdc_module_mock.powerflex_conn.sdc.get = MagicMock( |
| 162 |
return_value=MockSdcApi.get_sdc_details() |
| 163 |
) |
| 164 |
sdc_module_mock.powerflex_conn.sdc.rename = MagicMock(side_effect=MockApiException) |
| 165 |
self.capture_fail_json_call(MockSdcApi.get_sdc_exception_response( |
| 166 |
'rename_sdc_throws_exception'), sdc_module_mock) |
| 167 |
|
| 168 |
def test_remove_sdc(self, sdc_module_mock): |
| 169 |
self.get_module_args.update({ |
| 170 |
"sdc_name": "test_sdc", |
| 171 |
"state": "absent" |
| 172 |
}) |
| 173 |
sdc_module_mock.module.params = self.get_module_args |
| 174 |
sdc_module_mock.powerflex_conn.sdc.get = MagicMock( |
| 175 |
return_value=MockSdcApi.get_sdc_details() |
| 176 |
) |
| 177 |
sdc_module_mock.powerflex_conn.sdc.delete = MagicMock(return_value=True) |
| 178 |
sdc_module_mock.perform_module_operation() |
| 179 |
sdc_module_mock.powerflex_conn.sdc.delete.assert_called() |
| 180 |
|
| 181 |
def test_remove_sdc_throws_exception(self, sdc_module_mock): |
| 182 |
self.get_module_args.update({ |
| 183 |
"sdc_ip": "1.1.1.1", |
| 184 |
"state": "absent" |
| 185 |
}) |
| 186 |
sdc_module_mock.module.params = self.get_module_args |
| 187 |
sdc_module_mock.powerflex_conn.sdc.get = MagicMock( |
| 188 |
return_value=MockSdcApi.get_sdc_details() |
| 189 |
) |
| 190 |
sdc_module_mock.powerflex_conn.sdc.delete = MagicMock(side_effect=MockApiException) |
| 191 |
self.capture_fail_json_call(MockSdcApi.get_sdc_exception_response( |
| 192 |
'remove_sdc_throws_exception'), sdc_module_mock) |