Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions akanda/rug/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.')
Expand Down Expand Up @@ -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.
Expand Down
23 changes: 23 additions & 0 deletions akanda/rug/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
23 changes: 23 additions & 0 deletions akanda/rug/test/unit/test_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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)