Exemple #1
0
    def test_call_buildsys(self, mock_method):
        """ Assert that `__call__` calls correct method based on message topic. """
        message = Message(topic="buildsys.task.state.change")

        self.consumer.__call__(message)

        mock_method.assert_called_with(message)
Exemple #2
0
    def test_call_pass(self, mock_log):
        """ Assert that `__call__` pass based on message topic. """
        message = Message(topic="dummy")

        self.consumer.__call__(message)

        self.assertIn("Dropping 'dummy' {}", mock_log.debug.call_args_list[1][0][0])
Exemple #3
0
    def test_call_anitya_map(self, mock_method):
        """ Assert that `__call__` calls correct method based on message topic. """
        message = Message(topic="anitya.project.map.new")

        self.consumer.__call__(message)

        mock_method.assert_called_with(message)
        def wrapper(*args, **kwargs):
            fixture = os.path.join(FIXTURES_DIR, topic, name + ".json")
            with open(fixture, "r") as fp:
                body = json.load(fp)

            message = Message(topic=topic, body=body)
            kwargs["message"] = message

            return func(*args, **kwargs)
    def fedora_messaging_callback(self, message: Message):
        """
        Create celery task from fedora message
        :param message: Message from Fedora message bus
        :return: None
        """
        if message.body.get("user") != "packit":
            logger.info("Not built by packit!")
            return

        if message.topic not in INTERESTED_TOPICS:
            logger.debug("Not interested topic")
            return

        logger.info(message.body.get("what"))
        message.body["topic"] = message.topic
        message.body["timestamp"] = datetime.utcnow().timestamp()
        self.celery_app.send_task(name="task.steve_jobs.process_message",
                                  kwargs={"event": message.body})
    def fedora_messaging_callback(self, message: Message):
        """
        Create celery task from fedora message
        :param message: Message from Fedora message bus
        :return: None
        """
        if message.body.get("owner") != "packit":
            logger.info("Not handled by packit!")
            return

        logger.info(message.body.get("what"))

        message.body["topic"] = message.topic
        self.celery_app.send_task(name="task.steve_jobs.process_message",
                                  kwargs={"event": message.body})
    def publish(self, topic, msg):
        """
        Publish a fedora-messaging message to the specified topic.

        Args:
            topic (str): Topic to publish to
            msg (dict): Message to be send
        """
        _log.info("publishing topic %r" % topic)
        if self.config["legacy_messaging"]:
            fedmsg.publish(modname="hotness", topic=topic, msg=msg)
        else:
            try:
                fm_publish(Message(topic="hotness.{}".format(topic), body=msg))
            except PublishReturned as e:
                _log.warning("Fedora messaging broker rejected message %s:%s",
                             msg.id, e)
            except ConnectionException as e:
                _log.warning("Error sending the message %s:%s", msg.id, e)
Exemple #8
0
    def test_publish(self):
        # Check the publish method.
        body = {"bodykey": "bodyvalue"}
        headers = {"headerkey": "headervalue"}
        message = Message(body, headers, "testing.topic")
        d = self.protocol.publish(message, "test-exchange")

        def _check(_):
            self.protocol._channel.basic_publish.assert_called_once()
            args = self.protocol._channel.basic_publish.call_args_list[0][1]
            self.assertEqual(args["exchange"], "test-exchange")
            self.assertEqual(args["routing_key"], b"testing.topic")
            self.assertEqual(args["body"], json.dumps(body).encode("utf-8"))
            props = args["properties"]
            self.assertEqual(props.headers, headers)
            self.assertEqual(props.content_encoding, "utf-8")
            self.assertEqual(props.content_type, "application/json")
            self.assertEqual(props.delivery_mode, 2)

        d.addCallback(_check)
        return pytest_twisted.blockon(d)
Exemple #9
0
def _convert_and_maybe_publish(topic, zmq_message, exchange):
    """
    Try to convert a fedmsg to a valid fedora-messaging AMQP message and
    publish it.  If something is wrong, no exception will be raised and the
    message will just be dropped.

    Args:
        topic (bytes): The ZeroMQ message topic. Assumed to be UTF-8 encoded.
        zmq_message (bytes): The ZeroMQ message. Assumed to be UTF-8 encoded.
        exchange (str): The name of the AMQP exchange to publish to.
    """
    try:
        zmq_message = json.loads(zmq_message)
    except (ValueError, TypeError):
        _log.error("Failed to parse %r as a json message", repr(zmq_message))
        return

    try:
        if zmq_message["username"] == "amqp-bridge":
            _log.debug(
                "Dropping message %s as it's from the AMQP->ZMQ bridge",
                zmq_message["msg_id"],
            )
            return
    except KeyError:
        # Some messages aren't coming from fedmsg so they lack the username key
        if "msg_id" in zmq_message:
            _log.info(
                'Publishing %s despite it missing the normal "username" key',
                zmq_message["msg_id"],
            )
        else:
            _log.error("Message is missing a message id, dropping it")
            return

    if fedmsg_config.conf["validate_signatures"] and not fedmsg.crypto.validate(
        zmq_message, **fedmsg_config.conf
    ):
        _log.error("Message on topic %r failed validation", topic)
        return

    try:
        body = zmq_message["msg"]
    except KeyError:
        _log.error(
            "The zeromq message %r didn't have a 'msg' key; dropping", zmq_message
        )
        return

    try:
        headers = zmq_message["headers"]
    except KeyError:
        headers = None

    message = Message(body=body, headers=headers, topic=topic.decode("utf-8"))
    message.id = zmq_message["msg_id"]

    _log.debug("Publishing %r to %r", body, topic)
    try:
        api.publish(message, exchange=exchange)
    except Exception as e:
        _log.exception(
            'Publishing "%r" to exchange "%r" on topic "%r" failed (%r)',
            body,
            exchange,
            topic,
            e,
        )
Exemple #10
0
 def fedora_messaging_callback(self, message: Message):
     if message.topic not in GITHUB_TOPICS:
         logger.info(f"Message {message.topic} not in {GITHUB_TOPICS}.")
         return
     message.body["topic"] = message.topic
     self.execute(message)