|
| 1 | +from function import run_command |
| 2 | +from test_case import TestCaseCommon |
| 3 | +from errcode import E |
| 4 | +import traceback |
| 5 | + |
| 6 | + |
| 7 | +# CPU test class |
| 8 | +class CPUTC(TestCaseCommon): |
| 9 | + def __init__(self, index, logger, platform_cfg_file, case_cfg_file=None): |
| 10 | + MODULE_NAME = "cpu_tc" |
| 11 | + TestCaseCommon.__init__(self, index, MODULE_NAME, logger, platform_cfg_file, case_cfg_file) |
| 12 | + self.cpu_info_dict = None |
| 13 | + try: |
| 14 | + if self.platform_cfg_json and 'cpu_info' in self.platform_cfg_json.keys(): |
| 15 | + self.cpu_info_dict = self.platform_cfg_json['cpu_info'] |
| 16 | + except Exception as e: |
| 17 | + self.logger.log_err(str(e), True) |
| 18 | + self.logger.log_err(traceback.format_exc()) |
| 19 | + |
| 20 | + def test_cpu_info(self, also_print_console=False): |
| 21 | + ret = E.OK |
| 22 | + self.logger.log_info("check_cpu_info start", also_print_console) |
| 23 | + |
| 24 | + cmd = "lscpu | head -n25" |
| 25 | + status, log = run_command(cmd) |
| 26 | + if status != 0 or len(log) <= 0: |
| 27 | + reason = "Failed, get cpu info failed, command {}, status {}, log {}".format( \ |
| 28 | + cmd, status, log) |
| 29 | + self.log_reason(reason) |
| 30 | + ret = E.ECPU3005 |
| 31 | + else: |
| 32 | + lines = log.splitlines() |
| 33 | + expected_cpu_model = self.cpu_info_dict.get('Model name') |
| 34 | + expected_bogomips = self.cpu_info_dict.get('BogoMIPS') |
| 35 | + expected_cpu_num = self.cpu_info_dict.get('CPU(s)') |
| 36 | + expected_cpu_mhz = self.cpu_info_dict.get('CPU MHz') |
| 37 | + self.logger.log_dbg("Expected value: {}, {}, {}, {}".format(expected_cpu_model, \ |
| 38 | + expected_bogomips, expected_cpu_num, expected_cpu_mhz)) |
| 39 | + for line in lines: |
| 40 | + cols = line.strip().split(":") |
| 41 | + if len(cols) < 2: |
| 42 | + continue |
| 43 | + |
| 44 | + if expected_cpu_model and cols[0] == "Model name": |
| 45 | + if cols[1].strip() != expected_cpu_model: |
| 46 | + reason = "Failed, CPU model name {}(expected {})".format( \ |
| 47 | + cols[1].strip(), expected_cpu_model) |
| 48 | + self.log_reason(reason) |
| 49 | + ret = E.ECPU3001 |
| 50 | + else: |
| 51 | + msg = "Model name {} =======> OK".format(cols[1].strip()) |
| 52 | + self.logger.log_info(msg) |
| 53 | + |
| 54 | + if expected_bogomips and cols[0] == 'BogoMIPS': |
| 55 | + read_bogomips = float(cols[1].strip()) |
| 56 | + conf_bogomips = float(expected_bogomips) |
| 57 | + if read_bogomips <= (conf_bogomips * 0.99) or \ |
| 58 | + read_bogomips >= conf_bogomips * 1.01: |
| 59 | + reason = "Failed, BogoMIPS {}(expected {})".format( \ |
| 60 | + read_bogomips, expected_bogomips) |
| 61 | + self.log_reason(reason) |
| 62 | + ret = E.ECPU3001 |
| 63 | + else: |
| 64 | + msg = "BogoMIPS {} ===== OK".format(read_bogomips) |
| 65 | + self.logger.log_info(msg) |
| 66 | + |
| 67 | + if expected_cpu_num and cols[0] == 'CPU(s)': |
| 68 | + num_cpus = int(cols[1].strip()) |
| 69 | + if num_cpus != self.cpu_info_dict.get('CPU(s)'): |
| 70 | + reason = "Failed, CPU number {}(expected {})".format( \ |
| 71 | + num_cpus, expected_cpu_num) |
| 72 | + self.fail_reason.append(reason) |
| 73 | + ret = E.ECPU3001 |
| 74 | + else: |
| 75 | + msg = "Number of CPUs {} ===== OK".format(num_cpus) |
| 76 | + self.logger.log_info(msg) |
| 77 | + |
| 78 | + if expected_cpu_mhz and cols[0] == 'CPU MHz': |
| 79 | + read_cpu_mhz = float(cols[1].strip()) |
| 80 | + conf_cpu_mhz = float(expected_cpu_mhz) |
| 81 | + if read_cpu_mhz <= (conf_cpu_mhz * 0.99) or \ |
| 82 | + read_cpu_mhz >= (conf_cpu_mhz * 1.01): |
| 83 | + reason = "Failed, CPU MHz {}(expected {})".format( \ |
| 84 | + read_cpu_mhz, expected_cpu_mhz) |
| 85 | + self.log_reason(reason) |
| 86 | + ret = E.ECPU3001 |
| 87 | + else: |
| 88 | + msg = "CPU frequency {} ===== OK".format(read_cpu_mhz) |
| 89 | + self.logger.log_info(msg) |
| 90 | + |
| 91 | + if ret != E.OK: |
| 92 | + self.logger.log_err("test cpu info done, FAILED.", also_print_console) |
| 93 | + else: |
| 94 | + self.logger.log_info("test cpu info done, PASS.", also_print_console) |
| 95 | + |
| 96 | + return ret |
| 97 | + |
| 98 | + def run_test(self, *argv): |
| 99 | + try: |
| 100 | + ret = self.test_cpu_info(True) |
| 101 | + return ret |
| 102 | + except Exception as e: |
| 103 | + self.logger.log_err("test cpu info got exception: {}".format(str(e))) |
| 104 | + self.logger.log_err(traceback.format_exc()) |
| 105 | + return ret |
| 106 | + |
| 107 | + return E.OK |
0 commit comments