|
| 1 | +# Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +# or more contributor license agreements. See the NOTICE file |
| 3 | +# distributed with this work for additional information |
| 4 | +# regarding copyright ownership. The ASF licenses this file |
| 5 | +# to you under the Apache License, Version 2.0 (the |
| 6 | +# "License"); you may not use this file except in compliance |
| 7 | +# with the License. You may obtain a copy of the License at |
| 8 | +# |
| 9 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | +# |
| 11 | +# Unless required by applicable law or agreed to in writing, |
| 12 | +# software distributed under the License is distributed on an |
| 13 | +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +# KIND, either express or implied. See the License for the |
| 15 | +# specific language governing permissions and limitations |
| 16 | +# under the License. |
| 17 | +"""Tests for MACA architecture detection""" |
| 18 | +import os |
| 19 | +import subprocess |
| 20 | +import unittest |
| 21 | +from unittest.mock import patch |
| 22 | + |
| 23 | +from tvm.contrib import mxcc |
| 24 | + |
| 25 | + |
| 26 | +class TestMacaArchDetection(unittest.TestCase): |
| 27 | + """Test cases for get_maca_arch function""" |
| 28 | + |
| 29 | + def test_env_maca_arch(self): |
| 30 | + """Test MACA_ARCH environment variable is used when set""" |
| 31 | + with patch.dict(os.environ, {"MACA_ARCH": "xcore2000"}): |
| 32 | + arch = mxcc.get_maca_arch() |
| 33 | + self.assertEqual(arch, "xcore2000") |
| 34 | + |
| 35 | + def test_env_maca_arch_overrides_maca_path(self): |
| 36 | + """Test MACA_ARCH takes priority even when maca_path is given""" |
| 37 | + with patch.dict(os.environ, {"MACA_ARCH": "xcore3000"}): |
| 38 | + arch = mxcc.get_maca_arch(maca_path="/nonexistent") |
| 39 | + self.assertEqual(arch, "xcore3000") |
| 40 | + |
| 41 | + def test_maca_not_installed_default(self): |
| 42 | + """Test default arch is returned when MACA is not installed""" |
| 43 | + with patch.dict(os.environ, {}, clear=True): |
| 44 | + with patch("os.path.exists", return_value=False): |
| 45 | + arch = mxcc.get_maca_arch(maca_path="/nonexistent") |
| 46 | + self.assertEqual(arch, "xcore1000") |
| 47 | + |
| 48 | + def test_maca_path_env_var(self): |
| 49 | + """Test MACA_PATH environment variable is used as fallback path""" |
| 50 | + with patch.dict(os.environ, {"MACA_PATH": "/custom/maca"}, clear=True): |
| 51 | + with patch("os.path.exists", return_value=True): |
| 52 | + with patch( |
| 53 | + "subprocess.check_output", |
| 54 | + return_value=b"Name: XCORE1800\n", |
| 55 | + ): |
| 56 | + arch = mxcc.get_maca_arch() |
| 57 | + self.assertEqual(arch, "xcore1800") |
| 58 | + |
| 59 | + def test_oserror_is_caught(self): |
| 60 | + """Test OSError (e.g. missing binary) is caught gracefully""" |
| 61 | + with patch.dict(os.environ, {}, clear=True): |
| 62 | + with patch("os.path.exists", return_value=True): |
| 63 | + with patch( |
| 64 | + "subprocess.check_output", |
| 65 | + side_effect=OSError("macainfo binary not found"), |
| 66 | + ): |
| 67 | + arch = mxcc.get_maca_arch(maca_path="/valid/path") |
| 68 | + self.assertEqual(arch, "xcore1000") |
| 69 | + |
| 70 | + def test_calledprocesserror_is_caught(self): |
| 71 | + """Test CalledProcessError is caught gracefully""" |
| 72 | + with patch.dict(os.environ, {}, clear=True): |
| 73 | + with patch("os.path.exists", return_value=True): |
| 74 | + with patch( |
| 75 | + "subprocess.check_output", |
| 76 | + side_effect=subprocess.CalledProcessError(1, "macainfo"), |
| 77 | + ): |
| 78 | + arch = mxcc.get_maca_arch(maca_path="/valid/path") |
| 79 | + self.assertEqual(arch, "xcore1000") |
| 80 | + |
| 81 | + def test_macainfo_parsing(self): |
| 82 | + """Test parsing of macainfo output""" |
| 83 | + with patch.dict(os.environ, {}, clear=True): |
| 84 | + with patch("os.path.exists", return_value=True): |
| 85 | + with patch( |
| 86 | + "subprocess.check_output", |
| 87 | + return_value=b"Name: XCORE2000\n", |
| 88 | + ): |
| 89 | + arch = mxcc.get_maca_arch(maca_path="/valid/path") |
| 90 | + self.assertEqual(arch, "xcore2000") |
| 91 | + |
| 92 | + def test_macainfo_parsing_no_match(self): |
| 93 | + """Test fallback to default when macainfo output has no matching name""" |
| 94 | + with patch.dict(os.environ, {}, clear=True): |
| 95 | + with patch("os.path.exists", return_value=True): |
| 96 | + with patch( |
| 97 | + "subprocess.check_output", |
| 98 | + return_value=b"Name: UNKNOWN_DEVICE\n", |
| 99 | + ): |
| 100 | + arch = mxcc.get_maca_arch(maca_path="/valid/path") |
| 101 | + self.assertEqual(arch, "xcore1000") |
| 102 | + |
| 103 | + |
| 104 | +if __name__ == "__main__": |
| 105 | + unittest.main() |
0 commit comments