Esempio n. 1
0
def test_create_delete_tables():
    settings = StorageClientSettings(service_uri="bk://localhost:4181")
    client = admin.Client(storage_client_settings=settings)
    ns_name = "test_create_delete_tables_%s" % uuid.uuid4().hex
    ns_props = client.namespaces().create(ns_name)
    __logger__.info("Created namespace '%s' : %s", ns_name, ns_props)
    ns = client.namespace(ns_name)

    table_name = "table_%s" % uuid.uuid4().hex
    # test create, delete and get tables
    with pytest.raises(StreamNotFoundError):
        __logger__.info("getting non-existent table '%s'", table_name)
        ns.get(table_name)
    __logger__.info("creating table '%s'", table_name)
    table_props = ns.create(table_name)
    __logger__.info("created table '%s' : %s", table_name, table_props)
    __logger__.info("getting table '%s'", table_name)
    read_tbl_props = ns.get(table_name)
    __logger__.info("got table '%s' : %s", table_name, read_tbl_props)
    assert table_props == read_tbl_props
    with pytest.raises(StreamExistsError):
        __logger__.info("creating existed table '%s'", table_name)
        ns.create(table_name)
    __logger__.info("deleting existed table '%s'", table_name)
    ns.delete(table_name)
    with pytest.raises(StreamNotFoundError):
        ns.get(table_name)
    with pytest.raises(StreamNotFoundError):
        ns.delete(table_name)
    __logger__.info("end of test_create_delete_tables")
Esempio n. 2
0
 def __init__(self, state_storage_serviceurl, table_ns, table_name):
     client_settings = StorageClientSettings(
         service_uri=state_storage_serviceurl)
     admin_client = admin.client.Client(
         storage_client_settings=client_settings)
     # create namespace and table if needed
     ns = admin_client.namespace(table_ns)
     try:
         ns.get(stream_name=table_name)
     except NamespaceNotFoundError:
         admin_client.namespaces().create(namespace=table_ns)
         # TODO: make number of table ranges configurable
         table_conf = new_bk_table_conf(1)
         ns.create(
             stream_name=table_name,
             stream_config=table_conf)
     except StreamNotFoundError:
         # TODO: make number of table ranges configurable
         table_conf = new_bk_table_conf(1)
         ns.create(
             stream_name=table_name,
             stream_config=table_conf)
     self.__client__ = kv.Client(storage_client_settings=client_settings,
                                 namespace=table_ns)
     self.__table__ = self.__client__.table(table_name=table_name)
Esempio n. 3
0
def test_get_kv_from_table_updated_by_java_client():
    settings = StorageClientSettings(service_uri="bk://localhost:4181")
    ns = "default"
    tbl_name = "test-java-updates"
    kv_client = kv.Client(storage_client_settings=settings, namespace=ns)
    tbl = kv_client.table(tbl_name)

    for x in range(0, 20):
        expected_key = "java-key-%s" % x
        read_kv = tbl.get_str(expected_key)
        expected_value = "java-value-%s" % x
        assert read_kv.key == str.encode(expected_key, 'utf-8')
        assert read_kv.is_number is False
        assert read_kv.value == str.encode(expected_value, 'utf-8')
        assert read_kv.version == 0
Esempio n. 4
0
def test_create_delete_namespaces():
    settings = StorageClientSettings(service_uri="bk://localhost:4181")
    client = admin.Client(storage_client_settings=settings)
    ns = "test_create_delete_namespaces_%s" % uuid.uuid4().hex
    with pytest.raises(NamespaceNotFoundError):
        __logger__.info("getting non-existent namespace '%s'", ns)
        client.namespaces().get(ns)
    __logger__.info("creating namespace '%s'", ns)
    ns_props = client.namespaces().create(ns)
    __logger__.info("created namespace '%s' : %s", ns, ns_props)
    __logger__.info("getting namespace '%s'", ns)
    read_ns_props = client.namespaces().get(ns)
    __logger__.info("got namespace '%s' : %s", ns, read_ns_props)
    assert ns_props == read_ns_props
    with pytest.raises(NamespaceExistsError):
        __logger__.info("creating existed namespace '%s'", ns)
        client.namespaces().create(ns)
    __logger__.info("deleting existed namespace '%s'", ns)
    client.namespaces().delete(ns)
    with pytest.raises(NamespaceNotFoundError):
        client.namespaces().get(ns)
    with pytest.raises(NamespaceNotFoundError):
        client.namespaces().delete(ns)
    __logger__.info("end of test_create_delete_namespace")
Esempio n. 5
0
def test_kv_ops():
    settings = StorageClientSettings(service_uri="bk://localhost:4181")
    admin_client = admin.Client(storage_client_settings=settings)
    ns = "test_kv_ops_%s" % uuid.uuid4().hex
    ns_props = admin_client.namespaces().create(ns)
    __logger__.info("Created namespace '%s' : %s", ns, ns_props)
    tbl_name = "test_kv_ops_table"
    tbl_props = admin_client.namespace(ns).create(tbl_name)
    __logger__.info("Created table '%s' at namespace '%s' : %s", tbl_name, ns,
                    tbl_props)

    kv_client = kv.Client(storage_client_settings=settings, namespace=ns)
    tbl = kv_client.table(tbl_name)

    for x in range(0, 20):
        read_val = tbl.get_str("key-%s" % x)
        assert read_val is None

    for x in range(0, 20):
        tbl.put_str("key-%s" % x, "value-%s" % x)

    for x in range(0, 20):
        read_kv = tbl.get_str("key-%s" % x)
        expected_key = "key-%s" % x
        expected_value = "value-%s" % x
        assert read_kv.key == str.encode(expected_key, 'utf-8')
        assert read_kv.is_number is False
        assert read_kv.value == str.encode(expected_value, 'utf-8')
        assert read_kv.version == 0

    for x in range(0, 20):
        try:
            tbl.incr_str("key-%s" % x, 20)
            assert False
        except BadRequest as e:
            assert e.grpc_status_code == grpc.StatusCode.FAILED_PRECONDITION
            assert e.bk_status_code == storage_pb2.BAD_REQUEST

    for x in range(0, 20):
        read_val = tbl.get_str("counter-%s" % x)
        assert read_val is None

    for x in range(0, 20):
        tbl.incr_str("counter-%s" % x, (x + 1))

    for x in range(0, 20):
        read_kv = tbl.get_str("counter-%s" % x)
        expected_key = "counter-%s" % x
        expected_num = (x + 1)
        assert read_kv.key == str.encode(expected_key, 'utf-8')
        assert read_kv.is_number is True
        assert read_kv.number_value == expected_num
        assert read_kv.version == 0

    for x in range(0, 20):
        try:
            tbl.put_str("counter-%s" % x, "value-%s" % x)
            assert False
        except BadRequest as e:
            assert e.grpc_status_code == grpc.StatusCode.FAILED_PRECONDITION
            assert e.bk_status_code == storage_pb2.BAD_REQUEST

    for x in range(0, 20):
        tbl.delete_str("key-%s" % x)
        read_val = tbl.get_str("key-%s" % x)
        assert read_val is None

    for x in range(0, 20):
        tbl.delete_str("counter-%s" % x)
        read_val = tbl.get_str("counter-%s" % x)
        assert read_val is None