Skip to content

Report setup and intermediate command failures through the assertion - #728

Merged
eerxuan merged 5 commits into
documentdb:mainfrom
samafarc:fix-setup-error-surfacing
Nov 6, 2026
Merged

eerxuan merged 5 commits into
documentdb:mainfrom
samafarc:fix-setup-error-surfacing

Conversation

@samafarc

@samafarc samafarc commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Several tests run a setup or intermediate command (open a cursor or change stream, capture a resume token, run a reference call) and then index into its reply before reaching the assertion. When that command fails on the engine under test, the test crashed with a raw TypeError, KeyError, IndexError, AttributeError or StopIteration outside the assertion helper. The real engine error never reached the report. In a few cases the test passed without comparing anything.

This PR forwards the failed result to the test's single assertion, so the failure is reported by framework/assertions.py with the engine's own error. MongoDB behaviour and the property each test checks are unchanged, and each test still makes exactly one assertion.

Changes

  • listIndexes cursor batching: guard the initial listIndexes reply and every getMore; forward the first failure.
  • changeStream event read error: forward a failed stream open instead of indexing its cursor.
  • changeStream events: _emit_event returns the open or getMore error, and returns the getMore reply when the batch has no event of the expected type, so the test's assertProperties reports what the stream delivered.
  • changeStream resume: _first_event_token and _event_token_of_type return the engine error, or a LookupError when the capture batch is empty or has no event of the requested type. Every caller forwards it to the assertion instead of indexing into it or sending startAfter: null. Callers that open a resumed stream and then getMore also forward a failed open.
  • bulkWrite response structure: forward a failed bulkWrite instead of reading its cursor id.
  • setQuerySettings:
    • response_contains_hash / response_contains_representative_query no longer read fields off an exception.
    • remove, update, hash_same and hash_different forward a failed setup command to the assertion.
    • hash_different treats a missing hash on either the setup reply or the command reply as a failure, not a difference, and reads the command hash with .get().
    • hash_format forwards a failed command.
  • getParameter / getCmdLineOpts consistency: when either call fails, its error is the assertion's actual value.
  • serverStatus: pid, host and version consistency checks and the cross-database check forward a failed reference call instead of comparing against None or indexing it.
  • hello: connectionId_stable, legacy_isMaster_fields_match and topologyVersion_processId_stable forward a failed reference call.
  • dbStats: scale_divides_data_size forwards a failed dbStats call; avgObjSize no longer divides fields read off a failed or incomplete reply.

Testing

  • black, isort, flake8, mypy (pre-commit) pass; pytest -m unit passes (152).
  • All 15 touched files against a fresh MongoDB 8.2.4 single-node replica set: 249 passed on this branch, 249 passed on main.
  • Failure paths were checked with a stubbed executor that makes the setup/reference call fail (or return a reply missing the needed event or field). On main each case raised TypeError, KeyError, IndexError, AttributeError or StopIteration outside the helper, and hash_different passed when the setup reply had no hash. On this branch every case fails through the assertion helper ([UNEXPECTED_ERROR], [ERROR_MISMATCH], [RESULT_MISMATCH] or [PROPERTY_MISMATCH]), and the happy path still passes.

@samafarc
samafarc requested a review from a team as a code owner October 19, 2026 20:19

@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

Routing setup and intermediate failures to the test's single assertion is the right fix, and every test still makes exactly one assertion. getParameter, getCmdLineOpts and the listIndexes loops pass the failed result on as is, which is the clearest pattern. One change can now let a test pass when it shouldn't, and one test still crashes before its assertion.

Critical Issues

  1. [Required] test_setQuerySettings_verification.py, test_setQuerySettings_hash_different: setup_hash stays None when a setup command fails or its reply has no queryShapeHash. result["queryShapeHash"] != None is then True, so the test passes without ever comparing two hashes. Before this change it raised TypeError. Please pass a failed setup result to the assertion (as test_setQuerySettings_remove / _update now do), and treat a missing setup hash as a failure rather than as "differ". result["queryShapeHash"] still raises KeyError when the command reply has no hash.
  2. [Required] test_serverStatus_core_behavior.py, test_serverStatus_cross_database_same_core_fields: admin_result.get(...) isn't guarded, so a failed admin serverStatus still raises AttributeError outside the helper.

Suggestions

  1. [Suggestion] When the first (reference) call fails, these tests compare the second result against None. So the failure reads expected 'pid' == None and the first call's real error is lost:

    • test_serverStatus_pid_consistent_across_calls (result1)
    • test_hello_connectionId_stable (r1)
    • test_hello_legacy_isMaster_fields_match (r_hello)
    • test_hello_response_topologyVersion_processId_stable (r1)
    • test_dbStats_scale_divides_data_size (unscaled/scaled, reported as False != True)

    Passing the failed result to the assertion, as the getParameter and getCmdLineOpts changes in this PR do, would report the real error.

  2. [Suggestion] test_changeStream_resume.py: _first_event_token_or_error and _event_token_of_type_or_error duplicate _first_event_token / _event_token_of_type, which still have other callers. Folding the error return into the originals keeps one helper per job, the same approach as #734 with open_find_cursors. Separately, _first_event_token_or_error still raises IndexError on an empty batch, and _event_token_of_type_or_error returns None when no matching event arrives. That None is then sent as startAfter: null, so the failure is about the argument rather than the missing event.

  3. [Suggestion] test_changeStream_events.py, _emit_event: the plain assert matching that follows the new checks still fails outside the helper when the expected event isn't in the batch. Returning the getMore result in that case would let the test's assertion report it.

Questions

  1. [Nitpick] The branch is 3 commits behind main. Please rebase before merge.

Positive Feedback

  • The listIndexes getMore loops stop at the first failed getMore and pass that error on, instead of looping on an exception.
  • test_setQuerySettings_remove / _update pass the first failed setup result on, rather than building the command from it.

@documentdb-triage-tool documentdb-triage-tool Bot added compatibility test Compatibility test related enhancement New feature or request labels Nov 5, 2026
@documentdb-triage-tool

Copy link
Copy Markdown

🤖 Auto-triaged by documentdb-triage-tool.

Applied: compatibility test, enhancement
Project fields suggested: Component test-coverage · Priority P0 · Effort L · Status Needs Review
Confidence: 0.72 (mixed)

Reasoning

component from path globs (test-coverage); P0 keyword "crash" in body; effort from diff stats (298+135 LOC, 15 files); LLM: Improves test robustness by surfacing real engine errors through assertions instead of crashing with TypeError/KeyError/AttributeError, touching multiple test files across one framework component.

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.

Several compatibility tests crash with a Python TypeError/AttributeError/
KeyError before reaching framework/assertions.py when a setup or
intermediate execute_command result is an engine exception (or a
successful response missing a field): they index into the exception,
iterate it, or build an expectation from a missing field.

Apply the PR documentdb#718 pattern across these tests: extract cursor ids and
response fields defensively, and forward a failed setup/intermediate
result to the test's single assertion so the engine's real error or
value is visible through a framework assertion helper instead of a raw
crash. Where a first response is missing a field, read it with .get(...)
so the assertion reports the mismatch. MongoDB behavior and the checked
property are unchanged.

Covers listIndexes cursor batching, bulkWrite getMore drain, changeStream
events/event-read-error/resume, setQuerySettings response/remove/update
and hash tests, getParameter *, allParameters+named and setAt variants,
dbStats scale/avgObjSize, getCmdLineOpts, serverStatus, and hello.

Signed-off-by: Samar Farooqi <samafarc@amazon.com>
test_setQuerySettings_hash_same and test_setQuerySettings_hash_different now
forward a failed setup command to the assertion, as the remove and update
tests do. hash_different treats a missing hash on either the setup reply or
the command reply as a failure instead of a difference, and reads the command
hash with .get() so a reply without one no longer raises KeyError.

Signed-off-by: Samar Farooqi <samafarc@amazon.com>
The serverStatus, hello and dbStats consistency tests compare a second reply
against fields read from a first reference reply. When the reference call
fails, its error is now passed to the assertion instead of comparing the
second reply against None. The serverStatus host and version checks and the
cross-database check no longer index a failed reply outside the helper.

Signed-off-by: Samar Farooqi <samafarc@amazon.com>
Fold the error return into _first_event_token and _event_token_of_type and
drop the _or_error copies. The helpers return the engine error, or a
LookupError when the batch is empty or has no event of the requested type,
and every caller forwards it to the test's assertion instead of indexing into
it or sending startAfter: null.

_emit_event returns the getMore reply when the batch has no event of the
expected type, so the test's assertion reports what the stream delivered
instead of a bare assert failing outside the helper.

Signed-off-by: Samar Farooqi <samafarc@amazon.com>
@samafarc
samafarc force-pushed the fix-setup-error-surfacing branch from de76c37 to bf097fe Compare November 6, 2026 00:40
@samafarc

samafarc commented Nov 6, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @eerxuan, all addressed:

  1. test_setQuerySettings_hash_different: a failed setup command now goes to the assertion, like the remove/update tests. A missing hash on either the setup or the command reply counts as a failure, and the command hash is read with .get(), so a reply without one no longer raises KeyError. hash_same had the same gap and gets the same fix.
  2. test_serverStatus_cross_database_same_core_fields: a failed admin serverStatus is now the assertion's actual value. The host and version consistency tests had the same unguarded indexing, so those are fixed too.
  3. Failed reference calls: serverStatus_pid, the 3 hello tests and dbStats_scale_divides_data_size now pass the failed result to the assertion, so the report shows the real error instead of a comparison against None or False != True.
  4. changeStream resume helpers: folded the error return into _first_event_token and _event_token_of_type, removed the _or_error copies and updated every caller. An empty capture batch, or no event of the requested type, now comes back as an error the assertion reports, so nothing raises IndexError or sends startAfter: null any more.
  5. _emit_event: when the batch has no event of the expected type, it returns the getMore reply and the test's assertProperties reports it.
  6. Rebase: rebased onto current main (10eec47).

On MongoDB 8.2.4 the 15 touched files pass 249/249, same as main. A stubbed executor confirms every failure path now fails through the assertion helper.

@samafarc

samafarc commented Nov 6, 2026

Copy link
Copy Markdown
Contributor Author

Summary

Routing setup and intermediate failures to the test's single assertion is the right fix, and every test still makes exactly one assertion. getParameter, getCmdLineOpts and the listIndexes loops pass the failed result on as is, which is the clearest pattern. One change can now let a test pass when it shouldn't, and one test still crashes before its assertion.

Critical Issues

  1. [Required] test_setQuerySettings_verification.py, test_setQuerySettings_hash_different: setup_hash stays None when a setup command fails or its reply has no queryShapeHash. result["queryShapeHash"] != None is then True, so the test passes without ever comparing two hashes. Before this change it raised TypeError. Please pass a failed setup result to the assertion (as test_setQuerySettings_remove / _update now do), and treat a missing setup hash as a failure rather than as "differ". result["queryShapeHash"] still raises KeyError when the command reply has no hash.
  2. [Required] test_serverStatus_core_behavior.py, test_serverStatus_cross_database_same_core_fields: admin_result.get(...) isn't guarded, so a failed admin serverStatus still raises AttributeError outside the helper.

Suggestions

  1. [Suggestion] When the first (reference) call fails, these tests compare the second result against None. So the failure reads expected 'pid' == None and the first call's real error is lost:

    • test_serverStatus_pid_consistent_across_calls (result1)
    • test_hello_connectionId_stable (r1)
    • test_hello_legacy_isMaster_fields_match (r_hello)
    • test_hello_response_topologyVersion_processId_stable (r1)
    • test_dbStats_scale_divides_data_size (unscaled/scaled, reported as False != True)

    Passing the failed result to the assertion, as the getParameter and getCmdLineOpts changes in this PR do, would report the real error.

  2. [Suggestion] test_changeStream_resume.py: _first_event_token_or_error and _event_token_of_type_or_error duplicate _first_event_token / _event_token_of_type, which still have other callers. Folding the error return into the originals keeps one helper per job, the same approach as Fix/compat test side fixes #734 with open_find_cursors. Separately, _first_event_token_or_error still raises IndexError on an empty batch, and _event_token_of_type_or_error returns None when no matching event arrives. That None is then sent as startAfter: null, so the failure is about the argument rather than the missing event.

  3. [Suggestion] test_changeStream_events.py, _emit_event: the plain assert matching that follows the new checks still fails outside the helper when the expected event isn't in the batch. Returning the getMore result in that case would let the test's assertion report it.

Questions

  1. [Nitpick] The branch is 3 commits behind main. Please rebase before merge.

Positive Feedback

  • The listIndexes getMore loops stop at the first failed getMore and pass that error on, instead of looping on an exception.
  • test_setQuerySettings_remove / _update pass the first failed setup result on, rather than building the command from it.

@samafarc samafarc closed this Nov 6, 2026
@samafarc samafarc reopened this Nov 6, 2026
@eerxuan
eerxuan merged commit 31c156f into documentdb:main Nov 6, 2026
20 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

compatibility test Compatibility test related enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants