Skip to content

test: run the unit tests on b2-simulator by default, in every CI unit job - #801

Draft
sophiecarreras wants to merge 10 commits into
masterfrom
test/b2-simulator-default
Draft

sophiecarreras wants to merge 10 commits into
masterfrom
test/b2-simulator-default

Conversation

@sophiecarreras

@sophiecarreras sophiecarreras commented Nov 27, 2026 •

Copy link
Copy Markdown
Contributor

Paused while the author is away (back Dec 16). Draft; based on #769 (test/b2-simulator-harness), with #771 (test/migrate-unit-g1) and #775 (test/migrate-unit-internals) merged in. Review after those two.

What

The unit tests now run against b2-simulator (backblaze-labs/b2-simulator) by default, and every CI unit-test job checks out the pinned release. Test and CI code only; nothing under b2sdk/ changes. No test is skipped, deleted, xfailed or loosened.

  1. Default flip (test/unit/b2_simulator.py)
    • The unit tests need B2_SIMULATOR_DIR and node. Without them the run stops with a usage error that names the variable and the opt-out; it never skips. The old --b2-simulator flag is gone.
    • --rawsimulator runs the whole suite on the in-process RawSimulator and needs no checkout. It deselects the needs_b2_simulator tests.
    • @pytest.mark.rawsimulator(reason=..., from_ver=, to_ver=) keeps one test on RawSimulator. The reason is required.
    • An unmarked unit test that builds any RawSimulator, or a subclass, fails and names the marker. So every test left on RawSimulator is visible in the code (list below).
    • The unit conftest activates the plugin, so integration and static runs never need a checkout.
  2. Tighter assertions. The tests that accepted either simulator's error now accept only real B2's, exact type and code: a refused governance delete is Unauthorized access_denied (test_bucket.py, test_file_version.py, test_api.py). A wrong SHA-1 is BadRequest bad_request "Sha1 did not match data received". Also changed to real B2's answer: a wrong key on authorize is Unauthorized unauthorized, and a HEAD of a hidden file by name is FileNotPresent. Both were measured on B2 on 2026-11-27. RawSimulator answers differently, so these tests are needs_b2_simulator. That marker also covers the v0/v1 tests that compare raw answers whole in B2's shape (contentMd5, retainUntilTimestamp, options: ["s3"], isFileLockEnabled: false).
  3. Remaining test data that only RawSimulator accepted. This applies the test: run G1 unit tests on b2-simulator (test data fixes) #771 fixes to test_api.py, v0/test_bucket.py and test_regression_bucket.py: future retention dates on Object Lock buckets, and ids, timestamps, revisions and stored content types taken from the server. It also uses a three-part progress upload and b2/x-auto instead of a null contentType on raw b2_start_large_file calls. The legacy-API regression tests now take their raw API from raw_api_class().
  4. CI. ci.yml test (3 OS × 6 Python, 17 jobs, matrix unchanged) and cd.yml test (the release gate) follow simulator-integration.yml from ci: run pull-request integration against B2 Simulator; live B2 weekly #768:
    • The pin is set once per workflow: SIMULATOR_REVISION = 4052c6f8f45350486460d19bc796866cbdcdd3bc (b2-simulator v0.6.0).
    • The checkout uses the read-only SIMULATOR_DEPLOY_KEY with persist-credentials: false. A step verifies the revision.
    • A missing key fails with a message naming the Actions and Dependabot secrets.
    • In ci.yml, a pull request from a fork fails first with a message.
    • Node.js 22 is set up. B2_SIMULATOR_DIR goes in the unit step's env, with no GITHUB_ENV writes.
    • The SDK is checked out at sdk/, next to simulator/.
    • test/static/test_unit_tests_simulator.py checks all of this. It also runs the fork guard's script for a fork PR, a same-repository PR and a push.
  5. docs/testing.md, doc/source/contributing.rst and a changelog entry.

Tests left on RawSimulator

Count = test runs per apiver (v3 / v2 / v1 / v0), including the plugin's own marker check.

Reason (marker text) Tests v3 v2 v1 v0
Tests RawSimulator itself test_raw_simulator.py, test_simulator_encryption_context.py 9 12 12 12
Live-test helpers with HTTP forbidden test_testing_api_helpers.py 5 5 5 5
Plugin check that a marked test has no control plane test_b2_simulator_plugin.py 1 1 1 1
#776 SimpleDownloader does not resume after a mid-body drop TestTruncatedDownloadSimple (both test_bucket.py) 16 16 14 11
#772 copy_file asserts SSE-C source test_copy_encryption 1 1 1 1
b2-simulator gap: v0.6.0 refuses fileInfo {} with COPY, which v1 copy_file sends. Real B2 accepts it and copies the source metadata (measured 2026-11-27; #773 was closed as not a bug). Remove the marker once the simulator matches test_copy_without_optional_params, test_copy_with_range, test_copy_with_different_bucket (v1, v0) – – 3 3
#774 LegalHold.UNSET sent as off test_explicit_resume_accepts_matching_protections[retention1-LegalHold.UNSET] 1 1 1 1
#770 null contentType on start_large_file test_cancel_large_file_v1 / _v2 1 1 1 1
b2-simulator gap: it refuses a single-bucket key made by v3 b2_create_key at v3 authorize. Real B2 accepts that key (measured 2026-11-27). apiver v0–v2 authorize at v3 the 5 test_api.py restriction tests, test_upload_large_file_with_restricted_api_key (2 classes), TestAuthorizeForBucket, test_c55_failed_authorization_preserves_session (v2) – 18 7 5
Open question: SSE-C file read by get_file_info_by_name (HEAD without the key, no key parameter). b2-simulator answers 400; real B2 not measured TestListVersions::test_encryption (+ TestEmptyListVersions), test_get_file_info_by_name_with_properties 3 3 3 1
Open question: turning on file lock for a replication source. b2-simulator answers 400 bad_request with a text the SDK does not map to SourceReplicationConflict; real B2 not measured test_is_file_lock_enabled_source_replication 1 1 – –
b2-simulator gap: customUploadTimestamp on b2_start_large_file. B2's docs type it as a number, and the SDK sends a number; b2-simulator v0.6.0 accepts only a string (custom_timestamp_invalid). Remove the marker once the simulator matches 5 C45 tests in test_regression_bucket.py 5 5 – –

The two open questions were not in the planned list, and neither was the custom-timestamp gap. Each needs one measurement on real B2. The answer then decides whether the fix belongs in b2-simulator, in the SDK or in the test.

Numbers

b2-simulator v0.6.0 (4052c6f), Python 3.13, macOS, pytest -n 8 test/unit:

apiver default (b2-simulator) --rawsimulator before this branch, --b2-simulator (merged #771 + #775)
v3 1684 passed, 78 skipped, 1 xfailed, 0 failed 1656 passed, 74 skipped, 1 xfailed, 0 failed 30 failed, 1660 passed
v2 1655 passed, 117 skipped, 1 xfailed, 0 failed 1627 passed, 113 skipped, 1 xfailed, 0 failed 46 failed, 1615 passed
v1 1672 passed, 208 skipped, 0 failed 1642 passed, 206 skipped, 0 failed 35 failed, 1636 passed
v0 1510 passed, 495 skipped, 0 failed 1483 passed, 490 skipped, 0 failed 55 failed, 1457 passed

--rawsimulator ran with no B2_SIMULATOR_DIR and deselects 32 needs_b2_simulator tests per apiver. The "before" failures count subtests as well. nox -s unit-3.13, the command CI runs (-n auto, doctests, coverage), also passed at all four apivers, with the same counts as the default column, and cover passed.

How it was checked

  • Plugin tests were written first. Against the old plugin, 13 failed for the expected reasons: opt-in only, no marker, no guard. After the change they passed at v3, v2, v1 and v0 (24 passed, 1 skipped) and with --rawsimulator. Two mutations were each caught: removing the guard, and ignoring the marker.
  • The tightened assertions were run with --rawsimulator before needs_b2_simulator was added. They failed on RawSimulator's AccessDenied / FileSha1Mismatch (5 failures at v3 and v1), so they tell the two answers apart. On b2-simulator they pass.
  • Each test-data fix was red on b2-simulator in the baseline run and green after the fix, in both modes.
  • The first full --rawsimulator run had 1 failure per apiver. The plugin's whole-directory collection test ran its subprocess without --rawsimulator. That is fixed in the last commit; the run above is after the fix.
  • Static workflow tests were written first: 10 failed, then all 83 static tests passed. nox -s lint and nox -s towncrier_check passed. actionlint was not available locally: NOT RUN.
  • NOT RUN: the CI jobs themselves. ci.yml runs only for pull requests into master, so this PR (base test/b2-simulator-harness) gets no unit-test run. Windows and PyPy were not run locally; the guard and verify steps use bash.
  • Merge: test: move RawSimulator-internals unit tests onto b2-simulator hooks #775 was merged into test: run G1 unit tests on b2-simulator (test data fixes) #771's branch. test_bucket.py conflicted only on one import that test: move RawSimulator-internals unit tests onto b2-simulator hooks #775 made unused; it was dropped.

🤖 Generated with Claude Code

sophiecarreras and others added 9 commits November 27, 2026 07:04
Tests that use only the normal B2 API now pass on b2-simulator as well as
on RawSimulator, at apiver v3, v2, v1 and v0. Each fix stays an exact check:

- Retention dates are one day in the future, and tests that set retention
  or legal hold use a bucket with Object Lock (B2 refuses both otherwise).
- test_update sends B2-valid CORS, lifecycle and replication settings (real
  keys and destination buckets) and derives account id, bucket id, name,
  revision and options from the server instead of RawSimulator's values.
- Lifecycle rules carry an action, which B2 requires.
- The resume helper and v1 test_started_large_file pass a content type:
  B2 refuses the null that content_type=None sends.
- Expected content types for b2/x-auto uploads, part file ids and v1
  download file ids come from the server's answers.
- A refused governance delete is 401 access_denied on B2 (Unauthorized in
  the SDK), and a wrong SHA-1 is 400 bad_request "Sha1 did not match data
  received" (BadRequest); RawSimulator's AccessDenied and FileSha1Mismatch
  stay accepted until the default flips. The wrong-SHA-1 test now also
  checks that nothing was stored.
- test_custom_timestamp enables custom upload timestamps on the simulator.
- TestAuthorizeForBucket stays in-process at apiver v2: b2-simulator
  v0.6.0 refuses every bucket-restricted key at API v3.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…-simulator

Tests that reached into RawSimulator's state now use test/unit/simulator_hooks.py.
Each hook uses RawSimulator's test methods in the default run and b2-simulator's
control plane under --b2-simulator:

- Upload errors: POST /faults on b2_upload_file (503, 408, 400). On
  RawSimulator the upload raises what the SDK's HTTP layer raises for that
  answer. The fatal-error tests now expect BadRequest (code bad_request)
  instead of a test-only exception. Tests that recover from an injected
  error also check that it fired.
- Upload timeout: a stall wire fault longer than a shortened upload timeout;
  the SDK sees B2RequestTimeout, the exception requests' timeout becomes.
- Token expiry: POST /clock past the token lifetime. The reauthorization
  test now also checks that a new token was issued for the same key.
- Truncated downloads (parallel): a reset-mid-response wire fault on both
  download endpoints, with a check that a download was cut short.
- Corrupted stored SHA-1: POST /files/content-sha1.
- Notification rule suspension: POST /notification-rules/suspend.
- Second account: @pytest.mark.b2_simulator_config(extraAccounts=1); the
  harness's second create_account() returns account 2's credentials.

Rewritten without simulator state, valid on both:
- The empty-page listings use a client-side raw API wrapper instead of a
  BucketSimulator subclass, and a new test checks that empty pages are served.
- SSE-C downloads pass the key instead of bypassing the encryption check.
- The retention upload enables file lock and uses a future date.
- File lock state is read back with list_buckets.
- The source-replication test uses an existing destination bucket and key.
- The unverified-checksum test uploads with an "unverified:" SHA-1 on
  b2-simulator (RawSimulator refuses the prefix, so it keeps the old edit).
- The c8 test bucket name is 6 characters or more, as B2 requires.

The simple-downloader truncation tests and test_testing_api_helpers.py stay on
RawSimulator.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
- test_upload_bytes_progress uploads three parts, so the listener gets an
  update per part on any transport. It still requires several updates
  ending at the total; an 11-byte body is read once over HTTP, and only
  RawSimulator's forced chunked reads gave it several updates.
- test_scan_source: the source bucket has Object Lock (for the retention
  file) and the large-metadata files use 1,900 bytes of file info. That
  fits b2_upload_file's documented limits (7,000 bytes for name plus info,
  2,048 for SSE or Object Lock buckets); the old 7,000-byte value broke
  both. The upload headers stay over the 2,048 bytes has_large_header
  measures, so every category is still checked.
- Mark the two dual-acceptance error checks (governance delete, wrong
  SHA-1) with a TODO naming the real-B2 error to assert once
  --b2-simulator is the default.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…o test/b2-simulator-default

# Conflicts:
#	test/unit/bucket/test_bucket.py
The unit tests now use b2-simulator unless pytest gets --rawsimulator.
B2_SIMULATOR_DIR and node are required; without them the run stops
with a usage error that names the variable and the opt-out. The old
--b2-simulator flag is gone.

- @pytest.mark.rawsimulator(reason=..., from_ver=, to_ver=) keeps one
  test on the in-process RawSimulator at those apivers. The reason is
  required; a test cannot carry it together with needs_b2_simulator.
- An unmarked unit test that builds any RawSimulator (or a subclass)
  fails, naming the marker, so every test left on RawSimulator is
  visible in the code.
- --rawsimulator runs the whole suite on RawSimulator and deselects
  the needs_b2_simulator tests.
- The unit conftest activates the plugin, so integration and static
  runs never need a checkout.
- The part-size plugin tests read the server's authorize answer: at
  apiver v0 and v1 the account info reports the SDK's own minimum.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Each test left on the in-process RawSimulator now carries
@pytest.mark.rawsimulator with a reason; the reasons shared by several
files live in test/unit/b2_simulator.py.

- RawSimulator's own tests (test_raw_simulator.py,
  test_simulator_encryption_context.py) and the live-test helper tests
  that forbid HTTP (test_testing_api_helpers.py).
- TestTruncatedDownloadSimple (both test_bucket.py files): #776.
  The TRUNCATE_ON_B2_SIMULATOR switch is replaced by the marker.
- test_copy_encryption: #772. v1/v0 copy_file tests without a metadata
  directive: #773. The UNSET legal-hold case of the explicit-resume
  test: #774. test_cancel_large_file_v1/v2: #770.
- Tests that authorize with a single-bucket key at apiver v0 to v2
  (B2 API v3): b2-simulator v0.6.0 refuses those keys while real B2
  accepts a key made by v3 b2_create_key. Marked to_ver=2; at v3 they
  run on b2-simulator. TestAuthorizeForBucket's in-process subclass is
  replaced by the same marker.

The legacy-API regression tests only needed a raw API object; they now
get it from raw_api_class() and run on b2-simulator.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…data

With b2-simulator as the default, the tests that accepted either
RawSimulator's error or real B2's now accept only real B2's:

- governance-locked delete (test_bucket.py, test_file_version.py,
  test_api.py): Unauthorized with code access_denied (401), exact type.
- wrong SHA-1 upload: BadRequest bad_request "Sha1 did not match data
  received" (400), exact type.
- wrong key on authorize (test_reauth_drops_pooled_urls.py): Unauthorized
  with code unauthorized (401 unauthorized "", measured 2026-11-27).
- HEAD of a hidden file by name (test_api.py): FileNotPresent, as for a
  missing file (404 not_found, measured 2026-11-27).

RawSimulator gives other answers, so these tests are needs_b2_simulator
(deselected only with --rawsimulator); the same goes for the v0/v1 tests
that compare raw answers whole in real B2's shape (contentMd5,
retainUntilTimestamp, options ['s3'], isFileLockEnabled false).

Test data that only RawSimulator accepted, fixed as the G1 lane fixed
the v1+ copies: retention dates in the future and Object Lock buckets
for retention and legal hold, ids, timestamps, revisions and stored
content types taken from the server, three-part progress upload, and
b2/x-auto instead of a null contentType on raw b2_start_large_file
calls (test_api.py, v0/test_bucket.py, test_regression_bucket.py). The
C45 class turns on custom upload timestamps for its reset.

Also kept on RawSimulator with a written reason (real B2 not measured):
- SSE-C file read by get_file_info_by_name (HEAD without the key):
  TestListVersions::test_encryption, test_get_file_info_by_name_with_properties.
- file lock on a replication source: test_is_file_lock_enabled_source_replication.
- custom upload timestamp on b2_start_large_file: b2-simulator accepts
  only a string, the SDK sends a number (five C45 tests).
- c55 failed-authorization tests at apiver v2: the single-bucket key gap.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The unit tests now need a b2-simulator checkout, so each job that runs
`uv run nox -vs unit` gets one: the ci.yml test matrix (3 OS x 6
Python, unchanged) and the cd.yml release test job. The pattern is the
one of simulator-integration.yml:

- SIMULATOR_REVISION is stated once per workflow (4052c6f, b2-simulator
  v0.6.0); the checkout uses it, the read-only SIMULATOR_DEPLOY_KEY and
  persist-credentials: false, and a step verifies the revision.
- A missing deploy key fails with a message naming the Actions and
  Dependabot secrets; the key itself never reaches a shell.
- In ci.yml a pull request from a fork fails first, with a message:
  it cannot check out the private simulator.
- Node.js 22 is set up; B2_SIMULATOR_DIR is given to the unit-test step
  through its env, never through GITHUB_ENV. The SDK moves to an `sdk`
  checkout path next to `simulator`.
- Guard and verify steps use bash, so they also run on Windows.

test/static/test_unit_tests_simulator.py checks all of this and runs
the fork guard's script for a fork PR, a same-repository PR and a push.

docs/testing.md and the contributing guide describe the new default,
--rawsimulator and the rawsimulator marker.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
With --rawsimulator and no B2_SIMULATOR_DIR, the subprocess that
collects `test` ran in the default mode and stopped with the usage
error. It now gets --rawsimulator when its parent run has it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@coderabbitai

coderabbitai Bot commented Nov 27, 2026

Copy link
Copy Markdown

Important

Draft PR not reviewed

Draft PRs are not automatically reviewed by default.

  • Trigger a manual review

To automatically review draft PRs, update your CodeRabbit configuration:

reviews:
  auto_review:
    drafts: true
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

…ects

- v1/v0 copy_file without a metadata directive: real B2 accepts
  fileInfo {} with COPY and copies the source metadata (measured
  2026-11-27), so #773 is not an SDK defect. b2-simulator v0.6.0 is
  stricter; the marker now says so and is removed once it matches.
- C45 custom upload timestamps on b2_start_large_file: B2 documents
  customUploadTimestamp as a number, which the SDK sends; b2-simulator
  v0.6.0 accepts only a string. Same wording.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

@ffumero2003 ffumero2003 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Requesting changes to flag the blockers before this leaves draft — the test migration itself is careful and well-guarded; the concerns are the workflow side. No rush, you're out.

  • code/snyk is failing ("2 new Code Analysis issues found"). Likely a false positive on the new simulator credential-shaped strings in test code (f'sim-master-secret-{count+1}' in b2_simulator.py ~L1109, ('sim-master-id-2','sim-master-secret-2') in test_b2_simulator_plugin.py ~L2908 — loopback sim creds), but it needs resolving/dismissing with justification.
  • (decision) The new ci.yml first step "Require a pull request branch in this repository" makes every fork PR hard-fail the required unit matrix — b2-simulator is private, so external contributors can't get green CI on this public repo (documented in docs/testing.md). Plus Dependabot needs its own SIMULATOR_DEPLOY_KEY secret. This is a team policy call to settle before merge, and an admin should confirm the repo/Dependabot secrets exist.
  • (process) None of the new workflow steps have run — no GitHub Actions on the head SHA at all (stacked base isn't main), so the pinned checkout / Node setup / nox -s unit and the cd.yml release gate are unverified. Get a scoped branch run (ubuntu/macOS/Windows/pypy3.10) before merge, and check whether the required-checks list changes.
  • (non-blocking) Good: RawSimulator is kept as an explicit opt-in (@pytest.mark.rawsimulator), an unmarked test building a RawSimulator fails loudly (_refuse_raw_simulator), workflow hygiene is tight (SHA-pinned, simulator pinned+verified, persist-credentials: false, key only to the one checkout, enforced by test_unit_tests_simulator.py). Several assertions are genuinely tightened (CanRetry→BadRequest+code, real B2 messages, seconds→ms retention fix).
  • (non-blocking) Same self-referential pattern as #769/#771: b2/x-auto and fixed timestamps/ids replaced by values read from live state in test_api.py ~L110 and test_bucket.py ~L555/623/1430 — consider also pinning the type B2 resolves (the changelog says it was measured). And a few *_GAP/*_UNMEASURED markers have no linked issue — they'll rot without tracking.

Migration looks sound — mainly the Snyk finding, the fork-PR policy, and a real CI run before undraft. Happy to approve once those are settled.

Base automatically changed from test/b2-simulator-harness to master November 30, 2026 09:56

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants