From 4fd4d490ac1e12b2bf835949447d21b64c641616 Mon Sep 17 00:00:00 2001 From: Maru Newby Date: Mon, 25 Nov 2013 17:35:54 +0000 Subject: [PATCH] Fix format errors seen in rpc logging The previous commit for this bug didn't include the 'project_name' key in the context dict. The missing key was causing the amqp module to generate log formatting exceptions instead of normal log output. Separately, the context module itself was generating logging exceptions in the quantum service when logging was attempted before the context was fully initialized Change-Id: I0f4c6f5a6804442932c9b2bd409a258cfc2419ff Closes-Bug: #1254530 Related-Bug: #1239923 --- neutron/context.py | 11 +++++++---- neutron/tests/unit/test_neutron_context.py | 7 +++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/neutron/context.py b/neutron/context.py index 6cd9311e4b1..1dadd4c4b03 100644 --- a/neutron/context.py +++ b/neutron/context.py @@ -54,10 +54,6 @@ class ContextBase(common_context.RequestContext): :param kwargs: Extra arguments that might be present, but we ignore 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, is_admin=is_admin, request_id=request_id) @@ -81,6 +77,12 @@ class ContextBase(common_context.RequestContext): if overwrite or not hasattr(local.store, 'context'): 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 def project_id(self): return self.tenant @@ -128,6 +130,7 @@ class ContextBase(common_context.RequestContext): 'tenant': self.tenant, 'user': self.user, 'tenant_name': self.tenant_name, + 'project_name': self.tenant_name, 'user_name': self.user_name, } diff --git a/neutron/tests/unit/test_neutron_context.py b/neutron/tests/unit/test_neutron_context.py index 58a26ea9d9b..a756e8f2bbf 100644 --- a/neutron/tests/unit/test_neutron_context.py +++ b/neutron/tests/unit/test_neutron_context.py @@ -43,6 +43,11 @@ class TestNeutronContext(base.BaseTestCase): self.assertIsNone(ctx.user_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): ctx = context.Context('user_id', 'tenant_id', tenant_name='tenant_name', user_name='user_name') @@ -67,6 +72,7 @@ class TestNeutronContext(base.BaseTestCase): self.assertEqual('tenant_id', ctx_dict['tenant']) self.assertIsNone(ctx_dict['user_name']) self.assertIsNone(ctx_dict['tenant_name']) + self.assertIsNone(ctx_dict['project_name']) def test_neutron_context_to_dict_with_name(self): ctx = context.Context('user_id', 'tenant_id', @@ -74,6 +80,7 @@ class TestNeutronContext(base.BaseTestCase): ctx_dict = ctx.to_dict() self.assertEqual('user_name', ctx_dict['user_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): self.db_api_session.return_value = 'fakesession'