From a98b2ed7064d01696b8fc980cf90cefd7cbbd6d7 Mon Sep 17 00:00:00 2001 From: termie Date: Wed, 19 Oct 2011 13:28:22 +0300 Subject: get tenants passing, yay --- keystonelight/backends/kvs.py | 8 ++++++++ keystonelight/identity.py | 7 +++++-- keystonelight/keystone_compat.py | 3 ++- keystonelight/logging.py | 19 +++++++++++++++++++ keystonelight/models.py | 2 +- keystonelight/test.py | 41 +++++++++++++++++++++++++++++++++++++--- keystonelight/token.py | 2 ++ tests/test_keystone_compat.py | 8 ++++---- 8 files changed, 79 insertions(+), 11 deletions(-) create mode 100644 keystonelight/logging.py diff --git a/keystonelight/backends/kvs.py b/keystonelight/backends/kvs.py index 807492e0..246d2db8 100644 --- a/keystonelight/backends/kvs.py +++ b/keystonelight/backends/kvs.py @@ -23,6 +23,14 @@ class KvsIdentity(object): return o + def get_tenant(self, tenant_id): + tenant_ref = self.db.get('tenant-%s' % tenant_id) + return tenant_ref + + def get_user(self, user_id): + user_ref = self.db.get('user-%s' % user_id) + return user_ref + # Private CRUD for testing def _create_user(self, id, user): self.db.set('user-%s' % id, user) diff --git a/keystonelight/identity.py b/keystonelight/identity.py index 9b887038..92707191 100644 --- a/keystonelight/identity.py +++ b/keystonelight/identity.py @@ -20,5 +20,8 @@ class Manager(object): """ return self.driver.authenticate(**kwargs) - def get_tenants(self, context, user_id): - return self.driver.get_tenants(user_id) + def get_user(self, context, user_id): + return self.driver.get_user(user_id) + + def get_tenant(self, context, tenant_id): + return self.driver.get_tenant(tenant_id) diff --git a/keystonelight/keystone_compat.py b/keystonelight/keystone_compat.py index d47bbcd5..e776f7a1 100644 --- a/keystonelight/keystone_compat.py +++ b/keystonelight/keystone_compat.py @@ -149,7 +149,8 @@ class KeystoneController(service.BaseApplication): return self._format_tenants_for_token(tenant_refs) def _format_tenants_for_token(self, tenant_refs): - return [{}] + o = {'tenants': {'values': tenant_refs}} + return o def app_factory(global_conf, **local_conf): diff --git a/keystonelight/logging.py b/keystonelight/logging.py new file mode 100644 index 00000000..db4df551 --- /dev/null +++ b/keystonelight/logging.py @@ -0,0 +1,19 @@ +from __future__ import absolute_import + +import functools +import logging +import pprint + + +from logging import * + + +def log_debug(f): + @functools.wraps(f) + def wrapper(*args, **kw): + logging.debug('%s(%s, %s) ->', f.func_name, str(args), str(kw)) + rv = f(*args, **kw) + logging.debug(pprint.pformat(rv, indent=2)) + logging.debug('') + return rv + return wrapper diff --git a/keystonelight/models.py b/keystonelight/models.py index a56e8061..08747cdd 100644 --- a/keystonelight/models.py +++ b/keystonelight/models.py @@ -9,7 +9,7 @@ class User(dict): def __init__(self, id=None, tenants=None, *args, **kw): if tenants is None: tenants = [] - super(User, self).__init__(id=id, tenants=[], *args, **kw) + super(User, self).__init__(id=id, tenants=tenants, *args, **kw) class Tenant(dict): diff --git a/keystonelight/test.py b/keystonelight/test.py index 98557c7f..d5409de4 100644 --- a/keystonelight/test.py +++ b/keystonelight/test.py @@ -55,14 +55,49 @@ class TestCase(unittest.TestCase): def client(self, app, *args, **kw): return TestClient(app, *args, **kw) + + def assertListEquals(self, expected, actual): + copy = expected[:] + self.assertEquals(len(expected), len(actual)) + while copy: + item = copy.pop() + matched = False + for x in actual: + #print 'COMPARE', item, x, + try: + self.assertDeepEquals(item, x) + matched = True + #print 'MATCHED' + break + except AssertionError as e: + #print e + pass + if not matched: + raise AssertionError('Expected: %s\n Got: %s' % (expected, actual)) + + def assertDictEquals(self, expected, actual): for k in expected: self.assertTrue(k in actual, "Expected key %s not in %s." % (k, actual)) - self.assertEquals(expected[k], actual[k], - "Expected value for %s to be '%s', not '%s'." - % (k, expected[k], actual[k])) + self.assertDeepEquals(expected[k], actual[k]) + for k in actual: self.assertTrue(k in expected, "Unexpected key %s in %s." % (k, actual)) + + + def assertDeepEquals(self, expected, actual): + try: + if type(expected) is type([]) or type(expected) is type(tuple()): + # assert items equal, ignore order + self.assertListEquals(expected, actual) + elif type(expected) is type({}): + self.assertDictEquals(expected, actual) + else: + self.assertEquals(expected, actual) + except AssertionError as e: + raise AssertionError('Expected: %s\n Got: %s' % (expected, actual)) + + diff --git a/keystonelight/token.py b/keystonelight/token.py index 34a19308..a4c814e5 100644 --- a/keystonelight/token.py +++ b/keystonelight/token.py @@ -4,6 +4,7 @@ import uuid +from keystonelight import logging from keystonelight import utils @@ -19,6 +20,7 @@ class Manager(object): token_ref = self.driver.create_token(token, data) return token_ref + @logging.log_debug def get_token(self, context, token_id): """Return info for a token if it is valid.""" return self.driver.get_token(token_id) diff --git a/tests/test_keystone_compat.py b/tests/test_keystone_compat.py index 0d8cc014..9a75dcd7 100644 --- a/tests/test_keystone_compat.py +++ b/tests/test_keystone_compat.py @@ -49,13 +49,13 @@ class CompatTestCase(test.TestCase): '1234', models.Tenant(id='1234', name='ACME Corp', - description='A description...', + description='A description ...', enabled=True)) self.tenant_3456 = self.identity_backend._create_tenant( '3456', models.Tenant(id='3456', name='Iron Works', - description='A description...', + description='A description ...', enabled=True)) self.token_foo_unscoped = self.token_backend.create_token( @@ -90,11 +90,11 @@ class HeadCompatTestCase(CompatTestCase): client = self.client(self.app, token=self.token_foo_unscoped['id']) resp = client.get('/v2.0/tenants') data = json.loads(resp.body) - self.assertDictEquals(self.tenants_for_token, data) + self.assertDeepEquals(self.tenants_for_token, data) def test_tenants_for_token_scoped(self): # get_tenants_for_token client = self.client(self.app, token=self.token_foo_scoped['id']) resp = client.get('/v2.0/tenants') data = json.loads(resp.body) - self.assertDictEquals(self.tenants_for_token, data) + self.assertDeepEquals(self.tenants_for_token, data) -- cgit