Add sglang sglang check env topology oserror - #8
Conversation
There was a problem hiding this comment.
Code Review
This pull request improves environment checking in sglang/check_env.py by catching OSError (such as FileNotFoundError when a command is missing) in addition to subprocess.SubprocessError across various GPU topology checks. It also adds unit tests to verify that an empty topology dictionary is returned when the underlying command is not found. The reviewer suggested improving the unit tests by asserting that subprocess.run is called with the correct commands and arguments to ensure the tests are robust.
Important
The consumer version of Gemini Code Assist on GitHub is being sunset. Starting June 18, 2026, new organization installations will be blocked, and all code review activity will officially cease on July 17, 2026.
For more details on the timeline and next steps, please review the Help Documentation.
| import unittest | ||
| from unittest.mock import patch | ||
|
|
||
| from sglang.check_env import GPUEnv, HIPEnv, MUSAEnv, NPUEnv | ||
|
|
||
|
|
||
| class TestCheckEnv(unittest.TestCase): | ||
| @patch("sglang.check_env.subprocess.run", side_effect=FileNotFoundError) | ||
| def test_gpu_topology_missing_command_returns_empty_info(self, _): | ||
| self.assertEqual(GPUEnv().get_topology(), {}) | ||
|
|
||
| @patch("sglang.check_env.subprocess.run", side_effect=FileNotFoundError) | ||
| def test_hip_topology_missing_command_returns_empty_info(self, _): | ||
| self.assertEqual(HIPEnv().get_topology(), {}) | ||
|
|
||
| @patch("sglang.check_env.subprocess.run", side_effect=FileNotFoundError) | ||
| def test_npu_topology_missing_command_returns_empty_info(self, _): | ||
| self.assertEqual(NPUEnv().get_topology(), {}) | ||
|
|
||
| @patch("sglang.check_env.subprocess.run", side_effect=FileNotFoundError) | ||
| def test_musa_topology_missing_command_returns_empty_info(self, _): | ||
| self.assertEqual(MUSAEnv().get_topology(), {}) |
There was a problem hiding this comment.
The unit tests mock subprocess.run but do not assert that it was called with the correct command and arguments. Without these assertions, the tests could pass even if the underlying command is incorrect or if subprocess.run is not called at all. It is highly recommended to verify that the mock was called with the expected arguments.
import subprocess
import unittest
from unittest.mock import patch
from sglang.check_env import GPUEnv, HIPEnv, MUSAEnv, NPUEnv
class TestCheckEnv(unittest.TestCase):
@patch("sglang.check_env.subprocess.run", side_effect=FileNotFoundError)
def test_gpu_topology_missing_command_returns_empty_info(self, mock_run):
self.assertEqual(GPUEnv().get_topology(), {})
mock_run.assert_called_once_with(
["nvidia-smi", "topo", "-m"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
check=True,
)
@patch("sglang.check_env.subprocess.run", side_effect=FileNotFoundError)
def test_hip_topology_missing_command_returns_empty_info(self, mock_run):
self.assertEqual(HIPEnv().get_topology(), {})
mock_run.assert_called_once_with(
["rocm-smi", "--showtopotype"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
check=True,
)
@patch("sglang.check_env.subprocess.run", side_effect=FileNotFoundError)
def test_npu_topology_missing_command_returns_empty_info(self, mock_run):
self.assertEqual(NPUEnv().get_topology(), {})
mock_run.assert_called_once_with(
["npu-smi", "info", "-t", "topo"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
check=True,
)
@patch("sglang.check_env.subprocess.run", side_effect=FileNotFoundError)
def test_musa_topology_missing_command_returns_empty_info(self, mock_run):
self.assertEqual(MUSAEnv().get_topology(), {})
mock_run.assert_called_once_with(
["mthreads-gmi", "topo", "-m"],
stdout=subprocess.PIPE,
stderr=subprocess.PIPE,
text=True,
check=True,
)- Handle missing topology commands in check_env - Assert topology probe commands in tests
Summary
Validation
Review notes