Skip to content

Commit

Permalink
Pop extra keys from context in from_dict()
Browse files Browse the repository at this point in the history
RequestContext.to_dict() adds user/tenant keys, but from_dict() spews
this warning a "Arguments dropped when creating context" if you pass a
dict which contains user/tenant keys.

Seems to make perfect sense that from_dict() would remove these extra
keys. A test case ensures this won't happen for new arguments.

This is import for oslo.messaging because we deserialize request context
dicts into nova's RequestContext using from_dict().

blueprint: oslo-messaging
Change-Id: I0938f3c7c581383cbebc3a973d64454250f6ab8e
  • Loading branch information
markmc committed Aug 22, 2013
1 parent 4a661dd commit 5e573aa
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
2 changes: 2 additions & 0 deletions nova/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,8 @@ def to_dict(self):

@classmethod
def from_dict(cls, values):
values.pop('user', None)
values.pop('tenant', None)
return cls(**values)

def elevated(self, read_deleted=None, overwrite=False):
Expand Down
18 changes: 18 additions & 0 deletions nova/tests/test_context.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,3 +101,21 @@ def test_service_catalog_cinder_only(self):
ctxt = context.RequestContext('111', '222',
service_catalog=service_catalog)
self.assertEquals(ctxt.service_catalog, volume_catalog)

def test_to_dict_from_dict_no_log(self):
warns = []

def stub_warn(msg, *a, **kw):
if (a and len(a) == 1 and isinstance(a[0], dict) and a[0]):
a = a[0]
warns.append(str(msg) % a)

self.stubs.Set(context.LOG, 'warn', stub_warn)

ctxt = context.RequestContext('111',
'222',
roles=['admin', 'weasel'])

ctxt = context.RequestContext.from_dict(ctxt.to_dict())

self.assertEqual(len(warns), 0, warns)

0 comments on commit 5e573aa

Please sign in to comment.