Skip to content

test: move RawSimulator-internals unit tests onto b2-simulator hooks - #775

Draft
sophiecarreras wants to merge 1 commit into
masterfrom
test/migrate-unit-internals
Draft

sophiecarreras wants to merge 1 commit into
masterfrom
test/migrate-unit-internals

Conversation

@sophiecarreras

Copy link
Copy Markdown
Contributor

Paused while the author is away (back Dec 16).

Stacked on #769 (test/b2-simulator-harness). The migrated tests need backblaze-labs/b2-simulator v0.6.0 (4052c6f8f45350486460d19bc796866cbdcdd3bc) for the control routes they use (/faults, /wire-faults, /clock, /files/content-sha1, /notification-rules/suspend, extraAccounts). This branch adds no CI pin; --b2-simulator is still opt-in.

What changes

Unit tests that read or set RawSimulator state now go through 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. Test code only: nothing under b2sdk/ changes.

Old internal Under --b2-simulator
set_upload_errors([CanRetry(...)]), RequestTimeout POST /faults on b2_upload_file (503, 408, 400). On RawSimulator the upload raises what the SDK's HTTP layer raises for the same answer. Tests that recover also check that the fault fired
set_upload_errors([B2RequestTimeoutDuringUpload()]) a stall wire fault longer than a shortened upload timeout. The SDK raises B2RequestTimeout
expire_auth_token POST /clock past the token lifetime. The test also checks that a new token was issued
TruncatedDownloadRawSimulator (parallel downloader) a reset-mid-response wire fault on both download endpoints, plus a check that a download was cut short
stored content_sha1 = 'f' * 40 POST /files/content-sha1
simulate_notification_rule_suspension POST /notification-rules/suspend
second create_account() @pytest.mark.b2_simulator_config(extraAccounts=1); the harness's second create_account() returns account 2
upload_url_counter = itertools.count() not needed (b2-simulator has no 200-URL cap)

These tests were rewritten so they pass on both simulators without touching simulator state:

  • EmptyListSimulator is replaced by a raw API wrapper on the client side. A new test_empty_pages_are_served fails if the wrapper serves no empty page.
  • SSE-C downloads pass the key instead of using dont_check_encryption().
  • The retention upload enables file lock and uses a retention date in the future.
  • The file lock state is read back with list_buckets.
  • The source-replication test uses a real destination bucket and source key.
  • The unverified-checksum and name-prefix-key tests use the real B2 calls. RawSimulator cannot do either, so in the default run they keep the old simulator edit.
  • The bucket in the c8 test gets a name of 6 or more characters.

Changing a fault from a test-only exception to an HTTP answer changes one assertion: the fatal upload error now expects BadRequest with code bad_request instead of CanRetry(False).

Still on RawSimulator

  • TestTruncatedDownloadSimple (both test_bucket.py files). SimpleDownloader resumes only after a body that ends cleanly short. Over HTTP a short body raises requests.ChunkedEncodingError, because urllib3 enforces Content-Length, and SimpleDownloader doesn't catch it. Fixing that means changing the SDK, which this PR doesn't do.
  • test_testing_api_helpers.py. By design it forbids HTTP and builds RawSimulator() directly.
  • test_raw_simulator.py and test_simulator_encryption_context.py test RawSimulator itself.

Known failures under --b2-simulator in the touched tests

  • test_is_file_lock_enabled_source_replication (v2, v3). b2-simulator answers 400 bad_request "Operation not supported for buckets with source replication". b2sdk maps only source_replication_conflict, or bad_request with the text "Turning on file lock for an existing bucket having source replication configuration is not allowed.", to SourceReplicationConflict. The real code is unverified in the simulator's fidelity ledger (F-85).
  • test_upload_large_file_with_restricted_api_key (v1, v2) and test_c55_...[*-different] (v2). The v2 apiver authorizes over API v3, and b2-simulator refuses bucket-restricted keys there, as real B2 did in a measured probe (F-120). Whether real B2 refuses every key shape on v3 is unverified.
  • Tests inherited from TestLs, TestListVersions and DownloadTests that also fail in their plain classes because of test data (file lock, contentType: null, the v1 return-type literals). Those fixes are separate test-data work.

Verification

  • Default run, pytest --api=vX -n 8 test/unit: v3 1665, v2 1636, v1 1653, v0 1491 passed, 0 failed.
  • --b2-simulator, same command: failures fell from 103 / 113 / 102 / 69 to 66 / 84 / 79 / 59 (v3 / v2 / v1 / v0). No test that passed before fails now. Subtest ids are renumbered, so failing-subtest counts were compared per test.
  • With the fault calls disabled, the migrated fault and truncation tests fail (25 of 26 checked), so they depend on the faults.
  • ruff check, ruff format --check, pytest test/static: pass.

🤖 Generated with Claude Code

…-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>
@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

@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 for the red check — otherwise this is a sound migration of the RawSimulator-internals tests onto b2-simulator hooks. No rush, you're out; just flagging what to settle before undraft.

  • code/snyk is failing: "1 new Code Analysis issue found" on this PR. Needs a look before it leaves draft — may well be a false positive on test code, but it should be resolved or dismissed with justification.
  • (decision) The fatal-upload test changes from assertRaises(CanRetry) to pytest.raises(BadRequest) (code == 'bad_request'), so the default run no longer exercises the CanRetry(False) branch. The CanRetry(True) path now goes through real interpreted 503/408, which is arguably more faithful — but please decide whether to keep explicit CanRetry(False) coverage somewhere.
  • Stacking: base test/b2-simulator-harness (#769); no GitHub Actions ran, so no CI evidence beyond the body.
  • (non-blocking) Timing hooks: upload_times_out_once (0.5s timeout, 1500ms stall, 10s _wait_for) carries flake risk on slow runners, though it has post-conditions. expire_account_token advances the global virtual clock 24h+1ms (expires every token, not one) — documented, but note the interaction if combined with a time-relative retention date.
  • (non-blocking, disclosed) TestTruncatedDownloadSimple stays on RawSimulator because SimpleDownloader doesn't catch requests.ChunkedEncodingError — a real SDK gap surfaced, not a test problem. Source-replication test still fails under the flag (F-85).

Happy to approve once Snyk is resolved and the CanRetry coverage question is settled, with a real CI run.

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