コード例 #1
0
ファイル: manager_test.py プロジェクト: othercamb/GCP
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
コード例 #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
コード例 #3
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
コード例 #4
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_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_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
コード例 #7
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
コード例 #8
0
ファイル: manager_test.py プロジェクト: othercamb/GCP
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_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
コード例 #10
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
コード例 #11
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
コード例 #12
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
コード例 #13
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
コード例 #14
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
コード例 #15
0
num_messages = 15
jwt_exp_time = 20
listen_time = 30


if __name__ == '__main__':
    print("Running demo")

    gateway_id = device_id_template.format('RS256')
    device_id = device_id_template.format('noauthbind')

    # [START iot_gateway_demo_create_registry]
    print('Creating registry: {}'.format(registry_id))
    manager.create_registry(
            service_account_json, project_id, cloud_region, pubsub_topic,
            registry_id)
    # [END iot_gateway_demo_create_registry]

    # [START iot_gateway_demo_create_gateway]
    print('Creating gateway: {}'.format(gateway_id))
    manager.create_gateway(
            service_account_json, project_id, cloud_region, registry_id,
            None, gateway_id, rsa_cert_path, 'RS256')
    # [END iot_gateway_demo_create_gateway]

    # [START iot_gateway_demo_create_bound_device]
    print('Creating device to bind: {}'.format(device_id))
    manager.create_device(
            service_account_json, project_id, cloud_region, registry_id,
            device_id)
コード例 #16
0
num_messages = 15
jwt_exp_time = 20
listen_time = 30


if __name__ == "__main__":
    print("Running demo")

    gateway_id = device_id_template.format("RS256")
    device_id = device_id_template.format("noauthbind")

    # [START iot_gateway_demo_create_registry]
    print("Creating registry: {}".format(registry_id))
    manager.create_registry(
        service_account_json, project_id, cloud_region, pubsub_topic, registry_id
    )
    # [END iot_gateway_demo_create_registry]

    # [START iot_gateway_demo_create_gateway]
    print("Creating gateway: {}".format(gateway_id))
    manager.create_gateway(
        service_account_json,
        project_id,
        cloud_region,
        registry_id,
        None,
        gateway_id,
        rsa_cert_path,
        "RS256",
    )