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", "/mnt/station_ramdisk/pendrive") p = sh.mount(device, "/mnt/station_ramdisk/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", "/mnt/station_ramdisk/pendrive") except sh.ErrorReturnCode as e: try: sh.umount("/mnt/station_ramdisk/pendrive") except sh.ErrorReturnCode: pass sh.rmdir("/mnt/station_ramdisk/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("/mnt/station_ramdisk/pendrive"): sh.rm("/mnt/station_ramdisk/pendrive/*") sh.rmdir("/mnt/station_ramdisk/pendrive") self.fail("failed: USB device unmounted during/after copying files.") # check MD5 try: p = sh.md5sum("/mnt/station_ramdisk/pendrive/usbdatatest.bin") except sh.ErrorReturnCode as e: try: sh.umount("/mnt/station_ramdisk/pendrive") except sh.ErrorReturnCode: pass sh.rmdir("/mnt/station_ramdisk/pendrive") self.fail("failed: Unable to calculate MD5 of the copied file.") newmd5 = p.stdout.decode().split(" ")[0] with open('/mnt/station_ramdisk/pendrive/usbdatatest.bin.md5', 'r') as outfile: oldmd5 = outfile.read().rstrip("\n") outfile.close() if newmd5 != oldmd5: try: sh.umount("/mnt/station_ramdisk/pendrive") except sh.ErrorReturnCode: pass sh.rmdir("/mnt/station_ramdisk/pendrive") self.fail("failed: MD5 check failed.") # delete copied files sh.rm("-f", "/mnt/station_ramdisk/pendrive/usbdatatest.bin", "/mnt/station_ramdisk/pendrive/usbdatatest.bin.md5") # Finish try: sh.umount("/mnt/station_ramdisk/pendrive") except sh.ErrorReturnCode: pass sh.rmdir("/mnt/station_ramdisk/pendrive") def getresults(self): return self.__resultlist def gettextresult(self): return ""