Skip to content

Fix/compat test side fixes - #734

Merged
eerxuan merged 8 commits into
documentdb:mainfrom
samafarc:fix/compat-test-side-fixes
Nov 4, 2026
Merged

eerxuan merged 8 commits into
documentdb:mainfrom
samafarc:fix/compat-test-side-fixes

Conversation

@samafarc

@samafarc samafarc commented Nov 3, 2026 •

Copy link
Copy Markdown
Contributor

Summary

This PR fixes three groups of tests that fail for reasons unrelated to the
command they test. In every case, the failure happens either in the test's own
setup or outside the framework assertion helper (assertResult /
assertSuccess / assertProperties). On MongoDB nothing changes; these tests
pass before and after. On an engine whose behavior differs from MongoDB, the
test now gets as far as its assertion and fails there with the engine's real
result. It can then be judged, or captured by the opt-in override hook (#724).
Before this change it crashed with an unrelated Python error, or failed because
of an earlier test.

The three groups touch separate files and are in separate commits, so they can
be reviewed one at a time:

Group Commits Files Not covered by
1. Route helper-crash failures through the assertion helper 4 14 #728, #729 and #732, which apply the same pattern to other files
2. Isolate auth smoke tests; tolerate unsupported setup commands 2 5 #731, which turned the other smoke tests into capability probes
3. Close server cursors that tests open with raw commands 1 3 No existing PR

1. Route helper-crash failures through the assertion helper

What was broken

These tests run a command or a setup step, then read or re-raise the raw result
before calling the assertion helper. When the engine returns an error instead
of the expected reply, the test crashes in that read, and the assertion never
runs:

Area Tests Before
Cursor open / getMore Every test in getMore/ and killCursors/ that opens a cursor, whether through open_find_cursors, open_cursor (tailable) or a direct find / aggregate / listIndexes res["cursor"]["id"] on a failed open raised TypeError: 'OperationFailure' object is not subscriptable. Five tests also indexed a failed intermediate getMore reply the same way
Change-stream open test_changeStream_resume_mutual_exclusivity, test_changeStream_spec_acceptance opened["cursor"]["postBatchResumeToken"] on a failed $changeStream open raised the same TypeError
$rand projection test_rand_in_object_expression result["cursor"]["firstBatch"] on a failed projection raised the same TypeError
collStats histogram test_collStats_output_histogram_structure raise AssertionError(f"unexpected error: {result}") raised the engine error outside the helper
Geospatial / wildcard data load test_geospatial_creation, test_wildcard_data_types collection.insert_many(...) raised BulkWriteError when the engine rejected a setup document
dbStats setup test_dbStats_collection_scenarios raise setup_result raised the setup error outside the helper
listIndexes profiler test_listIndexes_collection_variants A raw db.command({"profile": 2}) raised outside the helper, and listed["cursor"]["id"] indexed a failed listIndexes
Transaction commit test_commitTransaction_supported_operations Every test called session.commit_transaction() directly, and three ran a driver write (update_one, delete_one, insert_one) directly. When the operation failed, the write or the commit of the aborted transaction raised outside the helper

What changed

Each test captures the result it already gets and passes it, or a not_error
check, to the one assertion it already makes. No new assertions are added, so
the one-assertion-per-test rule still holds:

  • open_find_cursors and open_cursor in cursor_test_case.py return the
    open error instead of indexing into it, and every caller passes that error to
    its assertion. Tests that open a cursor with a direct command, or read an
    intermediate getMore reply, pass a failed reply on the same way.
  • The change-stream tests pass a captured open failure to their assertion.
  • The $rand projection index is guarded. The collStats histogram error goes
    through assertResult(not_error=True).
  • Geospatial and wildcard documents are loaded with execute_command instead of
    insert_many. A rejected insert (an error, or writeErrors in an ok: 1
    reply) is passed to the assertion in place of the find result, so the test
    reports the insert rejection rather than a find mismatch.
  • The dbStats setup failure, the profiler enable and a failed listIndexes are
    passed to the test's assertion.
  • Every transaction commit goes through _commit_or_error, and the three driver
    writes through _apply_and_commit, which return the engine error instead of
    raising.
  • In test_collStats_output_histogram_structure, the test returns after the
    error assertion, so an override that handles the error doesn't fall through
    into the histogram checks.

Not changed

test_sample_statistical_randomness and test_sample_statistical_uniformity
are left as they are. They already fail inside their own statistical assertion,
because the engine's $sample really does behave differently. That is the test
doing its job, not a routing bug.

2. Isolate auth smoke tests and tolerate unsupported setup commands

What was broken

Test Before
test_smoke_logout Ran logout on the shared pooled connection. That de-authenticated the connection for every later test on the same xdist worker, so unrelated tests failed with authorization errors.
test_smoke_rolesInfo, test_smoke_usersInfo Checked ok exactly with assertSuccessPartial, so an engine returning ok: 1 (int32) instead of 1.0 (double) failed a capability probe.
fsync and autoCompact smoke tests Their setup helpers (drain_fsync_lock in utils/fsync_lock.py, ensure_autocompact_idle in autoCompact/utils/autoCompact_common.py) raised when the command was unsupported. So every test in those folders failed in setup and never ran the command it was meant to probe.

What changed

  • test_smoke_logout runs on a dedicated MongoClient built from the test's
    connection_string, so only that client is logged out.
  • test_smoke_rolesInfo and test_smoke_usersInfo assert with
    assertCommandSupported, like the capability probes in Make smoke tests capability probes; fix 5 that crash on engine divergence (PR#718 Rebased) #731. The command is
    unchanged.
  • The fsync and autoCompact setup helpers return without doing anything when the
    command is unsupported (CommandNotFound 59, or 303), since an engine without
    the command has no state to reset. The test then runs the command and reports
    the real result. On an engine that supports the command, the helpers behave
    as before.

3. Close server cursors that tests open with raw commands

What was broken

execute_command and execute_admin_command return the raw command reply.
When that reply holds an open cursor (a find or listIndexes with a small
batchSize, or an aggregate with $changeStream), no driver object owns the
cursor. Nothing closes it when the test ends, and dropping the test database
doesn't close it on every engine, so it stays open until the server's idle
timeout.

Engines that cap open cursors per instance then reject new cursors. Amazon
DocumentDB's quota is 30 on T3/T4G.medium instances and as low as 6 on a
serverless instance at 0.5 DCU
(quotas,
serverless limits).
Once the cap fills, a later test's setup fails with Cannot open a new cursor since too many cursors are already opened (code 2), and that test usually
crashes as described in group 1. Which test hits the cap depends on timing and
test order. So the failures look flaky, and they land on tests that did nothing
wrong.

What changed

The two helpers now record the namespace and id of every open cursor they
return. At test teardown, those cursors are killed with killCursors:

  • in the collection and database_client fixtures, before the drop;
  • in a new autouse fixture, close_leaked_cursors, for tests that use neither
    fixture.

Cleanup is best effort. A cursor that is already exhausted or killed comes back
in cursorsNotFound, and any error is ignored, so cleanup never changes a test's
outcome. Cursors opened through the driver API (collection.find(), watch())
are not touched, because the driver already closes them.

Testing

  • Pre-commit (black, isort, flake8, mypy) on the whole commit range:
    pass.
  • pytest -m unit: 152 passed. This includes 7 new cases in
    framework/test_executor_cursor_tracking.py:
    • an open cursor is tracked and killed;
    • an exhausted cursor is not tracked;
    • a failed command is not tracked;
    • cursors are grouped per namespace;
    • the admin $cmd.aggregate namespace is handled;
    • closing twice is safe;
    • a kill failure is ignored.
  • MongoDB 8.2.4 single-node replica set, over the affected directories (cursors,
    change streams, sessions, security, administration and others), on the first
    revision of this PR: 2,732 passed on main, and 2,732 passed with all three
    groups applied.
  • The 27 touched test files on MongoDB 8.2.4: 413 passed on main and 413
    passed with this change.
  • Error paths, checked by fault injection on the same 413 tests: every
    execute_command / execute_admin_command the test and its helpers issue
    returns an OperationFailure, and every assertion helper records its call and
    returns, as an override provider registered through Add opt-in assertion override hook for compatible engines #724 does. On main, 159
    tests raise outside the assertion helper (TypeError on the error, a re-raised
    error, or a fall-through after the assertion). With this change, 1 does:
    test_listIndexes_long_name_drop_secondary_indexes, whose driver
    drop_index call fails only because the injected createIndexes failure
    never created the index. It isn't part of this change.
  • The same 413 tests against an Amazon DocumentDB 8.0 dev build: 10 failures
    raised outside the assertion helper on main (BulkWriteError from the
    geospatial data load), 0 with this change. No test went from passing to
    failing; rolesInfo and usersInfo went from failing to passing.
  • Group 2, the logout, rolesInfo, usersInfo, fsync, fsyncUnlock and autoCompact
    smoke tests on MongoDB: pass, both serially and in parallel.
  • Group 3:
    • Whole suite on MongoDB 8.2.4, -n 8: 56,562 results, identical per test on
      main and with this change. 54,478 passed, 35 skipped, and 2,049 failed or
      errored; all 2,049 are $search / $vectorSearch / $searchMeta tests
      that need Atlas Search.
    • Against an engine with a 20-cursor cap, over the cursor, listIndexes,
      hidden-index and change-stream directories (-n 8, 2 runs):
      • 62 tests went from failing or flaky to passing in both runs, and none got
        worse.
      • Cap rejections dropped from 65 per run to 0.
      • With a 490-cursor cap, no test got worse.

killCursors/getMore tests opened a cursor with a raw command and indexed
res["cursor"]["id"] directly. When the engine rejects the open the result is an
exception, so the index raised a bare TypeError outside the assertion helper and
the test could not take an override. Add open_find_cursors_or_error and forward a
captured open failure into each test's single assertion so it fails through the
helper with the engine's real result. MongoDB behavior and test intent unchanged.

Signed-off-by: Samar Farooqi <samafarc@amazon.com>
These tests opened a base $changeStream and read opened["cursor"]["postBatchResumeToken"]
directly; when the open is rejected the result is an exception and the index raised
a bare TypeError outside the helper. Forward a captured open failure into the single
assertResult. MongoDB behavior and test intent unchanged.

Signed-off-by: Samar Farooqi <samafarc@amazon.com>
…on helper

test_rand_in_object_expression indexed result["cursor"]["firstBatch"] on a raw result,
and the collStats histogram test raised a bare AssertionError on an engine error.
Route both through the assertion helper (guarded index / assertResult not_error) so
they fail overridably with the real result. MongoDB behavior unchanged.

Signed-off-by: Samar Farooqi <samafarc@amazon.com>
geospatial and wildcard valid-data tests loaded docs with collection.insert_many
(raises BulkWriteError); dbStats re-raised a setup failure; listIndexes enabled the
profiler with a raw db.command; commitTransaction called session.commit_transaction
after an op the engine rejects in a txn. Each raised outside the assertion helper.
Load via execute_command / forward the captured setup/commit failure into the single
assertion so each fails overridably with the engine's real result. MongoDB unchanged.

Signed-off-by: Samar Farooqi <samafarc@amazon.com>
…ommands

Four section-4 "smoke tests no run can judge" issues not covered by documentdb#731:

- logout: run on a dedicated client. logout de-authenticates the
  connection it runs on; on the shared pooled session connection it logs
  out every later test on the same xdist worker (authorization failure).
- rolesInfo / usersInfo: query only the role/user the test creates, not
  the whole database. `{"rolesInfo": 1}` / `{"usersInfo": 1}` list every
  object, including ones parallel tests create and drop, so the result is
  non-deterministic.
- fsync/autoCompact setup helpers (drain_fsync_lock,
  ensure_autocompact_idle): return instead of raising when the command is
  unsupported (COMMAND_NOT_FOUND 59 / FEATURE_NOT_SUPPORTED 303), so the
  test under test runs and judges the command instead of failing in setup.

Each probe keeps the same assertion and is unchanged on an engine that
supports the command. Verified on MongoDB 8.2.4.

Signed-off-by: Samar Farooqi <samafarc@amazon.com>
Match the capability-probe pattern from documentdb#731 so these smoke tests stop
failing on the cosmetic ok int32 vs double difference.

Signed-off-by: Samar Farooqi <samafarc@amazon.com>
execute_command and execute_admin_command return the raw command
result, so a find with a small batchSize or an aggregate with
$changeStream leaves a server cursor that no driver object owns. Nothing
closes it when the test ends, and dropping the test database does not
close it on every engine. The cursor stays open until the server's idle
timeout.

On an engine that caps open cursors per instance, those leftovers add up
until a later test's find or aggregate is rejected with "Cannot open a
new cursor since too many cursors are already opened". Which test hits
the cap depends on timing and test order, so the failures look flaky and
land on tests that did nothing wrong.

Track the cursor id and namespace of every command result that returns
an open cursor, and kill them with killCursors at teardown: in the
collection and database_client fixtures before the drop, and in a new
autouse fixture for tests that use neither. Cleanup is best effort and
never changes a test's outcome.

Signed-off-by: Samar Farooqi <samafarc@amazon.com>
@samafarc
samafarc requested a review from a team as a code owner November 3, 2026 21:16

@eerxuan eerxuan left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Summary

Three useful fixes, split into clean commits. MongoDB behavior is unchanged and CI is green. The cursor cleanup is careful: it is best effort, groups cursors per namespace, and has 7 unit cases. One place can still fail outside the assertion helper when an override provider is registered.

Critical Issues

  1. [Required] test_collStats_output.py, test_collStats_output_histogram_structure: when an override provider registered through the assertion hook (#724) handles the assertResult(not_error=True) call, the call returns instead of raising. The next line, result["cursor"]["firstBatch"][0], then raises TypeError on the exception. Put the histogram checks in an else: branch, or return after the assertion.

Suggestions

  1. [Suggestion] test_commitTransaction_supported_operations.py: 10 other tests in this file still call session.commit_transaction() directly after an operation in the transaction. If that operation fails, the transaction is aborted and the commit raises outside the helper. Using _commit_or_error in all of them would keep the file consistent.
  2. [Suggestion] 28 open_find_cursors(...) call sites still index the open result and will crash the same way: 25 in getMore/ and 3 in test_killCursors_lifecycle.py itself. Please convert them, or explain in the description why only these tests changed. Two helpers whose names differ only in whether they crash on an open error will confuse future authors.
  3. [Suggestion] test_smoke_rolesInfo / test_smoke_usersInfo: the old assertion was assertSuccessPartial(result, {"ok": 1.0}), which compares only ok, so the role or user list could never make it fail. assertCommandSupported alone fixes the int32 ok case. {"rolesInfo": 1} / {"usersInfo": 1} with assertCommandSupported is enough, and the smoke probe then doesn't need to create a role, or a user with a password, in admin.
  4. [Suggestion] test_geospatial_creation.py / test_wildcard_data_types.py: a rejected document comes back as writeErrors in an ok: 1 reply, so the assertion reports a find mismatch rather than the insert rejection. Forwarding the insert reply when it has writeErrors would surface the real error.

Questions

  1. [Nitpick] test_listIndexes_comment_inherited_by_getMore: listed["cursor"]["id"] is still read without a check, so a failed listIndexes raises TypeError inside the try.
  2. [Nitpick] The description says test_commitTransaction_supported_operations had raise AssertionError(f"unexpected error: {result}"). That string is not in the file on main.

Positive Feedback

  • Running logout on a dedicated client follows the existing connection_string pattern (fsyncUnlock, abortTransaction).
  • The "unsupported means nothing to reset" early return in the fsync/autoCompact setup is narrow (codes 59 and 303 only), and behavior is unchanged where the commands are supported.
  • The cursor tracking handles the admin $cmd.aggregate namespace and double kills (cursorsNotFound) without affecting test outcomes.

- collStats histogram: return after the error assertion, so an override
  that handles the error doesn't fall through into the histogram checks.
- open_find_cursors and open_cursor return the open error instead of
  indexing into it; open_find_cursors_or_error is folded into
  open_find_cursors. Every caller in getMore/ and killCursors/ (and the
  direct find/aggregate/listIndexes opens and intermediate getMore replies
  in those files) passes a failed reply to the test's assertion.
- commitTransaction supported operations: every commit goes through
  _commit_or_error, and the three driver writes through _apply_and_commit.
- rolesInfo/usersInfo smoke: back to {"rolesInfo": 1} / {"usersInfo": 1};
  assertCommandSupported alone fixes the int32 ok case.
- geospatial/wildcard data load: a writeErrors entry in an ok: 1 insert
  reply is forwarded as the WriteError the driver would raise for it.
- listIndexes comment-inherited-by-getMore: a failed listIndexes is
  forwarded instead of indexed.

Signed-off-by: Samar Farooqi <samafarc@amazon.com>
@documentdb-triage-tool documentdb-triage-tool Bot added bug Something isn't working compatibility test Compatibility test related labels Nov 4, 2026
@documentdb-triage-tool

Copy link
Copy Markdown

🤖 Auto-triaged by documentdb-triage-tool.

Applied: compatibility test, bug
Project fields suggested: Component test-coverage · Priority P0 · Effort XL · Status Needs Review
Confidence: 0.95 (deterministic)

Reasoning

component from path globs (test-coverage, test-framework); P0 keyword "crash" in body; effort from diff stats (1122+505 LOC, 33 files)

If a label is wrong, remove it manually and ping @patty-chow so the rules can be tuned. The bot will not re-label items that already have component labels.

@eerxuan
eerxuan merged commit 10eec47 into documentdb:main Nov 4, 2026
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working compatibility test Compatibility test related

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants