def test_zmqdriver_listen_for_notification(self, mock_connection, mock_listener): mock_listener.return_value = listener = mock.Mock() mock_connection.return_value = conn = mock.Mock() conn.create_consumer = mock.Mock() conn.consume_in_thread = mock.Mock() topic = 'testtopic.foo' data = [(messaging.Target(topic=topic), 0)] # NOTE(jamespage): Pooling not supported, just pass None for now. self.driver.listen_for_notifications(data, None) conn.create_consumer.assert_called_with("%s-%s" % (topic, 0), listener)
def test_no_server_topic(self): transport = messaging.get_transport(self.conf, url='fake:') target = messaging.Target(server='testserver') server = messaging.get_rpc_server(transport, target, []) try: server.start() except Exception as ex: self.assertIsInstance(ex, messaging.InvalidTarget, ex) self.assertEqual('testserver', ex.target.server) else: self.assertTrue(False)
def test_send_no_reply(self): driver = amqp_driver.ProtonDriver(self.conf, self._broker_url) target = messaging.Target(topic="test-topic") listener = _ListenerThread(driver.listen(target), 1) rc = driver.send(target, {"context": True}, {"msg": "value"}, wait_for_reply=False) self.assertIsNone(rc) listener.join(timeout=30) self.assertFalse(listener.isAlive()) self.assertEqual(listener.messages.get().message, {"msg": "value"}) driver.cleanup()
def __init__(self, context, id): self.context = context self.id = id super(API, self).__init__() target = messaging.Target(topic=self._get_routing_key(), version=rpc_version.RPC_API_VERSION) self.version_cap = rpc_version.VERSION_ALIASES.get( CONF.upgrade_levels.guestagent) self.client = self.get_client(target, self.version_cap)
def test_send_notification(self): target = messaging.Target(exchange="exchange_test", topic="topic_test.info") with mock.patch('qpid.messaging.Connection') as conn_cls: conn = conn_cls.return_value session = conn.session.return_value self.driver.send_notification(target, {}, {}, "2.0") session.sender.assert_called_with( AddressNodeMatcher( "amq.topic/topic/exchange_test/topic_test.info"))
def test_dispatcher(self): endpoints = [] for endpoint_methods in self.endpoints: e = mock.Mock(spec=endpoint_methods) endpoints.append(e) for m in endpoint_methods: method = getattr(e, m) if self.ex: method.side_effect = self.ex() else: method.return_value = self.return_value msg = notification_msg.copy() msg['priority'] = self.priority targets = [messaging.Target(topic='notifications')] dispatcher = notify_dispatcher.NotificationDispatcher( targets, endpoints, None, allow_requeue=True) # check it listen on wanted topics self.assertEqual( sorted( set((targets[0], prio) for prio in itertools.chain.from_iterable(self.endpoints))), sorted(dispatcher._targets_priorities)) incoming = mock.Mock(ctxt={}, message=msg) with dispatcher(incoming) as callback: callback() # check endpoint callbacks are called or not for i, endpoint_methods in enumerate(self.endpoints): for m in endpoint_methods: if m == self.endpoints_expect_calls[i]: method = getattr(endpoints[i], m) method.assert_called_once_with({}, msg['publisher_id'], msg['event_type'], msg['payload'], { 'timestamp': mock.ANY, 'message_id': mock.ANY }) else: self.assertEqual(0, endpoints[i].call_count) if self.ex: self.assertEqual(1, incoming.acknowledge.call_count) self.assertEqual(0, incoming.requeue.call_count) elif self.return_value == messaging.NotificationResult.HANDLED \ or self.return_value is None: self.assertEqual(1, incoming.acknowledge.call_count) self.assertEqual(0, incoming.requeue.call_count) elif self.return_value == messaging.NotificationResult.REQUEUE: self.assertEqual(0, incoming.acknowledge.call_count) self.assertEqual(1, incoming.requeue.call_count)
def test_zmqdriver_send_notification(self, mock_multi_send, mock_cast): context = mock.Mock(autospec=impl_zmq.RpcContext) topic = 'testtopic.foo' topic_reformat = 'testtopic-foo' msg = 'jeronimo' self.driver.send_notification(messaging.Target(topic=topic), context, msg, False, False) mock_multi_send.assert_called_with(self.driver, mock_cast, context, topic_reformat, msg, allowed_remote_exmods=[], envelope=False, pooled=True)
def test_two_exchanges(self): transport = messaging.get_transport(self.conf, url='fake:') endpoint = mock.Mock() endpoint.info.return_value = None targets = [messaging.Target(topic="topic", exchange="exchange1"), messaging.Target(topic="topic", exchange="exchange2")] listener_thread = self._setup_listener(transport, [endpoint], 3, targets=targets) notifier = self._setup_notifier(transport, topic="topic") def mock_notifier_exchange(name): def side_effect(target, ctxt, message, version): target.exchange = name return transport._driver.send_notification(target, ctxt, message, version) transport._send_notification = mock.MagicMock( side_effect=side_effect) notifier.info({'ctxt': '0'}, 'an_event.start', 'test message default exchange') mock_notifier_exchange('exchange1') notifier.info({'ctxt': '1'}, 'an_event.start', 'test message exchange1') mock_notifier_exchange('exchange2') notifier.info({'ctxt': '2'}, 'an_event.start', 'test message exchange2') self._stop_listener(listener_thread) endpoint.info.assert_has_calls([ mock.call({'ctxt': '1'}, 'testpublisher', 'an_event.start', 'test message exchange1', {'timestamp': mock.ANY, 'message_id': mock.ANY}), mock.call({'ctxt': '2'}, 'testpublisher', 'an_event.start', 'test message exchange2', {'timestamp': mock.ANY, 'message_id': mock.ANY})], any_order=True)
def test_dispatcher(self): endpoints = [mock.Mock(spec=_FakeEndpoint, target=messaging.Target(**e)) for e in self.endpoints] serializer = None target = messaging.Target() dispatcher = messaging.RPCDispatcher(target, endpoints, serializer) def check_reply(reply=None, failure=None, log_failure=True): if self.ex and failure is not None: ex = failure[1] self.assertFalse(self.success, ex) self.assertIsNotNone(self.ex, ex) self.assertIsInstance(ex, self.ex, ex) if isinstance(ex, messaging.NoSuchMethod): self.assertEqual(self.msg.get('method'), ex.method) elif isinstance(ex, messaging.UnsupportedVersion): self.assertEqual(self.msg.get('version', '1.0'), ex.version) else: self.assertTrue(self.success, failure) self.assertIsNone(failure) incoming = mock.Mock(ctxt=self.ctxt, message=self.msg) incoming.reply.side_effect = check_reply with dispatcher(incoming) as callback: callback() for n, endpoint in enumerate(endpoints): for method_name in ['foo', 'bar']: method = getattr(endpoint, method_name) if self.dispatch_to and n == self.dispatch_to['endpoint'] and \ method_name == self.dispatch_to['method']: method.assert_called_once_with( self.ctxt, **self.msg.get('args', {})) else: self.assertEqual(0, method.call_count) self.assertEqual(1, incoming.reply.call_count)
def test_notification(self): driver = amqp_driver.ProtonDriver(self.conf, self._broker_url) notifications = [(messaging.Target(topic="topic-1"), 'info'), (messaging.Target(topic="topic-1"), 'error'), (messaging.Target(topic="topic-2"), 'debug')] nl = driver.listen_for_notifications(notifications, None) # send one for each support version: msg_count = len(notifications) * 2 listener = _ListenerThread(nl, msg_count) targets = [ 'topic-1.info', 'topic-1.bad', # will raise MessagingDeliveryFailure 'bad-topic.debug', # will raise MessagingDeliveryFailure 'topic-1.error', 'topic-2.debug' ] excepted_targets = [] exception_count = 0 for version in (1.0, 2.0): for t in targets: try: driver.send_notification(messaging.Target(topic=t), "context", {'target': t}, version) except messaging.MessageDeliveryFailure: exception_count += 1 excepted_targets.append(t) listener.join(timeout=30) self.assertFalse(listener.isAlive()) topics = [x.message.get('target') for x in listener.get_messages()] self.assertEqual(len(topics), msg_count) self.assertEqual(topics.count('topic-1.info'), 2) self.assertEqual(topics.count('topic-1.error'), 2) self.assertEqual(topics.count('topic-2.debug'), 2) self.assertEqual(self._broker.dropped_count, 4) self.assertEqual(exception_count, 4) self.assertEqual(excepted_targets.count('topic-1.bad'), 2) self.assertEqual(excepted_targets.count('bad-topic.debug'), 2) driver.cleanup()
def test_zmqdriver_send(self, mock_multi_send, mock_cast): context = mock.Mock(autospec=impl_zmq.RpcContext) topic = 'testtopic' msg = 'jeronimo' self.driver.send(messaging.Target(topic=topic), context, msg, False, 0, False) mock_multi_send.assert_called_with(mock_cast, context, topic, msg, allowed_remote_exmods=[], envelope=False)
def __init__(self, topic): self.topic = topic self.topic_network_delete = topics.get_topic_name( topic, topics.NETWORK, topics.DELETE) self.topic_port_update = topics.get_topic_name(topic, topics.PORT, topics.UPDATE) self.topic_port_delete = topics.get_topic_name(topic, topics.PORT, topics.DELETE) self.topic_tunnel_update = topics.get_topic_name( topic, constants.TUNNEL, topics.UPDATE) target = messaging.Target(topic=topic, version='1.0') self.client = n_rpc.get_client(target)
def get_monitorlistner(): global _MONITOR_LISTENER if not _MONITOR_LISTENER: targets = [messaging.Target(topic=cfg.CONF.monitor.notifications)] endpoints = [ def_monitor.DefaultMonitorNotificationEndPoint('defualt_monitor') ] _MONITOR_LISTENER = messaging.get_notification_listener( get_transport(), targets, endpoints, executor='eventlet') return _MONITOR_LISTENER
class CiscoRouterPluginRpcCallbacks(l3_router_rpc.L3RouterCfgRpcCallbackMixin, devices_rpc.DeviceCfgRpcCallbackMixin): target = messaging.Target(version='1.1') def __init__(self, l3plugin): super(CiscoRouterPluginRpcCallbacks, self).__init__() self._l3plugin = l3plugin @property def _core_plugin(self): return manager.NeutronManager.get_plugin()
def _setup_listener(self, transport, endpoints, expect_messages, targets=None): listener = self.Listener(transport, targets=targets or [ messaging.Target(topic='testtopic')], expect_messages=expect_messages, endpoints=endpoints) thread = threading.Thread(target=listener.start) thread.daemon = True thread.start() return thread
def __init__(self, topic=None): super(ConductorAPI, self).__init__() self.topic = topic if self.topic is None: self.topic = manager.MANAGER_TOPIC target = messaging.Target(topic=self.topic, version='1.0') serializer = objects_base.IronicObjectSerializer() self.client = rpc.get_client(target, version_cap=self.RPC_API_VERSION, serializer=serializer) self.ring_manager = hash.HashRingManager()
def __init__(self, transport=None, context=None, topic=None): serializer = RequestContextSerializer(JsonPayloadSerializer()) if transport is None: transport = messaging.get_transport(cfg.CONF, aliases=TRANSPORT_ALIASES) self._context = context if topic is None: topic = '' target = messaging.Target(topic=topic) self._client = messaging.RPCClient(transport, target, serializer=serializer)
def __init__(self, transport): """Constructs an RPC client for engine. :param transport: Messaging transport. """ serializer = auth_ctx.RpcContextSerializer( auth_ctx.JsonPayloadSerializer()) self._client = messaging.RPCClient( transport, messaging.Target(topic=cfg.CONF.engine.topic), serializer=serializer)
class ServiceVMPluginRpcCallbacks(svm_rpc.DeviceSVMRpcCallbackMixin): target = messaging.Target(version='1.0') def __init__(self, servicevm_plugin, notifier): super(ServiceVMPluginRpcCallbacks, self).__init__() self._svm_plugin = servicevm_plugin self.notifier = notifier @property def _core_plugin(self): return manager.NeutronManager.get_plugin()
def test_broker_failover(self): """Simulate failover of one broker to another.""" self._brokers[0].start() driver = amqp_driver.ProtonDriver(self.conf, self._broker_url) target = messaging.Target(topic="my-topic") listener = _ListenerThread(driver.listen(target), 2) rc = driver.send(target, {"context": "whatever"}, { "method": "echo", "id": "echo-1" }, wait_for_reply=True, timeout=30) self.assertIsNotNone(rc) self.assertEqual(rc.get('correlation-id'), 'echo-1') # 1 request msg, 1 response: self.assertEqual(self._brokers[0].topic_count, 1) self.assertEqual(self._brokers[0].direct_count, 1) # fail broker 0 and start broker 1: self._brokers[0].stop() self._brokers[1].start() deadline = time.time() + 30 responded = False sequence = 2 while deadline > time.time() and not responded: if not listener.isAlive(): # listener may have exited after replying to an old correlation # id: restart new listener listener = _ListenerThread(driver.listen(target), 1) try: rc = driver.send(target, {"context": "whatever"}, { "method": "echo", "id": "echo-%d" % sequence }, wait_for_reply=True, timeout=2) self.assertIsNotNone(rc) self.assertEqual(rc.get('correlation-id'), 'echo-%d' % sequence) responded = True except messaging.MessagingTimeout: sequence += 1 self.assertTrue(responded) listener.join(timeout=30) self.assertFalse(listener.isAlive()) # note: stopping the broker first tests cleaning up driver without a # connection active self._brokers[1].stop() driver.cleanup()
def test_no_target_topic(self): transport = messaging.get_transport(self.conf, url='fake:') listener = messaging.get_notification_listener(transport, [messaging.Target()], [mock.Mock()]) try: listener.start() except Exception as ex: self.assertIsInstance(ex, messaging.InvalidTarget, ex) else: self.assertTrue(False)
def send(self, msg, target): msg_ctxt = {} targ = messaging.Target(topic=target, server='server1', version='1.0') result = self._transport._send( targ, msg_ctxt, msg, wait_for_reply=True, timeout=self.timeout, ) return result
def notify(self, ctxt, message, priority): priority = priority.lower() for topic in self.topics: target = messaging.Target(topic='%s.%s' % (topic, priority)) try: self.transport._send_notification(target, ctxt, message, version=self.version) except Exception: LOG.exception( "Could not send notification to %(topic)s. " "Payload=%(message)s", dict(topic=topic, message=message))
def test_call_serializer(self): self.config(rpc_response_timeout=None) transport = _FakeTransport(self.conf) serializer = msg_serializer.NoOpSerializer() client = messaging.RPCClient(transport, messaging.Target(), serializer=serializer) self.mox.StubOutWithMock(transport, '_send') msg = dict(method='foo', args=dict([(k, 's' + v) for k, v in self.args.items()])) kwargs = dict(wait_for_reply=True, timeout=None) if self.call else {} kwargs['retry'] = None transport._send(messaging.Target(), dict(user='******'), msg, **kwargs).AndReturn(self.retval) self.mox.StubOutWithMock(serializer, 'serialize_entity') self.mox.StubOutWithMock(serializer, 'deserialize_entity') self.mox.StubOutWithMock(serializer, 'serialize_context') for arg in self.args: serializer.serialize_entity(self.ctxt, arg).AndReturn('s' + arg) if self.call: serializer.deserialize_entity(self.ctxt, self.retval).\ AndReturn('d' + self.retval) serializer.serialize_context(self.ctxt).AndReturn(dict(user='******')) self.mox.ReplayAll() method = client.call if self.call else client.cast retval = method(self.ctxt, 'foo', **self.args) if self.retval is not None: self.assertEqual('d' + self.retval, retval)
def main(): config.parse_args(sys.argv) logging.setup('quaker') messaging.set_transport_defaults('payload') transport = messaging.get_transport(cfg.CONF) targets = [messaging.Target(topic='notifications')] endpoints = [ NotificationEndpoint(), ] server = messaging.get_notification_listener(transport, targets, endpoints) server.start() server.wait()
def _get_targets(self): """ Set's up subscriptions for the various exchange+topic combinations that we have a handler for. """ targets = [] for handler in self.handlers: exchange, topics = handler.get_exchange_topics() for topic in topics: target = messaging.Target(exchange=exchange, topic=topic) targets.append(target) return targets
def test_no_target_server(self): transport = messaging.get_transport(self.conf, url='fake:') server = messaging.get_rpc_server(transport, messaging.Target(topic='testtopic'), []) try: server.start() except Exception as ex: self.assertIsInstance(ex, messaging.InvalidTarget, ex) self.assertEqual(ex.target.topic, 'testtopic') else: self.assertTrue(False)
class TestTransportMethodArgs(test_utils.BaseTestCase): _target = messaging.Target(topic='topic', server='server') def test_send_defaults(self): t = transport.Transport(_FakeDriver(cfg.CONF)) self.mox.StubOutWithMock(t._driver, 'send') t._driver.send(self._target, 'ctxt', 'message', wait_for_reply=None, timeout=None) self.mox.ReplayAll() t._send(self._target, 'ctxt', 'message') def test_send_all_args(self): t = transport.Transport(_FakeDriver(cfg.CONF)) self.mox.StubOutWithMock(t._driver, 'send') t._driver.send(self._target, 'ctxt', 'message', wait_for_reply='wait_for_reply', timeout='timeout') self.mox.ReplayAll() t._send(self._target, 'ctxt', 'message', wait_for_reply='wait_for_reply', timeout='timeout') def test_send_notification(self): t = transport.Transport(_FakeDriver(cfg.CONF)) self.mox.StubOutWithMock(t._driver, 'send_notification') t._driver.send_notification(self._target, 'ctxt', 'message', 1.0) self.mox.ReplayAll() t._send_notification(self._target, 'ctxt', 'message', version=1.0) def test_listen(self): t = transport.Transport(_FakeDriver(cfg.CONF)) self.mox.StubOutWithMock(t._driver, 'listen') t._driver.listen(self._target) self.mox.ReplayAll() t._listen(self._target)
def test_cast_to_target(self): target = messaging.Target(**self.ctor) expect_target = messaging.Target(**self.expect) transport = _FakeTransport(self.conf) client = messaging.RPCClient(transport, target) self.mox.StubOutWithMock(transport, '_send') msg = dict(method='foo', args={}) if 'namespace' in self.expect: msg['namespace'] = self.expect['namespace'] if 'version' in self.expect: msg['version'] = self.expect['version'] transport._send(expect_target, {}, msg, retry=None) self.mox.ReplayAll() if self.prepare: client = client.prepare(**self.prepare) if self.double_prepare: client = client.prepare(**self.prepare) client.cast({}, 'foo')
def test_request_wire_format(self): if hasattr(self, 'skip_msg'): self.skipTest(self.skip_msg) transport = messaging.get_transport(self.conf, 'kombu+memory:////') self.addCleanup(transport.cleanup) driver = transport._driver target = messaging.Target(topic=self.topic, server=self.server, fanout=self.fanout) connection, channel, queue = _declare_queue(target) self.addCleanup(connection.release) driver.send(target, self.ctxt, self.msg) msgs = [] def callback(msg): msg = channel.message_to_python(msg) msg.ack() msgs.append(msg.payload) queue.consume(callback=callback, consumer_tag='1', nowait=False) connection.drain_events() self.assertEqual(1, len(msgs)) self.assertIn('oslo.message', msgs[0]) received = msgs[0] received['oslo.message'] = jsonutils.loads(received['oslo.message']) # FIXME(markmc): add _msg_id and _reply_q check expected_msg = { '_unique_id': self.uuids[0].hex, } expected_msg.update(self.expected) expected_msg.update(self.expected_ctxt) expected = { 'oslo.version': '2.0', 'oslo.message': expected_msg, } self.assertEqual(expected, received)