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: I1af631f36c813740dbf22da030c2c1f50265d156
Partial-Bug: #1266962
tags/2014.1.rc1
Mikhail Durnosvistov 5 years ago
parent
commit
f356607a95
3 changed files with 18 additions and 11 deletions
  1. 0
    3
      keystone/tests/core.py
  2. 10
    5
      keystone/tests/test_auth.py
  3. 8
    3
      keystone/tests/test_keystoneclient.py

+ 0
- 3
keystone/tests/core.py View File

@@ -63,7 +63,6 @@ from keystone.openstack.common.db.sqlalchemy import migration
63 63
 from keystone.openstack.common.db.sqlalchemy import session
64 64
 from keystone.openstack.common.fixture import config as config_fixture
65 65
 from keystone.openstack.common import log
66
-from keystone.openstack.common import timeutils
67 66
 from keystone import service
68 67
 from keystone.tests import fixtures as ksfixtures
69 68
 
@@ -348,8 +347,6 @@ class TestCase(testtools.TestCase):
348 347
 
349 348
         self.addCleanup(kvs.INMEMDB.clear)
350 349
 
351
-        self.addCleanup(timeutils.clear_time_override)
352
-
353 350
         # Ensure Notification subscriotions and resource types are empty
354 351
         self.addCleanup(notifications.SUBSCRIBERS.clear)
355 352
         self.addCleanup(notifications._reset_notifier)

+ 10
- 5
keystone/tests/test_auth.py View File

@@ -16,6 +16,8 @@ import copy
16 16
 import datetime
17 17
 import uuid
18 18
 
19
+import mock
20
+
19 21
 from keystone import assignment
20 22
 from keystone import auth
21 23
 from keystone.common import authorization
@@ -911,9 +913,12 @@ class AuthWithTrust(AuthTest):
911 913
 
912 914
 
913 915
 class TokenExpirationTest(AuthTest):
914
-    def _maintain_token_expiration(self):
916
+
917
+    @mock.patch.object(timeutils, 'utcnow')
918
+    def _maintain_token_expiration(self, mock_utcnow):
915 919
         """Token expiration should be maintained after re-auth & validation."""
916
-        timeutils.set_time_override()
920
+        now = datetime.datetime.utcnow()
921
+        mock_utcnow.return_value = now
917 922
 
918 923
         r = self.controller.authenticate(
919 924
             {},
@@ -926,14 +931,14 @@ class TokenExpirationTest(AuthTest):
926 931
         unscoped_token_id = r['access']['token']['id']
927 932
         original_expiration = r['access']['token']['expires']
928 933
 
929
-        timeutils.advance_time_seconds(1)
934
+        mock_utcnow.return_value = now + datetime.timedelta(seconds=1)
930 935
 
931 936
         r = self.controller.validate_token(
932 937
             dict(is_admin=True, query_string={}),
933 938
             token_id=unscoped_token_id)
934 939
         self.assertEqual(original_expiration, r['access']['token']['expires'])
935 940
 
936
-        timeutils.advance_time_seconds(1)
941
+        mock_utcnow.return_value = now + datetime.timedelta(seconds=2)
937 942
 
938 943
         r = self.controller.authenticate(
939 944
             {},
@@ -946,7 +951,7 @@ class TokenExpirationTest(AuthTest):
946 951
         scoped_token_id = r['access']['token']['id']
947 952
         self.assertEqual(original_expiration, r['access']['token']['expires'])
948 953
 
949
-        timeutils.advance_time_seconds(1)
954
+        mock_utcnow.return_value = now + datetime.timedelta(seconds=3)
950 955
 
951 956
         r = self.controller.validate_token(
952 957
             dict(is_admin=True, query_string={}),

+ 8
- 3
keystone/tests/test_keystoneclient.py View File

@@ -12,10 +12,13 @@
12 12
 # License for the specific language governing permissions and limitations
13 13
 # under the License.
14 14
 
15
+import datetime
15 16
 import os
16 17
 import uuid
17 18
 import webob
18 19
 
20
+import mock
21
+
19 22
 from keystone.common import sql
20 23
 from keystone import config
21 24
 from keystone.openstack.common.db.sqlalchemy import session
@@ -530,12 +533,14 @@ class KeystoneClientTests(object):
530 533
                           client.tokens.authenticate,
531 534
                           token=token_id)
532 535
 
533
-    def test_token_expiry_maintained(self):
534
-        timeutils.set_time_override()
536
+    @mock.patch.object(timeutils, 'utcnow')
537
+    def test_token_expiry_maintained(self, mock_utcnow):
538
+        now = datetime.datetime.utcnow()
539
+        mock_utcnow.return_value = now
535 540
         foo_client = self.get_client(self.user_foo)
536 541
 
537 542
         orig_token = foo_client.service_catalog.catalog['token']
538
-        timeutils.advance_time_seconds(1)
543
+        mock_utcnow.return_value = now + datetime.timedelta(seconds=1)
539 544
         reauthenticated_token = foo_client.tokens.authenticate(
540 545
             token=foo_client.auth_token)
541 546
 

Loading…
Cancel
Save