Skip to content

[python tests] Fix ssdutil_test mock module importing.#4366

Open
judsonwilson-nvidia wants to merge 1 commit intosonic-net:masterfrom
judsonwilson-nvidia:judsonw-fix-ssdutil-test-modules
Open

[python tests] Fix ssdutil_test mock module importing.#4366
judsonwilson-nvidia wants to merge 1 commit intosonic-net:masterfrom
judsonwilson-nvidia:judsonw-fix-ssdutil-test-modules

Conversation

@judsonwilson-nvidia
Copy link
Copy Markdown

@judsonwilson-nvidia judsonwilson-nvidia commented Mar 17, 2026

What I did

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

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.

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()

@linux-foundation-easycla
Copy link
Copy Markdown

linux-foundation-easycla bot commented Mar 17, 2026

CLA Signed

The committers listed above are authorized under a signed CLA.

  • ✅ login: judsonwilson-nvidia / name: Judson Wilson (0bab814)

@mssonicbld
Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

@judsonwilson-nvidia judsonwilson-nvidia force-pushed the judsonw-fix-ssdutil-test-modules branch from a57dd7d to fc0b737 Compare March 17, 2026 07:05
@mssonicbld
Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines
Copy link
Copy Markdown

Azure Pipelines successfully started running 1 pipeline(s).

@judsonwilson-nvidia judsonwilson-nvidia force-pushed the judsonw-fix-ssdutil-test-modules branch from fc0b737 to eb43918 Compare March 17, 2026 07:31
@mssonicbld
Copy link
Copy Markdown
Collaborator

/azp run

@azure-pipelines
Copy link
Copy Markdown

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>
@judsonwilson-nvidia judsonwilson-nvidia force-pushed the judsonw-fix-ssdutil-test-modules branch from eb43918 to 0bab814 Compare March 17, 2026 07:53
@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