Skip to content

Commit

Permalink
Merge pull request #218 from jeremycline/configurable-topics
Browse files Browse the repository at this point in the history
Make the topic the FMN consumer subscribes to configurable
  • Loading branch information
jeremycline committed Aug 8, 2017
2 parents 8165b04 + 5e63845 commit e157251
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 3 deletions.
3 changes: 3 additions & 0 deletions fedmsg.d/fmn.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
config = {
# Consumer stuff
"fmn.consumer.enabled": True,
# The topics to subscribe to as a consumer, defaults to everything. Must be a list
# of bytestrings.
"fmn.topics": [b'*'],
"fmn.sqlalchemy.uri": "sqlite:////var/tmp/fmn-dev-db.sqlite",
"fmn.autocreate": True, # Should new packagers auto-get accounts?
"fmn.junk_suffixes": [
Expand Down
7 changes: 4 additions & 3 deletions fmn/consumer/consumer.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,12 +100,13 @@ class FMNConsumer(fedmsg.consumers.FedmsgConsumer):
config_key (str): The key to set to ``True`` in the fedmsg config to
enable this consumer. The key is ``fmn.consumer.enabled``.
"""
topic = '*'
config_key = 'fmn.consumer.enabled'

def __init__(self, *args, **kwargs):
def __init__(self, hub, *args, **kwargs):
self.topic = hub.config.get('fmn.topics', b'*')

log.debug("FMNConsumer initializing")
super(FMNConsumer, self).__init__(*args, **kwargs)
super(FMNConsumer, self).__init__(hub, *args, **kwargs)

self.uri = self.hub.config.get('fmn.sqlalchemy.uri', None)
self.autocreate = self.hub.config.get('fmn.autocreate', False)
Expand Down
52 changes: 52 additions & 0 deletions fmn/tests/consumer/test_consumer.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
# -*- coding: utf-8 -*-
#
# This file is part of FMN.
# Copyright (C) 2017 Red Hat, Inc.
#
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
"""Tests for the :mod:`fmn.consumer.consumer` module"""
from __future__ import absolute_import

import unittest

import mock

from fmn.consumer import consumer


class FMNConsumerTests(unittest.TestCase):

def setUp(self):
self.config = {
'fmn.consumer.enabled': True,
'validate_signatures': False,
'fmn.sqlalchemy.uri': 'sqlite://',
}
self.hub = mock.Mock(config=self.config)

def test_default_topic(self):
"""Assert the default topic for the FMN consumer is everything."""
fmn_consumer = consumer.FMNConsumer(self.hub)

self.assertEqual(b'*', fmn_consumer.topic)
self.hub.subscribe.assert_called_once_with(b'*', fmn_consumer._consume_json)

def test_custom_topics(self):
"""Assert the default topic for the FMN consumer is everything."""
self.config['fmn.topics'] = [b'my.custom.topic']
fmn_consumer = consumer.FMNConsumer(self.hub)

self.assertEqual([b'my.custom.topic'], fmn_consumer.topic)
self.hub.subscribe.assert_called_once_with(b'my.custom.topic', fmn_consumer._consume_json)

0 comments on commit e157251

Please sign in to comment.