Merge "notifier: simply notifier_strategy compat support"

This commit is contained in:
Jenkins 2014-06-18 19:54:29 +00:00 committed by Gerrit Code Review
commit ca6eb80ad4
2 changed files with 56 additions and 71 deletions

@ -26,7 +26,7 @@ import glance.openstack.common.log as logging
from glance.openstack.common import timeutils from glance.openstack.common import timeutils
notifier_opts = [ notifier_opts = [
cfg.StrOpt('notifier_strategy', default='default', cfg.StrOpt('notifier_strategy',
help=_('Notifications can be sent when images are create, ' help=_('Notifications can be sent when images are create, '
'updated or deleted. There are three methods of sending ' 'updated or deleted. There are three methods of sending '
'notifications, logging (via the log_file directive), ' 'notifications, logging (via the log_file directive), '
@ -61,57 +61,32 @@ _ALIASES = {
class Notifier(object): class Notifier(object):
"""Uses a notification strategy to send out messages about events.""" """Uses a notification strategy to send out messages about events."""
def __init__(self, strategy=None): def __init__(self):
_driver = None driver = None
_strategy = strategy transport_url = None
publisher_id = CONF.default_publisher_id
if CONF.notifier_strategy != 'default': if CONF.notifier_strategy:
msg = _("notifier_strategy was deprecated in " msg = _("notifier_strategy was deprecated in "
"favor of `notification_driver`") "favor of `notification_driver`")
LOG.warn(msg) LOG.warn(msg)
strategy = CONF.notifier_strategy
# NOTE(flaper87): Use this to keep backwards # NOTE(flaper87): Use this to keep backwards
# compatibility. We'll try to get an oslo.messaging # compatibility. We'll try to get an oslo.messaging
# driver from the specified strategy. # driver from the specified strategy.
_strategy = strategy or CONF.notifier_strategy driver = _STRATEGY_ALIASES.get(strategy)
_driver = _STRATEGY_ALIASES.get(_strategy) if driver == 'messaging':
transport_url = strategy + ':///'
publisher_id = CONF.default_publisher_id self._transport = messaging.get_transport(CONF,
url=transport_url,
try: aliases=_ALIASES)
# NOTE(flaper87): Assume the user has configured
# the transport url.
self._transport = messaging.get_transport(CONF,
aliases=_ALIASES)
except messaging.DriverLoadFailure:
# NOTE(flaper87): Catch driver load failures and re-raise
# them *just* if the `transport_url` option was set. This
# step is intended to keep backwards compatibility and avoid
# weird behaviors (like exceptions on missing dependencies)
# when the old notifier options are used.
if CONF.transport_url is not None:
with excutils.save_and_reraise_exception():
LOG.exception(_('Error loading the notifier'))
# NOTE(flaper87): This needs to be checked
# here because the `get_transport` call
# registers `transport_url` into ConfigOpts.
if not CONF.transport_url:
# NOTE(flaper87): The next 3 lines help
# with the migration to oslo.messaging.
# Without them, gate tests won't know
# what driver should be loaded.
# Once this patch lands, devstack will be
# updated and then these lines will be removed.
url = None
if _strategy in ['rabbit', 'qpid']:
url = _strategy + '://'
self._transport = messaging.get_transport(CONF, url,
aliases=_ALIASES)
self._notifier = messaging.Notifier(self._transport, self._notifier = messaging.Notifier(self._transport,
driver=_driver, driver=driver,
publisher_id=publisher_id) publisher_id=publisher_id)
def warn(self, event_type, payload): def warn(self, event_type, payload):

@ -17,6 +17,8 @@
import datetime import datetime
import mock import mock
from oslo.config import cfg
from oslo import messaging
import webob import webob
from glance.common import exception from glance.common import exception
@ -94,42 +96,50 @@ class TaskRepoStub(object):
class TestNotifier(utils.BaseTestCase): class TestNotifier(utils.BaseTestCase):
def test_load_rabbit(self): @mock.patch.object(messaging, 'Notifier')
nfier = notifier.Notifier('rabbit') @mock.patch.object(messaging, 'get_transport')
self.assertIsNotNone(nfier._transport) def _test_load_strategy(self,
mock_get_transport, mock_notifier,
def test_load_qpid(self): strategy, url, driver):
nfier = notifier.Notifier('qpid') if strategy is not None:
self.assertIsNotNone(nfier._transport) self.config(notifier_strategy=strategy)
self.assertEqual(str(nfier._transport._driver._url),
'qpid:///')
def test_notifier_strategy(self):
self.config(notifier_strategy='qpid')
nfier = notifier.Notifier() nfier = notifier.Notifier()
mock_get_transport.assert_called_with(cfg.CONF, url=url,
aliases=notifier._ALIASES)
self.assertIsNotNone(nfier._transport) self.assertIsNotNone(nfier._transport)
self.assertEqual(str(nfier._transport._driver._url), mock_notifier.assert_called_with(nfier._transport, driver=driver,
'qpid:///') publisher_id='image.localhost')
self.assertIsNotNone(nfier._notifier)
def test_transport_url(self): def test_notifier_strategy_default(self):
transport_url = "qpid://superhost:5672/" self._test_load_strategy(strategy='default',
self.config(transport_url=transport_url) url=None,
notify = notifier.Notifier() driver='noop')
self.assertEqual(str(notify._transport._driver._url),
transport_url)
def test_notification_driver_option(self): def test_notifier_strategy_noop(self):
self.config(rpc_backend='qpid') self._test_load_strategy(strategy='noop',
self.config(notification_driver='messaging') url=None,
self.config(notifier_strategy='rabbit') driver='noop')
notify = notifier.Notifier()
self.assertEqual(str(notify._transport._driver._url),
'rabbit:///')
self.config(notifier_strategy='default') def test_notifier_strategy_rabbit(self):
notify = notifier.Notifier() self._test_load_strategy(strategy='rabbit',
self.assertEqual(str(notify._transport._driver._url), url='rabbit:///',
'qpid:///') driver='messaging')
def test_notifier_strategy_qpid(self):
self._test_load_strategy(strategy='qpid',
url='qpid:///',
driver='messaging')
def test_notifier_strategy_logging(self):
self._test_load_strategy(strategy='logging',
url=None,
driver='log')
def test_notifier_strategy_none(self):
self._test_load_strategy(strategy=None,
url=None,
driver=None)
class TestImageNotifications(utils.BaseTestCase): class TestImageNotifications(utils.BaseTestCase):