Skip to content

Commit f612f9d

Browse files
committed
add tls test for python sdk KIKIMR-13478
ref:8861850 sync: https://proxy.sandbox.yandex-team.ru/2578565827
1 parent aacacb0 commit f612f9d

File tree

8 files changed

+104
-40
lines changed

8 files changed

+104
-40
lines changed

docker-compose.yml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
version: "3.9"
22
services:
33
ydb:
4-
image: registry.yandex.net/yandex-docker-local-ydb:stable
4+
image: cr.yandex/yc/yandex-docker-local-ydb:latest
55
restart: always
66
ports:
7-
- 2135:2135
87
- 2136:2136
9-
hostname: 0.0.0.0
8+
- 2135:2135
9+
hostname: localhost
10+
volumes:
11+
- ./ydb_certs:/ydb_certs
1012
environment:
1113
- YDB_USE_IN_MEMORY_PDISKS=true

kikimr/public/sdk/python/tests/aio/test_async_iter_stream.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,19 @@ async def test_read_table(driver, database):
1515
)
1616

1717
session = await driver.table_client.session().create()
18-
await session.create_table(database + "/some_table", description)
18+
await session.create_table(database + "/test_read_table", description)
1919

2020
await session.transaction(ydb.SerializableReadWrite()).execute(
21-
"""INSERT INTO `some_table` (`key1`, `value`) VALUES (1, "hello_world"), (2, "2")""",
21+
"""INSERT INTO `test_read_table` (`key1`, `value`) VALUES (1, "hello_world"), (2, "2")""",
2222
commit_tx=True,
2323
)
2424

2525
expected_res = [[{"key1": 1, "value": "hello_world"}], [{"key1": 2, "value": "2"}]]
2626

2727
i = 0
28-
async for resp in await session.read_table(database + "/some_table", row_limit=1):
28+
async for resp in await session.read_table(
29+
database + "/test_read_table", row_limit=1
30+
):
2931
assert resp.rows == expected_res[i]
3032
i += 1
3133

kikimr/public/sdk/python/tests/aio/test_connection_pool.py

Lines changed: 28 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -29,32 +29,6 @@ async def test_other_credentials(endpoint, database):
2929
await driver.stop()
3030

3131

32-
@pytest.mark.asyncio
33-
async def test_disconnect_by_call(endpoint, database, docker_project):
34-
driver_config = ydb.DriverConfig(
35-
endpoint,
36-
database,
37-
credentials=ydb.construct_credentials_from_environ(),
38-
root_certificates=ydb.load_ydb_root_certificate(),
39-
)
40-
41-
driver = Driver(driver_config=driver_config)
42-
43-
await driver.wait(timeout=10)
44-
45-
docker_project.stop()
46-
47-
try:
48-
await driver.scheme_client.make_directory("/local/lol")
49-
except Exception:
50-
pass
51-
52-
await asyncio.sleep(5)
53-
assert len(driver._store.connections) == 0
54-
docker_project.start()
55-
await driver.stop()
56-
57-
5832
@pytest.mark.asyncio
5933
async def test_session(endpoint, database):
6034
driver_config = ydb.DriverConfig(
@@ -92,9 +66,9 @@ async def test_session(endpoint, database):
9266
)
9367

9468
session = await driver.table_client.session().create()
95-
await session.create_table(database + "/some_table", description)
69+
await session.create_table(database + "/test_session", description)
9670

97-
response = await session.describe_table(database + "/some_table")
71+
response = await session.describe_table(database + "/test_session")
9872
assert [c.name for c in response.columns] == ["key1", "key2", "value"]
9973
await driver.stop()
10074

@@ -126,6 +100,32 @@ async def restart_docker():
126100
await driver.stop()
127101

128102

103+
@pytest.mark.asyncio
104+
async def test_disconnect_by_call(endpoint, database, docker_project):
105+
driver_config = ydb.DriverConfig(
106+
endpoint,
107+
database,
108+
credentials=ydb.construct_credentials_from_environ(),
109+
root_certificates=ydb.load_ydb_root_certificate(),
110+
)
111+
112+
driver = Driver(driver_config=driver_config)
113+
114+
await driver.wait(timeout=10)
115+
116+
docker_project.stop()
117+
118+
try:
119+
await driver.scheme_client.make_directory("/local/lol")
120+
except Exception:
121+
pass
122+
123+
await asyncio.sleep(5)
124+
assert len(driver._store.connections) == 0
125+
docker_project.start()
126+
await driver.stop()
127+
128+
129129
@pytest.mark.asyncio
130130
async def test_good_discovery_interval(driver):
131131
await driver.wait(timeout=10)

kikimr/public/sdk/python/tests/conftest.py

Lines changed: 51 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,69 @@
11
import os
22
import pytest
33
import ydb
4+
import time
5+
import shutil
46

57

68
@pytest.fixture(scope="module")
79
def docker_compose_file(pytestconfig):
810
return os.path.join(str(pytestconfig.rootdir), "docker-compose.yml")
911

1012

13+
def wait_container_ready(driver):
14+
driver.wait(timeout=10)
15+
16+
with ydb.SessionPool(driver) as pool:
17+
18+
started_at = time.time()
19+
while time.time() - started_at < 30:
20+
try:
21+
with pool.checkout() as session:
22+
session.execute_scheme(
23+
"create table `.sys_health/test_table` (A int32, primary key(A));"
24+
)
25+
26+
return True
27+
28+
except ydb.Error as e:
29+
time.sleep(1)
30+
31+
raise RuntimeError("Container is not ready after timeout.")
32+
33+
1134
@pytest.fixture(scope="module")
12-
def endpoint(module_scoped_container_getter):
13-
service = module_scoped_container_getter.get("ydb").network_info[0]
14-
return service.hostname + ":" + service.host_port
35+
def endpoint(pytestconfig, module_scoped_container_getter):
36+
with ydb.Driver(endpoint="localhost:2136", database="/local") as driver:
37+
wait_container_ready(driver)
38+
yield "localhost:2136"
39+
40+
shutil.rmtree(os.path.join(str(pytestconfig.rootdir), "ydb_certs"))
41+
42+
43+
@pytest.fixture(scope="module")
44+
def secure_endpoint(pytestconfig, module_scoped_container_getter):
45+
ca_path = os.path.join(str(pytestconfig.rootdir), "ydb_certs/ca.pem")
46+
iterations = 0
47+
while not os.path.exists(ca_path) and iterations < 10:
48+
time.sleep(1)
49+
iterations += 1
50+
51+
assert os.path.exists(ca_path)
52+
os.environ["YDB_SSL_ROOT_CERTIFICATES_FILE"] = ca_path
53+
with ydb.Driver(
54+
endpoint="grpcs://localhost:2135",
55+
database="/local",
56+
root_certificates=ydb.load_ydb_root_certificate(),
57+
) as driver:
58+
wait_container_ready(driver)
59+
yield "localhost:2135"
60+
61+
shutil.rmtree(os.path.join(str(pytestconfig.rootdir), "ydb_certs"))
1562

1663

1764
@pytest.fixture(scope="module")
1865
def database():
19-
return "local"
66+
return "/local"
2067

2168

2269
@pytest.fixture()

kikimr/public/sdk/python/tests/ssl/__init__.py

Whitespace-only changes.
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# -*- coding: utf-8 -*-
2+
import ydb
3+
4+
5+
def test_connect_secure(secure_endpoint, database):
6+
with ydb.Driver(
7+
endpoint="grpcs://localhost:2135",
8+
database="/local",
9+
root_certificates=ydb.load_ydb_root_certificate(),
10+
) as driver:
11+
driver.wait(timeout=10)
12+
driver.scheme_client.make_directory("/local/test")

tox.ini

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ deps =
1616

1717
[testenv:py38]
1818
commands =
19-
pytest --docker-compose=docker-compose.yml {posargs}
19+
pytest --docker-compose=docker-compose.yml --docker-compose-remove-volumes {posargs}
2020

2121
[testenv:black]
2222
skip_install = true

ydb/_session_impl.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -383,7 +383,8 @@ def read_table_request_factory(
383383
for column in columns:
384384
request.columns.append(column)
385385
if row_limit:
386-
request.row_limit = row_limit
386+
# NOTE(gvit): pylint cannot understand that row_limit is not None
387+
request.row_limit = row_limit # pylint: disable=E5903
387388
if use_snapshot is not None:
388389
if isinstance(use_snapshot, bool):
389390
if use_snapshot:

0 commit comments

Comments
 (0)