Skip to content

Reorganize and split tests dir and conftest files #3893

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
May 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
175 changes: 175 additions & 0 deletions python/tests/async_tests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
# Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0

from typing import AsyncGenerator, List, Optional, Union

import pytest

from glide.config import (
BackoffStrategy,
GlideClientConfiguration,
GlideClusterClientConfiguration,
NodeAddress,
ProtocolVersion,
ReadFrom,
ServerCredentials,
)
from glide.exceptions import ClosingError
from glide.glide_client import GlideClient, GlideClusterClient, TGlideClient
from glide.logger import Logger
from tests.utils.cluster import ValkeyCluster
from tests.utils.utils import (
DEFAULT_TEST_LOG_LEVEL,
INITIAL_PASSWORD,
NEW_PASSWORD,
USERNAME,
auth_client,
config_set_new_password,
create_client_config,
set_new_acl_username_with_password,
)

Logger.set_logger_config(DEFAULT_TEST_LOG_LEVEL)


@pytest.fixture(scope="function")
async def glide_client(
request,
cluster_mode: bool,
protocol: ProtocolVersion,
) -> AsyncGenerator[TGlideClient, None]:
"Get async socket client for tests"
client = await create_client(
request, cluster_mode, protocol=protocol, request_timeout=5000
)
yield client
await test_teardown(request, cluster_mode, protocol)
await client.close()


@pytest.fixture(scope="function")
async def management_client(
request,
cluster_mode: bool,
protocol: ProtocolVersion,
) -> AsyncGenerator[TGlideClient, None]:
"Get async socket client for tests, used to manage the state when tests are on the client ability to connect"
client = await create_client(request, cluster_mode, protocol=protocol)
yield client
await test_teardown(request, cluster_mode, protocol)
await client.close()


@pytest.fixture(scope="function")
async def acl_glide_client(
request,
cluster_mode: bool,
protocol: ProtocolVersion,
management_client: TGlideClient,
) -> AsyncGenerator[TGlideClient, None]:
"""
Client fot tests that use a server pre-configured with an ACL user.
This function first uses the management client to register the USERNAME with INITIAL_PASSWORD,so that
the client would be ablt to connect.
It then returns a client with this USERNAME and INITIAL_PASSWORD already set as its ServerCredentials.
"""

await set_new_acl_username_with_password(
management_client, USERNAME, INITIAL_PASSWORD
)

client = await create_client(
request,
cluster_mode,
protocol=protocol,
credentials=ServerCredentials(username=USERNAME, password=INITIAL_PASSWORD),
request_timeout=2000,
)
yield client
await test_teardown(request, cluster_mode, protocol)
await client.close()


async def create_client(
request,
cluster_mode: bool,
credentials: Optional[ServerCredentials] = None,
database_id: int = 0,
addresses: Optional[List[NodeAddress]] = None,
client_name: Optional[str] = None,
protocol: ProtocolVersion = ProtocolVersion.RESP3,
request_timeout: Optional[int] = 1000,
connection_timeout: Optional[int] = 1000,
cluster_mode_pubsub: Optional[
GlideClusterClientConfiguration.PubSubSubscriptions
] = None,
standalone_mode_pubsub: Optional[
GlideClientConfiguration.PubSubSubscriptions
] = None,
inflight_requests_limit: Optional[int] = None,
read_from: ReadFrom = ReadFrom.PRIMARY,
client_az: Optional[str] = None,
reconnect_strategy: Optional[BackoffStrategy] = None,
valkey_cluster: Optional[ValkeyCluster] = None,
) -> Union[GlideClient, GlideClusterClient]:
config = create_client_config(
request,
cluster_mode,
credentials,
database_id,
addresses,
client_name,
protocol,
request_timeout,
connection_timeout,
cluster_mode_pubsub,
standalone_mode_pubsub,
inflight_requests_limit,
read_from,
client_az,
reconnect_strategy,
valkey_cluster,
)
if cluster_mode:
return await GlideClusterClient.create(config)
else:
return await GlideClient.create(config)


async def test_teardown(request, cluster_mode: bool, protocol: ProtocolVersion):
"""
Perform teardown tasks such as flushing all data from the cluster.

If authentication is required, attempt to connect with the known password,
reset it back to empty, and proceed with teardown.
"""
credentials = None
try:
# Try connecting without credentials
client = await create_client(
request, cluster_mode, protocol=protocol, request_timeout=2000
)
await client.custom_command(["FLUSHALL"])
await client.close()
except ClosingError as e:
# Check if the error is due to authentication
if "NOAUTH" in str(e):
# Use the known password to authenticate
credentials = ServerCredentials(password=NEW_PASSWORD)
client = await create_client(
request,
cluster_mode,
protocol=protocol,
request_timeout=2000,
credentials=credentials,
)
try:
await auth_client(client, NEW_PASSWORD)
# Reset the server password back to empty
await config_set_new_password(client, "")
await client.update_connection_password(None)
# Perform the teardown
await client.custom_command(["FLUSHALL"])
finally:
await client.close()
else:
raise e
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@
SlotKeyRoute,
SlotType,
)
from tests.conftest import create_client
from tests.async_tests.conftest import create_client
from tests.utils.utils import (
check_function_list_response,
check_function_stats_response,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,14 @@
from glide.constants import OK
from glide.exceptions import RequestError
from glide.glide_client import TGlideClient
from tests.conftest import (
from tests.utils.utils import (
NEW_PASSWORD,
USERNAME,
WRONG_PASSWORD,
auth_client,
config_set_new_password,
kill_connections,
)
from tests.utils.utils import (
delete_acl_username_and_password,
kill_connections,
set_new_acl_username_with_password,
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
from glide.constants import OK, TResult, TSingleNodeRoute
from glide.glide_client import GlideClient, GlideClusterClient, TGlideClient
from glide.routes import AllNodes, SlotIdRoute, SlotKeyRoute, SlotType
from tests.conftest import create_client
from tests.async_tests.conftest import create_client
from tests.utils.utils import (
check_if_server_version_lt,
convert_bytes_to_string_object,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
from glide.constants import OK
from glide.exceptions import ConfigurationError
from glide.glide_client import GlideClient, GlideClusterClient, TGlideClient
from tests.conftest import create_client
from tests.async_tests.conftest import create_client
from tests.utils.utils import check_if_server_version_lt, get_random_string


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
from glide.constants import OK
from glide.glide_client import GlideClusterClient
from glide.routes import AllNodes, SlotIdRoute, SlotType
from tests.conftest import create_client
from tests.async_tests.conftest import create_client
from tests.utils.utils import get_first_result


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
from glide.exceptions import RequestError
from glide.glide import ClusterScanCursor
from glide.glide_client import GlideClient, GlideClusterClient
from tests.conftest import create_client
from tests.async_tests.conftest import create_client
from tests.utils.cluster import ValkeyCluster
from tests.utils.utils import get_random_string

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
from glide.constants import OK
from glide.exceptions import RequestError
from glide.glide_client import GlideClusterClient, TGlideClient
from tests.test_async_client import get_random_string
from tests.async_tests.test_async_client import get_random_string


def get_random_value(value_type="str"):
Expand Down
Loading
Loading