2012-02-09 09:53:03 -08:00
|
|
|
# vim: tabstop=4 shiftwidth=4 softtabstop=4
|
|
|
|
|
2012-02-08 16:08:08 -08:00
|
|
|
import datetime
|
|
|
|
import time
|
2012-02-13 14:15:33 -08:00
|
|
|
import uuid
|
|
|
|
|
|
|
|
import memcache
|
|
|
|
|
2012-02-09 09:53:03 -08:00
|
|
|
from keystone import exception
|
2012-02-06 18:49:03 -08:00
|
|
|
from keystone import test
|
|
|
|
from keystone.token.backends import memcache as token_memcache
|
|
|
|
|
2012-02-09 09:53:03 -08:00
|
|
|
import test_backend
|
|
|
|
|
2012-02-06 18:49:03 -08:00
|
|
|
|
|
|
|
class MemcacheClient(object):
|
|
|
|
"""Replicates a tiny subset of memcached client interface."""
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
"""Ignores the passed in args."""
|
|
|
|
self.cache = {}
|
|
|
|
|
2012-02-13 14:15:33 -08:00
|
|
|
def check_key(self, key):
|
|
|
|
if not isinstance(key, str):
|
|
|
|
raise memcache.Client.MemcachedStringEncodingError()
|
|
|
|
|
2012-02-06 18:49:03 -08:00
|
|
|
def get(self, key):
|
|
|
|
"""Retrieves the value for a key or None."""
|
2012-02-13 14:15:33 -08:00
|
|
|
self.check_key(key)
|
2012-02-08 16:08:08 -08:00
|
|
|
obj = self.cache.get(key)
|
|
|
|
now = time.mktime(datetime.datetime.now().timetuple())
|
|
|
|
if obj and (obj[1] == 0 or obj[1] > now):
|
|
|
|
return obj[0]
|
|
|
|
else:
|
2012-02-09 09:53:03 -08:00
|
|
|
raise exception.TokenNotFound(token_id=key)
|
2012-02-06 18:49:03 -08:00
|
|
|
|
2012-02-08 16:08:08 -08:00
|
|
|
def set(self, key, value, time=0):
|
2012-02-06 18:49:03 -08:00
|
|
|
"""Sets the value for a key."""
|
2012-02-13 14:15:33 -08:00
|
|
|
self.check_key(key)
|
2012-02-08 16:08:08 -08:00
|
|
|
self.cache[key] = (value, time)
|
2012-02-06 18:49:03 -08:00
|
|
|
return True
|
|
|
|
|
|
|
|
def delete(self, key):
|
2012-02-13 14:15:33 -08:00
|
|
|
self.check_key(key)
|
2012-02-06 18:49:03 -08:00
|
|
|
try:
|
|
|
|
del self.cache[key]
|
|
|
|
except KeyError:
|
|
|
|
#NOTE(bcwaldon): python-memcached always returns the same value
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2012-02-09 09:53:03 -08:00
|
|
|
class MemcacheToken(test.TestCase, test_backend.TokenTests):
|
2012-02-06 18:49:03 -08:00
|
|
|
def setUp(self):
|
|
|
|
super(MemcacheToken, self).setUp()
|
|
|
|
fake_client = MemcacheClient()
|
|
|
|
self.token_api = token_memcache.Token(client=fake_client)
|
2012-02-13 14:15:33 -08:00
|
|
|
|
|
|
|
def test_get_unicode(self):
|
|
|
|
token_id = unicode(uuid.uuid4().hex)
|
|
|
|
data = {'id': token_id, 'a': 'b'}
|
|
|
|
self.token_api.create_token(token_id, data)
|
|
|
|
self.token_api.get_token(token_id)
|