Merge "Fix format errors seen in rpc logging"

This commit is contained in:
Jenkins 2013-11-27 15:46:23 +00:00 committed by Gerrit Code Review
commit 522ec7de54
2 changed files with 14 additions and 4 deletions

View File

@ -54,10 +54,6 @@ class ContextBase(common_context.RequestContext):
:param kwargs: Extra arguments that might be present, but we ignore :param kwargs: Extra arguments that might be present, but we ignore
because they possibly came in from older rpc messages. because they possibly came in from older rpc messages.
""" """
if kwargs:
LOG.warn(_('Arguments dropped when creating '
'context: %s'), kwargs)
super(ContextBase, self).__init__(user=user_id, tenant=tenant_id, super(ContextBase, self).__init__(user=user_id, tenant=tenant_id,
is_admin=is_admin, is_admin=is_admin,
request_id=request_id) request_id=request_id)
@ -81,6 +77,12 @@ class ContextBase(common_context.RequestContext):
if overwrite or not hasattr(local.store, 'context'): if overwrite or not hasattr(local.store, 'context'):
local.store.context = self local.store.context = self
# Log only once the context has been configured to prevent
# format errors.
if kwargs:
LOG.warn(_('Arguments dropped when creating '
'context: %s'), kwargs)
@property @property
def project_id(self): def project_id(self):
return self.tenant return self.tenant
@ -128,6 +130,7 @@ class ContextBase(common_context.RequestContext):
'tenant': self.tenant, 'tenant': self.tenant,
'user': self.user, 'user': self.user,
'tenant_name': self.tenant_name, 'tenant_name': self.tenant_name,
'project_name': self.tenant_name,
'user_name': self.user_name, 'user_name': self.user_name,
} }

View File

@ -43,6 +43,11 @@ class TestNeutronContext(base.BaseTestCase):
self.assertIsNone(ctx.user_name) self.assertIsNone(ctx.user_name)
self.assertIsNone(ctx.tenant_name) self.assertIsNone(ctx.tenant_name)
def test_neutron_context_create_logs_unknown_kwargs(self):
with mock.patch.object(context.LOG, 'warn') as mock_warn:
context.Context('user_id', 'tenant_id', foo='bar')
self.assertEqual(mock_warn.call_count, 1)
def test_neutron_context_create_with_name(self): def test_neutron_context_create_with_name(self):
ctx = context.Context('user_id', 'tenant_id', ctx = context.Context('user_id', 'tenant_id',
tenant_name='tenant_name', user_name='user_name') tenant_name='tenant_name', user_name='user_name')
@ -67,6 +72,7 @@ class TestNeutronContext(base.BaseTestCase):
self.assertEqual('tenant_id', ctx_dict['tenant']) self.assertEqual('tenant_id', ctx_dict['tenant'])
self.assertIsNone(ctx_dict['user_name']) self.assertIsNone(ctx_dict['user_name'])
self.assertIsNone(ctx_dict['tenant_name']) self.assertIsNone(ctx_dict['tenant_name'])
self.assertIsNone(ctx_dict['project_name'])
def test_neutron_context_to_dict_with_name(self): def test_neutron_context_to_dict_with_name(self):
ctx = context.Context('user_id', 'tenant_id', ctx = context.Context('user_id', 'tenant_id',
@ -74,6 +80,7 @@ class TestNeutronContext(base.BaseTestCase):
ctx_dict = ctx.to_dict() ctx_dict = ctx.to_dict()
self.assertEqual('user_name', ctx_dict['user_name']) self.assertEqual('user_name', ctx_dict['user_name'])
self.assertEqual('tenant_name', ctx_dict['tenant_name']) self.assertEqual('tenant_name', ctx_dict['tenant_name'])
self.assertEqual('tenant_name', ctx_dict['project_name'])
def test_neutron_context_admin_to_dict(self): def test_neutron_context_admin_to_dict(self):
self.db_api_session.return_value = 'fakesession' self.db_api_session.return_value = 'fakesession'