From: Tom Barron Date: Sat, 27 Feb 2016 10:20:18 +0000 (-0500) Subject: Trim 12s from disco unit tests X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=c2166876d85b17872c6fe32916c6d0d1dc974847;p=openstack-build%2Fcinder-build.git Trim 12s from disco unit tests The unit tests under cinder/tests/unit/volume/drivers/disco contain three "timeout" tests, each of which runs more than 4s while waiting for timeouts to expire. This commit mocks time.time() in those tests using a utility method that ensures an immediate timeout. Change-Id: I8453e0231d7563d70732816bd3931140bcc01f4f --- diff --git a/cinder/tests/unit/utils.py b/cinder/tests/unit/utils.py index fdb720b79..b643456f2 100644 --- a/cinder/tests/unit/utils.py +++ b/cinder/tests/unit/utils.py @@ -245,3 +245,18 @@ def get_file_spec(): set(dir(_io.BytesIO)))) else: file_spec = file + + +def generate_timeout_series(timeout): + """Generate a series of times that exceeds the given timeout. + + Yields a series of fake time.time() floating point numbers + such that the difference between each pair in the series just + exceeds the timeout value that is passed in. Useful for + mocking time.time() in methods that otherwise wait for timeout + seconds. + """ + iteration = 0 + while True: + iteration += 1 + yield (iteration * timeout) + iteration diff --git a/cinder/tests/unit/volume/drivers/disco/test_create_cloned_volume.py b/cinder/tests/unit/volume/drivers/disco/test_create_cloned_volume.py index cfabf6cb4..c41aa3965 100644 --- a/cinder/tests/unit/volume/drivers/disco/test_create_cloned_volume.py +++ b/cinder/tests/unit/volume/drivers/disco/test_create_cloned_volume.py @@ -17,10 +17,12 @@ import copy import mock import six +import time from cinder import exception from cinder.tests.unit import fake_volume +from cinder.tests.unit import utils as utils from cinder.tests.unit.volume.drivers import disco @@ -137,9 +139,12 @@ class CreateCloneVolumeTestCase(disco.TestDISCODriver): self.test_pending = True self.test_create_cloned_volume() - def test_create_cloned_volume_timeout(self): + @mock.patch.object(time, 'time') + def test_create_cloned_volume_timeout(self, mock_time): """Clone request timeout.""" - self.driver.configuration.clone_check_timeout = 3 + timeout = 3 + mock_time.side_effect = utils.generate_timeout_series(timeout) + self.driver.configuration.clone_check_timeout = timeout self.response = self.FAKE_SOAP_RESPONSE['standard']['success'] self.response_detail = ( self.FAKE_SOAP_RESPONSE['clone_detail']['pending']) diff --git a/cinder/tests/unit/volume/drivers/disco/test_create_snapshot.py b/cinder/tests/unit/volume/drivers/disco/test_create_snapshot.py index 13087293e..898ecd6e4 100644 --- a/cinder/tests/unit/volume/drivers/disco/test_create_snapshot.py +++ b/cinder/tests/unit/volume/drivers/disco/test_create_snapshot.py @@ -17,10 +17,12 @@ import copy import mock +import time from cinder import db from cinder import exception from cinder.tests.unit import fake_snapshot +from cinder.tests.unit import utils as utils from cinder.tests.unit.volume.drivers import disco @@ -138,9 +140,12 @@ class CreateSnapshotTestCase(disco.TestDISCODriver): self.test_pending = True self.test_create_snapshot() - def test_create_snapshot_timeout(self): + @mock.patch.object(time, 'time') + def test_create_snapshot_timeout(self, mock_time): """Snapshot request timeout.""" - self.driver.configuration.snapshot_check_timeout = 3 + timeout = 3 + mock_time.side_effect = utils.generate_timeout_series(timeout) + self.driver.configuration.snapshot_check_timeout = timeout self.response = self.FAKE_SOAP_RESPONSE['standard']['success'] self.response_detail = ( self.FAKE_SOAP_RESPONSE['snapshot_detail']['pending']) diff --git a/cinder/tests/unit/volume/drivers/disco/test_create_volume_from_snapshot.py b/cinder/tests/unit/volume/drivers/disco/test_create_volume_from_snapshot.py index 2f89f312b..8895ab89c 100644 --- a/cinder/tests/unit/volume/drivers/disco/test_create_volume_from_snapshot.py +++ b/cinder/tests/unit/volume/drivers/disco/test_create_volume_from_snapshot.py @@ -16,9 +16,11 @@ import copy import mock +import time from cinder import exception from cinder.tests.unit import fake_snapshot +from cinder.tests.unit import utils as utils from cinder.tests.unit.volume.drivers import disco @@ -145,9 +147,12 @@ class CreateVolumeFromSnapshotTestCase(disco.TestDISCODriver): self.test_pending = True self.test_create_volume_from_snapshot() - def test_create_volume_from_snapshot_timeout(self): + @mock.patch.object(time, 'time') + def test_create_volume_from_snapshot_timeout(self, mock_time): """Create volume from snapshot task timeout.""" - self.driver.configuration.restore_check_timeout = 3 + timeout = 3 + mock_time.side_effect = utils.generate_timeout_series(timeout) + self.driver.configuration.restore_check_timeout = timeout self.response = self.FAKE_SOAP_RESPONSE['standard']['success'] self.response_detail = ( self.FAKE_SOAP_RESPONSE['restore_detail']['pending'])