diff --git a/akanda/rug/main.py b/akanda/rug/main.py index a4877de..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.') @@ -271,13 +271,14 @@ 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, + ) # 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)