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_module_send_event_to_iothub(): 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: " + str(sent_message)) module_client.send_event(sent_message) 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()
def test_module_can_set_reported_properties_and_service_can_retrieve_them(): reported_properties_sent = {"foo": random.randint(1, 9999)} log_message("connecting module client") module_client = connections.connect_test_module_client() log_message("enabling twin") module_client.enable_twin() log_message("patching twin") module_client.patch_twin(reported_properties_sent) log_message("disconnecting module client") module_client.disconnect() log_message("module client disconnected") log_message("connecting registry client") registry_client = connections.connect_registry_client() log_message("getting twin") twin_received = registry_client.get_module_twin( runtime_config.test_module.device_id, runtime_config.test_module.module_id) log_message("disconnecting registry client") registry_client.disconnect() log_message("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"] log_message("expected:" + str(reported_properties_sent)) log_message("received:" + str(reported_properties_received)) assert reported_properties_sent == reported_properties_received
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_service_can_set_desired_properties_and_module_can_retrieve_them(): twin_sent = {"properties": {"desired": {"foo": random.randint(1, 9999)}}} log_message("connecting registry client") registry_client = connections.connect_registry_client() log_message("patching twin") registry_client.patch_module_twin( environment.edge_device_id, environment.module_id, twin_sent ) 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("getting module twin") twin_received = module_client.get_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"] )
def test_service_can_set_multiple_desired_property_patches_and_module_can_retrieve_them_as_events( ): 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") 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() log_message("Tringgering patch #" + str(i) + " through registry client") 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) + " triggered") log_message("getting patch " + str(i) + " on 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_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_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()
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_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_device_method_from_module_to_leaf_device_fi(): """ invoke a method call from the test module and respond to it from the leaf device """ module_client = connections.connect_test_module_client() leaf_device_client = connections.connect_leaf_device_client() do_device_method_call(module_client, leaf_device_client, environment.leaf_device_id) module_client.disconnect() leaf_device_client.disconnect()
def test_device_method_from_module_to_leaf_device(): """ invoke a method call from the test module and respond to it from the leaf device """ module_client = connections.connect_test_module_client() leaf_device_client = connections.connect_leaf_device_client() do_device_method_call(module_client, leaf_device_client, get_current_config().leaf_device.device_id) module_client.disconnect() leaf_device_client.disconnect()
def test_module_method_from_friend_to_test(): """ invoke a method call from the friend module and respond to it from the test module """ module_client = connections.connect_test_module_client() friend_client = connections.connect_friend_module_client() do_module_method_call(friend_client, module_client, environment.edge_device_id, environment.module_id) module_client.disconnect() friend_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_method_from_friend_to_test_fi(): """ invoke a method call from the friend module and respond to it from the test module """ module_client = connections.connect_test_module_client() friend_client = connections.connect_friend_module_client() await asyncio.sleep(5) await do_module_method_call(friend_client, module_client, module_client.device_id, module_client.module_id) module_client.disconnect_sync() friend_client.disconnect_sync()
def test_module_to_friend_routing(): test_client = connections.connect_test_module_client() friend_client = connections.connect_friend_module_client() friend_client.enable_input_messages() friend_input_thread = friend_client.wait_for_input_event_async(test_to_friend_input) sent_message = test_utilities.max_random_string() test_client.send_output_event(test_to_friend_output, sent_message) received_message = friend_input_thread.get(receive_timeout) assert received_message == sent_message friend_client.disconnect() test_client.disconnect()
async def test_module_to_friend_routing(test_string): test_client = connections.connect_test_module_client() friend_client = connections.connect_friend_module_client() await friend_client.enable_input_messages() friend_input_future = asyncio.ensure_future( friend_client.wait_for_input_event(test_to_friend_input) ) await test_client.send_output_event(test_to_friend_output, test_string) received_message = await friend_input_future assert received_message == test_string friend_client.disconnect_sync() test_client.disconnect_sync()
async def test_module_to_friend_routing(sample_payload): payload = sample_payload() test_client = connections.connect_test_module_client() friend_client = connections.connect_friend_module_client() await friend_client.enable_input_messages() friend_input_future = asyncio.ensure_future( friend_client.wait_for_input_event(test_to_friend_input)) await test_client.send_output_event(test_to_friend_output, payload) received_message = await friend_input_future assert received_message == payload friend_client.disconnect_sync() test_client.disconnect_sync()
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 test_module_method_from_friend_to_test_fi(): """ invoke a method call from the friend module and respond to it from the test module """ module_client = connections.connect_test_module_client() friend_client = connections.connect_friend_module_client() time.sleep(5) do_module_method_call( friend_client, module_client, get_current_config().test_module.device_id, get_current_config().test_module.module_id, ) module_client.disconnect() friend_client.disconnect()
def test_module_method_from_test_to_friend_fi(): """ invoke a method call from the test module and respond to it from the friend module """ module_client = connections.connect_test_module_client() friend_client = connections.connect_friend_module_client() time.sleep(5) do_module_method_call( module_client, friend_client, environment.edge_device_id, environment.friend_module_id, ) module_client.disconnect() friend_client.disconnect()
def test_module_method_from_friend_to_test(): """ invoke a method call from the friend module and respond to it from the test module """ module_client = connections.connect_test_module_client() friend_client = connections.connect_friend_module_client() do_module_method_call( friend_client, module_client, runtime_config.test_module.device_id, runtime_config.test_module.module_id, ) module_client.disconnect() friend_client.disconnect()
def test_module_method_call_invoked_from_service(): """ invoke a module call from the service and responds to it from the test module. """ service_client = connections.connect_service_client() module_client = connections.connect_test_module_client() do_module_method_call( service_client, module_client, runtime_config.test_module.device_id, runtime_config.test_module.module_id, registration_sleep=time_for_method_to_fully_register_service_call, ) module_client.disconnect() service_client.disconnect()
def test_module_output_routed_upstream(): module_client = connections.connect_test_module_client() eventhub_client = connections.connect_eventhub_client() 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( environment.edge_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()
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 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)