From: john-griffith Date: Thu, 20 Mar 2014 03:53:41 +0000 (-0600) Subject: Serialize the notification payload X-Git-Url: https://review.fuel-infra.org/gitweb?a=commitdiff_plain;h=caff15af26b7622b2d6f84765bbc0ba8434a24be;p=openstack-build%2Fcinder-build.git Serialize the notification payload Notification payload in oslomessaging needs to be serialized correctly, currently it's not. Use json to encode the payload. Closes-Bug: 1294949 Change-Id: Idb2c13981cc1c60ed02445a28220d1dab1529d2b --- diff --git a/cinder/rpc.py b/cinder/rpc.py index bb34f950d..bdfffb1e7 100644 --- a/cinder/rpc.py +++ b/cinder/rpc.py @@ -31,6 +31,7 @@ from oslo import messaging import cinder.context import cinder.exception +from cinder.openstack.common import jsonutils CONF = cfg.CONF TRANSPORT = None @@ -60,8 +61,9 @@ def init(conf): TRANSPORT = messaging.get_transport(conf, allowed_remote_exmods=exmods, aliases=TRANSPORT_ALIASES) - NOTIFIER = messaging.Notifier(TRANSPORT, - serializer=RequestContextSerializer(None)) + + serializer = RequestContextSerializer(JsonPayloadSerializer()) + NOTIFIER = messaging.Notifier(TRANSPORT, serializer=serializer) def initialized(): @@ -92,6 +94,12 @@ def get_allowed_exmods(): return ALLOWED_EXMODS + EXTRA_EXMODS +class JsonPayloadSerializer(messaging.NoOpSerializer): + @staticmethod + def serialize_entity(context, entity): + return jsonutils.to_primitive(entity, convert_instances=True) + + class RequestContextSerializer(messaging.Serializer): def __init__(self, base): diff --git a/cinder/tests/fake_notifier.py b/cinder/tests/fake_notifier.py index 7074ca9b1..701a5acd5 100644 --- a/cinder/tests/fake_notifier.py +++ b/cinder/tests/fake_notifier.py @@ -15,6 +15,7 @@ import collections import functools +import anyjson from oslo import messaging from cinder import rpc @@ -33,19 +34,25 @@ FakeMessage = collections.namedtuple('Message', class FakeNotifier(object): - def __init__(self, transport, publisher_id): + def __init__(self, transport, publisher_id, serializer=None): self.transport = transport self.publisher_id = publisher_id for priority in ['debug', 'info', 'warn', 'error', 'critical']: setattr(self, priority, functools.partial(self._notify, priority.upper())) + self._serializer = serializer or messaging.serializer.NoOpSerializer() def prepare(self, publisher_id=None): if publisher_id is None: publisher_id = self.publisher_id - return self.__class__(self.transport, publisher_id) + return self.__class__(self.transport, publisher_id, self._serializer) def _notify(self, priority, ctxt, event_type, payload): + payload = self._serializer.serialize_entity(ctxt, payload) + # NOTE(sileht): simulate the kombu serializer + # this permit to raise an exception if something have not + # been serialized correctly + anyjson.serialize(payload) msg = dict(publisher_id=self.publisher_id, priority=priority, event_type=event_type, @@ -56,5 +63,7 @@ class FakeNotifier(object): def stub_notifier(stubs): stubs.Set(messaging, 'Notifier', FakeNotifier) if rpc.NOTIFIER: + serializer = getattr(rpc.NOTIFIER, '_serializer', None) stubs.Set(rpc, 'NOTIFIER', FakeNotifier(rpc.NOTIFIER.transport, - rpc.NOTIFIER.publisher_id)) + rpc.NOTIFIER.publisher_id, + serializer=serializer))