Ejemplo n.º 1
0
def test_gateway_listen_for_bound_device_configs(test_topic, test_registry_id,
                                                 device_and_gateways,
                                                 capsys):  # noqa
    (device_id, gateway_id, _) = device_and_gateways

    # 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,
        test_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,
    )

    out, _ = capsys.readouterr()
    assert "Received message" in out
Ejemplo n.º 2
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
Ejemplo n.º 3
0
def test_gateway_trigger_error_topic(test_topic, test_registry_id,
                                     device_and_gateways, capsys):  # noqa
    (device_id, _, gateway_id) = device_and_gateways

    # 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,
        test_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,
        test_registry_id,
        device_id,
        gateway_id,
        num_messages,
        rsa_private_path,
        "RS256",
        ca_cert_path,
        "mqtt.googleapis.com",
        8883,
        20,
        15,
        trigger_error,
    )

    out, _ = capsys.readouterr()
    assert "GATEWAY_ATTACHMENT_ERROR" 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
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.º 6
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
                with open(log_path, 'w') as csvfile:
                    logwriter = csv.writer(csvfile, dialect='excel')
                    logwriter.writerow(['time', 'topic', 'data'])

            with open(log_path, 'a') as csvfile:
                logwriter = csv.writer(csvfile, dialect='excel')
                logwriter.writerow([
                        datetime.datetime.now().isoformat(),
                        message.topic,
                        message.payload])

        client.on_message = log_on_message

    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, log_callback)
    # [END iot_gateway_demo_listen]

    # [START iot_gateway_demo_publish]
    print('Publishing messages demo')
    print('Publishing: {} messages'.format(num_messages))
    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, "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(
Ejemplo n.º 8
0
                        message.topic,
                        message.payload,
                    ]
                )

        client.on_message = log_on_message

    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,
        log_callback,
    )
    # [END iot_gateway_demo_listen]

    # [START iot_gateway_demo_publish]
    print("Publishing messages demo")
    print("Publishing: {} messages".format(num_messages))
    cloudiot_mqtt_example.send_data_from_bound_device(
        service_account_json,