Skip to content

Commit 05872bc

Browse files
committed
seperated tests dirs, split conftests
Signed-off-by: Lior Sventitzky <[email protected]>
1 parent 55bf629 commit 05872bc

14 files changed

+463
-420
lines changed

python/tests/async_tests/conftest.py

Lines changed: 208 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,208 @@
1+
# Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0
2+
3+
from typing import AsyncGenerator, List, Optional, Union
4+
5+
import pytest
6+
from glide.config import (
7+
BackoffStrategy,
8+
GlideClientConfiguration,
9+
GlideClusterClientConfiguration,
10+
NodeAddress,
11+
ProtocolVersion,
12+
ReadFrom,
13+
ServerCredentials,
14+
)
15+
from glide.exceptions import ClosingError
16+
from glide.glide_client import GlideClient, GlideClusterClient, TGlideClient
17+
from glide.logger import Logger
18+
from glide.routes import AllNodes
19+
20+
from tests.utils.cluster import ValkeyCluster
21+
from tests.utils.utils import (
22+
DEFAULT_TEST_LOG_LEVEL,
23+
INITIAL_PASSWORD,
24+
NEW_PASSWORD,
25+
USERNAME,
26+
create_client_config,
27+
set_new_acl_username_with_password,
28+
)
29+
30+
Logger.set_logger_config(DEFAULT_TEST_LOG_LEVEL)
31+
32+
33+
@pytest.fixture(scope="function")
34+
async def glide_client(
35+
request,
36+
cluster_mode: bool,
37+
protocol: ProtocolVersion,
38+
) -> AsyncGenerator[TGlideClient, None]:
39+
"Get async socket client for tests"
40+
client = await create_client(
41+
request, cluster_mode, protocol=protocol, request_timeout=5000
42+
)
43+
yield client
44+
await test_teardown(request, cluster_mode, protocol)
45+
await client.close()
46+
47+
48+
@pytest.fixture(scope="function")
49+
async def management_client(
50+
request,
51+
cluster_mode: bool,
52+
protocol: ProtocolVersion,
53+
) -> AsyncGenerator[TGlideClient, None]:
54+
"Get async socket client for tests, used to manage the state when tests are on the client ability to connect"
55+
client = await create_client(request, cluster_mode, protocol=protocol)
56+
yield client
57+
await test_teardown(request, cluster_mode, protocol)
58+
await client.close()
59+
60+
61+
@pytest.fixture(scope="function")
62+
async def acl_glide_client(
63+
request,
64+
cluster_mode: bool,
65+
protocol: ProtocolVersion,
66+
management_client: TGlideClient,
67+
) -> AsyncGenerator[TGlideClient, None]:
68+
"""
69+
Client fot tests that use a server pre-configured with an ACL user.
70+
This function first uses the management client to register the USERNAME with INITIAL_PASSWORD,so that
71+
the client would be ablt to connect.
72+
It then returns a client with this USERNAME and INITIAL_PASSWORD already set as its ServerCredentials.
73+
"""
74+
75+
await set_new_acl_username_with_password(
76+
management_client, USERNAME, INITIAL_PASSWORD
77+
)
78+
79+
client = await create_client(
80+
request,
81+
cluster_mode,
82+
protocol=protocol,
83+
credentials=ServerCredentials(username=USERNAME, password=INITIAL_PASSWORD),
84+
)
85+
yield client
86+
await test_teardown(request, cluster_mode, protocol)
87+
await client.close()
88+
89+
90+
async def create_client(
91+
request,
92+
cluster_mode: bool,
93+
credentials: Optional[ServerCredentials] = None,
94+
database_id: int = 0,
95+
addresses: Optional[List[NodeAddress]] = None,
96+
client_name: Optional[str] = None,
97+
protocol: ProtocolVersion = ProtocolVersion.RESP3,
98+
request_timeout: Optional[int] = 1000,
99+
connection_timeout: Optional[int] = 1000,
100+
cluster_mode_pubsub: Optional[
101+
GlideClusterClientConfiguration.PubSubSubscriptions
102+
] = None,
103+
standalone_mode_pubsub: Optional[
104+
GlideClientConfiguration.PubSubSubscriptions
105+
] = None,
106+
inflight_requests_limit: Optional[int] = None,
107+
read_from: ReadFrom = ReadFrom.PRIMARY,
108+
client_az: Optional[str] = None,
109+
reconnect_strategy: Optional[BackoffStrategy] = None,
110+
valkey_cluster: Optional[ValkeyCluster] = None,
111+
) -> Union[GlideClient, GlideClusterClient]:
112+
config = create_client_config(
113+
request,
114+
cluster_mode,
115+
credentials,
116+
database_id,
117+
addresses,
118+
client_name,
119+
protocol,
120+
request_timeout,
121+
connection_timeout,
122+
cluster_mode_pubsub,
123+
standalone_mode_pubsub,
124+
inflight_requests_limit,
125+
read_from,
126+
client_az,
127+
reconnect_strategy,
128+
valkey_cluster,
129+
)
130+
if cluster_mode:
131+
return await GlideClusterClient.create(config)
132+
else:
133+
return await GlideClient.create(config)
134+
135+
136+
async def auth_client(client: TGlideClient, password: str, username: str = "default"):
137+
"""
138+
Authenticates the given TGlideClient server connected. If no username is provided, uses the 'default' user.
139+
"""
140+
if isinstance(client, GlideClient):
141+
return await client.custom_command(["AUTH", username, password])
142+
elif isinstance(client, GlideClusterClient):
143+
return await client.custom_command(
144+
["AUTH", username, password], route=AllNodes()
145+
)
146+
147+
148+
async def config_set_new_password(client: TGlideClient, password):
149+
"""
150+
Sets a new password for the given TGlideClient server connected.
151+
This function updates the server to require a new password.
152+
"""
153+
if isinstance(client, GlideClient):
154+
await client.config_set({"requirepass": password})
155+
elif isinstance(client, GlideClusterClient):
156+
await client.config_set({"requirepass": password}, route=AllNodes())
157+
158+
159+
async def kill_connections(client: TGlideClient):
160+
"""
161+
Kills all connections to the given TGlideClient server connected.
162+
"""
163+
if isinstance(client, GlideClient):
164+
await client.custom_command(["CLIENT", "KILL", "TYPE", "normal"])
165+
elif isinstance(client, GlideClusterClient):
166+
await client.custom_command(
167+
["CLIENT", "KILL", "TYPE", "normal"], route=AllNodes()
168+
)
169+
170+
171+
async def test_teardown(request, cluster_mode: bool, protocol: ProtocolVersion):
172+
"""
173+
Perform teardown tasks such as flushing all data from the cluster.
174+
175+
If authentication is required, attempt to connect with the known password,
176+
reset it back to empty, and proceed with teardown.
177+
"""
178+
credentials = None
179+
try:
180+
# Try connecting without credentials
181+
client = await create_client(
182+
request, cluster_mode, protocol=protocol, request_timeout=2000
183+
)
184+
await client.custom_command(["FLUSHALL"])
185+
await client.close()
186+
except ClosingError as e:
187+
# Check if the error is due to authentication
188+
if "NOAUTH" in str(e):
189+
# Use the known password to authenticate
190+
credentials = ServerCredentials(password=NEW_PASSWORD)
191+
client = await create_client(
192+
request,
193+
cluster_mode,
194+
protocol=protocol,
195+
request_timeout=2000,
196+
credentials=credentials,
197+
)
198+
try:
199+
await auth_client(client, NEW_PASSWORD)
200+
# Reset the server password back to empty
201+
await config_set_new_password(client, "")
202+
await client.update_connection_password(None)
203+
# Perform the teardown
204+
await client.custom_command(["FLUSHALL"])
205+
finally:
206+
await client.close()
207+
else:
208+
raise e

python/tests/test_async_client.py renamed to python/tests/async_tests/test_async_client.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@
8484
SlotType,
8585
)
8686

87-
from tests.conftest import create_client
87+
from tests.async_tests.conftest import create_client
8888
from tests.utils.utils import (
8989
check_function_list_response,
9090
check_function_stats_response,

python/tests/test_auth.py renamed to python/tests/async_tests/test_auth.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,15 @@
88
from glide.exceptions import RequestError
99
from glide.glide_client import TGlideClient
1010

11-
from tests.conftest import (
12-
NEW_PASSWORD,
13-
USERNAME,
14-
WRONG_PASSWORD,
11+
from tests.async_tests.conftest import (
1512
auth_client,
1613
config_set_new_password,
1714
kill_connections,
1815
)
1916
from tests.utils.utils import (
17+
NEW_PASSWORD,
18+
USERNAME,
19+
WRONG_PASSWORD,
2020
delete_acl_username_and_password,
2121
set_new_acl_username_with_password,
2222
)

python/tests/test_batch.py renamed to python/tests/async_tests/test_batch.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@
6161
from glide.glide_client import GlideClient, GlideClusterClient, TGlideClient
6262
from glide.routes import AllNodes, SlotIdRoute, SlotKeyRoute, SlotType
6363

64-
from tests.conftest import create_client
64+
from tests.async_tests.conftest import create_client
6565
from tests.utils.utils import (
6666
check_if_server_version_lt,
6767
convert_bytes_to_string_object,

python/tests/test_pubsub.py renamed to python/tests/async_tests/test_pubsub.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
from glide.exceptions import ConfigurationError
1818
from glide.glide_client import GlideClient, GlideClusterClient, TGlideClient
1919

20-
from tests.conftest import create_client
20+
from tests.async_tests.conftest import create_client
2121
from tests.utils.utils import check_if_server_version_lt, get_random_string
2222

2323

python/tests/test_read_from_strategy.py renamed to python/tests/async_tests/test_read_from_strategy.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
from glide.glide_client import GlideClusterClient
1111
from glide.routes import AllNodes, SlotIdRoute, SlotType
1212

13-
from tests.conftest import create_client
13+
from tests.async_tests.conftest import create_client
1414
from tests.utils.utils import get_first_result
1515

1616

python/tests/test_scan.py renamed to python/tests/async_tests/test_scan.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from glide.glide import ClusterScanCursor
1010
from glide.glide_client import GlideClient, GlideClusterClient
1111

12-
from tests.conftest import create_client
12+
from tests.async_tests.conftest import create_client
1313
from tests.utils.cluster import ValkeyCluster
1414
from tests.utils.utils import get_random_string
1515

python/tests/tests_server_modules/test_json.py renamed to python/tests/async_tests/tests_server_modules/test_json.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@
2121
from glide.exceptions import RequestError
2222
from glide.glide_client import GlideClusterClient, TGlideClient
2323

24-
from tests.test_async_client import get_random_string
24+
from tests.async_tests.test_async_client import get_random_string
2525

2626

2727
def get_random_value(value_type="str"):

0 commit comments

Comments
 (0)