Skip to content

fix(tests): centralize sonic_platform mock in conftest.py#4601

Merged
vaibhavhd merged 1 commit into
sonic-net:masterfrom
rookie-who:fix/conftest-global-sonic-platform-mock
Jun 12, 2026
Merged

fix(tests): centralize sonic_platform mock in conftest.py#4601
vaibhavhd merged 1 commit into
sonic-net:masterfrom
rookie-who:fix/conftest-global-sonic-platform-mock

Conversation

@rookie-who

Copy link
Copy Markdown
Contributor

Why I did it

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 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:

PR What broke Root cause
#4583 sed_test.pyModuleNotFoundError: No module named 'sonic_platform' #4185 added SED tests without sys.modules injection
#4530 sfp_test.py, pcieutil_test.py — flaky failures under xdist Mock leakage after #4516 parallelized tests
#4366 ssdutil_test.pyTypeError from cached real module Module cache pollution across test files

Most recently, the missing mock in sed_test.py broke the 202605 sonic-buildimage submodule update (sonic-net/sonic-buildimage#27773) across aspeed, broadcom, marvell, alpinevs, vpp, and vs — all 8 SED tests FAILED with ModuleNotFoundError.

How I did it

Added a global autouse fixture _ensure_sonic_platform_mock() in tests/conftest.py that re-injects MagicMock() stubs into sys.modules for sonic_platform and sonic_platform.platform before 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.modules injections and _inject_sonic_platform fixtures from 8 test files:

  • sed_test.py
  • decode_syseeprom_test.py
  • chassis_modules_test.py
  • fwutil_test.py
  • psushow_test.py
  • psuutil_test.py
  • sfputil_test.py
  • ssdutil_test.py

How to verify it

All tests that use @patch('sonic_platform...') should pass without needing per-file sys.modules injection. New test files added in the future will automatically have sonic_platform available — no tribal knowledge required.

@mssonicbld

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
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>
@rookie-who rookie-who force-pushed the fix/conftest-global-sonic-platform-mock branch from 67fc358 to 37e2eee Compare June 11, 2026 01:22
@mssonicbld

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

Copy link
Copy Markdown
Azure Pipelines successfully started running 1 pipeline(s).

@vaibhavhd vaibhavhd merged commit 072fef3 into sonic-net:master Jun 12, 2026
9 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.

3 participants