def test_module_send_event_iothub_fi():
    try:
        log_message("connecting module client")
        module_client = connections.connect_test_module_client()
        log_message("connecting eventhub client")
        eventhub_client = connections.connect_eventhub_client()
        sent_message = test_utilities.random_string_in_json()
        disconnect_edgehub()  # DISCONNECT EDGEHUB
        log_message("sending event " + " async: " + str(sent_message))
        thread = module_client.send_event_async(sent_message)
        connect_edgehub()  # RECONNECT EDGEHUB
        log_message("getting result with timeout: " + str(local_timeout))
        thread.wait(local_timeout)  # Result is None if successful
        log_message("wait for event to arrive at eventhub")
        received_message = eventhub_client.wait_for_next_event(
            runtime_config.test_module.device_id,
            test_utilities.default_eventhub_timeout,
            expected=sent_message,
        )
        if not received_message:
            log_message("Message not received")
            assert False
        log_message("disconnecting module client")
        module_client.disconnect()
        log_message("disconnecting eventhub client")
        eventhub_client.disconnect()
    finally:
        restart_edgehub(hard=False)
示例#2
0
def test_module_test_to_friend_and_back_fi():
    try:
        test_client = connections.connect_test_module_client()
        test_client.enable_input_messages()
        friend_client = connections.connect_friend_module_client()
        friend_client.enable_input_messages()

        test_input_thread = test_client.wait_for_input_event_async(
            friend_to_test_input)
        friend_input_thread = friend_client.wait_for_input_event_async(
            test_to_friend_input)
        disconnect_edgehub()
        sent_message = test_utilities.max_random_string()
        test_client.send_output_event(test_to_friend_output, sent_message)
        connect_edgehub()
        midpoint_message = friend_input_thread.get(receive_timeout)
        assert midpoint_message == sent_message

        second_sent_message = test_utilities.max_random_string()
        friend_client.send_output_event(friend_to_test_output,
                                        second_sent_message)

        received_message = test_input_thread.get(receive_timeout)
        assert received_message == second_sent_message

        friend_client.disconnect()
        test_client.disconnect()
    finally:
        restart_edgehub(hard=True)
async def test_module_can_set_reported_properties_and_service_can_retrieve_them_fi(
    logger
):
    try:
        reported_properties_sent = {"foo": random.randint(1, 9999)}
        logger("connecting module client")
        module_client = connections.connect_test_module_client()
        logger("enabling twin")
        await module_client.enable_twin()
        logger("disabling edgehub")
        sleep(2)
        disconnect_edgehub()
        connect_edgehub()
        await module_client.patch_twin(reported_properties_sent)
        sleep(2)
        logger("patched twin")
        logger("disconnecting module client")
        module_client.disconnect_sync()
        logger("module client disconnected")
        logger("connecting registry client")
        registry_client = connections.connect_registry_client()
        logger("disabling edgehub")
        sleep(2)
        disconnect_edgehub()
        connect_edgehub()
        sleep(2)
        logger("reconnected edgehub")
        logger("getting twin")
        twin_received = await registry_client.get_module_twin(
            module_client.device_id, module_client.module_id
        )
        logger("disconnecting registry client")
        registry_client.disconnect_sync()
        logger("registry client disconnected")

        reported_properties_received = twin_received["properties"]["reported"]
        if "$version" in reported_properties_received:
            del reported_properties_received["$version"]
        if "$metadata" in reported_properties_received:
            del reported_properties_received["$metadata"]
        logger("expected:" + str(reported_properties_sent))
        logger("received:" + str(reported_properties_received))

        assert reported_properties_sent == reported_properties_received
    finally:
        restart_edgehub()
        sleep(5)
示例#4
0
def test_module_method_call_invoked_from_service():
    """
    invoke a module call from the service and responds to it from the test module.
    """

    restart_edgehub(hard=True)
    time.sleep(5)
    service_client = connections.connect_service_client()
    module_client = connections.connect_test_module_client()
    do_module_method_call(
        service_client,
        module_client,
        get_current_config().test_module.device_id,
        get_current_config().test_module.module_id,
        registration_sleep=time_for_method_to_fully_register_service_call,
    )

    module_client.disconnect()
    service_client.disconnect()
def do_device_method_call(source_module, destination_module,
                          destination_device_id):
    """
    Helper function which invokes a method call on one module and responds to it from another module
    """
    try:
        log_message("enabling methods on the destination")
        destination_module.enable_methods()

        # start listening for method calls on the destination side
        log_message("starting to listen from destination module")
        receiver_thread = destination_module.roundtrip_method_async(
            method_name, status_code, method_invoke_parameters,
            method_response_body)
        time.sleep(time_for_method_to_fully_register)

        disconnect_edgehub()
        # invoking the call from caller side
        time.sleep(5)
        connect_edgehub()
        log_message("invoking method call")
        request_thread = source_module.call_device_method_async(
            destination_device_id, method_invoke_parameters)
        response = request_thread.get()
        print("method call complete.  Response is:")
        print(str(response))

        # wait for that response to arrive back at the source and verify that it's all good.
        log_message("response = " + str(response) + "\n")
        assert response["status"] == status_code
        # edge bug: the response that edge returns is stringified.  The same response that comes back from an iothub service call is not stringified
        if isinstance(response["payload"], str):
            response["payload"] = json.loads(response["payload"])
        assert response["payload"] == method_response_body

        receiver_thread.wait()
    finally:
        connect_edgehub()
        restart_edgehub(hard=False)
async def do_device_method_call(source_module, destination_module,
                                destination_device_id):
    """
    Helper function which invokes a method call on one module and responds to it from another module
    """
    try:
        adapter_config.logger("enabling methods on the destination")
        await destination_module.enable_methods()

        # start listening for method calls on the destination side
        adapter_config.logger("starting to listen from destination module")
        receiver_future = asyncio.ensure_future(
            destination_module.wait_for_method_and_return_response(
                method_name, status_code, method_invoke_parameters,
                method_response_body))
        await asyncio.sleep(time_for_method_to_fully_register)

        disconnect_edgehub()
        # invoking the call from caller side
        await asyncio.sleep(5)
        connect_edgehub()
        adapter_config.logger("invoking method call")
        response = await source_module.call_device_method(
            destination_device_id, method_invoke_parameters)
        adapter_config.logger("method call complete.  Response is:")
        adapter_config.logger(str(response))

        # wait for that response to arrive back at the source and verify that it's all good.
        adapter_config.logger("response = " + str(response) + "\n")
        assert response["status"] == status_code
        # edge bug: the response that edge returns is stringified.  The same response that comes back from an iothub service call is not stringified
        if isinstance(response["payload"], str):
            response["payload"] = json.loads(response["payload"])
        assert response["payload"] == method_response_body

        await receiver_future
    finally:
        connect_edgehub()
        restart_edgehub(hard=False)
def test_module_send_multiple_event_iothub_fi():
    try:
        log_message("connecting module client")
        module_client = connections.connect_test_module_client()
        log_message("connecting eventhub client")
        eventhub_client = connections.connect_eventhub_client()
        log_message("enabling telemetry on eventhub client")
        eventhub_client.enable_telemetry()

        log_message("start waiting for events on eventhub")
        input_thread = eventhub_client.wait_for_event_async(
            environment.edge_device_id)
        sent_message = test_utilities.random_string_in_json()

        log_message("disconnecting edgehub")
        disconnect_edgehub()  # DISCONNECT EDGEHUB
        print("PYTEST FAKE: begin sending events async...")
        # Send String of Messages Asynchronously to create a queue
        for i in range(5):
            print("PYTEST FAKE: sending event " + str(i) + " async: " +
                  str(sent_message))
            thread = module_client.send_event_async(sent_message)
        connect_edgehub()  # RECONNECT EDGEHUB

        log_message("getting result with timeout: " + str(local_timeout))
        thread.wait(local_timeout)  # Result is None if successful
        log_message("wait for event to arrive at eventhub")
        received_message = input_thread.get(
            test_utilities.default_eventhub_timeout)
        log_message("expected event: " + str(sent_message))
        log_message("received event: " + str(received_message))
        test_utilities.assert_json_equality(received_message, sent_message)
        log_message("disconnecting module client")
        module_client.disconnect()
        log_message("disconnecting eventhub client")
        eventhub_client.disconnect()
    finally:
        restart_edgehub(hard=False)
def test_module_output_routed_upstream_fi():
    try:
        module_client = connections.connect_test_module_client()
        eventhub_client = connections.connect_eventhub_client()

        disconnect_edgehub()
        connect_edgehub()
        sent_message = test_utilities.random_string_in_json()
        module_client.send_output_event(output_name, sent_message)

        received_message = eventhub_client.wait_for_next_event(
            get_current_config().test_module.device_id,
            test_utilities.default_eventhub_timeout,
            expected=sent_message,
        )
        if not received_message:
            log_message("Message not received")
            assert False

        module_client.disconnect()
        eventhub_client.disconnect()
    finally:
        restart_edgehub(hard=False)