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: I203f7369943b948e0462becbddcba65b2d1576f0
Partial-Bug: #1266962
tags/2014.1.rc1
Zhongyue Luo 5 years ago
parent
commit
ed52d4538c

+ 3
- 3
glance/db/sqlalchemy/models.py View File

@@ -76,14 +76,14 @@ class GlanceBase(models.ModelBase, models.TimestampMixin):
76 76
         from glance.db.sqlalchemy import api as db_api
77 77
         super(GlanceBase, self).save(session or db_api.get_session())
78 78
 
79
-    created_at = Column(DateTime, default=timeutils.utcnow,
79
+    created_at = Column(DateTime, default=lambda: timeutils.utcnow(),
80 80
                         nullable=False)
81 81
     # TODO(vsergeyev): Column `updated_at` have no default value in
82 82
     #                  openstack common code. We should decide, is this value
83 83
     #                  required and make changes in oslo (if required) or
84 84
     #                  in glance (if not).
85
-    updated_at = Column(DateTime, default=timeutils.utcnow,
86
-                        nullable=False, onupdate=timeutils.utcnow)
85
+    updated_at = Column(DateTime, default=lambda: timeutils.utcnow(),
86
+                        nullable=False, onupdate=lambda: timeutils.utcnow())
87 87
     # TODO(boris-42): Use SoftDeleteMixin instead of deleted Column after
88 88
     #                 migration that provides UniqueConstraints and change
89 89
     #                 type of this column.

+ 8
- 7
glance/tests/functional/db/base.py View File

@@ -19,6 +19,8 @@ import copy
19 19
 import datetime
20 20
 import uuid
21 21
 
22
+import mock
23
+
22 24
 from glance.common import exception
23 25
 from glance import context
24 26
 from glance.openstack.common import timeutils
@@ -88,7 +90,6 @@ class TestDriver(test_utils.BaseTestCase):
88 90
         db_tests.reset_db(self.db_api)
89 91
         self.fixtures = self.build_image_fixtures()
90 92
         self.create_images(self.fixtures)
91
-        self.addCleanup(timeutils.clear_time_override)
92 93
 
93 94
     def build_image_fixtures(self):
94 95
         dt1 = timeutils.utcnow()
@@ -129,8 +130,9 @@ class DriverTests(object):
129 130
         fixture = {'name': 'mark', 'size': 12, 'status': 'queued'}
130 131
         self.db_api.image_create(self.context, fixture)
131 132
 
132
-    def test_image_create_defaults(self):
133
-        timeutils.set_time_override()
133
+    @mock.patch.object(timeutils, 'utcnow')
134
+    def test_image_create_defaults(self, mock_utcnow):
135
+        mock_utcnow.return_value = datetime.datetime.utcnow()
134 136
         create_time = timeutils.utcnow()
135 137
         values = {'status': 'queued',
136 138
                   'created_at': create_time,
@@ -944,8 +946,9 @@ class DriverTests(object):
944 946
         self.assertRaises(exception.NotFound, self.db_api.image_tag_delete,
945 947
                           self.context, UUID1, 'snap')
946 948
 
947
-    def test_image_member_create(self):
948
-        timeutils.set_time_override()
949
+    @mock.patch.object(timeutils, 'utcnow')
950
+    def test_image_member_create(self, mock_utcnow):
951
+        mock_utcnow.return_value = datetime.datetime.utcnow()
949 952
         memberships = self.db_api.image_member_find(self.context)
950 953
         self.assertEqual([], memberships)
951 954
 
@@ -1167,7 +1170,6 @@ class DriverQuotaTests(test_utils.BaseTestCase):
1167 1170
             auth_tok='%s:%s:user' % (self.owner_id1, self.owner_id1))
1168 1171
         self.db_api = db_tests.get_db(self.config)
1169 1172
         db_tests.reset_db(self.db_api)
1170
-        self.addCleanup(timeutils.clear_time_override)
1171 1173
         dt1 = timeutils.utcnow()
1172 1174
         dt2 = dt1 + datetime.timedelta(microseconds=5)
1173 1175
         fixtures = [
@@ -1267,7 +1269,6 @@ class TaskTests(test_utils.BaseTestCase):
1267 1269
         self.db_api = db_tests.get_db(self.config)
1268 1270
         self.fixtures = self.build_task_fixtures()
1269 1271
         db_tests.reset_db(self.db_api)
1270
-        self.addCleanup(timeutils.clear_time_override)
1271 1272
 
1272 1273
     def build_task_fixtures(self):
1273 1274
         self.context.tenant = str(uuid.uuid4())

+ 7
- 6
glance/tests/unit/test_domain.py View File

@@ -17,6 +17,7 @@
17 17
 import datetime
18 18
 import uuid
19 19
 
20
+import mock
20 21
 from oslo.config import cfg
21 22
 
22 23
 from glance.common import exception
@@ -422,8 +423,9 @@ class TestTask(test_utils.BaseTestCase):
422 423
         self.task.begin_processing()
423 424
         self.assertEqual(self.task.status, 'processing')
424 425
 
425
-    def test_succeed(self):
426
-        timeutils.set_time_override()
426
+    @mock.patch.object(timeutils, 'utcnow')
427
+    def test_succeed(self, mock_utcnow):
428
+        mock_utcnow.return_value = datetime.datetime.utcnow()
427 429
         self.task.begin_processing()
428 430
         self.task.succeed('{"location": "file://home"}')
429 431
         self.assertEqual(self.task.status, 'success')
@@ -433,10 +435,10 @@ class TestTask(test_utils.BaseTestCase):
433 435
             self.task.expires_at,
434 436
             expected
435 437
         )
436
-        timeutils.clear_time_override()
437 438
 
438
-    def test_fail(self):
439
-        timeutils.set_time_override()
439
+    @mock.patch.object(timeutils, 'utcnow')
440
+    def test_fail(self, mock_utcnow):
441
+        mock_utcnow.return_value = datetime.datetime.utcnow()
440 442
         self.task.begin_processing()
441 443
         self.task.fail('{"message": "connection failed"}')
442 444
         self.assertEqual(self.task.status, 'failure')
@@ -446,7 +448,6 @@ class TestTask(test_utils.BaseTestCase):
446 448
             self.task.expires_at,
447 449
             expected
448 450
         )
449
-        timeutils.clear_time_override()
450 451
 
451 452
 
452 453
 class TestTaskDetails(test_utils.BaseTestCase):

+ 5
- 2
glance/tests/unit/test_notifier.py View File

@@ -16,6 +16,7 @@
16 16
 
17 17
 import datetime
18 18
 
19
+import mock
19 20
 import webob
20 21
 
21 22
 from glance.common import exception
@@ -432,11 +433,13 @@ class TestTaskNotifications(utils.BaseTestCase):
432 433
         self.task_details_proxy = notifier.TaskDetailsProxy(self.task_details,
433 434
                                                             self.context,
434 435
                                                             self.notifier)
435
-        timeutils.set_time_override()
436
+        self.patcher = mock.patch.object(timeutils, 'utcnow')
437
+        mock_utcnow = self.patcher.start()
438
+        mock_utcnow.return_value = datetime.datetime.utcnow()
436 439
 
437 440
     def tearDown(self):
438 441
         super(TestTaskNotifications, self).tearDown()
439
-        timeutils.clear_time_override()
442
+        self.patcher.stop()
440 443
 
441 444
     def test_task_create_notification(self):
442 445
         self.task_repo_proxy.add(self.task_proxy, self.task_details_proxy)

Loading…
Cancel
Save