Browse Source

Remove use of timeutils.set_time_override

As timeutils.set_time_override() was created to mock utcnow
before 'mock' was generally used, now we should remove it.

Change-Id: I8257a969c14e438a10cf79fa300a18ae3f0128c3
Partial-Bug: #1266962
tags/10.0.0.0b2
Dao Cong Tien 2 years ago
parent
commit
d111600753
2 changed files with 10 additions and 7 deletions
  1. 4
    3
      cinder/tests/unit/message/test_api.py
  2. 6
    4
      cinder/tests/unit/test_volume.py

+ 4
- 3
cinder/tests/unit/message/test_api.py View File

@@ -42,10 +42,10 @@ class MessageApiTest(test.TestCase):
42 42
         self.ext_mgr.extensions = {}
43 43
         self.controller = messages.MessagesController(self.ext_mgr)
44 44
 
45
-    def test_create(self):
45
+    @mock.patch('oslo_utils.timeutils.utcnow')
46
+    def test_create(self, mock_utcnow):
46 47
         CONF.set_override('message_ttl', 300)
47
-        timeutils.set_time_override()
48
-        self.addCleanup(timeutils.clear_time_override)
48
+        mock_utcnow.return_value = datetime.datetime.utcnow()
49 49
         expected_expires_at = timeutils.utcnow() + datetime.timedelta(
50 50
             seconds=300)
51 51
         expected_message_record = {
@@ -64,6 +64,7 @@ class MessageApiTest(test.TestCase):
64 64
 
65 65
         self.message_api.db.message_create.assert_called_once_with(
66 66
             self.ctxt, expected_message_record)
67
+        mock_utcnow.assert_called_with()
67 68
 
68 69
     def test_create_swallows_exception(self):
69 70
         self.mock_object(self.message_api.db, 'create',

+ 6
- 4
cinder/tests/unit/test_volume.py View File

@@ -149,9 +149,9 @@ class AvailabilityZoneTestCase(base.BaseVolumeTestCase):
149 149
         self.assertEqual([{"name": 'a', 'available': False}], list(azs))
150 150
         self.assertIsNone(self.volume_api.availability_zones_last_fetched)
151 151
 
152
-    def test_list_availability_zones_refetched(self):
153
-        timeutils.set_time_override()
154
-        self.addCleanup(timeutils.clear_time_override)
152
+    @mock.patch('oslo_utils.timeutils.utcnow')
153
+    def test_list_availability_zones_refetched(self, mock_utcnow):
154
+        mock_utcnow.return_value = datetime.datetime.utcnow()
155 155
         azs = self.volume_api.list_availability_zones(enable_cache=True)
156 156
         self.assertEqual([{"name": 'a', 'available': True}], list(azs))
157 157
         self.assertIsNotNone(self.volume_api.availability_zones_last_fetched)
@@ -161,7 +161,8 @@ class AvailabilityZoneTestCase(base.BaseVolumeTestCase):
161 161
         self.assertEqual(1, self.get_all.call_count)
162 162
 
163 163
         # The default cache time is 3600, push past that...
164
-        timeutils.advance_time_seconds(3800)
164
+        mock_utcnow.return_value = (timeutils.utcnow() +
165
+                                    datetime.timedelta(0, 3800))
165 166
         self.get_all.return_value = [
166 167
             {
167 168
                 'availability_zone': 'a',
@@ -178,6 +179,7 @@ class AvailabilityZoneTestCase(base.BaseVolumeTestCase):
178 179
         self.assertEqual(2, self.get_all.call_count)
179 180
         self.assertGreater(self.volume_api.availability_zones_last_fetched,
180 181
                            last_fetched)
182
+        mock_utcnow.assert_called_with()
181 183
 
182 184
     def test_list_availability_zones_enabled_service(self):
183 185
         def sort_func(obj):

Loading…
Cancel
Save