Skip to content
This repository has been archived by the owner on Jun 26, 2020. It is now read-only.

Commit

Permalink
Ensure context type is handled when using to_dict
Browse files Browse the repository at this point in the history
Handle the case where the context passed into def pack_context() is a
dictionary. If a dictionary is passed in, we don't need to call to_dict
before updating the msg.

fixes bug 1208971

Change-Id: I2ce0b28f97634e717868e0ee5525189338d4981c
  • Loading branch information
Lance Bragstad committed Aug 10, 2013
1 parent de0427f commit 61c4cde
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
9 changes: 7 additions & 2 deletions openstack/common/rpc/amqp.py
Expand Up @@ -300,8 +300,13 @@ def pack_context(msg, context):
for args at some point.
"""
context_d = dict([('_context_%s' % key, value)
for (key, value) in context.to_dict().iteritems()])
if isinstance(context, dict):
context_d = dict([('_context_%s' % key, value)
for (key, value) in context.iteritems()])
else:
context_d = dict([('_context_%s' % key, value)
for (key, value) in context.to_dict().iteritems()])

msg.update(context_d)


Expand Down
11 changes: 11 additions & 0 deletions tests/unit/rpc/amqp.py
Expand Up @@ -22,6 +22,7 @@
import logging

from eventlet import greenthread
import mock
from oslo.config import cfg

from openstack.common import jsonutils
Expand Down Expand Up @@ -177,3 +178,13 @@ def _callback(message):
conn.close()

self.assertTrue(self.exc_raised)

def test_context_dict_type_check(self):
"""Test that context is handled properly depending on the type."""
fake_context = {'fake': 'context'}
mock_msg = mock.MagicMock()
rpc_amqp.pack_context(mock_msg, fake_context)

# assert first arg in args was a dict type
args = mock_msg.update.call_args[0]
self.assertIsInstance(args[0], dict)

0 comments on commit 61c4cde

Please sign in to comment.