forked from valkey-io/valkey-glide
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_dns.py
More file actions
87 lines (71 loc) · 3.01 KB
/
test_dns.py
File metadata and controls
87 lines (71 loc) · 3.01 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
# Copyright Valkey GLIDE Project Contributors - SPDX Identifier: Apache-2.0
"""
Tests for async client behavior when connecting with hostnames.
See `DNS Tests <../../DEVELOPER.md#dns-tests>`_ for setup instructions.
"""
import os
import pytest
from glide_shared.config import NodeAddress
from tests.async_tests.conftest import create_client
from tests.test_constants import HOSTNAME_NO_TLS, HOSTNAME_TLS
from tests.utils.utils import assert_connected, get_ca_certificate
async def create_client_with_hostname(cluster_mode: bool, use_tls: bool, hostname: str):
"""Builds and returns a client for the given parameters."""
if use_tls and cluster_mode:
cluster = pytest.valkey_tls_cluster # type: ignore[attr-defined]
elif use_tls:
cluster = pytest.standalone_tls_cluster # type: ignore[attr-defined]
elif cluster_mode:
cluster = pytest.valkey_cluster # type: ignore[attr-defined]
else:
cluster = pytest.standalone_cluster # type: ignore[attr-defined]
port = cluster.nodes_addr[0].port
address = NodeAddress(hostname, port)
return await create_client(
cluster_mode=cluster_mode,
use_tls=use_tls,
root_pem_cacerts=get_ca_certificate() if use_tls else None,
addresses=[address],
)
@pytest.mark.anyio
@pytest.mark.skipif(
not os.getenv("VALKEY_GLIDE_DNS_TESTS_ENABLED"),
reason="DNS tests disabled. Set VALKEY_GLIDE_DNS_TESTS_ENABLED to enable.",
)
@pytest.mark.parametrize("cluster_mode", [True, False])
class TestDns:
"""Async DNS resolution tests."""
async def test_connect_with_valid_hostname_succeeds(self, cluster_mode: bool):
"""Test connection with valid hostname (non-TLS)."""
client = await create_client_with_hostname(
cluster_mode=cluster_mode,
use_tls=False,
hostname=HOSTNAME_NO_TLS,
)
await assert_connected(client)
await client.close()
async def test_connect_with_invalid_hostname_fails(self, cluster_mode: bool):
"""Test connection with invalid hostname (non-TLS)."""
with pytest.raises(Exception):
await create_client_with_hostname(
cluster_mode=cluster_mode,
use_tls=False,
hostname="nonexistent.invalid",
)
async def test_tls_with_hostname_in_certificate_succeeds(self, cluster_mode: bool):
"""Test TLS connection with hostname in certificate SAN."""
client = await create_client_with_hostname(
cluster_mode=cluster_mode,
use_tls=True,
hostname=HOSTNAME_TLS,
)
await assert_connected(client)
await client.close()
async def test_tls_with_hostname_not_in_certificate_fails(self, cluster_mode: bool):
"""Test TLS connection with hostname not in certificate SAN."""
with pytest.raises(Exception):
await create_client_with_hostname(
cluster_mode=cluster_mode,
use_tls=True,
hostname=HOSTNAME_NO_TLS,
)