summaryrefslogtreecommitdiff
diff options
authorJeremy Szu <jeremy.szu@canonical.com>2021-01-05 11:44:34 +0800
committerJeremy Szu <jeremy.szu@canonical.com>2021-01-05 11:44:34 +0800
commit350ca9e240d4b4c767bb71c1b9f9a4b22b309f2a (patch)
tree260ca9ed03e8b506fa3ed1b6e9a3d006246dccff
parentc1e651c906af5d6c64f58ec745aac1e711a05201 (diff)
Rebase the new test coverage from Sylvain
* Rebase the new test coverage from Sylvain as https://code.launchpad.net/~os369510/plainbox-provider-checkbox/+git/plainbox-provider-checkbox/+merge/393535/comments/1041502 but keep to use the soft-link from sysfs.
-rw-r--r--[-rwxr-xr-x]bin/fan_reaction_test.py16
-rw-r--r--tests/test_fan_reaction.py53
2 files changed, 46 insertions, 23 deletions
diff --git a/bin/fan_reaction_test.py b/bin/fan_reaction_test.py
index 6441457..3b2257e 100755..100644
--- a/bin/fan_reaction_test.py
+++ b/bin/fan_reaction_test.py
@@ -30,10 +30,11 @@ class FanMonitor:
"""Device that reports fan RPM or something correlating to that."""
def __init__(self):
"""Use heuristics to find something that we can read."""
- hwmons = []
+ self.hwmons = []
self._fan_paths = glob.glob('/sys/class/hwmon/hwmon*/fan*_input')
- # All entries (except name) under /sys/class/hwmon/hwmon/* are optional,
- # and should only be created in a given driver if the chip has the feature.
+ # All entries (except name) under /sys/class/hwmon/hwmon/* are optional
+ # and should only be created in a given driver if the chip has
+ # the feature.
# Use fan*_input is because the "thinkpad_hwmon" driver is report
# fan_input only. If there is any driver has different implementation
# then may need to check other entries in the future.
@@ -46,21 +47,22 @@ class FanMonitor:
try:
with open(pci_class_path, 'r') as _file:
pci_class = _file.read().splitlines()
- pci_device_class = (int(pci_class[0], base=16) >> 16) & 0xff
+ pci_device_class = (
+ int(pci_class[0], base=16) >> 16) & 0xff
"""Make sure the fan is not on graphic card"""
if pci_device_class == 3:
continue
except OSError:
print('Not able to access {}'.format(pci_class_path))
continue
- hwmons.append(i)
- if not hwmons:
+ self.hwmons.append(i)
+ if not self.hwmons:
print('Fan monitoring interface not found in SysFS')
raise SystemExit(0)
def get_rpm(self):
result = {}
- for p in self._fan_paths:
+ for p in self.hwmons:
try:
with open(p, 'rt') as f:
fan_mon_name = os.path.relpath(p, '/sys/class/hwmon')
diff --git a/tests/test_fan_reaction.py b/tests/test_fan_reaction.py
index f390dfc..5701721 100644
--- a/tests/test_fan_reaction.py
+++ b/tests/test_fan_reaction.py
@@ -15,12 +15,10 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
-import glob
import os
import unittest
from unittest import mock
-from unittest.mock import patch
-import sys
+from unittest.mock import patch, mock_open
import tempfile
from fan_reaction_test import FanMonitor
@@ -60,10 +58,6 @@ class FanMonitorTests(unittest.TestCase):
fan_mon.get_rpm(),
{'hwmon4/fan1_input': 150, 'hwmon6/fan2_input': 1318})
- def fake_get_pci_addr(fakedir, addr):
- pci_class_path = fakedir + '/sys/bus/pci/devices/%s/class' % addr
- return pci_class_path
-
@mock.patch('glob.glob')
@mock.patch('os.path.realpath')
def test_discard_gpu_fan(self, realpath_mock, glob_mock):
@@ -74,15 +68,42 @@ class FanMonitorTests(unittest.TestCase):
amdgpu_fan_input_file = os.path.join(amdgpu_hwmon, 'fan1_input')
with open(amdgpu_fan_input_file, 'w') as f:
f.write('65536')
- amdgpu_pci_class = os.path.join(amdgpu_pci, 'class')
- with open(amdgpu_pci_class, 'w') as f:
- f.write('0x030000')
glob_mock.return_value = [amdgpu_fan_input_file]
realpath_mock.return_value = \
- "/sys/devices/pci0000:00/0000:00:01.0/0000:01:00.0/" + \
- "0000:02:00.0/0000:03:00.0"
- with self.assertRaises(SystemExit) as cm:
- fan_mon = FanMonitor()
+ ("/sys/devices/pci0000:00/0000:00:01.0/0000:01:00.0/"
+ "0000:02:00.0/0000:03:00.0")
+ # The following call is patching open(pci_class_path, 'r')
+ with patch("builtins.open", mock_open(read_data='0x030000')) as f:
+ with self.assertRaises(SystemExit) as cm:
+ FanMonitor()
+ the_exception = cm.exception
+ self.assertEqual(the_exception.code, 0)
- the_exception = cm.exception
- self.assertEqual(the_exception.code, 0)
+ @mock.patch('glob.glob')
+ @mock.patch('os.path.realpath')
+ @mock.patch.object(os.path, 'relpath', autospec=True)
+ def test_discard_gpu_fan_keep_cpu_fan(
+ self, relpath_mock, realpath_mock, glob_mock
+ ):
+ with tempfile.TemporaryDirectory() as fake_sysfs:
+ amdgpu_hwmon = os.path.join(fake_sysfs, 'amdgpu-1002-7340')
+ amdgpu_pci = os.path.join(amdgpu_hwmon, 'device')
+ os.makedirs(amdgpu_pci)
+ amdgpu_fan_input_file = os.path.join(amdgpu_hwmon, 'fan1_input')
+ with open(amdgpu_fan_input_file, 'w') as f:
+ f.write('65536')
+ fan_input_file2 = os.path.join(fake_sysfs, 'fan2_input')
+ with open(fan_input_file2, 'w') as f2:
+ f2.write('412')
+ glob_mock.return_value = [amdgpu_fan_input_file, fan_input_file2]
+ realpath_mock.side_effect = [
+ "/sys/devices/pci0000:00/0000:00:01.0/0000:01:00.0/"
+ "0000:02:00.0/0000:03:00.0", "foo"]
+ relpath_mock.side_effect = ['hwmon6/fan2_input']
+ # The following call is patching open(pci_class_path, 'r')
+ with patch("builtins.open", mock_open(read_data='0x030000')) as f:
+ fan_mon = FanMonitor()
+ self.assertEqual(len(fan_mon.hwmons), 1)
+ self.assertTrue(fan_mon.hwmons[0].endswith('fan2_input'))
+ self.assertEqual(
+ fan_mon.get_rpm(), {'hwmon6/fan2_input': 412})