def test_bind_device_to_gateway_and_unbind(iot_topic, capsys):
    gateway_id = device_id_template.format('RS256')
    device_id = device_id_template.format('noauthbind')
    create_registry(service_account_json, project_id, cloud_region,
                    pubsub_topic, registry_id)
    gateway.create_gateway(service_account_json, project_id, cloud_region,
                           registry_id, None, gateway_id, rsa_cert_path,
                           'RS256')

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

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

    # Clean up
    delete_device(service_account_json, project_id, cloud_region, registry_id,
                  device_id)
    delete_device(service_account_json, project_id, cloud_region, registry_id,
                  gateway_id)
    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_gateway_trigger_error_topic(iot_topic, capsys):
    gateway_id = device_id_template.format('RS256-err')
    device_id = device_id_template.format('noauthbind')
    create_registry(
            service_account_json, project_id, cloud_region, pubsub_topic,
            registry_id)
    gateway.create_gateway(
            service_account_json, project_id, cloud_region, registry_id,
            None, gateway_id, rsa_cert_path, 'RS256')
    gateway.create_device(
            service_account_json, project_id, cloud_region, registry_id,
            device_id)
    gateway.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
    jwt_exp_time = 30
    listen_time = 5

    # Callback for causing an error
    def trigger_error(client):
        gateway.attach_device(client, 'invalid_device_id')

    # Connect the gateway
    # Connect the gateway
    gateway.listen_for_config_and_error_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, trigger_error)

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

    out, _ = capsys.readouterr()
    assert 'Received ERROR message' in out
    assert 'Out of memory' not in out  # Indicates could not connect
def test_gateway_send_data_for_device(iot_topic, capsys):
    gateway_id = device_id_template.format('RS256')
    device_id = device_id_template.format('noauthbind')
    create_registry(
            service_account_json, project_id, cloud_region, pubsub_topic,
            registry_id)
    gateway.create_gateway(
            service_account_json, project_id, cloud_region, registry_id,
            None, gateway_id, rsa_cert_path, 'RS256')
    gateway.create_device(
            service_account_json, project_id, cloud_region, registry_id,
            device_id)
    gateway.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
    gateway.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
    gateway.unbind_device_from_gateway(
            service_account_json, project_id, cloud_region, registry_id,
            device_id, gateway_id)
    delete_device(
            service_account_json, project_id, cloud_region, registry_id,
            device_id)
    delete_device(
            service_account_json, project_id, cloud_region, registry_id,
            gateway_id)
    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(iot_topic, capsys):
    gateway_id = device_id_template.format('RS256')
    device_id = device_id_template.format('noauthbind')
    create_registry(
            service_account_json, project_id, cloud_region, pubsub_topic,
            registry_id)
    gateway.create_gateway(
            service_account_json, project_id, cloud_region, registry_id,
            None, gateway_id, rsa_cert_path, 'RS256')
    gateway.create_device(
            service_account_json, project_id, cloud_region, registry_id,
            device_id)
    gateway.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
    gateway.listen_for_config_and_error_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
    gateway.unbind_device_from_gateway(
            service_account_json, project_id, cloud_region, registry_id,
            device_id, gateway_id)
    delete_device(
            service_account_json, project_id, cloud_region, registry_id,
            device_id)
    delete_device(
            service_account_json, project_id, cloud_region, registry_id,
            gateway_id)
    delete_registry(
            service_account_json, project_id, cloud_region, registry_id)

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

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

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

    # Clean up
    delete_device(
            service_account_json, project_id, cloud_region, registry_id,
            device_id)
    delete_device(
            service_account_json, project_id, cloud_region, registry_id,
            gateway_id)
    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 '400' not in out
                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
    gateway.unbind_device_from_gateway(
            service_account_json, project_id, cloud_region, registry_id,
            device_id, gateway_id)
    delete_device(
            service_account_json, project_id, cloud_region, registry_id,
            device_id)
    delete_device(
            service_account_json, project_id, cloud_region, registry_id,
            gateway_id)
    delete_registry(
            service_account_json, project_id, cloud_region, registry_id)
    # [END iot_gateway_demo_cleanup]

# [END iot_gateway_demo]
Example #7
0
                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
    gateway.unbind_device_from_gateway(
            service_account_json, project_id, cloud_region, registry_id,
            device_id, gateway_id)
    delete_device(
            service_account_json, project_id, cloud_region, registry_id,
            device_id)
    delete_device(
            service_account_json, project_id, cloud_region, registry_id,
            gateway_id)
    delete_registry(
            service_account_json, project_id, cloud_region, registry_id)
    # [END iot_gateway_demo_cleanup]

# [END iot_gateway_demo]