More unit tests and rabbit hooks
This commit is contained in:
@@ -355,5 +355,6 @@ DEFINE_string('host', socket.gethostname(),
|
||||
DEFINE_string('node_availability_zone', 'nova',
|
||||
'availability zone of this node')
|
||||
|
||||
DEFINE_string('notification_driver', 'nova.notifier.no_op_driver.NoopDriver',
|
||||
DEFINE_string('notification_driver',
|
||||
'nova.notifier.no_op_notifier.NoopNotifier',
|
||||
'Default driver for sending notifications')
|
||||
|
||||
@@ -13,7 +13,12 @@
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
def notify(model):
|
||||
from nova import flags
|
||||
from nova import utils
|
||||
|
||||
FLAGS = flags.FLAGS
|
||||
|
||||
def notify(event_name, model):
|
||||
"""Sends a notification using the specified driver"""
|
||||
driver = FLAGS.notification_driver
|
||||
driver.notify(model)
|
||||
driver = utils.import_class(FLAGS.notification_driver)()
|
||||
driver.notify(event_name, model)
|
||||
|
||||
@@ -14,6 +14,6 @@
|
||||
# under the License.
|
||||
|
||||
class NoopNotifier(object):
|
||||
def notify(self, model):
|
||||
def notify(self, event_name, model):
|
||||
"""Notifies the recipient of the desired event given the model"""
|
||||
pass
|
||||
|
||||
@@ -13,12 +13,25 @@
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import json
|
||||
|
||||
import nova.context
|
||||
|
||||
from nova import flags
|
||||
from nova import rpc
|
||||
|
||||
FLAGS = flags.FLAGS
|
||||
|
||||
flags.DEFINE_string('notification_topic', 'notifications',
|
||||
'RabbitMQ topic used for Nova notifications')
|
||||
|
||||
class RabbitNotifier(object):
|
||||
"""Sends notifications to a specific RabbitMQ server and topic"""
|
||||
pass
|
||||
|
||||
def __init__(self):
|
||||
pass
|
||||
|
||||
def notify(self, model):
|
||||
def notify(self, event_name, model):
|
||||
"""Sends a notification to the RabbitMQ"""
|
||||
pass
|
||||
context = nova.context.get_admin_context()
|
||||
topic = FLAGS.notification_topic
|
||||
msg = { 'event_name': event_name, 'model': model.__dict__ }
|
||||
rpc.cast(context, topic, json.dumps(msg))
|
||||
|
||||
@@ -13,7 +13,11 @@
|
||||
# License for the specific language governing permissions and limitations
|
||||
# under the License.
|
||||
|
||||
import nova
|
||||
|
||||
from nova import flags
|
||||
from nova import notifier
|
||||
from nova.notifier import no_op_notifier
|
||||
from nova import test
|
||||
|
||||
import stubout
|
||||
@@ -30,12 +34,27 @@ class NotifierTestCase(test.TestCase):
|
||||
|
||||
def test_send_notification(self):
|
||||
self.notify_called = False
|
||||
def mock_notify(self, model):
|
||||
def mock_notify(cls, *args):
|
||||
self.notify_called = True
|
||||
|
||||
self.stubs.set(nova.notifier.no_op_notifier.NoopNotifier, 'notify',
|
||||
self.stubs.Set(nova.notifier.no_op_notifier.NoopNotifier, 'notify',
|
||||
mock_notify)
|
||||
|
||||
model = dict(x=1, y=2)
|
||||
notifier.notify(model)
|
||||
self.assertEqual(True, self.notify_called)
|
||||
class Mock(object):
|
||||
pass
|
||||
notifier.notify('derp', Mock())
|
||||
self.assertEqual(self.notify_called, True)
|
||||
|
||||
def test_send_rabbit_notification(self):
|
||||
self.stubs.Set(nova.flags.FLAGS, 'notification_driver',
|
||||
'nova.notifier.rabbit_notifier.RabbitNotifier')
|
||||
self.mock_cast = False
|
||||
def mock_cast(cls, *args):
|
||||
self.mock_cast = True
|
||||
|
||||
class Mock(object):
|
||||
pass
|
||||
self.stubs.Set(nova.rpc, 'cast', mock_cast)
|
||||
notifier.notify('derp', Mock())
|
||||
|
||||
self.assertEqual(self.mock_cast, True)
|
||||
|
||||
Reference in New Issue
Block a user