def test_resource_client_create_folder():
    config = load_config('local.yaml')
    resource_client = ResourceClient(config)
    path = config.server_test_path + '/folder%06d/folder%06d' % (
        random.randint(1, 999999), random.randint(1, 999999))
    resource_client.create_folder(path)
    assert resource_client.file_exists(path) == True
def test_resource_client_read_write_file():
    for new_version in [False]:
        config = load_config('local.yaml')
        resource_client = ResourceClient(config)
        contents = 'This is a test.\n%d.\n' % random.randint(1, 1000)
        resource_client.write_file(config.server_test_path + '/test.txt',
                                   contents,
                                   new_version=new_version)
        server_contents = resource_client.read_file(config.server_test_path +
                                                    '/test.txt').decode()
        assert contents == server_contents
def test_resource_client_send_message():

    # prepare to send messages
    config = load_config('local.yaml')
    resource_client = ResourceClient(config)
    params = {
        'abc': 'test',
        'xyz': 777,
    }

    # check sending message to valid testing folder
    resource_client.send_message(config.server_test_path, 'testMessage',
                                 params)

    # check non-existent path
    try:
        resource_client.send_message('/path/does/not/exist', 'testMessage',
                                     params)
        assert False
    except ApiError as e:
        assert e.status == 404

    # check non-authorized path
    try:
        resource_client.send_message('/system', 'testMessage', params)
        assert False
    except ApiError as e:
        assert e.status == 403
def test_resource_client_read_write_large_file():
    for binary in [False, True]:
        config = load_config('local.yaml')
        resource_client = ResourceClient(config)
        if binary:
            contents = bytearray(list(range(256)) * 100)
        else:
            contents = ('This is a test.\n%d.\n' %
                        random.randint(1, 1000)) * 1000
        resource_client.write_file(config.server_test_path + '/testLarge.txt',
                                   contents)
        data = resource_client.read_file(config.server_test_path +
                                         '/testLarge.txt')
        if not binary:
            data = data.decode()
        assert contents == data
def _test_send_image():
    r = random.randint(0, 255)
    g = random.randint(0, 255)
    b = random.randint(0, 255)
    width = 320
    height = 240
    image = Image.new('RGB', (width, height))
    pixel_data = image.load()
    for y in range(height):
        for x in range(width):
            pixel_data[x, y] = (r, g, b)
    contents = encode_image(image)
    c.sequences.update('image', contents, use_websocket=False)
    logging.info('updated image (%d, %d, %d)' % (r, g, b))
    gevent.sleep(2)  # wait so outbound messages are sent

    # read back using resource client
    if False:
        resource_client = ResourceClient(c.config)
        contents = resource_client.read_file(c.path_on_server() + '/image')
        open('test.jpg', 'w').write(contents)
def test_update_sequence():
    v = random.randint(1, 100)
    test_val1 = 'foo-%d' % v
    test_val2 = 'bar-%d' % v
    c.sequences.update(c.path_on_server() + '/test',
                       test_val1,
                       use_websocket=True)
    c.sequences.update(c.path_on_server() + '/testIntSeq',
                       v,
                       use_websocket=True)
    c.sequences.update('testFloatSeq', v + 0.5, use_websocket=True)
    c.sequences.update('folder/testSub', test_val2, use_websocket=True)
    logging.info('updated sequences (%d)' % v)
    gevent.sleep(10)  # wait so outbound messages are sent

    # read back using resource client
    if True:
        resource_client = ResourceClient(c.config)
        assert test_val1 == resource_client.read_file(c.path_on_server() +
                                                      '/test').decode()
        assert test_val2 == resource_client.read_file(
            c.path_on_server() + '/folder/testSub').decode()
        assert v == int(
            resource_client.read_file(c.path_on_server() +
                                      '/testIntSeq').decode())
        assert round(v + 0.5, 2) == round(
            float(
                resource_client.read_file(c.path_on_server() +
                                          '/testFloatSeq').decode()), 2)
def test_resource_client_read_write_sequence():
    for new_version in [False, False]:
        config = load_config('local.yaml')
        resource_client = ResourceClient(config)
        resource_client.write_file(config.server_test_path + '/test-text-seq',
                                   'ok',
                                   new_version=new_version)
        assert 'ok' == resource_client.read_file(config.server_test_path +
                                                 '/test-text-seq').decode()
        resource_client.write_file(config.server_test_path + '/test-text-seq',
                                   'test',
                                   new_version=new_version)
        assert 'test' == resource_client.read_file(config.server_test_path +
                                                   '/test-text-seq').decode()
def test_update_multi():
    for use_message in [False, True]:
        v = random.randint(1, 100)
        test_val1 = 'foo-%d' % v
        test_val2 = 'bar-%d' % v
        seq_values = {
            'test': test_val1,
            'testIntSeq': v,
            'folder/testSub': test_val2,
            'testFloatSeq': v + 0.5
        }
        c.sequences.update_multiple(seq_values, use_message=use_message)
        logging.info('updated multi sequences (%d)' % v)
        gevent.sleep(5)  # wait so outbound messages are sent
        resource_client = ResourceClient(c.config)
        assert test_val1 == resource_client.read_file(c.path_on_server() +
                                                      '/test').decode()
        assert test_val2 == resource_client.read_file(
            c.path_on_server() + '/folder/testSub').decode()
        assert v == int(
            resource_client.read_file(c.path_on_server() +
                                      '/testIntSeq').decode())
        assert round(v + 0.5, 2) == round(
            float(
                resource_client.read_file(c.path_on_server() +
                                          '/testFloatSeq').decode()), 2)
def test_resource_client_file_exists():
    config = load_config('local.yaml')
    resource_client = ResourceClient(config)
    assert resource_client.file_exists(config.server_test_path + '/test.txt')
    assert not resource_client.file_exists(config.server_test_path +
                                           '/test12345.txt')
Beispiel #10
0
def test_folder():
    config = load_config('local.yaml')
    resource_client = ResourceClient(config)