import unittest
from unittest.mock import patch, MagicMock
import sys
from msserviceprofiler.modelevalstate.patch.patch_vllm import PatchVllm
class TestPatchVllm(unittest.TestCase):
def tearDown(self):
if 'vllm_ascend' in sys.modules:
del sys.modules['vllm_ascend']
@patch("msserviceprofiler.modelevalstate.patch.patch_vllm.add_patch")
@patch("msserviceprofiler.modelevalstate.patch.patch_vllm.check_flag")
@patch("msserviceprofiler.modelevalstate.patch.patch_vllm.logger")
@patch("pathlib.Path.exists")
def test_patch_not_applied(self, mock_exists, mock_logger, mock_check_flag, mock_add_patch):
"""测试需要打补丁的场景"""
mock_exists.return_value = True
with patch.dict('sys.modules', {'vllm_ascend': MagicMock(__path__=["/vllm_ascend/path"])}):
mock_check_flag.return_value = True
PatchVllm.patch()
@patch("msserviceprofiler.modelevalstate.patch.patch_vllm.add_patch")
@patch("msserviceprofiler.modelevalstate.patch.patch_vllm.check_flag")
@patch("msserviceprofiler.modelevalstate.patch.patch_vllm.logger")
@patch("pathlib.Path.exists")
def test_patch_already_applied(self, mock_exists, mock_logger, mock_check_flag, mock_add_patch):
"""测试补丁已存在的场景"""
mock_exists.return_value = True
with patch.dict('sys.modules', {'vllm_ascend': MagicMock(__path__=["/vllm_ascend/path"])}):
mock_check_flag.return_value = False
PatchVllm.patch()
mock_add_patch.assert_not_called()
@patch("msserviceprofiler.modelevalstate.patch.patch_vllm.add_patch")
@patch("msserviceprofiler.modelevalstate.patch.patch_vllm.check_flag")
@patch("pathlib.Path.exists")
def test_vllm_path_handling(self, mock_exists, mock_check_flag, mock_add_patch):
"""测试不同vllm路径的处理"""
mock_exists.return_value = True
with patch.dict('sys.modules', {'vllm_ascend': MagicMock(__path__=["/custom/vllm/ascend"])}):
mock_check_flag.return_value = True
PatchVllm.patch()
def test_check_version(self):
"""测试check_version方法始终返回True"""
self.assertTrue(PatchVllm.check_version("any_version"))