/usr/lib/python2.7/dist-packages/os_win/tests/utils/storage/initiator/test_fc_utils.py is in python-os-win 0.4.1-2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 | # Copyright 2015 Cloudbase Solutions Srl
# All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
import ctypes
import mock
from oslotest import base
import six
from os_win import exceptions
from os_win.utils.storage.initiator import fc_structures as fc_struct
from os_win.utils.storage.initiator import fc_utils
class FCUtilsTestCase(base.BaseTestCase):
"""Unit tests for the Hyper-V FCUtils class."""
_FAKE_ADAPTER_NAME = 'fake_adapter_name'
_FAKE_ADAPTER_WWN = list(range(8))
@mock.patch.object(fc_utils, 'wmi', create=True)
def setUp(self, mock_wmi):
super(FCUtilsTestCase, self).setUp()
self._setup_lib_mocks()
self._fc_utils = fc_utils.FCUtils()
self._run_mocker = mock.patch.object(self._fc_utils,
'_run_and_check_output')
self._run_mocker.start()
self._mock_run = self._fc_utils._run_and_check_output
self.addCleanup(mock.patch.stopall)
def _setup_lib_mocks(self):
self._ctypes = mock.Mock()
# This is used in order to easily make assertions on the variables
# passed by reference.
self._ctypes.byref = lambda x: (x, "byref")
mock.patch.object(fc_utils, 'hbaapi', create=True).start()
self._ctypes_mocker = mock.patch.object(fc_utils, 'ctypes',
self._ctypes)
self._ctypes_mocker.start()
def test_run_and_check_output(self):
self._run_mocker.stop()
with mock.patch.object(fc_utils.win32utils.Win32Utils,
'run_and_check_output') as mock_win32_run:
self._fc_utils._run_and_check_output(
adapter_name=self._FAKE_ADAPTER_NAME)
mock_win32_run.assert_called_once_with(
adapter_name=self._FAKE_ADAPTER_NAME,
failure_exc=exceptions.FCWin32Exception)
def test_get_fc_hba_count(self):
hba_count = self._fc_utils.get_fc_hba_count()
fc_utils.hbaapi.HBA_GetNumberOfAdapters.assert_called_once_with()
self.assertEqual(fc_utils.hbaapi.HBA_GetNumberOfAdapters.return_value,
hba_count)
def _test_open_adapter(self, adapter_name=None, adapter_wwn=None):
self._ctypes_mocker.stop()
self._mock_run.return_value = mock.sentinel.handle
if adapter_name:
expected_func = fc_utils.hbaapi.HBA_OpenAdapter
elif adapter_wwn:
expected_func = fc_utils.hbaapi.HBA_OpenAdapterByWWN
resulted_handle = self._fc_utils._open_adapter(
adapter_name=adapter_name, adapter_wwn=adapter_wwn)
args_list = self._mock_run.call_args_list[0][0]
self.assertEqual(expected_func, args_list[0])
if adapter_name:
self.assertEqual(six.b(adapter_name),
args_list[1].value)
else:
self.assertEqual(adapter_wwn, list(args_list[1]))
self.assertEqual(mock.sentinel.handle, resulted_handle)
def test_open_adapter_by_name(self):
self._test_open_adapter(adapter_name=self._FAKE_ADAPTER_NAME)
def test_open_adapter_by_wwn(self):
self._test_open_adapter(adapter_wwn=self._FAKE_ADAPTER_WWN)
def test_open_adapter_not_specified(self):
self.assertRaises(exceptions.FCException,
self._fc_utils._open_adapter)
def test_close_adapter(self):
self._fc_utils._close_adapter(mock.sentinel.hba_handle)
fc_utils.hbaapi.HBA_CloseAdapter.assert_called_once_with(
mock.sentinel.hba_handle)
@mock.patch.object(fc_utils.FCUtils, '_open_adapter')
@mock.patch.object(fc_utils.FCUtils, '_close_adapter')
def test_get_hba_handle(self, mock_close_adapter, mock_open_adapter):
with self._fc_utils._get_hba_handle(
adapter_name=self._FAKE_ADAPTER_NAME):
mock_open_adapter.assert_called_once_with(
adapter_name=self._FAKE_ADAPTER_NAME)
mock_close_adapter.assert_called_once_with(
mock_open_adapter.return_value)
@mock.patch.object(ctypes, 'byref')
def test_get_adapter_name(self, mock_byref):
self._ctypes_mocker.stop()
fake_adapter_index = 1
def update_buff(buff):
buff.value = six.b(self._FAKE_ADAPTER_NAME)
mock_byref.side_effect = update_buff
resulted_adapter_name = self._fc_utils._get_adapter_name(
fake_adapter_index)
args_list = self._mock_run.call_args_list[0][0]
self.assertEqual(fc_utils.hbaapi.HBA_GetAdapterName,
args_list[0])
self.assertIsInstance(args_list[1], ctypes.c_uint32)
self.assertEqual(fake_adapter_index, args_list[1].value)
arg_byref = mock_byref.call_args_list[0][0][0]
buff = ctypes.cast(arg_byref, ctypes.POINTER(
ctypes.c_char * 256)).contents
self.assertIsInstance(buff, ctypes.c_char * 256)
self.assertEqual(self._FAKE_ADAPTER_NAME, resulted_adapter_name)
@mock.patch.object(fc_struct, 'get_target_mapping_struct')
def test_get_target_mapping(self, mock_get_target_mapping):
fake_entry_count = 10
hresults = [fc_utils.HBA_STATUS_ERROR_MORE_DATA,
fc_utils.HBA_STATUS_OK]
mock_mapping = mock.Mock(NumberOfEntries=fake_entry_count)
mock_get_target_mapping.return_value = mock_mapping
self._mock_run.side_effect = hresults
resulted_mapping = self._fc_utils._get_target_mapping(
mock.sentinel.hba_handle)
expected_calls = [
mock.call(fc_utils.hbaapi.HBA_GetFcpTargetMapping,
mock.sentinel.hba_handle,
self._ctypes.byref(mock_mapping),
ignored_error_codes=[fc_utils.HBA_STATUS_ERROR_MORE_DATA]
)] * 2
self._mock_run.assert_has_calls(expected_calls)
self.assertEqual(mock_mapping, resulted_mapping)
mock_get_target_mapping.assert_has_calls([mock.call(0),
mock.call(fake_entry_count)])
@mock.patch.object(fc_struct, 'HBA_PortAttributes')
def test_get_adapter_port_attributes(self, mock_class_HBA_PortAttributes):
resulted_port_attributes = self._fc_utils._get_adapter_port_attributes(
mock.sentinel.hba_handle, mock.sentinel.port_index)
self._mock_run.assert_called_once_with(
fc_utils.hbaapi.HBA_GetAdapterPortAttributes,
mock.sentinel.hba_handle,
mock.sentinel.port_index,
self._ctypes.byref(mock_class_HBA_PortAttributes.return_value))
self.assertEqual(mock_class_HBA_PortAttributes.return_value,
resulted_port_attributes)
@mock.patch.object(fc_struct, 'HBA_AdapterAttributes')
def test_get_adapter_attributes(self, mock_class_HBA_AdapterAttributes):
resulted_hba_attributes = self._fc_utils._get_adapter_attributes(
mock.sentinel.hba_handle)
self._mock_run.assert_called_once_with(
fc_utils.hbaapi.HBA_GetAdapterAttributes,
mock.sentinel.hba_handle,
self._ctypes.byref(mock_class_HBA_AdapterAttributes.return_value))
self.assertEqual(mock_class_HBA_AdapterAttributes.return_value,
resulted_hba_attributes)
@mock.patch.object(fc_utils.FCUtils, 'get_fc_hba_count')
def test_get_fc_hba_ports_missing_hbas(self, mock_get_fc_hba_count):
mock_get_fc_hba_count.return_value = 0
resulted_hba_ports = self._fc_utils.get_fc_hba_ports()
self.assertEqual([], resulted_hba_ports)
@mock.patch.object(fc_utils.FCUtils, '_get_fc_hba_adapter_ports')
@mock.patch.object(fc_utils.FCUtils, '_get_adapter_name')
@mock.patch.object(fc_utils.FCUtils, 'get_fc_hba_count')
def test_get_fc_hba_ports(self, mock_get_fc_hba_count,
mock_get_adapter_name,
mock_get_adapter_ports):
fake_adapter_count = 2
mock_get_adapter_name.return_value = mock.sentinel.adapter_name
mock_get_fc_hba_count.return_value = fake_adapter_count
mock_get_adapter_ports.side_effect = [Exception,
[mock.sentinel.port]]
expected_hba_ports = [mock.sentinel.port]
resulted_hba_ports = self._fc_utils.get_fc_hba_ports()
self.assertEqual(expected_hba_ports, resulted_hba_ports)
self.assertEqual(expected_hba_ports, resulted_hba_ports)
mock_get_adapter_name.assert_has_calls(
[mock.call(index) for index in range(fake_adapter_count)])
mock_get_adapter_ports.assert_has_calls(
[mock.call(mock.sentinel.adapter_name)] * fake_adapter_count)
@mock.patch.object(fc_utils.FCUtils, '_open_adapter')
@mock.patch.object(fc_utils.FCUtils, '_close_adapter')
@mock.patch.object(fc_utils.FCUtils, '_get_adapter_port_attributes')
@mock.patch.object(fc_utils.FCUtils, '_get_adapter_attributes')
def test_get_fc_hba_adapter_ports(self, mock_get_adapter_attributes,
mock_get_adapter_port_attributes,
mock_close_adapter,
mock_open_adapter):
fake_port_count = 1
fake_port_index = 0
# Local WWNs
fake_node_wwn = list(range(3))
fake_port_wwn = list(range(3))
mock_adapter_attributes = mock.MagicMock()
mock_adapter_attributes.NumberOfPorts = fake_port_count
mock_port_attributes = mock.MagicMock()
mock_port_attributes.NodeWWN = fake_node_wwn
mock_port_attributes.PortWWN = fake_port_wwn
mock_get_adapter_attributes.return_value = mock_adapter_attributes
mock_get_adapter_port_attributes.return_value = mock_port_attributes
resulted_hba_ports = self._fc_utils._get_fc_hba_adapter_ports(
mock.sentinel.adapter_name)
expected_hba_ports = [{
'node_name': self._fc_utils._wwn_array_to_hex_str(fake_node_wwn),
'port_name': self._fc_utils._wwn_array_to_hex_str(fake_port_wwn)
}]
self.assertEqual(expected_hba_ports, resulted_hba_ports)
mock_open_adapter.assert_called_once_with(
adapter_name=mock.sentinel.adapter_name)
mock_close_adapter.assert_called_once_with(
mock_open_adapter(mock.sentinel.adapter_nam))
mock_get_adapter_attributes.assert_called_once_with(
mock_open_adapter.return_value)
mock_get_adapter_port_attributes.assert_called_once_with(
mock_open_adapter.return_value, fake_port_index)
def test_wwn_hex_string_to_array(self):
fake_wwn_hex_string = '000102'
resulted_array = self._fc_utils._wwn_hex_string_to_array(
fake_wwn_hex_string)
expected_wwn_hex_array = list(range(3))
self.assertEqual(expected_wwn_hex_array, resulted_array)
def test_wwn_array_to_hex_str(self):
fake_wwn_array = list(range(3))
resulted_string = self._fc_utils._wwn_array_to_hex_str(fake_wwn_array)
expected_string = '000102'
self.assertEqual(expected_string, resulted_string)
@mock.patch.object(fc_utils.FCUtils, '_open_adapter')
@mock.patch.object(fc_utils.FCUtils, '_close_adapter')
@mock.patch.object(fc_utils.FCUtils, '_get_target_mapping')
def test_get_fc_target_mapping(self, mock_get_target_mapping,
mock_close_adapter, mock_open_adapter):
# Local WWNN
fake_node_wwn_string = "123"
# Remote WWNs
fake_node_wwn = list(range(3))
fake_port_wwn = list(range(3))
mock_fcp_mappings = mock.MagicMock()
mock_entry = mock.MagicMock()
mock_entry.FcpId.NodeWWN = fake_node_wwn
mock_entry.FcpId.PortWWN = fake_port_wwn
mock_entry.ScsiId.OSDeviceName = mock.sentinel.OSDeviceName
mock_entry.ScsiId.ScsiOSLun = mock.sentinel.ScsiOSLun
mock_fcp_mappings.Entries = [mock_entry]
mock_get_target_mapping.return_value = mock_fcp_mappings
mock_node_wwn = self._fc_utils._wwn_hex_string_to_array(
fake_node_wwn_string)
resulted_mappings = self._fc_utils.get_fc_target_mappings(
fake_node_wwn_string)
expected_mappings = [{
'node_name': self._fc_utils._wwn_array_to_hex_str(fake_node_wwn),
'port_name': self._fc_utils._wwn_array_to_hex_str(fake_port_wwn),
'device_name': mock.sentinel.OSDeviceName,
'lun': mock.sentinel.ScsiOSLun
}]
self.assertEqual(expected_mappings, resulted_mappings)
mock_open_adapter.assert_called_once_with(adapter_wwn=mock_node_wwn)
mock_close_adapter.assert_called_once_with(
mock_open_adapter.return_value)
def test_refresh_hba_configuration(self):
self._fc_utils.refresh_hba_configuration()
expected_func = fc_utils.hbaapi.HBA_RefreshAdapterConfiguration
expected_func.assert_called_once_with()
|