from unittest.mock import patch
from torch_npu.profiler.analysis.prof_common_func._constant import Constant
from torch_npu.profiler.analysis.prof_view.cann_parse._cann_export import (
CANNExportParser,
)
from torch_npu.testing.testcase import run_tests, TestCase
class TestCannExport(TestCase):
def test_cann_export_parser_run_db_export_success(self):
with (
patch("os.path.isdir", return_value=True),
patch("subprocess.run") as mock_run,
patch(
"torch_npu.profiler.analysis.prof_common_func._path_manager.ProfilerPathManager.get_cann_path",
return_value="/fake/cann/path",
),
patch(
"torch_npu.profiler.analysis.prof_view.cann_parse._cann_export.shutil.which",
return_value="/usr/bin/msprof",
),
patch(
"torch_npu.profiler.analysis.prof_view.cann_parse._cann_export.ProfilerConfig"
),
patch(
"torch_npu.profiler.analysis.prof_view.cann_parse._cann_export.ProfilerLogger"
),
patch(
"torch_npu.profiler.analysis.prof_view.cann_parse._cann_export.CANNExportParser._check_msprof_environment"
),
patch(
"torch_npu.profiler.analysis.prof_view.cann_parse._cann_export.CANNExportParser._check_prof_data_size"
),
):
mock_run.return_value.returncode = 0
parser = CANNExportParser(
"test", {"profiler_path": "/fake/path", Constant.EXPORT_TYPE: ["db"]}
)
parser.msprof_path = "/usr/bin/msprof"
result = parser.run({})
self.assertEqual(result, (Constant.SUCCESS, None))
def test_cann_export_parser_init(self):
with (
patch(
"torch_npu.profiler.analysis.prof_common_func._path_manager.os.listdir"
) as mock_listdir,
patch(
"torch_npu.profiler.analysis.prof_common_func._path_manager.ProfilerPathManager.get_cann_path",
return_value="/fake/cann/path",
),
patch(
"torch_npu.profiler.analysis.prof_view.cann_parse._cann_export.shutil.which",
return_value="/usr/bin/msprof",
),
):
mock_listdir.return_value = ["cann1", "cann2"]
parser = CANNExportParser(
"test", {"profiler_path": "/fake/path", Constant.EXPORT_TYPE: ["db"]}
)
self.assertEqual(parser._profiler_path, "/fake/path")
self.assertEqual(parser._export_type, ["db"])
self.assertIsNotNone(parser._cann_path)
self.assertIsNotNone(parser.msprof_path)
def test_cann_export_parser_msprof_path_none(self):
with (
patch("os.path.isdir", return_value=True),
patch("subprocess.run") as mock_run,
patch(
"torch_npu.profiler.analysis.prof_common_func._path_manager.ProfilerPathManager.get_cann_path",
return_value="/fake/cann/path",
),
patch(
"torch_npu.profiler.analysis.prof_view.cann_parse._cann_export.shutil.which",
return_value="/usr/bin/msprof",
),
patch(
"torch_npu.profiler.analysis.prof_view.cann_parse._cann_export.ProfilerConfig"
) as mock_config,
patch(
"torch_npu.profiler.analysis.prof_view.cann_parse._cann_export.ProfilerLogger"
) as mock_logger,
):
mock_run.return_value.returncode = 0
parser = CANNExportParser(
"test", {"profiler_path": "/fake/path", Constant.EXPORT_TYPE: ["db"]}
)
parser.msprof_path = None
result = parser.run({})
self.assertEqual(result, (Constant.FAIL, None))
if __name__ == "__main__":
run_tests()