def query_service_registry(self, client, query):
     request = Request(self.DXL_SERVICE_REGISTRY_QUERY_TOPIC)
     if not query:
         query = {}
     request.payload = json.dumps(query)
     response = client.sync_request(request, timeout=self.RESPONSE_WAIT)
     return json.loads(
         response.payload.decode("utf8").rstrip("\0"))["services"]
    def test_specify_service_in_request(self):
        service_count = 10
        request_count = 100

        request_received_count = [0]
        request_to_wrong_service_id_count = [0]
        requests_by_service = {}
        request_lock = threading.Lock()

        topic = UuidGenerator.generate_id_as_string()

        with self.create_client() as service_client:
            service_client.connect()

            def my_request(callback_service_id, request):
                with request_lock:
                    request_received_count[0] += 1
                    if request.service_id and \
                            (request.service_id != callback_service_id):
                        request_to_wrong_service_id_count[0] += 1
                    if request.service_id in requests_by_service:
                        requests_by_service[request.service_id] += 1
                    else:
                        requests_by_service[request.service_id] = 1
                    response = Response(request)
                    service_client.send_response(response)

            def create_service_reg_info():
                reg_info = ServiceRegistrationInfo(
                    service_client, "registry_specified_service_id_test")
                callback = RequestCallback()
                callback.on_request = \
                    lambda request: my_request(reg_info.service_id, request)
                reg_info.add_topic(topic, callback)
                service_client.register_service_sync(reg_info,
                                                     self.DEFAULT_TIMEOUT)
                return reg_info

            reg_infos = [create_service_reg_info()
                         for _ in range(service_count)]
            with self.create_client() as request_client:
                request_client.connect()
                for _ in range(0, request_count):
                    request = Request(topic)
                    request.service_id = reg_infos[0].service_id
                    response = request_client.sync_request(
                        request, timeout=self.RESPONSE_WAIT)
                    self.assertNotIsInstance(response, ErrorResponse)
                    self.assertEqual(request.message_id,
                                     response.request_message_id)
            with request_lock:
                self.assertEqual(0, request_to_wrong_service_id_count[0])
                self.assertEqual(request_count, request_received_count[0])
                self.assertEqual(1, len(requests_by_service))
                self.assertIn(reg_infos[0].service_id, requests_by_service)
                self.assertEqual(
                    request_count, requests_by_service[reg_infos[0].service_id])
    def set_certificate_reputation(self, trust_level, sha1, public_key_sha1=None, comment=""):
        """
        Sets the "Enterprise" reputation (`trust level`) of a specified certificate (as identified by hashes).

        .. note::

            **Client Authorization**

            The OpenDXL Python client invoking this method must have permission to send messages to the
            ``/mcafee/service/tie/cert/reputation/set`` topic which is part of the
            ``TIE Server Set Enterprise Reputation`` authorization group.

            The following page provides an example of authorizing a Python client to send messages to an
            `authorization group`. While the example is based on McAfee Active Response (MAR), the
            instructions are the same with the exception of swapping the ``TIE Server Set Enterprise Reputation``
            `authorization group` in place of ``Active Response Server API``:

            `<https://opendxl.github.io/opendxl-client-python/pydoc/marsendauth.html>`_

        **Example Usage**

            .. code-block:: python

                    # Set the enterprise reputation (trust level) for the certificate to Known Trusted
                    tie_client.set_certificate_reputation(
                        TrustLevel.KNOWN_TRUSTED,
                        "1C26E2037C8E205B452CAB3565D696512207D66D",
                        public_key_sha1="B4C3B2D596D1461C1BB417B92DCD74817ABB829D",
                        comment="Reputation set via OpenDXL")

        :param trust_level: The new `trust level` for the file. The list of standard `trust levels` can be found in the
            :class:`dxltieclient.constants.TrustLevel` constants class.
        :param sha1: The SHA-1 of the certificate
        :param public_key_sha1: The SHA-1 of the certificate's public key (optional)
        :param comment: A comment to associate with the certificate (optional)
        """
        # Create the request message
        req = Request(TIE_SET_CERT_REPUTATION_TOPIC)

        # Create a dictionary for the payload
        payload_dict = {
            "trustLevel": trust_level,
            "providerId": CertProvider.ENTERPRISE,
            "comment": comment,
            "hashes": [
                {"type": "sha1", "value": base64.b64encode(sha1.decode('hex'))}
            ]}

        # Add public key SHA-1 (if specified)
        if public_key_sha1:
            payload_dict["publicKeySha1"] = base64.b64encode(public_key_sha1.decode('hex'))

        # Set the payload
        req.payload = json.dumps(payload_dict).encode(encoding="UTF-8")

        # Send the request
        self.__dxl_sync_request(req)
    def test_execute_async_callback_timeout(self):

        # TODO: Set SYSPROP_ASYNC_CALLBACK_CHECK_INTERVAL = 10000 when it is available
        def resp_callback():
            pass

        cb = ResponseCallback()
        cb.on_response = resp_callback

        with self.create_client() as client:
            client.connect()

            req_topic = UuidGenerator.generate_id_as_string()
            missing_topic = UuidGenerator.generate_id_as_string()

            test_service = TestService(client, 1)

            def empty_on_request(request):
                pass

            test_service.on_request = empty_on_request

            reg_info = ServiceRegistrationInfo(client,
                                               "async_callback_test_service")
            reg_info.add_topic(req_topic, test_service)
            # Register the service
            client.register_service_sync(reg_info, self.DEFAULT_TIMEOUT)

            async_req = Request(destination_topic=req_topic)
            client.async_request(
                async_req,
                cb)  # TODO: Use the method with timeout when is will available

            for i in range(0, 10):
                req = Request(destination_topic=req_topic)
                client.async_request(
                    req, cb
                )  # TODO: Use the updated method with timeout when it is available

            req_for_error = Request(destination_topic=missing_topic)
            client.async_request(req_for_error)
            async_callback_count = client._get_async_callback_count()
            self.assertEquals(11, async_callback_count)

            for i in range(0, 20):
                print "asyncCallbackCount = " + str(
                    client._get_async_callback_count())
                time.sleep(1)
                req = Request(destination_topic=req_topic)
                client.async_request(req, cb)

            self.assertEquals(1, async_callback_count)
Beispiel #5
0
    def test_execute_registry_query(self):
        with self.create_client() as client:
            client.connect()
            topic = "/mcafee/service/dxl/brokerregistry/query"

            req = Request(topic)
            req.payload = "{}"
            response = client.sync_request(req)

            self.assertNotIsInstance(response, ErrorResponse)
            print("## sourceBrokerGuid: " + str(response.source_broker_id))
            print("## sourceClientGuid: " + str(response.source_client_id))
            print(str(response.payload))
    def test_execute_message_payload(self):

        # Create a server that handles a request, unpacks the payload, and
        # asserts that the information in the payload was delivered successfully.
        with self.create_client(max_retries=0) as server:
            test_service = TestService(server, 1)
            server.connect()
            topic = UuidGenerator.generate_id_as_string()
            reg_info = ServiceRegistrationInfo(
                server, "message_payload_runner_service")

            # callback definition
            def on_request(request):

                unpacker = Unpacker(
                    file_like=StringIO.StringIO(request.payload))

                with self.request_complete_condition:
                    try:
                        self.assertEquals(unpacker.next(), self.TEST_STRING)
                        self.assertEquals(unpacker.next(), self.TEST_BYTE)
                        self.assertEquals(unpacker.next(), self.TEST_INT)
                        self.received_request = True
                    except Exception, e:
                        print e.message
                    self.request_complete_condition.notify_all()

            request_callback = RequestCallback()
            request_callback.on_request = on_request
            reg_info.add_topic(topic, request_callback)
            # Register the service
            server.register_service_sync(reg_info, self.DEFAULT_TIMEOUT)

            with self.create_client() as client:
                client.connect()
                packer = Packer()

                # Send a request to the server with information contained
                # in the payload
                request = Request(destination_topic=topic)
                request.payload = packer.pack(self.TEST_STRING)
                request.payload += packer.pack(self.TEST_BYTE)
                request.payload += packer.pack(self.TEST_INT)
                client.async_request(request, request_callback)

                with self.request_complete_condition:
                    if not self.received_request:
                        # Wait until the request has been processed
                        self.request_complete_condition.wait(self.MAX_WAIT)
                        if not self.received_request:
                            self.fail("Request not received.")
Beispiel #7
0
    def test_execute_message_payload(self):

        # Create a server that handles a request, unpacks the payload, and
        # asserts that the information in the payload was delivered successfully.
        with self.create_client(max_retries=0) as service_client:
            service_client.connect()
            topic = UuidGenerator.generate_id_as_string()
            reg_info = ServiceRegistrationInfo(
                service_client, "message_payload_runner_service")

            # callback definition
            def on_request(request):
                with self.request_complete_condition:
                    try:
                        self.request_received = request
                    except Exception as ex:  # pylint: disable=broad-except
                        print(ex)
                    self.request_complete_condition.notify_all()

            request_callback = RequestCallback()
            request_callback.on_request = on_request
            reg_info.add_topic(topic, request_callback)
            # Register the service
            service_client.register_service_sync(reg_info,
                                                 self.DEFAULT_TIMEOUT)

            with self.create_client() as request_client:
                request_client.connect()
                packer = msgpack.Packer()

                # Send a request to the server with information contained
                # in the payload
                request = Request(destination_topic=topic)
                request.payload = packer.pack(self.TEST_STRING)
                request.payload += packer.pack(self.TEST_BYTE)
                request.payload += packer.pack(self.TEST_INT)
                request_client.async_request(request, request_callback)

                start = time.time()
                # Wait until the request has been processed
                with self.request_complete_condition:
                    while (time.time() - start < self.MAX_WAIT) and \
                            not self.request_received:
                        self.request_complete_condition.wait(self.MAX_WAIT)

                self.assertIsNotNone(self.request_received)
                unpacker = msgpack.Unpacker(file_like=BytesIO(request.payload))
                self.assertEqual(
                    next(unpacker).decode('utf8'), self.TEST_STRING)
                self.assertEqual(next(unpacker), self.TEST_BYTE)
                self.assertEqual(next(unpacker), self.TEST_INT)
    def _invoke_mar_search_api(self, payload_dict):
        """
        Executes a query against the MAR search API

        :param payload_dict: The payload
        :return: A dictionary containing the results of the query
        """
        # Create the request message
        req = Request(MAR_SEARCH_TOPIC)
        # Set the payload
        req.payload = json.dumps(payload_dict).encode(encoding="UTF-8")

        # Display the request that is going to be sent
        logger.debug(
            "Request:\n%s",
            json.dumps(payload_dict,
                       sort_keys=True,
                       indent=4,
                       separators=(',', ': ')))

        # Send the request and wait for a response (synchronous)
        res = self._dxl_sync_request(req)

        # Return a dictionary corresponding to the response payload
        resp_dict = MessageUtils.json_payload_to_dict(res)
        # Display the response
        logger.debug(
            "Response:\n%s",
            json.dumps(resp_dict,
                       sort_keys=True,
                       indent=4,
                       separators=(',', ': ')))
        if "code" in resp_dict:
            code = resp_dict['code']
            if code < 200 or code >= 300:
                if "body" in resp_dict and "applicationErrorList" in \
                        resp_dict["body"]:
                    error = resp_dict["body"]["applicationErrorList"][0]
                    raise Exception(error["message"] + ": " +
                                    str(error["code"]))
                elif "body" in resp_dict:
                    raise Exception(resp_dict["body"] + ": " + str(code))
                else:
                    raise Exception("Error: Received failure response code: " +
                                    str(code))
        else:
            raise Exception("Error: unable to find response code")
        return resp_dict
    def test_multiple_registrations(self):
        service_registration_count = 10
        request_received_count = [0]

        topic = UuidGenerator.generate_id_as_string()

        with self.create_client() as service_client:
            service_client.connect()

            def my_request(request):
                request_received_count[0] += 1
                response = Response(request)
                service_client.send_response(response)

            reg_info = ServiceRegistrationInfo(service_client,
                                               "multiple_registrations_test")
            callback = RequestCallback()
            callback.on_request = my_request
            reg_info.add_topic(topic, callback)
            with self.create_client() as request_client:
                request_client.connect()
                for _ in range(0, service_registration_count):
                    service_client.register_service_sync(reg_info,
                                                         self.DEFAULT_TIMEOUT)
                    request = Request(topic)
                    response = request_client.sync_request(
                        request, timeout=self.RESPONSE_WAIT)
                    self.assertNotIsInstance(response, ErrorResponse)
                    self.assertEqual(request.message_id,
                                     response.request_message_id)
                    service_client.unregister_service_sync(reg_info,
                                                           self.DEFAULT_TIMEOUT)
                self.assertEqual(service_registration_count,
                                 request_received_count[0])
Beispiel #10
0
    def test_callback_parkeddomain(self):
        with BaseClientTest.create_client(max_retries=0) as dxl_client:
            dxl_client.connect()

            with MockServerRunner() as server_runner:
                with ApiVoidService(TEST_FOLDER) as apivoid_service:
                    apivoid_service._dxl_client = dxl_client

                    apivoid_service.API_VOID_URL_FORMAT = "http://127.0.0.1:" \
                                                  + str(server_runner.mock_server_port) \
                                                  + "/{0}/v1/pay-as-you-go/{1}"

                    apivoid_service.run()

                    request_topic = ApiVoidService.REQ_TOPIC_PARKED_DOMAIN
                    req = Request(request_topic)
                    MessageUtils.dict_to_json_payload(
                        req, {ApiVoidCallback.PARAM_HOST: SAMPLE_HOST})

                    res = apivoid_service._dxl_client.sync_request(req,
                                                                   timeout=30)

                    res_dict = MessageUtils.json_payload_to_dict(res)

                    self.assertDictEqual(SAMPLE_PARKED_DOMAIN, res_dict)
    def test_callback_statsremained(self):
        with MockServerRunner() as server_runner:
            with UrlVoidApiService(TEST_FOLDER) as urlvoid_service:

                urlvoid_service.URL_VOID_API_URL_FORMAT = "http://127.0.0.1:" \
                                              + str(server_runner.mock_server_port) \
                                              + "/api1000/{0}/"

                urlvoid_service.run()

                request_topic = UrlVoidApiService.REQ_TOPIC_STATS_REMAINED
                req = Request(request_topic)
                MessageUtils.dict_to_json_payload(
                    req,
                    {
                        UrlVoidApiCallback.PARAM_HOST: SAMPLE_HOST
                    }
                )

                res = urlvoid_service._dxl_client.sync_request(req, timeout=30)

                self.assertEqual(
                    dicttoxml(
                        SAMPLE_REMAINED_OUTPUT,
                        custom_root=XML_ROOT_RESPONSE,
                        attr_type=False
                    ),
                    res.payload
                )
    def lookup_epo_unique_identifiers(dxl_client,
                                      response_timeout=DEFAULT_RESPONSE_TIMEOUT
                                      ):
        """
        Returns a ``set`` containing the unique identifiers for the ePO servers that are currently
        exposed to the DXL fabric

        :param dxl_client: The DXL client with which to perform the request
        :param response_timeout: (optional) The maximum amount of time to wait for a response
        :return: A ``set`` containing the unique identifiers for the ePO servers that are currently
            exposed to the DXL fabric.
        """
        res = EpoClient._sync_request(
            dxl_client, Request("/mcafee/service/dxl/svcregistry/query"),
            response_timeout, {"serviceType": EpoClient.DXL_SERVICE_TYPE})
        res_dict = json.loads(res.rstrip("\0"))

        ret_ids = set()
        if "services" in res_dict:
            for service in res_dict["services"].values():
                if "requestChannels" in service:
                    channels = service['requestChannels']
                    for channel in channels:
                        if channel.startswith(EpoClient.DXL_REQUEST_PREFIX):
                            ret_ids.add(
                                channel[len(EpoClient.DXL_REQUEST_PREFIX):])
        return ret_ids
 def event_callback(event):
     try:
         req = Request(destination_topic=req_topic)
         client.sync_request(req)
     except Exception, e:
         self.exceptions.append(e)
         raise e
    def test_error_message(self):
        with self.create_client() as client:
            test_service = TestService(client, 1)
            client.connect()

            error_code = 9090
            error_message = "My error message"

            topic = UuidGenerator.generate_id_as_string()

            #
            # Create a test service that returns error messages
            #

            reg_info = ServiceRegistrationInfo(client,
                                               "testErrorMessageService")
            reg_info.add_topic(topic, test_service)
            client.register_service_sync(reg_info, self.DEFAULT_TIMEOUT)

            test_service.return_error = True
            test_service.error_code = error_code
            test_service.error_message = error_message
            client.add_request_callback(topic, test_service)

            # Send a request and ensure the response is an error message
            response = client.sync_request(Request(topic))
            self.assertIsInstance(response,
                                  ErrorResponse,
                                  msg="Response is not an ErrorResponse")
            self.assertEqual(error_code, response.error_code)
            self.assertEqual(error_message, response.error_message)
Beispiel #15
0
    def test_register_service_and_send_request(self):

        with self.create_client() as client:
            self.add_client_callbacks(client)
            client.register_service_async(self.info)
            time.sleep(self.POST_OP_DELAY)
            client.connect()
            time.sleep(self.POST_OP_DELAY)

            request = Request("/mcafee/service/JTI/file/reputation/" + self.info.service_id)
            request.payload = bytes("Test")

            response = client.sync_request(request, self.POST_OP_DELAY)
            logging.info("Response payload: {0}".format(str(response.payload)))

            self.assertEquals("Ok", str(response.payload))
Beispiel #16
0
    def test_async_request(self):
        request = Request(destination_topic="/test")

        class TestResponseCallback(ResponseCallback):
            def __init__(self):
                super(TestResponseCallback, self).__init__()
                self.response = None

            def on_response(self, response):
                self.response = response

        callback = TestResponseCallback()
        self.assertIsNone(callback.response)

        self.request_manager.async_request(request, callback)

        self.assertEqual(0, len(self.request_manager.sync_wait_message_ids))
        self.assertEqual(0,
                         len(self.request_manager.sync_wait_message_responses))
        self.assertTrue(
            request.message_id in self.request_manager.callback_map)

        response = Response(request=request)
        self.request_manager.on_response(response)

        self.assertIsNotNone(callback.response)
        self.assertEqual(request.message_id,
                         callback.response.request_message_id)

        self.assertEqual(0, len(self.request_manager.sync_wait_message_ids))
        self.assertEqual(0,
                         len(self.request_manager.sync_wait_message_responses))
        self.assertFalse(
            request.message_id in self.request_manager.callback_map)
Beispiel #17
0
 def test_client_send_request_publishes_message_to_dxl_fabric(self):
     self.client._client.publish = Mock(return_value=None)
     # Create and process Request
     msg = Request(destination_topic="")
     self.client._send_request(msg)
     # Check that callback was called
     self.assertEqual(self.client._client.publish.call_count, 1)
    def test_sync_request(self):
        request = Request(destination_topic="/test")

        with self.assertRaises(exceptions.WaitTimeoutException):
            self.rm.sync_request(request, 2)

        self.assertEquals(0, len(self.rm.sync_wait_message_ids))
        self.assertEquals(0, len(self.rm.sync_wait_message_responses))
    def test_register_async_callback(self):
        request = Request(destination_topic="/test")
        cb = MockResponseCallback()

        self.rm.register_async_callback(request, cb)
        self.assertTrue(request.message_id in self.rm.callback_map)
        self.rm.unregister_async_callback(request.message_id)
        self.assertFalse(request.message_id in self.rm.callback_map)
    def test_register_service_and_send_request(self):

        with self.create_client() as client:
            self.add_client_callbacks(client)
            client.register_service_async(self.info)
            client.connect()
            self.assertTrue(self.wait_info_registered())

            request = Request("/mcafee/service/JTI/file/reputation/" +
                              self.info.service_id)
            request.payload = "Test"

            response = client.sync_request(request, self.POST_OP_DELAY)
            logging.info("Response payload: %s",
                         response.payload.decode("utf8"))

            self.assertEqual("Ok", response.payload.decode("utf8"))
 def test_register_wait_for_response(self):
     request = Request(destination_topic="/test")
     self.rm.register_wait_for_response(request)
     self.assertEquals(1, len(self.rm.sync_wait_message_ids))
     self.assertTrue(request.message_id in self.rm.sync_wait_message_ids)
     self.rm.unregister_wait_for_response(request)
     self.assertEquals(0, len(self.rm.sync_wait_message_ids))
     self.assertFalse(request.message_id in self.rm.sync_wait_message_ids)
 def event_callback(_):
     with self.event_received_condition:
         self.event_received = True
         try:
             req = Request(destination_topic=req_topic)
             client.sync_request(req)
         except Exception as ex: # pylint: disable=broad-except
             self.request_exception_message = str(ex)
         self.event_received_condition.notify_all()
Beispiel #23
0
 def test_client_handle_message_with_request_calls_request_callback(self):
     req_callback = RequestCallback()
     req_callback.on_request = Mock()
     self.client.add_request_callback(self.test_channel, req_callback)
     # Create and process Request
     req = Request(destination_topic=self.test_channel)._to_bytes()
     self.client._handle_message(self.test_channel, req)
     # Check that callback was called
     self.assertEqual(req_callback.on_request.call_count, 1)
Beispiel #24
0
    def test_register_service_weak_reference_after_connect_and_send_request(
            self):

        with self.create_client() as client:
            self.add_client_callbacks(client)
            ref = weakref.ref(self.info)

            client.register_service_async(self.info)
            client.connect()

            info_guid = self.info.service_id
            # Theoretically, sending the destroy method when creating the weakref
            # would made the magic of calling that method when info get unref
            self.info._destroy()
            # Deleted the service registration
            self.info = 1
            time.sleep(self.POST_OP_DELAY * 2)

            # Enforce garbage collection
            gc.collect()
            # Weak reference should now be null
            self.assertEquals(None, ref())

            # Sending an request should result in a WaitTimeoutException since the destroy() method
            # of ServiceRegistrationInfo will unregister the service
            request = Request("/mcafee/service/JTI/file/reputation/" +
                              info_guid)
            request.payload = bytes("Test")

            try:
                response = client.sync_request(request, 2)
                # Depending upon the timing, the broker can respond with 404 or the request might timeout
                # self.assertIsInstance(response, ErrorResponse, "response is instance of ErrorResponse")
                self.assertTrue(isinstance(response, ErrorResponse),
                                response.__class__)
            except WaitTimeoutException as ex:
                assert (ex.message.__contains__(request.message_id))

        self.assertEquals(1, self.register_callback.get())
        logging.debug("Waiting for unregister event...")
        ttw = 30
        while self.unregister_callback.get() < 1 and ttw > 0:
            time.sleep(self.POST_OP_DELAY)
            ttw -= 1
    def test_multiple_services(self):
        with self.create_client() as service_client:
            service_client.connect()
            reg_info_topic_1 = "multiple_services_test_1_" + \
                               UuidGenerator.generate_id_as_string()
            reg_info_1 = ServiceRegistrationInfo(
                service_client, "multiple_services_test_1")

            def reg_info_request_1(request):
                response = Response(request)
                response.payload = "service1"
                service_client.send_response(response)

            reg_info_callback_1 = RequestCallback()
            reg_info_callback_1.on_request = reg_info_request_1
            reg_info_1.add_topic(reg_info_topic_1, reg_info_callback_1)
            service_client.register_service_sync(reg_info_1,
                                                 self.DEFAULT_TIMEOUT)

            reg_info_topic_2 = "multiple_services_test_2_" + \
                               UuidGenerator.generate_id_as_string()
            reg_info_2 = ServiceRegistrationInfo(
                service_client, "multiple_services_test_2")

            def reg_info_request_2(request):
                response = Response(request)
                response.payload = "service2"
                service_client.send_response(response)

            reg_info_callback_2 = RequestCallback()
            reg_info_callback_2.on_request = reg_info_request_2
            reg_info_2.add_topic(reg_info_topic_2, reg_info_callback_2)
            service_client.register_service_sync(reg_info_2,
                                                 self.DEFAULT_TIMEOUT)
            with self.create_client() as request_client:
                request_client.connect()
                response = request_client.sync_request(
                    Request(reg_info_topic_1), self.DEFAULT_TIMEOUT)
                self.assertIsInstance(response, Response)
                self.assertEqual(response.payload.decode("utf8"), "service1")
                response = request_client.sync_request(
                    Request(reg_info_topic_2), self.DEFAULT_TIMEOUT)
                self.assertIsInstance(response, Response)
                self.assertEqual(response.payload.decode("utf8"), "service2")
Beispiel #26
0
            def run():
                try:
                    with client_factory(max_retries=0) as client:
                        retries = self.MAX_CONNECT_RETRIES
                        connected = False
                        while not connected and retries > 0:
                            try:
                                client.connect()
                                connected = True
                            except Exception: # pylint: disable=broad-except
                                if retries > 0:
                                    retries -= 1
                                    self.connect_retries += 1
                        self.assertTrue(connected, "Unable to connect after retries")

                        # Waiting all clients have connected
                        with self.connect_condition:
                            self.atomic_connect_count += 1
                            self.connect_condition.notify_all()

                            while self.atomic_connect_count != self.THREAD_COUNT:
                                curr_count = self.atomic_connect_count
                                self.connect_condition.wait(self.MAX_CONNECT_WAIT)
                                if self.atomic_connect_count == curr_count:
                                    self.fail("Timeout waiting for all threads to connect")

                            # Once all clients have connected, reset timing information
                            if self.requests_start_time == 0:
                                self.requests_start_time = time.time()
                                self.connect_time = self.requests_start_time - connect_time_start

                        for _ in range(0, self.REQUEST_COUNT):
                            req = Request(topic)
                            call_start_time = time.time()
                            response = client.sync_request(req, timeout=self.DEFAULT_TIMEOUT)
                            response_time = time.time() - call_start_time
                            self.assertNotIsInstance(response, ErrorResponse)

                            with self.response_count_condition:
                                self.response_count += 1
                                count = self.response_count
                                if (self.requests_end_time == 0) and \
                                        (count == (self.THREAD_COUNT * self.REQUEST_COUNT)):
                                    self.requests_end_time = time.time()

                            if count % 100 == 0:
                                print(str(count) + ", " + str(time.time() - self.requests_start_time))

                            # Calulate and track response times
                            self.cummulative_response_time = self.cummulative_response_time + response_time
                            self.response_times.append(response_time)

                except Exception as ex:
                    print(ex)
                    logging.info(ex)
                    raise ex
 def _invoke_epo_service(self, payload_dict):
     """
     Invokes the ePO DXL service for the purposes of executing a remote command
     :param payload_dict: The dictionary (``dict``) to use as the payload of the DXL request
     :return: The result of the remote command execution
     """
     return self._sync_request(
         self._dxl_client,
         Request(self.DXL_REQUEST_FORMAT.format(self._epo_unique_id)),
         self._response_timeout, payload_dict)
Beispiel #28
0
    def test_client_receives_error_response_on_request_to_unknown_service(self):
        """
        The idea of this test is to send a sync request to an unknown service
        and get a "unable to locate service" error response.
        """
        with self.create_client() as client:
            test_topic = '/test/doesntexists/' + client.config._client_id
            client.connect()
            time.sleep(2)
            self.assertTrue(client.connected)

            # Send request thru dxl fabric to a service which doesn't exists
            msg = Request(destination_topic=test_topic)
            msg.service_id = UuidGenerator.generate_id_as_string()
            response = client.sync_request(msg, 1)

            # Check that we have an error response for our request
            self.assertTrue(isinstance(response, ErrorResponse))
            self.assertEqual(response.service_id, msg.service_id)
    def test_response_service_not_found_no_service_id_at_client(self):
        request_received = [False]

        topic = UuidGenerator.generate_id_as_string()

        with self.create_client() as service_client:
            service_client.connect()

            def my_request(request):
                request_received[0] = True
                service_client.send_response(Response(request))

            reg_info = ServiceRegistrationInfo(
                service_client,
                "response_service_not_found_no_service_id_at_client_test")
            callback = RequestCallback()
            callback.on_request = my_request
            reg_info.add_topic(topic, callback)
            reg_info.add_topic(topic, callback)
            service_client.register_service_sync(reg_info,
                                                 self.DEFAULT_TIMEOUT)
            self.assertIsNotNone(
                self.query_service_registry_by_service(
                    service_client, reg_info))
            with self.create_client() as request_client:
                request_client.connect()
                # Remove the service's registration with the client-side
                # ServiceManager, avoiding unregistration of the service from
                # the broker. This should allow the broker to forward the
                # request on to the service client.
                registered_services = service_client._service_manager.services
                service = registered_services[reg_info.service_id]
                del registered_services[reg_info.service_id]
                request = Request(topic)
                response = request_client.sync_request(
                    request, timeout=self.RESPONSE_WAIT)
                # Re-register the service with the internal ServiceManager so
                # that its resources (TTL timeout, etc.) can be cleaned up
                # properly at shutdown.
                registered_services[reg_info.service_id] = service
                # The request should receive an 'unavailable service' error
                # response because the service client should be unable to route
                # the request to an internally registered service.
                self.assertFalse(request_received[0])
                self.assertIsInstance(response, ErrorResponse)
                self.assertEqual(reg_info.service_id, response.service_id)
                self.assertEqual(
                    self.DXL_SERVICE_UNAVAILABLE_ERROR_CODE,
                    BrokerServiceRegistryTest.normalized_error_code(response))
                self.assertEqual(self.DXL_SERVICE_UNAVAILABLE_ERROR_MESSAGE,
                                 response.error_message)
                self.assertIsNone(self.query_service_registry_by_service(
                    service_client, reg_info))
Beispiel #30
0
 def test_messages_are_fired_with_wildcards_enabled(self):
     with DxlClient(self.config) as dxl_client:
         dxl_client.add_request_callback("/this/channel/has/wildcard/#",
                                         self.req_callback)
         dxl_client.add_request_callback("/this/channel/has/wildcard/not/",
                                         self.req_callback)
         # Create and process Request
         req = Request(destination_topic=
                       "/this/channel/has/wildcard/not/")._to_bytes()
         dxl_client._handle_message("/this/channel/has/wildcard/not/", req)
         # Check that callback was called
         self.assertEqual(self.req_callback.on_request.call_count, 2)