Skip to content

Commit ae5065d

Browse files
committed
Move to test_dns
1 parent f72cf87 commit ae5065d

File tree

2 files changed

+180
-0
lines changed

2 files changed

+180
-0
lines changed

test/asynchronous/test_dns.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
unittest,
3131
)
3232
from test.utils_shared import async_wait_until
33+
from unittest.mock import MagicMock, patch
3334

3435
from pymongo.asynchronous.uri_parser import parse_uri
3536
from pymongo.common import validate_read_preference_tags
@@ -39,6 +40,26 @@
3940
_IS_SYNC = False
4041

4142

43+
def run_initial_dns_seedlist_discovery_prose_tests(self, test_cases):
44+
for case in test_cases:
45+
with patch("dns.resolver.resolve") as mock_resolver:
46+
47+
def mock_resolve(query, record_type, *args, **kwargs):
48+
mock_srv = MagicMock()
49+
mock_srv.target.to_text.return_value = case["mock_target"]
50+
return [mock_srv]
51+
52+
mock_resolver.side_effect = mock_resolve
53+
domain = case["query"].split("._tcp.")[1]
54+
connection_string = f"mongodb+srv://{domain}"
55+
try:
56+
parse_uri(connection_string)
57+
except ConfigurationError as e:
58+
self.assertIn(case["expected_error"], str(e))
59+
else:
60+
self.fail(f"ConfigurationError was not raised for query: {case['query']}")
61+
62+
4263
class TestDNSRepl(AsyncPyMongoTestCase):
4364
if _IS_SYNC:
4465
TEST_PATH = os.path.join(
@@ -201,5 +222,74 @@ async def test_connect_case_insensitive(self):
201222
self.assertGreater(len(client.topology_description.server_descriptions()), 1)
202223

203224

225+
class TestInitialDnsSeedlistDiscovery(AsyncPyMongoTestCase):
226+
"""
227+
Initial DNS Seedlist Discovery prose tests
228+
https://github.com/mongodb/specifications/blob/0a7a8b5/source/initial-dns-seedlist-discovery/tests/README.md#prose-tests
229+
"""
230+
231+
def test_1_allow_srv_hosts_with_fewer_than_three_dot_separated_parts(self):
232+
with patch("dns.resolver.resolve"):
233+
parse_uri("mongodb+srv://localhost/")
234+
parse_uri("mongodb+srv://mongo.local/")
235+
236+
def test_2_throw_when_return_address_does_not_end_with_srv_domain(self):
237+
test_cases = [
238+
{
239+
"query": "_mongodb._tcp.localhost",
240+
"mock_target": "localhost.mongodb",
241+
"expected_error": "Invalid SRV host",
242+
},
243+
{
244+
"query": "_mongodb._tcp.blogs.mongodb.com",
245+
"mock_target": "blogs.evil.com",
246+
"expected_error": "Invalid SRV host",
247+
},
248+
{
249+
"query": "_mongodb._tcp.blogs.mongo.local",
250+
"mock_target": "test_1.evil.com",
251+
"expected_error": "Invalid SRV host",
252+
},
253+
]
254+
run_initial_dns_seedlist_discovery_prose_tests(self, test_cases)
255+
256+
def test_3_throw_when_return_address_is_identical_to_srv_hostname(self):
257+
test_cases = [
258+
{
259+
"query": "_mongodb._tcp.localhost",
260+
"mock_target": "localhost",
261+
"expected_error": "Invalid SRV host",
262+
},
263+
{
264+
"query": "_mongodb._tcp.mongo.local",
265+
"mock_target": "mongo.local",
266+
"expected_error": "Invalid SRV host",
267+
},
268+
]
269+
run_initial_dns_seedlist_discovery_prose_tests(self, test_cases)
270+
271+
def test_4_throw_when_return_address_does_not_contain_dot_separating_shared_part_of_domain(
272+
self
273+
):
274+
test_cases = [
275+
{
276+
"query": "_mongodb._tcp.localhost",
277+
"mock_target": "test_1.cluster_1localhost",
278+
"expected_error": "Invalid SRV host",
279+
},
280+
{
281+
"query": "_mongodb._tcp.mongo.local",
282+
"mock_target": "test_1.my_hostmongo.local",
283+
"expected_error": "Invalid SRV host",
284+
},
285+
{
286+
"query": "_mongodb._tcp.blogs.mongodb.com",
287+
"mock_target": "cluster.testmongodb.com",
288+
"expected_error": "Invalid SRV host",
289+
},
290+
]
291+
run_initial_dns_seedlist_discovery_prose_tests(self, test_cases)
292+
293+
204294
if __name__ == "__main__":
205295
unittest.main()

test/test_dns.py

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
unittest,
3131
)
3232
from test.utils_shared import wait_until
33+
from unittest.mock import MagicMock, patch
3334

3435
from pymongo.common import validate_read_preference_tags
3536
from pymongo.errors import ConfigurationError
@@ -39,6 +40,26 @@
3940
_IS_SYNC = True
4041

4142

43+
def run_initial_dns_seedlist_discovery_prose_tests(self, test_cases):
44+
for case in test_cases:
45+
with patch("dns.resolver.resolve") as mock_resolver:
46+
47+
def mock_resolve(query, record_type, *args, **kwargs):
48+
mock_srv = MagicMock()
49+
mock_srv.target.to_text.return_value = case["mock_target"]
50+
return [mock_srv]
51+
52+
mock_resolver.side_effect = mock_resolve
53+
domain = case["query"].split("._tcp.")[1]
54+
connection_string = f"mongodb+srv://{domain}"
55+
try:
56+
parse_uri(connection_string)
57+
except ConfigurationError as e:
58+
self.assertIn(case["expected_error"], str(e))
59+
else:
60+
self.fail(f"ConfigurationError was not raised for query: {case['query']}")
61+
62+
4263
class TestDNSRepl(PyMongoTestCase):
4364
if _IS_SYNC:
4465
TEST_PATH = os.path.join(
@@ -199,5 +220,74 @@ def test_connect_case_insensitive(self):
199220
self.assertGreater(len(client.topology_description.server_descriptions()), 1)
200221

201222

223+
class TestInitialDnsSeedlistDiscovery(PyMongoTestCase):
224+
"""
225+
Initial DNS Seedlist Discovery prose tests
226+
https://github.com/mongodb/specifications/blob/0a7a8b5/source/initial-dns-seedlist-discovery/tests/README.md#prose-tests
227+
"""
228+
229+
def test_1_allow_srv_hosts_with_fewer_than_three_dot_separated_parts(self):
230+
with patch("dns.resolver.resolve"):
231+
parse_uri("mongodb+srv://localhost/")
232+
parse_uri("mongodb+srv://mongo.local/")
233+
234+
def test_2_throw_when_return_address_does_not_end_with_srv_domain(self):
235+
test_cases = [
236+
{
237+
"query": "_mongodb._tcp.localhost",
238+
"mock_target": "localhost.mongodb",
239+
"expected_error": "Invalid SRV host",
240+
},
241+
{
242+
"query": "_mongodb._tcp.blogs.mongodb.com",
243+
"mock_target": "blogs.evil.com",
244+
"expected_error": "Invalid SRV host",
245+
},
246+
{
247+
"query": "_mongodb._tcp.blogs.mongo.local",
248+
"mock_target": "test_1.evil.com",
249+
"expected_error": "Invalid SRV host",
250+
},
251+
]
252+
run_initial_dns_seedlist_discovery_prose_tests(self, test_cases)
253+
254+
def test_3_throw_when_return_address_is_identical_to_srv_hostname(self):
255+
test_cases = [
256+
{
257+
"query": "_mongodb._tcp.localhost",
258+
"mock_target": "localhost",
259+
"expected_error": "Invalid SRV host",
260+
},
261+
{
262+
"query": "_mongodb._tcp.mongo.local",
263+
"mock_target": "mongo.local",
264+
"expected_error": "Invalid SRV host",
265+
},
266+
]
267+
run_initial_dns_seedlist_discovery_prose_tests(self, test_cases)
268+
269+
def test_4_throw_when_return_address_does_not_contain_dot_separating_shared_part_of_domain(
270+
self
271+
):
272+
test_cases = [
273+
{
274+
"query": "_mongodb._tcp.localhost",
275+
"mock_target": "test_1.cluster_1localhost",
276+
"expected_error": "Invalid SRV host",
277+
},
278+
{
279+
"query": "_mongodb._tcp.mongo.local",
280+
"mock_target": "test_1.my_hostmongo.local",
281+
"expected_error": "Invalid SRV host",
282+
},
283+
{
284+
"query": "_mongodb._tcp.blogs.mongodb.com",
285+
"mock_target": "cluster.testmongodb.com",
286+
"expected_error": "Invalid SRV host",
287+
},
288+
]
289+
run_initial_dns_seedlist_discovery_prose_tests(self, test_cases)
290+
291+
202292
if __name__ == "__main__":
203293
unittest.main()

0 commit comments

Comments
 (0)