diff options
author | Lance Bragstad <ldbragst@us.ibm.com> | 2013-08-06 20:27:18 +0000 |
---|---|---|
committer | Lance Bragstad <ldbragst@us.ibm.com> | 2013-08-10 03:26:53 +0000 |
commit | 61c4cdec304d79f43c8a549c2543c939492c4812 (patch) | |
tree | 38b13670816930854bd8baa2dee4790cd9ad2639 | |
parent | de0427f8f886f6be4c0777cfaccc336bef6aee7a (diff) | |
download | oslo-61c4cdec304d79f43c8a549c2543c939492c4812.tar.gz oslo-61c4cdec304d79f43c8a549c2543c939492c4812.tar.xz oslo-61c4cdec304d79f43c8a549c2543c939492c4812.zip |
Ensure context type is handled when using to_dict
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
-rw-r--r-- | openstack/common/rpc/amqp.py | 9 | ||||
-rw-r--r-- | tests/unit/rpc/amqp.py | 11 |
2 files changed, 18 insertions, 2 deletions
diff --git a/openstack/common/rpc/amqp.py b/openstack/common/rpc/amqp.py index 1afd2ab..38f2515 100644 --- a/openstack/common/rpc/amqp.py +++ b/openstack/common/rpc/amqp.py @@ -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) diff --git a/tests/unit/rpc/amqp.py b/tests/unit/rpc/amqp.py index 83713c7..76d6946 100644 --- a/tests/unit/rpc/amqp.py +++ b/tests/unit/rpc/amqp.py @@ -22,6 +22,7 @@ Unit Tests for AMQP-based remote procedure calls import logging from eventlet import greenthread +import mock from oslo.config import cfg from openstack.common import jsonutils @@ -177,3 +178,13 @@ class BaseRpcAMQPTestCase(common.BaseRpcTestCase): 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) |