def test_publish_with_none_rabbit_driver(self, cgt): sample_publisher = msg_publisher.SampleNotifierPublisher( self.CONF, netutils.urlsplit('notifier://127.0.0.1:9092?driver=kafka')) cgt.assert_called_with(self.CONF, 'kafka://127.0.0.1:9092') transport = oslo_messaging.get_transport(self.CONF, 'kafka://127.0.0.1:9092') self.assertIsInstance(transport._driver, kafka_driver.KafkaDriver) side_effect = msg_publisher.DeliveryFailure() with mock.patch.object(sample_publisher, '_send') as fake_send: fake_send.side_effect = side_effect self.assertRaises( msg_publisher.DeliveryFailure, sample_publisher.publish_samples, self.test_sample_data) self.assertEqual(0, len(sample_publisher.local_queue)) self.assertEqual(100, len(fake_send.mock_calls)) fake_send.assert_called_with('metering', mock.ANY) event_publisher = msg_publisher.EventNotifierPublisher( self.CONF, netutils.urlsplit('notifier://127.0.0.1:9092?driver=kafka')) cgt.assert_called_with(self.CONF, 'kafka://127.0.0.1:9092') with mock.patch.object(event_publisher, '_send') as fake_send: fake_send.side_effect = side_effect self.assertRaises( msg_publisher.DeliveryFailure, event_publisher.publish_events, self.test_event_data) self.assertEqual(0, len(event_publisher.local_queue)) self.assertEqual(100, len(fake_send.mock_calls)) fake_send.assert_called_with('event', mock.ANY)
def publish_data(metering_connection, events_connection): '''Connects to given QDR passed by argument (default '127.0.0.1:5672') and sends sample metric and event data to the bus. ''' conf = cfg.ConfigOpts() for opts in [oslo_opts, ceilo_opts]: for group, options in opts.list_opts(): conf.register_opts(list(options), group=None if group == "DEFAULT" else group) # override default configuration according to overcloud conf.set_override('notify_address_prefix', '', group='oslo_messaging_amqp') conf.set_override('control_exchange', 'ceilometer') try: metric_publisher = messaging.SampleNotifierPublisher( conf, netutils.urlsplit(metering_connection)) event_publisher = messaging.EventNotifierPublisher( conf, netutils.urlsplit(events_connection)) except Exception as ex: print(f'Failed to connect to QDR ({url}) due to {ex}') sys.exit(1) for evt in EVENTS: itm = event.Event(message_id=evt['message_id'], event_type=evt['event_type'], generated=evt['generated'], traits=[event.Trait(*tr) for tr in evt['traits']], raw=evt['raw']) topic = conf.publisher_notifier.event_topic print( f'Sending event to {topic}: ' f'{utils.message_from_event(itm, conf.publisher.telemetry_secret)}' ) try: event_publisher.publish_events([itm]) except Exception as ex: print(f'Failed to send event due to {ex}') sys.exit(1) for metr in METRICS: itm = sample.Sample(name=metr['counter_name'], type=metr['counter_type'], unit=metr['counter_unit'], volume=metr['counter_volume'], user_id=metr['user_id'], project_id=metr['project_id'], resource_id=metr['resource_id'], timestamp=metr['timestamp'], resource_metadata=metr['resource_metadata']) topic = conf.publisher_notifier.metering_topic print( f'Sending metric to {topic}: ' f'{utils.meter_message_from_counter(itm, conf.publisher.telemetry_secret)}' ) try: metric_publisher.publish_samples([itm]) except Exception as ex: print(f'Failed to send metric due to {ex}') sys.exit(1)
def test_publish_other_host(self, cgt): msg_publisher.SampleNotifierPublisher( self.CONF, netutils.urlsplit('notifier://*****:*****@127.0.0.1:1234')) cgt.assert_called_with(self.CONF, 'rabbit://*****:*****@127.0.0.1:1234') msg_publisher.EventNotifierPublisher( self.CONF, netutils.urlsplit('notifier://*****:*****@127.0.0.1:1234')) cgt.assert_called_with(self.CONF, 'rabbit://*****:*****@127.0.0.1:1234')
def test_publish_topic_override(self, notifier): msg_publisher.SampleNotifierPublisher( netutils.urlsplit('notifier://?topic=custom_topic')) notifier.assert_called_with(mock.ANY, topic='custom_topic', driver=mock.ANY, retry=mock.ANY, publisher_id=mock.ANY) msg_publisher.EventNotifierPublisher( netutils.urlsplit('notifier://?topic=custom_event_topic')) notifier.assert_called_with(mock.ANY, topic='custom_event_topic', driver=mock.ANY, retry=mock.ANY, publisher_id=mock.ANY)
def test_publish_other_host_vhost_and_query(self, cgt): msg_publisher.SampleNotifierPublisher( netutils.urlsplit('notifier://*****:*****@127.0.0.1:1234/foo' '?driver=amqp&amqp_auto_delete=true')) cgt.assert_called_with('amqp://*****:*****@127.0.0.1:1234/foo' '?amqp_auto_delete=true') msg_publisher.EventNotifierPublisher( netutils.urlsplit('notifier://*****:*****@127.0.0.1:1234/foo' '?driver=amqp&amqp_auto_delete=true')) cgt.assert_called_with('amqp://*****:*****@127.0.0.1:1234/foo' '?amqp_auto_delete=true')