import sh import unittest from test.helpers.usb import USBDevices 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 if "repetitions" in varlist: self.__repetitions = varlist["repetitions"] else: raise Exception('repetitions param inside Qusb must be defined') 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.") # 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 as e: # error = 1 means "no found" pass # mount the device sh.mkdir("-p", "/mnt/pendrive") p = sh.mount(device, "/mnt/pendrive") if p.exit_code != 0: self.fail("failed: Unable to mount the USB memory device.") # execute test for i in range(self.__repetitions): # copy files try: p = sh.cp("/var/lib/hwtest-files/usbdatatest.bin", "/var/lib/hwtest-files/usbdatatest.bin.md5", "/mnt/pendrive") except sh.ErrorReturnCode as e: sh.umount("/mnt/pendrive") sh.rmdir("/mnt/pendrive") self.fail("failed: Unable to copy files to the USB memory device.") # check MD5 try: p = sh.md5sum("/mnt/pendrive/usbdatatest.bin") except sh.ErrorReturnCode as e: sh.umount("/mnt/pendrive") sh.rmdir("/mnt/pendrive") self.fail("failed: Unable to calculate MD5 of the copied file.") newmd5 = p.stdout.decode().split(" ")[0] with open('/mnt/pendrive/usbdatatest.bin.md5', 'r') as outfile: oldmd5 = outfile.read() outfile.close() if newmd5 != oldmd5: sh.umount("/mnt/pendrive") sh.rmdir("/mnt/pendrive") self.fail("failed: MD5 check failed.") # delete copied files sh.rm("-f", "/mnt/pendrive/usbdatatest.bin", "/mnt/pendrive/usbdatatest.bin.md5") # Finish sh.umount("/mnt/pendrive") sh.rmdir("/mnt/pendrive") def getresults(self): return self.__resultlist def gettextresult(self): return ""