Browse Source

Removes use of timeutils.set_time_override

The set_time_override function in timeutils was written as a
helper function to mock utcnow for unittests before 'mock' was
generally used. Now that we have mock and fixture, we no longer
need to use it.

Change-Id: I057d4bc3a8b1d4e96e6830cd3071dc96a05496dc
Partial-Bug: #1266962
tags/2014.1.b3
Zhongyue Luo 5 years ago
parent
commit
e3eb709b08
2 changed files with 20 additions and 17 deletions
  1. 9
    6
      cinder/tests/test_quota.py
  2. 11
    11
      cinder/tests/test_utils.py

+ 9
- 6
cinder/tests/test_quota.py View File

@@ -18,6 +18,7 @@
18 18
 
19 19
 import datetime
20 20
 
21
+import mock
21 22
 from oslo.config import cfg
22 23
 
23 24
 from cinder import context
@@ -737,11 +738,10 @@ class DbQuotaDriverTestCase(test.TestCase):
737 738
 
738 739
         self.calls = []
739 740
 
740
-        timeutils.set_time_override()
741
-
742
-    def tearDown(self):
743
-        timeutils.clear_time_override()
744
-        super(DbQuotaDriverTestCase, self).tearDown()
741
+        patcher = mock.patch.object(timeutils, 'utcnow')
742
+        self.addCleanup(patcher.stop)
743
+        self.mock_utcnow = patcher.start()
744
+        self.mock_utcnow.return_value = datetime.datetime.utcnow()
745 745
 
746 746
     def test_get_defaults(self):
747 747
         # Use our pre-defined resources
@@ -1163,7 +1163,10 @@ class QuotaReserveSqlAlchemyTestCase(test.TestCase):
1163 1163
         self.stubs.Set(sqa_api, '_quota_usage_create', fake_quota_usage_create)
1164 1164
         self.stubs.Set(sqa_api, '_reservation_create', fake_reservation_create)
1165 1165
 
1166
-        timeutils.set_time_override()
1166
+        patcher = mock.patch.object(timeutils, 'utcnow')
1167
+        self.addCleanup(patcher.stop)
1168
+        self.mock_utcnow = patcher.start()
1169
+        self.mock_utcnow.return_value = datetime.datetime.utcnow()
1167 1170
 
1168 1171
     def _make_quota_usage(self, project_id, resource, in_use, reserved,
1169 1172
                           until_refresh, created_at, updated_at):

+ 11
- 11
cinder/tests/test_utils.py View File

@@ -22,6 +22,7 @@ import StringIO
22 22
 import tempfile
23 23
 import uuid
24 24
 
25
+import mock
25 26
 import mox
26 27
 from oslo.config import cfg
27 28
 import paramiko
@@ -614,17 +615,16 @@ class AuditPeriodTest(test.TestCase):
614 615
     def setUp(self):
615 616
         super(AuditPeriodTest, self).setUp()
616 617
         #a fairly random time to test with
617
-        self.test_time = datetime.datetime(second=23,
618
-                                           minute=12,
619
-                                           hour=8,
620
-                                           day=5,
621
-                                           month=3,
622
-                                           year=2012)
623
-        timeutils.set_time_override(override_time=self.test_time)
624
-
625
-    def tearDown(self):
626
-        timeutils.clear_time_override()
627
-        super(AuditPeriodTest, self).tearDown()
618
+        test_time = datetime.datetime(second=23,
619
+                                      minute=12,
620
+                                      hour=8,
621
+                                      day=5,
622
+                                      month=3,
623
+                                      year=2012)
624
+        patcher = mock.patch.object(timeutils, 'utcnow')
625
+        self.addCleanup(patcher.stop)
626
+        self.mock_utcnow = patcher.start()
627
+        self.mock_utcnow.return_value = test_time
628 628
 
629 629
     def test_hour(self):
630 630
         begin, end = utils.last_completed_audit_period(unit='hour')

Loading…
Cancel
Save