def rw_test(self): """ RW mode test of CDROM :return: """ try: devname = self.device.get_property("DEVNAME") Command("umount %s" % devname).run(ignore_errors=True) if "BD" in self.type: print("Formatting ...") sys.stdout.flush() Command("dvd+rw-format -format=full %s 2>/dev/null" % devname).echo() self.reload_disc(devname) sys.stdout.flush() return self.write_test() elif "DVD_PLUS" in self.type: print("Formatting ...") sys.stdout.flush() Command("dvd+rw-format -force %s 2>/dev/null" % devname).echo() self.reload_disc(devname) sys.stdout.flush() return self.write_test() else: print("Blanking ...") sys.stdout.flush() # blankCommand = Command("cdrecord -v dev=%s blank=fast" % devname).echo() Command("cdrecord -v dev=%s blank=fast" % devname).echo() self.reload_disc(devname) sys.stdout.flush() return self.write_test() except CertCommandError: return False
def test_bw(self, cmd): """ Test bandwidth :param cmd: :return: """ if self.link_layer == 'Ethernet': cmd = cmd + ' -R' if not self.call_remote_server(cmd, 'start', self.server_ip): print("start %s server failed." % cmd) return False cmd = "%s %s -d %s -i %s" % (cmd, self.server_ip, self.ib_device, self.ib_port) print(cmd) com = Command(cmd) pattern = r"\s+(\d+)\s+(\d+)\s+([\.\d]+)\s+(?P<avg_bw>[\.\d]+)\s+([\.\d]+)" try: avg_bw = com.get_str(pattern, 'avg_bw', False) # MB/sec avg_bw = float(avg_bw) * 8 tgt_bw = self.target_bandwidth_percent * self.speed print("Current bandwidth is %.2fMb/s, target is %.2fMb/s" % (avg_bw, tgt_bw)) return avg_bw > tgt_bw except Exception as concrete_error: print(concrete_error) self.call_remote_server(cmd, 'stop') return False
def exec_perf(self): """ Execute perf command :return: """ # record print("Collecting the perf record using the command '%s'." % self.perfRecord) perfRecordEcho = Command(self.perfRecord).read() perfRecordMacth = re.search("perf record", perfRecordEcho) if not perfRecordMacth: print("Error: failed to record events because of :\n %s." % perfRecordEcho) else: print("Success to record events :\n %s." % perfRecordEcho) # evList perfEvlistEcho = Command(self.perfEvlist).read() perfEvlistdMacth = re.search("cycles", perfEvlistEcho) if not perfEvlistdMacth: print("Error: required hardware event not available because of :\n %s." % perfEvlistEcho) return False else: print("Hardware event found : \n %s." % perfEvlistEcho) # report perfReportEcho = Command(self.perfReport).read() perfReportMacth = re.search(r"\s*\S+\s+(\[\S+.\S+\])\s+\S+", perfReportEcho) if not perfReportMacth: print("Error: no samples found. Failed to fetch report because of:\n %s." % perfReportEcho) return False else: print("Samples found for the hardware event :\n %s." % perfReportEcho) return True
class Load: """ Let a program run on a specific CPU """ def __init__(self, cpu): self.cpu = cpu self.process = Command("taskset -c {} python -u {}/cpufreq/cal.py".\ format(self.cpu, CertEnv.testdirectoy)) self.returncode = None def run(self): """ Process started :return: """ self.process.start() # background def get_runtime(self): """ Get the running time of the process :return: """ if not self.process: return None while self.returncode is None: self.returncode = self.process.poll() if self.returncode == 0: line = self.process.readline() return float(line) else: return False
def test_tcp_bandwidth(self): """ Test tcp bandwidth :return: """ cmd = "qperf %s tcp_bw" % self.server_ip print(cmd) com = Command(cmd) pattern = r"\s+bw\s+=\s+(?P<bandwidth>[\.0-9]+ [MG]B/sec)" for _ in range(self.retries): try: bandwidth = com.get_str(pattern, 'bandwidth', False) band_width = bandwidth.split() if 'GB' in band_width[1]: bandwidth = float(band_width[0]) * 8 * 1024 else: bandwidth = float(band_width[0]) * 8 target_bandwidth = self.target_bandwidth_percent * self.speed print("Current bandwidth is %.2fMb/s, target is %.2fMb/s" % (bandwidth, target_bandwidth)) if bandwidth > target_bandwidth: return True except Exception as concrete_error: print(concrete_error) return False
def test(self): """ test case :return: """ if not os.path.exists("/dev/watchdog"): os.system("modprobe softdog") os.chdir(self.test_dir) try: timeout = Command("./watchdog -g").get_str( regex="^Watchdog timeout is (?P<timeout>[0-9]*) seconds.$", regex_group="timeout") timeout = int(timeout) if timeout > self.max_timeout: Command("./watchdog -s %d" % self.max_timeout).echo() except CertCommandError as e: print(e) print("Set/get watchdog timeout failed.") return False ui = CommandUI() if ui.prompt_confirm("System will reboot, are you ready?"): print("") sys.stdout.flush() os.system("sync") os.system("./watchdog -t") time.sleep(5) return False else: print("") return False
def test(self): """ Test case :return: """ if not (self.method and self.device and self.type): return False if self.method not in dir(self): return False devname = self.device.get_property("DEVNAME") Command("eject %s" % devname).run(ignore_errors=True) while True: print("Please insert %s disc into %s, then close the tray manually."\ % (self.type.lower(), devname)) if self.method == "write_test": print(" tips:disc should be new.") elif self.method == "read_test": print(" tips:disc should not be blank.") if self.com_ui.prompt_confirm("Done well?"): break Command("eject -t %s" % devname).run(ignore_errors=True) print("Waiting media..).") time.sleep(20) if not getattr(self, self.method)(): return False return True
def reload_disc(self, device): """ Reloading the media :param device: :return: """ if not device: return False print("Reloading the media ... ") sys.stdout.flush() try: Command("eject %s" % device).run() print("tray ejected.") sys.stdout.flush() except Exception: pass try: Command("eject -t %s" % device).run() print("tray auto-closed.\n") sys.stdout.flush() except Exception: print( "Could not auto-close the tray, please close the tray manually." ) self.com_ui.prompt_confirm("Done well?") time.sleep(20) return True
def get_info(self): """ Get CPU info :return: """ cmd = Command("lscpu") try: nums = cmd.get_str(r'^CPU\S*:\s+(?P<cpus>\d+)$', 'cpus', False) except Exception as concrete_error: print(concrete_error) return False self.nums = int(nums) self.list = range(self.nums) cmd = Command( "cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_max_freq") try: max_freq = cmd.get_str() except Exception as concrete_error: print(concrete_error) 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 Exception as concrete_error: print(concrete_error) return False self.min_freq = int(min_freq) return True
def get_interface_ip(self): """ Get interface ip :return: """ com = Command("ip addr show %s" % self.interface) pattern = r".*inet.? (?P<ip>.+)/.*" try: ip_addr = com.get_str(pattern, 'ip', False) return ip_addr except Exception: print("[X] No available ip on the interface.") return None
def offline_memory(self, memory_path): """ Set memory offline :param memory_path: :return: """ try: Command("echo 0 > %s/online" % memory_path).run() Command("cat %s/state" % memory_path).get_str("offline") return True except Exception: print("Error: fail to online %s." % memory_path) return False
def get_governor(self, cpu): """ Get cpu governor :param cpu: :return: """ cmd = Command("cpupower -c %s frequency-info -p" % cpu) try: return cmd.get_str(r'.* governor "(?P<governor>\w+)".*', 'governor', False) except Exception as concrete_error: print(concrete_error) return False
def start_ipmi(self): """ Start IPMI test :return: """ try: Command("systemctl start ipmi").run() Command("systemctl status ipmi.service").get_str(regex="Active: active", \ single_line=False) except Exception: print("ipmi service cant't be started") return False return True
def get_speed(self): """ Get speed on the interface :return: """ com = Command("ethtool %s" % self.interface) pattern = r".*Speed:\s+(?P<speed>\d+)Mb/s" try: speed = com.get_str(pattern, 'speed', False) return int(speed) except Exception: print("[X] No speed found on the interface.") return None
def get_freq(self, cpu): """ Get CPU frequency :param cpu: :return: """ cmd = Command("cpupower -c %s frequency-info -w" % cpu) try: return int( cmd.get_str(r'.* frequency: (?P<freq>\d+) .*', 'freq', False)) except Exception as concrete_error: print(concrete_error) return False
def set_freq(self, freq, cpu='all'): """ Set CPU frequency :param freq: :param cpu: :return: """ cmd = Command("cpupower -c %s frequency-set --freq %s" % (cpu, freq)) try: cmd.run() return cmd.returncode except Exception as concrete_error: print(concrete_error) return False
def find_path(self, parent_dir, target_name): """ Find the target path from the specified directory :param parent_dir: :param target_name: :return: """ cmd = Command("find %s -name %s" % (parent_dir, target_name)) try: cmd.run() return cmd.returncode except Exception as concrete_error: print(concrete_error) return False
def setup(self, args=None): """ The Setup before testing :return: """ try: self.args = args or argparse.Namespace() self.logpath = getattr(args, "logdir", None) + "/disk.log" os.system("echo 'Disk Info: ' >> %s" % self.logpath) Command("fdisk -l &>> %s" % self.logpath).echo(ignore_errors=True) os.system("echo 'Partition Info: ' >> %s" % self.logpath) Command("df -h &>> %s" % self.logpath).echo(ignore_errors=True) os.system("echo 'Mount Info: ' >> %s" % self.logpath) Command("mount &>> %s" % self.logpath).echo(ignore_errors=True) os.system("echo 'Swap Info: ' >> %s" % self.logpath) Command("cat /proc/swaps &>> %s" % self.logpath).echo(ignore_errors=True) os.system("echo 'LVM Info: ' >> %s" % self.logpath) Command("pvdisplay &>> %s" % self.logpath).echo(ignore_errors=True) Command("vgdisplay &>> %s" % self.logpath).echo(ignore_errors=True) Command("lvdisplay &>> %s" % self.logpath).echo(ignore_errors=True) os.system("echo 'Md Info: ' >> %s" % self.logpath) Command("cat /proc/mdstat &>> %s" % self.logpath).echo(ignore_errors=True) sys.stdout.flush() except Exception as concrete_error: print("Warning: could not get disk info.\n", concrete_error)
def get_ibstatus(self): """ Get ibstatus :return: """ path_netdev = ''.join(['/sys', self.device.get_property("DEVPATH")]) path_pci = path_netdev.split('net')[0] path_ibdev = 'infiniband_verbs/uverb*/ibdev' path_ibdev = ''.join([path_pci, path_ibdev]) cmd = "cat %s" % path_ibdev com = Command(cmd) try: self.ib_device = com.read() except Exception as concrete_error: print(concrete_error) return False path_ibport = '/sys/class/net/%s/dev_id' % self.interface cmd = "cat %s" % path_ibport com = Command(cmd) try: self.ib_port = int(com.read(), 16) + 1 except Exception as concrete_error: print(concrete_error) return False ib_str = "Infiniband device '%s' port %d" % (self.ib_device, self.ib_port) print("Interface %s ===> %s" % (self.interface, ib_str)) cmd = "ibstatus" print(cmd) com = Command(cmd) try: output = com.read() for info in output.split('\n\n'): if ib_str not in info: continue print(info) self.gid = re.search(r"default gid:\s+(.*)", info).group(1) self.base_lid = re.search(r"base lid:\s+(.*)", info).group(1) self.sm_lid = re.search(r"sm lid:\s+(.*)", info).group(1) self.state = re.search(r"state:\s+(.*)", info).group(1) self.phys_state = re.search(r"phys state:\s+(.*)", info).group(1) self.link_layer = re.search(r"link_layer:\s+(.*)", info).group(1) self.speed = int(re.search(r"rate:\s+(\d*)", info).group(1)) * 1024 except Exception as concrete_error: print(concrete_error) return False return True
def set_governor(self, governor, cpu='all'): """ Set the frequency governor mode of CPU :param governor: :param cpu: :return: """ cmd = Command("cpupower -c %s frequency-set --governor %s" % (cpu, governor)) try: cmd.run() return cmd.returncode except Exception as concrete_error: print(concrete_error) return False
def verify_vmcore(self): """ Verify vmcore :return: """ config = ConfigFile(self.kdump_conf) if config.get_parameter("path"): self.vmcore_path = config.get_parameter("path") dir_pattern = re.compile( r'(?P<ipaddr>[0-9]+\.[0-9]+\.[0-9]+)-(?P<date>[0-9]+[-.][0-9]+[-.][0-9]+)-' r'(?P<time>[0-9]+:[0-9]+:[0-9]+)') vmcore_dirs = list() for (root, dirs, files) in os.walk(self.vmcore_path): for eve_dir in dirs: if dir_pattern.search(eve_dir): vmcore_dirs.append(eve_dir) vmcore_dirs.sort() vmcore_file = os.path.join(self.vmcore_path, vmcore_dirs[-1], "vmcore") try: Command( "echo \"sys\nq\" | crash -s %s /usr/lib/debug/lib/modules/`uname -r`/vmlinux" % \ vmcore_file).echo() print("kdump image %s verified" % vmcore_file) return True except CertCommandError as concrete_error: print("Error: could not verify kdump image %s" % vmcore_file) print(concrete_error) return False
def get_other_interfaces(self): """ Get other interfaces :return: """ ignore_interfaces = ['^lo', '^v', 'docker', 'br', 'bond'] cmd = "ip route show default | awk '/default/ {print $5}'" com = Command(cmd) management_interface = com.read() if management_interface: ignore_interfaces.append(management_interface) # print(cmd) # print("Management interface: %s" % management_interface) ignore_pattern = '|'.join(ignore_interfaces) # print(ignore_pattern) cmd = "ls /sys/class/net/ | grep -vE '%s'" % ignore_pattern # print(cmd) com = Command(cmd) try: com.run() return com.output except Exception as concrete_error: print(concrete_error) return []
def test(self): """ Test case :return: """ try: Command("cat /proc/cmdline").get_str(r"crashkernel=[^\ ]*") except Exception: print("Error: no crashkernel found.") return False config = ConfigFile(self.kdump_conf) if not config.get_parameter("path"): config.add_parameter("path", self.vmcore_path) else: self.vmcore_path = config.get_parameter("path") if config.get_parameter("kdump_obj") == "kbox": config.remove_parameter("kdump_obj") config.add_parameter("kdump_obj", "all") try: Command("systemctl restart kdump").run() Command("systemctl status kdump").get_str(regex="Active: active", single_line=False) except Exception: print("Error: kdump service not working.") return False print("kdump config:") print("#############") config.dump() print("#############") com_ui = CommandUI() if com_ui.prompt_confirm("System will reboot, are you ready?"): print("\ntrigger crash...") sys.stdout.flush() os.system("sync") os.system("echo c > /proc/sysrq-trigger") time.sleep(30) return False else: print("") return False
def setup(self, args=None): """ Initialization before test :param args: :return: """ self.args = args or argparse.Namespace() self.device = getattr(args, "device", None) Command("nvme list").echo(ignore_errors=True)
def test_icmp(self): """ Test ICMP :return: """ count = 500 com = Command("ping -q -c %d -i 0 %s" % (count, self.server_ip)) pattern = r".*, (?P<loss>\d+\.{0,1}\d*)% packet loss.*" for _ in range(self.retries): try: print(com.command) loss = com.get_str(pattern, 'loss', False) com.print_output() if float(loss) == 0: return True except Exception as concrete_error: print(concrete_error) return False
def check_certrpm(self): """ Check installed cert package :return: """ print("\nChecking installed cert package...") return_code = True for cert_package in ["oec-hardware"]: rpm_verify = Command( "rpm -V --nomtime --nomode --nocontexts %s &>> %s" % (cert_package, self.logpath)) try: rpm_verify.echo() sys.stdout.flush() if rpm_verify.output and len(rpm_verify.output) > 0: return_code = False except Exception: print("Error: files in %s have been tampered." % cert_package) return_code = False return return_code
def ipmitool(self): """ Testing with iptool tools :return: """ cmd_list = ["ipmitool fru", "ipmitool sensor"] for cmd in cmd_list: try: Command(cmd).echo() except Exception: print("%s return error." % cmd) return False return True
def get_modulefile(self, module): """ Get module file :param module: :return: """ try: modulefile = Command("modinfo -F filename %s" % module).get_str() if os.path.islink(modulefile): modulefile = os.readlink(modulefile) return modulefile except Exception: print("Error: could no find module file for %s:" % module) return None
def memory_hotplug(self): """ Memory hotplug test :return: """ print("\nMemory hotplug testing...") if not self.hot_plug_verify(): print("Warning: memory hotplug test skipped.") return True print("Searching removable memory...") if not os.path.exists("/sys/devices/system/node/"): return False return_code = True mem_path_list = list() pattern = re.compile("^memory[0-9]*$") for (dirpath, dirs, filenames) in os.walk("/sys/devices/system/node/"): mem_dirs = filter(pattern.search, dirs) for mem_dirname in mem_dirs: mem_path_list.append(os.path.join(dirpath, mem_dirname)) if not mem_path_list: print("Error: no memory found.") return False test_flag = 0 for memory_path in mem_path_list: try: Command("cat %s/removable" % memory_path).get_str("1") print("%s is removable, start testing..." % os.path.basename(memory_path)) test_flag = 1 except: continue if not self.hotplug_memory_test(memory_path): print("%s hotplug test fail." % os.path.basename(memory_path)) return_code = False if test_flag == 0: print("No removable memory found.") if self.retry_list: print("Retry to online memory after 2mins.") time.sleep(120) for memory_path in self.retry_list: self.online_memory(memory_path) return return_code
def hot_plug_verify(self): """ Verify hot plug :return: """ kernel = Command("uname -r").read() config_file = "/boot/config-" + kernel if not os.path.exists(config_file): print("Config %s does not exist." % config_file) return False if os.system( "grep -q -w 'CONFIG_MEMORY_HOTPLUG=y' %s" % config_file) != 0: print("Memory hotplug is not supported.") return False return True