summaryrefslogtreecommitdiff
path: root/test-cli/test/tests/qusb.py
blob: 32d99ef43ff29e963836abd4c3520dd0cfbd93a1 (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
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import sh
import unittest
import re
import os
from test.helpers.usb import USBDevices
from test.helpers.changedir import changedir


class Qusb(unittest.TestCase):
    params = None
    __resultlist = None  # resultlist is a python list of python dictionaries

    def __init__(self, testname, testfunc, varlist):
        self.params = varlist
        super(Qusb, self).__init__(testfunc)
        self._testMethodDoc = testname
        self.__resultlist = []

    def execute(self):
        # get usb device
        dev_obj = USBDevices(self.params["xml"])
        if dev_obj.getMassStorage():
            device = dev_obj.getMassStorage()['disk'] + "1"
        else:
            self.__resultlist.append(
                {
                    "desc": "Test result",
                    "data": "FAILED: No USB memory found",
                    "type": "string"
                }
            )
            self.fail("failed: No USB memory found.")

        # create a new folder where the pendrive is going to be mounted
        sh.mkdir("-p", "/mnt/pendrive")
        # check if the device is mounted, and umount it
        try:
            p = sh.findmnt("-n", device)
            if p.exit_code == 0:
                sh.umount(device)
        except sh.ErrorReturnCode_1:
            # error = 1 means "no found"
            pass
        # mount the device
        p = sh.mount(device, "/mnt/pendrive")
        if p.exit_code == 0:
            # copy files
            test_abspath = os.path.dirname(os.path.abspath(__file__)) + "/../"
            p = sh.cp(os.path.join(test_abspath, "files/usbtest/usbdatatest.bin"),
                      os.path.join(test_abspath, "files/usbtest/usbdatatest.md5"),
                      "/mnt/pendrive")
            if p.exit_code == 0:
                # check
                with changedir("/mnt/pendrive/"):
                    p = sh.md5sum("-c", "usbdatatest.md5")
                q = re.search("OK", p.stdout.decode('ascii'))
                # delete files
                sh.rm("-f", "/mnt/pendrive/usbdatatest.bin", "/mnt/pendrive/usbdatatest.md5")
                # umount
                sh.umount("/mnt/pendrive")
                # check result
                if q is None:
                    self.__resultlist.append(
                        {
                            "desc": "Test result",
                            "data": "FAILED: Wrong md5 result",
                            "type": "string"
                        }
                    )
                    self.fail("failed: Wrong md5 result.")
            else:
                # umount
                sh.umount("/mnt/pendrive")
                self.__resultlist.append(
                    {
                        "desc": "Test result",
                        "data": "FAILED: Unable to copy files to the USB memory device",
                        "type": "string"
                    }
                )
                self.fail("failed: Unable to copy files to the USB memory device.")
        else:
            self.__resultlist.append(
                {
                    "desc": "Test result",
                    "data": "FAILED: Unable to mount the USB memory device",
                    "type": "string"
                }
            )
            self.fail("failed: Unable to mount the USB memory device.")

        # Test successful
        self.__resultlist.append(
            {
                "desc": "Test result",
                "data": "OK",
                "type": "string"
            }
        )

    def getresults(self):
        return self.__resultlist