Skip to content

Commit b167df0

Browse files
committed
tests: move certificate discovery to a separate module
1 parent 318b114 commit b167df0

File tree

3 files changed

+20
-24
lines changed

3 files changed

+20
-24
lines changed

tests/ssl_certificates.py

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import os
2+
3+
4+
def get_ssl_certificate(name):
5+
root = os.path.join(os.path.dirname(__file__), "..")
6+
cert_dir = os.path.abspath(os.path.join(root, "docker", "stunnel", "keys"))
7+
if not os.path.isdir(cert_dir): # github actions package validation case
8+
cert_dir = os.path.abspath(
9+
os.path.join(root, "..", "docker", "stunnel", "keys")
10+
)
11+
if not os.path.isdir(cert_dir):
12+
raise IOError(f"No SSL certificates found. They should be in {cert_dir}")
13+
14+
return os.path.join(cert_dir, name)

tests/test_asyncio/test_cluster.py

+3-12
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import asyncio
22
import binascii
33
import datetime
4-
import os
54
import warnings
65
from typing import Any, Awaitable, Callable, Dict, List, Optional, Type, Union
76
from urllib.parse import urlparse
@@ -36,6 +35,7 @@
3635
skip_unless_arch_bits,
3736
)
3837

38+
from ..ssl_certificates import get_ssl_certificate
3939
from .compat import mock
4040

4141
pytestmark = pytest.mark.onlycluster
@@ -2641,17 +2641,8 @@ class TestSSL:
26412641
appropriate port.
26422642
"""
26432643

2644-
ROOT = os.path.join(os.path.dirname(__file__), "../..")
2645-
CERT_DIR = os.path.abspath(os.path.join(ROOT, "docker", "stunnel", "keys"))
2646-
if not os.path.isdir(CERT_DIR): # github actions package validation case
2647-
CERT_DIR = os.path.abspath(
2648-
os.path.join(ROOT, "..", "docker", "stunnel", "keys")
2649-
)
2650-
if not os.path.isdir(CERT_DIR):
2651-
raise IOError(f"No SSL certificates found. They should be in {CERT_DIR}")
2652-
2653-
SERVER_CERT = os.path.join(CERT_DIR, "server-cert.pem")
2654-
SERVER_KEY = os.path.join(CERT_DIR, "server-key.pem")
2644+
SERVER_CERT = get_ssl_certificate("server-cert.pem")
2645+
SERVER_KEY = get_ssl_certificate("server-key.pem")
26552646

26562647
@pytest_asyncio.fixture()
26572648
def create_client(self, request: FixtureRequest) -> Callable[..., RedisCluster]:

tests/test_ssl.py

+3-12
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
import os
21
import socket
32
import ssl
43
from urllib.parse import urlparse
@@ -9,6 +8,7 @@
98
from redis.exceptions import ConnectionError, RedisError
109

1110
from .conftest import skip_if_cryptography, skip_if_nocryptography
11+
from .ssl_certificates import get_ssl_certificate
1212

1313

1414
@pytest.mark.ssl
@@ -19,17 +19,8 @@ class TestSSL:
1919
and connecting to the appropriate port.
2020
"""
2121

22-
ROOT = os.path.join(os.path.dirname(__file__), "..")
23-
CERT_DIR = os.path.abspath(os.path.join(ROOT, "docker", "stunnel", "keys"))
24-
if not os.path.isdir(CERT_DIR): # github actions package validation case
25-
CERT_DIR = os.path.abspath(
26-
os.path.join(ROOT, "..", "docker", "stunnel", "keys")
27-
)
28-
if not os.path.isdir(CERT_DIR):
29-
raise IOError(f"No SSL certificates found. They should be in {CERT_DIR}")
30-
31-
SERVER_CERT = os.path.join(CERT_DIR, "server-cert.pem")
32-
SERVER_KEY = os.path.join(CERT_DIR, "server-key.pem")
22+
SERVER_CERT = get_ssl_certificate("server-cert.pem")
23+
SERVER_KEY = get_ssl_certificate("server-key.pem")
3324

3425
def test_ssl_with_invalid_cert(self, request):
3526
ssl_url = request.config.option.redis_ssl_url

0 commit comments

Comments
 (0)