def test_registry_client_connect_disconnect_fi(): log_message("registry_client = connections.connect_registry_client()") registry_client = connections.connect_registry_client() disconnect_edgehub() connect_edgehub() log_message("registry_client.disconnect()") registry_client.disconnect()
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)
async def test_service_can_set_desired_properties_and_module_can_retrieve_them_fi( logger): twin_sent = {"properties": {"desired": {"foo": random.randint(1, 9999)}}} logger("connecting registry client") registry_client = connections.connect_registry_client() logger("disconnecting edgehub") sleep(2) disconnect_edgehub() # DISCONNECTING EGEHUB connect_edgehub() # CONNECTING EDGEHUB await registry_client.patch_module_twin(settings.test_module.device_id, settings.test_module.module_id, twin_sent) logger("patching twin") logger("disconnecting registry client") registry_client.disconnect_sync() logger("connecting module client") module_client = connections.connect_test_module_client() logger("enabling twin") await module_client.enable_twin() logger("disconnecting edgehub") sleep(2) disconnect_edgehub() # DISCONNECTING EGEHUB sleep(5) connect_edgehub() # CONNECTING EDGEHUB twin_received = await module_client.get_twin() logger("getting module twin") logger("disconnecting module client") module_client.disconnect_sync() logger("module client disconnected") logger("twin sent: " + str(twin_sent)) logger("twin received:" + str(twin_received)) assert (twin_sent["properties"]["desired"]["foo"] == twin_received["properties"]["desired"]["foo"])
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)
def test_service_client_connect_disconnect_fi(): log_message("service_client = connections.connect_service_client()") service_client = connections.connect_service_client() disconnect_edgehub() connect_edgehub() log_message("service_client.disconnect()") service_client.disconnect()
async def test_module_client_connect_enable_methods_disconnect_fi(logger): logger("Connect Test Module Client") module_client = connections.connect_test_module_client() logger("Enable Methods on Module Client") await module_client.enable_methods() disconnect_edgehub() connect_edgehub() module_client.disconnect_sync()
def test_module_client_connect_enable_input_messages_disconnect_fi(): module_client = connections.connect_test_module_client() log_message("Enable Input Messages on Module Client") module_client.enable_input_messages() disconnect_edgehub() # Disconnecting Edgehub connect_edgehub() # Reconnecting EdgeHub log_message("Disconnect Module Client") module_client.disconnect()
def test_module_client_connect_enable_methods_disconnect_fi(): log_message("Connect Test Module Client") module_client = connections.connect_test_module_client() log_message("Enable Methods on Module Client") module_client.enable_methods() disconnect_edgehub() connect_edgehub() module_client.disconnect()
def test_module_client_connect_enable_twin_disconnect_fi(): log_message("Connect Test Module Client") module_client = connections.connect_test_module_client() log_message("Enable Twin on Module Client") module_client.enable_twin() disconnect_edgehub() connect_edgehub() log_message("Disconnect Module Client") module_client.disconnect()
async def test_module_client_connect_enable_twin_disconnect_fi(logger): logger("Connect Test Module Client") module_client = connections.connect_test_module_client() logger("Enable Twin on Module Client") await module_client.enable_twin() disconnect_edgehub() connect_edgehub() logger("Disconnect Module Client") module_client.disconnect_sync()
def test_service_can_set_multiple_desired_property_patches_and_module_can_retrieve_them_as_events_fi( ): log_message("connecting registry client") registry_client = connections.connect_registry_client() log_message("connecting module client") module_client = connections.connect_test_module_client() log_message("enabling twin") module_client.enable_twin() base = random.randint(1, 9999) * 100 for i in range(1, 4): log_message("sending patch #" + str(i) + " through registry client") # Send patch twin_sent = {"properties": {"desired": {"foo": base + i}}} registry_client.patch_module_twin( runtime_config.test_module.device_id, runtime_config.test_module.module_id, twin_sent, ) log_message("patch " + str(i) + " sent") log_message("start waiting for patch #" + str(i)) patch_thread = (module_client.wait_for_desired_property_patch_async() ) # Set Twin Callback log_message("Fault Injection: disconnecting edgehub") disconnect_edgehub() # DISCONNECTING EGEHUB log_message("Fault Injection: reconnecting edgehub") connect_edgehub() # CONNECTING EDGEHUB sleep(2) log_message("Tringgering patch #" + str(i) + " through registry client") # Trigger Twin Callback registry_client.patch_module_twin( runtime_config.test_module.device_id, runtime_config.test_module.module_id, twin_sent, ) log_message("patch " + str(i) + " triggered") log_message("waiting for patch " + str(i) + " to arrive at module client") patch_received = patch_thread.get() # Get twin from patch received log_message("patch received:" + json.dumps(patch_received)) log_message("desired properties sent: " + str(twin_sent["properties"]["desired"]["foo"])) foo_val = get_patch_received(patch_received) if foo_val == -1: log_message("patch received of invalid format!") assert 0 log_message("desired properties recieved: " + str(foo_val)) assert twin_sent["properties"]["desired"]["foo"] == foo_val registry_client.disconnect() module_client.disconnect()
def test_service_can_set_multiple_desired_property_patches_and_module_can_retrieve_them_as_events_fi( ): log_message("connecting registry client") registry_client = connections.connect_registry_client() log_message("connecting module client") module_client = connections.connect_test_module_client() log_message("enabling twin") module_client.enable_twin() base = random.randint(1, 9999) * 100 for i in range(1, 4): log_message("start waiting for patch #" + str(i)) patch_thread = module_client.wait_for_desired_property_patch_async() log_message("sending patch #" + str(i) + " through registry client") log_message("disconnecting edgehub") sleep(2) disconnect_edgehub() # DISCONNECTING EGEHUB connect_edgehub() # CONNECTING EDGEHUB twin_sent = {"properties": {"desired": {"foo": base + i}}} registry_client.patch_module_twin(environment.edge_device_id, environment.module_id, twin_sent) log_message("patch " + str(i) + " sent") log_message("waiting for patch " + str(i) + " to arrive at module client") patch_received = patch_thread.get() log_message("patch received:" + json.dumps(patch_received)) log_message("desired properties sent: " + str(twin_sent["properties"]["desired"]["foo"])) # Most of the time, the C wrapper returns a patch with "foo" at the root. Sometimes it # returns a patch with "properties/desired" at the root. I know that this has to do with timing and # the difference between the code that handles the initial GET and the code that handles # the PATCH that arrives later. I suspect it has something to do with the handling for # DEVICE_TWIN_UPDATE_COMPLETE and maybe we occasionally get a full twin when we're waiting # for a patch, but that's just an educated guess. # # I don't know if this is happening in the SDK or in the glue. # this happens relatively rarely. Maybe 1/20, maybe 1/100 times foo_val = get_patch_received(patch_received) if (foo_val == -1): log_message("patch received of invalid format!") assert 0 log_message("desired properties recieved: " + str(foo_val)) assert twin_sent["properties"]["desired"]["foo"] == foo_val registry_client.disconnect() module_client.disconnect()
def test_service_can_set_desired_properties_and_module_can_retrieve_them_fi(): try: twin_sent = { "properties": { "desired": { "foo": random.randint(1, 9999) } } } log_message("connecting registry client") registry_client = connections.connect_registry_client() log_message("disconnecting edgehub") sleep(2) disconnect_edgehub() # DISCONNECTING EGEHUB connect_edgehub() # CONNECTING EDGEHUB registry_client.patch_module_twin( runtime_config.test_module.device_id, runtime_config.test_module.module_id, twin_sent, ) log_message("patching twin") log_message("disconnecting registry client") registry_client.disconnect() log_message("connecting module client") module_client = connections.connect_test_module_client() log_message("enabling twin") module_client.enable_twin() log_message("disconnecting edgehub") sleep(2) disconnect_edgehub() # DISCONNECTING EGEHUB sleep(5) connect_edgehub() # CONNECTING EDGEHUB twin_received = module_client.get_twin() log_message("getting module twin") log_message("disconnecting module client") module_client.disconnect() log_message("module client disconnected") log_message("twin sent: " + str(twin_sent)) log_message("twin received:" + str(twin_received)) assert (twin_sent["properties"]["desired"]["foo"] == twin_received["properties"]["desired"]["foo"]) finally: cMod = client.containers.get("cMod") friendMod = client.containers.get("friendMod") edgeHub = client.containers.get("edgeHub") edgeHub.restart() friendMod.restart() cMod.restart()
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)
def do_module_method_call( source_module, destination_module, destination_device_id, destination_module_id, registration_sleep=time_for_method_to_fully_register, ): """ Helper function which invokes a method call on one module and responds to it from another module """ 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) log_message( "sleeping for {} seconds to make sure all registration is complete". format(registration_sleep)) time.sleep(registration_sleep) disconnect_edgehub() # One point that could be good to disconnect edgeHub # time.sleep(1) connect_edgehub() log_message("Sleeping") time.sleep(30) log_message(" Done Sleeping") # invoking the call from caller side log_message("invoking method call") response = source_module.call_module_method_async( destination_device_id, destination_module_id, method_invoke_parameters).get() log_message("method call complete. Response is:") log_message(str(response)) # wait for that response to arrive back at the source and verify that it's all good. 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()
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 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)
def test_module_input_output_loopback_fi(): log_message("connecting module client") module_client = connections.connect_test_module_client() log_message("enabling input messages") module_client.enable_input_messages() log_message("listening for input messages") input_thread = module_client.wait_for_input_event_async(input_name) sent_message = test_utilities.max_random_string() disconnect_edgehub() # Disconnect Edgehub module_client.send_output_event(output_name, sent_message) connect_edgehub() # Reconnect Edgehub log_message("sent output event: " + str(sent_message)) log_message("waiting for input message to arrive") received_message = input_thread.get(receive_timeout) log_message("input message arrived") log_message("expected message: " + str(sent_message)) log_message("received message: " + str(received_message)) assert received_message == sent_message module_client.disconnect()
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)
def test_module_send_event_iothub_fi(): """ Sends event through Edge Hub to IoT Hub and validates the message is received using the Event Hub API. The module client is in the langauge being tested, and the eventhub client is directly connected to Azure to receive the event. """ 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() log_message("sending event " + " async: " + str(sent_message)) module_client.send_event_async(sent_message) log_message("wait for event to arrive at eventhub") 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("Intial message not received") assert False disconnect_edgehub() # DISCONNECT EDGEHUB module_client.send_event_async(sent_message) connect_edgehub() # RECONNECT EDGEHUB 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("Second message not received") assert False log_message("disconnecting module client") module_client.disconnect() log_message("disconnecting eventhub client") eventhub_client.disconnect()
async def test_module_send_event_iothub_fi(test_object_stringified, test_object_stringified_2, logger, eventhub): """ Sends event through Edge Hub to IoT Hub and validates the message is received using the Event Hub API. The module client is in the langauge being tested, and the eventhub client is directly connected to Azure to receive the event. """ module_client = connections.connect_test_module_client() await eventhub.connect() await module_client.send_event(test_object_stringified) received_message = await eventhub.wait_for_next_event( module_client.device_id, expected=test_object_stringified) if not received_message: logger("Intial message not received") assert False disconnect_edgehub() # DISCONNECT EDGEHUB module_client.send_event(test_object_stringified_2) # do not await connect_edgehub() # RECONNECT EDGEHUB received_message = await eventhub.wait_for_next_event( module_client.device_id, expected=test_object_stringified_2) if not received_message: logger("Second message not received") assert False module_client.disconnect_sync()