Merge "Huawei: Simplify wait_for_condition calls"

This commit is contained in:
Zuul 2018-12-07 13:45:30 +00:00 committed by Gerrit Code Review
commit b56ebd44b2
2 changed files with 19 additions and 27 deletions

View File

@ -2299,6 +2299,12 @@ class HuaweiTestBase(test.TestCase):
self.group = fake_group.fake_group_obj( self.group = fake_group.fake_group_obj(
admin_contex, id=ID, status='available') admin_contex, id=ID, status='available')
constants.DEFAULT_REPLICA_WAIT_INTERVAL = .1
constants.DEFAULT_REPLICA_WAIT_TIMEOUT = .5
constants.DEFAULT_WAIT_INTERVAL = .1
constants.DEFAULT_WAIT_TIMEOUT = .5
constants.MIGRATION_WAIT_INTERVAL = .1
def test_encode_name(self): def test_encode_name(self):
lun_name = huawei_utils.encode_name(self.volume.id) lun_name = huawei_utils.encode_name(self.volume.id)
self.assertEqual('21ec7341-ca82ece92e1ac480c963f1', lun_name) self.assertEqual('21ec7341-ca82ece92e1ac480c963f1', lun_name)
@ -4117,7 +4123,7 @@ class HuaweiISCSIDriverTestCase(HuaweiTestBase):
replica.wait_volume_online(self.driver.client, lun_info) replica.wait_volume_online(self.driver.client, lun_info)
with mock.patch.object(rest_client.RestClient, 'get_lun_info', with mock.patch.object(rest_client.RestClient, 'get_lun_info',
offline_status): return_value=offline_status):
self.assertRaises(exception.VolumeBackendAPIException, self.assertRaises(exception.VolumeBackendAPIException,
replica.wait_volume_online, replica.wait_volume_online,
self.driver.client, self.driver.client,
@ -4131,16 +4137,11 @@ class HuaweiISCSIDriverTestCase(HuaweiTestBase):
common_driver = replication.ReplicaCommonDriver(self.configuration, op) common_driver = replication.ReplicaCommonDriver(self.configuration, op)
self.mock_object(replication.PairOp, 'get_replica_info', self.mock_object(replication.PairOp, 'get_replica_info',
return_value={'SECRESACCESS': access_ro}) return_value={'SECRESACCESS': access_ro})
self.mock_object(huawei_utils.time, 'time',
side_effect=utils.generate_timeout_series(
constants.DEFAULT_REPLICA_WAIT_TIMEOUT))
common_driver.wait_second_access(pair_id, access_ro) common_driver.wait_second_access(pair_id, access_ro)
self.assertRaises(exception.VolumeBackendAPIException, self.assertRaises(exception.VolumeBackendAPIException,
common_driver.wait_second_access, pair_id, access_rw) common_driver.wait_second_access, pair_id, access_rw)
@mock.patch('oslo_service.loopingcall.FixedIntervalLoopingCall',
new=utils.ZeroIntervalLoopingCall)
def test_wait_replica_ready(self): def test_wait_replica_ready(self):
normal_status = { normal_status = {
'RUNNINGSTATUS': constants.REPLICA_RUNNING_STATUS_NORMAL, 'RUNNINGSTATUS': constants.REPLICA_RUNNING_STATUS_NORMAL,

View File

@ -15,12 +15,11 @@
import hashlib import hashlib
import json import json
import six
import time
from oslo_log import log as logging from oslo_log import log as logging
from oslo_service import loopingcall
from oslo_utils import units from oslo_utils import units
import retrying
import six
from cinder import exception from cinder import exception
from cinder.i18n import _ from cinder.i18n import _
@ -62,25 +61,17 @@ def old_encode_host_name(name):
def wait_for_condition(func, interval, timeout): def wait_for_condition(func, interval, timeout):
start_time = time.time()
def _inner(): r = retrying.Retrying(retry_on_result=lambda result: not result,
try: retry_on_exception=lambda result: False,
res = func() wait_fixed=interval * 1000,
except Exception as ex: stop_max_delay=timeout * 1000)
raise exception.VolumeBackendAPIException(data=ex) try:
r.call(func)
if res: except retrying.RetryError:
raise loopingcall.LoopingCallDone() msg = _('wait_for_condition: %s timed out.') % func.__name__
LOG.error(msg)
if int(time.time()) - start_time > timeout: raise exception.VolumeBackendAPIException(data=msg)
msg = (_('wait_for_condition: %s timed out.')
% func.__name__)
LOG.error(msg)
raise exception.VolumeBackendAPIException(data=msg)
timer = loopingcall.FixedIntervalLoopingCall(_inner)
timer.start(interval=interval).wait()
def get_volume_size(volume): def get_volume_size(volume):