Repository navigation
Stop static_ip_test stubs leaking into later test collection - #2
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
The scheduled Unit job on
mainhas failed since the 2026-09-14 upstream merge, at pytest collection:APScheduler==3.10.4is installed correctly. The cause istesting/unit/framework/static_ip_test.py, which unconditionally placedMagicMockobjects insys.modulesforapscheduler,apscheduler.schedulers,dockerand others. pytest collects the directory alphabetically, and the upstream-addedtest_orchestrator_test.py(google#1636) is collected right afterstatic_ip_test.py. AMagicMockhas no__path__, so importing the realapscheduler.schedulers.asynciosubmodule through it fails. The same chain also importsdocker.typesanddocker.models.containers, so removing only the apscheduler entries would have hit the identical error next.Fix
Stub a module only when
importlib.util.find_speccannot 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 andsys.modulesis left exactly as upstream's tests expect. On a dev box missing the Linux-only or heavy packages, the file still imports.Verification
framework/requirements.txton Ubuntu (WSL): unfixed tree reproduces the CI error exactly; fixed tree runspytest testing/unit/frameworkwith 64 passed.pytest static_ip_test.py test_orchestrator_test.py: 20 passed.pylint==3.2.6 --rcfile=.pylintrcon the file: 10.00/10.static_ip_test.pyalone, 13 passed, with only those three names stubbed.