Skip to content

Commit e9355df

Browse files
committed
Update command monitoring tests.
1 parent 1146480 commit e9355df

File tree

6 files changed

+251
-4
lines changed

6 files changed

+251
-4
lines changed

src/MongoDB.Driver.Core.Tests/Core/Misc/SemanticVersionTests.cs

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,7 @@ public void Parse_should_handle_valid_semantic_version_strings(string versionStr
7474

7575
[Test]
7676
[TestCase("1")]
77-
[TestCase("1.0")]
78-
[TestCase("1.0.a")]
7977
[TestCase("1-rc2")]
80-
[TestCase("1.0-rc2")]
8178
[TestCase("alpha")]
8279
public void Parse_should_throw_a_FormatException_when_the_version_string_is_invalid(string versionString)
8380
{

src/MongoDB.Driver.Core/Core/Misc/SemanticVersion.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ public static bool TryParse(string value, out SemanticVersion result)
209209
{
210210
var major = int.Parse(match.Groups["major"].Value);
211211
var minor = int.Parse(match.Groups["minor"].Value);
212-
var patch = int.Parse(match.Groups["patch"].Value);
212+
var patch = match.Groups["patch"].Success ? int.Parse(match.Groups["patch"].Value) : 0;
213213
var preRelease = match.Groups["preRelease"].Success ? match.Groups["preRelease"].Value : null;
214214

215215
result = new SemanticVersion(major, minor, patch, preRelease);

src/MongoDB.Driver.Tests/Specifications/command-monitoring/TestRunner.cs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,26 @@ public void TestFixtureSetUp()
9292
[TestCaseSource(typeof(TestCaseFactory), "GetTestCases")]
9393
public async Task RunTestDefinitionAsync(IEnumerable<BsonDocument> data, string databaseName, string collectionName, BsonDocument definition)
9494
{
95+
BsonValue bsonValue;
96+
if (definition.TryGetValue("ignore_if_server_version_greater_than", out bsonValue))
97+
{
98+
var serverVersion = CoreTestConfiguration.ServerVersion;
99+
var maxServerVersion = SemanticVersion.Parse(bsonValue.AsString);
100+
if (serverVersion > maxServerVersion)
101+
{
102+
Assert.Ignore($"Test ignored because server version {serverVersion} is greater than max server version {maxServerVersion}.");
103+
}
104+
}
105+
if (definition.TryGetValue("ignore_if_server_version_less_than", out bsonValue))
106+
{
107+
var serverVersion = CoreTestConfiguration.ServerVersion;
108+
var minServerVersion = SemanticVersion.Parse(bsonValue.AsString);
109+
if (serverVersion < minServerVersion)
110+
{
111+
Assert.Ignore($"Test ignored because server version {serverVersion} is less than min server version {minServerVersion}.");
112+
}
113+
}
114+
95115
var database = __client
96116
.GetDatabase(databaseName);
97117
var collection = database
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
.. role:: javascript(code)
2+
:language: javascript
3+
4+
==================
5+
Command Monitoring
6+
==================
7+
8+
.. contents::
9+
10+
--------
11+
12+
Testing
13+
=======
14+
15+
Tests are provided in YML and JSON format to assert proper upconversion of commands.
16+
17+
Data
18+
----
19+
20+
The {{data}} at the beginning of each test file is the data that should exist in the
21+
collection under test before each test run.
22+
23+
Expectations
24+
------------
25+
26+
Fake Placeholder Values
27+
```````````````````````
28+
29+
When an attribute in an expectation contains the value {{"42"}}, {{42}} or {{""}}, this is a fake
30+
placeholder value indicating that a special case MUST be tested that could not be
31+
expressed in a YAML or JSON test. These cases are as follows:
32+
33+
Cursor Matching
34+
^^^^^^^^^^^^^^^
35+
36+
When encountering a {{cursor}} or {{getMore}} value of {{"42"}} in a test, the driver MUST assert
37+
that the values are equal to each other and greater than zero.
38+
39+
Errors
40+
^^^^^^
41+
42+
For write errors, {{code}} values of {{42}} MUST assert that the value is present and
43+
greater than zero. {{errmsg}} values of {{""}} MUST assert that the value is not empty
44+
(a string of length greater than 1).
45+
46+
OK Values
47+
^^^^^^^^^
48+
49+
The server is inconsistent on whether the ok values returned are integers or doubles so
50+
for simplicity the tests specify all expected values as doubles. Server 'ok' values of
51+
integers MUST be converted to doubles for comparison with the expected values.
52+
53+
Additional Values
54+
`````````````````
55+
56+
The expected events provide the minimum data that is required and can be tested. It is
57+
possible for more values to be present in the events, such as extra data provided when
58+
using sharded clusters or ``nModified`` field in updates. The driver MUST assert the
59+
expected data is present and also MUST allow for additional data to be present as well.
60+
61+
Ignoring Tests Based On Server Version
62+
``````````````````````````````````````
63+
64+
Due to variations in server behaviour, some tests may not be valid on a specific range
65+
of server versions and MUST NOT be run. These tests are indicated with the fields
66+
{{ignore_if_server_version_greater_than}} and {{ignore_if_server_version_less_than}} which
67+
are optionally provided at the description level of the test. When determining if the test
68+
must be run or not, use only the minor server version.
69+
70+
Example:
71+
72+
If {{ignore_if_server_version_greater_than}} is {{3.0}}, then the tests MUST NOT run on
73+
{{3.1}} and higher, but MUST run on {{3.0.3}}.
74+
75+
Tests which do not have either one of these fields MUST run on all supported server
76+
versions.

src/MongoDB.Driver.Tests/Specifications/command-monitoring/tests/find.json

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@
270270
},
271271
{
272272
"description": "A successful find event with a getmore and killcursors",
273+
"ignore_if_server_version_greater_than": "3.0",
273274
"operation": {
274275
"name": "find",
275276
"arguments": {
@@ -395,6 +396,107 @@
395396
}
396397
]
397398
},
399+
{
400+
"description": "A successful find event with a getmore and the server kills the cursor",
401+
"ignore_if_server_version_less_than": "3.1",
402+
"operation": {
403+
"name": "find",
404+
"arguments": {
405+
"filter": {
406+
"_id": {
407+
"$gte": 1
408+
}
409+
},
410+
"sort": {
411+
"_id": 1
412+
},
413+
"batchSize": 3,
414+
"limit": 4
415+
}
416+
},
417+
"expectations": [
418+
{
419+
"command_started_event": {
420+
"command": {
421+
"find": "test",
422+
"filter": {
423+
"_id": {
424+
"$gte": 1
425+
}
426+
},
427+
"sort": {
428+
"_id": 1
429+
},
430+
"batchSize": 3,
431+
"limit": 4
432+
},
433+
"command_name": "find",
434+
"database_name": "command-monitoring-tests"
435+
}
436+
},
437+
{
438+
"command_succeeded_event": {
439+
"reply": {
440+
"ok": 1,
441+
"cursor": {
442+
"id": {
443+
"$numberLong": "42"
444+
},
445+
"ns": "command-monitoring-tests.test",
446+
"firstBatch": [
447+
{
448+
"_id": 1,
449+
"x": 11
450+
},
451+
{
452+
"_id": 2,
453+
"x": 22
454+
},
455+
{
456+
"_id": 3,
457+
"x": 33
458+
}
459+
]
460+
}
461+
},
462+
"command_name": "find"
463+
}
464+
},
465+
{
466+
"command_started_event": {
467+
"command": {
468+
"getMore": {
469+
"$numberLong": "42"
470+
},
471+
"collection": "test",
472+
"batchSize": 3
473+
},
474+
"command_name": "getMore",
475+
"database_name": "command-monitoring-tests"
476+
}
477+
},
478+
{
479+
"command_succeeded_event": {
480+
"reply": {
481+
"ok": 1,
482+
"cursor": {
483+
"id": {
484+
"$numberLong": "0"
485+
},
486+
"ns": "command-monitoring-tests.test",
487+
"nextBatch": [
488+
{
489+
"_id": 4,
490+
"x": 44
491+
}
492+
]
493+
}
494+
},
495+
"command_name": "getMore"
496+
}
497+
}
498+
]
499+
},
398500
{
399501
"description": "A failed find event",
400502
"operation": {

src/MongoDB.Driver.Tests/Specifications/command-monitoring/tests/find.yml

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ tests:
134134
command_name: "getMore"
135135
-
136136
description: "A successful find event with a getmore and killcursors"
137+
ignore_if_server_version_greater_than: "3.0"
137138
operation:
138139
name: "find"
139140
arguments:
@@ -197,6 +198,57 @@ tests:
197198
cursorsUnknown:
198199
- { $numberLong: "42" }
199200
command_name: "killCursors"
201+
-
202+
description: "A successful find event with a getmore and the server kills the cursor"
203+
ignore_if_server_version_less_than: "3.1"
204+
operation:
205+
name: "find"
206+
arguments:
207+
filter: { _id: { $gte: 1 }}
208+
sort: { _id: 1 }
209+
batchSize: 3
210+
limit: 4
211+
expectations:
212+
-
213+
command_started_event:
214+
command:
215+
find: *collection_name
216+
filter: { _id: { $gte : 1 }}
217+
sort: { _id: 1 }
218+
batchSize: 3
219+
limit: 4
220+
command_name: "find"
221+
database_name: *database_name
222+
-
223+
command_succeeded_event:
224+
reply:
225+
ok: 1.0
226+
cursor:
227+
id: { $numberLong: "42" }
228+
ns: *namespace
229+
firstBatch:
230+
- { _id: 1, x: 11 }
231+
- { _id: 2, x: 22 }
232+
- { _id: 3, x: 33 }
233+
command_name: "find"
234+
-
235+
command_started_event:
236+
command:
237+
getMore: { $numberLong: "42" }
238+
collection: *collection_name
239+
batchSize: 3
240+
command_name: "getMore"
241+
database_name: *database_name
242+
-
243+
command_succeeded_event:
244+
reply:
245+
ok: 1.0
246+
cursor:
247+
id: { $numberLong: "0" }
248+
ns: *namespace
249+
nextBatch:
250+
- { _id: 4, x: 44 }
251+
command_name: "getMore"
200252
-
201253
description: "A failed find event"
202254
operation:

0 commit comments

Comments
 (0)