Debian Codegraph @

tensilelite/Tensile/Tests/unit/Common/test_Utilities.py

raw symbols SCIP
1 ################################################################################
2 #
3 # Copyright (C) 2025 Advanced Micro Devices, Inc. All rights reserved.
4 #
5 # Permission is hereby granted, free of charge, to any person obtaining a copy
6 # of this software and associated documentation files (the "Software"), to deal
7 # in the Software without restriction, including without limitation the rights
8 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9 # copies of the Software, and to permit persons to whom the Software is
10 # furnished to do so, subject to the following conditions:
11 #
12 # The above copyright notice and this permission notice shall be included in
13 # all copies or substantial portions of the Software.
14 #
15 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21 # SOFTWARE.
22 #
23 ################################################################################
24
25 import pytest
26 from unittest.mock import mock_open, patch
27 from pathlib import Path
28 from Tensile.Common.Utilities import isRhel8
29
30 @pytest.fixture
31 def mock_openFile():
32 with patch("builtins.open", mock_open()) as mock:
33 yield mock
34
35 @pytest.fixture
36 def mock_exists():
37 with patch.object(Path, "exists", return_value=True) as mock:
38 yield mock
39
40 @pytest.fixture
41 def mock_notExists():
42 with patch.object(Path, "exists", return_value=False) as mock:
43 yield mock
44
45 # Test cases for isRhel8
46 def test_isRhel8_true(mock_exists, mock_openFile):
47 mock_openFile.return_value.read.return_value = 'NAME="Red Hat Enterprise Linux" VERSION_ID="8.4"'
48 assert isRhel8() is True
49 mock_exists.assert_called_once_with()
50 mock_openFile.assert_called_once_with(Path("/etc/os-release"), "r")
51
52 def test_isRhel8_false_non_rhel(mock_exists, mock_openFile):
53 mock_openFile.return_value.read.return_value = 'NAME="Ubuntu" VERSION_ID="20.04"'
54 assert isRhel8() is False
55 mock_exists.assert_called_once_with()
56 mock_openFile.assert_called_once_with(Path("/etc/os-release"), "r")
57
58 def test_isRhel8_false_new_version(mock_exists, mock_openFile):
59 mock_openFile.return_value.read.return_value = 'NAME="Red Hat Enterprise Linux" VERSION_ID="9.0"'
60 assert isRhel8() is False
61 mock_exists.assert_called_once_with()
62 mock_openFile.assert_called_once_with(Path("/etc/os-release"), "r")
63
64 def test_isRhel8_true_with_warning(mock_exists, mock_openFile):
65 mock_openFile.return_value.read.return_value = 'NAME="Red Hat Enterprise Linux" VERSION_ID="8.5"'
66 assert isRhel8() is True
67 mock_exists.assert_called_once_with()
68 mock_openFile.assert_called_once_with(Path("/etc/os-release"), "r")
69
70 def test_isRhel8_file_not_found(mock_notExists, mock_openFile):
71 assert isRhel8() is False
72 mock_notExists.assert_called_once_with()
73 mock_openFile.assert_not_called() # No file open attempt if the file doesn't exist