Skip to content

Commit 98fa92f

Browse files
committed
Added better endpoitns handling
1 parent 68ebdee commit 98fa92f

File tree

3 files changed

+60
-15
lines changed

3 files changed

+60
-15
lines changed

tests/conftest.py

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,13 @@ def pytest_addoption(parser):
168168
help="Name of the Redis master service that the sentinels are monitoring",
169169
)
170170

171+
parser.addoption(
172+
"--endpoint-name",
173+
action="store",
174+
default=None,
175+
help="Name of the Redis endpoint the tests should be executed on",
176+
)
177+
171178

172179
def _get_info(redis_url):
173180
client = redis.Redis.from_url(redis_url)
@@ -352,13 +359,6 @@ def _get_client(
352359
else:
353360
redis_url = from_url
354361

355-
endpoints_config = os.getenv("REDIS_ENDPOINTS_CONFIG_PATH", None)
356-
if endpoints_config is not None:
357-
with open(endpoints_config, 'r') as f:
358-
data = json.load(f)
359-
db = next(iter(data.values()))
360-
redis_url = db['endpoints'][0]
361-
362362
redis_tls_url = request.config.getoption("--redis-ssl-url")
363363

364364
if "protocol" not in redis_url and kwargs.get("protocol") is None:
@@ -723,6 +723,25 @@ def credential_provider(request) -> CredentialProvider:
723723
return get_credential_provider(request)
724724

725725

726+
def get_endpoint(endpoint_name: str):
727+
endpoints_config = os.getenv("REDIS_ENDPOINTS_CONFIG_PATH", None)
728+
729+
if not (endpoints_config and os.path.exists(endpoints_config)):
730+
raise FileNotFoundError(
731+
f"Endpoints config file not found: {endpoints_config}"
732+
)
733+
734+
try:
735+
with open(endpoints_config, 'r') as f:
736+
data = json.load(f)
737+
db = data[endpoint_name]
738+
return db['endpoints'][0]
739+
except Exception as e:
740+
raise ValueError(
741+
f"Failed to load endpoints config file: {endpoints_config}"
742+
) from e
743+
744+
726745
def wait_for_command(client, monitor, command, key=None):
727746
# issue a command with a key name that's local to this process.
728747
# if we find a command with our key before the command we're waiting

tests/test_asyncio/test_credentials.py

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,23 @@
1818
from redis.backoff import NoBackoff
1919
from redis.credentials import CredentialProvider, UsernamePasswordCredentialProvider
2020
from redis.utils import str_if_bytes
21-
from tests.conftest import skip_if_redis_enterprise
21+
from tests.conftest import skip_if_redis_enterprise, get_endpoint
2222
from tests.test_asyncio.conftest import get_credential_provider
2323

2424

25+
@pytest.fixture()
26+
def endpoint(request):
27+
endpoint_name = request.config.getoption("--endpoint-name")
28+
29+
try:
30+
return get_endpoint(endpoint_name)
31+
except FileNotFoundError as e:
32+
pytest.skip(
33+
f"Skipping scenario test because endpoints file is missing: {str(e)}"
34+
)
35+
2536
@pytest_asyncio.fixture()
26-
async def r_credential(request, create_redis):
37+
async def r_credential(request, create_redis, endpoint):
2738
credential_provider = request.param.get("cred_provider_class", None)
2839

2940
if credential_provider is not None:
@@ -33,7 +44,7 @@ async def r_credential(request, create_redis):
3344
"credential_provider": credential_provider,
3445
}
3546

36-
return await create_redis(**kwargs)
47+
return await create_redis(url=endpoint, **kwargs)
3748

3849

3950
@pytest_asyncio.fixture()

tests/test_credentials.py

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
import functools
2+
import json
3+
import os
24
import random
35
import string
46
import threading
@@ -22,11 +24,23 @@
2224
from redis.credentials import CredentialProvider, UsernamePasswordCredentialProvider
2325
from redis.retry import Retry
2426
from redis.utils import str_if_bytes
25-
from tests.conftest import _get_client, skip_if_redis_enterprise, get_credential_provider
27+
from tests.conftest import _get_client, skip_if_redis_enterprise, get_credential_provider, get_endpoint
2628

2729

2830
@pytest.fixture()
29-
def r(request):
31+
def endpoint(request):
32+
endpoint_name = request.config.getoption("--endpoint-name")
33+
34+
try:
35+
return get_endpoint(endpoint_name)
36+
except FileNotFoundError as e:
37+
pytest.skip(
38+
f"Skipping scenario test because endpoints file is missing: {str(e)}"
39+
)
40+
41+
42+
@pytest.fixture()
43+
def r_entra(request, endpoint):
3044
credential_provider = request.param.get("cred_provider_class", None)
3145
single_connection = request.param.get("single_connection_client", False)
3246

@@ -38,6 +52,7 @@ def r(request):
3852
request,
3953
credential_provider=credential_provider,
4054
single_connection_client=single_connection,
55+
from_url=endpoint
4156
) as client:
4257
yield client
4358

@@ -502,7 +517,7 @@ def re_auth_callback(token):
502517
@pytest.mark.cp_integration
503518
class TestEntraIdCredentialsProvider:
504519
@pytest.mark.parametrize(
505-
"r",
520+
"r_entra",
506521
[
507522
{
508523
"cred_provider_class": EntraIdCredentialsProvider,
@@ -518,8 +533,8 @@ class TestEntraIdCredentialsProvider:
518533
)
519534
@pytest.mark.onlynoncluster
520535
@pytest.mark.cp_integration
521-
def test_auth_pool_with_credential_provider(self, r: redis.Redis):
522-
assert r.ping() is True
536+
def test_auth_pool_with_credential_provider(self, r_entra: redis.Redis):
537+
assert r_entra.ping() is True
523538

524539
@pytest.mark.parametrize(
525540
"r",

0 commit comments

Comments
 (0)