From 3035a6b394ba4b460d9ea18409fa0cb87c86d38d Mon Sep 17 00:00:00 2001 From: Morgan Fainberg Date: Sat, 8 Mar 2014 21:57:51 -0800 Subject: [PATCH] Call an existing method in sync cache for revoke events The cache used for synchronizing the revocation tree across green threads had an issue where it was calling a non-existant method ``remove`` instead of ``remove_event``. The correct method is now being called and an expanded test to exercise the synchronize method has been added. Change-Id: I3fe47fa51f88aab89480831b2d95746319f82ceb Closes-Bug: 1289935 --- keystone/contrib/revoke/core.py | 2 +- keystone/tests/test_revoke.py | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 34 insertions(+), 1 deletion(-) diff --git a/keystone/contrib/revoke/core.py b/keystone/contrib/revoke/core.py index b02f34dc97..b552f72d02 100644 --- a/keystone/contrib/revoke/core.py +++ b/keystone/contrib/revoke/core.py @@ -76,7 +76,7 @@ class _Cache(object): with self._store.get_lock(_TREE_KEY): for e in self._current_events: if e.revoked_at < cutoff: - self.revoke_map.remove(e) + self.revoke_map.remove_event(e) self._current_events.remove(e) else: break diff --git a/keystone/tests/test_revoke.py b/keystone/tests/test_revoke.py index 9203ca48fe..55a27cd422 100644 --- a/keystone/tests/test_revoke.py +++ b/keystone/tests/test_revoke.py @@ -14,9 +14,12 @@ import datetime import uuid +import mock + from keystone.common import dependency from keystone import config from keystone.contrib.revoke import model +from keystone import exception from keystone.openstack.common import timeutils from keystone import tests from keystone.tests import test_backend_sql @@ -137,6 +140,36 @@ class RevokeTests(object): self.revoke_api.revoke(event) self.assertEqual(1, len(self.revoke_api.get_events())) + @mock.patch.object(timeutils, 'utcnow') + def test_expired_events_removed_validate_token_success(self, mock_utcnow): + def _sample_token_values(): + token = _sample_blank_token() + token['expires_at'] = timeutils.isotime(_future_time(), + subsecond=True) + return token + + now = datetime.datetime.utcnow() + now_plus_2h = now + datetime.timedelta(hours=2) + mock_utcnow.return_value = now + + # Build a token and validate it. This will seed the cache for the + # future 'synchronize' call. + token_values = _sample_token_values() + + user_id = _new_id() + self.revoke_api.revoke_by_user(user_id) + token_values['user_id'] = user_id + self.assertRaises(exception.TokenNotFound, + self.revoke_api.check_token, + token_values) + + # Move our clock forward by 2h, build a new token and validate it. + # 'synchronize' should now be exercised and remove old expired events + mock_utcnow.return_value = now_plus_2h + self.revoke_api.revoke_by_expiration(_new_id(), now_plus_2h) + #should no longer throw an exception + self.revoke_api.check_token(token_values) + class SqlRevokeTests(test_backend_sql.SqlTests, RevokeTests): def setUp(self):