From 765e43bb51fce98d411010723f82094fadb399a0 Mon Sep 17 00:00:00 2001 From: Zhongyue Luo Date: Fri, 10 Jan 2014 01:56:15 +0900 Subject: [PATCH] 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 --- .../tests/test_auth_token_middleware.py | 36 ++++++++++--------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/keystoneclient/tests/test_auth_token_middleware.py b/keystoneclient/tests/test_auth_token_middleware.py index 69358aca4..f5815fe62 100644 --- a/keystoneclient/tests/test_auth_token_middleware.py +++ b/keystoneclient/tests/test_auth_token_middleware.py @@ -715,16 +715,16 @@ class CommonAuthTokenMiddlewareTest(object): token = self.token_dict['signed_token_scoped'] req.headers['X-Auth-Token'] = token req.environ.update(extra_environ) - try: - now = datetime.datetime.utcnow() - timeutils.set_time_override(now) + timeutils_utcnow = 'keystoneclient.openstack.common.timeutils.utcnow' + now = datetime.datetime.utcnow() + with mock.patch(timeutils_utcnow) as mock_utcnow: + mock_utcnow.return_value = now self.middleware(req.environ, self.start_fake_response) self.assertNotEqual(self._get_cached_token(token), None) - expired = now + datetime.timedelta(seconds=token_cache_time) - timeutils.set_time_override(expired) + expired = now + datetime.timedelta(seconds=token_cache_time) + with mock.patch(timeutils_utcnow) as mock_utcnow: + mock_utcnow.return_value = expired self.assertEqual(self._get_cached_token(token), None) - finally: - timeutils.clear_time_override() def test_old_swift_memcache_set_expired(self): extra_conf = {'cache': 'swift.cache'} @@ -1330,7 +1330,6 @@ class TokenEncodingTest(testtools.TestCase): class TokenExpirationTest(BaseAuthTokenMiddlewareTest): def setUp(self): super(TokenExpirationTest, self).setUp() - timeutils.set_time_override() self.now = timeutils.utcnow() self.delta = datetime.timedelta(hours=1) self.one_hour_ago = timeutils.isotime(self.now - self.delta, @@ -1340,7 +1339,6 @@ class TokenExpirationTest(BaseAuthTokenMiddlewareTest): def tearDown(self): super(TokenExpirationTest, self).tearDown() - timeutils.clear_time_override() def create_v2_token_fixture(self, expires=None): v2_fixture = { @@ -1422,20 +1420,22 @@ class TokenExpirationTest(BaseAuthTokenMiddlewareTest): auth_token.confirm_token_not_expired, data) - def test_v2_token_with_timezone_offset_not_expired(self): + @mock.patch('keystoneclient.openstack.common.timeutils.utcnow') + def test_v2_token_with_timezone_offset_not_expired(self, mock_utcnow): current_time = timeutils.parse_isotime('2000-01-01T00:01:10.000123Z') current_time = timeutils.normalize_time(current_time) - timeutils.set_time_override(current_time) + mock_utcnow.return_value = current_time data = self.create_v2_token_fixture( expires='2000-01-01T00:05:10.000123-05:00') expected_expires = '2000-01-01T05:05:10.000123Z' actual_expires = auth_token.confirm_token_not_expired(data) self.assertEqual(actual_expires, expected_expires) - def test_v2_token_with_timezone_offset_expired(self): + @mock.patch('keystoneclient.openstack.common.timeutils.utcnow') + def test_v2_token_with_timezone_offset_expired(self, mock_utcnow): current_time = timeutils.parse_isotime('2000-01-01T00:01:10.000123Z') current_time = timeutils.normalize_time(current_time) - timeutils.set_time_override(current_time) + mock_utcnow.return_value = current_time data = self.create_v2_token_fixture( expires='2000-01-01T00:05:10.000123+05:00') data['access']['token']['expires'] = '2000-01-01T00:05:10.000123+05:00' @@ -1455,10 +1455,11 @@ class TokenExpirationTest(BaseAuthTokenMiddlewareTest): auth_token.confirm_token_not_expired, data) - def test_v3_token_with_timezone_offset_not_expired(self): + @mock.patch('keystoneclient.openstack.common.timeutils.utcnow') + def test_v3_token_with_timezone_offset_not_expired(self, mock_utcnow): current_time = timeutils.parse_isotime('2000-01-01T00:01:10.000123Z') current_time = timeutils.normalize_time(current_time) - timeutils.set_time_override(current_time) + mock_utcnow.return_value = current_time data = self.create_v3_token_fixture( expires='2000-01-01T00:05:10.000123-05:00') expected_expires = '2000-01-01T05:05:10.000123Z' @@ -1466,10 +1467,11 @@ class TokenExpirationTest(BaseAuthTokenMiddlewareTest): actual_expires = auth_token.confirm_token_not_expired(data) self.assertEqual(actual_expires, expected_expires) - def test_v3_token_with_timezone_offset_expired(self): + @mock.patch('keystoneclient.openstack.common.timeutils.utcnow') + def test_v3_token_with_timezone_offset_expired(self, mock_utcnow): current_time = timeutils.parse_isotime('2000-01-01T00:01:10.000123Z') current_time = timeutils.normalize_time(current_time) - timeutils.set_time_override(current_time) + mock_utcnow.return_value = current_time data = self.create_v3_token_fixture( expires='2000-01-01T00:05:10.000123+05:00') self.assertRaises(auth_token.InvalidUserToken,