from test.helpers.syscmd import SysCommand import unittest class Qusb(unittest.TestCase): __numPorts = None __devLabel = None params = None def __init__(self, testname, testfunc, varlist): self.params = varlist super(Qusb, self).__init__(testfunc) if "numPorts" in varlist: self.__numPorts = varlist["numPorts"] else: raise Exception('numPorts param inside Qusb must be defined') self._testMethodDoc = testname if "devLabel" in varlist: self.__devLabel = varlist["devLabel"] else: raise Exception('devLabel param inside Qusb must be defined') if testname == "USBOTG": self.__usbFileName = "/this_is_an_usb_otg" self.__usbtext = "USBOTG" 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")