import unittest import sh import re class Qethernet(unittest.TestCase): __serverip = None __numbytestx = None __bind = None __bwexpected = None __port = None params = None #varlist content: serverip, bwexpected, port, (optional)bind def __init__(self, testname, testfunc, varlist): self.params = varlist super(Qethernet, self).__init__(testfunc) if "serverip" in varlist: self.__serverip = varlist["serverip"] else: raise Exception('sip param inside Qethernet have been be defined') if "bind" in varlist: self.__bind = varlist["bind"] else: self.__bind = None if "bwexpected" in varlist: self.__bwexpected = varlist["bwexpected"] else: raise Exception('bwexpected param inside Qethernet must be defined') if "port" in varlist: self.__port = varlist["port"] else: raise Exception('port param inside Qethernet must be defined') self.__numbytestx = "10M" self._testMethodDoc = testname def execute(self): # execute iperf command against the server if self.__bind is None: p = sh.iperf("-c", self.__serverip, "-x", "CMSV", "-n", self.__numbytestx, "-f", "m", "-p", self.__port) else: p = sh.iperf("-c", self.__serverip, "-x", "CMSV", "-n", self.__numbytestx, "-f", "m", "-p", self.__port, "-B", self.__bind) # check if it was executed succesfully if p.exit_code == 0: if p.stdout == "": self.fail("failed: error executing iperf command") # analyze output string # split by lines lines = p.stdout.splitlines() # get first line dat = lines[1].decode('ascii') # search for the BW value a = re.search("\d+(\.\d)? Mbits/sec", dat) b = a.group().split() bwreal = b[0] # check if BW is in the expected range self.failUnless(float(bwreal) > float(self.__bwexpected) * 0.9, "failed: speed is lower than spected. Speed(MB/s)" + str(bwreal)) else: self.fail("failed: could not complete iperf command")