Skip to content

Commit 1224db3

Browse files
committed
PYTHON-2678 Resync SRV spec tests (#613)
Add support for validating parsed_options and running non-TLS tests. (cherry picked from commit 0535f5d)
1 parent bac6761 commit 1224db3

File tree

6 files changed

+105
-8
lines changed

6 files changed

+105
-8
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"uri": "mongodb+srv://b*b%40f3tt%3D:%244to%40L8%3DMC@test3.test.build.10gen.cc/mydb%3F?replicaSet=repl0",
3+
"seeds": [
4+
"localhost.test.build.10gen.cc:27017"
5+
],
6+
"hosts": [
7+
"localhost:27017",
8+
"localhost:27018",
9+
"localhost:27019"
10+
],
11+
"options": {
12+
"replicaSet": "repl0",
13+
"ssl": true
14+
},
15+
"parsed_options": {
16+
"user": "b*b@f3tt=",
17+
"password": "$4to@L8=MC",
18+
"db": "mydb?"
19+
},
20+
"comment": "Encoded user, pass, and DB parse correctly"
21+
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
{
2+
"uri": "mongodb+srv://test10.test.build.10gen.cc/?replicaSet=repl0",
3+
"seeds": [],
4+
"hosts": [],
5+
"error": true,
6+
"comment": "Should fail because socketTimeoutMS is not an allowed option."
7+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
{
2+
"uri": "mongodb+srv://test5.test.build.10gen.cc/?ssl=false",
3+
"seeds": [
4+
"localhost.test.build.10gen.cc:27017"
5+
],
6+
"hosts": [
7+
"localhost:27017",
8+
"localhost:27018",
9+
"localhost:27019"
10+
],
11+
"options": {
12+
"replicaSet": "repl0",
13+
"authSource": "thisDB",
14+
"ssl": false
15+
}
16+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
{
2+
"uri": "mongodb+srv://test1.test.build.10gen.cc/adminDB?replicaSet=repl0",
3+
"seeds": [
4+
"localhost.test.build.10gen.cc:27017",
5+
"localhost.test.build.10gen.cc:27018"
6+
],
7+
"hosts": [
8+
"localhost:27017",
9+
"localhost:27018",
10+
"localhost:27019"
11+
],
12+
"options": {
13+
"replicaSet": "repl0",
14+
"ssl": true
15+
},
16+
"parsed_options": {
17+
"auth_database": "adminDB"
18+
}
19+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"uri": "mongodb+srv://auser:apass@test1.test.build.10gen.cc/?replicaSet=repl0",
3+
"seeds": [
4+
"localhost.test.build.10gen.cc:27017",
5+
"localhost.test.build.10gen.cc:27018"
6+
],
7+
"hosts": [
8+
"localhost:27017",
9+
"localhost:27018",
10+
"localhost:27019"
11+
],
12+
"parsed_options": {
13+
"user": "auser",
14+
"password": "apass"
15+
},
16+
"comment": "Should preserve auth credentials"
17+
}

test/test_dns.py

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@
3030
from test.utils import wait_until
3131

3232

33-
_TEST_PATH = os.path.join(
33+
TEST_PATH = os.path.join(
3434
os.path.dirname(os.path.realpath(__file__)), 'srv_seedlist')
3535

3636
class TestDNS(unittest.TestCase):
@@ -40,14 +40,22 @@ class TestDNS(unittest.TestCase):
4040
def create_test(test_case):
4141

4242
@client_context.require_replica_set
43-
@client_context.require_tls
4443
def run_test(self):
4544
if not _HAVE_DNSPYTHON:
4645
raise unittest.SkipTest("DNS tests require the dnspython module")
4746
uri = test_case['uri']
4847
seeds = test_case['seeds']
4948
hosts = test_case['hosts']
5049
options = test_case.get('options')
50+
parsed_options = test_case.get('parsed_options')
51+
# See DRIVERS-1324, unless tls is explicitly set to False we need TLS.
52+
needs_tls = not (options and (options.get('ssl') == False or
53+
options.get('tls') == False))
54+
if needs_tls and not client_context.tls:
55+
self.skipTest('this test requires a TLS cluster')
56+
if not needs_tls and client_context.tls:
57+
self.skipTest('this test requires a non-TLS cluster')
58+
5159
if seeds:
5260
seeds = split_hosts(','.join(seeds))
5361
if hosts:
@@ -63,18 +71,27 @@ def run_test(self):
6371
'readPreferenceTags', opts.pop('readpreferencetags'))
6472
opts['readPreferenceTags'] = rpts
6573
self.assertEqual(result['options'], options)
74+
if parsed_options:
75+
for opt, expected in parsed_options.items():
76+
if opt == 'user':
77+
self.assertEqual(result['username'], expected)
78+
elif opt == 'password':
79+
self.assertEqual(result['password'], expected)
80+
elif opt == 'auth_database' or opt == 'db':
81+
self.assertEqual(result['database'], expected)
6682

6783
hostname = next(iter(client_context.client.nodes))[0]
6884
# The replica set members must be configured as 'localhost'.
6985
if hostname == 'localhost':
7086
copts = client_context.default_client_options.copy()
71-
if client_context.tls is True:
72-
# Our test certs don't support the SRV hosts used in these tests.
73-
copts['ssl_match_hostname'] = False
87+
# Remove tls since SRV parsing should add it automatically.
88+
copts.pop('tls', None)
89+
if client_context.tls:
90+
# Our test certs don't support the SRV hosts used in these
91+
# tests.
92+
copts['tlsAllowInvalidHostnames'] = True
7493

7594
client = MongoClient(uri, **copts)
76-
# Force server selection
77-
client.admin.command('ismaster')
7895
wait_until(
7996
lambda: hosts == client.nodes,
8097
'match test hosts to client nodes')
@@ -90,7 +107,7 @@ def run_test(self):
90107

91108

92109
def create_tests():
93-
for filename in glob.glob(os.path.join(_TEST_PATH, '*.json')):
110+
for filename in glob.glob(os.path.join(TEST_PATH, '*.json')):
94111
test_suffix, _ = os.path.splitext(os.path.basename(filename))
95112
with open(filename) as dns_test_file:
96113
test_method = create_test(json.load(dns_test_file))

0 commit comments

Comments
 (0)