diff --git a/tests/test_notifier.py b/tests/test_notifier.py index 81637c483..0f8b3cf1c 100644 --- a/tests/test_notifier.py +++ b/tests/test_notifier.py @@ -13,11 +13,13 @@ # License for the specific language governing permissions and limitations # under the License. +import datetime import logging import sys import uuid import fixtures +import mock import testscenarios from oslo import messaging @@ -132,13 +134,12 @@ class TestMessagingNotifier(test_utils.BaseTestCase): self.conf.register_opts(msg_notifier._notifier_opts) - self.addCleanup(timeutils.clear_time_override) - self.logger = self.useFixture(_ReRaiseLoggedExceptionsFixture()).logger self.stubs.Set(_impl_messaging, '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 = [] if self.v1: drivers.append('messaging') @@ -165,7 +166,7 @@ class TestMessagingNotifier(test_utils.BaseTestCase): self.mox.StubOutWithMock(uuid, 'uuid4') uuid.uuid4().AndReturn(message_id) - timeutils.set_time_override() + mock_utcnow.return_value = datetime.datetime.utcnow() message = { 'message_id': str(message_id), @@ -173,7 +174,7 @@ class TestMessagingNotifier(test_utils.BaseTestCase): 'event_type': 'test.notify', 'priority': self.priority.upper(), 'payload': self.payload, - 'timestamp': str(timeutils.utcnow.override_time), + 'timestamp': str(timeutils.utcnow()), } sends = [] @@ -203,9 +204,9 @@ class TestSerializer(test_utils.BaseTestCase): def setUp(self): super(TestSerializer, self).setUp() 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) serializer = msg_serializer.NoOpSerializer() @@ -220,7 +221,7 @@ class TestSerializer(test_utils.BaseTestCase): self.mox.StubOutWithMock(uuid, 'uuid4') 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_entity') @@ -238,7 +239,7 @@ class TestSerializer(test_utils.BaseTestCase): 'event_type': 'test.notify', 'priority': 'INFO', 'payload': 'sbar', - 'timestamp': str(timeutils.utcnow.override_time), + 'timestamp': str(timeutils.utcnow()), } self.assertEqual(_impl_test.NOTIFICATIONS, @@ -250,9 +251,9 @@ class TestLogNotifier(test_utils.BaseTestCase): def setUp(self): super(TestLogNotifier, self).setUp() 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']) transport = _FakeTransport(self.conf) @@ -263,7 +264,7 @@ class TestLogNotifier(test_utils.BaseTestCase): self.mox.StubOutWithMock(uuid, 'uuid4') uuid.uuid4().AndReturn(message_id) - timeutils.set_time_override() + mock_utcnow.return_value = datetime.datetime.utcnow() message = { 'message_id': str(message_id), @@ -271,7 +272,7 @@ class TestLogNotifier(test_utils.BaseTestCase): 'event_type': 'test.notify', 'priority': 'INFO', 'payload': 'bar', - 'timestamp': str(timeutils.utcnow.override_time), + 'timestamp': str(timeutils.utcnow()), } logger = self.mox.CreateMockAnything() diff --git a/tests/test_notifier_logger.py b/tests/test_notifier_logger.py index d6af9c305..708cc95da 100644 --- a/tests/test_notifier_logger.py +++ b/tests/test_notifier_logger.py @@ -12,6 +12,7 @@ # License for the specific language governing permissions and limitations # under the License. +import datetime import logging import logging.config import os @@ -59,16 +60,16 @@ class TestLogNotifier(test_utils.BaseTestCase): def setUp(self): super(TestLogNotifier, self).setUp() - self.addCleanup(timeutils.clear_time_override) self.addCleanup(messaging.notify._impl_test.reset) 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', return_value=test_notifier._FakeTransport(self.conf)): self.logger = messaging.LoggingNotificationHandler('test://') - timeutils.set_time_override() + mock_utcnow.return_value = datetime.datetime.utcnow() levelno = getattr(logging, self.priority.upper(), 42) @@ -86,7 +87,7 @@ class TestLogNotifier(test_utils.BaseTestCase): self.assertEqual(n['priority'], getattr(self, 'queue', self.priority.upper())) 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['payload'], @@ -105,7 +106,8 @@ class TestLogNotifier(test_utils.BaseTestCase): @testtools.skipUnless(hasattr(logging.config, 'dictConfig'), "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', return_value=test_notifier._FakeTransport(self.conf)): 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()) @@ -137,7 +139,7 @@ class TestLogNotifier(test_utils.BaseTestCase): self.assertEqual(n['priority'], getattr(self, 'queue', self.priority.upper())) 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) pathname = __file__ if pathname.endswith(('.pyc', '.pyo')):