import sh import unittest from test.helpers.usb import USBDevices import os.path 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", "/tmp/station/pendrive") p = sh.mount(device, "/tmp/station/pendrive") if p.exit_code != 0: self.fail("failed: Unable to mount the USB memory device.") # execute test for i in range(int(self.__repetitions)): # copy files try: p = sh.cp("/var/lib/hwtest-files/usbdatatest.bin", "/var/lib/hwtest-files/usbdatatest.bin.md5", "/tmp/station/pendrive") except sh.ErrorReturnCode as e: try: sh.umount("/tmp/station/pendrive") except sh.ErrorReturnCode: pass sh.rmdir("/tmp/station/pendrive") self.fail("failed: Unable to copy files to the USB memory device.") # check if the device is still mounted if not os.path.ismount("/tmp/station/pendrive"): sh.rm("/tmp/station/pendrive/*") sh.rmdir("/tmp/station/pendrive") self.fail("failed: USB device unmounted during/after copying files.") # check MD5 try: p = sh.md5sum("/tmp/station/pendrive/usbdatatest.bin") except sh.ErrorReturnCode as e: try: sh.umount("/tmp/station/pendrive") except sh.ErrorReturnCode: pass sh.rmdir("/tmp/station/pendrive") self.fail("failed: Unable to calculate MD5 of the copied file.") newmd5 = p.stdout.decode().split(" ")[0] with open('/tmp/station/pendrive/usbdatatest.bin.md5', 'r') as outfile: oldmd5 = outfile.read().rstrip("\n") outfile.close() if newmd5 != oldmd5: try: sh.umount("/tmp/station/pendrive") except sh.ErrorReturnCode: pass sh.rmdir("/tmp/station/pendrive") self.fail("failed: MD5 check failed.") # delete copied files sh.rm("-f", "/tmp/station/pendrive/usbdatatest.bin", "/tmp/station/pendrive/usbdatatest.bin.md5") # Finish try: sh.umount("/tmp/station/pendrive") except sh.ErrorReturnCode: pass sh.rmdir("/tmp/station/pendrive") def getresults(self): return self.__resultlist def gettextresult(self): return ""