From d111600753ce7c1bfa8b1ad1c8e0ddf908555abc Mon Sep 17 00:00:00 2001 From: Dao Cong Tien Date: Tue, 15 Nov 2016 16:42:27 +0700 Subject: [PATCH] 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 --- cinder/tests/unit/message/test_api.py | 7 ++++--- cinder/tests/unit/test_volume.py | 10 ++++++---- 2 files changed, 10 insertions(+), 7 deletions(-) diff --git a/cinder/tests/unit/message/test_api.py b/cinder/tests/unit/message/test_api.py index 59fb83facaa..ef7c134808f 100644 --- a/cinder/tests/unit/message/test_api.py +++ b/cinder/tests/unit/message/test_api.py @@ -42,10 +42,10 @@ class MessageApiTest(test.TestCase): self.ext_mgr.extensions = {} self.controller = messages.MessagesController(self.ext_mgr) - def test_create(self): + @mock.patch('oslo_utils.timeutils.utcnow') + def test_create(self, mock_utcnow): CONF.set_override('message_ttl', 300) - timeutils.set_time_override() - self.addCleanup(timeutils.clear_time_override) + mock_utcnow.return_value = datetime.datetime.utcnow() expected_expires_at = timeutils.utcnow() + datetime.timedelta( seconds=300) expected_message_record = { @@ -64,6 +64,7 @@ class MessageApiTest(test.TestCase): self.message_api.db.message_create.assert_called_once_with( self.ctxt, expected_message_record) + mock_utcnow.assert_called_with() def test_create_swallows_exception(self): self.mock_object(self.message_api.db, 'create', diff --git a/cinder/tests/unit/test_volume.py b/cinder/tests/unit/test_volume.py index 65a20236c40..c16003f486f 100644 --- a/cinder/tests/unit/test_volume.py +++ b/cinder/tests/unit/test_volume.py @@ -149,9 +149,9 @@ class AvailabilityZoneTestCase(base.BaseVolumeTestCase): self.assertEqual([{"name": 'a', 'available': False}], list(azs)) self.assertIsNone(self.volume_api.availability_zones_last_fetched) - def test_list_availability_zones_refetched(self): - timeutils.set_time_override() - self.addCleanup(timeutils.clear_time_override) + @mock.patch('oslo_utils.timeutils.utcnow') + def test_list_availability_zones_refetched(self, mock_utcnow): + mock_utcnow.return_value = datetime.datetime.utcnow() azs = self.volume_api.list_availability_zones(enable_cache=True) self.assertEqual([{"name": 'a', 'available': True}], list(azs)) self.assertIsNotNone(self.volume_api.availability_zones_last_fetched) @@ -161,7 +161,8 @@ class AvailabilityZoneTestCase(base.BaseVolumeTestCase): self.assertEqual(1, self.get_all.call_count) # The default cache time is 3600, push past that... - timeutils.advance_time_seconds(3800) + mock_utcnow.return_value = (timeutils.utcnow() + + datetime.timedelta(0, 3800)) self.get_all.return_value = [ { 'availability_zone': 'a', @@ -178,6 +179,7 @@ class AvailabilityZoneTestCase(base.BaseVolumeTestCase): self.assertEqual(2, self.get_all.call_count) self.assertGreater(self.volume_api.availability_zones_last_fetched, last_fetched) + mock_utcnow.assert_called_with() def test_list_availability_zones_enabled_service(self): def sort_func(obj):