Mock sleep in unit tests that rely on it

This change fixes the worst offenders, potentially reducing the test
runs by more than 10 seconds.

I could not fix iLO tests though because they heavily rely on time.sleep
being precise.

Change-Id: I10d7845700275d9d03b98ebadd0f12540f1e7656
This commit is contained in:
Dmitry Tantsur 2023-06-07 15:30:59 +02:00
parent 97f7177495
commit ca5b2feeee
3 changed files with 11 additions and 4 deletions

View File

@ -19,6 +19,7 @@ from unittest import mock
from dracclient import constants as drac_constants
from dracclient import exceptions as drac_exceptions
from oslo_service import loopingcall
from ironic.common import exception
from ironic.common import states
@ -69,8 +70,9 @@ class DracPowerTestCase(test_utils.BaseDracTest):
mock_client.get_power_state.assert_called_once_with()
@mock.patch.object(loopingcall.BackOffLoopingCall, '_sleep', autospec=True)
@mock.patch.object(drac_power.LOG, 'warning', autospec=True)
def test_set_power_state(self, mock_log, mock_get_drac_client):
def test_set_power_state(self, mock_log, mock_sleep, mock_get_drac_client):
mock_client = mock_get_drac_client.return_value
mock_client.get_power_state.side_effect = [drac_constants.POWER_ON,
drac_constants.POWER_OFF]
@ -96,8 +98,10 @@ class DracPowerTestCase(test_utils.BaseDracTest):
drac_power_state = drac_power.REVERSE_POWER_STATES[states.POWER_OFF]
mock_client.set_power_state.assert_called_once_with(drac_power_state)
@mock.patch.object(loopingcall.BackOffLoopingCall, '_sleep', autospec=True)
@mock.patch.object(drac_power.LOG, 'warning', autospec=True)
def test_set_power_state_timeout(self, mock_log, mock_get_drac_client):
def test_set_power_state_timeout(self, mock_log, mock_sleep,
mock_get_drac_client):
mock_client = mock_get_drac_client.return_value
mock_client.get_power_state.side_effect = [drac_constants.POWER_ON,
drac_constants.POWER_OFF]

View File

@ -132,8 +132,9 @@ class IBMCUtilsTestCase(base.IBMCTestCase):
"value2": "key2"
}, revert)
@mock.patch('time.sleep', autospec=True)
@mock.patch.object(ibmc_client, 'connect', spec=object)
def test_handle_ibmc_exception_retry(self, connect_ibmc):
def test_handle_ibmc_exception_retry(self, connect_ibmc, mock_sleep):
@utils.handle_ibmc_exception('get IBMC system')
def get_ibmc_system(_task):

View File

@ -523,7 +523,8 @@ class TestAgentClient(base.TestCase):
self.assertRaises(exception.InvalidParameterValue,
self.client._command, self.node, method, params)
def test__command_poll(self):
@mock.patch('time.sleep', autospec=True)
def test__command_poll(self, mock_sleep):
response_data = {'status': 'ok'}
final_status = MockCommandStatus('SUCCEEDED', name='run_image')
self.client.session.post.return_value = MockResponse(response_data)
@ -553,6 +554,7 @@ class TestAgentClient(base.TestCase):
verify=True)
self.client.session.get.assert_called_with(url, timeout=60,
verify=True)
mock_sleep.assert_called_with(CONF.agent.command_wait_interval)
def test_get_commands_status(self):
if not mock._is_instance_mock(self.client.session):