def test_custom_annotations_match(self): """ The linkRoute on Routers C and B is set to org.apache. Creates a receiver listening on the address 'org.apache' and a sender that sends to address 'org.apache'. Sends a message with custom annotations to org.apache via router QDR.C and makes sure that the message was successfully routed (using full address matching) and received using pre-created links that were created as a result of specifying addresses in the linkRoute attribute('org.apache.'). Make sure custom annotations arrived as well. """ hello_world_3 = "Hello World_3!" # Connects to listener #2 on QDR.C addr = self.routers[2].addresses[0] blocking_connection = BlockingConnection(addr) # Receive on org.apache blocking_receiver = blocking_connection.create_receiver(address="org.apache") apply_options = AtMostOnce() # Sender to to org.apache blocking_sender = blocking_connection.create_sender(address="org.apache", options=apply_options) msg = Message(body=hello_world_3) annotations = {'custom-annotation': '1/Custom_Annotation'} msg.annotations = annotations # Send a message blocking_sender.send(msg) received_message = blocking_receiver.receive() self.assertEqual(hello_world_3, received_message.body) self.assertEqual(received_message.annotations, annotations) blocking_connection.close()
def is_proto_allowed(self, listener_port, tls_protocol): """ Opens a simple proton client connection to the provided TCP port using a specific TLS protocol version and returns True in case connection was established and accepted or False otherwise. :param listener_port: TCP port number :param tls_protocol: TLSv1, TLSv1.1 or TLSv1.2 (string) :return: """ # Management address to connect using the given TLS protocol url = Url("amqps://0.0.0.0:%d/$management" % listener_port) # Preparing SSLDomain (client cert) and SASL authentication info domain = SSLDomain(SSLDomain.MODE_CLIENT) # Enforcing given TLS protocol cproton.pn_ssl_domain_set_protocols(domain._domain, tls_protocol) # Try opening the secure and authenticated connection try: connection = BlockingConnection(url, sasl_enabled=False, ssl_domain=domain, timeout=self.TIMEOUT) except proton.Timeout: return False except proton.ConnectionException: return False except: return False # TLS version provided was accepted connection.close() return True
def run(self): try: ssl = SSLDomain(SSLDomain.MODE_CLIENT) ssl.set_credentials(str(self.options.accountPublicKey), str(self.options.accountPrivateKey), str("")) ssl.set_trusted_ca_db(str(self.options.brokerPublicKey)) ssl.set_peer_authentication(SSLDomain.VERIFY_PEER_NAME, trusted_CAs=str(self.options.brokerPublicKey)) connection = BlockingConnection(self.address, ssl_domain=ssl, heartbeat=60000) receiver = connection.create_receiver(self.broadcast_address, credit=self.capacity) while True: received_message = None try: received_message = receiver.receive(timeout=self.options.timeout) except Timeout, e: print("-I- No message received for ", self.options.timeout, " seconds") break self.message_counter += 1 print("-I- Received broadcast message: " + received_message.body) receiver.accept() print("-I- " + str(self.message_counter) + " messages received") connection.close()
def test_message_user_id_proxy_bad_name_disallowed(self): ssl_opts = dict() ssl_opts['ssl-trustfile'] = self.ssl_file('ca-certificate.pem') ssl_opts['ssl-certificate'] = self.ssl_file('client-certificate.pem') ssl_opts['ssl-key'] = self.ssl_file('client-private-key.pem') ssl_opts['ssl-password'] = '******' # create the SSL domain object domain = self.create_ssl_domain(ssl_opts) # Send a message with bad user_id. This message should be rejected. # Connection has user_id 'user13'. addr = self.address(13).replace("amqp", "amqps") blocking_connection = BlockingConnection(addr, ssl_domain=domain) blocking_sender = blocking_connection.create_sender("$management") request = proton.Message() request.user_id = u"bad-user-id" result = Delivery.ACCEPTED try: delivery = blocking_sender.send(request, timeout=10) result = delivery.remote_state except proton.utils.SendException as e: result = e.state self.assertTrue (result == Delivery.REJECTED, "Router accepted a message with user_id that did not match connection user_id")
def test_verify_n_senders(self): n = 2 addr = self.address() # connection should be ok denied = False try: bs1 = BlockingConnection(addr) except ConnectionException: denied = True self.assertFalse(denied) # assert if connections that should open did not open # n senders OK try: s1 = bs1.create_sender(address="****YES_1of2****") s2 = bs1.create_sender(address="****YES_2of2****") except Exception: denied = True self.assertFalse(denied) # n senders should have worked # receiver n+1 should be denied try: s3 = bs1.create_sender("****NO****") except Exception: denied = True self.assertTrue(denied) # sender n+1 should have failed bs1.close()
def is_ssl_sasl_client_accepted(self, listener_port, tls_protocol): """ Attempts to connect a proton client to the management address on the given listener_port using the specific tls_protocol provided. If connection was established and accepted, returns True and False otherwise. :param listener_port: :param tls_protocol: :return: """ # Management address to connect using the given TLS protocol url = Url("amqps://0.0.0.0:%d/$management" % listener_port) # Preparing SSLDomain (client cert) and SASL authentication info domain = SSLDomain(SSLDomain.MODE_CLIENT) domain.set_credentials(self.ssl_file('client-certificate.pem'), self.ssl_file('client-private-key.pem'), 'client-password') # Enforcing given TLS protocol cproton.pn_ssl_domain_set_protocols(domain._domain, tls_protocol) # Try opening the secure and authenticated connection try: connection = BlockingConnection(url, sasl_enabled=True, ssl_domain=domain, allowed_mechs='PLAIN', user='******', password='******') except proton.ConnectionException: return False # TLS version provided was accepted connection.close() return True
def test_message_user_id_proxy_zzz_credit_handled(self): # Test for DISPATCH-519. Make sure the REJECTED messages result # in the client receiving credit. credit_limit = 250 # router issues 250 credits ssl_opts = dict() ssl_opts['ssl-trustfile'] = self.ssl_file('ca-certificate.pem') ssl_opts['ssl-certificate'] = self.ssl_file('client-certificate.pem') ssl_opts['ssl-key'] = self.ssl_file('client-private-key.pem') ssl_opts['ssl-password'] = '******' # create the SSL domain object domain = self.create_ssl_domain(ssl_opts) # Send a message with bad user_id. This message should be rejected. # Connection has user_id 'user13'. addr = self.address(13).replace("amqp", "amqps") blocking_connection = BlockingConnection(addr, ssl_domain=domain) blocking_sender = blocking_connection.create_sender("$management") request = proton.Message() request.user_id = u"bad-user-id" for i in range(0, credit_limit+1): result = Delivery.ACCEPTED try: delivery = blocking_sender.send(request, timeout=10) result = delivery.remote_state except proton.utils.SendException as e: result = e.state except proton.utils.Timeout as e: self.assertTrue(False, "Timed out waiting for send credit") self.assertTrue(result == Delivery.REJECTED, "Router accepted a message with user_id that did not match connection user_id")
def connect(self, host, port=5672, user=None, passw=None, timeout=10): """Method connects to server Args: host (str): hostname port (str): port user (str): username passw (str): password timeout (int): timeout Returns: bool: result Raises: event: jms_before_connect event: jms_after_connected """ try: msg = 'host:{0}, port:{1}, user:{2}, passw:{3}, timeout:{4}'.format( host, port, user, passw, timeout) self._mh.demsg('htk_on_debug_info', self._mh._trn.msg( 'htk_jms_connecting', msg), self._mh.fromhere()) ev = event.Event( 'jms_before_connect', host, port, user, passw, timeout) if (self._mh.fire_event(ev) > 0): host = ev.argv(0) port = ev.argv(1) user = ev.argv(2) passw = ev.argv(3) timeout = ev.argv(4) self._host = host self._port = port self._user = user self._passw = passw if (ev.will_run_default()): if (self._user == None): self._client = BlockingConnection( 'amqp://{0}:{1}'.format(self._host, self._port), timeout=timeout) else: self._client = BlockingConnection('amqp://{0}:{1}@{2}:{3}'.format(self._user, self._passw, self._host, self._port), timeout=timeout) self._is_connected = True self._mh.demsg('htk_on_debug_info', self._mh._trn.msg( 'htk_jms_connected'), self._mh.fromhere()) ev = event.Event('jms_after_connect') self._mh.fire_event(ev) return True except ProtonException as ex: self._mh.demsg('htk_on_error', ex, self._mh.fromhere()) return False
def test_non_zero_timeout_sender_disallowed(self): addr = self.routers[1].addresses[0] connection = BlockingConnection(addr) try: sender = connection.create_sender(address="org.apache", options=[SenderTimeout(10)]) self.fail("link should have been detached") except LinkDetached: pass connection.close()
def test_expire_never_sender_disallowed(self): addr = self.routers[1].addresses[0] connection = BlockingConnection(addr) try: sender = connection.create_sender(address="org.apache", options=[SenderExpiry(Terminus.EXPIRE_NEVER)]) self.fail("link should have been detached") except LinkDetached: pass connection.close()
def test_resumable_receiver_disallowed(self): addr = self.routers[1].addresses[0] connection = BlockingConnection(addr) try: receiver = connection.create_receiver(address="org.apache", options=[DurableSubscription()]) self.fail("link should have been detached") except LinkDetached: pass connection.close()
def test_yy_query_many_links(self): # This test will fail without the fix for DISPATCH-974 c = BlockingConnection(self.address()) count = 0 links = [] COUNT = 5000 ADDRESS_SENDER = "examples-sender" ADDRESS_RECEIVER = "examples-receiver" # This loop creates 5000 consumer and 5000 producer links with # different addresses while True: count += 1 r = c.create_receiver(ADDRESS_RECEIVER + str(count)) links.append(r) s = c.create_sender(ADDRESS_SENDER + str(count)) links.append(c) if count == COUNT: break # Try fetching all 10,000 addresses # This qdmanage query command would fail without the fix # for DISPATCH-974 query_command = 'QUERY --type=org.apache.qpid.dispatch.router.address' outs = json.loads(self.run_qdmanage(query_command)) sender_addresses = 0 receiver_addresses = 0 for out in outs: if ADDRESS_SENDER in out['name']: sender_addresses += 1 if ADDRESS_RECEIVER in out['name']: receiver_addresses += 1 self.assertEqual(sender_addresses, COUNT) self.assertEqual(receiver_addresses, COUNT) query_command = 'QUERY --type=link' outs = json.loads(self.run_qdmanage(query_command)) out_links = 0 in_links = 0 for out in outs: if out.get('owningAddr'): if ADDRESS_SENDER in out['owningAddr']: in_links += 1 if ADDRESS_RECEIVER in out['owningAddr']: out_links += 1 self.assertEqual(out_links, COUNT) self.assertEqual(in_links, COUNT)
def test_forwarding_sync(self): """ Forward unsettled messages to multiple subscribers """ config = [ ('router', {'mode': 'standalone', 'id': 'QDR.mcast'}), ('listener', {'role': 'normal', 'host': '0.0.0.0', 'port': self.tester.get_port(), 'saslMechanisms':'ANONYMOUS'}), ('address', {'pattern': 'nextHop2/#', 'distribution': 'multicast'}), ('exchange', {'address': 'Address3', 'name': 'Exchange1', 'alternateAddress': 'altNextHop'}), ('binding', {'name': 'binding1', 'exchangeName': 'Exchange1', 'bindingKey': 'a.b', 'nextHopAddress': 'nextHop1'}), ('binding', {'name': 'binding2', 'exchangeName': 'Exchange1', 'bindingKey': '*.b', 'nextHopAddress': 'nextHop2'}) ] router = self.tester.qdrouterd('QDR.mcast', Qdrouterd.Config(config)) # create clients for message transfer conn = BlockingConnection(router.addresses[0]) sender = conn.create_sender(address="Address3", options=AtLeastOnce()) nhop1 = AsyncTestReceiver(address=router.addresses[0], source="nextHop1") nhop2A = AsyncTestReceiver(address=router.addresses[0], source="nextHop2") nhop2B = AsyncTestReceiver(address=router.addresses[0], source="nextHop2") alt = AsyncTestReceiver(address=router.addresses[0], source="altNextHop") sender.send(Message(subject='a.b', body='A')) sender.send(Message(subject='x.y', body='B')) self.assertEqual('A', nhop1.queue.get(timeout=TIMEOUT).body) self.assertEqual('A', nhop2A.queue.get(timeout=TIMEOUT).body) self.assertEqual('A', nhop2B.queue.get(timeout=TIMEOUT).body) self.assertEqual('B', alt.queue.get(timeout=TIMEOUT).body) nhop1.stop() nhop2A.stop() nhop2B.stop() alt.stop() conn.close() self.assertTrue(nhop1.queue.empty()) self.assertTrue(nhop2A.queue.empty()) self.assertTrue(nhop2B.queue.empty()) self.assertTrue(alt.queue.empty())
def test_max_frame_max_session_too_big(self): # Set up a connection to get the Open and a receiver to get a Begin frame in the log bc = BlockingConnection(self.router.addresses[0]) bc.create_receiver("xxx") bc.close() with open('../setUpClass/MaxFrameMaxSessionFramesTooBig.log', 'r') as router_log: log_lines = router_log.read().split("\n") open_lines = [s for s in log_lines if "-> @open" in s] # max-frame is from the config self.assertTrue(' max-frame-size=1000000,' in open_lines[0]) begin_lines = [s for s in log_lines if "-> @begin" in s] # incoming-window is truncated self.assertTrue(" incoming-window=2147," in begin_lines[0]) warning_lines = [s for s in log_lines if "requested maxSessionFrames truncated from 5000000 to 2147" in s] self.assertTrue(len(warning_lines) == 1)
def test_max_session_frames_default(self): # Set up a connection to get the Open and a receiver to get a Begin frame in the log if skip_test: return self.skipTest("Test skipped on non-64 bit architectures") bc = BlockingConnection(self.router.addresses[0]) bc.create_receiver("xxx") bc.close() with open('../setUpClass/MaxSessionFramesDefault.log', 'r') as router_log: log_lines = router_log.read().split("\n") open_lines = [s for s in log_lines if "-> @open" in s] # if frame size not set then a default is used self.assertTrue(" max-frame-size=16384" in open_lines[0]) begin_lines = [s for s in log_lines if "-> @begin" in s] # incoming-window is from the config self.assertTrue(" incoming-window=2147483647," in begin_lines[0])
def test_large_messages(self): """ Verify that multi-frame messages are forwarded properly """ MAX_FRAME=1024 config = [ ('router', {'mode': 'interior', 'id': 'QDR.X', 'allowUnsettledMulticast': 'yes'}), ('listener', {'port': self.tester.get_port(), 'stripAnnotations': 'no', 'maxFrameSize': MAX_FRAME}), ('address', {'pattern': 'nextHop1/#', 'distribution': 'multicast'}), ('exchange', {'address': 'AddressA', 'name': 'ExchangeA'}), ('binding', {'name': 'bindingA1', 'exchangeName': 'ExchangeA', 'bindingKey': 'a/b', 'nextHopAddress': 'nextHop1'}) ] router = self.tester.qdrouterd('QDR.X', Qdrouterd.Config(config), wait=True) # connect clients to router B (no exchange) nhop1A = AsyncTestReceiver(router.addresses[0], 'nextHop1', conn_args={'max_frame_size': MAX_FRAME}) nhop1B = AsyncTestReceiver(router.addresses[0], 'nextHop1', conn_args={'max_frame_size': MAX_FRAME}) conn = BlockingConnection(router.addresses[0], max_frame_size=MAX_FRAME) sender = conn.create_sender(address="AddressA") jumbo = (10 * MAX_FRAME) * 'X' sender.send(Message(subject='a/b', body=jumbo)) # multicast self.assertEqual(jumbo, nhop1A.queue.get(timeout=TIMEOUT).body) self.assertEqual(jumbo, nhop1B.queue.get(timeout=TIMEOUT).body) nhop1A.stop() nhop1B.stop() conn.close()
def test_verify_n_receivers(self): n = 4 addr = self.address() # connection should be ok denied = False try: br1 = BlockingConnection(addr) except ConnectionException: denied = True self.assertFalse(denied) # assert if connections that should open did not open # n receivers OK try: r1 = br1.create_receiver(address="****YES_1of4***") r2 = br1.create_receiver(address="****YES_20f4****") r3 = br1.create_receiver(address="****YES_3of4****") r4 = br1.create_receiver(address="****YES_4of4****") except Exception: denied = True self.assertFalse(denied) # n receivers should have worked # receiver n+1 should be denied try: r5 = br1.create_receiver("****NO****") except Exception: denied = True self.assertTrue(denied) # receiver n+1 should have failed br1.close()
def test_full_link_route_match(self): """ The linkRoutePattern on Routers C and B is set to org.apache. Creates a receiver listening on the address 'org.apache' and a sender that sends to address 'org.apache'. Sends a message to org.apache via router QDR.C and makes sure that the message was successfully routed (using full address matching) and received using pre-created links that were created as a result of specifying addresses in the linkRoutePattern attribute('org.apache.'). """ hello_world_3 = "Hello World_3!" # Connects to listener #2 on QDR.C addr = self.routers[2].addresses[1] blocking_connection = BlockingConnection(addr) # Receive on org.apache blocking_receiver = blocking_connection.create_receiver(address="org.apache") apply_options = AtMostOnce() # Sender to to org.apache blocking_sender = blocking_connection.create_sender(address="org.apache", options=apply_options) msg = Message(body=hello_world_3) # Send a message blocking_sender.send(msg) received_message = blocking_receiver.receive() self.assertEqual(hello_world_3, received_message.body) local_node = Node.connect(self.routers[0].addresses[0], timeout=TIMEOUT) # Make sure that the router node acting as the broker (QDR.A) had one message routed through it. This confirms # that the message was link routed self.assertEqual(1, local_node.read(type='org.apache.qpid.dispatch.router.address', name='router.address/M0org.apache').deliveriesEgress, "deliveriesEgress is wrong") self.assertEqual(1, local_node.read(type='org.apache.qpid.dispatch.router.address', name='router.address/M0org.apache').deliveriesIngress, "deliveriesIngress is wrong") #blocking_receiver.close() blocking_connection.close()
def test_full_link_route_match_1(self): """ This test is pretty much the same as the previous test (test_full_link_route_match) but the connection is made to router QDR.B instead of QDR.C and we expect the message to be link routed successfully. """ hello_world_4 = "Hello World_4!" addr = self.routers[2].addresses[0] blocking_connection = BlockingConnection(addr) # Receive on org.apache blocking_receiver = blocking_connection.create_receiver(address="org.apache") apply_options = AtMostOnce() # Sender to to org.apache blocking_sender = blocking_connection.create_sender(address="org.apache", options=apply_options) msg = Message(body=hello_world_4) # Send a message blocking_sender.send(msg) received_message = blocking_receiver.receive() self.assertEqual(hello_world_4, received_message.body) local_node = Node.connect(self.routers[0].addresses[0], timeout=TIMEOUT) # Make sure that the router node acting as the broker (QDR.A) had one message routed through it. This confirms # that the message was link routed self.assertEqual(1, local_node.read(type='org.apache.qpid.dispatch.router.address', name='M0org.apache').deliveriesEgress, "deliveriesEgress is wrong") self.assertEqual(1, local_node.read(type='org.apache.qpid.dispatch.router.address', name='M0org.apache').deliveriesIngress, "deliveriesIngress is wrong") blocking_connection.close()
def test_connection_properties(self): connection = BlockingConnection(self.router.addresses[0], timeout=60, properties=CONNECTION_PROPERTIES) client = SyncRequestResponse(connection) node = Node.connect(self.router.addresses[0]) results = node.query(type='org.apache.qpid.dispatch.connection', attribute_names=[u'properties']).results self.assertEqual(results[0][0][u'connection'], u'properties') self.assertEqual(results[0][0][u'int_property'], 6451) client.connection.close()
def run(self): try: ssl = SSLDomain(SSLDomain.MODE_CLIENT) ssl.set_credentials(str(self.options.accountPublicKey), str(self.options.accountPrivateKey), str("")) ssl.set_trusted_ca_db(str(self.options.brokerPublicKey)) ssl.set_peer_authentication(SSLDomain.VERIFY_PEER_NAME, trusted_CAs=str(self.options.brokerPublicKey)) connection = BlockingConnection(self.address, ssl_domain=ssl, heartbeat=60000) receiver = connection.create_receiver(self.response_address) sender = connection.create_sender(self.request_address) message = Message(body="<FIXML>...</FIXML>", reply_to=self.reply_adress) print("-I- Sending request message: " + message.body) sender.send(message); try: received_message = receiver.receive(timeout=self.options.timeout) print("-I- Received response message: " + received_message.body) self.message_counter += 1 receiver.accept() except Timeout, e: print("-I- No message received for ", self.options.timeout, " seconds") connection.close()
def test_message_user_id_proxy_zzz_credit_handled(self): # Test for DISPATCH-519. Make sure the REJECTED messages result # in the client receiving credit. credit_limit = 250 # router issues 250 credits ssl_opts = dict() ssl_opts['ssl-trustfile'] = self.ssl_file('ca-certificate.pem') ssl_opts['ssl-certificate'] = self.ssl_file('client-certificate.pem') ssl_opts['ssl-key'] = self.ssl_file('client-private-key.pem') ssl_opts['ssl-password'] = '******' # create the SSL domain object domain = self.create_ssl_domain(ssl_opts) # Send a message with bad user_id. This message should be rejected. # Connection has user_id 'user13'. addr = self.address(13).replace("amqp", "amqps") blocking_connection = BlockingConnection(addr, ssl_domain=domain) blocking_sender = blocking_connection.create_sender("$management") request = proton.Message() request.user_id = u"bad-user-id" for i in range(0, credit_limit + 1): result = Delivery.ACCEPTED try: delivery = blocking_sender.send(request, timeout=10) result = delivery.remote_state except proton.utils.SendException as e: result = e.state except proton.utils.Timeout as e: self.assertTrue(False, "Timed out waiting for send credit") self.assertTrue( result == Delivery.REJECTED, "Router accepted a message with user_id that did not match connection user_id" )
def open(self): """ Open a connection to the broker. """ if self.is_open(): # already open return connector = Connector.find(self.url) domain = self.ssl_domain(connector) log.info('open: %s', connector) self._impl = BlockingConnection( connector.url.canonical, heartbeat=connector.heartbeat, ssl_domain=domain) log.info('opened: %s', self.url)
def test_sync_request_response_blocking_connection_no_object_leaks(self): with test_broker() as tb: sockname = tb.get_acceptor_sockname() url = "{0}:{1}".format(*sockname) opts = namedtuple('Opts', ['address', 'timeout'])(address=url, timeout=3) with no_fd_leaks(self): client = SyncRequestResponse( BlockingConnection(url, opts.timeout, allowed_mechs="ANONYMOUS"), "somequeue") try: request = "One Two Three Four" response = client.call(Message(body=request)) finally: client.connection.close() gc.collect()
def connection(url=None, router=None, timeout=10, ssl_domain=None, sasl=None, edge_router=None): """Return a BlockingConnection suitable for connecting to a management node """ if ssl_domain: sasl_enabled = True else: sasl_enabled = True if sasl else False # if sasl_mechanism is unicode, convert it to python string return BlockingConnection(url, timeout=timeout, ssl_domain=ssl_domain, sasl_enabled=sasl_enabled, allowed_mechs=str(sasl.mechs) if sasl and sasl.mechs is not None else None, user=str(sasl.user) if sasl and sasl.user is not None else None, password=str(sasl.password) if sasl and sasl.password is not None else None)
def test_max_sessions(self): # Set up a connection to get the Open and a receiver to get a Begin frame in the log bc = BlockingConnection(self.router.addresses[0]) bc.create_receiver("xxx") bc.close() with open('../setUpClass/MaxSessions.log', 'r') as router_log: log_lines = router_log.read().split("\n") open_lines = [s for s in log_lines if "-> @open" in s] # channel-max is 10 self.assertTrue(" channel-max=9" in open_lines[0])
def test_max_sessions_large(self): # Set up a connection to get the Open and a receiver to get a Begin frame in the log bc = BlockingConnection(self.router.addresses[0]) bc.create_receiver("xxx") bc.close() with open('../setUpClass/MaxSessionsLarge.log', 'r') as router_log: log_lines = router_log.read().split("\n") open_lines = [s for s in log_lines if "-> @open" in s] # channel-max is 0. Should get proton default 32767 self.assertIn(" channel-max=32767", open_lines[0])
def test_max_frame_default(self): # Set up a connection to get the Open and a receiver to get a Begin frame in the log bc = BlockingConnection(self.router.addresses[0]) bc.create_receiver("xxx") bc.close() with open('../setUpClass/MaxFrameDefault.log', 'r') as router_log: log_lines = router_log.read().split("\n") open_lines = [s for s in log_lines if "-> @open" in s] # if frame size not set then a default is used self.assertTrue(" max-frame-size=16384" in open_lines[0])
def test_max_frame_small(self): # Set up a connection to get the Open and a receiver to get a Begin frame in the log bc = BlockingConnection(self.router.addresses[0]) bc.create_receiver("xxx") bc.close() with open('../setUpClass/MaxFrameSmall.log', 'r') as router_log: log_lines = router_log.read().split("\n") open_lines = [s for s in log_lines if "-> @open" in s] # if frame size <= 512 proton set min of 512 self.assertTrue(" max-frame-size=512" in open_lines[0])
def get_metrics(self): client = SyncRequestResponse(BlockingConnection("127.0.0.1:5672", 30), "$management") try: properties = {} properties["entityType"] = "org.apache.qpid.dispatch.connection" properties["operation"] = "QUERY" properties["name"] = "self" message = Message(body=None, properties=properties) response = client.call(message) return response except: e = sys.exc_info()[0] print("Error querying router for metrics: %s" % e) return None finally: client.connection.close()
def ecp_publish(msg, queue=CIM_ECP_PUBLISH, recipient=CIM_ECP_RECIPIENT, label=CIM_ECP_LABEL, sender=CIM_ECP_SENDER, msgid=None, msgctx=None, session=None): log.debug('ENTER: ecp_publish(msg=#' + str(len(msg)) + ',queue=' + json.dumps(queue) + ',recipient=' + json.dumps(recipient) + ',label=' + json.dumps(label) + ',sender=' + json.dumps(sender) + ',msgid=' + json.dumps(msgid) + ',msgctx=' + json.dumps(msgctx) + ',session=' + json.dumps(session) + ')') result = {} CONNECTION = None CHANNEL = None try: CONNECTION = BlockingConnection(URL, timeout=25) CHANNEL = CONNECTION.create_sender(address=queue) correlation_id = str(uuid.uuid4()) result = { 'correlation_id': correlation_id, 'queue': queue, 'send': "%sZ" % datetime.now().isoformat() } props = { 'baMessageId': str(uuid.uuid4()), 'baCorrelationId': correlation_id, # prefer to use the same AMQP message correlation id as the AMQP application-property "baCorrelationId" 'businessType': CIM_ECP_LABEL, 'receiverCode': CIM_ECP_RECIPIENT, 'senderApplication': CIM_ECP_SENDER } for k,v in props.items(): result[k] = v # channel.basic_publish( CHANNEL.send( Message( body = msg, correlation_id = correlation_id, properties = props, creation_time = int(time.time()), durable = True) , timeout = False) result['published'] = "%sZ" % datetime.now().isoformat() # print('PUBLISHED: ' + str(result)) CHANNEL.close() CONNECTION.close() except Exception as e: print('EXCEPTION -- ecp_publish') print(e) log.error(e) result['error'] = str(e) if CHANNEL is not None: CHANNEL.close() if CONNECTION is not None: CONNECTION.close() # print('RETURNING: ' + str(result)) return result
def test_verify_n_senders(self): n = 2 addr = self.address() bs1 = BlockingConnection(addr) # n senders OK bs1.create_sender(address="****YES_1of2****") bs1.create_sender(address="****YES_2of2****") # sender n+1 should be denied self.assertRaises(LinkDetached, bs1.create_sender, "****NO****") bs1.close()
def test_aaa_partial_link_route_match(self): """ The linkRoutePattern on Routers C and B is set to org.apache. Creates a receiver listening on the address 'org.apache.dev' and a sender that sends to address 'org.apache.dev'. Sends a message to org.apache.dev via router QDR.C and makes sure that the message was successfully routed (using partial address matching) and received using pre-created links that were created as a result of specifying addresses in the linkRoutePattern attribute('org.apache.'). """ hello_world_1 = "Hello World_1!" # Connects to listener #2 on QDR.C addr = self.routers[2].addresses[1] blocking_connection = BlockingConnection(addr) # Receive on org.apache.dev blocking_receiver = blocking_connection.create_receiver(address="org.apache.dev") apply_options = AtMostOnce() # Sender to to org.apache.dev blocking_sender = blocking_connection.create_sender(address="org.apache.dev", options=apply_options) msg = Message(body=hello_world_1) # Send a message blocking_sender.send(msg) received_message = blocking_receiver.receive() self.assertEqual(hello_world_1, received_message.body) # Connect to the router acting like the broker (QDR.A) and check the deliveriesIngress and deliveriesEgress local_node = Node.connect(self.routers[0].addresses[0], timeout=TIMEOUT) self.assertEqual(u'QDR.A', local_node.query(type='org.apache.qpid.dispatch.router', attribute_names=['routerId']).results[0][0]) self.assertEqual(1, local_node.read(type='org.apache.qpid.dispatch.router.address', name='router.address/M0org.apache.dev').deliveriesEgress, "deliveriesEgress is wrong") self.assertEqual(1, local_node.read(type='org.apache.qpid.dispatch.router.address', name='router.address/M0org.apache.dev').deliveriesIngress, "deliveriesIngress is wrong") # There should be 4 links - # 1. outbound receiver link on org.apache.dev # 2. inbound sender link on blocking_sender # 3. inbound link to the $management # 4. outbound link to $management # self.assertEqual(4, len() self.assertEquals(4, len(local_node.query(type='org.apache.qpid.dispatch.router.link').results)) #blocking_receiver.close() blocking_connection.close()
def test_connection_properties(self): server = ConnPropertiesServer(Url(host="127.0.0.1", port=free_tcp_port()), timeout=self.timeout) server.start() server.wait() connection = BlockingConnection( server.url, timeout=self.timeout, properties=CONNECTION_PROPERTIES, offered_capabilities=OFFERED_CAPABILITIES, desired_capabilities=DESIRED_CAPABILITIES) client = SyncRequestResponse(connection) client.connection.close() server.join(timeout=self.timeout) self.assertEquals(server.properties_received, True) self.assertEquals(server.offered_capabilities_received, True) self.assertEquals(server.desired_capabilities_received, True)
def test_verify_maximum_connections(self): addr = self.address() # two connections should be ok denied = False try: bc1 = BlockingConnection(addr) bc2 = BlockingConnection(addr) except ConnectionException: denied = True self.assertFalse(denied) # assert if connections that should open did not open # third connection should be denied denied = False try: bc3 = BlockingConnection(addr) except ConnectionException: denied = True self.assertTrue(denied) # assert if connection that should not open did open bc1.close() bc2.close()
def test_forwarding_fanout(self): """ Verify bindings that do not have a key receive all messages """ config = [ ('exchange', {'address': 'AddressF', 'name': 'ExchangeF'}), ('binding', {'name': 'binding1', 'exchangeName': 'ExchangeF', 'bindingKey': 'pattern', 'nextHopAddress': 'nextHop1'}), # two bindings w/o key ('binding', {'name': 'binding2', 'exchangeName': 'ExchangeF', 'nextHopAddress': 'nextHop2'}), ('binding', {'name': 'binding3', 'exchangeName': 'ExchangeF', 'nextHopAddress': 'nextHop3'}) ] for meth in ['amqp', 'mqtt']: config[0][1]['matchMethod'] = meth router = self._create_router('A', config) # create clients for message transfer conn = BlockingConnection(router.addresses[0]) sender = conn.create_sender(address="AddressF", options=AtMostOnce()) nhop1 = conn.create_receiver(address="nextHop1", credit=100) nhop2 = conn.create_receiver(address="nextHop2", credit=100) nhop3 = conn.create_receiver(address="nextHop3", credit=100) # send message with subject "nope" # should arrive at nextHop2 & 3 only sender.send(Message(subject='nope', body='A')) self.assertEqual('A', nhop2.receive(timeout=TIMEOUT).body) self.assertEqual('A', nhop3.receive(timeout=TIMEOUT).body) # send message with subject "pattern" # forwarded to all bindings: sender.send(Message(subject='pattern', body='B')) self.assertEqual('B', nhop1.receive(timeout=TIMEOUT).body) self.assertEqual('B', nhop2.receive(timeout=TIMEOUT).body) self.assertEqual('B', nhop3.receive(timeout=TIMEOUT).body) conn.close() router.teardown()
def test_request_response(self): ensureCanTestExtendedSASL() def test(name, address="x"): for i in range(5): body="%s%s" % (name, i) response = client.call(Message(address=address, body=body)) self.assertEquals(response.address, client.reply_to) self.assertEquals(response.body, body) server = EchoServer(Url(host="127.0.0.1", port=free_tcp_port()), self.timeout) server.start() server.wait() connection = BlockingConnection(server.url, timeout=self.timeout) client = SyncRequestResponse(connection) try: test("foo") # Simple request/resposne finally: client.connection.close() server.join(timeout=self.timeout)
def test_max_frame_max_session_zero(self): # Set up a connection to get the Open and a receiver to get a Begin frame in the log bc = BlockingConnection(self.router.addresses[0]) bc.create_receiver("xxx") bc.close() with open('../setUpClass/MaxFrameMaxSessionFramesZero.log', 'r') as router_log: log_lines = router_log.read().split("\n") open_lines = [s for s in log_lines if "-> @open" in s] # max-frame gets set to protocol min self.assertTrue(' max-frame-size=512,' in open_lines[0]) begin_lines = [s for s in log_lines if "-> @begin" in s] # incoming-window is defaulted to 2^31-1 self.assertTrue(" incoming-window=2147483647," in begin_lines[0])
def connection(url=None, timeout=10, ssl_domain=None, sasl=None): """ Return a BlockingConnection for connection to a managemenet node """ url = Url(url) url.path = u'$management' if ssl_domain: sasl_enabled = True else: sasl_enabled = True if sasl else False return BlockingConnection(url, timeout=timeout, ssl_domain=ssl_domain, sasl_enabled=sasl_enabled, allowed_mechs=str(sasl.mechs) if sasl and sasl.mechs != None else None, user=str(sasl.user) if sasl else None, password=str(sasl.password) if sasl else None)
def test_allowed_mechs_external(self): # All this test does it make sure that if we pass allowed_mechs to BlockingConnection, it is actually used. port = free_tcp_port() server = ConnPropertiesServer(Url(host="127.0.0.1", port=port), timeout=self.timeout) server.start() server.wait() try: # This will cause an exception because we are specifying allowed_mechs as EXTERNAL. The SASL handshake will fail because the server is not setup to handle EXTERNAL connection = BlockingConnection( server.url, timeout=self.timeout, properties=CONNECTION_PROPERTIES, offered_capabilities=OFFERED_CAPABILITIES, desired_capabilities=DESIRED_CAPABILITIES, allowed_mechs=EXTERNAL) self.fail("Expected ConnectionException") except ConnectionException as e: self.assertTrue('amqp:unauthorized-access' in str(e), "expected unauthorized-access") server.join(timeout=self.timeout)
def test_allowed_mechs_anonymous(self): # All this test does it make sure that if we pass allowed_mechs to BlockingConnection, it is actually used. server = ConnPropertiesServer(Url(host="127.0.0.1", port=free_tcp_port()), timeout=self.timeout) server.start() server.wait() # An ANONYMOUS allowed_mechs will work, anonymous connections are allowed by ConnPropertiesServer connection = BlockingConnection( server.url, timeout=self.timeout, properties=CONNECTION_PROPERTIES, offered_capabilities=OFFERED_CAPABILITIES, desired_capabilities=DESIRED_CAPABILITIES, allowed_mechs=ANONYMOUS) client = SyncRequestResponse(connection) client.connection.close() server.join(timeout=self.timeout) self.assertEquals(server.properties_received, True) self.assertEquals(server.offered_capabilities_received, True) self.assertEquals(server.desired_capabilities_received, True)
def collect_metric(self, entityType): try: client = SyncRequestResponse( BlockingConnection("127.0.0.1:5672", 30), "$management") try: properties = {} properties["entityType"] = entityType properties["operation"] = "QUERY" properties["name"] = "self" message = Message(body=None, properties=properties) response = client.call(message) if response == None: return response else: return RouterResponse(response) finally: client.connection.close() except: e = sys.exc_info()[0] print("Error querying router for metrics: %s" % e) return None
def test_max_frame_max_session_frames__max_sessions_default(self): # Set up a connection to get the Open and a receiver to get a Begin frame in the log bc = BlockingConnection(self.router.addresses[0]) bc.create_receiver("xxx") bc.close() with open('../setUpClass/MaxFrameMaxSessionFrames.log', 'r') as router_log: log_lines = router_log.read().split("\n") open_lines = [s for s in log_lines if "-> @open" in s] # max-frame is from the config self.assertTrue(' max-frame-size=2048,' in open_lines[0]) # channel-max is default self.assertTrue(" channel-max=32767" in open_lines[0]) begin_lines = [s for s in log_lines if "-> @begin" in s] # incoming-window is from the config self.assertTrue(" incoming-window=10," in begin_lines[0] )
def test_full_link_route_match(self): """ The linkRoute on Routers C and B is set to org.apache. Creates a receiver listening on the address 'org.apache' and a sender that sends to address 'org.apache'. Sends a message to org.apache via router QDR.C and makes sure that the message was successfully routed (using full address matching) and received using pre-created links that were created as a result of specifying addresses in the linkRoute attribute('org.apache.'). """ hello_world_3 = "Hello World_3!" # Connects to listener #2 on QDR.C addr = self.routers[2].addresses[0] blocking_connection = BlockingConnection(addr) # Receive on org.apache blocking_receiver = blocking_connection.create_receiver( address="org.apache") apply_options = AtMostOnce() # Sender to to org.apache blocking_sender = blocking_connection.create_sender( address="org.apache", options=apply_options) msg = Message(body=hello_world_3) # Send a message blocking_sender.send(msg) received_message = blocking_receiver.receive() self.assertEqual(hello_world_3, received_message.body) local_node = Node.connect(self.routers[0].addresses[0], timeout=TIMEOUT) # Make sure that the router node acting as the broker (QDR.A) had one message routed through it. This confirms # that the message was link routed self.assertEqual( 1, local_node.read(type='org.apache.qpid.dispatch.router.address', name='M0org.apache').deliveriesEgress) self.assertEqual( 1, local_node.read(type='org.apache.qpid.dispatch.router.address', name='M0org.apache').deliveriesIngress) blocking_connection.close()
def connection(url=None, router=None, timeout=10, ssl_domain=None, sasl=None, edge_router=None): """Return a BlockingConnection suitable for connecting to a management node @param url: URL of the management node. @param router: If address does not contain a path, use the management node for this router ID. If not specified and address does not contain a path, use the default management node. """ url = Url(url) # Convert string to Url class. if url.path is None: if router: url.path = u'_topo/0/%s/$management' % router elif edge_router: url.path = u'_edge/%s/$management' % edge_router else: url.path = u'$management' if ssl_domain: sasl_enabled = True else: sasl_enabled = True if sasl else False # if sasl_mechanism is unicode, convert it to python string return BlockingConnection( url, timeout=timeout, ssl_domain=ssl_domain, sasl_enabled=sasl_enabled, allowed_mechs=str(sasl.mechs) if sasl and sasl.mechs != None else None, user=str(sasl.user) if sasl and sasl.user != None else None, password=str(sasl.password) if sasl and sasl.password != None else None)
def test_full_link_route_match_1(self): """ This test is pretty much the same as the previous test (test_full_link_route_match) but the connection is made to router QDR.B instead of QDR.C and we expect the message to be link routed successfully. """ hello_world_4 = "Hello World_4!" addr = self.routers[1].addresses[0] blocking_connection = BlockingConnection(addr) # Receive on org.apache blocking_receiver = blocking_connection.create_receiver( address="org.apache") apply_options = AtMostOnce() # Sender to to org.apache blocking_sender = blocking_connection.create_sender( address="org.apache", options=apply_options) msg = Message(body=hello_world_4) # Send a message blocking_sender.send(msg) received_message = blocking_receiver.receive() self.assertEqual(hello_world_4, received_message.body) local_node = Node.connect(self.routers[0].addresses[0], timeout=TIMEOUT) # Make sure that the router node acting as the broker (QDR.A) had one message routed through it. This confirms # that the message was link routed self.assertEqual( 1, local_node.read(type='org.apache.qpid.dispatch.router.address', name='M0org.apache').deliveriesEgress) self.assertEqual( 1, local_node.read(type='org.apache.qpid.dispatch.router.address', name='M0org.apache').deliveriesIngress) blocking_connection.close()
class ReceiverThread(threading.Thread): def __init__(self, _id, address=ADDRESS, domain=None): super(ReceiverThread, self).__init__() self._id = _id self.address = address self.domain = domain self.running = True self.nr = 0 self.max = RECEIVERS def connect(self): self.conn = BlockingConnection(ROUTER_ADDRESS, ssl_domain=self.domain, heartbeat=HEARTBEAT) def run(self): self.connect() for x in xrange(self.max): name = '%s.%s' % (self.address, x) self.recv = self.conn.create_receiver(name, name=str(uuid4()), dynamic=False, options=None) print("created: ", name)
def test_max_frame_max_session_zero(self): # Set up a connection to get the Open and a receiver to get a Begin frame in the log bc = BlockingConnection(self.router.addresses[0]) bc.create_receiver("xxx") bc.close() with open('../setUpClass/MaxFrameMaxSessionFramesZero.log', 'r') as router_log: log_lines = router_log.read().split("\n") open_lines = [s for s in log_lines if "-> @open" in s] # max-frame gets set to protocol min self.assertIn(' max-frame-size=512,', open_lines[0]) begin_lines = [s for s in log_lines if "-> @begin" in s] # incoming-window should be 2^31-1 (64-bit) or # (2^31-1) / max-frame-size (32-bit) is_64bits = sys.maxsize > 2**32 expected = " incoming-window=2147483647," if is_64bits else \ (" incoming-window=%d," % int(2147483647 / 512)) self.assertIn(expected, begin_lines[0])
class SyncRequestClient(IncomingMessageHandler): """ Implementation of the synchronous request-responce (aka RPC) pattern. Create an instance and call invoke() to send a request and wait for a response. """ def __init__(self, url, timeout=None): """ @param url: a proton.Url or a URL string of the form 'host:port/path' host:port is used to connect, path is used to identify the remote messaging endpoint. """ super(SyncRequestClient, self).__init__() self.connection = BlockingConnection(Url(url).defaults(), timeout=timeout) self.sender = self.connection.create_sender(url.path) # dynamic=true generates a unique address dynamically for this receiver. # credit=1 because we want to receive 1 response message initially. self.receiver = self.connection.create_receiver(None, dynamic=True, credit=1, handler=self) self.response = None def invoke(self, request): """Send a request, wait for and return the response""" request.reply_to = self.reply_to self.sender.send(request) self.connection.wait(lambda: self.response, msg="Waiting for response") response = self.response self.response = None # Ready for next response. self.receiver.flow(1) # Set up credit for the next response. return response @property def reply_to(self): """Return the dynamic address of our receiver.""" return self.receiver.remote_source.address def on_message(self, event): """Called when we receive a message for our receiver.""" self.response = event.message # Store the response def close(self): self.connection.close()
def test_max_session_frames_default(self): # Set up a connection to get the Open and a receiver to get a Begin frame in the log bc = BlockingConnection(self.router.addresses[0]) bc.create_receiver("xxx") bc.close() with open('../setUpClass/MaxSessionFramesDefault.log', 'r') as router_log: log_lines = router_log.read().split("\n") open_lines = [s for s in log_lines if "-> @open" in s] # if frame size not set then a default is used self.assertIn(" max-frame-size=16384", open_lines[0]) begin_lines = [s for s in log_lines if "-> @begin" in s] # incoming-window should be 2^31-1 (64-bit) or # (2^31-1) / max-frame-size (32-bit) is_64bits = sys.maxsize > 2**32 expected = " incoming-window=2147483647," if is_64bits else \ (" incoming-window=%d," % int(2147483647 / 16384)) #self.assertIn(expected, begin_lines[0], "Expected:'%s' not found in '%s'" % (expected, begin_lines[0])) self.assertIn(expected, begin_lines[0])
def test_zzz_qdmanage_delete_link_route(self): """ We are deleting the link route using qdmanage short name. This should be the last test to run """ # First delete linkRoutes on QDR.B local_node = Node.connect(self.routers[1].addresses[0], timeout=TIMEOUT) result_list = local_node.query(type='org.apache.qpid.dispatch.router.config.linkRoute').results identity_1 = result_list[0][1] identity_2 = result_list[1][1] identity_3 = result_list[2][1] identity_4 = result_list[3][1] cmd = 'DELETE --type=linkRoute --identity=' + identity_1 self.run_qdmanage(cmd=cmd, address=self.routers[1].addresses[0]) cmd = 'DELETE --type=linkRoute --identity=' + identity_2 self.run_qdmanage(cmd=cmd, address=self.routers[1].addresses[0]) cmd = 'DELETE --type=linkRoute --identity=' + identity_3 self.run_qdmanage(cmd=cmd, address=self.routers[1].addresses[0]) cmd = 'DELETE --type=linkRoute --identity=' + identity_4 self.run_qdmanage(cmd=cmd, address=self.routers[1].addresses[0]) cmd = 'QUERY --type=linkRoute' out = self.run_qdmanage(cmd=cmd, address=self.routers[1].addresses[0]) self.assertEquals(out.rstrip(), '[]') # linkRoutes now gone on QDR.B but remember that it still exist on QDR.C # We will now try to create a receiver on address org.apache.dev on QDR.C. # Since the linkRoute on QDR.B is gone, QDR.C # will not allow a receiver to be created since there is no route to destination. # Connects to listener #2 on QDR.C addr = self.routers[2].addresses[1] # Now delete linkRoutes on QDR.C to eradicate linkRoutes completely local_node = Node.connect(addr, timeout=TIMEOUT) result_list = local_node.query(type='org.apache.qpid.dispatch.router.config.linkRoute').results identity_1 = result_list[0][1] identity_2 = result_list[1][1] identity_3 = result_list[2][1] identity_4 = result_list[3][1] cmd = 'DELETE --type=linkRoute --identity=' + identity_1 self.run_qdmanage(cmd=cmd, address=addr) cmd = 'DELETE --type=linkRoute --identity=' + identity_2 self.run_qdmanage(cmd=cmd, address=addr) cmd = 'DELETE --type=linkRoute --identity=' + identity_3 self.run_qdmanage(cmd=cmd, address=addr) cmd = 'DELETE --type=linkRoute --identity=' + identity_4 self.run_qdmanage(cmd=cmd, address=addr) cmd = 'QUERY --type=linkRoute' out = self.run_qdmanage(cmd=cmd, address=addr) self.assertEquals(out.rstrip(), '[]') blocking_connection = BlockingConnection(addr, timeout=3) # Receive on org.apache.dev (this address used to be linkRouted but not anymore since we deleted linkRoutes # on both QDR.C and QDR.B) blocking_receiver = blocking_connection.create_receiver(address="org.apache.dev") apply_options = AtMostOnce() hello_world_1 = "Hello World_1!" # Sender to org.apache.dev blocking_sender = blocking_connection.create_sender(address="org.apache.dev", options=apply_options) msg = Message(body=hello_world_1) # Send a message blocking_sender.send(msg) received_message = blocking_receiver.receive(timeout=5) self.assertEqual(hello_world_1, received_message.body)