def test_should_send_unicode_message(self, kp):
        instance = log_publisher.LogPublisher()
        instance._kafka_publisher = kp

        for um in base.UNICODE_MESSAGES:
            case, msg = um.values()
            try:
                envelope = model.Envelope(log={
                    'message': msg,
                    'application_type': 'test',
                    'dimensions': {
                        'test': 'test_log_publisher',
                        'case': 'test_should_send_unicode_message'
                    }
                },
                                          meta={'tenantId': 1})
                instance.send_message(envelope)

                expected_message = ujson.dumps(envelope, ensure_ascii=False)

                if six.PY3:
                    expected_message = expected_message.encode('utf-8')

                instance._kafka_publisher.publish.assert_called_with(
                    self.conf.conf.log_publisher.topics[0], [expected_message])
            except Exception:
                LOG.exception('Failed to evaluate unicode case %s', case)
                raise
    def test_should_send_message(self, kafka_producer):
        instance = log_publisher.LogPublisher()
        instance._kafka_publisher = kafka_producer
        instance.send_message({})

        creation_time = ((datetime.datetime.utcnow() -
                          EPOCH_START).total_seconds())
        application_type = 'monasca-log-api'
        dimension_1_name = 'disk_usage'
        dimension_1_value = '50'
        dimension_2_name = 'cpu_time'
        dimension_2_value = '60'

        msg = model.Envelope(log={
            'message': '1',
            'application_type': application_type,
            'dimensions': {
                dimension_1_name: dimension_1_value,
                dimension_2_name: dimension_2_value
            }
        },
                             meta={'tenantId': '1'})
        msg['creation_time'] = creation_time
        instance.send_message(msg)

        instance._kafka_publisher.publish.assert_called_once_with(
            self.conf.conf.log_publisher.topics[0],
            [ujson.dumps(msg, ensure_ascii=False).encode('utf-8')])
    def test_should_send_message_multiple_topics(self, _):
        topics = ['logs', 'analyzer', 'tester']
        self.conf.config(topics=topics,
                         max_message_size=5000,
                         group='log_publisher')

        instance = log_publisher.LogPublisher()
        instance._kafka_publisher = mock.Mock()
        instance.send_message({})

        creation_time = ((datetime.datetime.utcnow() - EPOCH_START)
                         .total_seconds())
        dimension_1_name = 'disk_usage'
        dimension_1_value = '50'
        dimension_2_name = 'cpu_time'
        dimension_2_value = '60'
        application_type = 'monasca-log-api'
        msg = model.Envelope(
            log={
                'message': 1,
                'application_type': application_type,
                'dimensions': {
                    dimension_1_name: dimension_1_value,
                    dimension_2_name: dimension_2_value
                }
            },
            meta={
                'tenantId': 1
            }
        )
        msg['creation_time'] = creation_time
        json_msg = ujson.dumps(msg)

        instance.send_message(msg)

        self.assertEqual(len(topics),
                         instance._kafka_publisher.publish.call_count)
        for topic in topics:
            instance._kafka_publisher.publish.assert_any_call(
                topic,
                [json_msg])
Ejemplo n.º 4
0
 def new_log_envelope(self, log_object, tenant_id):
     return model.Envelope(
         log=log_object,
         meta=self._create_meta_info(tenant_id)
     )