]> review.fuel-infra Code Review - openstack-build/cinder-build.git/commitdiff
Serialize the notification payload
authorjohn-griffith <john.griffith@solidfire.com>
Thu, 20 Mar 2014 03:53:41 +0000 (21:53 -0600)
committerFlavio Percoco <flaper87@gmail.com>
Tue, 25 Mar 2014 09:37:32 +0000 (10:37 +0100)
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

cinder/rpc.py
cinder/tests/fake_notifier.py

index bb34f950d496328aedb412347fe7c9d60dde76a8..bdfffb1e70a0b43d01d5cda231e6df5468120d16 100644 (file)
@@ -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):
index 7074ca9b1870e90f48a0b0e68d6052117b9eb96d..701a5acd5fed4a0791f386450b2b7f1371fc05e9 100644 (file)
@@ -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))