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( get_current_config().test_module.device_id, get_current_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_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( get_current_config().test_module.device_id, get_current_config().test_module.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_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( get_current_config().test_module.device_id, get_current_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( get_current_config().test_module.device_id, get_current_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_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( 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 log_message("disconnecting module client") module_client.disconnect() log_message("disconnecting eventhub client") eventhub_client.disconnect()
def connect_eventhub_client(): """ connect the module client for the EventHub implementation we're using return the client object """ current_config = runtime_config.get_current_config() client = adapters.EventHubClient() client.connect(current_config.eventhub.connection_string) return client
def connect_registry_client(): """ connect the module client for the Registry implementation we're using return the client object """ current_config = runtime_config.get_current_config() client = adapters.RegistryClient() client.connect(current_config.registry.connection_string) return client
def connect_service_client(): """ connect the module client for the ServiceClient implementation we're using return the client object """ current_config = runtime_config.get_current_config() client = adapters.ServiceClient() client.connect(current_config.service.connection_string) return client
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_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, 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 set_up_log_watcher(): global log_watcher filters = ["PYTEST: ", "Getting next batch", "Obtained next batch"] container_names = [ "edgeHub", "friendMod", runtime_config.get_current_config().test_module.module_id, ] log_watcher = DockerLogWatcher(container_names, filters)
def connect_test_device_client(): """ connect the device client for the test device and return the client object """ current_config = runtime_config.get_current_config() client = adapters.TestDeviceClient() client.connect( current_config.test_device.transport, current_config.test_device.connection_string, current_config.ca_certificate, ) return client
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, get_current_config().leaf_device.device_id) module_client.disconnect() leaf_device_client.disconnect()
def test_device_method_from_service_to_leaf_device(): """ invoke a method call from the service API and respond to it from the leaf device """ service_client = connections.connect_service_client() leaf_device_client = connections.connect_leaf_device_client() do_device_method_call(service_client, leaf_device_client, get_current_config().leaf_device.device_id) service_client.disconnect() leaf_device_client.disconnect()
def test_device_method_from_service_to_leaf_device_fi(): """ """ service_client = connections.connect_service_client() leaf_device_client = connections.connect_leaf_device_client() do_device_method_call(service_client, leaf_device_client, get_current_config().leaf_device.device_id) service_client.disconnect() leaf_device_client.disconnect()
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()
def connect_friend_module_client(): """ connect the module client for the friend module and return the client object """ current_config = runtime_config.get_current_config() client = adapters.FriendModuleClient() if (current_config.friend_module.connection_type == runtime_config_templates.ENVIRONMENT): client.connect_from_environment(current_config.friend_module.transport) else: client.connect( current_config.friend_module.transport, current_config.friend_module.connection_string, current_config.ca_certificate, ) return client
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( 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()
def test_device_receive_c2d(): device_client = None service = None try: device_client = connections.connect_test_device_client() service = connections.connect_service_client() sent_message = test_utilities.max_random_string() device_client.enable_c2d() test_input_thread = device_client.wait_for_c2d_message_async() service.send_c2d(get_current_config().test_device.device_id, sent_message) received_message = test_input_thread.get(receive_timeout) assert received_message == sent_message finally: if device_client: device_client.disconnect() if service: service.disconnect()
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( get_current_config().test_module.device_id, get_current_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( get_current_config().test_module.device_id, get_current_config().test_module.module_id, twin_sent, ) log_message("patch " + str(i) + " triggered") done = False mistakes_left = 1 while not done: 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"])) 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)) if twin_sent["properties"]["desired"]["foo"] == foo_val: log_message("success") done = True else: if mistakes_left: # We sometimes get the old value before we get the new value, and that's # perfectly valid (especially with QOS 1 on MQTT). If we got the wrong # value, we just try again. mistakes_left = mistakes_left - 1 log_message( "trying again. We still have {} mistakes left".format( mistakes_left)) log_message("start waiting for patch #{} again".format(i)) patch_thread = module_client.wait_for_desired_property_patch_async( ) else: log_message("too many mistakes. Failing") assert False registry_client.disconnect() module_client.disconnect()
def set_channels(request): global friend_to_test_output global test_to_friend_input friend_to_test_output = "to" + get_current_config().test_module.module_id test_to_friend_input = "from" + get_current_config().test_module.module_id