Skip to content

Stop static_ip_test stubs leaking into later test collection - #2

Merged
IMINABO1 merged 1 commit into
mainfrom
fix/static-ip-test-stub-leak
Sep 16, 2026
Merged

IMINABO1 merged 1 commit into
mainfrom
fix/static-ip-test-stub-leak

Conversation

@IMINABO1

Copy link
Copy Markdown
Owner

Problem

The scheduled Unit job on main has failed since the 2026-09-14 upstream merge, at pytest collection:

testing/unit/framework/test_orchestrator_test.py -> test_orc.test_orchestrator -> core.testrun -> api.api -> core.tasks
ModuleNotFoundError: No module named 'apscheduler.schedulers.asyncio'; 'apscheduler.schedulers' is not a package

APScheduler==3.10.4 is installed correctly. The cause is testing/unit/framework/static_ip_test.py, which unconditionally placed MagicMock objects in sys.modules for apscheduler, apscheduler.schedulers, docker and others. pytest collects the directory alphabetically, and the upstream-added test_orchestrator_test.py (google#1636) is collected right after static_ip_test.py. A MagicMock has no __path__, so importing the real apscheduler.schedulers.asyncio submodule through it fails. The same chain also imports docker.types and docker.models.containers, so removing only the apscheduler entries would have hit the identical error next.

Fix

Stub a module only when importlib.util.find_spec cannot find it, and trim the list to the five names this file's own import chain reaches (weasyprint, netifaces, jinja2, bs4, pwd). In CI every requirement is installed, so nothing is stubbed and sys.modules is left exactly as upstream's tests expect. On a dev box missing the Linux-only or heavy packages, the file still imports.

Verification

  • Python 3.10 venv from framework/requirements.txt on Ubuntu (WSL): unfixed tree reproduces the CI error exactly; fixed tree runs pytest testing/unit/framework with 64 passed.
  • Explicit order pytest static_ip_test.py test_orchestrator_test.py: 20 passed.
  • pylint==3.2.6 --rcfile=.pylintrc on the file: 10.00/10.
  • Windows, no weasyprint/netifaces/pwd installed: static_ip_test.py alone, 13 passed, with only those three names stubbed.

static_ip_test.py put MagicMock stand-ins for apscheduler, docker and
other packages into sys.modules unconditionally. A MagicMock has no
__path__, so once test_orchestrator_test.py (added upstream in google#1636)
was collected after it, importing apscheduler.schedulers.asyncio failed
with "'apscheduler.schedulers' is not a package" and the Unit job
errored at collection. The same import chain also needs docker.types,
so dropping only the apscheduler entries would not have been enough.

Only stub modules that importlib.util.find_spec cannot find, and trim
the list to the five names this file's import chain actually reaches.
In CI every requirement is installed, so nothing is stubbed and later
test files import the real packages; on a dev box without weasyprint,
netifaces or pwd the file still imports.
@IMINABO1
IMINABO1 merged commit be45cad into main Sep 16, 2026
17 checks passed
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.

1 participant