From adffcd58fb09dafb0c4e128797b4a61a59087f9a Mon Sep 17 00:00:00 2001 From: Russell Bryant Date: Thu, 13 Dec 2012 12:53:43 -0500 Subject: Optionally add message envelope to notifications. When message envelopes were added for rpc messages, notifications were explicitly left out. This was due to the fact that notifications are consumed outside of OpenStack. However, notification consumers will likely want to eventually start taking advantage of some of the features that the envelope is a prerequisite for, such as message signing. This patch adds a new notification driver that produces message based notifications including the message envelope. Related to blueprint version-rpc-messages. Change-Id: Iaa8b437e6b4f64053f4b02bf486516f60bb020e9 --- tests/unit/rpc/common.py | 36 ++++++++++++++++++++++++++++++++++++ tests/unit/test_notifier.py | 19 +++++++++++++++---- 2 files changed, 51 insertions(+), 4 deletions(-) (limited to 'tests') diff --git a/tests/unit/rpc/common.py b/tests/unit/rpc/common.py index 70a1ad0..9838c6f 100644 --- a/tests/unit/rpc/common.py +++ b/tests/unit/rpc/common.py @@ -29,6 +29,7 @@ import nose from openstack.common import cfg from openstack.common import exception from openstack.common.gettextutils import _ +from openstack.common import jsonutils from openstack.common.rpc import amqp as rpc_amqp from openstack.common.rpc import common as rpc_common from openstack.common.rpc import dispatcher as rpc_dispatcher @@ -276,6 +277,41 @@ class BaseRpcAMQPTestCase(BaseRpcTestCase): "args": {"value": value}}) self.assertEqual(value, result) + def test_notification_envelope(self): + raw_msg = {'a': 'b'} + self.test_msg = None + + def fake_notify_send(_conn, topic, msg): + self.test_msg = msg + + self.stubs.Set(self.rpc.Connection, 'notify_send', fake_notify_send) + + self.rpc.notify(FLAGS, self.context, 'notifications.info', raw_msg, + envelope=False) + self.assertEqual(self.test_msg, raw_msg) + + # Envelopes enabled, but not enabled for notifications + self.stubs.Set(rpc_common, '_SEND_RPC_ENVELOPE', True) + self.rpc.notify(FLAGS, self.context, 'notifications.info', raw_msg, + envelope=False) + self.assertEqual(self.test_msg, raw_msg) + + # Now turn it on for notifications + msg = { + 'oslo.version': rpc_common._RPC_ENVELOPE_VERSION, + 'oslo.message': jsonutils.dumps(raw_msg), + } + self.rpc.notify(FLAGS, self.context, 'notifications.info', raw_msg, + envelope=True) + self.assertEqual(self.test_msg, msg) + + # Make sure envelopes are still on notifications, even if turned off + # for general messages. + self.stubs.Set(rpc_common, '_SEND_RPC_ENVELOPE', False) + self.rpc.notify(FLAGS, self.context, 'notifications.info', raw_msg, + envelope=True) + self.assertEqual(self.test_msg, msg) + class TestReceiver(object): """Simple Proxy class so the consumer has methods to call. diff --git a/tests/unit/test_notifier.py b/tests/unit/test_notifier.py index 1cabae0..be58da2 100644 --- a/tests/unit/test_notifier.py +++ b/tests/unit/test_notifier.py @@ -75,19 +75,30 @@ class NotifierTestCase(test_utils.BaseTestCase): notifier_api.notify(ctxt, 'publisher_id', 'event_type', notifier_api.WARN, dict(a=3)) - def test_send_rabbit_notification(self): - self.stubs.Set(cfg.CONF, 'notification_driver', - ['openstack.common.notifier.rabbit_notifier']) + def _test_rpc_notify(self, driver, envelope=False): + self.stubs.Set(cfg.CONF, 'notification_driver', [driver]) self.mock_notify = False + self.envelope = False - def mock_notify(cls, *args): + def mock_notify(cls, *args, **kwargs): self.mock_notify = True + self.envelope = kwargs.get('envelope', False) self.stubs.Set(rpc, 'notify', mock_notify) notifier_api.notify(ctxt, 'publisher_id', 'event_type', notifier_api.WARN, dict(a=3)) self.assertEqual(self.mock_notify, True) + self.assertEqual(self.envelope, envelope) + + def test_rabbit_notifier(self): + self._test_rpc_notify('openstack.common.notifier.rabbit_notifier') + + def test_rpc_notifier(self): + self._test_rpc_notify('openstack.common.notifier.rpc_notifier') + + def test_rpc_notifier2(self): + self._test_rpc_notify('openstack.common.notifier.rpc_notifier2', True) def test_invalid_priority(self): self.assertRaises(notifier_api.BadPriorityException, -- cgit