summaryrefslogtreecommitdiff
path: root/test-cli/test/tests/qusb.py
blob: 70265a56ed626ccc41d0ff55685f5793c87171e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
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")