Skip to content

Commit 307af01

Browse files
committed
tests: move certificate discovery to a separate module
1 parent 2d9b5ac commit 307af01

File tree

3 files changed

+20
-24
lines changed

3 files changed

+20
-24
lines changed

tests/ssl_utils.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import os
2+
3+
4+
def get_ssl_filename(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

Lines changed: 3 additions & 12 deletions
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_utils import get_ssl_filename
3939
from .compat import mock
4040

4141
pytestmark = pytest.mark.onlycluster
@@ -2744,17 +2744,8 @@ class TestSSL:
27442744
appropriate port.
27452745
"""
27462746

2747-
ROOT = os.path.join(os.path.dirname(__file__), "../..")
2748-
CERT_DIR = os.path.abspath(os.path.join(ROOT, "docker", "stunnel", "keys"))
2749-
if not os.path.isdir(CERT_DIR): # github actions package validation case
2750-
CERT_DIR = os.path.abspath(
2751-
os.path.join(ROOT, "..", "docker", "stunnel", "keys")
2752-
)
2753-
if not os.path.isdir(CERT_DIR):
2754-
raise IOError(f"No SSL certificates found. They should be in {CERT_DIR}")
2755-
2756-
SERVER_CERT = os.path.join(CERT_DIR, "server-cert.pem")
2757-
SERVER_KEY = os.path.join(CERT_DIR, "server-key.pem")
2747+
SERVER_CERT = get_ssl_filename("server-cert.pem")
2748+
SERVER_KEY = get_ssl_filename("server-key.pem")
27582749

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

tests/test_ssl.py

Lines changed: 3 additions & 12 deletions
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_utils import get_ssl_filename
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_filename("server-cert.pem")
23+
SERVER_KEY = get_ssl_filename("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)