def test_state(test_topic, capsys): device_id = device_id_template.format('RSA256') manager.open_registry(service_account_json, project_id, cloud_region, pubsub_topic, registry_id) manager.create_rs256_device(service_account_json, project_id, cloud_region, registry_id, device_id, rsa_cert_path) manager.get_device(service_account_json, project_id, cloud_region, registry_id, device_id) jwt_token = cloudiot_http_example.create_jwt(project_id, 'resources/rsa_private.pem', 'RS256') print( cloudiot_http_example.publish_message('hello', 'state', _BASE_URL, project_id, cloud_region, registry_id, device_id, jwt_token)) manager.get_state(service_account_json, project_id, cloud_region, registry_id, device_id) manager.delete_device(service_account_json, project_id, cloud_region, registry_id, device_id) manager.delete_registry(service_account_json, project_id, cloud_region, registry_id) out, _ = capsys.readouterr() assert 'format : RSA_X509_PEM' in out assert 'State: {' in out assert 'aGVsbG8=' in out assert '200' in out
def test_add_delete_rs256_device(test_topic, capsys): device_id = device_id_template.format('RSA256') manager.open_registry( service_account_json, project_id, cloud_region, pubsub_topic, registry_id) manager.create_rs256_device( service_account_json, project_id, cloud_region, registry_id, device_id, rsa_cert_path) manager.get_device( service_account_json, project_id, cloud_region, registry_id, device_id) manager.get_state( service_account_json, project_id, cloud_region, registry_id, device_id) manager.delete_device( service_account_json, project_id, cloud_region, registry_id, device_id) manager.delete_registry( service_account_json, project_id, cloud_region, registry_id) out, _ = capsys.readouterr() assert 'format : RSA_X509_PEM' in out assert 'State: {' in out
def test_add_patch_delete_rs256(test_topic, capsys): device_id = device_id_template.format('PATCHME') manager.open_registry(service_account_json, project_id, cloud_region, pubsub_topic, registry_id) manager.create_rs256_device(service_account_json, project_id, cloud_region, registry_id, device_id, rsa_cert_path) manager.get_device(service_account_json, project_id, cloud_region, registry_id, device_id) out, _ = capsys.readouterr() assert 'format : RSA_X509_PEM' in out manager.patch_es256_auth(service_account_json, project_id, cloud_region, registry_id, device_id, es_cert_path) manager.get_device(service_account_json, project_id, cloud_region, registry_id, device_id) out, _ = capsys.readouterr() assert 'format : ES256_PEM' in out manager.delete_device(service_account_json, project_id, cloud_region, registry_id, device_id) manager.delete_registry(service_account_json, project_id, cloud_region, registry_id)
def test_config(test_topic, capsys): device_id = device_id_template.format('RSA256') manager.open_registry(service_account_json, project_id, cloud_region, pubsub_topic, registry_id) manager.create_rs256_device(service_account_json, project_id, cloud_region, registry_id, device_id, rsa_cert_path) manager.get_device(service_account_json, project_id, cloud_region, registry_id, device_id) client = cloudiot_mqtt_example.get_client(project_id, cloud_region, registry_id, device_id, rsa_private_path, 'RS256', ca_cert_path, 'mqtt.googleapis.com', 443) client.loop_start() time.sleep(5) client.loop_stop() manager.get_state(service_account_json, project_id, cloud_region, registry_id, device_id) manager.delete_device(service_account_json, project_id, cloud_region, registry_id, device_id) manager.delete_registry(service_account_json, project_id, cloud_region, registry_id) out, _ = capsys.readouterr() assert "Received message" in out assert '/devices/{}/config'.format(device_id) in out
def test_config(test_topic, capsys): device_id = device_id_template.format('RSA256') manager.open_registry(service_account_json, project_id, cloud_region, pubsub_topic, registry_id) manager.create_rs256_device(service_account_json, project_id, cloud_region, registry_id, device_id, rsa_cert_path) manager.get_device(service_account_json, project_id, cloud_region, registry_id, device_id) jwt_token = cloudiot_http_example.create_jwt(project_id, 'resources/rsa_private.pem', 'RS256') print( cloudiot_http_example.get_config('0', 'state', _BASE_URL, project_id, cloud_region, registry_id, device_id, jwt_token).text) manager.get_state(service_account_json, project_id, cloud_region, registry_id, device_id) manager.delete_device(service_account_json, project_id, cloud_region, registry_id, device_id) manager.delete_registry(service_account_json, project_id, cloud_region, registry_id) out, _ = capsys.readouterr() assert 'format : RSA_X509_PEM' in out assert '"version": "1"' in out
def test_add_patch_delete_rs256(test_topic, capsys): device_id = device_id_template.format('PATCHME') manager.open_registry( service_account_json, project_id, cloud_region, pubsub_topic, registry_id) manager.create_rs256_device( service_account_json, project_id, cloud_region, registry_id, device_id, rsa_cert_path) manager.get_device( service_account_json, project_id, cloud_region, registry_id, device_id) out, _ = capsys.readouterr() assert 'format : RSA_X509_PEM' in out manager.patch_es256_auth( service_account_json, project_id, cloud_region, registry_id, device_id, es_cert_path) manager.get_device( service_account_json, project_id, cloud_region, registry_id, device_id) out, _ = capsys.readouterr() assert 'format : ES256_PEM' in out manager.delete_device( service_account_json, project_id, cloud_region, registry_id, device_id) manager.delete_registry( service_account_json, project_id, cloud_region, registry_id)
def test_image(test_topic, capsys): """Send an inage to a device registry""" device_id = device_id_template.format('RSA256') manager.open_registry(service_account_json, project_id, cloud_region, pubsub_topic, registry_id) manager.create_rs256_device(service_account_json, project_id, cloud_region, registry_id, device_id, rsa_cert_path) manager.get_device(service_account_json, project_id, cloud_region, registry_id, device_id) cloudiot_mqtt_image.transmit_image(cloud_region, registry_id, device_id, rsa_private_path, ca_cert_path, image_path, project_id, service_account_json) # Clean up manager.delete_device(service_account_json, project_id, cloud_region, registry_id, device_id) manager.delete_registry(service_account_json, project_id, cloud_region, registry_id) out, _ = capsys.readouterr() assert 'on_publish' in out
def test_state(test_topic, capsys): device_id = device_id_template.format('RSA256') manager.open_registry(service_account_json, project_id, cloud_region, pubsub_topic, registry_id) manager.create_rs256_device(service_account_json, project_id, cloud_region, registry_id, device_id, rsa_cert_path) manager.get_device(service_account_json, project_id, cloud_region, registry_id, device_id) sub_topic = 'state' mqtt_topic = '/devices/{}/{}'.format(device_id, sub_topic) client = cloudiot_mqtt_example.get_client(project_id, cloud_region, registry_id, device_id, rsa_private_path, 'RS256', ca_cert_path, 'mqtt.googleapis.com', 443) client.publish(mqtt_topic, 'state test', qos=1) time.sleep(3) client.loop_stop() manager.get_state(service_account_json, project_id, cloud_region, registry_id, device_id) manager.delete_device(service_account_json, project_id, cloud_region, registry_id, device_id) manager.delete_registry(service_account_json, project_id, cloud_region, registry_id) out, _ = capsys.readouterr() assert 'on_publish' in out assert 'c3RhdGUgdGVzdA' in out
def test_add_delete_rs256_device(test_topic, capsys): device_id = device_id_template.format('RSA256') manager.open_registry( service_account_json, project_id, cloud_region, pubsub_topic, registry_id) manager.create_rs256_device( service_account_json, project_id, cloud_region, registry_id, device_id, rsa_cert_path) manager.get_device( service_account_json, project_id, cloud_region, registry_id, device_id) manager.get_state( service_account_json, project_id, cloud_region, registry_id, device_id) manager.delete_device( service_account_json, project_id, cloud_region, registry_id, device_id) manager.delete_registry( service_account_json, project_id, cloud_region, registry_id) out, _ = capsys.readouterr() assert 'format : RSA_X509_PEM' in out assert 'State: {' in out
def create_device(): try: manager.create_rs256_device(service_account_json, project_id, cloud_region, test_registry_id, device_id, rsa_cert_path) except AlreadyExists as e: # We ignore this case. print("The device already exists: detail: {}".format(str(e)))
def test_state(test_topic, capsys): device_id = device_id_template.format("RSA256") manager.open_registry( service_account_json, project_id, cloud_region, pubsub_topic, registry_id ) manager.create_rs256_device( service_account_json, project_id, cloud_region, registry_id, device_id, rsa_cert_path, ) manager.get_device( service_account_json, project_id, cloud_region, registry_id, device_id ) jwt_token = cloudiot_http_example.create_jwt( project_id, "resources/rsa_private.pem", "RS256" ) print( cloudiot_http_example.publish_message( "hello", "state", _BASE_URL, project_id, cloud_region, registry_id, device_id, jwt_token, ) ) manager.get_state( service_account_json, project_id, cloud_region, registry_id, device_id ) manager.delete_device( service_account_json, project_id, cloud_region, registry_id, device_id ) manager.delete_registry(service_account_json, project_id, cloud_region, registry_id) out, _ = capsys.readouterr() assert "format : RSA_X509_PEM" in out assert 'binary_data: "hello"' in out assert "200" in out
def test_send_command(test_topic, capsys): device_id = device_id_template.format('RSA256') manager.create_registry( service_account_json, project_id, cloud_region, pubsub_topic, registry_id) exists = False devices = manager.list_devices( service_account_json, project_id, cloud_region, registry_id) for device in devices: if device.get('id') == device_id: exists = True if not exists: manager.create_rs256_device( service_account_json, project_id, cloud_region, registry_id, device_id, rsa_cert_path) # Exercize the functionality client = cloudiot_mqtt_example.get_client( project_id, cloud_region, registry_id, device_id, rsa_private_path, 'RS256', ca_cert_path, 'mqtt.googleapis.com', 443) client.loop_start() out, _ = capsys.readouterr() # Pre-process commands for i in range(1, 5): client.loop() time.sleep(1) manager.send_command( service_account_json, project_id, cloud_region, registry_id, device_id, 'me want cookies') out, _ = capsys.readouterr() # Process commands for i in range(1, 5): client.loop() time.sleep(1) # Clean up manager.delete_device( service_account_json, project_id, cloud_region, registry_id, device_id) manager.delete_registry( service_account_json, project_id, cloud_region, registry_id) assert 'Sending command to device' in out assert '400' not in out
def test_send_command(test_topic, capsys): device_id = device_id_template.format('RSA256') manager.create_registry( service_account_json, project_id, cloud_region, pubsub_topic, registry_id) exists = False devices = manager.list_devices( service_account_json, project_id, cloud_region, registry_id) for device in devices: if device.get('id') == device_id: exists = True if not exists: manager.create_rs256_device( service_account_json, project_id, cloud_region, registry_id, device_id, rsa_cert_path) # Exercize the functionality client = cloudiot_mqtt_example.get_client( project_id, cloud_region, registry_id, device_id, rsa_private_path, 'RS256', ca_cert_path, 'mqtt.googleapis.com', 443) client.loop_start() out, _ = capsys.readouterr() # Pre-process commands for i in range(1, 5): client.loop() time.sleep(1) manager.send_command( service_account_json, project_id, cloud_region, registry_id, device_id, 'me want cookies') out, _ = capsys.readouterr() # Process commands for i in range(1, 5): client.loop() time.sleep(1) # Clean up manager.delete_device( service_account_json, project_id, cloud_region, registry_id, device_id) manager.delete_registry( service_account_json, project_id, cloud_region, registry_id) assert 'Sending command to device' in out assert '400' not in out
def test_state(test_topic, capsys): device_id = device_id_template.format('RSA256') manager.open_registry( service_account_json, project_id, cloud_region, pubsub_topic, registry_id) manager.create_rs256_device( service_account_json, project_id, cloud_region, registry_id, device_id, rsa_cert_path) manager.get_device( service_account_json, project_id, cloud_region, registry_id, device_id) sub_topic = 'state' mqtt_topic = '/devices/{}/{}'.format(device_id, sub_topic) client = cloudiot_mqtt_example.get_client( project_id, cloud_region, registry_id, device_id, rsa_private_path, 'RS256', ca_cert_path, 'mqtt.googleapis.com', 443) client.publish(mqtt_topic, 'state test', qos=1) client.loop_start() time.sleep(3) client.loop_stop() manager.get_state( service_account_json, project_id, cloud_region, registry_id, device_id) manager.delete_device( service_account_json, project_id, cloud_region, registry_id, device_id) manager.delete_registry( service_account_json, project_id, cloud_region, registry_id) out, _ = capsys.readouterr() assert 'on_publish' in out assert 'c3RhdGUgdGVzdA' in out
def test_image_recv(test_topic, capsys): """Transmit an image with IoT Core and receive it from PubSub""" subscriber = pubsub.SubscriberClient() topic_path = subscriber.topic_path(project_id, topic_id) subscription_path = subscriber.subscription_path(project_id, subscription_name) subscriber.create_subscription(subscription_path, topic_path) time.sleep(10) device_id = device_id_template.format('RSA256') manager.open_registry(service_account_json, project_id, cloud_region, pubsub_topic, registry_id) manager.create_rs256_device(service_account_json, project_id, cloud_region, registry_id, device_id, rsa_cert_path) manager.get_device(service_account_json, project_id, cloud_region, registry_id, device_id) cloudiot_mqtt_image.transmit_image(cloud_region, registry_id, device_id, rsa_private_path, ca_cert_path, image_path, project_id, service_account_json) time.sleep(10) cloudiot_mqtt_image.receive_image(project_id, subscription_name, 'test', 'png', 60) # Clean up subscriber.delete_subscription(subscription_path) manager.delete_device(service_account_json, project_id, cloud_region, registry_id, device_id) manager.delete_registry(service_account_json, project_id, cloud_region, registry_id) out, _ = capsys.readouterr() assert 'Received image' in out
def test_receive_command(capsys): device_id = device_id_template.format('RSA256') manager.create_registry( service_account_json, project_id, cloud_region, pubsub_topic, registry_id) manager.create_rs256_device( service_account_json, project_id, cloud_region, registry_id, device_id, rsa_cert_path) # Exercize the functionality client = cloudiot_mqtt_example.get_client( project_id, cloud_region, registry_id, device_id, rsa_private_path, 'RS256', ca_cert_path, 'mqtt.googleapis.com', 443) client.loop_start() # Pre-process commands for i in range(1, 3): client.loop() time.sleep(1) manager.send_command( service_account_json, project_id, cloud_region, registry_id, device_id, 'me want cookies') # Process commands for i in range(1, 3): client.loop() time.sleep(1) # Clean up manager.delete_device( service_account_json, project_id, cloud_region, registry_id, device_id) manager.delete_registry( service_account_json, project_id, cloud_region, registry_id) out, _ = capsys.readouterr() assert 'on_connect' in out # Verify can connect assert '\'me want cookies\'' in out # Verify can receive command
def test_config(test_topic, capsys): device_id = device_id_template.format('RSA256') manager.open_registry( service_account_json, project_id, cloud_region, pubsub_topic, registry_id) manager.create_rs256_device( service_account_json, project_id, cloud_region, registry_id, device_id, rsa_cert_path) manager.get_device( service_account_json, project_id, cloud_region, registry_id, device_id) client = cloudiot_mqtt_example.get_client( project_id, cloud_region, registry_id, device_id, rsa_private_path, 'RS256', ca_cert_path, 'mqtt.googleapis.com', 443) client.loop_start() time.sleep(5) client.loop_stop() manager.get_state( service_account_json, project_id, cloud_region, registry_id, device_id) manager.delete_device( service_account_json, project_id, cloud_region, registry_id, device_id) manager.delete_registry( service_account_json, project_id, cloud_region, registry_id) out, _ = capsys.readouterr() assert "Received message" in out assert '/devices/{}/config'.format(device_id) in out
def test_receive_command(capsys): device_id = device_id_template.format('RSA256') manager.create_registry(service_account_json, project_id, cloud_region, pubsub_topic, registry_id) manager.create_rs256_device(service_account_json, project_id, cloud_region, registry_id, device_id, rsa_cert_path) # Exercize the functionality client = cloudiot_mqtt_example.get_client(project_id, cloud_region, registry_id, device_id, rsa_private_path, 'RS256', ca_cert_path, 'mqtt.googleapis.com', 443) client.loop_start() # Pre-process commands for i in range(1, 3): client.loop() time.sleep(1) manager.send_command(service_account_json, project_id, cloud_region, registry_id, device_id, 'me want cookies') # Process commands for i in range(1, 3): client.loop() time.sleep(1) # Clean up manager.delete_device(service_account_json, project_id, cloud_region, registry_id, device_id) manager.delete_registry(service_account_json, project_id, cloud_region, registry_id) out, _ = capsys.readouterr() assert 'on_connect' in out # Verify can connect assert '\'me want cookies\'' in out # Verify can receive command
def test_state(test_topic, capsys): device_id = device_id_template.format('RSA256') manager.open_registry( service_account_json, project_id, cloud_region, pubsub_topic, registry_id) manager.create_rs256_device( service_account_json, project_id, cloud_region, registry_id, device_id, rsa_cert_path) manager.get_device( service_account_json, project_id, cloud_region, registry_id, device_id) jwt_token = cloudiot_http_example.create_jwt( project_id, 'resources/rsa_private.pem', 'RS256') print(cloudiot_http_example.publish_message( 'hello', 'state', _BASE_URL, project_id, cloud_region, registry_id, device_id, jwt_token)) manager.get_state( service_account_json, project_id, cloud_region, registry_id, device_id) manager.delete_device( service_account_json, project_id, cloud_region, registry_id, device_id) manager.delete_registry( service_account_json, project_id, cloud_region, registry_id) out, _ = capsys.readouterr() assert 'format : RSA_X509_PEM' in out assert 'State: {' in out assert 'aGVsbG8=' in out assert '200' in out