]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Trim 12s from disco unit tests
authorTom Barron <tpb@dyncloud.net>
Sat, 27 Feb 2016 10:20:18 +0000 (05:20 -0500)
committerTom Barron <tpb@dyncloud.net>
Sat, 27 Feb 2016 16:14:53 +0000 (11:14 -0500)
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

cinder/tests/unit/utils.py
cinder/tests/unit/volume/drivers/disco/test_create_cloned_volume.py
cinder/tests/unit/volume/drivers/disco/test_create_snapshot.py
cinder/tests/unit/volume/drivers/disco/test_create_volume_from_snapshot.py

index fdb720b799e56db9283f81579f9408bd182dfd90..b643456f2830ea8b53e03d40b01b704f813a2733 100644 (file)
@@ -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
index cfabf6cb4f33a4891c85256311b522aff97df2ba..c41aa3965f64329b0109bba9463b89f62d6d6df1 100644 (file)
 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'])
index 13087293e7694b45df2305926839b39709a92bb3..898ecd6e48b11ef8a37fe389288a1941c4e61d6b 100644 (file)
 
 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'])
index 2f89f312b957367d21848dfebcaed68aee4abe53..8895ab89cf8a206010e3b07d40b90feb3ebd2c4a 100644 (file)
 
 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'])