Skip to content

Commit a9d668c

Browse files
authored
PYTHON-1878 Add mongodb+srv URIs to Atlas Connectivity tests (#538)
Enable xtrace with silent:false to make test failures easier to diagnose.
1 parent 6b01235 commit a9d668c

File tree

3 files changed

+123
-41
lines changed

3 files changed

+123
-41
lines changed

.evergreen/config.yml

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -444,8 +444,29 @@ functions:
444444
silent: true
445445
working_dir: "src"
446446
script: |
447-
# DO NOT ECHO WITH XTRACE (which PREPARE_SHELL does)
448-
PYTHON_BINARY=${PYTHON_BINARY} ATLAS_REPL='${atlas_repl}' ATLAS_SHRD='${atlas_shrd}' ATLAS_FREE='${atlas_free}' ATLAS_TLS11='${atlas_tls11}' ATLAS_TLS12='${atlas_tls12}' sh ${PROJECT_DIRECTORY}/.evergreen/run-atlas-tests.sh
447+
cat <<EOT > prepare_atlas_connectivity.sh
448+
export ATLAS_FREE='${atlas_free}'
449+
export ATLAS_REPL='${atlas_repl}'
450+
export ATLAS_SHRD='${atlas_shrd}'
451+
export ATLAS_TLS11='${atlas_tls11}'
452+
export ATLAS_TLS12='${atlas_tls12}'
453+
export ATLAS_SRV_FREE='${atlas_srv_free}'
454+
export ATLAS_SRV_REPL='${atlas_srv_repl}'
455+
export ATLAS_SRV_SHRD='${atlas_srv_shrd}'
456+
export ATLAS_SRV_TLS11='${atlas_srv_tls11}'
457+
export ATLAS_SRV_TLS12='${atlas_srv_tls12}'
458+
EOT
459+
- command: shell.exec
460+
type: test
461+
params:
462+
working_dir: "src"
463+
script: |
464+
# Disable xtrace (just in case it was accidentally set).
465+
set +x
466+
. ./prepare_atlas_connectivity.sh
467+
rm -f ./prepare_atlas_connectivity.sh
468+
469+
PYTHON_BINARY=${PYTHON_BINARY} bash ${PROJECT_DIRECTORY}/.evergreen/run-atlas-tests.sh
449470
450471
"add aws auth variables to file":
451472
- command: shell.exec

.evergreen/run-atlas-tests.sh

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
#!/bin/bash
22

3-
# Don't trace to avoid secrets showing up in the logs
3+
# Exit on error and enable trace.
44
set -o errexit
5+
set -o xtrace
56

67
export JAVA_HOME=/opt/java/jdk8
78

@@ -15,27 +16,37 @@ if [ -z "$PYTHON_BINARY" ]; then
1516
fi
1617

1718
IMPL=$(${PYTHON_BINARY} -c "import platform, sys; sys.stdout.write(platform.python_implementation())")
18-
if [ $IMPL = "Jython" -o $IMPL = "PyPy" ]; then
19-
echo "Using Jython or PyPy"
19+
20+
if [ $IMPL = "Jython" ]; then
21+
# The venv created by createvirtualenv is incompatible with Jython
2022
$PYTHON_BINARY -m virtualenv --never-download --no-wheel atlastest
2123
. atlastest/bin/activate
22-
trap "deactivate; rm -rf atlastest" EXIT HUP
23-
pip install certifi
24-
PYTHON=python
2524
else
26-
IS_PRE_279=$(${PYTHON_BINARY} -c "import sys; sys.stdout.write('1' if sys.version_info < (2, 7, 9) else '0')")
25+
# All other pythons work with createvirtualenv.
26+
. .evergreen/utils.sh
27+
createvirtualenv $PYTHON_BINARY atlastest
28+
fi
29+
trap "deactivate; rm -rf atlastest" EXIT HUP
30+
31+
if [ $IMPL = "Jython" -o $IMPL = "PyPy" ]; then
32+
echo "Using Jython or PyPy"
33+
python -m pip install certifi
34+
else
35+
IS_PRE_279=$(python -c "import sys; sys.stdout.write('1' if sys.version_info < (2, 7, 9) else '0')")
2736
if [ $IS_PRE_279 = "1" ]; then
2837
echo "Using a Pre-2.7.9 CPython"
29-
$PYTHON_BINARY -m virtualenv --never-download --no-wheel atlastest
30-
. atlastest/bin/activate
31-
trap "deactivate; rm -rf atlastest" EXIT HUP
32-
pip install pyopenssl>=17.2.0 service_identity>18.1.0
33-
PYTHON=python
38+
python -m pip install pyopenssl>=17.2.0 service_identity>18.1.0
3439
else
3540
echo "Using CPython 2.7.9+"
36-
PYTHON=$PYTHON_BINARY
3741
fi
3842
fi
3943

40-
echo "Running tests"
41-
$PYTHON test/atlas/test_connection.py
44+
echo "Running tests without dnspython"
45+
python test/atlas/test_connection.py
46+
47+
# dnspython is incompatible with Jython so don't test that combination.
48+
if [ $IMPL != "Jython" ]; then
49+
python -m pip install dnspython
50+
echo "Running tests with dnspython"
51+
MUST_TEST_SRV="1" python test/atlas/test_connection.py
52+
fi

test/atlas/test_connection.py

Lines changed: 74 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,41 @@
1818
import sys
1919
import unittest
2020

21+
from collections import defaultdict
22+
2123
sys.path[0:0] = [""]
2224

2325
import pymongo
2426
from pymongo.ssl_support import HAS_SNI
2527

26-
27-
_REPL = os.environ.get("ATLAS_REPL")
28-
_SHRD = os.environ.get("ATLAS_SHRD")
29-
_FREE = os.environ.get("ATLAS_FREE")
30-
_TLS11 = os.environ.get("ATLAS_TLS11")
31-
_TLS12 = os.environ.get("ATLAS_TLS12")
32-
33-
34-
def _connect(uri):
28+
try:
29+
import dns
30+
HAS_DNS = True
31+
except ImportError:
32+
HAS_DNS = False
33+
34+
35+
URIS = {
36+
"ATLAS_REPL": os.environ.get("ATLAS_REPL"),
37+
"ATLAS_SHRD": os.environ.get("ATLAS_SHRD"),
38+
"ATLAS_FREE": os.environ.get("ATLAS_FREE"),
39+
"ATLAS_TLS11": os.environ.get("ATLAS_TLS11"),
40+
"ATLAS_TLS12": os.environ.get("ATLAS_TLS12"),
41+
"ATLAS_SRV_REPL": os.environ.get("ATLAS_SRV_REPL"),
42+
"ATLAS_SRV_SHRD": os.environ.get("ATLAS_SRV_SHRD"),
43+
"ATLAS_SRV_FREE": os.environ.get("ATLAS_SRV_FREE"),
44+
"ATLAS_SRV_TLS11": os.environ.get("ATLAS_SRV_TLS11"),
45+
"ATLAS_SRV_TLS12": os.environ.get("ATLAS_SRV_TLS12"),
46+
}
47+
48+
# Set this variable to true to run the SRV tests even when dnspython is not
49+
# installed.
50+
MUST_TEST_SRV = os.environ.get("MUST_TEST_SRV")
51+
52+
53+
def connect(uri):
54+
if not uri:
55+
raise Exception("Must set env variable to test.")
3556
client = pymongo.MongoClient(uri)
3657
# No TLS error
3758
client.admin.command('ismaster')
@@ -40,29 +61,58 @@ def _connect(uri):
4061

4162

4263
class TestAtlasConnect(unittest.TestCase):
43-
44-
@classmethod
45-
def setUpClass(cls):
46-
if not all([_REPL, _SHRD, _FREE]):
47-
raise Exception(
48-
"Must set ATLAS_REPL/SHRD/FREE env variables to test.")
64+
@unittest.skipUnless(HAS_SNI, 'Free tier requires SNI support')
65+
def test_free_tier(self):
66+
connect(URIS['ATLAS_FREE'])
4967

5068
def test_replica_set(self):
51-
_connect(_REPL)
69+
connect(URIS['ATLAS_REPL'])
5270

5371
def test_sharded_cluster(self):
54-
_connect(_SHRD)
55-
56-
def test_free_tier(self):
57-
if not HAS_SNI:
58-
raise unittest.SkipTest("Free tier requires SNI support.")
59-
_connect(_FREE)
72+
connect(URIS['ATLAS_SHRD'])
6073

6174
def test_tls_11(self):
62-
_connect(_TLS11)
75+
connect(URIS['ATLAS_TLS11'])
6376

6477
def test_tls_12(self):
65-
_connect(_TLS12)
78+
connect(URIS['ATLAS_TLS12'])
79+
80+
def connect_srv(self, uri):
81+
connect(uri)
82+
self.assertIn('mongodb+srv://', uri)
83+
84+
@unittest.skipUnless(HAS_SNI, 'Free tier requires SNI support')
85+
@unittest.skipUnless(HAS_DNS or MUST_TEST_SRV, 'SRV requires dnspython')
86+
def test_srv_free_tier(self):
87+
self.connect_srv(URIS['ATLAS_SRV_FREE'])
88+
89+
@unittest.skipUnless(HAS_DNS or MUST_TEST_SRV, 'SRV requires dnspython')
90+
def test_srv_replica_set(self):
91+
self.connect_srv(URIS['ATLAS_SRV_REPL'])
92+
93+
@unittest.skipUnless(HAS_DNS or MUST_TEST_SRV, 'SRV requires dnspython')
94+
def test_srv_sharded_cluster(self):
95+
self.connect_srv(URIS['ATLAS_SRV_SHRD'])
96+
97+
@unittest.skipUnless(HAS_DNS or MUST_TEST_SRV, 'SRV requires dnspython')
98+
def test_srv_tls_11(self):
99+
self.connect_srv(URIS['ATLAS_SRV_TLS11'])
100+
101+
@unittest.skipUnless(HAS_DNS or MUST_TEST_SRV, 'SRV requires dnspython')
102+
def test_srv_tls_12(self):
103+
self.connect_srv(URIS['ATLAS_SRV_TLS12'])
104+
105+
def test_uniqueness(self):
106+
"""Ensure that we don't accidentally duplicate the test URIs."""
107+
uri_to_names = defaultdict(list)
108+
for name, uri in URIS.items():
109+
if uri:
110+
uri_to_names[uri].append(name)
111+
duplicates = [names for names in uri_to_names.values()
112+
if len(names) > 1]
113+
self.assertFalse(duplicates, 'Error: the following env variables have '
114+
'duplicate values: %s' % (duplicates,))
115+
66116

67117

68118
if __name__ == '__main__':

0 commit comments

Comments
 (0)