| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
"""Unit Tests for host 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_storagepool_api \ |
| 14 |
import MockStoragePoolApi |
| 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() |
| 26 |
from ansible.module_utils import basic |
| 27 |
basic.AnsibleModule = MagicMock() |
| 28 |
|
| 29 |
from ansible_collections.dellemc.unity.plugins.modules.storagepool import StoragePool |
| 30 |
|
| 31 |
|
| 32 |
class TestUnityStoragePool(): |
| 33 |
|
| 34 |
get_module_args = MockStoragePoolApi.STORAGE_POOL_MODULE_ARGS |
| 35 |
|
| 36 |
@pytest.fixture |
| 37 |
def storagepool_module_mock(self): |
| 38 |
storagepool_module_mock = StoragePool() |
| 39 |
storagepool_module_mock.conn = MagicMock() |
| 40 |
return storagepool_module_mock |
| 41 |
|
| 42 |
def test_get_host_details(self, storagepool_module_mock): |
| 43 |
self.get_module_args.update({ |
| 44 |
'pool_name': 'Ansible_Unity_TEST_1', |
| 45 |
}) |
| 46 |
storagepool_module_mock.module.params = self.get_module_args |
| 47 |
get_pool = MockSDKObject(MockStoragePoolApi.get_pool_details_response('get_pool')) |
| 48 |
get_pool._get_property_from_raw = MagicMock(return_value=MockSDKObject({'is_schedule_enabled': True})) |
| 49 |
get_pool.add_to_skip_list('_get_property_from_raw') |
| 50 |
storagepool_module_mock.conn.get_pool = MagicMock(return_value=get_pool) |
| 51 |
pool_object = MockStoragePoolApi.get_pool_details_response('pool_object') |
| 52 |
utils.UnityPool = MagicMock() |
| 53 |
utils.UnityPool.get = MagicMock(return_value=MockSDKObject(pool_object)) |
| 54 |
disk_list = MockStoragePoolApi.get_pool_details_response('disk_list') |
| 55 |
utils.UnityDiskList = MagicMock() |
| 56 |
utils.UnityDiskList.get = MagicMock(return_value=disk_list) |
| 57 |
storagepool_module_mock.perform_module_operation() |
| 58 |
assert MockStoragePoolApi.get_pool_details_response('module')['storage_pool_details'] == \ |
| 59 |
storagepool_module_mock.module.exit_json.call_args[1]['storage_pool_details'] |
| 60 |
|
| 61 |
def test_get_host_details_throws_exception(self, storagepool_module_mock): |
| 62 |
self.get_module_args.update({ |
| 63 |
'pool_name': 'Ansible_Unity_SP_3', |
| 64 |
}) |
| 65 |
storagepool_module_mock.module.params = self.get_module_args |
| 66 |
storagepool_module_mock.conn.get_pool = MagicMock(side_effect=MockApiException) |
| 67 |
storagepool_module_mock.result = MagicMock() |
| 68 |
storagepool_module_mock.get_pool_drives = MagicMock() |
| 69 |
storagepool_module_mock.perform_module_operation() |
| 70 |
storagepool_module_mock.is_pool_modification_required = MagicMock(return_value=False) |
| 71 |
assert MockStoragePoolApi.get_pool_details_response('error') == storagepool_module_mock.module.fail_json.call_args[1]['msg'] |
| 72 |
|
| 73 |
def test_create_pool(self, storagepool_module_mock): |
| 74 |
self.get_module_args.update({ |
| 75 |
'pool_name': 'test_pool', |
| 76 |
'pool_description': 'Unity test pool.', |
| 77 |
'raid_groups': { |
| 78 |
'disk_group_id': "dg_16", |
| 79 |
'disk_num': 3, |
| 80 |
'raid_type': 'RAID10', |
| 81 |
'stripe_width': 'BEST_FIT', |
| 82 |
}, |
| 83 |
'alert_threshold': 50, |
| 84 |
'is_harvest_enabled': True, |
| 85 |
'pool_harvest_high_threshold': 59, |
| 86 |
'pool_harvest_low_threshold': 40, |
| 87 |
'is_snap_harvest_enabled': True, |
| 88 |
'snap_harvest_high_threshold': 80, |
| 89 |
'snap_harvest_low_threshold': 60, |
| 90 |
'fast_vp': "enabled", |
| 91 |
'fast_cache': "disabled", |
| 92 |
'pool_type': 'TRADITIONAL', |
| 93 |
'state': 'present' |
| 94 |
}) |
| 95 |
storagepool_module_mock.module.params = self.get_module_args |
| 96 |
storagepool_module_mock.get_raid_groups_response = MagicMock(return_value=None) |
| 97 |
storagepool_module_mock.get_details = MagicMock(return_value=None) |
| 98 |
pool_object = MockStoragePoolApi.create_pool_response('api') |
| 99 |
utils.UnityPool = MagicMock() |
| 100 |
utils.UnityPool.create = MagicMock(return_value=MockSDKObject(pool_object)) |
| 101 |
storagepool_module_mock.perform_module_operation() |
| 102 |
assert storagepool_module_mock.module.exit_json.call_args[1]['changed'] |
| 103 |
|
| 104 |
def test_create_pool_throws_exception(self, storagepool_module_mock): |
| 105 |
self.get_module_args.update({ |
| 106 |
'pool_name': 'test_pool', |
| 107 |
'pool_description': 'Unity test pool.', |
| 108 |
'raid_groups': { |
| 109 |
'disk_group_id': "dg_16", |
| 110 |
'disk_num': 3, |
| 111 |
'raid_type': 'RAID10', |
| 112 |
'stripe_width': 'BEST_FIT', |
| 113 |
}, |
| 114 |
'alert_threshold': 50, |
| 115 |
'is_harvest_enabled': True, |
| 116 |
'pool_harvest_high_threshold': 59, |
| 117 |
'pool_harvest_low_threshold': 40, |
| 118 |
'is_snap_harvest_enabled': True, |
| 119 |
'snap_harvest_high_threshold': 80, |
| 120 |
'snap_harvest_low_threshold': 60, |
| 121 |
'fast_vp': "enabled", |
| 122 |
'fast_cache': "disabled", |
| 123 |
'pool_type': 'TRADITIONAL', |
| 124 |
'state': 'present' |
| 125 |
}) |
| 126 |
storagepool_module_mock.module.params = self.get_module_args |
| 127 |
storagepool_module_mock.get_details = MagicMock(return_value=None) |
| 128 |
utils.UnityPool = MagicMock() |
| 129 |
storagepool_module_mock.get_raid_groups_response = MagicMock(side_effect=MockApiException) |
| 130 |
storagepool_module_mock.perform_module_operation() |
| 131 |
assert MockStoragePoolApi.create_pool_response('error') in \ |
| 132 |
storagepool_module_mock.module.fail_json.call_args[1]['msg'] |