diff --git a/tests/cpufreq/cpufreq.py b/tests/cpufreq/cpufreq.py old mode 100755 new mode 100644 index 832e943851f65aa873691c0a23e1b592a6f2ee5d..d950cfbc063398da213c0a40bf86228503c0fd03 --- a/tests/cpufreq/cpufreq.py +++ b/tests/cpufreq/cpufreq.py @@ -17,12 +17,11 @@ from time import sleep from hwcompatible.env import CertEnv from hwcompatible.test import Test -from hwcompatible.command import Command +from hwcompatible.command import Command, CertCommandError class CPU: def __init__(self): - self.requirements = ['util-linux', 'kernel-tools'] self.cpu = None self.nums = None self.list = None @@ -35,8 +34,9 @@ class CPU: def get_info(self): cmd = Command("lscpu") try: - nums = cmd.get_str('CPU\(s\):\s+(?P\d+)', 'cpus', False) - except: + nums = cmd.get_str(r'^CPU\S*:\s+(?P\d+)$', 'cpus', False) + except CertCommandError as e: + print(e) return False self.nums = int(nums) self.list = range(self.nums) @@ -44,14 +44,16 @@ class CPU: cmd = Command("cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq") try: max_freq = cmd.get_str() - except: + except CertCommandError as e: + print(e) return False self.max_freq = int(max_freq) cmd = Command("cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_min_freq") try: min_freq = cmd.get_str() - except: + except CertCommandError as e: + print(e) return False self.min_freq = int(min_freq) @@ -62,14 +64,16 @@ class CPU: try: cmd.run() return cmd.returncode - except: + except CertCommandError as e: + print(e) return False def get_freq(self, cpu): cmd = Command("cpupower -c %s frequency-info -w" % cpu) try: - return int(cmd.get_str('.* frequency: (?P\d+) .*', 'freq', False)) - except: + return int(cmd.get_str(r'.* frequency: (?P\d+) .*', 'freq', False)) + except CertCommandError as e: + print(e) return False def set_governor(self, governor, cpu='all'): @@ -77,14 +81,16 @@ class CPU: try: cmd.run() return cmd.returncode - except: + except CertCommandError as e: + print(e) return False def get_governor(self, cpu): cmd = Command("cpupower -c %s frequency-info -p" % cpu) try: - return cmd.get_str('.* governor "(?P\w+)".*', 'governor', False) - except: + return cmd.get_str(r'.* governor "(?P\w+)".*', 'governor', False) + except CertCommandError as e: + print(e) return False def find_path(self, parent_dir, target_name): @@ -92,7 +98,8 @@ class CPU: try: cmd.run() return cmd.returncode - except: + except CertCommandError as e: + print(e) return False @@ -119,6 +126,12 @@ class Load: class CPUFreqTest(Test): + def __init__(self): + Test.__init__(self) + self.requirements = ['util-linux', 'kernel-tools'] + self.cpu = CPU() + self.original_governor = self.cpu.get_governor(0) + def test_userspace(self): target_cpu = randint(0, self.cpu.nums-1) target_freq = randint(self.cpu.min_freq, self.cpu.max_freq) @@ -335,9 +348,6 @@ class CPUFreqTest(Test): return True def test(self): - self.cpu = CPU() - self.original_governor = self.cpu.get_governor(0) - if not self.cpu.get_info(): print("[X] Fail to get CPU info." \ " Please check if the CPU supports cpufreq.")