Skip to content

Commit 5dcbbab

Browse files
authored
RUST-1334 Use unified format command monitoring tests (mongodb#667)
1 parent 3d9deea commit 5dcbbab

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+2925
-2692
lines changed

.evergreen/run-serverless-tests.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,8 @@ cargo_test test::spec::transactions > transactions.xml
4646
cargo_test test::spec::load_balancers > load_balancers.xml
4747
cargo_test test::cursor > cursor.xml
4848
cargo_test test::spec::collection_management > coll.xml
49+
cargo_test test::spec::command_monitoring_unified > monitoring.xml
4950

50-
junit-report-merger results.xml crud.xml retryable_reads.xml retryable_writes.xml versioned_api.xml sessions.xml transactions.xml load_balancers.xml cursor.xml coll.xml
51+
junit-report-merger results.xml crud.xml retryable_reads.xml retryable_writes.xml versioned_api.xml sessions.xml transactions.xml load_balancers.xml cursor.xml coll.xml monitoring.xml
5152

5253
exit $CARGO_RESULT
Lines changed: 11 additions & 123 deletions
Original file line numberDiff line numberDiff line change
@@ -1,130 +1,18 @@
1-
mod event;
2-
mod operation;
1+
use crate::test::{spec::run_spec_test, LOCK};
32

4-
use std::convert::Into;
5-
6-
use semver::VersionReq;
7-
use serde::Deserialize;
8-
use tokio::sync::RwLockWriteGuard;
9-
10-
use self::{event::TestEvent, operation::*};
11-
use crate::{
12-
bson::{Bson, Document},
13-
options::ClientOptions,
14-
test::{assert_matches, log_uncaptured, util::TestClient, EventClient, CLIENT_OPTIONS, LOCK},
15-
};
16-
17-
#[derive(Deserialize)]
18-
struct TestFile {
19-
data: Vec<Document>,
20-
collection_name: String,
21-
database_name: String,
22-
tests: Vec<TestCase>,
23-
}
24-
25-
#[derive(Deserialize)]
26-
struct TestCase {
27-
description: String,
28-
#[serde(rename = "ignore_if_server_version_greater_than", default)]
29-
max_version: Option<String>,
30-
#[serde(rename = "ignore_if_server_version_less_than", default)]
31-
min_version: Option<String>,
32-
operation: Document,
33-
expectations: Vec<TestEvent>,
34-
}
35-
36-
async fn run_command_monitoring_test(test_file: TestFile) {
37-
let _guard: RwLockWriteGuard<()> = LOCK.run_exclusively().await;
38-
39-
let client = TestClient::new().await;
40-
41-
let skipped_tests = vec![
42-
// uses old count
43-
"A successful command",
44-
"A failed command event",
45-
"A successful command with a non-primary read preference",
46-
// bulk write not implemented
47-
"A successful mixed bulk write",
48-
"A successful unordered bulk write with an unacknowledged write concern",
49-
// We can't pass this test since it relies on old OP_QUERY behavior (SPEC-1519)
50-
"A successful find event with a getmore and the server kills the cursor (<= 4.4)",
51-
];
52-
53-
for test_case in test_file.tests {
54-
if skipped_tests.iter().any(|st| st == &test_case.description) {
55-
log_uncaptured(format!("Skipping {}", test_case.description));
56-
continue;
57-
}
58-
59-
if let Some(ref max_version) = test_case.max_version {
60-
let req = VersionReq::parse(&format!("<= {}", &max_version)).unwrap();
61-
if !req.matches(&client.server_version) {
62-
log_uncaptured(format!("Skipping {}", test_case.description));
63-
continue;
64-
}
65-
}
66-
67-
if let Some(ref min_version) = test_case.min_version {
68-
let req = VersionReq::parse(&format!(">= {}", &min_version)).unwrap();
69-
if !req.matches(&client.server_version) {
70-
log_uncaptured(format!("Skipping {}", test_case.description));
71-
continue;
72-
}
73-
}
74-
75-
println!("Running {}", test_case.description);
76-
77-
let operation: AnyTestOperation =
78-
bson::from_bson(Bson::Document(test_case.operation)).unwrap();
79-
80-
client
81-
.init_db_and_coll(&test_file.database_name, &test_file.collection_name)
82-
.await
83-
.insert_many(test_file.data.clone(), None)
84-
.await
85-
.expect("insert many error");
86-
87-
let options = ClientOptions::builder()
88-
.retry_writes(false)
89-
.hosts(CLIENT_OPTIONS.get().await.hosts.clone())
90-
.build();
91-
let client =
92-
EventClient::with_additional_options(Some(options), None, Some(false), None).await;
93-
94-
let events: Vec<TestEvent> = client
95-
.run_operation_with_events(
96-
operation,
97-
&test_file.database_name,
98-
&test_file.collection_name,
99-
)
100-
.await
101-
.into_iter()
102-
.map(Into::into)
103-
.collect();
104-
105-
assert_eq!(events.len(), test_case.expectations.len());
106-
for (actual_event, expected_event) in events.iter().zip(test_case.expectations.iter()) {
107-
assert_matches(actual_event, expected_event, None);
108-
}
109-
}
110-
}
111-
112-
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
113-
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
114-
async fn command_monitoring_legacy() {
115-
crate::test::run_spec_test(
116-
&["command-monitoring", "legacy"],
117-
run_command_monitoring_test,
118-
)
119-
.await;
120-
}
3+
use super::{run_unified_format_test_filtered, unified_runner::TestCase};
1214

1225
#[cfg_attr(feature = "tokio-runtime", tokio::test)]
1236
#[cfg_attr(feature = "async-std-runtime", async_std::test)]
1247
async fn command_monitoring_unified() {
125-
crate::test::run_spec_test(
126-
&["command-monitoring", "unified"],
127-
super::run_unified_format_test,
128-
)
8+
let _guard = LOCK.run_exclusively().await;
9+
10+
let pred = |tc: &TestCase|
11+
// This test relies on old OP_QUERY behavior that many drivers still use for < 4.4, but we do not use, due to never implementing OP_QUERY.
12+
tc.description != "A successful find event with a getmore and the server kills the cursor (<= 4.4)";
13+
14+
run_spec_test(&["command-monitoring", "unified"], |f| {
15+
run_unified_format_test_filtered(f, pred)
16+
})
12917
.await;
13018
}

src/test/spec/json/command-monitoring/README.rst

Lines changed: 1 addition & 117 deletions
Original file line numberDiff line numberDiff line change
@@ -12,120 +12,4 @@ Command Monitoring
1212
Testing
1313
=======
1414

15-
Tests in ``unified`` are implemented in the `Unified Test Format <../../unified-test-format/unified-test-format.rst>`__ and require
16-
schema version 1.0. Tests in ``legacy`` should be run as described below.
17-
18-
Tests are provided in YML and JSON format to assert proper upconversion of commands.
19-
20-
Database and Collection Names
21-
-----------------------------
22-
23-
The collection under test is specified in each test file with the fields
24-
``database_name`` and ``collection_name``.
25-
26-
Data
27-
----
28-
29-
The ``data`` at the beginning of each test file is the data that should exist in the
30-
collection under test before each test run.
31-
32-
Expectations
33-
------------
34-
35-
Fake Placeholder Values
36-
```````````````````````
37-
38-
When an attribute in an expectation contains the value ``"42"``, ``42`` or ``""``, this is a fake
39-
placeholder value indicating that a special case MUST be tested that could not be
40-
expressed in a YAML or JSON test. These cases are as follows:
41-
42-
Cursor Matching
43-
^^^^^^^^^^^^^^^
44-
45-
When encountering a ``cursor`` or ``getMore`` value of ``"42"`` in a test, the driver MUST assert
46-
that the values are equal to each other and greater than zero.
47-
48-
Errors
49-
^^^^^^
50-
51-
For write errors, ``code`` values of ``42`` MUST assert that the value is present and
52-
greater than zero. ``errmsg`` values of ``""`` MUST assert that the value is not empty
53-
(a string of length greater than 1).
54-
55-
OK Values
56-
^^^^^^^^^
57-
58-
The server is inconsistent on whether the ok values returned are integers or doubles so
59-
for simplicity the tests specify all expected values as doubles. Server 'ok' values of
60-
integers MUST be converted to doubles for comparison with the expected values.
61-
62-
Additional Values
63-
`````````````````
64-
65-
The expected events provide the minimum data that is required and can be tested. It is
66-
possible for more values to be present in the events, such as extra data provided when
67-
using sharded clusters or ``nModified`` field in updates. The driver MUST assert the
68-
expected data is present and also MUST allow for additional data to be present as well
69-
at the top level of the command document or reply document.
70-
71-
For example, say the client sends a causally-consistent "distinct" command with
72-
readConcern level "majority", like::
73-
74-
{
75-
"distinct": "collection",
76-
"key": "key",
77-
"readConcern":{
78-
"afterClusterTime": {"$timestamp":{"t":1522336030,"i":1}},
79-
"level":"majority"
80-
},
81-
"$clusterTime": {
82-
"clusterTime": { "$timestamp": { "i": 1, "t": 1522335530 } },
83-
"signature": {
84-
"hash": { "$binary": "AAAAAAAAAAAAAAAAAAAAAAAAAAA=", "$type": "00" },
85-
"keyId": { "$numberLong": "0" }
86-
}
87-
},
88-
"lsid": {
89-
"id": { "$binary": "RaigP3oASqu+galPvRAfcg==", "$type": "04" }
90-
}
91-
}
92-
93-
Then it would pass a command-started event like the following YAML, because the
94-
fields not mentioned in the YAML are ignored::
95-
96-
command:
97-
distinct: collection
98-
key: key
99-
100-
However, if there are fields in command subdocuments that are not mentioned in
101-
the YAML, then the command does *not* pass the test::
102-
103-
command:
104-
distinct: collection
105-
key: key
106-
# Fails because the expected readConcern has no "afterClusterTime".
107-
readConcern:
108-
level: majority
109-
110-
Ignoring Tests Based On Server Version or Topology Type
111-
```````````````````````````````````````````````````````
112-
113-
Due to variations in server behavior, some tests may not be valid and MUST NOT be run on
114-
certain server versions or topology types. These tests are indicated with any of the
115-
following fields, which will be optionally provided at the ``description`` level of each
116-
test:
117-
118-
- ``ignore_if_server_version_greater_than`` (optional): If specified, the test MUST be
119-
skipped if the minor version of the server is greater than this minor version. The
120-
server's patch version MUST NOT be considered. For example, a value of ``3.0`` implies
121-
that the test can run on server version ``3.0.15`` but not ``3.1.0``.
122-
123-
- ``ignore_if_server_version_less_than`` (optional): If specified, the test MUST be
124-
skipped if the minor version of the server is less than this minor version. The
125-
server's patch version MUST NOT be considered. For example, a value of ``3.2`` implies
126-
that the test can run on server version ``3.2.0`` but not ``3.0.15``.
127-
128-
- ``ignore_if_topology_type`` (optional): An array of server topologies for which the test
129-
MUST be skipped. Valid topologies are "single", "replicaset", and "sharded".
130-
131-
Tests that have none of these fields MUST be run on all supported server versions.
15+
Tests in ``unified`` are implemented in the `Unified Test Format <../../unified-test-format/unified-test-format.rst>`__.

src/test/spec/json/command-monitoring/legacy/bulkWrite.json

Lines changed: 0 additions & 110 deletions
This file was deleted.

0 commit comments

Comments
 (0)