summaryrefslogtreecommitdiff
path: root/test-cli/test/helpers/syscmd.py
diff options
context:
space:
mode:
Diffstat (limited to 'test-cli/test/helpers/syscmd.py')
-rw-r--r--test-cli/test/helpers/syscmd.py88
1 files changed, 59 insertions, 29 deletions
diff --git a/test-cli/test/helpers/syscmd.py b/test-cli/test/helpers/syscmd.py
index 6114449..7223774 100644
--- a/test-cli/test/helpers/syscmd.py
+++ b/test-cli/test/helpers/syscmd.py
@@ -1,5 +1,6 @@
import unittest
import subprocess
+import threading
class TestSysCommand(unittest.TestCase):
@@ -9,24 +10,15 @@ class TestSysCommand(unittest.TestCase):
__outdata = None
__outtofile = False
- #varlist: str_cmd, outtofile
- def __init__(self, testname, testfunc, varlist):
+ def __init__(self, testname, testfunc, str_cmd, outtofile=False):
""" init """
super(TestSysCommand, self).__init__(testfunc)
- if "str_cmd" in varlist:
- self.__str_cmd = varlist["str_cmd"]
- else:
- raise Exception('str_cmd param inside TestSysCommand have been be defined')
+ self.__str_cmd = str_cmd
self.__testname = testname
- if "outtofile" in varlist:
- self.__outtofile = varlist["outtofile"]
- if self.__outtofile is True:
- self.__outfilename = '/tmp/{}.txt'.format(testname)
- else:
- self.__outtofile = None
- self.__outfilename = None
+ self.__outtofile = outtofile
self._testMethodDoc = testname
-
+ if self.__outtofile is True:
+ self.__outfilename = '/tmp/{}.txt'.format(testname)
def getName(self):
return self.__testname
@@ -50,7 +42,7 @@ class TestSysCommand(unittest.TestCase):
else:
res = -3
outdata = completed.stdout
- self.longMessage=str(outdata).replace("'","")
+ self.longMessage = str(outdata).replace("'", "")
self.assertTrue(True)
except subprocess.CalledProcessError as err:
self.assertTrue(False)
@@ -62,11 +54,14 @@ class TestSysCommand(unittest.TestCase):
def remove_file(self):
pass
+
class SysCommand(object):
__str_cmd = None
__cmdname = None
__outdata = None
__errdata = None
+ __returnCode = None
+ __exception = None
def __init__(self, cmdname, str_cmd):
""" init """
@@ -74,31 +69,37 @@ class SysCommand(object):
self.__cmdname = cmdname
def getName(self):
- return self.__testname
+ return self.__cmdname
+
+ def setName(self, cmdName):
+ self.__cmdname = cmdName
+
+ def getParams(self):
+ return self.__str_cmd
+
+ def setParam(self, params):
+ self.__str_cmd = params
def execute(self):
- res = -1
+ # try:
+ self.__outdata = None
+ self.__errdata = None
+ self.__exception = None
try:
- self.__outdata = None
- self.__errdata = None
completed = subprocess.run(
self.__str_cmd,
- check=True,
+ check=False,
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
)
+ self.__returnCode = completed.returncode
self.__outdata = completed.stdout
- if completed.returncode is 0:
- res = 0
- if completed.stderr.decode('ascii') != "":
- res = -1
- self.__errdata = completed.stderr
+ self.__errdata = completed.stderr.decode('ascii')
except subprocess.CalledProcessError as err:
- res = -2
- except Exception as t:
- res = -3
- return res
+ self.__exception = err
+ self.__returnCode = -1
+ return self.__returnCode
def getOutput(self):
return self.__outdata
@@ -106,6 +107,12 @@ class SysCommand(object):
def getOutErr(self):
return self.__errdata
+ def getException(self):
+ return self.__exception
+
+ def IsException(self):
+ return self.__exception is not None
+
def getOutputlines(self):
return self.__outdata.splitlines()
@@ -114,3 +121,26 @@ class SysCommand(object):
f.write(self.__outdata)
f.close()
+
+class AsyncSys(threading.Thread):
+ sysObject = None
+ sysres = None
+
+ def __init__(self, threadID, name, counter):
+ threading.Thread.__init__(self)
+ self.threadID = threadID
+ self.name = name
+ self.counter = counter
+ self.sysObject = SysCommand(name, None)
+
+ def setParams(self, Params):
+ self.sysObject.setParam(Params)
+
+ def getRes(self):
+ return self.sysres
+
+ def getData(self):
+ return self.sysObject.getOutput()
+
+ def run(self):
+ self.sysres = self.sysObject.execute()