diff options
| -rw-r--r-- | openstack/common/log.py | 7 | ||||
| -rw-r--r-- | openstack/common/notifier/api.py | 60 | ||||
| -rw-r--r-- | openstack/common/notifier/list_notifier.py | 118 | ||||
| -rw-r--r-- | openstack/common/plugin/plugin.py | 1 | ||||
| -rw-r--r-- | openstack/common/plugin/pluginmanager.py | 21 | ||||
| -rw-r--r-- | tests/unit/notifier/__init__.py | 14 | ||||
| -rw-r--r-- | tests/unit/notifier/test_list_notifier.py | 135 | ||||
| -rw-r--r-- | tests/unit/test_log.py | 17 | ||||
| -rw-r--r-- | tests/unit/test_notifier.py | 106 | ||||
| -rw-r--r-- | tests/unit/test_plugin.py | 21 |
10 files changed, 159 insertions, 341 deletions
diff --git a/openstack/common/log.py b/openstack/common/log.py index 76962c2..49e6107 100644 --- a/openstack/common/log.py +++ b/openstack/common/log.py @@ -247,10 +247,9 @@ class JSONFormatter(logging.Formatter): class PublishErrorsHandler(logging.Handler): def emit(self, record): - if 'list_notifier_drivers' in CONF: - if ('openstack.common.notifier.log_notifier' in - CONF.list_notifier_drivers): - return + if ('openstack.common.notifier.log_notifier' in + CONF.notification_driver): + return notifier.api.notify(None, 'error.publisher', 'error_notification', notifier.api.ERROR, diff --git a/openstack/common/notifier/api.py b/openstack/common/notifier/api.py index e699620..f36469c 100644 --- a/openstack/common/notifier/api.py +++ b/openstack/common/notifier/api.py @@ -28,9 +28,10 @@ from openstack.common import timeutils LOG = logging.getLogger(__name__) notifier_opts = [ - cfg.StrOpt('notification_driver', - default='openstack.common.notifier.no_op_notifier', - help='Default driver for sending notifications'), + cfg.MultiStrOpt('notification_driver', + default=[], + deprecated_name='list_notifier_drivers', + help='Driver or drivers to handle sending notifications'), cfg.StrOpt('default_notification_level', default='INFO', help='Default notification level for outgoing notifications'), @@ -127,16 +128,55 @@ def notify(context, publisher_id, event_type, priority, payload): # Ensure everything is JSON serializable. payload = jsonutils.to_primitive(payload, convert_instances=True) - driver = importutils.import_module(CONF.notification_driver) msg = dict(message_id=str(uuid.uuid4()), publisher_id=publisher_id, event_type=event_type, priority=priority, payload=payload, timestamp=str(timeutils.utcnow())) - try: - driver.notify(context, msg) - except Exception, e: - LOG.exception(_("Problem '%(e)s' attempting to " - "send to notification system. Payload=%(payload)s") % - locals()) + + for driver in _get_drivers(): + try: + driver.notify(context, msg) + except Exception, e: + LOG.exception(_("Problem '%(e)s' attempting to " + "send to notification system. Payload=%(payload)s") % + locals()) + + +_drivers = None + + +def _get_drivers(): + """Instantiate, cache, and return drivers based on the CONF.""" + global _drivers + if _drivers is None: + _drivers = {} + for notification_driver in CONF.notification_driver: + add_driver(notification_driver) + + return _drivers.values() + + +def add_driver(notification_driver): + """Add a notification driver at runtime.""" + # Make sure the driver list is initialized. + _get_drivers() + if isinstance(notification_driver, basestring): + # Load and add + try: + driver = importutils.import_module(notification_driver) + _drivers[notification_driver] = driver + except ImportError as e: + LOG.exception(_("Failed to load notifier %s. " + "These notifications will not be sent.") % + notification_driver) + else: + # Driver is already loaded; just add the object. + _drivers[notification_driver] = notification_driver + + +def _reset_drivers(): + """Used by unit tests to reset the drivers.""" + global _drivers + _drivers = None diff --git a/openstack/common/notifier/list_notifier.py b/openstack/common/notifier/list_notifier.py deleted file mode 100644 index 15ae470..0000000 --- a/openstack/common/notifier/list_notifier.py +++ /dev/null @@ -1,118 +0,0 @@ -# Copyright 2011 OpenStack LLC. -# All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -from openstack.common import cfg -from openstack.common.gettextutils import _ -from openstack.common import importutils -from openstack.common import log as logging - - -list_notifier_drivers_opt = cfg.MultiStrOpt( - 'list_notifier_drivers', - default=['openstack.common.notifier.no_op_notifier'], - help='List of drivers to send notifications') - -CONF = cfg.CONF -CONF.register_opt(list_notifier_drivers_opt) - -LOG = logging.getLogger(__name__) - -drivers = None - - -class ImportFailureNotifier(object): - """Noisily re-raises some exception over-and-over when notify is called.""" - - def __init__(self, exception): - self.exception = exception - - def notify(self, context, message): - raise self.exception - - -def _get_drivers(): - """Instantiates and returns drivers based on the flag values.""" - global drivers - if drivers is None: - drivers = [] - for notification_driver in CONF.list_notifier_drivers: - try: - drivers.append(importutils.import_module(notification_driver)) - except ImportError as e: - drivers.append(ImportFailureNotifier(e)) - return drivers - - -def add_driver(notification_driver): - """Add a notification driver at runtime.""" - # Make sure the driver list is initialized. - _get_drivers() - if isinstance(notification_driver, basestring): - # Load and add - try: - drivers.append(importutils.import_module(notification_driver)) - except ImportError as e: - drivers.append(ImportFailureNotifier(e)) - else: - # Driver is already loaded; just add the object. - drivers.append(notification_driver) - - -def _object_name(obj): - name = [] - if hasattr(obj, '__module__'): - name.append(obj.__module__) - if hasattr(obj, '__name__'): - name.append(obj.__name__) - else: - name.append(obj.__class__.__name__) - return '.'.join(name) - - -def remove_driver(notification_driver): - """Remove a notification driver at runtime.""" - # Make sure the driver list is initialized. - _get_drivers() - removed = False - if notification_driver in drivers: - # We're removing an object. Easy. - drivers.remove(notification_driver) - removed = True - else: - # We're removing a driver by name. Search for it. - for driver in drivers: - if _object_name(driver) == notification_driver: - drivers.remove(driver) - removed = True - - if not removed: - raise ValueError("Cannot remove; %s is not in list" % - notification_driver) - - -def notify(context, message): - """Passes notification to multiple notifiers in a list.""" - for driver in _get_drivers(): - try: - driver.notify(context, message) - except Exception as e: - LOG.exception(_("Problem '%(e)s' attempting to send to " - "notification driver %(driver)s."), locals()) - - -def _reset_drivers(): - """Used by unit tests to reset the drivers.""" - global drivers - drivers = None diff --git a/openstack/common/plugin/plugin.py b/openstack/common/plugin/plugin.py index 9f06342..dc41f3d 100644 --- a/openstack/common/plugin/plugin.py +++ b/openstack/common/plugin/plugin.py @@ -14,7 +14,6 @@ # under the License. from openstack.common import log as logging -from openstack.common.notifier import list_notifier LOG = logging.getLogger(__name__) diff --git a/openstack/common/plugin/pluginmanager.py b/openstack/common/plugin/pluginmanager.py index d9b6bc3..b10ce46 100644 --- a/openstack/common/plugin/pluginmanager.py +++ b/openstack/common/plugin/pluginmanager.py @@ -19,7 +19,7 @@ import pkg_resources from openstack.common import cfg from openstack.common import log as logging -from openstack.common.notifier import list_notifier +from openstack.common.notifier import api as notifier_api CONF = cfg.CONF @@ -53,17 +53,6 @@ class PluginManager(object): self._service_name = service_name self.plugins = [] - def _force_use_list_notifier(self): - if (CONF.notification_driver != - 'openstack.common.notifier.list_notifier'): - if not hasattr(CONF, "list_notifier_drivers"): - CONF.list_notifier_drivers = [] - old_notifier = CONF.notification_driver - drvstring = 'openstack.common.notifier.list_notifier' - CONF.notification_driver = drvstring - if old_notifier: - list_notifier.add_driver(old_notifier) - def load_plugins(self): self.plugins = [] @@ -77,16 +66,10 @@ class PluginManager(object): LOG.error(_("Failed to load plugin %(plug)s: %(exc)s") % {'plug': entrypoint, 'exc': exc}) - # See if we need to turn on the list notifier - for plugin in self.plugins: - if plugin.notifiers: - self._force_use_list_notifier() - break - # Register individual notifiers. for plugin in self.plugins: for notifier in plugin.notifiers: - list_notifier.add_driver(notifier) + notifier_api.add_driver(notifier) def plugin_extension_factory(self, ext_mgr): for plugin in self.plugins: diff --git a/tests/unit/notifier/__init__.py b/tests/unit/notifier/__init__.py deleted file mode 100644 index 482d54e..0000000 --- a/tests/unit/notifier/__init__.py +++ /dev/null @@ -1,14 +0,0 @@ -# Copyright 2011 OpenStack LLC. -# All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. diff --git a/tests/unit/notifier/test_list_notifier.py b/tests/unit/notifier/test_list_notifier.py deleted file mode 100644 index c441067..0000000 --- a/tests/unit/notifier/test_list_notifier.py +++ /dev/null @@ -1,135 +0,0 @@ -# Copyright 2011 OpenStack LLC. -# All Rights Reserved. -# -# Licensed under the Apache License, Version 2.0 (the "License"); you may -# not use this file except in compliance with the License. You may obtain -# a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT -# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the -# License for the specific language governing permissions and limitations -# under the License. - -from openstack.common import log as logging -from openstack.common.notifier import api -from openstack.common.notifier import list_notifier -from openstack.common.notifier import log_notifier -from openstack.common.notifier import no_op_notifier -from tests import utils as test_utils - - -class SimpleNotifier(object): - def __init__(self): - self.notified = False - - def notify(self, *args): - self.notified = True - - -class NotifierListTestCase(test_utils.BaseTestCase): - """Test case for notifications""" - - def setUp(self): - super(NotifierListTestCase, self).setUp() - list_notifier._reset_drivers() - # Mock log to add one to exception_count when log.exception is called - - def mock_exception(cls, *args): - self.exception_count += 1 - - self.exception_count = 0 - list_notifier_log = logging.getLogger( - 'openstack.common.notifier.list_notifier') - self.stubs.Set(list_notifier_log, "exception", mock_exception) - # Mock no_op notifier to add one to notify_count when called. - - def mock_notify(cls, *args): - self.notify_count += 1 - - self.notify_count = 0 - self.stubs.Set(no_op_notifier, 'notify', mock_notify) - # Mock log_notifier to raise RuntimeError when called. - - def mock_notify2(cls, *args): - raise RuntimeError("Bad notifier.") - - self.stubs.Set(log_notifier, 'notify', mock_notify2) - - def tearDown(self): - list_notifier._reset_drivers() - super(NotifierListTestCase, self).tearDown() - - def test_send_notifications_successfully(self): - self.config(notification_driver='openstack.common.' - 'notifier.list_notifier', - list_notifier_drivers=[ - 'openstack.common.notifier.no_op_notifier', - 'openstack.common.notifier.no_op_notifier']) - api.notify('contextarg', 'publisher_id', 'event_type', - api.WARN, dict(a=3)) - self.assertEqual(self.notify_count, 2) - self.assertEqual(self.exception_count, 0) - - def test_send_notifications_with_errors(self): - - self.config(notification_driver='openstack.common.' - 'notifier.list_notifier', - list_notifier_drivers=[ - 'openstack.common.notifier.no_op_notifier', - 'openstack.common.notifier.log_notifier']) - api.notify('contextarg', 'publisher_id', - 'event_type', api.WARN, dict(a=3)) - self.assertEqual(self.notify_count, 1) - self.assertEqual(self.exception_count, 1) - - def test_when_driver_fails_to_import(self): - self.config(notification_driver='openstack.common.' - 'notifier.list_notifier', - list_notifier_drivers=[ - 'openstack.common.notifier.no_op_notifier', - 'openstack.common.notifier.logo_notifier', - 'fdsjgsdfhjkhgsfkj']) - api.notify('contextarg', 'publisher_id', - 'event_type', api.WARN, dict(a=3)) - self.assertEqual(self.exception_count, 2) - self.assertEqual(self.notify_count, 1) - - def test_adding_and_removing_notifier_object(self): - self.notifier_object = SimpleNotifier() - self.config(notification_driver='openstack.common.' - 'notifier.list_notifier', - list_notifier_drivers=[ - 'openstack.common.notifier.no_op_notifier']) - - list_notifier.add_driver(self.notifier_object) - api.notify(None, 'publisher_id', 'event_type', - api.WARN, dict(a=3)) - self.assertEqual(self.notify_count, 1) - self.assertTrue(self.notifier_object.notified) - - self.notifier_object.notified = False - list_notifier.remove_driver(self.notifier_object) - - api.notify(None, 'publisher_id', 'event_type', - api.WARN, dict(a=3)) - self.assertEqual(self.notify_count, 2) - self.assertFalse(self.notifier_object.notified) - - def test_adding_and_removing_notifier_module(self): - self.config(notification_driver='openstack.common.' - 'notifier.list_notifier', - list_notifier_drivers=[]) - - list_notifier.add_driver('openstack.common.notifier.no_op_notifier') - api.notify(None, 'publisher_id', 'event_type', - api.WARN, dict(a=3)) - self.assertEqual(self.notify_count, 1) - - list_notifier.remove_driver('openstack.common.notifier.no_op_notifier') - - api.notify(None, 'publisher_id', 'event_type', - api.WARN, dict(a=3)) - self.assertEqual(self.notify_count, 1) diff --git a/tests/unit/test_log.py b/tests/unit/test_log.py index 35ff72d..f0aab28 100644 --- a/tests/unit/test_log.py +++ b/tests/unit/test_log.py @@ -10,7 +10,6 @@ from openstack.common import cfg from openstack.common import jsonutils from openstack.common import log from openstack.common.notifier import api as notifier -from openstack.common.notifier import list_notifier from tests import utils as test_utils CONF = cfg.CONF @@ -86,20 +85,8 @@ class PublishErrorsHandlerTestCase(test_utils.BaseTestCase): super(PublishErrorsHandlerTestCase, self).setUp() self.publiserrorshandler = log.PublishErrorsHandler(logging.ERROR) - def test_emit_cfg_list_notifier_drivers_in_flags(self): - self.stub_flg = False - - def fake_notifier(*args, **kwargs): - self.stub_flg = True - - self.stubs.Set(notifier, 'notify', fake_notifier) - logrecord = logging.LogRecord('name', 'WARN', '/tmp', 1, - 'Message', None, None) - self.publiserrorshandler.emit(logrecord) - self.assertTrue(self.stub_flg) - - def test_emit_cfg_log_notifier_in_list_notifier_drivers(self): - self.config(list_notifier_drivers=[ + def test_emit_cfg_log_notifier_in_notifier_drivers(self): + self.config(notification_driver=[ 'openstack.common.notifier.rabbit_notifier', 'openstack.common.notifier.log_notifier']) self.stub_flg = True diff --git a/tests/unit/test_notifier.py b/tests/unit/test_notifier.py index 0ffc35c..e5c69b4 100644 --- a/tests/unit/test_notifier.py +++ b/tests/unit/test_notifier.py @@ -17,6 +17,7 @@ from openstack.common import cfg from openstack.common import context from openstack.common import log from openstack.common.notifier import api as notifier_api +from openstack.common.notifier import log_notifier from openstack.common.notifier import no_op_notifier from openstack.common.notifier import rabbit_notifier from openstack.common import rpc @@ -31,10 +32,14 @@ class NotifierTestCase(test_utils.BaseTestCase): """Test case for notifications""" def setUp(self): super(NotifierTestCase, self).setUp() - self.config(notification_driver='openstack.common.' - 'notifier.no_op_notifier') + self.config(notification_driver=['openstack.common.' + 'notifier.no_op_notifier']) self.config(default_publisher_id='publisher') + def tearDown(self): + notifier_api._reset_drivers() + super(NotifierTestCase, self).tearDown() + def test_send_notification(self): self.notify_called = False @@ -70,7 +75,7 @@ class NotifierTestCase(test_utils.BaseTestCase): def test_send_rabbit_notification(self): self.stubs.Set(cfg.CONF, 'notification_driver', - 'openstack.common.notifier.rabbit_notifier') + ['openstack.common.notifier.rabbit_notifier']) self.mock_notify = False def mock_notify(cls, *args): @@ -89,7 +94,7 @@ class NotifierTestCase(test_utils.BaseTestCase): def test_rabbit_priority_queue(self): self.stubs.Set(cfg.CONF, 'notification_driver', - 'openstack.common.notifier.rabbit_notifier') + ['openstack.common.notifier.rabbit_notifier']) self.stubs.Set(cfg.CONF, 'notification_topics', ['testnotify', ]) @@ -105,7 +110,7 @@ class NotifierTestCase(test_utils.BaseTestCase): def test_error_notification(self): self.stubs.Set(cfg.CONF, 'notification_driver', - 'openstack.common.notifier.rabbit_notifier') + ['openstack.common.notifier.rabbit_notifier']) self.stubs.Set(cfg.CONF, 'publish_errors', True) LOG = log.getLogger('common') log.setup(None) @@ -184,3 +189,94 @@ class NotifierTestCase(test_utils.BaseTestCase): self.assertEqual(3, example_api2(1, 2, bananas="delicious")) self.assertEqual(self.notify_called, True) self.assertEqual(self.context_arg, None) + + +class SimpleNotifier(object): + def __init__(self): + self.notified = False + + def notify(self, *args): + self.notified = True + + +class MultiNotifierTestCase(test_utils.BaseTestCase): + """Test case for notifications""" + + def setUp(self): + super(MultiNotifierTestCase, self).setUp() + # Mock log to add one to exception_count when log.exception is called + + def mock_exception(cls, *args): + self.exception_count += 1 + + self.exception_count = 0 + + notifier_log = log.getLogger( + 'openstack.common.notifier.api') + self.stubs.Set(notifier_log, "exception", mock_exception) + + # Mock no_op notifier to add one to notify_count when called. + def mock_notify(cls, *args): + self.notify_count += 1 + + self.notify_count = 0 + self.stubs.Set(no_op_notifier, 'notify', mock_notify) + # Mock log_notifier to raise RuntimeError when called. + + def mock_notify2(cls, *args): + raise RuntimeError("Bad notifier.") + + self.stubs.Set(log_notifier, 'notify', mock_notify2) + notifier_api._reset_drivers() + + def tearDown(self): + notifier_api._reset_drivers() + super(MultiNotifierTestCase, self).tearDown() + + def test_send_notifications_successfully(self): + self.config(notification_driver=[ + 'openstack.common.notifier.no_op_notifier']) + notifier_api.notify('contextarg', 'publisher_id', 'event_type', + notifier_api.WARN, dict(a=3)) + self.assertEqual(self.notify_count, 1) + self.assertEqual(self.exception_count, 0) + + def test_send_notifications_with_errors(self): + self.config(notification_driver=[ + 'openstack.common.notifier.no_op_notifier', + 'openstack.common.notifier.log_notifier']) + notifier_api.notify('contextarg', 'publisher_id', + 'event_type', notifier_api.WARN, dict(a=3)) + self.assertEqual(self.notify_count, 1) + self.assertEqual(self.exception_count, 1) + + def test_when_driver_fails_to_import(self): + self.config(notification_driver=[ + 'openstack.common.notifier.no_op_notifier', + 'openstack.common.notifier.logo_notifier', + 'fdsjgsdfhjkhgsfkj']) + notifier_api.notify('contextarg', 'publisher_id', + 'event_type', notifier_api.WARN, dict(a=3)) + self.assertEqual(self.exception_count, 2) + self.assertEqual(self.notify_count, 1) + + def test_adding_and_removing_notifier_object(self): + self.notifier_object = SimpleNotifier() + self.config(notification_driver=[ + 'openstack.common.notifier.no_op_notifier']) + + notifier_api.add_driver(self.notifier_object) + notifier_api.notify(None, 'publisher_id', 'event_type', + notifier_api.WARN, dict(a=3)) + self.assertEqual(self.notify_count, 1) + self.assertTrue(self.notifier_object.notified) + + self.notifier_object.notified = False + + def test_adding_and_removing_notifier_module(self): + self.config(notification_driver=[]) + + notifier_api.add_driver('openstack.common.notifier.no_op_notifier') + notifier_api.notify(None, 'publisher_id', 'event_type', + notifier_api.WARN, dict(a=3)) + self.assertEqual(self.notify_count, 1) diff --git a/tests/unit/test_plugin.py b/tests/unit/test_plugin.py index 31f2e7a..b68d6f5 100644 --- a/tests/unit/test_plugin.py +++ b/tests/unit/test_plugin.py @@ -17,9 +17,7 @@ import pkg_resources import unittest from openstack.common import cfg -from openstack.common import notifier from openstack.common.notifier import api as notifier_api -from openstack.common.notifier import no_op_notifier from openstack.common.plugin import plugin from openstack.common.plugin import pluginmanager from tests import utils as test_utils @@ -34,12 +32,6 @@ class SimpleNotifier(object): self.message_list.append(message) -class SimplerNotifier(object): - def notify(self, context, message): - global simpler_notify_called - simpler_notify_called = True - - class ManagerTestCase(test_utils.BaseTestCase): def tearDown(self): super(ManagerTestCase, self).tearDown() @@ -54,15 +46,9 @@ class NotifyTestCase(test_utils.BaseTestCase): def setUp(self): super(NotifyTestCase, self).setUp() - # Set up a 'normal' notifier to make sure the plugin logic - # doesn't mess anything up. - self.stubs.Set(cfg.CONF, 'notification_driver', - SimplerNotifier()) - global simpler_notify_called - simpler_notify_called = False - def tearDown(self): super(NotifyTestCase, self).tearDown() + notifier_api._reset_drivers() def test_add_notifier(self): notifier1 = SimpleNotifier() @@ -95,11 +81,6 @@ class NotifyTestCase(test_utils.BaseTestCase): self.assertEqual(len(notifier.message_list), 1) - # Verify that the original baseline notifier is still - # installed and working. - global simpler_notify_called - self.assertTrue(simpler_notify_called) - class StubControllerExtension(object): name = 'stubextension' |
