PYTHON-3789 Use tox for Evergreen mockupdb tests (#1277)

This commit is contained in:
Noah Stapp 2023-06-28 10:57:23 -07:00 committed by GitHub
parent 01dd2f8ce0
commit d5882075d6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 60 additions and 68 deletions

View File

@ -357,7 +357,9 @@ functions:
script: |
set -o xtrace
${PREPARE_SHELL}
PYTHON_BINARY=${PYTHON_BINARY} bash ${PROJECT_DIRECTORY}/.evergreen/run-mockupdb-tests.sh
alias python=${PYTHON_BINARY}
python -m tox -e test-mockupdb
"run doctests":
- command: shell.exec

View File

@ -1,18 +0,0 @@
#!/bin/bash
# Must be run from pymongo repo root
set -o xtrace
set -o errexit
. .evergreen/utils.sh
${PYTHON_BINARY} setup.py clean
createvirtualenv ${PYTHON_BINARY} mockuptests
trap "deactivate; rm -rf mockuptests" EXIT HUP
# Install PyMongo from git clone so mockup-tests don't
# download it from pypi.
python -m pip install .
python -m pip install --upgrade 'https://github.com/ajdavis/mongo-mockup-db/archive/master.zip'
cd ./test/mockupdb
python -m unittest discover -v

View File

@ -23,49 +23,6 @@ from pymongo.errors import OperationFailure
from pymongo.server_api import ServerApi, ServerApiVersion
def test_hello_with_option(self, protocol, **kwargs):
hello = "ismaster" if isinstance(protocol(), OpQuery) else "hello"
# `db.command("hello"|"ismaster")` commands are the same for primaries and
# secondaries, so we only need one server.
primary = MockupDB()
# Set up a custom handler to save the first request from the driver.
self.handshake_req = None
def respond(r):
# Only save the very first request from the driver.
if self.handshake_req is None:
self.handshake_req = r
load_balanced_kwargs = {"serviceId": ObjectId()} if kwargs.get("loadBalanced") else {}
return r.reply(
OpMsgReply(minWireVersion=0, maxWireVersion=13, **kwargs, **load_balanced_kwargs)
)
primary.autoresponds(respond)
primary.run()
self.addCleanup(primary.stop)
# We need a special dict because MongoClient uses "server_api" and all
# of the commands use "apiVersion".
k_map = {("apiVersion", "1"): ("server_api", ServerApi(ServerApiVersion.V1))}
client = MongoClient(
"mongodb://" + primary.address_string,
appname="my app", # For _check_handshake_data()
**dict([k_map.get((k, v), (k, v)) for k, v in kwargs.items()]) # type: ignore[arg-type]
)
self.addCleanup(client.close)
# We have an autoresponder luckily, so no need for `go()`.
assert client.db.command(hello)
# We do this checking here rather than in the autoresponder `respond()`
# because it runs in another Python thread so there are some funky things
# with error handling within that thread, and we want to be able to use
# self.assertRaises().
self.handshake_req.assert_matches(protocol(hello, **kwargs))
_check_handshake_data(self.handshake_req)
def _check_handshake_data(request):
assert "client" in request
data = request["client"]
@ -79,6 +36,48 @@ def _check_handshake_data(request):
class TestHandshake(unittest.TestCase):
def hello_with_option_helper(self, protocol, **kwargs):
hello = "ismaster" if isinstance(protocol(), OpQuery) else "hello"
# `db.command("hello"|"ismaster")` commands are the same for primaries and
# secondaries, so we only need one server.
primary = MockupDB()
# Set up a custom handler to save the first request from the driver.
self.handshake_req = None
def respond(r):
# Only save the very first request from the driver.
if self.handshake_req is None:
self.handshake_req = r
load_balanced_kwargs = {"serviceId": ObjectId()} if kwargs.get("loadBalanced") else {}
return r.reply(
OpMsgReply(minWireVersion=0, maxWireVersion=13, **kwargs, **load_balanced_kwargs)
)
primary.autoresponds(respond)
primary.run()
self.addCleanup(primary.stop)
# We need a special dict because MongoClient uses "server_api" and all
# of the commands use "apiVersion".
k_map = {("apiVersion", "1"): ("server_api", ServerApi(ServerApiVersion.V1))}
client = MongoClient(
"mongodb://" + primary.address_string,
appname="my app", # For _check_handshake_data()
**dict([k_map.get((k, v), (k, v)) for k, v in kwargs.items()]) # type: ignore[arg-type]
)
self.addCleanup(client.close)
# We have an autoresponder luckily, so no need for `go()`.
assert client.db.command(hello)
# We do this checking here rather than in the autoresponder `respond()`
# because it runs in another Python thread so there are some funky things
# with error handling within that thread, and we want to be able to use
# self.assertRaises().
self.handshake_req.assert_matches(protocol(hello, **kwargs))
_check_handshake_data(self.handshake_req)
def test_client_handshake_data(self):
primary, secondary = MockupDB(), MockupDB()
for server in primary, secondary:
@ -208,21 +207,21 @@ class TestHandshake(unittest.TestCase):
return
def test_handshake_load_balanced(self):
test_hello_with_option(self, OpMsg, loadBalanced=True)
self.hello_with_option_helper(OpMsg, loadBalanced=True)
with self.assertRaisesRegex(AssertionError, "does not match"):
test_hello_with_option(self, Command, loadBalanced=True)
self.hello_with_option_helper(Command, loadBalanced=True)
def test_handshake_versioned_api(self):
test_hello_with_option(self, OpMsg, apiVersion="1")
self.hello_with_option_helper(OpMsg, apiVersion="1")
with self.assertRaisesRegex(AssertionError, "does not match"):
test_hello_with_option(self, Command, apiVersion="1")
self.hello_with_option_helper(Command, apiVersion="1")
def test_handshake_not_either(self):
# If we don't specify either option then it should be using
# OP_QUERY for the initial step of the handshake.
test_hello_with_option(self, Command)
self.hello_with_option_helper(Command)
with self.assertRaisesRegex(AssertionError, "does not match"):
test_hello_with_option(self, OpMsg)
self.hello_with_option_helper(OpMsg)
def test_handshake_max_wire(self):
server = MockupDB()

View File

@ -121,3 +121,12 @@ deps =
{[testenv:doc]deps}
commands =
sphinx-build -E -b linkcheck doc ./doc/_build/linkcheck
[testenv:test-mockupdb]
description = run mockupdb tests
deps =
{[testenv:test]deps}
https://github.com/ajdavis/mongo-mockup-db/archive/master.zip
passenv = *
commands =
python -m pytest -v ./test/mockupdb