diff --git a/redis/_parsers/helpers.py b/redis/_parsers/helpers.py index 80531bb249..bf3b2ee2f2 100644 --- a/redis/_parsers/helpers.py +++ b/redis/_parsers/helpers.py @@ -137,11 +137,11 @@ def parse_sentinel_state(item): return result -def parse_sentinel_master(response): +def parse_sentinel_master(response, **options): return parse_sentinel_state(map(str_if_bytes, response)) -def parse_sentinel_state_resp3(response): +def parse_sentinel_state_resp3(response, **options): result = {} for key in response: try: @@ -154,7 +154,7 @@ def parse_sentinel_state_resp3(response): return result -def parse_sentinel_masters(response): +def parse_sentinel_masters(response, **options): result = {} for item in response: state = parse_sentinel_state(map(str_if_bytes, item)) @@ -162,19 +162,19 @@ def parse_sentinel_masters(response): return result -def parse_sentinel_masters_resp3(response): - return [parse_sentinel_state(master) for master in response] +def parse_sentinel_masters_resp3(response, **options): + return [parse_sentinel_state_resp3(master) for master in response] -def parse_sentinel_slaves_and_sentinels(response): +def parse_sentinel_slaves_and_sentinels(response, **options): return [parse_sentinel_state(map(str_if_bytes, item)) for item in response] -def parse_sentinel_slaves_and_sentinels_resp3(response): - return [parse_sentinel_state_resp3(item) for item in response] +def parse_sentinel_slaves_and_sentinels_resp3(response, **options): + return [parse_sentinel_state_resp3(item, **options) for item in response] -def parse_sentinel_get_master(response): +def parse_sentinel_get_master(response, **options): return response and (response[0], int(response[1])) or None diff --git a/tests/test_asyncio/test_sentinel.py b/tests/test_asyncio/test_sentinel.py index 867ff15405..87532ae5d5 100644 --- a/tests/test_asyncio/test_sentinel.py +++ b/tests/test_asyncio/test_sentinel.py @@ -3,6 +3,8 @@ import pytest import pytest_asyncio +from redis.asyncio.client import StrictRedis + import redis.asyncio.sentinel from redis import exceptions from redis.asyncio.sentinel import ( @@ -11,6 +13,7 @@ SentinelConnectionPool, SlaveNotFoundError, ) +from tests.conftest import is_resp2_connection @pytest_asyncio.fixture(scope="module", loop_scope="module") @@ -363,3 +366,33 @@ async def test_redis_master_usage(deployed_sentinel): r = await deployed_sentinel.master_for("redis-py-test", db=0) await r.set("foo", "bar") assert (await r.get("foo")) == "bar" + + +@pytest.mark.onlynoncluster +async def test_sentinel_commands_with_strict_redis_client(request): + sentinel_ips = request.config.getoption("--sentinels") + sentinel_host, sentinel_port = sentinel_ips.split(",")[0].split(":") + protocol = request.config.getoption("--protocol", 2) + + client = StrictRedis( + host=sentinel_host, port=sentinel_port, decode_responses=True, protocol=protocol + ) + # skipping commands that change the state of the sentinel setup + assert isinstance( + await client.sentinel_get_master_addr_by_name("redis-py-test"), tuple + ) + assert isinstance(await client.sentinel_master("redis-py-test"), dict) + if is_resp2_connection(client): + assert isinstance(await client.sentinel_masters(), dict) + else: + masters = await client.sentinel_masters() + assert isinstance(masters, list) + for master in masters: + assert isinstance(master, dict) + + assert isinstance(await client.sentinel_sentinels("redis-py-test"), list) + assert isinstance(await client.sentinel_slaves("redis-py-test"), list) + + assert isinstance(await client.sentinel_ckquorum("redis-py-test"), bool) + + await client.close() diff --git a/tests/test_sentinel.py b/tests/test_sentinel.py index 0e7624c836..fb4a2d4056 100644 --- a/tests/test_sentinel.py +++ b/tests/test_sentinel.py @@ -2,6 +2,8 @@ from unittest import mock import pytest +from redis.client import StrictRedis + import redis.sentinel from redis import exceptions from redis.sentinel import ( @@ -10,6 +12,7 @@ SentinelConnectionPool, SlaveNotFoundError, ) +from tests.conftest import is_resp2_connection @pytest.fixture(scope="module") @@ -342,3 +345,31 @@ def test_redis_master_usage(deployed_sentinel): r = deployed_sentinel.master_for("redis-py-test", db=0) r.set("foo", "bar") assert r.get("foo") == "bar" + + +@pytest.mark.onlynoncluster +def test_sentinel_commands_with_strict_redis_client(request): + sentinel_ips = request.config.getoption("--sentinels") + sentinel_host, sentinel_port = sentinel_ips.split(",")[0].split(":") + protocol = request.config.getoption("--protocol", 2) + + client = StrictRedis( + host=sentinel_host, port=sentinel_port, decode_responses=True, protocol=protocol + ) + # skipping commands that change the state of the sentinel setup + assert isinstance(client.sentinel_get_master_addr_by_name("redis-py-test"), tuple) + assert isinstance(client.sentinel_master("redis-py-test"), dict) + if is_resp2_connection(client): + assert isinstance(client.sentinel_masters(), dict) + else: + masters = client.sentinel_masters() + assert isinstance(masters, list) + for master in masters: + assert isinstance(master, dict) + + assert isinstance(client.sentinel_sentinels("redis-py-test"), list) + assert isinstance(client.sentinel_slaves("redis-py-test"), list) + + assert isinstance(client.sentinel_ckquorum("redis-py-test"), bool) + + client.close()