Skip to content

Commit 3748a8b

Browse files
Add RedisCluster.remap_host_port, Update tests for CWE 404 (#2706)
* Use provided redis address. Bind to IPv4 * Add missing "await" and perform the correct test for pipe eimpty * Wait for a send event, rather than rely on sleep time. Excpect cancel errors. * set delay to 0 except for operation we want to cancel This speeds up the unit tests considerably by eliminating unnecessary delay. * Release resources in test * Fix cluster test to use address_remap and multiple proxies. * Use context manager to manage DelayProxy * Mark failing pipeline tests * lint * Use a common "master_host" test fixture
1 parent ffb2b83 commit 3748a8b

File tree

7 files changed

+227
-155
lines changed

7 files changed

+227
-155
lines changed

tests/conftest.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -441,7 +441,7 @@ def mock_cluster_resp_slaves(request, **kwargs):
441441
def master_host(request):
442442
url = request.config.getoption("--redis-url")
443443
parts = urlparse(url)
444-
yield parts.hostname, parts.port
444+
return parts.hostname, (parts.port or 6379)
445445

446446

447447
@pytest.fixture()

tests/test_asyncio/conftest.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import random
22
from contextlib import asynccontextmanager as _asynccontextmanager
33
from typing import Union
4-
from urllib.parse import urlparse
54

65
import pytest
76
import pytest_asyncio
@@ -209,13 +208,6 @@ async def mock_cluster_resp_slaves(create_redis, **kwargs):
209208
return _gen_cluster_mock_resp(r, response)
210209

211210

212-
@pytest_asyncio.fixture(scope="session")
213-
def master_host(request):
214-
url = request.config.getoption("--redis-url")
215-
parts = urlparse(url)
216-
return parts.hostname
217-
218-
219211
async def wait_for_command(
220212
client: redis.Redis, monitor: Monitor, command: str, key: Union[str, None] = None
221213
):

tests/test_asyncio/test_cluster.py

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -102,18 +102,6 @@ async def pipe(
102102
await writer.drain()
103103

104104

105-
@pytest.fixture
106-
def redis_addr(request):
107-
redis_url = request.config.getoption("--redis-url")
108-
scheme, netloc = urlparse(redis_url)[:2]
109-
assert scheme == "redis"
110-
if ":" in netloc:
111-
host, port = netloc.split(":")
112-
return host, int(port)
113-
else:
114-
return netloc, 6379
115-
116-
117105
@pytest_asyncio.fixture()
118106
async def slowlog(r: RedisCluster) -> None:
119107
"""
@@ -874,15 +862,16 @@ async def test_default_node_is_replaced_after_exception(self, r):
874862
# Rollback to the old default node
875863
r.replace_default_node(curr_default_node)
876864

877-
async def test_address_remap(self, create_redis, redis_addr):
865+
async def test_address_remap(self, create_redis, master_host):
878866
"""Test that we can create a rediscluster object with
879867
a host-port remapper and map connections through proxy objects
880868
"""
881869

882870
# we remap the first n nodes
883871
offset = 1000
884872
n = 6
885-
ports = [redis_addr[1] + i for i in range(n)]
873+
hostname, master_port = master_host
874+
ports = [master_port + i for i in range(n)]
886875

887876
def address_remap(address):
888877
# remap first three nodes to our local proxy
@@ -895,8 +884,7 @@ def address_remap(address):
895884

896885
# create the proxies
897886
proxies = [
898-
NodeProxy(("127.0.0.1", port + offset), (redis_addr[0], port))
899-
for port in ports
887+
NodeProxy(("127.0.0.1", port + offset), (hostname, port)) for port in ports
900888
]
901889
await asyncio.gather(*[p.start() for p in proxies])
902890
try:

tests/test_asyncio/test_connection_pool.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -136,14 +136,14 @@ async def test_connection_creation(self):
136136
assert connection.kwargs == connection_kwargs
137137

138138
async def test_multiple_connections(self, master_host):
139-
connection_kwargs = {"host": master_host}
139+
connection_kwargs = {"host": master_host[0]}
140140
async with self.get_pool(connection_kwargs=connection_kwargs) as pool:
141141
c1 = await pool.get_connection("_")
142142
c2 = await pool.get_connection("_")
143143
assert c1 != c2
144144

145145
async def test_max_connections(self, master_host):
146-
connection_kwargs = {"host": master_host}
146+
connection_kwargs = {"host": master_host[0]}
147147
async with self.get_pool(
148148
max_connections=2, connection_kwargs=connection_kwargs
149149
) as pool:
@@ -153,7 +153,7 @@ async def test_max_connections(self, master_host):
153153
await pool.get_connection("_")
154154

155155
async def test_reuse_previously_released_connection(self, master_host):
156-
connection_kwargs = {"host": master_host}
156+
connection_kwargs = {"host": master_host[0]}
157157
async with self.get_pool(connection_kwargs=connection_kwargs) as pool:
158158
c1 = await pool.get_connection("_")
159159
await pool.release(c1)
@@ -237,7 +237,7 @@ async def test_multiple_connections(self, master_host):
237237

238238
async def test_connection_pool_blocks_until_timeout(self, master_host):
239239
"""When out of connections, block for timeout seconds, then raise"""
240-
connection_kwargs = {"host": master_host}
240+
connection_kwargs = {"host": master_host[0]}
241241
async with self.get_pool(
242242
max_connections=1, timeout=0.1, connection_kwargs=connection_kwargs
243243
) as pool:
@@ -270,7 +270,7 @@ async def target():
270270
assert asyncio.get_running_loop().time() - start >= 0.1
271271

272272
async def test_reuse_previously_released_connection(self, master_host):
273-
connection_kwargs = {"host": master_host}
273+
connection_kwargs = {"host": master_host[0]}
274274
async with self.get_pool(connection_kwargs=connection_kwargs) as pool:
275275
c1 = await pool.get_connection("_")
276276
await pool.release(c1)

0 commit comments

Comments
 (0)