summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorCerberus <matt.dietz@rackspace.com>2011-05-10 16:40:47 -0500
committerCerberus <matt.dietz@rackspace.com>2011-05-10 16:40:47 -0500
commitfa3195b6206cffc26d421db891e1a580a18f0fb0 (patch)
tree4f79b7b0e7be9fe677361ef639365bb5783d4508
parent90d7e6771cf28725a6b4296b44e5d078f2ed9544 (diff)
Better tests
-rw-r--r--nova/notifier/__init__.py5
-rw-r--r--nova/notifier/rabbit_notifier.py2
-rw-r--r--nova/tests/test_notifier.py40
3 files changed, 44 insertions, 3 deletions
diff --git a/nova/notifier/__init__.py b/nova/notifier/__init__.py
index aacbf8ac3..942c1a1a2 100644
--- a/nova/notifier/__init__.py
+++ b/nova/notifier/__init__.py
@@ -32,6 +32,9 @@ DEBUG = 'DEBUG'
log_levels = (DEBUG, WARN, INFO, ERROR, CRITICAL)
+class BadPriorityException(Exception):
+ pass
+
def notify(event_name, publisher_id, event_type, priority, payload):
"""
Sends a notification using the specified driver
@@ -58,6 +61,8 @@ def notify(event_name, publisher_id, event_type, priority, payload):
'payload': {'instance_id': 12, ... }}
"""
+ if priority not in log_levels:
+ raise BadPriorityException('%s not in valid priorities' % priority)
driver = utils.import_class(FLAGS.notification_driver)()
message = dict(publisher_id=publisher_id,
event_type=event_type,
diff --git a/nova/notifier/rabbit_notifier.py b/nova/notifier/rabbit_notifier.py
index e4bd85398..1d62005a8 100644
--- a/nova/notifier/rabbit_notifier.py
+++ b/nova/notifier/rabbit_notifier.py
@@ -34,4 +34,4 @@ class RabbitNotifier(object):
"""Sends a notification to the RabbitMQ"""
context = nova.context.get_admin_context()
topic = FLAGS.notification_topic
- rpc.cast(context, topic, msg)
+ rpc.cast(context, topic, payload)
diff --git a/nova/tests/test_notifier.py b/nova/tests/test_notifier.py
index 4d6289e6a..396ce13b1 100644
--- a/nova/tests/test_notifier.py
+++ b/nova/tests/test_notifier.py
@@ -13,6 +13,8 @@
# License for the specific language governing permissions and limitations
# under the License.
+import json
+
import nova
from nova import flags
@@ -42,9 +44,27 @@ class NotifierTestCase(test.TestCase):
class Mock(object):
pass
- notifier.notify('derp', Mock())
+ nova.notifier.notify('event_name', 'publisher_id', 'event_type',
+ nova.notifier.WARN, dict(a=3))
self.assertEqual(self.notify_called, True)
+ def test_verify_message_format(self):
+ """A test to ensure changing the message format is prohibitively
+ annoying"""
+ def message_assert(cls, blob):
+ message = json.loads(blob)
+ fields = [ ('publisher_id', 'publisher_id'),
+ ('event_type', 'event_type'),
+ ('priority', 'WARN'),
+ ('payload', dict(a=3))]
+ for k, v in fields:
+ self.assertEqual(message[k], v)
+
+ self.stubs.Set(nova.notifier.no_op_notifier.NoopNotifier, 'notify',
+ message_assert)
+ nova.notifier.notify('event_name', 'publisher_id', 'event_type',
+ nova.notifier.WARN, dict(a=3))
+
def test_send_rabbit_notification(self):
self.stubs.Set(nova.flags.FLAGS, 'notification_driver',
'nova.notifier.rabbit_notifier.RabbitNotifier')
@@ -55,6 +75,22 @@ class NotifierTestCase(test.TestCase):
class Mock(object):
pass
self.stubs.Set(nova.rpc, 'cast', mock_cast)
- notifier.notify('derp', Mock())
+ nova.notifier.notify('event_name', 'publisher_id', 'event_type',
+ nova.notifier.WARN, dict(a=3))
self.assertEqual(self.mock_cast, True)
+
+ def test_invalid_priority(self):
+ self.stubs.Set(nova.flags.FLAGS, 'notification_driver',
+ 'nova.notifier.rabbit_notifier.RabbitNotifier')
+ self.mock_cast = False
+ def mock_cast(cls, *args):
+ pass
+
+ class Mock(object):
+ pass
+
+ self.stubs.Set(nova.rpc, 'cast', mock_cast)
+ self.assertRaises(nova.notifier.BadPriorityException,
+ nova.notifier.notify, 'event_name', 'publisher_id',
+ 'event_type', 'not a priority', dict(a=3))