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.

Partial-Bug: #1266962

Change-Id: I56935911a08ccd9847419eb118af2d2c9dd6c1cd
tags/0.4.2
Zhongyue Luo 5 years ago
parent
commit
765e43bb51
1 changed files with 19 additions and 17 deletions
  1. 19
    17
      keystoneclient/tests/test_auth_token_middleware.py

+ 19
- 17
keystoneclient/tests/test_auth_token_middleware.py View File

@@ -715,16 +715,16 @@ class CommonAuthTokenMiddlewareTest(object):
715 715
         token = self.token_dict['signed_token_scoped']
716 716
         req.headers['X-Auth-Token'] = token
717 717
         req.environ.update(extra_environ)
718
-        try:
719
-            now = datetime.datetime.utcnow()
720
-            timeutils.set_time_override(now)
718
+        timeutils_utcnow = 'keystoneclient.openstack.common.timeutils.utcnow'
719
+        now = datetime.datetime.utcnow()
720
+        with mock.patch(timeutils_utcnow) as mock_utcnow:
721
+            mock_utcnow.return_value = now
721 722
             self.middleware(req.environ, self.start_fake_response)
722 723
             self.assertNotEqual(self._get_cached_token(token), None)
723
-            expired = now + datetime.timedelta(seconds=token_cache_time)
724
-            timeutils.set_time_override(expired)
724
+        expired = now + datetime.timedelta(seconds=token_cache_time)
725
+        with mock.patch(timeutils_utcnow) as mock_utcnow:
726
+            mock_utcnow.return_value = expired
725 727
             self.assertEqual(self._get_cached_token(token), None)
726
-        finally:
727
-            timeutils.clear_time_override()
728 728
 
729 729
     def test_old_swift_memcache_set_expired(self):
730 730
         extra_conf = {'cache': 'swift.cache'}
@@ -1330,7 +1330,6 @@ class TokenEncodingTest(testtools.TestCase):
1330 1330
 class TokenExpirationTest(BaseAuthTokenMiddlewareTest):
1331 1331
     def setUp(self):
1332 1332
         super(TokenExpirationTest, self).setUp()
1333
-        timeutils.set_time_override()
1334 1333
         self.now = timeutils.utcnow()
1335 1334
         self.delta = datetime.timedelta(hours=1)
1336 1335
         self.one_hour_ago = timeutils.isotime(self.now - self.delta,
@@ -1340,7 +1339,6 @@ class TokenExpirationTest(BaseAuthTokenMiddlewareTest):
1340 1339
 
1341 1340
     def tearDown(self):
1342 1341
         super(TokenExpirationTest, self).tearDown()
1343
-        timeutils.clear_time_override()
1344 1342
 
1345 1343
     def create_v2_token_fixture(self, expires=None):
1346 1344
         v2_fixture = {
@@ -1422,20 +1420,22 @@ class TokenExpirationTest(BaseAuthTokenMiddlewareTest):
1422 1420
                           auth_token.confirm_token_not_expired,
1423 1421
                           data)
1424 1422
 
1425
-    def test_v2_token_with_timezone_offset_not_expired(self):
1423
+    @mock.patch('keystoneclient.openstack.common.timeutils.utcnow')
1424
+    def test_v2_token_with_timezone_offset_not_expired(self, mock_utcnow):
1426 1425
         current_time = timeutils.parse_isotime('2000-01-01T00:01:10.000123Z')
1427 1426
         current_time = timeutils.normalize_time(current_time)
1428
-        timeutils.set_time_override(current_time)
1427
+        mock_utcnow.return_value = current_time
1429 1428
         data = self.create_v2_token_fixture(
1430 1429
             expires='2000-01-01T00:05:10.000123-05:00')
1431 1430
         expected_expires = '2000-01-01T05:05:10.000123Z'
1432 1431
         actual_expires = auth_token.confirm_token_not_expired(data)
1433 1432
         self.assertEqual(actual_expires, expected_expires)
1434 1433
 
1435
-    def test_v2_token_with_timezone_offset_expired(self):
1434
+    @mock.patch('keystoneclient.openstack.common.timeutils.utcnow')
1435
+    def test_v2_token_with_timezone_offset_expired(self, mock_utcnow):
1436 1436
         current_time = timeutils.parse_isotime('2000-01-01T00:01:10.000123Z')
1437 1437
         current_time = timeutils.normalize_time(current_time)
1438
-        timeutils.set_time_override(current_time)
1438
+        mock_utcnow.return_value = current_time
1439 1439
         data = self.create_v2_token_fixture(
1440 1440
             expires='2000-01-01T00:05:10.000123+05:00')
1441 1441
         data['access']['token']['expires'] = '2000-01-01T00:05:10.000123+05:00'
@@ -1455,10 +1455,11 @@ class TokenExpirationTest(BaseAuthTokenMiddlewareTest):
1455 1455
                           auth_token.confirm_token_not_expired,
1456 1456
                           data)
1457 1457
 
1458
-    def test_v3_token_with_timezone_offset_not_expired(self):
1458
+    @mock.patch('keystoneclient.openstack.common.timeutils.utcnow')
1459
+    def test_v3_token_with_timezone_offset_not_expired(self, mock_utcnow):
1459 1460
         current_time = timeutils.parse_isotime('2000-01-01T00:01:10.000123Z')
1460 1461
         current_time = timeutils.normalize_time(current_time)
1461
-        timeutils.set_time_override(current_time)
1462
+        mock_utcnow.return_value = current_time
1462 1463
         data = self.create_v3_token_fixture(
1463 1464
             expires='2000-01-01T00:05:10.000123-05:00')
1464 1465
         expected_expires = '2000-01-01T05:05:10.000123Z'
@@ -1466,10 +1467,11 @@ class TokenExpirationTest(BaseAuthTokenMiddlewareTest):
1466 1467
         actual_expires = auth_token.confirm_token_not_expired(data)
1467 1468
         self.assertEqual(actual_expires, expected_expires)
1468 1469
 
1469
-    def test_v3_token_with_timezone_offset_expired(self):
1470
+    @mock.patch('keystoneclient.openstack.common.timeutils.utcnow')
1471
+    def test_v3_token_with_timezone_offset_expired(self, mock_utcnow):
1470 1472
         current_time = timeutils.parse_isotime('2000-01-01T00:01:10.000123Z')
1471 1473
         current_time = timeutils.normalize_time(current_time)
1472
-        timeutils.set_time_override(current_time)
1474
+        mock_utcnow.return_value = current_time
1473 1475
         data = self.create_v3_token_fixture(
1474 1476
             expires='2000-01-01T00:05:10.000123+05:00')
1475 1477
         self.assertRaises(auth_token.InvalidUserToken,

Loading…
Cancel
Save