Merge "Improve sushy mocks"

This commit is contained in:
Zuul 2018-11-06 04:17:14 +00:00 committed by Gerrit Code Review
commit 819491ee17
5 changed files with 57 additions and 65 deletions

View File

@ -31,10 +31,6 @@ sushy = importutils.try_import('sushy')
INFO_DICT = db_utils.get_test_redfish_info()
class MockedSushyError(Exception):
pass
class RedfishManagementTestCase(db_base.DbTestCase):
def setUp(self):
@ -122,12 +118,13 @@ class RedfishManagementTestCase(db_base.DbTestCase):
fake_system.set_system_boot_source.reset_mock()
mock_get_system.reset_mock()
@mock.patch('ironic.drivers.modules.redfish.management.sushy')
@mock.patch.object(sushy, 'Sushy', autospec=True)
@mock.patch.object(redfish_utils, 'get_system', autospec=True)
def test_set_boot_device_fail(self, mock_get_system, mock_sushy):
fake_system = mock.Mock()
mock_sushy.exceptions.SushyError = MockedSushyError
fake_system.set_system_boot_source.side_effect = MockedSushyError
fake_system.set_system_boot_source.side_effect = (
sushy.exceptions.SushyError()
)
mock_get_system.return_value = fake_system
with task_manager.acquire(self.context, self.node.uuid,
shared=False) as task:
@ -185,12 +182,12 @@ class RedfishManagementTestCase(db_base.DbTestCase):
fake_system.set_system_boot_source.reset_mock()
mock_get_system.reset_mock()
@mock.patch('ironic.drivers.modules.redfish.management.sushy')
@mock.patch.object(sushy, 'Sushy', autospec=True)
@mock.patch.object(redfish_utils, 'get_system', autospec=True)
def test_set_boot_mode_fail(self, mock_get_system, mock_sushy):
fake_system = mock.Mock()
mock_sushy.exceptions.SushyError = MockedSushyError
fake_system.set_system_boot_source.side_effect = MockedSushyError
fake_system.set_system_boot_source.side_effect = (
sushy.exceptions.SushyError)
mock_get_system.return_value = fake_system
with task_manager.acquire(self.context, self.node.uuid,
shared=False) as task:
@ -232,12 +229,12 @@ class RedfishManagementTestCase(db_base.DbTestCase):
fake_system.reset_system.assert_called_once_with(sushy.RESET_NMI)
mock_get_system.assert_called_once_with(task.node)
@mock.patch('ironic.drivers.modules.redfish.management.sushy')
@mock.patch.object(sushy, 'Sushy', autospec=True)
@mock.patch.object(redfish_utils, 'get_system', autospec=True)
def test_inject_nmi_fail(self, mock_get_system, mock_sushy):
fake_system = mock.Mock()
mock_sushy.exceptions.SushyError = MockedSushyError
fake_system.reset_system.side_effect = MockedSushyError
fake_system.reset_system.side_effect = (
sushy.exceptions.SushyError)
mock_get_system.return_value = fake_system
with task_manager.acquire(self.context, self.node.uuid,
shared=False) as task:
@ -245,5 +242,5 @@ class RedfishManagementTestCase(db_base.DbTestCase):
exception.RedfishError, 'Redfish inject NMI',
task.driver.management.inject_nmi, task)
fake_system.reset_system.assert_called_once_with(
mock_sushy.RESET_NMI)
sushy.RESET_NMI)
mock_get_system.assert_called_once_with(task.node)

View File

@ -30,10 +30,6 @@ sushy = importutils.try_import('sushy')
INFO_DICT = db_utils.get_test_redfish_info()
class MockedSushyError(Exception):
pass
@mock.patch('eventlet.greenthread.sleep', lambda _t: None)
class RedfishPowerTestCase(db_base.DbTestCase):
@ -149,12 +145,12 @@ class RedfishPowerTestCase(db_base.DbTestCase):
# Reset mocks
mock_get_system.reset_mock()
@mock.patch('ironic.drivers.modules.redfish.power.sushy')
@mock.patch.object(sushy, 'Sushy', autospec=True)
@mock.patch.object(redfish_utils, 'get_system', autospec=True)
def test_set_power_state_fail(self, mock_get_system, mock_sushy):
fake_system = mock_get_system.return_value
mock_sushy.exceptions.SushyError = MockedSushyError
fake_system.reset_system.side_effect = MockedSushyError()
fake_system.reset_system.side_effect = (
sushy.exceptions.SushyError())
with task_manager.acquire(self.context, self.node.uuid,
shared=False) as task:
@ -209,12 +205,12 @@ class RedfishPowerTestCase(db_base.DbTestCase):
fake_system.reset_system.assert_called_once_with(sushy.RESET_ON)
mock_get_system.assert_called_with(task.node)
@mock.patch('ironic.drivers.modules.redfish.power.sushy')
@mock.patch.object(sushy, 'Sushy', autospec=True)
@mock.patch.object(redfish_utils, 'get_system', autospec=True)
def test_reboot_fail(self, mock_get_system, mock_sushy):
fake_system = mock_get_system.return_value
mock_sushy.exceptions.SushyError = MockedSushyError
fake_system.reset_system.side_effect = MockedSushyError()
fake_system.reset_system.side_effect = (
sushy.exceptions.SushyError())
with task_manager.acquire(self.context, self.node.uuid,
shared=False) as task:

View File

@ -31,14 +31,6 @@ sushy = importutils.try_import('sushy')
INFO_DICT = db_utils.get_test_redfish_info()
class MockedConnectionError(Exception):
pass
class MockedResourceNotFoundError(Exception):
pass
class RedfishUtilsTestCase(db_base.DbTestCase):
def setUp(self):
@ -146,27 +138,24 @@ class RedfishUtilsTestCase(db_base.DbTestCase):
'The value should be a Boolean',
redfish_utils.parse_driver_info, self.node)
@mock.patch('ironic.drivers.modules.redfish.utils.sushy')
@mock.patch.object(sushy, 'Sushy', autospec=True)
@mock.patch('ironic.drivers.modules.redfish.utils.'
'SessionCache.sessions', {})
def test_get_system(self, mock_sushy):
mock_sushy.exceptions.ConnectionError = MockedConnectionError
fake_conn = mock_sushy.Sushy.return_value
fake_conn = mock_sushy.return_value
fake_system = fake_conn.get_system.return_value
response = redfish_utils.get_system(self.node)
self.assertEqual(fake_system, response)
fake_conn.get_system.assert_called_once_with(
'/redfish/v1/Systems/FAKESYSTEM')
@mock.patch('ironic.drivers.modules.redfish.utils.sushy')
@mock.patch.object(sushy, 'Sushy', autospec=True)
@mock.patch('ironic.drivers.modules.redfish.utils.'
'SessionCache.sessions', {})
def test_get_system_resource_not_found(self, mock_sushy):
mock_sushy.exceptions.ConnectionError = MockedConnectionError
fake_conn = mock_sushy.Sushy.return_value
mock_sushy.exceptions.ResourceNotFoundError = (
MockedResourceNotFoundError)
fake_conn.get_system.side_effect = MockedResourceNotFoundError()
fake_conn = mock_sushy.return_value
fake_conn.get_system.side_effect = (
sushy.exceptions.ResourceNotFoundError())
self.assertRaises(exception.RedfishError,
redfish_utils.get_system, self.node)
@ -174,7 +163,7 @@ class RedfishUtilsTestCase(db_base.DbTestCase):
'/redfish/v1/Systems/FAKESYSTEM')
@mock.patch('time.sleep', autospec=True)
@mock.patch('ironic.drivers.modules.redfish.utils.sushy')
@mock.patch.object(sushy, 'Sushy', autospec=True)
@mock.patch('ironic.drivers.modules.redfish.utils.'
'SessionCache.sessions', {})
def test_get_system_resource_connection_error_retry(self, mock_sushy,
@ -182,11 +171,8 @@ class RedfishUtilsTestCase(db_base.DbTestCase):
# Redfish specific configurations
self.config(connection_attempts=3, group='redfish')
fake_conn = mock_sushy.Sushy.return_value
mock_sushy.exceptions.ResourceNotFoundError = (
MockedResourceNotFoundError)
mock_sushy.exceptions.ConnectionError = MockedConnectionError
fake_conn.get_system.side_effect = MockedConnectionError()
fake_conn = mock_sushy.return_value
fake_conn.get_system.side_effect = sushy.exceptions.ConnectionError()
self.assertRaises(exception.RedfishConnectionError,
redfish_utils.get_system, self.node)
@ -200,56 +186,50 @@ class RedfishUtilsTestCase(db_base.DbTestCase):
mock_sleep.assert_called_with(
redfish_utils.CONF.redfish.connection_retry_interval)
@mock.patch('ironic.drivers.modules.redfish.utils.sushy')
@mock.patch.object(sushy, 'Sushy', autospec=True)
@mock.patch('ironic.drivers.modules.redfish.utils.'
'SessionCache.sessions', {})
def test_auth_auto(self, mock_sushy):
mock_sushy.exceptions.ConnectionError = MockedConnectionError
redfish_utils.get_system(self.node)
mock_sushy.Sushy.assert_called_with(
mock_sushy.assert_called_with(
self.parsed_driver_info['address'],
username=self.parsed_driver_info['username'],
password=self.parsed_driver_info['password'],
verify=True)
@mock.patch('ironic.drivers.modules.redfish.utils.sushy')
@mock.patch.object(sushy, 'Sushy', autospec=True)
@mock.patch('ironic.drivers.modules.redfish.utils.'
'SessionCache.sessions', {})
def test_ensure_session_reuse(self, mock_sushy):
mock_sushy.exceptions.ConnectionError = MockedConnectionError
redfish_utils.get_system(self.node)
redfish_utils.get_system(self.node)
self.assertEqual(1, mock_sushy.Sushy.call_count)
self.assertEqual(1, mock_sushy.call_count)
@mock.patch('ironic.drivers.modules.redfish.utils.sushy')
@mock.patch.object(sushy, 'Sushy', autospec=True)
def test_ensure_new_session_address(self, mock_sushy):
mock_sushy.exceptions.ConnectionError = MockedConnectionError
self.node.driver_info['redfish_address'] = 'http://bmc.foo'
redfish_utils.get_system(self.node)
self.node.driver_info['redfish_address'] = 'http://bmc.bar'
redfish_utils.get_system(self.node)
self.assertEqual(2, mock_sushy.Sushy.call_count)
self.assertEqual(2, mock_sushy.call_count)
@mock.patch('ironic.drivers.modules.redfish.utils.sushy')
@mock.patch.object(sushy, 'Sushy', autospec=True)
def test_ensure_new_session_username(self, mock_sushy):
mock_sushy.exceptions.ConnectionError = MockedConnectionError
self.node.driver_info['redfish_username'] = 'foo'
redfish_utils.get_system(self.node)
self.node.driver_info['redfish_username'] = 'bar'
redfish_utils.get_system(self.node)
self.assertEqual(2, mock_sushy.Sushy.call_count)
self.assertEqual(2, mock_sushy.call_count)
@mock.patch('ironic.drivers.modules.redfish.utils.sushy')
@mock.patch.object(sushy, 'Sushy', autospec=True)
@mock.patch('ironic.drivers.modules.redfish.utils.'
'SessionCache.MAX_SESSIONS', 10)
@mock.patch('ironic.drivers.modules.redfish.utils.SessionCache.sessions',
collections.OrderedDict())
def test_expire_old_sessions(self, mock_sushy):
mock_sushy.exceptions.ConnectionError = MockedConnectionError
for num in range(20):
self.node.driver_info['redfish_username'] = 'foo-%d' % num
redfish_utils.get_system(self.node)
self.assertEqual(mock_sushy.Sushy.call_count, 20)
self.assertEqual(mock_sushy.call_count, 20)
self.assertEqual(len(redfish_utils.SessionCache.sessions), 10)

View File

@ -115,7 +115,10 @@ REDFISH_SPEC = (
'redfish',
)
SUSHY_CONSTANTS_SPEC = (
SUSHY_SPEC = (
'auth',
'exceptions',
'Sushy',
'BOOT_SOURCE_TARGET_PXE',
'BOOT_SOURCE_TARGET_HDD',
'BOOT_SOURCE_TARGET_CD',
@ -136,6 +139,12 @@ SUSHY_CONSTANTS_SPEC = (
'BOOT_SOURCE_MODE_UEFI',
)
SUSHY_AUTH_SPEC = (
'BasicAuth',
'SessionAuth',
'SessionOrBasicAuth',
)
XCLARITY_SPEC = (
'client',
'states',

View File

@ -200,7 +200,7 @@ if not imcsdk:
sushy = importutils.try_import('sushy')
if not sushy:
sushy = mock.MagicMock(
spec_set=mock_specs.SUSHY_CONSTANTS_SPEC,
spec_set=mock_specs.SUSHY_SPEC,
BOOT_SOURCE_TARGET_PXE='Pxe',
BOOT_SOURCE_TARGET_HDD='Hdd',
BOOT_SOURCE_TARGET_CD='Cd',
@ -222,6 +222,16 @@ if not sushy:
)
sys.modules['sushy'] = sushy
sys.modules['sushy.exceptions'] = sushy.exceptions
sushy.exceptions.SushyError = (
type('SushyError', (MockKwargsException,), {}))
sushy.exceptions.ConnectionError = (
type('ConnectionError', (MockKwargsException,), {}))
sushy.exceptions.ResourceNotFoundError = (
type('ResourceNotFoundError', (MockKwargsException,), {}))
sushy.auth = mock.MagicMock(spec_set=mock_specs.SUSHY_AUTH_SPEC)
sys.modules['sushy.auth'] = sushy.auth
if 'ironic.drivers.modules.redfish' in sys.modules:
six.moves.reload_module(
sys.modules['ironic.drivers.modules.redfish'])