import unittest import subprocess from test.helpers.globalVariables import globalVar class TestSysCommand(unittest.TestCase): __str_cmd = None __testname = None __outfilename = None __outdata = None __outtofile = False def __init__(self, testname, testfunc, str_cmd, outtofile = False): """ init """ super(TestSysCommand, self).__init__(testfunc) self.__str_cmd = str_cmd self.__testname = testname self.__outtofile = outtofile self._testMethodDoc = testname if self.__outtofile is True: self.__outfilename = '/tmp/{}.txt'.format(testname) def getName(self): return self.__testname def execute(self): res = -1 try: completed = subprocess.run( self.__str_cmd, check=True, shell=True, stdout=subprocess.PIPE, ) self.assertTrue(completed.returncode is 0) if completed.returncode is 0: if self.__outtofile is True: f = open(self.__outfilename, 'wb') f.write(completed.stdout) f.close() res = 0 else: res = -3 outdata = completed.stdout self.longMessage=str(outdata).replace("'","") self.assertTrue(True) except subprocess.CalledProcessError as err: self.assertTrue(False) res = -1 except Exception as t: res = -2 return res def remove_file(self): pass class SysCommand(object): __str_cmd = None __cmdname = None __outdata = None __errdata = None def __init__(self, cmdname, str_cmd): """ init """ self.__str_cmd = str_cmd self.__cmdname = cmdname def getName(self): return self.__testname def execute(self): res = -1 try: self.__outdata = None self.__errdata = None completed = subprocess.run( self.__str_cmd, check=True, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE ) self.__outdata = completed.stdout if completed.returncode is 0: res = 0 if completed.stderr.decode('ascii') != "": res = -1 self.__errdata = completed.stderr except subprocess.CalledProcessError as err: res = -2 except Exception as t: res = -3 return res def getOutput(self): return self.__outdata def getOutErr(self): return self.__errdata def getOutputlines(self): return self.__outdata.splitlines() def save_file(self, fname): f = open(fname, 'wb') f.write(self.__outdata) f.close()