fix(tests): centralize sonic_platform mock in conftest.py#4601
Merged
vaibhavhd merged 1 commit intoJun 12, 2026
Merged
Conversation
Collaborator
|
/azp run |
|
Azure Pipelines successfully started running 1 pipeline(s). |
sonic_platform is a hardware-specific package unavailable in the build/test environment. Tests that use @patch('sonic_platform...') need it pre-injected into sys.modules or the decorator raises ModuleNotFoundError. Previously every test file had to know about this and inject the mock individually — an undocumented pattern scattered across 8+ files. When contributors missed it (most recently sed_test.py in sonic-net#4185), the sonic-buildimage submodule update PR failed across ALL platforms: sonic-net/sonic-buildimage#27773 (aspeed, broadcom, marvell, alpinevs, vpp, vs — 8 tests FAILED) This was already fixed reactively at least twice: - sonic-net#4583 (sed_test.py — same ModuleNotFoundError) - sonic-net#4530 (sfp/pcie tests — mock leakage under xdist) - sonic-net#4366 (ssdutil_test.py — module cache pollution) Add a global autouse fixture _ensure_sonic_platform_mock() in tests/conftest.py that re-injects the MagicMock stubs after _reset_between_files() clears them. This covers all tests that use @patch('sonic_platform...') at decoration time. Test files that import sonic_platform at module level (fwutil_test, psuutil_test, sfputil_test) retain their own sys.modules injection before the import line, since module collection happens before fixtures run. Remove the now-redundant per-file injections and fixtures from: sed_test.py, decode_syseeprom_test.py, chassis_modules_test.py, psushow_test.py, ssdutil_test.py. Signed-off-by: rookie-who <rookie-who@users.noreply.github.com>
67fc358 to
37e2eee
Compare
Collaborator
|
/azp run |
|
Azure Pipelines successfully started running 1 pipeline(s). |
vaibhavhd
approved these changes
Jun 12, 2026
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.
Why I did it
sonic_platformis a hardware-specific package unavailable in the build/test environment. Tests that use@patch('sonic_platform...')need it pre-injected intosys.modulesor the decorator raisesModuleNotFoundError.Previously every test file had to know about this undocumented pattern and inject the mock individually. When contributors missed it, the sonic-buildimage submodule update PR failed across all platforms. This has happened multiple times:
sed_test.py—ModuleNotFoundError: No module named 'sonic_platform'sys.modulesinjectionsfp_test.py,pcieutil_test.py— flaky failures under xdistssdutil_test.py—TypeErrorfrom cached real moduleMost recently, the missing mock in
sed_test.pybroke the 202605 sonic-buildimage submodule update (sonic-net/sonic-buildimage#27773) across aspeed, broadcom, marvell, alpinevs, vpp, and vs — all 8 SED tests FAILED withModuleNotFoundError.How I did it
Added a global
autousefixture_ensure_sonic_platform_mock()intests/conftest.pythat re-injectsMagicMock()stubs intosys.modulesforsonic_platformandsonic_platform.platformbefore every test function. This runs after_reset_between_files()clears the mocks between test files (to prevent cross-file leakage under xdist), ensuring every test can resolve@patch('sonic_platform...')without any per-file boilerplate.Removed the now-redundant per-file
sys.modulesinjections and_inject_sonic_platformfixtures from 8 test files:sed_test.pydecode_syseeprom_test.pychassis_modules_test.pyfwutil_test.pypsushow_test.pypsuutil_test.pysfputil_test.pyssdutil_test.pyHow to verify it
All tests that use
@patch('sonic_platform...')should pass without needing per-filesys.modulesinjection. New test files added in the future will automatically havesonic_platformavailable — no tribal knowledge required.