Skip to content

Commit

Permalink
tests: move certificate discovery to a separate module
Browse files Browse the repository at this point in the history
  • Loading branch information
woutdenolf committed Mar 22, 2023
1 parent 318b114 commit b167df0
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 24 deletions.
14 changes: 14 additions & 0 deletions tests/ssl_certificates.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import os


def get_ssl_certificate(name):
root = os.path.join(os.path.dirname(__file__), "..")
cert_dir = os.path.abspath(os.path.join(root, "docker", "stunnel", "keys"))
if not os.path.isdir(cert_dir): # github actions package validation case
cert_dir = os.path.abspath(
os.path.join(root, "..", "docker", "stunnel", "keys")
)
if not os.path.isdir(cert_dir):
raise IOError(f"No SSL certificates found. They should be in {cert_dir}")

return os.path.join(cert_dir, name)
15 changes: 3 additions & 12 deletions tests/test_asyncio/test_cluster.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import asyncio
import binascii
import datetime
import os
import warnings
from typing import Any, Awaitable, Callable, Dict, List, Optional, Type, Union
from urllib.parse import urlparse
Expand Down Expand Up @@ -36,6 +35,7 @@
skip_unless_arch_bits,
)

from ..ssl_certificates import get_ssl_certificate
from .compat import mock

pytestmark = pytest.mark.onlycluster
Expand Down Expand Up @@ -2641,17 +2641,8 @@ class TestSSL:
appropriate port.
"""

ROOT = os.path.join(os.path.dirname(__file__), "../..")
CERT_DIR = os.path.abspath(os.path.join(ROOT, "docker", "stunnel", "keys"))
if not os.path.isdir(CERT_DIR): # github actions package validation case
CERT_DIR = os.path.abspath(
os.path.join(ROOT, "..", "docker", "stunnel", "keys")
)
if not os.path.isdir(CERT_DIR):
raise IOError(f"No SSL certificates found. They should be in {CERT_DIR}")

SERVER_CERT = os.path.join(CERT_DIR, "server-cert.pem")
SERVER_KEY = os.path.join(CERT_DIR, "server-key.pem")
SERVER_CERT = get_ssl_certificate("server-cert.pem")
SERVER_KEY = get_ssl_certificate("server-key.pem")

@pytest_asyncio.fixture()
def create_client(self, request: FixtureRequest) -> Callable[..., RedisCluster]:
Expand Down
15 changes: 3 additions & 12 deletions tests/test_ssl.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import os
import socket
import ssl
from urllib.parse import urlparse
Expand All @@ -9,6 +8,7 @@
from redis.exceptions import ConnectionError, RedisError

from .conftest import skip_if_cryptography, skip_if_nocryptography
from .ssl_certificates import get_ssl_certificate


@pytest.mark.ssl
Expand All @@ -19,17 +19,8 @@ class TestSSL:
and connecting to the appropriate port.
"""

ROOT = os.path.join(os.path.dirname(__file__), "..")
CERT_DIR = os.path.abspath(os.path.join(ROOT, "docker", "stunnel", "keys"))
if not os.path.isdir(CERT_DIR): # github actions package validation case
CERT_DIR = os.path.abspath(
os.path.join(ROOT, "..", "docker", "stunnel", "keys")
)
if not os.path.isdir(CERT_DIR):
raise IOError(f"No SSL certificates found. They should be in {CERT_DIR}")

SERVER_CERT = os.path.join(CERT_DIR, "server-cert.pem")
SERVER_KEY = os.path.join(CERT_DIR, "server-key.pem")
SERVER_CERT = get_ssl_certificate("server-cert.pem")
SERVER_KEY = get_ssl_certificate("server-key.pem")

def test_ssl_with_invalid_cert(self, request):
ssl_url = request.config.option.redis_ssl_url
Expand Down

0 comments on commit b167df0

Please sign in to comment.