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
This commit is contained in:
Zhongyue Luo 2014-01-10 09:07:16 +08:00
parent abfa7ff5e9
commit d3f3fd7759
2 changed files with 23 additions and 20 deletions

View File

@ -13,11 +13,13 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import datetime
import logging import logging
import sys import sys
import uuid import uuid
import fixtures import fixtures
import mock
import testscenarios import testscenarios
from oslo import messaging from oslo import messaging
@ -132,13 +134,12 @@ class TestMessagingNotifier(test_utils.BaseTestCase):
self.conf.register_opts(msg_notifier._notifier_opts) self.conf.register_opts(msg_notifier._notifier_opts)
self.addCleanup(timeutils.clear_time_override)
self.logger = self.useFixture(_ReRaiseLoggedExceptionsFixture()).logger self.logger = self.useFixture(_ReRaiseLoggedExceptionsFixture()).logger
self.stubs.Set(_impl_messaging, 'LOG', self.logger) self.stubs.Set(_impl_messaging, 'LOG', self.logger)
self.stubs.Set(msg_notifier, '_LOG', self.logger) self.stubs.Set(msg_notifier, '_LOG', self.logger)
def test_notifier(self): @mock.patch('oslo.messaging.openstack.common.timeutils.utcnow')
def test_notifier(self, mock_utcnow):
drivers = [] drivers = []
if self.v1: if self.v1:
drivers.append('messaging') drivers.append('messaging')
@ -165,7 +166,7 @@ class TestMessagingNotifier(test_utils.BaseTestCase):
self.mox.StubOutWithMock(uuid, 'uuid4') self.mox.StubOutWithMock(uuid, 'uuid4')
uuid.uuid4().AndReturn(message_id) uuid.uuid4().AndReturn(message_id)
timeutils.set_time_override() mock_utcnow.return_value = datetime.datetime.utcnow()
message = { message = {
'message_id': str(message_id), 'message_id': str(message_id),
@ -173,7 +174,7 @@ class TestMessagingNotifier(test_utils.BaseTestCase):
'event_type': 'test.notify', 'event_type': 'test.notify',
'priority': self.priority.upper(), 'priority': self.priority.upper(),
'payload': self.payload, 'payload': self.payload,
'timestamp': str(timeutils.utcnow.override_time), 'timestamp': str(timeutils.utcnow()),
} }
sends = [] sends = []
@ -203,9 +204,9 @@ class TestSerializer(test_utils.BaseTestCase):
def setUp(self): def setUp(self):
super(TestSerializer, self).setUp() super(TestSerializer, self).setUp()
self.addCleanup(_impl_test.reset) self.addCleanup(_impl_test.reset)
self.addCleanup(timeutils.clear_time_override)
def test_serializer(self): @mock.patch('oslo.messaging.openstack.common.timeutils.utcnow')
def test_serializer(self, mock_utcnow):
transport = _FakeTransport(self.conf) transport = _FakeTransport(self.conf)
serializer = msg_serializer.NoOpSerializer() serializer = msg_serializer.NoOpSerializer()
@ -220,7 +221,7 @@ class TestSerializer(test_utils.BaseTestCase):
self.mox.StubOutWithMock(uuid, 'uuid4') self.mox.StubOutWithMock(uuid, 'uuid4')
uuid.uuid4().AndReturn(message_id) uuid.uuid4().AndReturn(message_id)
timeutils.set_time_override() mock_utcnow.return_value = datetime.datetime.utcnow()
self.mox.StubOutWithMock(serializer, 'serialize_context') self.mox.StubOutWithMock(serializer, 'serialize_context')
self.mox.StubOutWithMock(serializer, 'serialize_entity') self.mox.StubOutWithMock(serializer, 'serialize_entity')
@ -238,7 +239,7 @@ class TestSerializer(test_utils.BaseTestCase):
'event_type': 'test.notify', 'event_type': 'test.notify',
'priority': 'INFO', 'priority': 'INFO',
'payload': 'sbar', 'payload': 'sbar',
'timestamp': str(timeutils.utcnow.override_time), 'timestamp': str(timeutils.utcnow()),
} }
self.assertEqual(_impl_test.NOTIFICATIONS, self.assertEqual(_impl_test.NOTIFICATIONS,
@ -250,9 +251,9 @@ class TestLogNotifier(test_utils.BaseTestCase):
def setUp(self): def setUp(self):
super(TestLogNotifier, self).setUp() super(TestLogNotifier, self).setUp()
self.conf.register_opts(msg_notifier._notifier_opts) self.conf.register_opts(msg_notifier._notifier_opts)
self.addCleanup(timeutils.clear_time_override)
def test_notifier(self): @mock.patch('oslo.messaging.openstack.common.timeutils.utcnow')
def test_notifier(self, mock_utcnow):
self.config(notification_driver=['log']) self.config(notification_driver=['log'])
transport = _FakeTransport(self.conf) transport = _FakeTransport(self.conf)
@ -263,7 +264,7 @@ class TestLogNotifier(test_utils.BaseTestCase):
self.mox.StubOutWithMock(uuid, 'uuid4') self.mox.StubOutWithMock(uuid, 'uuid4')
uuid.uuid4().AndReturn(message_id) uuid.uuid4().AndReturn(message_id)
timeutils.set_time_override() mock_utcnow.return_value = datetime.datetime.utcnow()
message = { message = {
'message_id': str(message_id), 'message_id': str(message_id),
@ -271,7 +272,7 @@ class TestLogNotifier(test_utils.BaseTestCase):
'event_type': 'test.notify', 'event_type': 'test.notify',
'priority': 'INFO', 'priority': 'INFO',
'payload': 'bar', 'payload': 'bar',
'timestamp': str(timeutils.utcnow.override_time), 'timestamp': str(timeutils.utcnow()),
} }
logger = self.mox.CreateMockAnything() logger = self.mox.CreateMockAnything()

View File

@ -12,6 +12,7 @@
# License for the specific language governing permissions and limitations # License for the specific language governing permissions and limitations
# under the License. # under the License.
import datetime
import logging import logging
import logging.config import logging.config
import os import os
@ -59,16 +60,16 @@ class TestLogNotifier(test_utils.BaseTestCase):
def setUp(self): def setUp(self):
super(TestLogNotifier, self).setUp() super(TestLogNotifier, self).setUp()
self.addCleanup(timeutils.clear_time_override)
self.addCleanup(messaging.notify._impl_test.reset) self.addCleanup(messaging.notify._impl_test.reset)
self.config(notification_driver=['test']) self.config(notification_driver=['test'])
def test_logger(self): @mock.patch('oslo.messaging.openstack.common.timeutils.utcnow')
def test_logger(self, mock_utcnow):
with mock.patch('oslo.messaging.transport.get_transport', with mock.patch('oslo.messaging.transport.get_transport',
return_value=test_notifier._FakeTransport(self.conf)): return_value=test_notifier._FakeTransport(self.conf)):
self.logger = messaging.LoggingNotificationHandler('test://') self.logger = messaging.LoggingNotificationHandler('test://')
timeutils.set_time_override() mock_utcnow.return_value = datetime.datetime.utcnow()
levelno = getattr(logging, self.priority.upper(), 42) levelno = getattr(logging, self.priority.upper(), 42)
@ -86,7 +87,7 @@ class TestLogNotifier(test_utils.BaseTestCase):
self.assertEqual(n['priority'], self.assertEqual(n['priority'],
getattr(self, 'queue', self.priority.upper())) getattr(self, 'queue', self.priority.upper()))
self.assertEqual(n['event_type'], 'logrecord') self.assertEqual(n['event_type'], 'logrecord')
self.assertEqual(n['timestamp'], str(timeutils.utcnow.override_time)) self.assertEqual(n['timestamp'], str(timeutils.utcnow()))
self.assertEqual(n['publisher_id'], None) self.assertEqual(n['publisher_id'], None)
self.assertEqual( self.assertEqual(
n['payload'], n['payload'],
@ -105,7 +106,8 @@ class TestLogNotifier(test_utils.BaseTestCase):
@testtools.skipUnless(hasattr(logging.config, 'dictConfig'), @testtools.skipUnless(hasattr(logging.config, 'dictConfig'),
"Need logging.config.dictConfig (Python >= 2.7)") "Need logging.config.dictConfig (Python >= 2.7)")
def test_logging_conf(self): @mock.patch('oslo.messaging.openstack.common.timeutils.utcnow')
def test_logging_conf(self, mock_utcnow):
with mock.patch('oslo.messaging.transport.get_transport', with mock.patch('oslo.messaging.transport.get_transport',
return_value=test_notifier._FakeTransport(self.conf)): return_value=test_notifier._FakeTransport(self.conf)):
logging.config.dictConfig({ logging.config.dictConfig({
@ -125,7 +127,7 @@ class TestLogNotifier(test_utils.BaseTestCase):
}, },
}) })
timeutils.set_time_override() mock_utcnow.return_value = datetime.datetime.utcnow()
levelno = getattr(logging, self.priority.upper()) levelno = getattr(logging, self.priority.upper())
@ -137,7 +139,7 @@ class TestLogNotifier(test_utils.BaseTestCase):
self.assertEqual(n['priority'], self.assertEqual(n['priority'],
getattr(self, 'queue', self.priority.upper())) getattr(self, 'queue', self.priority.upper()))
self.assertEqual(n['event_type'], 'logrecord') self.assertEqual(n['event_type'], 'logrecord')
self.assertEqual(n['timestamp'], str(timeutils.utcnow.override_time)) self.assertEqual(n['timestamp'], str(timeutils.utcnow()))
self.assertEqual(n['publisher_id'], None) self.assertEqual(n['publisher_id'], None)
pathname = __file__ pathname = __file__
if pathname.endswith(('.pyc', '.pyo')): if pathname.endswith(('.pyc', '.pyo')):