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: I9a0727edab12ccd5f1e4eb4f5f62d588f5a0faee
Partial-Bug: #1266962
tags/1.3.0a3^0
Zhongyue Luo 5 years ago
parent
commit
d3f3fd7759
2 changed files with 23 additions and 20 deletions
  1. 14
    13
      tests/test_notifier.py
  2. 9
    7
      tests/test_notifier_logger.py

+ 14
- 13
tests/test_notifier.py View File

@@ -13,11 +13,13 @@
13 13
 #    License for the specific language governing permissions and limitations
14 14
 #    under the License.
15 15
 
16
+import datetime
16 17
 import logging
17 18
 import sys
18 19
 import uuid
19 20
 
20 21
 import fixtures
22
+import mock
21 23
 import testscenarios
22 24
 
23 25
 from oslo import messaging
@@ -132,13 +134,12 @@ class TestMessagingNotifier(test_utils.BaseTestCase):
132 134
 
133 135
         self.conf.register_opts(msg_notifier._notifier_opts)
134 136
 
135
-        self.addCleanup(timeutils.clear_time_override)
136
-
137 137
         self.logger = self.useFixture(_ReRaiseLoggedExceptionsFixture()).logger
138 138
         self.stubs.Set(_impl_messaging, 'LOG', self.logger)
139 139
         self.stubs.Set(msg_notifier, '_LOG', self.logger)
140 140
 
141
-    def test_notifier(self):
141
+    @mock.patch('oslo.messaging.openstack.common.timeutils.utcnow')
142
+    def test_notifier(self, mock_utcnow):
142 143
         drivers = []
143 144
         if self.v1:
144 145
             drivers.append('messaging')
@@ -165,7 +166,7 @@ class TestMessagingNotifier(test_utils.BaseTestCase):
165 166
         self.mox.StubOutWithMock(uuid, 'uuid4')
166 167
         uuid.uuid4().AndReturn(message_id)
167 168
 
168
-        timeutils.set_time_override()
169
+        mock_utcnow.return_value = datetime.datetime.utcnow()
169 170
 
170 171
         message = {
171 172
             'message_id': str(message_id),
@@ -173,7 +174,7 @@ class TestMessagingNotifier(test_utils.BaseTestCase):
173 174
             'event_type': 'test.notify',
174 175
             'priority': self.priority.upper(),
175 176
             'payload': self.payload,
176
-            'timestamp': str(timeutils.utcnow.override_time),
177
+            'timestamp': str(timeutils.utcnow()),
177 178
         }
178 179
 
179 180
         sends = []
@@ -203,9 +204,9 @@ class TestSerializer(test_utils.BaseTestCase):
203 204
     def setUp(self):
204 205
         super(TestSerializer, self).setUp()
205 206
         self.addCleanup(_impl_test.reset)
206
-        self.addCleanup(timeutils.clear_time_override)
207 207
 
208
-    def test_serializer(self):
208
+    @mock.patch('oslo.messaging.openstack.common.timeutils.utcnow')
209
+    def test_serializer(self, mock_utcnow):
209 210
         transport = _FakeTransport(self.conf)
210 211
 
211 212
         serializer = msg_serializer.NoOpSerializer()
@@ -220,7 +221,7 @@ class TestSerializer(test_utils.BaseTestCase):
220 221
         self.mox.StubOutWithMock(uuid, 'uuid4')
221 222
         uuid.uuid4().AndReturn(message_id)
222 223
 
223
-        timeutils.set_time_override()
224
+        mock_utcnow.return_value = datetime.datetime.utcnow()
224 225
 
225 226
         self.mox.StubOutWithMock(serializer, 'serialize_context')
226 227
         self.mox.StubOutWithMock(serializer, 'serialize_entity')
@@ -238,7 +239,7 @@ class TestSerializer(test_utils.BaseTestCase):
238 239
             'event_type': 'test.notify',
239 240
             'priority': 'INFO',
240 241
             'payload': 'sbar',
241
-            'timestamp': str(timeutils.utcnow.override_time),
242
+            'timestamp': str(timeutils.utcnow()),
242 243
         }
243 244
 
244 245
         self.assertEqual(_impl_test.NOTIFICATIONS,
@@ -250,9 +251,9 @@ class TestLogNotifier(test_utils.BaseTestCase):
250 251
     def setUp(self):
251 252
         super(TestLogNotifier, self).setUp()
252 253
         self.conf.register_opts(msg_notifier._notifier_opts)
253
-        self.addCleanup(timeutils.clear_time_override)
254 254
 
255
-    def test_notifier(self):
255
+    @mock.patch('oslo.messaging.openstack.common.timeutils.utcnow')
256
+    def test_notifier(self, mock_utcnow):
256 257
         self.config(notification_driver=['log'])
257 258
 
258 259
         transport = _FakeTransport(self.conf)
@@ -263,7 +264,7 @@ class TestLogNotifier(test_utils.BaseTestCase):
263 264
         self.mox.StubOutWithMock(uuid, 'uuid4')
264 265
         uuid.uuid4().AndReturn(message_id)
265 266
 
266
-        timeutils.set_time_override()
267
+        mock_utcnow.return_value = datetime.datetime.utcnow()
267 268
 
268 269
         message = {
269 270
             'message_id': str(message_id),
@@ -271,7 +272,7 @@ class TestLogNotifier(test_utils.BaseTestCase):
271 272
             'event_type': 'test.notify',
272 273
             'priority': 'INFO',
273 274
             'payload': 'bar',
274
-            'timestamp': str(timeutils.utcnow.override_time),
275
+            'timestamp': str(timeutils.utcnow()),
275 276
         }
276 277
 
277 278
         logger = self.mox.CreateMockAnything()

+ 9
- 7
tests/test_notifier_logger.py View File

@@ -12,6 +12,7 @@
12 12
 #    License for the specific language governing permissions and limitations
13 13
 #    under the License.
14 14
 
15
+import datetime
15 16
 import logging
16 17
 import logging.config
17 18
 import os
@@ -59,16 +60,16 @@ class TestLogNotifier(test_utils.BaseTestCase):
59 60
 
60 61
     def setUp(self):
61 62
         super(TestLogNotifier, self).setUp()
62
-        self.addCleanup(timeutils.clear_time_override)
63 63
         self.addCleanup(messaging.notify._impl_test.reset)
64 64
         self.config(notification_driver=['test'])
65 65
 
66
-    def test_logger(self):
66
+    @mock.patch('oslo.messaging.openstack.common.timeutils.utcnow')
67
+    def test_logger(self, mock_utcnow):
67 68
         with mock.patch('oslo.messaging.transport.get_transport',
68 69
                         return_value=test_notifier._FakeTransport(self.conf)):
69 70
             self.logger = messaging.LoggingNotificationHandler('test://')
70 71
 
71
-        timeutils.set_time_override()
72
+        mock_utcnow.return_value = datetime.datetime.utcnow()
72 73
 
73 74
         levelno = getattr(logging, self.priority.upper(), 42)
74 75
 
@@ -86,7 +87,7 @@ class TestLogNotifier(test_utils.BaseTestCase):
86 87
         self.assertEqual(n['priority'],
87 88
                          getattr(self, 'queue', self.priority.upper()))
88 89
         self.assertEqual(n['event_type'], 'logrecord')
89
-        self.assertEqual(n['timestamp'], str(timeutils.utcnow.override_time))
90
+        self.assertEqual(n['timestamp'], str(timeutils.utcnow()))
90 91
         self.assertEqual(n['publisher_id'], None)
91 92
         self.assertEqual(
92 93
             n['payload'],
@@ -105,7 +106,8 @@ class TestLogNotifier(test_utils.BaseTestCase):
105 106
 
106 107
     @testtools.skipUnless(hasattr(logging.config, 'dictConfig'),
107 108
                           "Need logging.config.dictConfig (Python >= 2.7)")
108
-    def test_logging_conf(self):
109
+    @mock.patch('oslo.messaging.openstack.common.timeutils.utcnow')
110
+    def test_logging_conf(self, mock_utcnow):
109 111
         with mock.patch('oslo.messaging.transport.get_transport',
110 112
                         return_value=test_notifier._FakeTransport(self.conf)):
111 113
             logging.config.dictConfig({
@@ -125,7 +127,7 @@ class TestLogNotifier(test_utils.BaseTestCase):
125 127
                 },
126 128
             })
127 129
 
128
-        timeutils.set_time_override()
130
+        mock_utcnow.return_value = datetime.datetime.utcnow()
129 131
 
130 132
         levelno = getattr(logging, self.priority.upper())
131 133
 
@@ -137,7 +139,7 @@ class TestLogNotifier(test_utils.BaseTestCase):
137 139
         self.assertEqual(n['priority'],
138 140
                          getattr(self, 'queue', self.priority.upper()))
139 141
         self.assertEqual(n['event_type'], 'logrecord')
140
-        self.assertEqual(n['timestamp'], str(timeutils.utcnow.override_time))
142
+        self.assertEqual(n['timestamp'], str(timeutils.utcnow()))
141 143
         self.assertEqual(n['publisher_id'], None)
142 144
         pathname = __file__
143 145
         if pathname.endswith(('.pyc', '.pyo')):

Loading…
Cancel
Save