Better tests

This commit is contained in:
Cerberus
2011-05-10 16:40:47 -05:00
parent 06dc53a859
commit ab016fa19b
3 changed files with 44 additions and 3 deletions

View File

@@ -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,

View File

@@ -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)

View File

@@ -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))