| 1 |
===================================== |
| 2 |
Server Discovery And Monitoring Tests |
| 3 |
===================================== |
| 4 |
|
| 5 |
.. contents:: |
| 6 |
|
| 7 |
---- |
| 8 |
|
| 9 |
The YAML and JSON files in this directory tree are platform-independent tests |
| 10 |
that drivers can use to prove their conformance to the |
| 11 |
Server Discovery And Monitoring Spec. |
| 12 |
|
| 13 |
Additional prose tests, that cannot be represented as spec tests, are |
| 14 |
described and MUST be implemented. |
| 15 |
|
| 16 |
Version |
| 17 |
------- |
| 18 |
|
| 19 |
Files in the "specifications" repository have no version scheme. They are not |
| 20 |
tied to a MongoDB server version. |
| 21 |
|
| 22 |
Format |
| 23 |
------ |
| 24 |
|
| 25 |
Each YAML file has the following keys: |
| 26 |
|
| 27 |
- description: A textual description of the test. |
| 28 |
- uri: A connection string. |
| 29 |
- phases: An array of "phase" objects. |
| 30 |
A phase of the test optionally sends inputs to the client, |
| 31 |
then tests the client's resulting TopologyDescription. |
| 32 |
|
| 33 |
Each phase object has the following keys: |
| 34 |
|
| 35 |
- description: (optional) A textual description of this phase. |
| 36 |
- responses: (optional) An array of "response" objects. If not provided, |
| 37 |
the test runner should construct the client and perform assertions specified |
| 38 |
in the outcome object without processing any responses. |
| 39 |
- applicationErrors: (optional) An array of "applicationError" objects. |
| 40 |
- outcome: An "outcome" object representing the TopologyDescription. |
| 41 |
|
| 42 |
A response is a pair of values: |
| 43 |
|
| 44 |
- The source, for example "a:27017". |
| 45 |
This is the address the client sent the "hello" or legacy hello command to. |
| 46 |
- A hello or legacy hello response, for example ``{ok: 1, helloOk: true, isWritablePrimary: true}``. |
| 47 |
If the response includes an electionId it is shown in extended JSON like |
| 48 |
``{"$oid": "000000000000000000000002"}``. |
| 49 |
The empty response `{}` indicates a network error |
| 50 |
when attempting to call "hello" or legacy hello. |
| 51 |
|
| 52 |
An "applicationError" object has the following keys: |
| 53 |
|
| 54 |
- address: The source address, for example "a:27017". |
| 55 |
- generation: (optional) The error's generation number, for example ``1``. |
| 56 |
When absent this value defaults to the pool's current generation number. |
| 57 |
- maxWireVersion: The ``maxWireVersion`` of the connection the error occurs |
| 58 |
on, for example ``9``. Added to support testing the behavior of "not writable primary" |
| 59 |
errors on <4.2 and >=4.2 servers. |
| 60 |
- when: A string describing when this mock error should occur. Supported |
| 61 |
values are: |
| 62 |
|
| 63 |
- "beforeHandshakeCompletes": Simulate this mock error as if it occurred |
| 64 |
during a new connection's handshake for an application operation. |
| 65 |
- "afterHandshakeCompletes": Simulate this mock error as if it occurred |
| 66 |
on an established connection for an application operation (i.e. after |
| 67 |
the connection pool check out succeeds). |
| 68 |
|
| 69 |
- type: The type of error to mock. Supported values are: |
| 70 |
|
| 71 |
- "command": A command error. Always accompanied with a "response". |
| 72 |
- "network": A non-timeout network error. |
| 73 |
- "timeout": A network timeout error. |
| 74 |
|
| 75 |
- response: (optional) A command error response, for example |
| 76 |
``{ok: 0, errmsg: "not primary"}``. Present if and only if ``type`` is |
| 77 |
"command". Note the server only returns "not primary" if the "hello" command |
| 78 |
has been run on this connection. Otherwise the legacy error message is returned. |
| 79 |
|
| 80 |
In non-monitoring tests, an "outcome" represents the correct |
| 81 |
TopologyDescription that results from processing the responses in the phases |
| 82 |
so far. It has the following keys: |
| 83 |
|
| 84 |
- topologyType: A string like "ReplicaSetNoPrimary". |
| 85 |
- setName: A string with the expected replica set name, or null. |
| 86 |
- servers: An object whose keys are addresses like "a:27017", and whose values |
| 87 |
are "server" objects. |
| 88 |
- logicalSessionTimeoutMinutes: null or an integer. |
| 89 |
- maxSetVersion: absent or an integer. |
| 90 |
- maxElectionId: absent or a BSON ObjectId. |
| 91 |
- compatible: absent or a bool. |
| 92 |
|
| 93 |
A "server" object represents a correct ServerDescription within the client's |
| 94 |
current TopologyDescription. It has the following keys: |
| 95 |
|
| 96 |
- type: A ServerType name, like "RSSecondary". |
| 97 |
- setName: A string with the expected replica set name, or null. |
| 98 |
- setVersion: absent or an integer. |
| 99 |
- electionId: absent, null, or an ObjectId. |
| 100 |
- logicalSessionTimeoutMinutes: absent, null, or an integer. |
| 101 |
- minWireVersion: absent or an integer. |
| 102 |
- maxWireVersion: absent or an integer. |
| 103 |
- topologyVersion: absent, null, or a topologyVersion document. |
| 104 |
- pool: (optional) A "pool" object. |
| 105 |
|
| 106 |
A "pool" object represents a correct connection pool for a given server. |
| 107 |
It has the following keys: |
| 108 |
|
| 109 |
- generation: This server's expected pool generation, like ``0``. |
| 110 |
|
| 111 |
In monitoring tests, an "outcome" contains a list of SDAM events that should |
| 112 |
have been published by the client as a result of processing hello or legacy hello |
| 113 |
responses in the current phase. Any SDAM events published by the client during its |
| 114 |
construction (that is, prior to processing any of the responses) should be |
| 115 |
combined with the events published during processing of hello or legacy hello |
| 116 |
responses of the first phase of the test. A test MAY explicitly verify events |
| 117 |
published during client construction by providing an empty responses array for the |
| 118 |
first phase. |
| 119 |
|
| 120 |
|
| 121 |
Use as unittests |
| 122 |
---------------- |
| 123 |
|
| 124 |
Mocking |
| 125 |
~~~~~~~ |
| 126 |
|
| 127 |
Drivers should be able to test their server discovery and monitoring logic without |
| 128 |
any network I/O, by parsing hello (or legacy hello) and application error from the |
| 129 |
test file and passing them into the driver code. Parts of the client and |
| 130 |
monitoring code may need to be mocked or subclassed to achieve this. |
| 131 |
`A reference implementation for PyMongo 3.10.1 is available here |
| 132 |
<https://github.com/mongodb/mongo-python-driver/blob/3.10.1/test/test_discovery_and_monitoring.py>`_. |
| 133 |
|
| 134 |
Initialization |
| 135 |
~~~~~~~~~~~~~~ |
| 136 |
|
| 137 |
For each file, create a fresh client object initialized with the file's "uri". |
| 138 |
|
| 139 |
All files in the "single" directory include a connection string with one host |
| 140 |
and no "replicaSet" option. |
| 141 |
Set the client's initial TopologyType to Single, however that is achieved using the client's API. |
| 142 |
(The spec says "The user MUST be able to set the initial TopologyType to Single" |
| 143 |
without specifying how.) |
| 144 |
|
| 145 |
All files in the "sharded" directory include a connection string with multiple hosts |
| 146 |
and no "replicaSet" option. |
| 147 |
Set the client's initial TopologyType to Unknown or Sharded, depending on the client's API. |
| 148 |
|
| 149 |
All files in the "rs" directory include a connection string with a "replicaSet" option. |
| 150 |
Set the client's initial TopologyType to ReplicaSetNoPrimary. |
| 151 |
(For most clients, parsing a connection string with a "replicaSet" option |
| 152 |
automatically sets the TopologyType to ReplicaSetNoPrimary.) |
| 153 |
|
| 154 |
Set up a listener to collect SDAM events published by the client, including |
| 155 |
events published during client construction. |
| 156 |
|
| 157 |
Test Phases |
| 158 |
~~~~~~~~~~~ |
| 159 |
|
| 160 |
For each phase in the file: |
| 161 |
|
| 162 |
#. Parse the "responses" array. Pass in the responses in order to the driver |
| 163 |
code. If a response is the empty object ``{}``, simulate a network error. |
| 164 |
|
| 165 |
#. Parse the "applicationErrors" array. For each element, simulate the given |
| 166 |
error as if it occurred while running an application operation. Note that |
| 167 |
it is sufficient to construct a mock error and call the procedure which |
| 168 |
updates the topology, e.g. |
| 169 |
``topology.handleApplicationError(address, generation, maxWireVersion, error)``. |
| 170 |
|
| 171 |
For non-monitoring tests, |
| 172 |
once all responses are processed, assert that the phase's "outcome" object |
| 173 |
is equivalent to the driver's current TopologyDescription. |
| 174 |
|
| 175 |
For monitoring tests, once all responses are processed, assert that the |
| 176 |
events collected so far by the SDAM event listener are equivalent to the |
| 177 |
events specified in the phase. |
| 178 |
|
| 179 |
Some fields such as "logicalSessionTimeoutMinutes", "compatible", and |
| 180 |
"topologyVersion" were added later and haven't been added to all test files. |
| 181 |
If these fields are present, test that they are equivalent to the fields of |
| 182 |
the driver's current TopologyDescription or ServerDescription. |
| 183 |
|
| 184 |
For monitoring tests, clear the list of events collected so far. |
| 185 |
|
| 186 |
Continue until all phases have been executed. |
| 187 |
|
| 188 |
Integration Tests |
| 189 |
----------------- |
| 190 |
|
| 191 |
Integration tests are provided in the "integration" directory. |
| 192 |
|
| 193 |
Test Format |
| 194 |
~~~~~~~~~~~ |
| 195 |
|
| 196 |
The same as the `Transactions Spec Test format |
| 197 |
</source/transactions/tests/README.rst#test-format>`_ with the following |
| 198 |
additions: |
| 199 |
|
| 200 |
- The ``runOn`` requirement gains a new field: |
| 201 |
|
| 202 |
- ``authEnabled`` (optional): If True, skip this test if auth is not enabled. |
| 203 |
If False, skip this test if auth is enabled. If this field is omitted, |
| 204 |
this test can be run on clusters with or without auth. |
| 205 |
|
| 206 |
Special Test Operations |
| 207 |
~~~~~~~~~~~~~~~~~~~~~~~ |
| 208 |
|
| 209 |
Certain operations that appear in the "operations" array do not correspond to |
| 210 |
API methods but instead represent special test operations. Such operations are |
| 211 |
defined on the "testRunner" object and are documented in the |
| 212 |
`Transactions Spec Test |
| 213 |
</source/transactions/tests/README.rst#special-test-operations>`_. |
| 214 |
|
| 215 |
Additional, SDAM test specific operations are documented here: |
| 216 |
|
| 217 |
configureFailPoint |
| 218 |
'''''''''''''''''' |
| 219 |
|
| 220 |
The "configureFailPoint" operation instructs the test runner to configure |
| 221 |
the given server failpoint on the "admin" database. The runner MUST disable |
| 222 |
this failpoint at the end of the test. For example:: |
| 223 |
|
| 224 |
- name: configureFailPoint |
| 225 |
object: testRunner |
| 226 |
arguments: |
| 227 |
failPoint: |
| 228 |
configureFailPoint: failCommand |
| 229 |
mode: { times: 1 } |
| 230 |
data: |
| 231 |
failCommands: ["insert"] |
| 232 |
closeConnection: true |
| 233 |
|
| 234 |
Tests that use the "configureFailPoint" operation do not include |
| 235 |
``configureFailPoint`` commands in their command expectations. Drivers MUST |
| 236 |
ensure that ``configureFailPoint`` commands do not appear in the list of logged |
| 237 |
commands, either by manually filtering it from the list of observed commands or |
| 238 |
by using a different MongoClient to execute ``configureFailPoint``. |
| 239 |
|
| 240 |
Note, similar to the ``tests.failPoint`` field described in the `Transactions |
| 241 |
Spec Test format </source/transactions/tests/README.rst#test-format>`_ tests |
| 242 |
with ``useMultipleMongoses: true`` will not contain a ``configureFailPoint`` |
| 243 |
operation. |
| 244 |
|
| 245 |
wait |
| 246 |
'''' |
| 247 |
|
| 248 |
The "wait" operation instructs the test runner to sleep for "ms" |
| 249 |
milliseconds. For example:: |
| 250 |
|
| 251 |
- name: wait |
| 252 |
object: testRunner |
| 253 |
arguments: |
| 254 |
ms: 1000 |
| 255 |
|
| 256 |
waitForEvent |
| 257 |
'''''''''''' |
| 258 |
|
| 259 |
The "waitForEvent" operation instructs the test runner to wait until the test's |
| 260 |
MongoClient has published a specific event a given number of times. For |
| 261 |
example, the following instructs the test runner to wait for at least one |
| 262 |
PoolClearedEvent to be published:: |
| 263 |
|
| 264 |
- name: waitForEvent |
| 265 |
object: testRunner |
| 266 |
arguments: |
| 267 |
event: PoolClearedEvent |
| 268 |
count: 1 |
| 269 |
|
| 270 |
Note that "count" includes events that were published while running previous |
| 271 |
operations. |
| 272 |
|
| 273 |
If the "waitForEvent" operation is not satisfied after 10 seconds, the |
| 274 |
operation is considered an error. |
| 275 |
|
| 276 |
ServerMarkedUnknownEvent |
| 277 |
```````````````````````` |
| 278 |
|
| 279 |
The ServerMarkedUnknownEvent may appear as an event in `waitForEvent`_ and |
| 280 |
`assertEventCount`_. This event is defined as ServerDescriptionChangedEvent |
| 281 |
where newDescription.type is ``Unknown``. |
| 282 |
|
| 283 |
assertEventCount |
| 284 |
'''''''''''''''' |
| 285 |
|
| 286 |
The "assertEventCount" operation instructs the test runner to assert the test's |
| 287 |
MongoClient has published a specific event a given number of times. For |
| 288 |
example, the following instructs the test runner to assert that a single |
| 289 |
PoolClearedEvent was published:: |
| 290 |
|
| 291 |
- name: assertEventCount |
| 292 |
object: testRunner |
| 293 |
arguments: |
| 294 |
event: PoolClearedEvent |
| 295 |
count: 1 |
| 296 |
|
| 297 |
recordPrimary |
| 298 |
''''''''''''' |
| 299 |
|
| 300 |
The "recordPrimary" operation instructs the test runner to record the current |
| 301 |
primary of the test's MongoClient. For example:: |
| 302 |
|
| 303 |
- name: recordPrimary |
| 304 |
object: testRunner |
| 305 |
|
| 306 |
runAdminCommand |
| 307 |
''''''''''''''' |
| 308 |
|
| 309 |
The "runAdminCommand" operation instructs the test runner to run the given |
| 310 |
command on the admin database. Drivers MUST run this command on a different |
| 311 |
MongoClient from the one used for test operations. For example:: |
| 312 |
|
| 313 |
- name: runAdminCommand |
| 314 |
object: testRunner |
| 315 |
command_name: replSetFreeze |
| 316 |
arguments: |
| 317 |
command: |
| 318 |
replSetFreeze: 0 |
| 319 |
readPreference: |
| 320 |
mode: Secondary |
| 321 |
|
| 322 |
waitForPrimaryChange |
| 323 |
'''''''''''''''''''' |
| 324 |
|
| 325 |
The "waitForPrimaryChange" operation instructs the test runner to wait up to |
| 326 |
"timeoutMS" milliseconds for the MongoClient to discover a new primary server. |
| 327 |
The new primary should be different from the one recorded by "recordPrimary". |
| 328 |
For example:: |
| 329 |
|
| 330 |
- name: waitForPrimaryChange |
| 331 |
object: testRunner |
| 332 |
arguments: |
| 333 |
timeoutMS: 15000 |
| 334 |
|
| 335 |
To implement, Drivers can subscribe to ServerDescriptionChangedEvents and wait |
| 336 |
for an event where newDescription.type is ``RSPrimary`` and the address is |
| 337 |
different from the one previously recorded by "recordPrimary". |
| 338 |
|
| 339 |
startThread |
| 340 |
''''''''''' |
| 341 |
|
| 342 |
The "startThread" operation instructs the test runner to start a new thread |
| 343 |
with the provided "name". The `runOnThread`_ and `waitForThread`_ operations |
| 344 |
reference a thread by its "name". For example:: |
| 345 |
|
| 346 |
- name: startThread |
| 347 |
object: testRunner |
| 348 |
arguments: |
| 349 |
name: thread1 |
| 350 |
|
| 351 |
runOnThread |
| 352 |
''''''''''' |
| 353 |
|
| 354 |
The "runOnThread" operation instructs the test runner to schedule an operation |
| 355 |
to be run on the given thread. runOnThread MUST NOT wait for the scheduled |
| 356 |
operation to complete. For example:: |
| 357 |
|
| 358 |
- name: runOnThread |
| 359 |
object: testRunner |
| 360 |
arguments: |
| 361 |
name: thread1 |
| 362 |
operation: |
| 363 |
name: insertOne |
| 364 |
object: collection |
| 365 |
arguments: |
| 366 |
document: |
| 367 |
_id: 2 |
| 368 |
error: true |
| 369 |
|
| 370 |
waitForThread |
| 371 |
''''''''''''' |
| 372 |
|
| 373 |
The "waitForThread" operation instructs the test runner to stop the given |
| 374 |
thread, wait for it to complete, and assert that the thread exited without |
| 375 |
any errors. For example:: |
| 376 |
|
| 377 |
- name: waitForThread |
| 378 |
object: testRunner |
| 379 |
arguments: |
| 380 |
name: thread1 |
| 381 |
|
| 382 |
Prose Tests |
| 383 |
----------- |
| 384 |
|
| 385 |
The following prose tests cannot be represented as spec tests and MUST be |
| 386 |
implemented. |
| 387 |
|
| 388 |
Streaming protocol Tests |
| 389 |
~~~~~~~~~~~~~~~~~~~~~~~~ |
| 390 |
|
| 391 |
Drivers that implement the streaming protocol (multi-threaded or |
| 392 |
asynchronous drivers) must implement the following tests. Each test should be |
| 393 |
run against a standalone, replica set, and sharded cluster unless otherwise |
| 394 |
noted. |
| 395 |
|
| 396 |
Some of these cases should already be tested with the old protocol; in |
| 397 |
that case just verify the test cases succeed with the new protocol. |
| 398 |
|
| 399 |
1. Configure the client with heartbeatFrequencyMS set to 500, |
| 400 |
overriding the default of 10000. Assert the client processes |
| 401 |
hello and legacy hello replies more frequently (approximately every 500ms). |
| 402 |
|
| 403 |
RTT Tests |
| 404 |
~~~~~~~~~ |
| 405 |
|
| 406 |
Run the following test(s) on MongoDB 4.4+. |
| 407 |
|
| 408 |
1. Test that RTT is continuously updated. |
| 409 |
|
| 410 |
#. Create a client with ``heartbeatFrequencyMS=500``, |
| 411 |
``appName=streamingRttTest``, and subscribe to server events. |
| 412 |
|
| 413 |
#. Run a find command to wait for the server to be discovered. |
| 414 |
|
| 415 |
#. Sleep for 2 seconds. This must be long enough for multiple heartbeats |
| 416 |
to succeed. |
| 417 |
|
| 418 |
#. Assert that each ``ServerDescriptionChangedEvent`` includes a non-zero |
| 419 |
RTT. |
| 420 |
|
| 421 |
#. Configure the following failpoint to block hello or legacy hello commands |
| 422 |
for 250ms which should add extra latency to each RTT check:: |
| 423 |
|
| 424 |
db.adminCommand({ |
| 425 |
configureFailPoint: "failCommand", |
| 426 |
mode: {times: 1000}, |
| 427 |
data: { |
| 428 |
failCommands: ["hello"], // or the legacy hello command |
| 429 |
blockConnection: true, |
| 430 |
blockTimeMS: 500, |
| 431 |
appName: "streamingRttTest", |
| 432 |
}, |
| 433 |
}); |
| 434 |
|
| 435 |
#. Wait for the server's RTT to exceed 250ms. Eventually the average RTT |
| 436 |
should also exceed 500ms but we use 250ms to speed up the test. Note |
| 437 |
that the `Server Description Equality`_ rule means that |
| 438 |
ServerDescriptionChangedEvents will not be published. This test may |
| 439 |
need to use a driver specific helper to obtain the latest RTT instead. |
| 440 |
If the RTT does not exceed 250ms after 10 seconds, consider the test |
| 441 |
failed. |
| 442 |
|
| 443 |
#. Disable the failpoint:: |
| 444 |
|
| 445 |
db.adminCommand({ |
| 446 |
configureFailPoint: "failCommand", |
| 447 |
mode: "off", |
| 448 |
}); |
| 449 |
|
| 450 |
.. Section for links. |
| 451 |
|
| 452 |
.. _Server Description Equality: /source/server-discovery-and-monitoring/server-discovery-and-monitoring.rst#server-description-equality |