[python tests] Fix ssdutil_test mock module importing.#4366
Open
judsonwilson-nvidia wants to merge 1 commit intosonic-net:masterfrom
Open
[python tests] Fix ssdutil_test mock module importing.#4366judsonwilson-nvidia wants to merge 1 commit intosonic-net:masterfrom
judsonwilson-nvidia wants to merge 1 commit intosonic-net:masterfrom
Conversation
|
|
Collaborator
|
/azp run |
|
Azure Pipelines successfully started running 1 pipeline(s). |
a57dd7d to
fc0b737
Compare
Collaborator
|
/azp run |
|
Azure Pipelines successfully started running 1 pipeline(s). |
fc0b737 to
eb43918
Compare
Collaborator
|
/azp run |
|
Azure Pipelines successfully started running 1 pipeline(s). |
Fixed failures in `tests/ssdutil_test.py` which fail under certain conditions
where `psutil` has already been imported by the python runtime, e.g. by a
previous test. When that occurs, it appears that the mock replacement modules
fail to import, and the cached module is used instead.
This change uses monkeypatch to remove the existing modules from sys.modules,
and otherwise configure the environment to import the mock/fake modules when
the next import occurs. The mocked imports and path will be restored after
importing ssdutil, and ssdutil will be restored after the test is over.
The errors that this fixes are:
```
FAILED tests/ssdutil_test.py::TestSsdutil::test_get_default_disk - TypeError: an integer is required
FAILED tests/ssdutil_test.py::TestSsdutil::test_sonic_storage_path - TypeError: an integer is required
```
Both look similar to:
```
______________________ TestSsdutil.test_get_default_disk _______________________
self = <tests.ssdutil_test.TestSsdutil object at 0xffff7d20c2d0>
@patch('os.geteuid', MagicMock(return_value=0))
@patch('os.stat', MagicMock(st_rdev=2049))
@patch('os.major', MagicMock(return_value=8))
def test_get_default_disk(self):
> (default_device, disk_type) = ssdutil.get_default_disk()
tests/ssdutil_test.py:52:
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
ssdutil/main.py:32: in get_default_disk
partitions = psutil.disk_partitions()
/usr/local/lib/python3.13/dist-packages/psutil/__init__.py:2076: in disk_partitions
return _psplatform.disk_partitions(all)
/usr/local/lib/python3.13/dist-packages/psutil/_pslinux.py:1264: in disk_partitions
if procfs_path == "/proc" and os.path.isfile('/etc/mtab'):
_ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _ _
path = '/etc/mtab'
> ???
E TypeError: an integer is required
<frozen genericpath>:42: TypeError
```
Monkeypatched away existing imported modules from the module cache during the
test cases.
Run the test with this code inserted at the top. It will fail with the errors
mentioned above. Applying the changes in this PR allows the test to succeed.
```
def foo():
# Import it, but don't bring it into module scope.
# Next import uses cached version rather than loading
# the mocked one from the path location.
import psutil
foo()
```
Signed-off-by: Judson Wilson <judsonw@nvidia.com>
eb43918 to
0bab814
Compare
Collaborator
|
/azp run |
|
Azure Pipelines successfully started running 1 pipeline(s). |
oleksandrivantsiv
approved these changes
Mar 17, 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.
What I did
Fixed failures in
tests/ssdutil_test.pywhich fail under certain conditions wherepsutilhas already been imported by the python runtime, e.g. by a previous test. When that occurs, it appears that the mock replacement modules fail to import, and the cached module is used instead.This change uses monkeypatch to remove the existing modules from sys.modules, and otherwise configure the environment to import the mock/fake modules when the next import occurs. The mocked imports and path will be restored after importing ssdutil, and ssdutil will be restored after the test is over.
The errors that this fixes are:
Both look similar to:
How I did it
Monkeypatched away existing imported modules from the module cache during the test cases.
How to verify it
Run the test with this code inserted at the top. It will fail with the errors mentioned above. Applying the changes in this PR allows the test to succeed.