|
| 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 |
0 commit comments