| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
"""Unit Tests for FileSystem module on Unity""" |
| 6 |
|
| 7 |
from __future__ import (absolute_import, division, print_function) |
| 8 |
|
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
import pytest |
| 12 |
import re |
| 13 |
from mock.mock import MagicMock |
| 14 |
from ansible_collections.dellemc.unity.tests.unit.plugins.module_utils.mock_filesystem_api \ |
| 15 |
import MockFileSystemApi |
| 16 |
from ansible_collections.dellemc.unity.tests.unit.plugins.module_utils.mock_sdk_response \ |
| 17 |
import MockSDKObject |
| 18 |
from ansible_collections.dellemc.unity.plugins.module_utils.storage.dell \ |
| 19 |
import utils |
| 20 |
|
| 21 |
utils.get_logger = MagicMock() |
| 22 |
utils.UnityReplicationSession = object |
| 23 |
|
| 24 |
from ansible.module_utils import basic |
| 25 |
basic.AnsibleModule = MagicMock() |
| 26 |
|
| 27 |
from ansible_collections.dellemc.unity.plugins.modules.filesystem import Filesystem |
| 28 |
|
| 29 |
|
| 30 |
class TestFileSystem(): |
| 31 |
|
| 32 |
FILE_SYSTEM_MODULE_ARGS = {'filesystem_id': '123', 'filesystem_name': None, 'nas_server_name': None, |
| 33 |
'nas_server_id': None, 'pool_name': None, 'pool_id': None, 'size': None, |
| 34 |
'cap_unit': None, 'quota_config': None, 'snap_schedule_name': None, |
| 35 |
'snap_schedule_id': None, 'replication_params': {}, 'replication_state': None, 'state': None} |
| 36 |
|
| 37 |
@pytest.fixture |
| 38 |
def filesystem_module_mock(self): |
| 39 |
filesystem_module_mock = Filesystem() |
| 40 |
filesystem_module_mock.unity_conn = MagicMock() |
| 41 |
return filesystem_module_mock |
| 42 |
|
| 43 |
def test_enable_fs_replication(self, filesystem_module_mock): |
| 44 |
self.FILE_SYSTEM_MODULE_ARGS.update(MockFileSystemApi.get_replication_params()) |
| 45 |
filesystem_module_mock.module.params = self.FILE_SYSTEM_MODULE_ARGS |
| 46 |
filesystem_response = MockFileSystemApi.get_file_system_response() |
| 47 |
filesystem_response['replicate_with_dst_resource_provisioning'] = MagicMock(return_value=True) |
| 48 |
filesystem_module_mock.perform_module_operation() |
| 49 |
assert filesystem_module_mock.module.exit_json.call_args[1]['changed'] is True |
| 50 |
|
| 51 |
def test_enable_fs_replication_invalid_params(self, filesystem_module_mock): |
| 52 |
self.FILE_SYSTEM_MODULE_ARGS.update(MockFileSystemApi.get_replication_params(False)) |
| 53 |
filesystem_module_mock.module.params = self.FILE_SYSTEM_MODULE_ARGS |
| 54 |
filesystem_module_mock.is_modify_required = MagicMock(return_value=False) |
| 55 |
filesystem_module_mock.perform_module_operation() |
| 56 |
assert "Invalid rpo value" in \ |
| 57 |
filesystem_module_mock.module.fail_json.call_args[1]['msg'] |
| 58 |
|
| 59 |
def test_enable_fs_replication_throws_ex(self, filesystem_module_mock): |
| 60 |
self.FILE_SYSTEM_MODULE_ARGS.update(MockFileSystemApi.get_replication_params()) |
| 61 |
filesystem_module_mock.module.params = self.FILE_SYSTEM_MODULE_ARGS |
| 62 |
filesystem_module_mock.is_modify_required = MagicMock(return_value=False) |
| 63 |
filesystem_response = MockFileSystemApi.get_file_system_response() |
| 64 |
filesystem_response['replicate_with_dst_resource_provisioning'] = MagicMock(side_effect=Exception) |
| 65 |
filesystem_module_mock.get_filesystem = MagicMock(side_effect=[ |
| 66 |
MockSDKObject(filesystem_response)]) |
| 67 |
filesystem_module_mock.get_filesystem_display_attributes = MagicMock(side_effect=[ |
| 68 |
MockSDKObject(filesystem_response)]) |
| 69 |
filesystem_module_mock.perform_module_operation() |
| 70 |
assert "Enabling replication to the filesystem failed with error" in \ |
| 71 |
re.sub(' <.*?>>', '', filesystem_module_mock.module.fail_json.call_args[1]['msg']) |
| 72 |
|
| 73 |
def test_modify_fs_replication(self, filesystem_module_mock): |
| 74 |
self.FILE_SYSTEM_MODULE_ARGS.update(MockFileSystemApi.get_replication_params()) |
| 75 |
filesystem_module_mock.module.params = self.FILE_SYSTEM_MODULE_ARGS |
| 76 |
filesystem_module_mock.perform_module_operation() |
| 77 |
filesystem_module_mock.get_replication_session_on_filter = MagicMock() |
| 78 |
assert filesystem_module_mock.module.exit_json.call_args[1]['changed'] is True |
| 79 |
|
| 80 |
def test_disable_replication(self, filesystem_module_mock): |
| 81 |
self.FILE_SYSTEM_MODULE_ARGS.update({'replication_state': 'disable', 'state': 'present'}) |
| 82 |
filesystem_module_mock.module.params = self.FILE_SYSTEM_MODULE_ARGS |
| 83 |
filesystem_module_mock.get_filesystem_display_attributes = MagicMock() |
| 84 |
filesystem_module_mock.perform_module_operation() |
| 85 |
assert filesystem_module_mock.module.exit_json.call_args[1]['changed'] is True |
| 86 |
|
| 87 |
def test_disable_replication_throws_ex(self, filesystem_module_mock): |
| 88 |
self.FILE_SYSTEM_MODULE_ARGS.update({'replication_state': 'disable', 'state': 'present'}) |
| 89 |
filesystem_module_mock.module.params = self.FILE_SYSTEM_MODULE_ARGS |
| 90 |
filesystem_module_mock.get_replication_session = MagicMock(side_effect=Exception) |
| 91 |
filesystem_module_mock.get_filesystem_display_attributes = MagicMock() |
| 92 |
filesystem_module_mock.perform_module_operation() |
| 93 |
assert "Disabling replication on the filesystem failed with error" in \ |
| 94 |
re.sub(' <.*?>', '', filesystem_module_mock.module.fail_json.call_args[1]['msg']) |