From 202f56882928531ccfb10cff1426cb3a35e512a3 Mon Sep 17 00:00:00 2001 From: Russell Bryant Date: Sat, 1 Dec 2012 09:51:17 -0500 Subject: Use json instead of jsonutils in rpc.impl_fake. The fake rpc backend uses json just to make sure the data that was passed in can be serialized. Our jsonutils module makes this check a no-op, because it will automatically convert the data we pass in as primitive types if needed. Change this module back to using the json module directly and add a comment about why we don't use jsonutils here. Also add a test that ensures that non-primitive types raise an exception in the fake rpc driver. Change-Id: I61f46242cb98c875a94d86283e30efb24cc580ad --- openstack/common/rpc/impl_fake.py | 9 +++++++-- tests/unit/rpc/test_fake.py | 14 ++++++++++++++ 2 files changed, 21 insertions(+), 2 deletions(-) diff --git a/openstack/common/rpc/impl_fake.py b/openstack/common/rpc/impl_fake.py index ff16208..59f6fb3 100644 --- a/openstack/common/rpc/impl_fake.py +++ b/openstack/common/rpc/impl_fake.py @@ -18,11 +18,15 @@ queues. Casts will block, but this is very useful for tests. """ import inspect +# NOTE(russellb): We specifically want to use json, not our own jsonutils. +# jsonutils has some extra logic to automatically convert objects to primitive +# types so that they can be serialized. We want to catch all cases where +# non-primitive types make it into this code and treat it as an error. +import json import time import eventlet -from openstack.common import jsonutils from openstack.common.rpc import common as rpc_common CONSUMERS = {} @@ -121,7 +125,7 @@ def create_connection(conf, new=True): def check_serialize(msg): """Make sure a message intended for rpc can be serialized.""" - jsonutils.dumps(msg) + json.dumps(msg) def multicall(conf, context, topic, msg, timeout=None): @@ -154,6 +158,7 @@ def call(conf, context, topic, msg, timeout=None): def cast(conf, context, topic, msg): + check_serialize(msg) try: call(conf, context, topic, msg) except Exception: diff --git a/tests/unit/rpc/test_fake.py b/tests/unit/rpc/test_fake.py index 8ceac47..b3079d6 100644 --- a/tests/unit/rpc/test_fake.py +++ b/tests/unit/rpc/test_fake.py @@ -22,11 +22,25 @@ Unit Tests for remote procedure calls using fake_impl import eventlet eventlet.monkey_patch() +from openstack.common import cfg +from openstack.common import rpc from openstack.common.rpc import impl_fake from tests.unit.rpc import common +CONF = cfg.CONF + + class RpcFakeTestCase(common.BaseRpcTestCase): def setUp(self): self.rpc = impl_fake super(RpcFakeTestCase, self).setUp() + + def test_non_primitive_raises(self): + class Foo(object): + pass + + self.assertRaises(TypeError, self.rpc.cast, CONF, self.context, + 'foo', {'x': Foo()}) + self.assertRaises(TypeError, self.rpc.call, CONF, self.context, + 'foo', {'x': Foo()}) -- cgit