| 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_host_api \ |
| 14 |
import MockHostApi |
| 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 HttpError as http_error |
| 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.host import Host |
| 30 |
|
| 31 |
|
| 32 |
class TestHost(): |
| 33 |
|
| 34 |
get_module_args = MockHostApi.HOST_MODULE_ARGS |
| 35 |
|
| 36 |
@pytest.fixture |
| 37 |
def host_module_mock(self): |
| 38 |
host_module_mock = Host() |
| 39 |
host_module_mock.unity = MagicMock() |
| 40 |
utils.host = MagicMock() |
| 41 |
return host_module_mock |
| 42 |
|
| 43 |
def test_get_host_details(self, host_module_mock): |
| 44 |
host_details = MockHostApi.get_host_details_response('api') |
| 45 |
self.get_module_args.update({ |
| 46 |
'host_name': 'host_name_1', |
| 47 |
}) |
| 48 |
host_module_mock.module.params = self.get_module_args |
| 49 |
host_module_mock.get_host_initiators_list = MagicMock(return_value=MockHostApi.get_host_initiators_list()) |
| 50 |
utils.host.UnityHostList.get = MagicMock(return_value=MockHostApi.get_host_count_response()) |
| 51 |
host_module_mock.unity.get_initiator = MagicMock(side_effect=[host_details['fc_host_initiators'][0], host_details['iscsi_host_initiators'][0]]) |
| 52 |
host_module_mock.unity.get_host = MagicMock(return_value=MockSDKObject(host_details)) |
| 53 |
host_module_mock.perform_module_operation() |
| 54 |
assert MockHostApi.get_host_details_response('module')['host_details'] == host_module_mock.module.exit_json.call_args[1]['host_details'] |
| 55 |
|
| 56 |
def test_get_host_details_throws_exception(self, host_module_mock): |
| 57 |
self.get_module_args.update({ |
| 58 |
'host_name': 'name1' |
| 59 |
}) |
| 60 |
host_module_mock.module.params = self.get_module_args |
| 61 |
utils.HttpError = http_error |
| 62 |
utils.host.UnityHostList.get = MagicMock(side_effect=http_error) |
| 63 |
host_module_mock.create_host = MagicMock(return_value=(False, MagicMock())) |
| 64 |
host_module_mock.perform_module_operation() |
| 65 |
assert MockHostApi.get_host_details_response('error') == host_module_mock.module.fail_json.call_args[1]['msg'] |
| 66 |
|
| 67 |
def test_add_network_address_to_host(self, host_module_mock): |
| 68 |
self.get_module_args.update({ |
| 69 |
'host_name': 'host_name_1', |
| 70 |
'network_address': 'net_add_1', |
| 71 |
'network_address_state': 'present-in-host', |
| 72 |
'state': 'present' |
| 73 |
}) |
| 74 |
host_module_mock.module.params = self.get_module_args |
| 75 |
host_details = MockHostApi.get_host_details_response('api') |
| 76 |
host_module_mock.unity.get_initiator = MagicMock(side_effect=[host_details['fc_host_initiators'][0], host_details['iscsi_host_initiators'][0]]) |
| 77 |
host_module_mock.get_host_initiators_list = MagicMock(return_value=MockHostApi.get_host_initiators_list()) |
| 78 |
host_module_mock.unity.get_host = MagicMock(return_value=MockSDKObject(MockHostApi.get_host_details_after_network_address_addition('api'))) |
| 79 |
host_details = MockSDKObject(host_details) |
| 80 |
host_details.add_ip_port = MagicMock(return_value=None) |
| 81 |
host_details.add_to_skip_list('add_ip_port') |
| 82 |
host_module_mock.get_host_details = MagicMock(return_value=host_details) |
| 83 |
host_module_mock.perform_module_operation() |
| 84 |
assert MockHostApi.get_host_details_after_network_address_addition('module')['host_details'] == \ |
| 85 |
host_module_mock.module.exit_json.call_args[1]['host_details'] |
| 86 |
assert MockHostApi.get_host_details_after_network_address_addition('module')['changed'] == host_module_mock.module.exit_json.call_args[1]['changed'] |
| 87 |
|
| 88 |
def test_add_network_address_to_host_negative(self, host_module_mock): |
| 89 |
self.get_module_args.update({ |
| 90 |
'host_name': 'host_name_1', |
| 91 |
'network_address': 'net_ad$$$$$d_12', |
| 92 |
'network_address_state': 'present-in-host', |
| 93 |
'state': 'present' |
| 94 |
}) |
| 95 |
host_module_mock.module.params = self.get_module_args |
| 96 |
host_details = MockHostApi.get_host_details_response('api') |
| 97 |
host_module_mock.unity.get_initiator = MagicMock(side_effect=[host_details['fc_host_initiators'][0], host_details['iscsi_host_initiators'][0]]) |
| 98 |
host_module_mock.get_host_initiators_list = MagicMock(return_value=MockHostApi.get_host_initiators_list()) |
| 99 |
host_module_mock.manage_network_address = MagicMock(return_value=(None, False)) |
| 100 |
host_module_mock.get_host_details = MagicMock(return_value=MockSDKObject(host_details)) |
| 101 |
host_module_mock.perform_module_operation() |
| 102 |
assert MockHostApi.get_host_details_after_network_address_addition('invalid_address') == \ |
| 103 |
host_module_mock.module.fail_json.call_args[1]['msg'] |
| 104 |
assert host_module_mock.module.exit_json.call_args[1]['changed'] is False |
| 105 |
|
| 106 |
def test_remove_network_address_from_host(self, host_module_mock): |
| 107 |
self.get_module_args.update({ |
| 108 |
'host_name': 'host_name_1', |
| 109 |
'network_address': 'host_name_1', |
| 110 |
'network_address_state': 'absent-in-host', |
| 111 |
'state': 'present' |
| 112 |
}) |
| 113 |
host_module_mock.module.params = self.get_module_args |
| 114 |
host_details = MockHostApi.get_host_details_response('api') |
| 115 |
host_module_mock.unity.get_initiator = MagicMock(side_effect=[host_details['fc_host_initiators'][0], host_details['iscsi_host_initiators'][0]]) |
| 116 |
host_module_mock.get_host_initiators_list = MagicMock(return_value=MockHostApi.get_host_initiators_list()) |
| 117 |
host_module_mock.unity.get_host = MagicMock(return_value=MockSDKObject(MockHostApi.get_host_details_after_network_address_removal('api'))) |
| 118 |
host_details = MockSDKObject(host_details) |
| 119 |
host_details.delete_ip_port = MagicMock(return_value=None) |
| 120 |
host_details.add_to_skip_list('delete_ip_port') |
| 121 |
host_module_mock.get_host_details = MagicMock(return_value=host_details) |
| 122 |
host_module_mock.perform_module_operation() |
| 123 |
assert MockHostApi.get_host_details_after_network_address_removal('module')['host_details'] == \ |
| 124 |
host_module_mock.module.exit_json.call_args[1]['host_details'] |
| 125 |
assert MockHostApi.get_host_details_after_network_address_removal('module')['changed'] == host_module_mock.module.exit_json.call_args[1]['changed'] |
| 126 |
|
| 127 |
def test_remove_network_address_from_host_negative(self, host_module_mock): |
| 128 |
self.get_module_args.update({ |
| 129 |
'host_name': 'host_name_1', |
| 130 |
'network_address': '1.1.1', |
| 131 |
'network_address_state': 'absent-in-host', |
| 132 |
'state': 'present' |
| 133 |
}) |
| 134 |
host_module_mock.module.params = self.get_module_args |
| 135 |
host_details = MockHostApi.get_host_details_response('api') |
| 136 |
host_module_mock.unity.get_initiator = MagicMock(side_effect=[host_details['fc_host_initiators'][0], host_details['iscsi_host_initiators'][0]]) |
| 137 |
host_module_mock.get_host_initiators_list = MagicMock(return_value=MockHostApi.get_host_initiators_list()) |
| 138 |
host_module_mock.manage_network_address = MagicMock(return_value=(None, False)) |
| 139 |
host_module_mock.get_host_details = MagicMock(return_value=MockSDKObject(host_details)) |
| 140 |
host_module_mock.perform_module_operation() |
| 141 |
assert MockHostApi.get_host_details_after_network_address_removal('invalid_IPV4') == \ |
| 142 |
host_module_mock.module.fail_json.call_args[1]['msg'] |
| 143 |
assert host_module_mock.module.exit_json.call_args[1]['changed'] is False |