Ejemplo n.º 1
0
class TestCore(unittest.TestCase):
    def setUp(self):
        config = load_config()
        config['io_threads'] = 1
        self.ctx = FedMsgContext(**config)

    def test_send_message(self):
        """send_message is deprecated

        It tests
        - deprecation warning showing up appropriately
        - that we call publish method behind the scene
        """
        fake_topic = "org.fedoraproject.prod.compose.rawhide.complete"
        fake_msg = "{'arch'': 's390', 'branch': 'rawhide', 'log': 'done'}"
        self.ctx.publish = mock.Mock(spec_set=FedMsgContext.publish)
        with warnings.catch_warnings(record=True) as w:
            warnings.simplefilter("always")
            self.ctx.send_message(topic=fake_topic, msg=fake_msg)
            assert len(w) == 1
            assert str(w[0].message) == ".send_message is deprecated."
            assert self.ctx.publish.called
            topic, msg, modname = self.ctx.publish.call_args[0]
            assert topic == fake_topic
            assert msg == fake_msg
            assert modname is None
Ejemplo n.º 2
0
def test_init_missing_cert():
    """ Try to initialize the context with a nonexistant cert. """
    config = load_config()
    config["name"] = "failboat"
    config["sign_messages"] = True
    context = FedMsgContext(**config)
    context.publish(topic="awesome", msg=dict(foo="bar"))
Ejemplo n.º 3
0
def test_init_missing_cert():
    """ Try to initialize the context with a nonexistant cert. """
    config = load_config()
    config['name'] = "failboat"
    config['sign_messages'] = True
    context = FedMsgContext(**config)
    context.publish(topic='awesome', msg=dict(foo='bar'))
Ejemplo n.º 4
0
    def setUp(self):
        config = load_config()
        self.hub = CentralMokshaHub(config=config)
        self.context = FedMsgContext(**config)

        # fully qualified
        self.fq_topic = "org.fedoraproject.dev.unittest.foo"
        # short version
        self.topic = "foo"
Ejemplo n.º 5
0
    def setUp(self):
        config = load_config()
        self.hub = CentralMokshaHub(config=config)
        self.context = FedMsgContext(**config)

        # fully qualified
        self.fq_topic = "com.test_prefix.dev.%s.foo" % unittest.__name__
        # short version
        self.topic = "foo"
Ejemplo n.º 6
0
    def setUp(self):
        config = load_config()
        self.hub = CentralMokshaHub(config=config)
        self.context = FedMsgContext(**config)

        # fully qualified
        self.fq_topic = "org.fedoraproject.dev.unittest.foo"
        # short version
        self.topic = "foo"
Ejemplo n.º 7
0
    def setUp(self):
        config = load_config()
        self.hub = CentralMokshaHub(config=config)
        self.context = FedMsgContext(**config)

        # fully qualified
        self.fq_topic = "com.test_prefix.dev.%s.foo" % unittest.__name__
        # short version
        self.topic = "foo"
Ejemplo n.º 8
0
#   HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
#   WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
#   FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
#   OTHER DEALINGS IN THE SOFTWARE.
"""
Setup a fedmsg context suitable for Debexpo.
"""

__author__ = 'Simon Chopin'
__copyright__ = 'Copyright © 2013 Simon Chopin'
__license__ = 'MIT'

from fedmsg.core import FedMsgContext
from fedmsg.config import load_config
from threading import Lock
import pylons

# We use the fedmsg configuration from /etc/fedmsg.d/
fedmsg_config = load_config([], None)

fedmsg_config['name'] = pylons.config["debexpo.fedmsg_endpoint_name"]

__context = FedMsgContext(**fedmsg_config)
__context_lock = Lock()


def publish(**kw):
    __context_lock.acquire()
    __context.publish(**kw)
    __context_lock.release()
Ejemplo n.º 9
0
class TestHub(unittest.TestCase):
    def setUp(self):
        config = load_config()
        self.hub = CentralMokshaHub(config=config)
        self.context = FedMsgContext(**config)

        # fully qualified
        self.fq_topic = "org.fedoraproject.dev.unittest.foo"
        # short version
        self.topic = "foo"

    def tearDown(self):
        self.context.destroy()
        self.hub.close()

    def test_send_recv(self):
        """ Send a message and receive it.

        Admittedly, this is not a unit test, but an integration test.

        It tests:

            - Sending a message.
            - Receiving a message.
            - Encoding *and* decoding.

        """
        messages_received = []

        def callback(json):
            messages_received.append(fedmsg.encoding.loads(json.body))

        self.hub.subscribe(topic=self.fq_topic, callback=callback)
        sleep(sleep_duration)

        self.context.publish(topic=self.topic, msg=secret)

        simulate_reactor(sleep_duration)
        sleep(sleep_duration)

        eq_(len(messages_received), 1)
        eq_(messages_received[0]['msg'], secret)

    def fake_register_consumer(self, cons):
        """ Fake register a consumer, not by entry-point like usual.

        Normally, consumers are identified by the hub by way of entry-points
        Ideally, this test would register the TestConsumer on the
        moksha.consumers entry point, and the hub would pick it up.
        I'm not sure how to do that, so we're going to fake it and manually
        add this consumer to the list of consumers of which the Hub is aware.
        """
        self.hub.topics[cons.topic] = self.hub.topics.get(cons.topic, [])
        self.hub.topics[cons.topic].append(cons(self.hub).consume)
        sleep(sleep_duration)

    def test_consumer(self):
        """ Check that a consumer can get messages. """
        obj = {'secret': secret}
        messages_received = []

        class TestConsumer(fedmsg.consumers.FedmsgConsumer):
            topic = self.fq_topic
            config_key = "test_consumer_enabled"

            def consume(self, message):
                messages_received.append(message['body']['msg'])

        self.fake_register_consumer(TestConsumer)

        # Now, send a generic message to that topic, and see if the consumer
        # processed it.
        self.context.publish(topic=self.topic, msg=obj)

        simulate_reactor(sleep_duration)
        sleep(sleep_duration)

        eq_(len(messages_received), 1)
        eq_(messages_received[0], obj)

    def test_double_consumers(self):
        """ Check that two consumers can get messages. """
        obj = {'secret': secret}
        messages_received = []

        class TestConsumer1(fedmsg.consumers.FedmsgConsumer):
            topic = self.fq_topic
            config_key = "test_consumer_enabled"

            def consume(self, message):
                messages_received.append(message['body']['msg'])

        class TestConsumer2(fedmsg.consumers.FedmsgConsumer):
            topic = self.fq_topic
            config_key = "test_consumer_enabled"

            def consume(self, message):
                messages_received.append(message['body']['msg'])

        self.fake_register_consumer(TestConsumer1)
        self.fake_register_consumer(TestConsumer2)

        # Now, send a generic message to that topic, and see if the consumer
        # processed it.
        self.context.publish(topic=self.topic, msg=obj)

        simulate_reactor(sleep_duration)
        sleep(sleep_duration)

        eq_(len(messages_received), 2)
        eq_(messages_received[0], obj)
        eq_(messages_received[1], obj)

    def test_consumer_failed_validation(self):
        """ Check that a consumer won't consume invalid message. """
        obj = {'secret': secret}
        messages_received = []

        class TestConsumer(fedmsg.consumers.FedmsgConsumer):
            topic = self.fq_topic
            config_key = "test_consumer_enabled"

            def consume(self, message):
                messages_received.append(message['body']['msg'])

            def validate(self, message):
                raise RuntimeWarning("Marking message as invalid.")

        self.fake_register_consumer(TestConsumer)
        self.context.publish(topic=self.topic, msg=obj)
        simulate_reactor(sleep_duration)
        sleep(sleep_duration)

        # Verify that we received no message.
        eq_(len(messages_received), 0)
Ejemplo n.º 10
0
def test_init_missing_endpoint():
    """ Try to initialize the context with a nonexistant service name. """
    config = load_config()
    config['name'] = "failboat"
    config['sign_messages'] = True
    context = FedMsgContext(**config)
Ejemplo n.º 11
0
class TestHub(unittest.TestCase):

    def setUp(self):
        config = load_config()
        self.hub = CentralMokshaHub(config=config)
        self.context = FedMsgContext(**config)

        # fully qualified
        self.fq_topic = "org.fedoraproject.dev.unittest.foo"
        # short version
        self.topic = "foo"

    def tearDown(self):
        self.context.destroy()
        self.hub.close()

    def test_send_recv(self):
        """ Send a message and receive it.

        Admittedly, this is not a unit test, but an integration test.

        It tests:

            - Sending a message.
            - Receiving a message.
            - Encoding *and* decoding.

        """
        messages_received = []

        def callback(json):
            messages_received.append(fedmsg.encoding.loads(json.body))

        self.hub.subscribe(topic=self.fq_topic, callback=callback)
        sleep(sleep_duration)

        self.context.publish(topic=self.topic, msg=secret)

        simulate_reactor(sleep_duration)
        sleep(sleep_duration)

        eq_(len(messages_received), 1)
        eq_(messages_received[0]['msg'], secret)

    def fake_register_consumer(self, cons):
        """ Fake register a consumer, not by entry-point like usual.

        Normally, consumers are identified by the hub by way of entry-points
        Ideally, this test would register the TestConsumer on the
        moksha.consumers entry point, and the hub would pick it up.
        I'm not sure how to do that, so we're going to fake it and manually
        add this consumer to the list of consumers of which the Hub is aware.
        """
        self.hub.topics[cons.topic] = self.hub.topics.get(cons.topic, [])
        self.hub.topics[cons.topic].append(cons(self.hub).consume)
        sleep(sleep_duration)

    def test_consumer(self):
        """ Check that a consumer can get messages. """
        obj = {'secret': secret}
        messages_received = []

        class TestConsumer(fedmsg.consumers.FedmsgConsumer):
            topic = self.fq_topic
            config_key = "test_consumer_enabled"

            def consume(self, message):
                messages_received.append(
                    message['body']['msg']
                )

        self.fake_register_consumer(TestConsumer)

        # Now, send a generic message to that topic, and see if the consumer
        # processed it.
        fedmsg.publish(topic=self.topic, msg=obj)

        simulate_reactor(sleep_duration)
        sleep(sleep_duration)

        eq_(len(messages_received), 1)
        eq_(messages_received[0], obj)

    def test_double_consumers(self):
        """ Check that two consumers can get messages. """
        obj = {'secret': secret}
        messages_received = []

        class TestConsumer1(fedmsg.consumers.FedmsgConsumer):
            topic = self.fq_topic
            config_key = "test_consumer_enabled"

            def consume(self, message):
                messages_received.append(
                    message['body']['msg']
                )

        class TestConsumer2(fedmsg.consumers.FedmsgConsumer):
            topic = self.fq_topic
            config_key = "test_consumer_enabled"

            def consume(self, message):
                messages_received.append(
                    message['body']['msg']
                )

        self.fake_register_consumer(TestConsumer1)
        self.fake_register_consumer(TestConsumer2)

        # Now, send a generic message to that topic, and see if the consumer
        # processed it.
        fedmsg.publish(topic=self.topic, msg=obj)

        simulate_reactor(sleep_duration)
        sleep(sleep_duration)

        eq_(len(messages_received), 2)
        eq_(messages_received[0], obj)
        eq_(messages_received[1], obj)

    def test_consumer_failed_validation(self):
        """ Check that a consumer won't consume invalid message. """
        obj = {'secret': secret}
        messages_received = []

        class TestConsumer(fedmsg.consumers.FedmsgConsumer):
            topic = self.fq_topic
            config_key = "test_consumer_enabled"

            def consume(self, message):
                messages_received.append(
                    message['body']['msg']
                )

            def validate(self, message):
                raise RuntimeWarning("Marking message as invalid.")

        self.fake_register_consumer(TestConsumer)
        fedmsg.publish(topic=self.topic, msg=obj)
        simulate_reactor(sleep_duration)
        sleep(sleep_duration)

        # Verify that we received no message.
        eq_(len(messages_received), 0)
Ejemplo n.º 12
0
class TestHub(TestCase):

    def setUp(self):
        config = load_config()
        self.hub = CentralMokshaHub(config=config)
        self.context = FedMsgContext(**config)

        # fully qualified
        self.fq_topic = "org.fedoraproject.dev.unittest.foo"
        # short version
        self.topic = "foo"

    def tearDown(self):
        self.context.destroy()
        self.hub.close()

    def test_run_hub_get_heartbeat(self):
        """ Start the heartbeat producer and ensure it emits a message. """
        messages_received = []

        def callback(json):
            messages_received.append(fedmsg.json.loads(json.body))

        self.hub.subscribe(
            topic=HeartbeatProducer.topic,
            callback=callback,
        )

        simulate_reactor(HeartbeatProducer.frequency.seconds*1.1)
        sleep(HeartbeatProducer.frequency.seconds*1.1)

        eq_(len(messages_received), 1)

    def test_send_recv(self):
        """ Send a message and receive it.

        Admittedly, this is not a unit test, but an integration test.

        It tests:

            - Sending a message.
            - Receiving a message.
            - Encoding *and* decoding.

        """
        messages_received = []

        def callback(json):
            messages_received.append(fedmsg.json.loads(json.body))

        self.hub.subscribe(topic=self.fq_topic, callback=callback)
        sleep(sleep_duration)

        self.context.send_message(topic=self.topic, msg=secret)

        simulate_reactor(sleep_duration)
        sleep(sleep_duration)

        eq_(len(messages_received), 1)
        eq_(messages_received[0]['msg'], secret)

    def fake_register_consumer(self, cons):
        """ Fake register a consumer, not by entry-point like usual.

        Normally, consumers are identified by the hub by way of entry-points
        Ideally, this test would register the TestConsumer on the
        moksha.consumers entry point, and the hub would pick it up.
        I'm not sure how to do that, so we're going to fake it and manually
        add this consumer to the list of consumers of which the Hub is aware.
        """
        self.hub.topics[cons.topic] = self.hub.topics.get(cons.topic, [])
        self.hub.topics[cons.topic].append(cons(self.hub).consume)
        sleep(sleep_duration)

    def test_consumer(self):
        """ Check that a consumer can get messages. """
        obj = {'secret': secret}
        messages_received = []

        class TestConsumer(moksha.api.hub.consumer.Consumer):
            topic = self.fq_topic

            def consume(self, message):
                messages_received.append(
                    message['body']['msg']
                )

        self.fake_register_consumer(TestConsumer)

        # Now, send a generic message to that topic, and see if the consumer
        # processed it.
        fedmsg.send_message(topic=self.topic, msg=obj)

        simulate_reactor(sleep_duration)
        sleep(sleep_duration)

        eq_(len(messages_received), 1)
        eq_(messages_received[0], obj)

    def test_double_consumers(self):
        """ Check that two consumers can get messages. """
        obj = {'secret': secret}
        messages_received = []

        class TestConsumer1(moksha.api.hub.consumer.Consumer):
            topic = self.fq_topic

            def consume(self, message):
                messages_received.append(
                    message['body']['msg']
                )

        class TestConsumer2(moksha.api.hub.consumer.Consumer):
            topic = self.fq_topic

            def consume(self, message):
                messages_received.append(
                    message['body']['msg']
                )

        self.fake_register_consumer(TestConsumer1)
        self.fake_register_consumer(TestConsumer2)

        # Now, send a generic message to that topic, and see if the consumer
        # processed it.
        fedmsg.send_message(topic=self.topic, msg=obj)

        simulate_reactor(sleep_duration)
        sleep(sleep_duration)

        eq_(len(messages_received), 2)
        eq_(messages_received[0], obj)
        eq_(messages_received[1], obj)
Ejemplo n.º 13
0
 def setUp(self):
     config = load_config()
     config['io_threads'] = 1
     self.ctx = FedMsgContext(**config)
Ejemplo n.º 14
0
class TestHub(TestCase):
    def setUp(self):
        config = load_config()
        self.hub = CentralMokshaHub(config=config)
        self.context = FedMsgContext(**config)

        # fully qualified
        self.fq_topic = "org.fedoraproject.dev.unittest.foo"
        # short version
        self.topic = "foo"

    def tearDown(self):
        self.context.destroy()
        self.hub.close()

    def test_run_hub_get_heartbeat(self):
        """ Start the heartbeat producer and ensure it emits a message. """
        messages_received = []

        def callback(json):
            messages_received.append(fedmsg.json.loads(json.body))

        self.hub.subscribe(
            topic=HeartbeatProducer.topic,
            callback=callback,
        )

        simulate_reactor(HeartbeatProducer.frequency.seconds * 1.1)
        sleep(HeartbeatProducer.frequency.seconds * 1.1)

        eq_(len(messages_received), 1)

    def test_send_recv(self):
        """ Send a message and receive it.

        Admittedly, this is not a unit test, but an integration test.

        It tests:

            - Sending a message.
            - Receiving a message.
            - Encoding *and* decoding.

        """
        messages_received = []

        def callback(json):
            messages_received.append(fedmsg.json.loads(json.body))

        self.hub.subscribe(topic=self.fq_topic, callback=callback)
        sleep(sleep_duration)

        self.context.send_message(topic=self.topic, msg=secret)

        simulate_reactor(sleep_duration)
        sleep(sleep_duration)

        eq_(len(messages_received), 1)
        eq_(messages_received[0]['msg'], secret)

    def fake_register_consumer(self, cons):
        """ Fake register a consumer, not by entry-point like usual.

        Normally, consumers are identified by the hub by way of entry-points
        Ideally, this test would register the TestConsumer on the
        moksha.consumers entry point, and the hub would pick it up.
        I'm not sure how to do that, so we're going to fake it and manually
        add this consumer to the list of consumers of which the Hub is aware.
        """
        self.hub.topics[cons.topic] = self.hub.topics.get(cons.topic, [])
        self.hub.topics[cons.topic].append(cons(self.hub).consume)
        sleep(sleep_duration)

    def test_consumer(self):
        """ Check that a consumer can get messages. """
        obj = {'secret': secret}
        messages_received = []

        class TestConsumer(moksha.api.hub.consumer.Consumer):
            topic = self.fq_topic

            def consume(self, message):
                messages_received.append(message['body']['msg'])

        self.fake_register_consumer(TestConsumer)

        # Now, send a generic message to that topic, and see if the consumer
        # processed it.
        fedmsg.send_message(topic=self.topic, msg=obj)

        simulate_reactor(sleep_duration)
        sleep(sleep_duration)

        eq_(len(messages_received), 1)
        eq_(messages_received[0], obj)

    def test_double_consumers(self):
        """ Check that two consumers can get messages. """
        obj = {'secret': secret}
        messages_received = []

        class TestConsumer1(moksha.api.hub.consumer.Consumer):
            topic = self.fq_topic

            def consume(self, message):
                messages_received.append(message['body']['msg'])

        class TestConsumer2(moksha.api.hub.consumer.Consumer):
            topic = self.fq_topic

            def consume(self, message):
                messages_received.append(message['body']['msg'])

        self.fake_register_consumer(TestConsumer1)
        self.fake_register_consumer(TestConsumer2)

        # Now, send a generic message to that topic, and see if the consumer
        # processed it.
        fedmsg.send_message(topic=self.topic, msg=obj)

        simulate_reactor(sleep_duration)
        sleep(sleep_duration)

        eq_(len(messages_received), 2)
        eq_(messages_received[0], obj)
        eq_(messages_received[1], obj)