Exemple #1
0
    def setUp(self):
        self.amqp_handler = logs.AMQPHandler(level=logging.DEBUG)
        self.amqp_handler.set_job_id(None)

        self.log = logging.getLogger(self.LOGGER_NAME)
        self.log.setLevel(logging.DEBUG)
        self.log.addHandler(self.amqp_handler)

        cfg = config.get_section('amqp')
        self.connection = kombu.BrokerConnection(hostname=cfg.get('host'),
                                                 userid=cfg['user'],
                                                 password=cfg['password'],
                                                 virtual_host=cfg['vhost'])
        self.channel = self.connection.channel()
        self.exchange = kombu.entity.Exchange(cfg['exchange'],
                                              type='topic',
                                              channel=self.channel)
        self.queue = kombu.entity.Queue(exchange=self.exchange,
                                        channel=self.channel,
                                        routing_key=self.ROUTING_KEY,
                                        exclusive=True)
        self.queue.queue_declare()
        self.queue.queue_bind()
        self.consumer = kombu.messaging.Consumer(self.channel,
                                                 self.queue,
                                                 no_ack=True,
                                                 auto_declare=False)
        self.producer = kombu.messaging.Producer(self.channel,
                                                 self.exchange,
                                                 serializer='json')
Exemple #2
0
 def test_init_logs_amqp_send_with_existing_amqp_handler(self):
     """
     init_logs_amqp_send() will not add more than one `AMQPHandler`
     instance to the root logger.
     """
     mm = mock.MagicMock(spec=kombu.messaging.Producer)
     with mock.patch.object(logs.AMQPHandler, "_initialize") as minit:
         minit.return_value = mm
         handler = logs.AMQPHandler()
         handler.set_job_id = mock.Mock()
         logging.root.handlers.append(handler)
         with helpers.patch("logging.root.addHandler") as mah:
             logs.init_logs_amqp_send("info", 322)
             self.assertEqual(0, mah.call_count)
             self.assertEqual(1, handler.set_job_id.call_count)
             self.assertEqual((322, ), handler.set_job_id.call_args[0])
Exemple #3
0
    def test_init_logs_amqp_send_changes_logging_level(self):
        """
        init_logs_amqp_send() will change the root level logger anyway.
        """
        mm = mock.MagicMock(spec=kombu.messaging.Producer)
        with mock.patch.object(logs.AMQPHandler, "_initialize") as minit:
            minit.return_value = mm
            handler = logs.AMQPHandler()
            logging.root.handlers.append(handler)
            handler.set_job_id = mock.Mock()

            logging.root.setLevel(logging.INFO)

            logs.init_logs_amqp_send("warning", 322)
            self.assertEqual(logging.root.level, logging.WARNING)

            logs.init_logs_amqp_send("debug", 323)
            self.assertEqual(logging.root.level, logging.DEBUG)

            logs.init_logs_amqp_send("error", 324)
            self.assertEqual(logging.root.level, logging.ERROR)