From 6d867ad3e2b89d44400d19482687d235f5c4a46e Mon Sep 17 00:00:00 2001 From: James King Date: Fri, 23 Jan 2015 16:36:54 -0500 Subject: [PATCH 1/2] Add NoopPublisher The code that enables publishing notifications is spread across several code paths. Instead of checking for the toggle flag at every point or in the Publisher class itself we replace the Publisher class with a NoopPublisher class which simply stubs out the methods and does nothing. This allows us to configure and declare the behaviour in main.py without spreading more conditionals all over the place. --- akanda/rug/main.py | 16 +++++++++------- akanda/rug/notifications.py | 23 +++++++++++++++++++++++ akanda/rug/test/unit/test_main.py | 23 +++++++++++++++++++++++ 3 files changed, 55 insertions(+), 7 deletions(-) diff --git a/akanda/rug/main.py b/akanda/rug/main.py index a4877de..f2f64a9 100644 --- a/akanda/rug/main.py +++ b/akanda/rug/main.py @@ -271,13 +271,15 @@ def _stop_processing(*args): ) metadata_proc.start() - if cfg.CONF.ceilometer.enabled: - # Set up the notifications publisher - publisher = notifications.Publisher( - cfg.CONF.amqp_url, - exchange_name=cfg.CONF.outgoing_notifications_exchange, - topic=cfg.CONF.ceilometer.topic, - ) + # Set up the notifications publisher + Publisher = (notifications.Publisher if cfg.CONF.ceilometer.enabled + else notifications.NoopPublisher) + publisher = Publisher( + cfg.CONF.amqp_url, + exchange_name=cfg.CONF.outgoing_notifications_exchange, + topic=cfg.CONF.ceilometer.topic, + enabled=cfg.CONF.ceilometer.enabled + ) # Set up a factory to make Workers that know how many threads to # run. diff --git a/akanda/rug/notifications.py b/akanda/rug/notifications.py index e18a591..117e541 100644 --- a/akanda/rug/notifications.py +++ b/akanda/rug/notifications.py @@ -431,3 +431,26 @@ def _send(self, ready): sender.send(msg) except Exception: LOG.exception('could not publish notification') + + +class NoopPublisher(Publisher): + """A Publisher that doesn't do anything. + + The code that publishes notifications is spread across several + classes and cannot be easily disabled in configurations that do + not require sending metrics to ceilometer. + + This class is used in place of the Publisher class to disable + sending metrics without explicitly checking in various places + across the code base. + + """ + + def start(self): + pass + + def stop(self): + pass + + def publish(self, incoming): + pass diff --git a/akanda/rug/test/unit/test_main.py b/akanda/rug/test/unit/test_main.py index 9d82140..5b50802 100644 --- a/akanda/rug/test/unit/test_main.py +++ b/akanda/rug/test/unit/test_main.py @@ -19,6 +19,7 @@ import unittest2 as unittest from akanda.rug import main +from akanda.rug import notifications as ak_notifications @mock.patch('akanda.rug.main.cfg') @@ -65,3 +66,25 @@ def test_ensure_local_service_port(self, shuffle_notifications, health, main.main() quantum = quantum_api.Quantum.return_value quantum.ensure_local_service_port.assert_called_once_with() + + def test_ceilometer_disabled(self, shuffle_notifications, health, + populate, scheduler, notifications, + multiprocessing, quantum_api, cfg): + cfg.CONF.ceilometer.enabled = False + notifications.Publisher = mock.Mock(spec=ak_notifications.Publisher) + notifications.NoopPublisher = mock.Mock( + spec=ak_notifications.NoopPublisher) + main.main() + self.assertEqual(len(notifications.Publisher.mock_calls), 0) + self.assertEqual(len(notifications.NoopPublisher.mock_calls), 1) + + def test_ceilometer_enabled(self, shuffle_notifications, health, + populate, scheduler, notifications, + multiprocessing, quantum_api, cfg): + cfg.CONF.ceilometer.enabled = True + notifications.Publisher = mock.Mock(spec=ak_notifications.Publisher) + notifications.NoopPublisher = mock.Mock( + spec=ak_notifications.NoopPublisher) + main.main() + self.assertEqual(len(notifications.Publisher.mock_calls), 1) + self.assertEqual(len(notifications.NoopPublisher.mock_calls), 0) From a6de1f665e508253ca7ceeced732b5d644ec3ba9 Mon Sep 17 00:00:00 2001 From: James King Date: Tue, 27 Jan 2015 16:22:37 -0500 Subject: [PATCH 2/2] Rename notification_topic option Renames the option to 'topic'. --- akanda/rug/main.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/akanda/rug/main.py b/akanda/rug/main.py index f2f64a9..2be2007 100644 --- a/akanda/rug/main.py +++ b/akanda/rug/main.py @@ -202,7 +202,7 @@ def register_and_load_opts(): ceilometer_group = cfg.OptGroup(name='ceilometer', title='Ceilometer Reporting Options') c_enable_reporting = cfg.BoolOpt('enabled', default=False) - c_topic = cfg.StrOpt('notification_topic', + c_topic = cfg.StrOpt('topic', default='notifications.info', help='The name of the topic queue ceilometer ' 'consumes events from.') @@ -278,7 +278,6 @@ def _stop_processing(*args): cfg.CONF.amqp_url, exchange_name=cfg.CONF.outgoing_notifications_exchange, topic=cfg.CONF.ceilometer.topic, - enabled=cfg.CONF.ceilometer.enabled ) # Set up a factory to make Workers that know how many threads to