Merge "Fix Manila RequestContext.to_dict() AttributeError"

This commit is contained in:
Jenkins 2016-05-16 17:52:45 +00:00 committed by Gerrit Code Review
commit 5c961ab615
2 changed files with 27 additions and 7 deletions

View File

@ -108,13 +108,14 @@ class RequestContext(context.RequestContext):
def to_dict(self):
values = super(RequestContext, self).to_dict()
values.update({
'user_id': self.user_id,
'project_id': self.project_id,
'read_deleted': self.read_deleted,
'remote_address': self.remote_address,
'timestamp': self.timestamp.isoformat(),
'quota_class': self.quota_class,
'service_catalog': self.service_catalog})
'user_id': getattr(self, 'user_id', None),
'project_id': getattr(self, 'project_id', None),
'read_deleted': getattr(self, 'read_deleted', None),
'remote_address': getattr(self, 'remote_address', None),
'timestamp': self.timestamp.isoformat() if hasattr(
self, 'timestamp') else None,
'quota_class': getattr(self, 'quota_class', None),
'service_catalog': getattr(self, 'service_catalog', None)})
return values
@classmethod

View File

@ -84,3 +84,22 @@ class ContextTestCase(test.TestCase):
# user and tenant kwargs get popped off before we log anything
self.assertNotIn("'user': 'user'", info['log_msg'])
self.assertNotIn("'tenant': 'project'", info['log_msg'])
def test_to_dict_works_w_missing_manila_context_attributes(self):
manila_context_attributes = ['user_id', 'project_id', 'read_deleted',
'remote_address', 'timestamp',
'quota_class', 'service_catalog']
ctxt = context.RequestContext('111', '222', roles=['admin', 'weasel'])
# Early in context initialization to_dict() can be triggered
# before all manila specific context attributes have been set.
# Set up this situation here.
for attr in manila_context_attributes:
delattr(ctxt, attr)
# We should be able to run to_dict() without getting an
# AttributeError exception
res = ctxt.to_dict()
for attr in manila_context_attributes:
self.assertIsNone(res[attr])