openlibrary icon indicating copy to clipboard operation
openlibrary copied to clipboard

Documented local test command fails: LOCAL_DEV fallback route breaks 5 FastAPI route-registration tests

Open pnelson1337 opened this issue 2 months ago • 8 comments

Problem

docker compose run --rm home make test — the test command from the Quick Start — fails on a clean checkout of master with 5 failures, while CI is green on the same commit.

FAILED openlibrary/tests/fastapi/test_borrow.py::TestBorrowRoute::test_missing_slug_404s
FAILED openlibrary/tests/fastapi/test_cdn.py::TestIaJsCdn::test_various_invalid_filenames_return_404[../../etc/passwd]
FAILED openlibrary/tests/fastapi/test_cdn.py::TestIaJsCdn::test_various_invalid_filenames_return_404[]
FAILED openlibrary/tests/fastapi/test_hide_banner.py::TestHideBannerEndpoint::test_only_post_without_json_suffix_is_registered
FAILED openlibrary/tests/fastapi/test_qrcode.py::TestQRCodeEndpoint::test_only_get_is_registered
= 5 failed, 5956 passed, 41 skipped, 5 deselected, 1 xfailed in 58.78s =
make: *** [Makefile:104: test] Error 2

The cause is LOCAL_DEV. At openlibrary/asgi_app.py:275 the catch-all proxy route is registered only in local dev:

if get_ol_env().LOCAL_DEV or get_deployment_name() == "testing":
    @app.api_route("/{path:path}", methods=["GET", "POST", "PUT", "DELETE", "PATCH", "OPTIONS", "HEAD"])
    async def fallback(request: Request, path: str) -> Response:

Those 5 tests assert that a method or path is not registered — test_only_get_is_registered does POST /qrcode and expects 405. With the fallback present that request is captured and proxied to web:8080 instead, which the test container can't resolve, so the assertion sees an httpx.ConnectError rather than a 405.

The home container sets LOCAL_DEV=true, so the documented local command runs these tests in the one environment where they cannot pass. CI never sets LOCAL_DEV, so it cannot catch this.

This is a recent regression rather than longstanding. The fallback landed 2026-08-26 in #13423; checking out its parent 2f3f64979 and running the same four files in the same container, with LOCAL_DEV left at the container default, gives 39 passed. The same command on master gives the 5 failures above.

Toggling only that variable is enough to flip them:

$ docker compose run --rm --no-deps -T home python -m pytest <the 4 files> -q
5 failed

$ docker compose run --rm --no-deps -T -e LOCAL_DEV=false home python -m pytest <the 4 files> -q
39 passed

Reproducing the bug

  1. Clone master fresh (I was on e5b6d22c8), make git, docker compose up -d
  2. docker compose run --rm home make test
  • Expected behavior: the suite passes, as it does in CI on the same commit
  • Actual behavior: 5 failures and make exits non-zero

Adding -e LOCAL_DEV=false to the same command makes all 5 pass.

Context

  • Browser (Chrome, Safari, Firefox, etc): n/a
  • OS (Windows, Mac, etc): Windows 11 + WSL2, Ubuntu 26.04
  • Logged in (Y/N): n/a
  • Environment (prod, dev, local): local — Docker Engine 29.1.3, Compose 2.40.3, no Docker Desktop

Breakdown

Implementation Details (for maintainers)

Two directions, and I didn't want to assume which you'd prefer. The fastapi_client fixture could build the app with LOCAL_DEV forced off, since these are unit tests of route registration rather than of the dev proxy — that keeps them meaningful in both environments. Alternatively the 5 tests could account for the fallback being present, though that makes them assert proxy behaviour rather than what they were written to check.

Worth noting either way that CI cannot regress-test this, since it never sets LOCAL_DEV.

Requirements Checklist

  • [ ] docker compose run --rm home make test passes on a clean checkout
  • [ ] the 5 route-registration tests stay meaningful with the dev fallback registered

Related files

  • openlibrary/asgi_app.py:275 — the LOCAL_DEV-gated catch-all fallback route
  • openlibrary/tests/fastapi/conftest.py:19 — fastapi_client, builds the real app via create_app()
  • openlibrary/tests/fastapi/ — test_borrow.py, test_cdn.py, test_hide_banner.py, test_qrcode.py

Stakeholders

  • @RayBB (#13423, added the fallback route)

Instructions for Contributors

  • Please run these commands to ensure your repository is up to date before creating a new branch to work on this issue and each time after pushing code to Github, because the pre-commit bot may add commits to your PRs upstream.

pnelson1337 avatar Sep 08 '26 01:09 pnelson1337

Thank you @pnelson1337 for submitting this issue!

Context

Verified each claim in this report against the repo at e5b6d22c8 (the same commit the reporter tested against):

  • The fallback route is exactly as described — openlibrary/asgi_app.py#L275-L284. It's gated on get_ol_env().LOCAL_DEV or get_deployment_name() == "testing".
  • fastapi_client in openlibrary/tests/fastapi/conftest.py#L18-L29 builds the app via a plain create_app() call with no override of LOCAL_DEV — it inherits whatever the process environment sets, confirming the fixture is the actual point of failure rather than the four test files.
  • Each of the 5 failing assertions does expect a "not registered" response for a path/method the fallback route would otherwise intercept:
  • The fallback route was added in #13423 ("Add FastAPI fallback proxy to web.py for local development and testing"), merged 2026-08-26 by @RayBB — matches the reporter's bisection exactly.
  • Related but separate: #13586 (open) reports a different fresh-checkout breakage (_testing-prs.json bind-mount write failure) in the same Docker/dev-environment area. Not a duplicate of this issue, but worth being aware of if both are being triaged together.
  • #13514 (closed, also @RayBB) added get_deployment_name()/OL_DEPLOYMENT_NAME, which is the other half of the same gating condition — relevant background for anyone touching this conditional.

Issue Assessment

Open questions

  • [x] Which of the two proposed directions is preferred — force LOCAL_DEV=false in the fastapi_client fixture, or rewrite the 5 tests to expect proxy behavior? (Partial answer: forcing the fixture is consistent with what these tests were written to check — route registration semantics — while updating the tests to expect a proxy call would make them assert proxy_to_webpy behavior instead, which duplicates what test_various_invalid_filenames_return_404 and friends are not trying to test. The reporter's own framing agrees. Final sign-off on scope/approach is still a maintainer call, since it affects the intended coverage of these route-registration tests.)
  • [ ] Should make test (or the home container's default env) set LOCAL_DEV=false for test runs generally, independent of the fixture-level fix, so this class of drift can't recur for future fallback-dependent routes? This is a broader dev-environment/CI policy question outside what can be resolved from the codebase alone.

Stakeholders

🔔 Stakeholders

@RayBB — authored #13423 which introduced the LOCAL_DEV-gated fallback route this issue traces back to, and is the listed lead for FastAPI migration / dev experience work.

Full triage checklist
  • [x] Triage — correct labels applied or removed
  • [x] Problem / opportunity — problem is clear, audience identified, actionable
  • [x] Scope — issue is well-scoped; does not try to do too many things
  • [x] Justification — reasoning and impact stated; sufficient for leads to prioritize
  • [x] Success & testing criteria — defines what "done" looks like, verifiable by a contributor
  • [x] Proposal & breakdown — concrete plan present; checklist actionable for a contributor
  • [x] If bug: environment & repro steps — browser, version, local vs prod; steps to reproduce
  • [x] Risks, concerns, open questions — identified and surfaced
  • [x] Richy context — relevant code, commits, PRs reviewed; no duplicate found
  • [x] References — screenshots, designs, docs, and links present where needed

[!NOTE] This comment was automatically generated by Richy, Open Library's issue enrichment assistant, on behalf of @mekarpeles. This is NOT a sign-off. This issue needs lead review before work begins.

openlibrary-bot avatar Sep 08 '26 02:09 openlibrary-bot

Tried option (a) locally, to save whoever picks this up the experiment.

Patching the fastapi_client fixture to build the app with LOCAL_DEV forced off — patch.object(get_ol_env(), "LOCAL_DEV", False) around the create_app() call — gives:

before:  5 failed, 5956 passed, 41 skipped, 5 deselected, 1 xfailed
after:   5961 passed, 41 skipped, 5 deselected, 1 xfailed

Exactly the five flip and nothing else moves, so at least it has no collateral effect on the rest of the suite. Not making a case for (a) over (b) — just removing the guesswork about whether it's safe.

pnelson1337 avatar Sep 08 '26 02:09 pnelson1337

Hi — I'd like to work on this.

Problem docker compose run --rm home make test fails five FastAPI tests because the home container sets LOCAL_DEV=true. That makes create_app() register the catch-all fallback in openlibrary/asgi_app.py (/{path:path} for all methods, gated on get_ol_env().LOCAL_DEV or get_deployment_name() == "testing").

fastapi_client in openlibrary/tests/fastapi/conftest.py is session-scoped and calls create_app() with whatever env the process has, so those unit tests inherit the proxy.

The five failures are all “this path/method is not a FastAPI route” checks:

  • POST /qrcode — only GET is registered (internal/api.py)
  • GET /hide_banner and POST /hide_banner.json — only POST /hide_banner exists
  • GET /books/OL1M/borrow — the route requires {olid}/{slug}/borrow
  • GET /cdn/archive.org/../../etc/passwd and empty filename — {filename} is a single segment, so these miss the CDN route entirely (the evil.js case still 404s from ALLOWED_FILES and is not one of the failures)

With the fallback present, Starlette forwards them to web:8080 instead of returning 404/405. Proxy behaviour is already covered in test_proxy.py.

Approach (option a from the report) Keep the five tests as route-registration tests. In fastapi_client, build the app with LOCAL_DEV forced off so the fallback is not registered.

OLEnv.LOCAL_DEV is a cached_property on a singleton, so I would patch get_ol_env().LOCAL_DEV at create_app() time (same experiment @pnelson1337 already ran: the five tests pass and the rest of the suite does not move). I would not change asgi_app.py or rewrite the five tests, and I would not set LOCAL_DEV=false on the home service — that proxy is still needed for local browsing.

The other gate is get_deployment_name() == "testing". The home service does not set OL_DEPLOYMENT_NAME, so forcing LOCAL_DEV off should be enough for the documented make test path.

Files

  • openlibrary/tests/fastapi/conftest.py (the fixture)
  • no changes expected in asgi_app.py or the four test files unless review asks for them

Done when

  • docker compose run --rm home make test passes on a clean checkout
  • the five tests still assert 404/405 (not proxy behaviour)

@RayBB this still has Needs: Response / Needs: Triage. Happy to wait for a go-ahead on option (a) before I open a PR. One question: should this PR also add a LOCAL_DEV=true CI job so this class of failure is caught in GitHub Actions, or keep that as a follow-up?

csy20 avatar Sep 09 '26 21:09 csy20

Taking this if csy20 isn't still on it — no PR in the 31 days since, so I'll assume it's free.

Re-ran on current master (28be8a99d): it's now 5 failures, not 4 — test_qrcode.py::TestQRCodeEndpoint::test_only_get_is_registered joined since this was filed. That's the pattern worth noting: every new route-registration test lands in the same trap, so it grows with the FastAPI migration rather than staying put.

I hit this independently in #13718 and had to prove on clean master that it wasn't mine before I found this issue — that note is still in that PR's description. The documented quick-start command failing while CI is green on the same commit means the natural reading is "I broke something", which is the real cost.

@pnelson1337's experiment with (a) is the convincing part — 5 failed → 5961 passed with nothing else moving. I'll go with that: force LOCAL_DEV off around create_app() in the fastapi_client fixture.

One thing I'd flag rather than decide alone: (a) means the test app never registers the fallback at all, so that route stays untested. It has no coverage today either, so nothing changes in practice — but if you'd rather keep the fallback present and give just those five tests their own app, say so and I'll do (b).

@RayBB

MohitSAGAR11 avatar Oct 10 '26 08:10 MohitSAGAR11

@RayBB can you explain what the different environment expectations are, and why 'fastapi' has them, and why there seems to be a confusion between the testing environment (testing.openlibrary.org, which is what I think the PR status page relates to) and LOCAL_DEV? Is CI LOCAL_DEV in this approach?

I thought the environments might be an ENUM, but it seems to be implemented as a wrapper for a LOCAL_DEV boolean.

@MohitSAGAR11's PR had a detailed explanation about what they changed, but your comment "Decided to push up a slightly different approach that's a bit simpler and cleaner long term here." probably overwrites the majority of that.

It makes it very hard to review PRs when the descriptions don't match the content. It doesn't appear the original change was ever tested in LOCAL_DEV, so I don't have much confidence the underlying feature is necessarily sound or has clear naming.

Linking a "closes issue number" is less important than explaining what you changed and how it relates to, and documents usable features.

hornc avatar Oct 13 '26 12:10 hornc

Confirming from the reporter's side on current master (510e39f1d), in the dev container with LOCAL_DEV=true as compose sets it:

docker compose run --rm home make test-py
4286 passed, 20 skipped, 79 deselected, 1 xfailed

All five tests from the original report run and pass; none of them are among the deselected.

On whether CI is LOCAL_DEV: it never was. The workflow doesn't set it, and the root conftest.py now drops it before anything imports, so a local run and CI see the same environment.

One thing this doesn't cover, in case it bears on the naming question: the gate itself is still untested. test_proxy.py exercises proxy_to_webpy on its own throwaway app, but nothing checks which routes create_app() registers under LOCAL_DEV versus the testing deployment.

pnelson1337 avatar Oct 13 '26 15:10 pnelson1337

Yes, the PR description should have been updated to match better to the changes.

To answer your questions:

  • Local dev and testing.openlibrary.org: FastAPI is the front door. Any request it doesn't handle gets proxied to web.py.
  • Production: nginx handles the split between webpy and fastapi.

That should change in the near future and that's why fastapi needs to know what environment it is in. Before changing we want to be careful to understand the performance implications and ensure our metrics won't become messy from this.

The proxy gate checks two separate things: LOCAL_DEV (set by the dev compose file) and the deployment name ("testing" for testing.openlibrary.org). The two are unrelated except that both turn on the proxy.

CI is not LOCAL_DEV. The bug was that tests run inside the dev container did see LOCAL_DEV=true, while CI didn't.

RayBB avatar Oct 14 '26 03:10 RayBB

Having all the test environments be intentionally different than the production environment sounds like a recipe for trouble.

tfmorris avatar Oct 14 '26 16:10 tfmorris