from test.helpers.syscmd import SysCommand import unittest class Qusb(unittest.TestCase): def __init__(self, testname, testfunc, devLabel, numPorts): super(Qusb, self).__init__(testfunc) self.__numPorts = numPorts self._testMethodDoc = testname self.__devLabel = devLabel if testname=="USBOTG": self.__usbFileName = "/this_is_an_usb_otg" self.__usbtext = "USBOTG" elif testname=="SATA": self.__usbFileName = "/this_is_a_sata" self.__usbtext = "SATA" else: self.__usbFileName = "/this_is_an_usb_host" self.__usbtext = "USBHOST" self.__numUsbFail=[] def execute(self): str_cmd= "lsblk -o LABEL" lsblk_command = SysCommand("lsblk", str_cmd) if lsblk_command.execute() == 0: self.__raw_out = lsblk_command.getOutput() if self.__raw_out == "": return -1 lines = lsblk_command.getOutput().splitlines() host_list=[] for i in range(len(lines)): if str(lines[i].decode('ascii'))==self.__devLabel: host_list.append(i) if len(host_list)==int(self.__numPorts): str_cmd = "lsblk -o MOUNTPOINT" lsblk_command = SysCommand("lsblk", str_cmd) if lsblk_command.execute() == 0: self.__raw_out = lsblk_command.getOutput() if self.__raw_out == "": print("failed: no command output") self.fail("failed: no command output") else: lines = lsblk_command.getOutput().splitlines() for i in range(len(host_list)): file_path=str(lines[host_list[i]].decode('ascii')) + self.__usbFileName usb_file = open(file_path, 'r') read=usb_file.read() if read.find(self.__usbtext)!=-1: print(file_path + " --> OK!") else: self.fail("failed: could not read from usb {}".format(lines[host_list[i]].decode('ascii'))) self.__numUsbFail.append(host_list[i]) usb_file.close() else: self.fail("failed: couldn't execute lsblk command") else: self.fail("failed: reference and real usb host devices number mismatch") else: self.fail("failed: couldn't execute lsblk command")