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.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.fail("failed: Wrong md5 result.") else: # umount sh.umount("/mnt/pendrive") self.fail("failed: Unable to copy files to the USB memory device.") else: self.fail("failed: Unable to mount the USB memory device.") def getresults(self): return self.__resultlist def gettextresult(self): return ""