PYTHON-2684 Send Versioned API options with getMore+txn commands (#618)

This commit is contained in:
Shane Harvey 2021-05-17 15:26:50 -07:00 committed by GitHub
parent 048ee81836
commit e221b49dfc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 62 additions and 241 deletions

View File

@ -305,7 +305,7 @@ class _Query(object):
self.name = 'explain'
cmd = SON([('explain', cmd)])
session = self.session
sock_info.add_server_api(cmd, session)
sock_info.add_server_api(cmd)
if session:
session._apply_to(cmd, False, self.read_preference)
# Explain does not support readConcern.
@ -409,6 +409,7 @@ class _GetMore(object):
if self.session:
self.session._apply_to(cmd, False, self.read_preference)
sock_info.add_server_api(cmd)
sock_info.send_cluster_time(cmd, self.session, self.client)
# Support auto encryption
client = self.client
@ -891,7 +892,7 @@ class _BulkWriteContext(object):
self.compress = True if sock_info.compression_context else False
self.op_type = op_type
self.codec = codec
sock_info.add_server_api(command, session)
sock_info.add_server_api(command)
def _batch_command(self, docs):
namespace = self.db_name + '.$cmd'

View File

@ -668,7 +668,7 @@ class SocketInfo(object):
raise ConfigurationError(
'Must be connected to MongoDB 3.4+ to use a collation.')
self.add_server_api(spec, session)
self.add_server_api(spec)
if session:
session._apply_to(spec, retryable_write, read_preference)
self.send_cluster_time(spec, session, client)
@ -846,11 +846,8 @@ class SocketInfo(object):
if self.max_wire_version >= 6 and client:
client._send_cluster_time(command, session)
def add_server_api(self, command, session):
def add_server_api(self, command):
"""Add server_api parameters."""
if (session and session.in_transaction and
not session._starting_transaction):
return
if self.opts.server_api:
_add_to_command(command, self.opts.server_api)

View File

@ -68,10 +68,9 @@ class TestServerApi(IntegrationTest):
def assertNoServerApi(self, event):
self.assertNotIn('apiVersion', event.command)
def assertServerApiOnlyInFirstCommand(self, events):
self.assertServerApi(events[0])
for event in events[1:]:
self.assertNoServerApi(event)
def assertServerApiInAllCommands(self, events):
for event in events:
self.assertServerApi(event)
@client_context.require_version_min(4, 7)
def test_command_options(self):
@ -84,11 +83,7 @@ class TestServerApi(IntegrationTest):
self.addCleanup(coll.delete_many, {})
list(coll.find(batch_size=25))
client.admin.command('ping')
for event in listener.results['started']:
if event.command_name == 'getMore':
self.assertNoServerApi(event)
else:
self.assertServerApi(event)
self.assertServerApiInAllCommands(listener.results['started'])
@client_context.require_version_min(4, 7)
@client_context.require_transactions
@ -106,21 +101,7 @@ class TestServerApi(IntegrationTest):
coll.insert_many([{} for _ in range(100)], session=s)
list(coll.find(batch_size=25, session=s))
client.test.command('find', 'test', session=s)
self.assertServerApiOnlyInFirstCommand(listener.results['started'])
listener.reset()
with client.start_session() as s, s.start_transaction():
list(coll.find(batch_size=25, session=s))
coll.insert_many([{} for _ in range(100)], session=s)
client.test.command('find', 'test', session=s)
self.assertServerApiOnlyInFirstCommand(listener.results['started'])
listener.reset()
with client.start_session() as s, s.start_transaction():
client.test.command('find', 'test', session=s)
list(coll.find(batch_size=25, session=s))
coll.insert_many([{} for _ in range(100)], session=s)
self.assertServerApiOnlyInFirstCommand(listener.results['started'])
self.assertServerApiInAllCommands(listener.results['started'])
if __name__ == "__main__":

View File

@ -520,7 +520,7 @@ class UnifiedSpecTestMixinV1(IntegrationTest):
Specification of the test suite being currently run is available as
a class attribute ``TEST_SPEC``.
"""
SCHEMA_VERSION = Version.from_string('1.1')
SCHEMA_VERSION = Version.from_string('1.4')
@staticmethod
def should_run_on(run_on_spec):

View File

@ -1,6 +1,6 @@
{
"description": "CRUD Api Version 1 (strict)",
"schemaVersion": "1.1",
"schemaVersion": "1.4",
"runOnRequirements": [
{
"minServerVersion": "4.9"
@ -141,6 +141,11 @@
},
{
"description": "aggregate on database appends declared API version",
"runOnRequirements": [
{
"serverless": "forbid"
}
],
"operations": [
{
"name": "aggregate",
@ -651,7 +656,7 @@
]
},
{
"description": "find command with declared API version appends to the command, but getMore does not",
"description": "find and getMore append API version",
"operations": [
{
"name": "find",
@ -712,14 +717,10 @@
"long"
]
},
"apiVersion": {
"$$exists": false
},
"apiStrict": {
"$$exists": false
},
"apiVersion": "1",
"apiStrict": true,
"apiDeprecationErrors": {
"$$exists": false
"$$unsetOrMatches": false
}
}
}

View File

@ -1,6 +1,6 @@
{
"description": "CRUD Api Version 1",
"schemaVersion": "1.1",
"schemaVersion": "1.4",
"runOnRequirements": [
{
"minServerVersion": "4.9"
@ -141,6 +141,11 @@
},
{
"description": "aggregate on database appends declared API version",
"runOnRequirements": [
{
"serverless": "forbid"
}
],
"operations": [
{
"name": "aggregate",
@ -643,7 +648,7 @@
]
},
{
"description": "find command with declared API version appends to the command, but getMore does not",
"description": "find and getMore append API version",
"operations": [
{
"name": "find",
@ -704,15 +709,11 @@
"long"
]
},
"apiVersion": {
"$$exists": false
},
"apiVersion": "1",
"apiStrict": {
"$$exists": false
"$$unsetOrMatches": false
},
"apiDeprecationErrors": {
"$$exists": false
}
"apiDeprecationErrors": true
}
}
}

View File

@ -1,6 +1,6 @@
{
"description": "RunCommand helper: No API version declared",
"schemaVersion": "1.1",
"schemaVersion": "1.4",
"runOnRequirements": [
{
"minServerVersion": "4.9",
@ -29,6 +29,11 @@
"tests": [
{
"description": "runCommand does not inspect or change the command document",
"runOnRequirements": [
{
"serverless": "forbid"
}
],
"operations": [
{
"name": "runCommand",
@ -72,6 +77,11 @@
},
{
"description": "runCommand does not prevent sending invalid API version declarations",
"runOnRequirements": [
{
"serverless": "forbid"
}
],
"operations": [
{
"name": "runCommand",

View File

@ -1,12 +1,13 @@
{
"description": "Test commands: strict mode",
"schemaVersion": "1.1",
"schemaVersion": "1.4",
"runOnRequirements": [
{
"minServerVersion": "4.9",
"serverParameters": {
"enableTestCommands": true
}
},
"serverless": "forbid"
}
],
"createEntities": [

View File

@ -6,7 +6,8 @@
"minServerVersion": "4.9",
"topologies": [
"replicaset",
"sharded-replicaset"
"sharded-replicaset",
"load-balanced"
]
}
],
@ -53,17 +54,6 @@
"apiDeprecationErrors": {
"$$unsetOrMatches": false
}
},
{
"apiVersion": {
"$$exists": false
},
"apiStrict": {
"$$exists": false
},
"apiDeprecationErrors": {
"$$exists": false
}
}
]
},
@ -97,12 +87,13 @@
],
"tests": [
{
"description": "Only the first command in a transaction declares an API version",
"description": "All commands in a transaction declare an API version",
"runOnRequirements": [
{
"topologies": [
"replicaset",
"sharded-replicaset"
"sharded-replicaset",
"load-balanced"
]
}
],
@ -193,119 +184,6 @@
"lsid": {
"$$sessionLsid": "session"
},
"apiVersion": {
"$$exists": false
},
"apiStrict": {
"$$exists": false
},
"apiDeprecationErrors": {
"$$exists": false
}
}
}
},
{
"commandStartedEvent": {
"command": {
"commitTransaction": 1,
"lsid": {
"$$sessionLsid": "session"
},
"apiVersion": {
"$$exists": false
},
"apiStrict": {
"$$exists": false
},
"apiDeprecationErrors": {
"$$exists": false
}
}
}
}
]
}
]
},
{
"description": "Committing a transaction twice does not append server API options",
"runOnRequirements": [
{
"topologies": [
"replicaset",
"sharded-replicaset"
]
}
],
"operations": [
{
"name": "startTransaction",
"object": "session"
},
{
"name": "insertOne",
"object": "collection",
"arguments": {
"session": "session",
"document": {
"_id": 6,
"x": 66
}
},
"expectResult": {
"$$unsetOrMatches": {
"insertedId": {
"$$unsetOrMatches": 6
}
}
}
},
{
"name": "insertOne",
"object": "collection",
"arguments": {
"session": "session",
"document": {
"_id": 7,
"x": 77
}
},
"expectResult": {
"$$unsetOrMatches": {
"insertedId": {
"$$unsetOrMatches": 7
}
}
}
},
{
"name": "commitTransaction",
"object": "session"
},
{
"name": "commitTransaction",
"object": "session"
}
],
"expectEvents": [
{
"client": "client",
"events": [
{
"commandStartedEvent": {
"command": {
"insert": "test",
"documents": [
{
"_id": 6,
"x": 66
}
],
"lsid": {
"$$sessionLsid": "session"
},
"startTransaction": true,
"apiVersion": "1",
"apiStrict": {
"$$unsetOrMatches": false
@ -316,31 +194,6 @@
}
}
},
{
"commandStartedEvent": {
"command": {
"insert": "test",
"documents": [
{
"_id": 7,
"x": 77
}
],
"lsid": {
"$$sessionLsid": "session"
},
"apiVersion": {
"$$exists": false
},
"apiStrict": {
"$$exists": false
},
"apiDeprecationErrors": {
"$$exists": false
}
}
}
},
{
"commandStartedEvent": {
"command": {
@ -348,33 +201,12 @@
"lsid": {
"$$sessionLsid": "session"
},
"apiVersion": {
"$$exists": false
},
"apiVersion": "1",
"apiStrict": {
"$$exists": false
"$$unsetOrMatches": false
},
"apiDeprecationErrors": {
"$$exists": false
}
}
}
},
{
"commandStartedEvent": {
"command": {
"commitTransaction": 1,
"lsid": {
"$$sessionLsid": "session"
},
"apiVersion": {
"$$exists": false
},
"apiStrict": {
"$$exists": false
},
"apiDeprecationErrors": {
"$$exists": false
"$$unsetOrMatches": false
}
}
}
@ -384,12 +216,13 @@
]
},
{
"description": "abortTransaction does not include an API version",
"description": "abortTransaction includes an API version",
"runOnRequirements": [
{
"topologies": [
"replicaset",
"sharded-replicaset"
"sharded-replicaset",
"load-balanced"
]
}
],
@ -480,14 +313,12 @@
"lsid": {
"$$sessionLsid": "session"
},
"apiVersion": {
"$$exists": false
},
"apiVersion": "1",
"apiStrict": {
"$$exists": false
"$$unsetOrMatches": false
},
"apiDeprecationErrors": {
"$$exists": false
"$$unsetOrMatches": false
}
}
}
@ -499,14 +330,12 @@
"lsid": {
"$$sessionLsid": "session"
},
"apiVersion": {
"$$exists": false
},
"apiVersion": "1",
"apiStrict": {
"$$exists": false
"$$unsetOrMatches": false
},
"apiDeprecationErrors": {
"$$exists": false
"$$unsetOrMatches": false
}
}
}