Ejemplo n.º 1
0
def test_bind_device_to_gateway_and_unbind(test_topic, capsys):
    gateway_id = device_id_template.format('RS256')
    device_id = device_id_template.format('noauthbind')
    manager.create_registry(service_account_json, project_id, cloud_region,
                            pubsub_topic, registry_id)
    manager.create_gateway(service_account_json, project_id, cloud_region,
                           registry_id, None, gateway_id, rsa_cert_path,
                           'RS256')

    manager.create_device(service_account_json, project_id, cloud_region,
                          registry_id, device_id)

    manager.bind_device_to_gateway(service_account_json, project_id,
                                   cloud_region, registry_id, device_id,
                                   gateway_id)
    manager.unbind_device_from_gateway(service_account_json, project_id,
                                       cloud_region, registry_id, device_id,
                                       gateway_id)

    # Clean up
    manager.delete_device(service_account_json, project_id, cloud_region,
                          registry_id, device_id)
    manager.delete_device(service_account_json, project_id, cloud_region,
                          registry_id, gateway_id)
    manager.delete_registry(service_account_json, project_id, cloud_region,
                            registry_id)

    out, _ = capsys.readouterr()

    assert 'Device Bound' in out
    assert 'Device unbound' in out
    assert 'HttpError 404' 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)
    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
Ejemplo n.º 3
0
def test_add_config_unauth_device(test_topic, capsys):
    device_id = device_id_template.format('UNAUTH')
    manager.open_registry(
            service_account_json, project_id, cloud_region, pubsub_topic,
            registry_id)

    manager.create_unauth_device(
            service_account_json, project_id, cloud_region, registry_id,
            device_id)

    manager.set_config(
            service_account_json, project_id, cloud_region, registry_id,
            device_id, 0, 'test')

    manager.get_device(
            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)

    out, _ = capsys.readouterr()
    assert 'Set device configuration' in out
    assert 'UNAUTH' in out
    assert 'version: 2' 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)

    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_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_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
Ejemplo n.º 7
0
def test_add_patch_delete_es256(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_es256_device(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.patch_rsa256_auth(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.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)
Ejemplo n.º 8
0
def test_add_delete_es256_device(test_topic, capsys):
    device_id = device_id_template.format('ES256')
    manager.open_registry(
            service_account_json, project_id, cloud_region, pubsub_topic,
            registry_id)

    manager.create_es256_device(
            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)

    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 : ES256_PEM' in out
    assert 'State: {' in out
Ejemplo n.º 9
0
 def delete_device():
     try:
         manager.delete_device(service_account_json, project_id,
                               cloud_region, test_registry_id, device_id)
     except NotFound as e:
         # We ignore this case.
         print("The device doesn't exist: detail: {}".format(str(e)))
Ejemplo n.º 10
0
def test_add_patch_delete_es256(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_es256_device(
            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.patch_rsa256_auth(
            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.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)
Ejemplo n.º 11
0
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_add_delete_es256_device(test_topic, capsys):
    device_id = device_id_template.format('ES256')
    manager.open_registry(
            service_account_json, project_id, cloud_region, pubsub_topic,
            registry_id)

    manager.create_es256_device(
            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)

    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 : ES256_PEM' in out
    assert 'State: {' in out
Ejemplo n.º 13
0
def test_add_config_unauth_device(test_topic, capsys):
    device_id = device_id_template.format('UNAUTH')
    manager.open_registry(service_account_json, project_id, cloud_region,
                          pubsub_topic, registry_id)

    manager.create_unauth_device(service_account_json, project_id,
                                 cloud_region, registry_id, device_id)

    manager.set_config(service_account_json, project_id, cloud_region,
                       registry_id, device_id, 0, 'test')

    manager.get_device(service_account_json, project_id, cloud_region,
                       registry_id, device_id)

    manager.get_config_versions(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 'Set device configuration' in out
    assert 'UNAUTH' in out
    assert 'version: 2' in out
def test_send_to_device(capsys):
    manager.create_iot_topic(project_id, topic_id)
    manager.open_registry(service_account_json, project_id, cloud_region,
                          pubsub_topic, registry_id)

    manager.create_unauth_device(service_account_json, project_id,
                                 cloud_region, registry_id, device_id)

    gcs_to_device.send_to_device(gcs_bucket, gcs_file_name,
                                 destination_file_name, project_id,
                                 cloud_region, registry_id, device_id,
                                 service_account_json)

    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)

    pubsub_client = pubsub.PublisherClient()
    topic_path = pubsub_client.topic_path(project_id, topic_id)
    pubsub_client.delete_topic(topic_path)

    out, _ = capsys.readouterr()
    assert 'Successfully sent file to device' in out
Ejemplo n.º 15
0
def test_gateway_trigger_error_topic(test_topic, capsys):
    gateway_id = device_id_template.format('RS256-err')
    device_id = device_id_template.format('noauthbind')
    manager.create_registry(
            service_account_json, project_id, cloud_region, pubsub_topic,
            registry_id)
    manager.create_gateway(
            service_account_json, project_id, cloud_region, registry_id,
            None, gateway_id, rsa_cert_path, 'RS256')
    manager.create_device(
            service_account_json, project_id, cloud_region, registry_id,
            device_id)
    manager.bind_device_to_gateway(
            service_account_json, project_id, cloud_region, registry_id,
            device_id, gateway_id)

    # Setup for listening for config messages
    num_messages = 4

    # Hardcoded callback for causing an error
    def trigger_error(client):
        cloudiot_mqtt_example.attach_device(client, 'invalid_device_id', '')

    # Connect the gateway
    cloudiot_mqtt_example.listen_for_messages(
                service_account_json, project_id, cloud_region, registry_id,
                device_id, gateway_id, num_messages, rsa_private_path,
                'RS256', ca_cert_path, 'mqtt.googleapis.com', 443,
                20, 42, trigger_error)
    # Try to connect the gateway aagin on 8883
    cloudiot_mqtt_example.listen_for_messages(
                service_account_json, project_id, cloud_region, registry_id,
                device_id, gateway_id, num_messages, rsa_private_path,
                'RS256', ca_cert_path, 'mqtt.googleapis.com', 8883,
                20, 15, trigger_error)

    # Clean up
    manager.unbind_device_from_gateway(
            service_account_json, project_id, cloud_region, registry_id,
            device_id, gateway_id)
    manager.delete_device(
            service_account_json, project_id, cloud_region, registry_id,
            device_id)
    manager.delete_device(
            service_account_json, project_id, cloud_region, registry_id,
            gateway_id)
    manager.delete_registry(
            service_account_json, project_id, cloud_region, registry_id)

    out, _ = capsys.readouterr()
    assert 'GATEWAY_ATTACHMENT_ERROR' 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
Ejemplo n.º 17
0
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
    )

    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
Ejemplo n.º 19
0
def test_add_delete_unauth_device(test_topic, capsys):
    device_id = device_id_template.format('UNAUTH')
    manager.open_registry(service_account_json, project_id, cloud_region,
                          pubsub_topic, registry_id)

    manager.create_unauth_device(service_account_json, project_id,
                                 cloud_region, registry_id, device_id)

    manager.get_device(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)

    out, _ = capsys.readouterr()
    assert 'UNAUTH' in out
def test_gateway_trigger_error_topic(test_topic, capsys):
    gateway_id = device_id_template.format('RS256-err')
    device_id = device_id_template.format('noauthbind')
    manager.create_registry(
            service_account_json, project_id, cloud_region, pubsub_topic,
            registry_id)
    manager.create_gateway(
            service_account_json, project_id, cloud_region, registry_id,
            None, gateway_id, rsa_cert_path, 'RS256')
    manager.create_device(
            service_account_json, project_id, cloud_region, registry_id,
            device_id)
    manager.bind_device_to_gateway(
            service_account_json, project_id, cloud_region, registry_id,
            device_id, gateway_id)

    # Setup for listening for config messages
    num_messages = 4

    # Hardcoded callback for causing an error
    def trigger_error(client):
        cloudiot_mqtt_example.attach_device(client, 'invalid_device_id', '')

    # Connect the gateway
    cloudiot_mqtt_example.listen_for_messages(
                service_account_json, project_id, cloud_region, registry_id,
                device_id, gateway_id, num_messages, rsa_private_path,
                'RS256', ca_cert_path, 'mqtt.googleapis.com', 443,
                20, 15, trigger_error)

    # Clean up
    manager.unbind_device_from_gateway(
            service_account_json, project_id, cloud_region, registry_id,
            device_id, gateway_id)
    manager.delete_device(
            service_account_json, project_id, cloud_region, registry_id,
            device_id)
    manager.delete_device(
            service_account_json, project_id, cloud_region, registry_id,
            gateway_id)
    manager.delete_registry(
            service_account_json, project_id, cloud_region, registry_id)

    out, _ = capsys.readouterr()
    assert 'GATEWAY_ATTACHMENT_ERROR' in out
Ejemplo n.º 21
0
def test_gateway_send_data_for_device(test_topic, capsys):
    gateway_id = device_id_template.format('RS256')
    device_id = device_id_template.format('noauthbind')
    manager.create_registry(
            service_account_json, project_id, cloud_region, pubsub_topic,
            registry_id)
    manager.create_gateway(
            service_account_json, project_id, cloud_region, registry_id,
            None, gateway_id, rsa_cert_path, 'RS256')
    manager.create_device(
            service_account_json, project_id, cloud_region, registry_id,
            device_id)
    manager.bind_device_to_gateway(
            service_account_json, project_id, cloud_region, registry_id,
            device_id, gateway_id)

    # Setup for listening for config messages
    num_messages = 5
    jwt_exp_time = 60
    listen_time = 20

    # Connect the gateway
    cloudiot_mqtt_example.send_data_from_bound_device(
                service_account_json, project_id, cloud_region, registry_id,
                device_id, gateway_id, num_messages, rsa_private_path,
                'RS256', ca_cert_path, mqtt_bridge_hostname, mqtt_bridge_port,
                jwt_exp_time, listen_time)

    # Clean up
    manager.unbind_device_from_gateway(
            service_account_json, project_id, cloud_region, registry_id,
            device_id, gateway_id)
    manager.delete_device(
            service_account_json, project_id, cloud_region, registry_id,
            device_id)
    manager.delete_device(
            service_account_json, project_id, cloud_region, registry_id,
            gateway_id)
    manager.delete_registry(
            service_account_json, project_id, cloud_region, registry_id)

    out, _ = capsys.readouterr()
    assert 'Publishing message 5/5' in out
    assert 'Out of memory' not in out  # Indicates could not connect
def test_gateway_send_data_for_device(test_topic, capsys):
    gateway_id = device_id_template.format('RS256')
    device_id = device_id_template.format('noauthbind')
    manager.create_registry(
            service_account_json, project_id, cloud_region, pubsub_topic,
            registry_id)
    manager.create_gateway(
            service_account_json, project_id, cloud_region, registry_id,
            None, gateway_id, rsa_cert_path, 'RS256')
    manager.create_device(
            service_account_json, project_id, cloud_region, registry_id,
            device_id)
    manager.bind_device_to_gateway(
            service_account_json, project_id, cloud_region, registry_id,
            device_id, gateway_id)

    # Setup for listening for config messages
    num_messages = 5
    jwt_exp_time = 60
    listen_time = 20

    # Connect the gateway
    cloudiot_mqtt_example.send_data_from_bound_device(
                service_account_json, project_id, cloud_region, registry_id,
                device_id, gateway_id, num_messages, rsa_private_path,
                'RS256', ca_cert_path, mqtt_bridge_hostname, mqtt_bridge_port,
                jwt_exp_time, listen_time)

    # Clean up
    manager.unbind_device_from_gateway(
            service_account_json, project_id, cloud_region, registry_id,
            device_id, gateway_id)
    manager.delete_device(
            service_account_json, project_id, cloud_region, registry_id,
            device_id)
    manager.delete_device(
            service_account_json, project_id, cloud_region, registry_id,
            gateway_id)
    manager.delete_registry(
            service_account_json, project_id, cloud_region, registry_id)

    out, _ = capsys.readouterr()
    assert 'Publishing message 5/5' in out
    assert 'Out of memory' not in out  # Indicates could not connect
def test_gateway_listen_for_bound_device_configs(test_topic, capsys):
    gateway_id = device_id_template.format('RS256')
    device_id = device_id_template.format('noauthbind')
    manager.create_registry(
            service_account_json, project_id, cloud_region, pubsub_topic,
            registry_id)
    manager.create_gateway(
            service_account_json, project_id, cloud_region, registry_id,
            None, gateway_id, rsa_cert_path, 'RS256')
    manager.create_device(
            service_account_json, project_id, cloud_region, registry_id,
            device_id)
    manager.bind_device_to_gateway(
            service_account_json, project_id, cloud_region, registry_id,
            device_id, gateway_id)

    # Setup for listening for config messages
    num_messages = 0
    jwt_exp_time = 60
    listen_time = 30

    # Connect the gateway
    cloudiot_mqtt_example.listen_for_messages(
                service_account_json, project_id, cloud_region, registry_id,
                device_id, gateway_id, num_messages, rsa_private_path,
                'RS256', ca_cert_path, mqtt_bridge_hostname, mqtt_bridge_port,
                jwt_exp_time, listen_time, None)

    # Clean up
    manager.unbind_device_from_gateway(
            service_account_json, project_id, cloud_region, registry_id,
            device_id, gateway_id)
    manager.delete_device(
            service_account_json, project_id, cloud_region, registry_id,
            device_id)
    manager.delete_device(
            service_account_json, project_id, cloud_region, registry_id,
            gateway_id)
    manager.delete_registry(
            service_account_json, project_id, cloud_region, registry_id)

    out, _ = capsys.readouterr()
    assert 'Received message' in out
Ejemplo n.º 24
0
def test_gateway_listen_for_bound_device_configs(test_topic, capsys):
    gateway_id = device_id_template.format('RS256')
    device_id = device_id_template.format('noauthbind')
    manager.create_registry(
            service_account_json, project_id, cloud_region, pubsub_topic,
            registry_id)
    manager.create_gateway(
            service_account_json, project_id, cloud_region, registry_id,
            None, gateway_id, rsa_cert_path, 'RS256')
    manager.create_device(
            service_account_json, project_id, cloud_region, registry_id,
            device_id)
    manager.bind_device_to_gateway(
            service_account_json, project_id, cloud_region, registry_id,
            device_id, gateway_id)

    # Setup for listening for config messages
    num_messages = 0
    jwt_exp_time = 60
    listen_time = 30

    # Connect the gateway
    cloudiot_mqtt_example.listen_for_messages(
                service_account_json, project_id, cloud_region, registry_id,
                device_id, gateway_id, num_messages, rsa_private_path,
                'RS256', ca_cert_path, mqtt_bridge_hostname, mqtt_bridge_port,
                jwt_exp_time, listen_time, None)

    # Clean up
    manager.unbind_device_from_gateway(
            service_account_json, project_id, cloud_region, registry_id,
            device_id, gateway_id)
    manager.delete_device(
            service_account_json, project_id, cloud_region, registry_id,
            device_id)
    manager.delete_device(
            service_account_json, project_id, cloud_region, registry_id,
            gateway_id)
    manager.delete_registry(
            service_account_json, project_id, cloud_region, registry_id)

    out, _ = capsys.readouterr()
    assert 'Received message' in out
Ejemplo n.º 25
0
def test_create_gateway(test_topic, capsys):
    gateway_id = device_id_template.format('RS256')
    manager.create_registry(service_account_json, project_id, cloud_region,
                            pubsub_topic, registry_id)

    # TODO: consider adding test for ES256
    manager.create_gateway(service_account_json, project_id, cloud_region,
                           registry_id, None, gateway_id, rsa_cert_path,
                           'RS256')

    # Clean up
    manager.delete_device(service_account_json, project_id, cloud_region,
                          registry_id, gateway_id)
    manager.delete_registry(service_account_json, project_id, cloud_region,
                            registry_id)

    out, _ = capsys.readouterr()

    assert 'Created Gateway' 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
Ejemplo n.º 27
0
def test_add_delete_rs256_device(test_topic, capsys):
    device_id = device_id_template.format('RSA256')
    manager.open_registry(service_account_json, api_key, project_id,
                          cloud_region, pubsub_topic, registry_id)

    manager.create_rs256_device(service_account_json, api_key, project_id,
                                cloud_region, registry_id, device_id,
                                rsa_cert_path)

    manager.get_device(service_account_json, api_key, project_id, cloud_region,
                       registry_id, device_id)

    manager.delete_device(service_account_json, api_key, project_id,
                          cloud_region, registry_id, device_id)

    manager.delete_registry(service_account_json, api_key, project_id,
                            cloud_region, registry_id)

    out, _ = capsys.readouterr()
    assert 'format : RSA_X509_PEM' in out
Ejemplo n.º 28
0
def test_add_delete_unauth_device(test_topic, capsys):
    device_id = device_id_template.format('UNAUTH')
    manager.open_registry(
            service_account_json, api_key, project_id, cloud_region,
            pubsub_topic, registry_id)

    manager.create_unauth_device(
            service_account_json, api_key, project_id, cloud_region,
            registry_id, device_id)

    manager.get_device(
            service_account_json, api_key, project_id, cloud_region,
            registry_id, device_id)

    manager.delete_device(
            service_account_json, api_key, project_id, cloud_region,
            registry_id, device_id)

    out, _ = capsys.readouterr()
    assert 'UNAUTH' in out
def test_create_gateway(test_topic, capsys):
    gateway_id = device_id_template.format('RS256')
    manager.create_registry(
            service_account_json, project_id, cloud_region, pubsub_topic,
            registry_id)

    # TODO: consider adding test for ES256
    manager.create_gateway(
            service_account_json, project_id, cloud_region, registry_id,
            None, gateway_id, rsa_cert_path, 'RS256')

    # Clean up
    manager.delete_device(
            service_account_json, project_id, cloud_region, registry_id,
            gateway_id)
    manager.delete_registry(
            service_account_json, project_id, cloud_region, registry_id)

    out, _ = capsys.readouterr()

    assert 'Created gateway' in out
Ejemplo n.º 30
0
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_send_to_device(capsys):
    manager.create_iot_topic(project_id, topic_id)
    manager.open_registry(
        service_account_json,
        project_id,
        cloud_region,
        pubsub_topic,
        registry_id)

    manager.create_unauth_device(
        service_account_json,
        project_id,
        cloud_region,
        registry_id,
        device_id)

    gcs_to_device.send_to_device(
        gcs_bucket,
        gcs_file_name,
        destination_file_name,
        project_id,
        cloud_region,
        registry_id,
        device_id,
        service_account_json)

    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)

    pubsub_client = pubsub.PublisherClient()
    topic_path = pubsub_client.topic_path(project_id, topic_id)
    pubsub_client.delete_topic(topic_path)

    out, _ = capsys.readouterr()
    assert 'Successfully sent file to device' 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)

    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
Ejemplo n.º 34
0
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_get_state(capsys):
    manager.create_iot_topic(project_id, topic_id)
    manager.open_registry(
        service_account_json,
        project_id,
        cloud_region,
        pubsub_topic,
        registry_id)

    manager.create_unauth_device(
        service_account_json,
        project_id,
        cloud_region,
        registry_id,
        device_id)

    gcs_to_device.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)

    pubsub_client = pubsub.PublisherClient()
    topic_path = pubsub_client.topic_path(project_id, topic_id)
    pubsub_client.delete_topic(topic_path)

    out, _ = capsys.readouterr()
    assert 'Id' in out
    assert 'Config' 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)

    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
Ejemplo n.º 37
0
def test_add_delete_rs256_device(test_topic, capsys):
    device_id = device_id_template.format('RSA256')
    manager.open_registry(
            service_account_json, api_key, project_id, cloud_region,
            pubsub_topic, registry_id)

    manager.create_rs256_device(
            service_account_json, api_key, project_id, cloud_region,
            registry_id, device_id, rsa_cert_path)

    manager.get_device(
            service_account_json, api_key, project_id, cloud_region,
            registry_id, device_id)

    manager.delete_device(
            service_account_json, api_key, project_id, cloud_region,
            registry_id, device_id)

    manager.delete_registry(
            service_account_json, api_key, project_id, cloud_region,
            registry_id)

    out, _ = capsys.readouterr()
    assert 'format : RSA_X509_PEM' in out
def test_bind_device_to_gateway_and_unbind(test_topic, capsys):
    gateway_id = device_id_template.format('RS256')
    device_id = device_id_template.format('noauthbind')
    manager.create_registry(
            service_account_json, project_id, cloud_region, pubsub_topic,
            registry_id)
    manager.create_gateway(
            service_account_json, project_id, cloud_region, registry_id,
            None, gateway_id, rsa_cert_path, 'RS256')

    manager.create_device(
            service_account_json, project_id, cloud_region, registry_id,
            device_id)

    manager.bind_device_to_gateway(
            service_account_json, project_id, cloud_region, registry_id,
            device_id, gateway_id)
    manager.unbind_device_from_gateway(
            service_account_json, project_id, cloud_region, registry_id,
            device_id, gateway_id)

    # Clean up
    manager.delete_device(
            service_account_json, project_id, cloud_region, registry_id,
            device_id)
    manager.delete_device(
            service_account_json, project_id, cloud_region, registry_id,
            gateway_id)
    manager.delete_registry(
            service_account_json, project_id, cloud_region, registry_id)

    out, _ = capsys.readouterr()

    assert 'Device Bound' in out
    assert 'Device unbound' in out
    assert 'HttpError 404' not in out
def test_get_state(capsys):
    manager.create_iot_topic(project_id, topic_id)
    manager.open_registry(service_account_json, project_id, cloud_region,
                          pubsub_topic, registry_id)

    manager.create_unauth_device(service_account_json, project_id,
                                 cloud_region, registry_id, device_id)

    gcs_to_device.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)

    pubsub_client = pubsub.PublisherClient()
    topic_path = pubsub_client.topic_path(project_id, topic_id)
    pubsub_client.delete_topic(topic_path)

    out, _ = capsys.readouterr()
    assert 'Id' in out
    assert 'Config' in out
Ejemplo n.º 40
0
    )

    print("You can read the state messages for your device at this URL:")
    print("\t{}".format(device_url_template).format(registry_id, device_id, project_id))
    try:
        input("Press enter to continue after reading the messages.")
    except SyntaxError:
        pass
    # [END iot_gateway_demo_publish]

    # [START iot_gateway_demo_cleanup]
    # Clean up
    manager.unbind_device_from_gateway(
        service_account_json,
        project_id,
        cloud_region,
        registry_id,
        device_id,
        gateway_id,
    )
    manager.delete_device(
        service_account_json, project_id, cloud_region, registry_id, device_id
    )
    manager.delete_device(
        service_account_json, project_id, cloud_region, registry_id, gateway_id
    )
    manager.delete_registry(service_account_json, project_id, cloud_region, registry_id)
    # [END iot_gateway_demo_cleanup]

# [END iot_gateway_demo]
                service_account_json, project_id, cloud_region, registry_id,
                device_id, gateway_id, num_messages, rsa_private_path, 'RS256',
                ca_cert_path, mqtt_bridge_hostname, mqtt_bridge_port,
                jwt_exp_time, "Hello from gateway_demo.py")

    print('You can read the state messages for your device at this URL:')
    print('\t{}'.format(device_url_template).format(
            registry_id, device_id, project_id))
    try:
        input('Press enter to continue after reading the messages.')
    except SyntaxError:
        pass
    # [END iot_gateway_demo_publish]

    # [START iot_gateway_demo_cleanup]
    # Clean up
    manager.unbind_device_from_gateway(
            service_account_json, project_id, cloud_region, registry_id,
            device_id, gateway_id)
    manager.delete_device(
            service_account_json, project_id, cloud_region, registry_id,
            device_id)
    manager.delete_device(
            service_account_json, project_id, cloud_region, registry_id,
            gateway_id)
    manager.delete_registry(
            service_account_json, project_id, cloud_region, registry_id)
    # [END iot_gateway_demo_cleanup]

# [END iot_gateway_demo]