-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Expand file tree
/
Copy pathtest_dns.py
More file actions
293 lines (250 loc) · 10.6 KB
/
test_dns.py
File metadata and controls
293 lines (250 loc) · 10.6 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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
# Copyright 2017 MongoDB, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""Run the SRV support tests."""
from __future__ import annotations
import glob
import json
import os
import pathlib
import sys
sys.path[0:0] = [""]
from test import (
IntegrationTest,
PyMongoTestCase,
client_context,
unittest,
)
from test.utils_shared import wait_until
from unittest.mock import MagicMock, patch
from pymongo.common import validate_read_preference_tags
from pymongo.errors import ConfigurationError
from pymongo.synchronous.uri_parser import parse_uri
from pymongo.uri_parser_shared import split_hosts
_IS_SYNC = True
def run_initial_dns_seedlist_discovery_prose_tests(self, test_cases):
for case in test_cases:
with patch("dns.resolver.resolve") as mock_resolver:
def mock_resolve(query, record_type, *args, **kwargs):
mock_srv = MagicMock()
mock_srv.target.to_text.return_value = case["mock_target"]
return [mock_srv]
mock_resolver.side_effect = mock_resolve
domain = case["query"].split("._tcp.")[1]
connection_string = f"mongodb+srv://{domain}"
try:
parse_uri(connection_string)
except ConfigurationError as e:
self.assertIn(case["expected_error"], str(e))
else:
self.fail(f"ConfigurationError was not raised for query: {case['query']}")
class TestDNSRepl(PyMongoTestCase):
if _IS_SYNC:
TEST_PATH = os.path.join(
pathlib.Path(__file__).resolve().parent, "srv_seedlist", "replica-set"
)
else:
TEST_PATH = os.path.join(
pathlib.Path(__file__).resolve().parent.parent, "srv_seedlist", "replica-set"
)
load_balanced = False
@client_context.require_replica_set
def setUp(self):
pass
class TestDNSLoadBalanced(PyMongoTestCase):
if _IS_SYNC:
TEST_PATH = os.path.join(
pathlib.Path(__file__).resolve().parent, "srv_seedlist", "load-balanced"
)
else:
TEST_PATH = os.path.join(
pathlib.Path(__file__).resolve().parent.parent, "srv_seedlist", "load-balanced"
)
load_balanced = True
@client_context.require_load_balancer
def setUp(self):
pass
class TestDNSSharded(PyMongoTestCase):
if _IS_SYNC:
TEST_PATH = os.path.join(pathlib.Path(__file__).resolve().parent, "srv_seedlist", "sharded")
else:
TEST_PATH = os.path.join(
pathlib.Path(__file__).resolve().parent.parent, "srv_seedlist", "sharded"
)
load_balanced = False
@client_context.require_mongos
def setUp(self):
pass
def create_test(test_case):
def run_test(self):
uri = test_case["uri"]
seeds = test_case.get("seeds")
num_seeds = test_case.get("numSeeds", len(seeds or []))
hosts = test_case.get("hosts")
num_hosts = test_case.get("numHosts", len(hosts or []))
options = test_case.get("options", {})
if "ssl" in options:
options["tls"] = options.pop("ssl")
parsed_options = test_case.get("parsed_options")
# See DRIVERS-1324, unless tls is explicitly set to False we need TLS.
needs_tls = not (options and (options.get("ssl") is False or options.get("tls") is False))
if needs_tls and not client_context.tls:
self.skipTest("this test requires a TLS cluster")
if not needs_tls and client_context.tls:
self.skipTest("this test requires a non-TLS cluster")
if seeds:
seeds = split_hosts(",".join(seeds))
if hosts:
hosts = frozenset(split_hosts(",".join(hosts)))
if seeds or num_seeds:
result = parse_uri(uri, validate=True)
if seeds is not None:
self.assertEqual(sorted(result["nodelist"]), sorted(seeds))
if num_seeds is not None:
self.assertEqual(len(result["nodelist"]), num_seeds)
if options:
opts = result["options"]
if "readpreferencetags" in opts:
rpts = validate_read_preference_tags(
"readPreferenceTags", opts.pop("readpreferencetags")
)
opts["readPreferenceTags"] = rpts
self.assertEqual(result["options"], options)
if parsed_options:
for opt, expected in parsed_options.items():
if opt == "user":
self.assertEqual(result["username"], expected)
elif opt == "password":
self.assertEqual(result["password"], expected)
elif opt == "auth_database" or opt == "db":
self.assertEqual(result["database"], expected)
hostname = next(iter(client_context.client.nodes))[0]
# The replica set members must be configured as 'localhost'.
if hostname == "localhost":
copts = client_context.default_client_options.copy()
# Remove tls since SRV parsing should add it automatically.
copts.pop("tls", None)
if client_context.tls:
# Our test certs don't support the SRV hosts used in these
# tests.
copts["tlsAllowInvalidHostnames"] = True
client = self.simple_client(uri, **copts)
if client._options.connect:
client._connect()
if num_seeds is not None:
self.assertEqual(len(client._topology_settings.seeds), num_seeds)
if hosts is not None:
wait_until(lambda: hosts == client.nodes, "match test hosts to client nodes")
if num_hosts is not None:
wait_until(
lambda: num_hosts == len(client.nodes), "wait to connect to num_hosts"
)
if test_case.get("ping", True):
client.admin.command("ping")
# XXX: we should block until SRV poller runs at least once
# and re-run these assertions.
else:
try:
parse_uri(uri)
except (ConfigurationError, ValueError):
pass
else:
self.fail("failed to raise an exception")
return run_test
def create_tests(cls):
for filename in glob.glob(os.path.join(cls.TEST_PATH, "*.json")):
test_suffix, _ = os.path.splitext(os.path.basename(filename))
with open(filename) as dns_test_file:
test_method = create_test(json.load(dns_test_file))
setattr(cls, "test_" + test_suffix, test_method)
create_tests(TestDNSRepl)
create_tests(TestDNSLoadBalanced)
create_tests(TestDNSSharded)
class TestParsingErrors(PyMongoTestCase):
def test_invalid_host(self):
with self.assertRaisesRegex(ConfigurationError, "Invalid URI host: an IP address is not"):
client = self.simple_client("mongodb+srv://127.0.0.1")
client._connect()
with self.assertRaisesRegex(ConfigurationError, "Invalid URI host: an IP address is not"):
client = self.simple_client("mongodb+srv://[::1]")
client._connect()
class TestCaseInsensitive(IntegrationTest):
def test_connect_case_insensitive(self):
client = self.simple_client("mongodb+srv://TEST1.TEST.BUILD.10GEN.cc/")
client._connect()
self.assertGreater(len(client.topology_description.server_descriptions()), 1)
class TestInitialDnsSeedlistDiscovery(PyMongoTestCase):
"""
Initial DNS Seedlist Discovery prose tests
https://github.com/mongodb/specifications/blob/0a7a8b5/source/initial-dns-seedlist-discovery/tests/README.md#prose-tests
"""
def test_1_allow_srv_hosts_with_fewer_than_three_dot_separated_parts(self):
with patch("dns.resolver.resolve"):
parse_uri("mongodb+srv://localhost/")
parse_uri("mongodb+srv://mongo.local/")
def test_2_throw_when_return_address_does_not_end_with_srv_domain(self):
test_cases = [
{
"query": "_mongodb._tcp.localhost",
"mock_target": "localhost.mongodb",
"expected_error": "Invalid SRV host",
},
{
"query": "_mongodb._tcp.blogs.mongodb.com",
"mock_target": "blogs.evil.com",
"expected_error": "Invalid SRV host",
},
{
"query": "_mongodb._tcp.blogs.mongo.local",
"mock_target": "test_1.evil.com",
"expected_error": "Invalid SRV host",
},
]
run_initial_dns_seedlist_discovery_prose_tests(self, test_cases)
def test_3_throw_when_return_address_is_identical_to_srv_hostname(self):
test_cases = [
{
"query": "_mongodb._tcp.localhost",
"mock_target": "localhost",
"expected_error": "Invalid SRV host",
},
{
"query": "_mongodb._tcp.mongo.local",
"mock_target": "mongo.local",
"expected_error": "Invalid SRV host",
},
]
run_initial_dns_seedlist_discovery_prose_tests(self, test_cases)
def test_4_throw_when_return_address_does_not_contain_dot_separating_shared_part_of_domain(
self
):
test_cases = [
{
"query": "_mongodb._tcp.localhost",
"mock_target": "test_1.cluster_1localhost",
"expected_error": "Invalid SRV host",
},
{
"query": "_mongodb._tcp.mongo.local",
"mock_target": "test_1.my_hostmongo.local",
"expected_error": "Invalid SRV host",
},
{
"query": "_mongodb._tcp.blogs.mongodb.com",
"mock_target": "cluster.testmongodb.com",
"expected_error": "Invalid SRV host",
},
]
run_initial_dns_seedlist_discovery_prose_tests(self, test_cases)
if __name__ == "__main__":
unittest.main()