Skip to content

[202605] fix(tests): centralize sonic_platform mock in conftest.py#4610

Open
rookie-who wants to merge 1 commit into
sonic-net:202605from
rookie-who:fix/conftest-sonic-platform-mock-202605
Open

[202605] fix(tests): centralize sonic_platform mock in conftest.py#4610
rookie-who wants to merge 1 commit into
sonic-net:202605from
rookie-who:fix/conftest-sonic-platform-mock-202605

Conversation

@rookie-who

Copy link
Copy Markdown
Contributor

Why I did it

Cherry-pick of #4601 to 202605.

tests/sed_test.py uses @patch('sonic_platform.platform.Platform') but sonic_platform is a platform-specific package not available in the build environment. This causes ModuleNotFoundError and fails the sonic-utilities wheel build, blocking the sonic-utilities submodule update (#27773 in sonic-buildimage).

How I did it

Add a session-scoped sonic_platform mock to conftest.py that installs a fake sonic_platform.platform.Platform module before any test imports it. Remove per-file mock setups that are now redundant.

Cherry-pick of 37e2eee from master.

How to verify it

pytest tests/sed_test.py passes without sonic_platform installed.

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>
@mssonicbld

Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines

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

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