提交 654d1f47 编写于 作者: L liuyq-617

[TD-12186]<test>import lib from any path in test frame

上级 1a6350de
...@@ -37,68 +37,83 @@ if __name__ == "__main__": ...@@ -37,68 +37,83 @@ if __name__ == "__main__":
stop = 0 stop = 0
restart = False restart = False
windows = 0 windows = 0
opts, args = getopt.gnu_getopt(sys.argv[1:], 'f:p:m:l:scghrw', [ opts, args = getopt.gnu_getopt(
'file=', 'path=', 'master', 'logSql', 'stop', 'cluster', 'valgrind', 'help', 'windows']) sys.argv[1:],
"f:p:m:l:scghrw",
[
"file=",
"path=",
"master",
"logSql",
"stop",
"cluster",
"valgrind",
"help",
"windows",
],
)
for key, value in opts: for key, value in opts:
if key in ['-h', '--help']: if key in ["-h", "--help"]:
tdLog.printNoPrefix( tdLog.printNoPrefix("A collection of test cases written using Python")
'A collection of test cases written using Python') tdLog.printNoPrefix("-f Name of test case file written by Python")
tdLog.printNoPrefix('-f Name of test case file written by Python') tdLog.printNoPrefix("-p Deploy Path for Simulator")
tdLog.printNoPrefix('-p Deploy Path for Simulator') tdLog.printNoPrefix("-m Master Ip for Simulator")
tdLog.printNoPrefix('-m Master Ip for Simulator') tdLog.printNoPrefix("-l <True:False> logSql Flag")
tdLog.printNoPrefix('-l <True:False> logSql Flag') tdLog.printNoPrefix("-s stop All dnodes")
tdLog.printNoPrefix('-s stop All dnodes') tdLog.printNoPrefix("-c Test Cluster Flag")
tdLog.printNoPrefix('-c Test Cluster Flag') tdLog.printNoPrefix("-g valgrind Test Flag")
tdLog.printNoPrefix('-g valgrind Test Flag') tdLog.printNoPrefix("-r taosd restart test")
tdLog.printNoPrefix('-r taosd restart test') tdLog.printNoPrefix("-w taos on windows")
tdLog.printNoPrefix('-w taos on windows')
sys.exit(0) sys.exit(0)
if key in ['-r', '--restart']: if key in ["-r", "--restart"]:
restart = True restart = True
if key in ['-f', '--file']: if key in ["-f", "--file"]:
fileName = value fileName = value
if key in ['-p', '--path']: if key in ["-p", "--path"]:
deployPath = value deployPath = value
if key in ['-m', '--master']: if key in ["-m", "--master"]:
masterIp = value masterIp = value
if key in ['-l', '--logSql']: if key in ["-l", "--logSql"]:
if (value.upper() == "TRUE"): if value.upper() == "TRUE":
logSql = True logSql = True
elif (value.upper() == "FALSE"): elif value.upper() == "FALSE":
logSql = False logSql = False
else: else:
tdLog.printNoPrefix("logSql value %s is invalid" % logSql) tdLog.printNoPrefix("logSql value %s is invalid" % logSql)
sys.exit(0) sys.exit(0)
if key in ['-c', '--cluster']: if key in ["-c", "--cluster"]:
testCluster = True testCluster = True
if key in ['-g', '--valgrind']: if key in ["-g", "--valgrind"]:
valgrind = 1 valgrind = 1
if key in ['-s', '--stop']: if key in ["-s", "--stop"]:
stop = 1 stop = 1
if key in ['-w', '--windows']: if key in ["-w", "--windows"]:
windows = 1 windows = 1
if (stop != 0): if stop != 0:
if (valgrind == 0): if valgrind == 0:
toBeKilled = "taosd" toBeKilled = "taosd"
else: else:
toBeKilled = "valgrind.bin" toBeKilled = "valgrind.bin"
killCmd = "ps -ef|grep -w %s| grep -v grep | awk '{print $2}' | xargs kill -TERM > /dev/null 2>&1" % toBeKilled killCmd = (
"ps -ef|grep -w %s| grep -v grep | awk '{print $2}' | xargs kill -TERM > /dev/null 2>&1"
% toBeKilled
)
psCmd = "ps -ef|grep -w %s| grep -v grep | awk '{print $2}'" % toBeKilled psCmd = "ps -ef|grep -w %s| grep -v grep | awk '{print $2}'" % toBeKilled
processID = subprocess.check_output(psCmd, shell=True) processID = subprocess.check_output(psCmd, shell=True)
while(processID): while processID:
os.system(killCmd) os.system(killCmd)
time.sleep(1) time.sleep(1)
processID = subprocess.check_output(psCmd, shell=True) processID = subprocess.check_output(psCmd, shell=True)
...@@ -115,25 +130,26 @@ if __name__ == "__main__": ...@@ -115,25 +130,26 @@ if __name__ == "__main__":
if valgrind: if valgrind:
time.sleep(2) time.sleep(2)
tdLog.info('stop All dnodes') tdLog.info("stop All dnodes")
if masterIp == "": if masterIp == "":
host = '127.0.0.1' host = "127.0.0.1"
else: else:
host = masterIp host = masterIp
tdLog.info("Procedures for tdengine deployed in %s" % (host)) tdLog.info("Procedures for tdengine deployed in %s" % (host))
uModule = tdDnodes.import_module(fileName)
if windows: if windows:
tdCases.logSql(logSql) tdCases.logSql(logSql)
tdLog.info("Procedures for testing self-deployment") tdLog.info("Procedures for testing self-deployment")
td_clinet = TDSimClient("C:\\TDengine") td_clinet = TDSimClient("C:\\TDengine")
td_clinet.deploy() td_clinet.deploy()
remote_conn = Connection("root@%s"%host) remote_conn = Connection("root@%s" % host)
with remote_conn.cd('/var/lib/jenkins/workspace/TDinternal/community/tests/pytest'): with remote_conn.cd(
"/var/lib/jenkins/workspace/TDinternal/community/tests/pytest"
):
remote_conn.run("python3 ./test.py") remote_conn.run("python3 ./test.py")
conn = taos.connect( conn = taos.connect(host="%s" % (host), config=td_clinet.cfgDir)
host="%s"%(host),
config=td_clinet.cfgDir)
tdCases.runOneWindows(conn, fileName) tdCases.runOneWindows(conn, fileName)
else: else:
tdDnodes.init(deployPath) tdDnodes.init(deployPath)
...@@ -141,27 +157,24 @@ if __name__ == "__main__": ...@@ -141,27 +157,24 @@ if __name__ == "__main__":
tdDnodes.setValgrind(valgrind) tdDnodes.setValgrind(valgrind)
tdDnodes.stopAll() tdDnodes.stopAll()
is_test_framework = 0 is_test_framework = 0
key_word = 'tdCases.addLinux' key_word = "tdCases.addLinux"
try: try:
if key_word in open(fileName).read(): if key_word in open(fileName).read():
is_test_framework = 1 is_test_framework = 1
except: except:
pass pass
if is_test_framework: if is_test_framework:
moduleName = fileName.replace(".py", "").replace("/", ".")
uModule = importlib.import_module(moduleName)
try: try:
ucase = uModule.TDTestCase() ucase = uModule.TDTestCase()
tdDnodes.deploy(1,ucase.updatecfgDict) tdDnodes.deploy(1, ucase.updatecfgDict)
except : del uModule
tdDnodes.deploy(1,{}) except:
tdDnodes.deploy(1, {})
else: else:
pass pass
tdDnodes.deploy(1,{}) tdDnodes.deploy(1, {})
tdDnodes.start(1) tdDnodes.start(1)
tdCases.logSql(logSql) tdCases.logSql(logSql)
if testCluster: if testCluster:
...@@ -172,13 +185,11 @@ if __name__ == "__main__": ...@@ -172,13 +185,11 @@ if __name__ == "__main__":
tdCases.runOneCluster(fileName) tdCases.runOneCluster(fileName)
else: else:
tdLog.info("Procedures for testing self-deployment") tdLog.info("Procedures for testing self-deployment")
conn = taos.connect( conn = taos.connect(host, config=tdDnodes.getSimCfgPath())
host,
config=tdDnodes.getSimCfgPath())
if fileName == "all": if fileName == "all":
tdCases.runAllLinux(conn) tdCases.runAllLinux(conn)
else: else:
tdCases.runOneWindows(conn, fileName) tdCases.runOneLinux(conn, fileName, uModule)
if restart: if restart:
if fileName == "all": if fileName == "all":
tdLog.info("not need to query ") tdLog.info("not need to query ")
...@@ -188,7 +199,7 @@ if __name__ == "__main__": ...@@ -188,7 +199,7 @@ if __name__ == "__main__":
tdDnodes.stopAll() tdDnodes.stopAll()
tdDnodes.start(1) tdDnodes.start(1)
time.sleep(1) time.sleep(1)
conn = taos.connect( host, config=tdDnodes.getSimCfgPath()) conn = taos.connect(host, config=tdDnodes.getSimCfgPath())
tdLog.info("Procedures for tdengine deployed in %s" % (host)) tdLog.info("Procedures for tdengine deployed in %s" % (host))
tdLog.info("query test after taosd restart") tdLog.info("query test after taosd restart")
tdCases.runOneLinux(conn, sp[0] + "_" + "restart.py") tdCases.runOneLinux(conn, sp[0] + "_" + "restart.py")
......
...@@ -17,6 +17,7 @@ import time ...@@ -17,6 +17,7 @@ import time
import datetime import datetime
import inspect import inspect
import importlib import importlib
from util.dnodes import *
from util.log import * from util.log import *
...@@ -34,8 +35,9 @@ class TDCases: ...@@ -34,8 +35,9 @@ class TDCases:
self.clusterCases = [] self.clusterCases = []
def __dynamicLoadModule(self, fileName): def __dynamicLoadModule(self, fileName):
moduleName = fileName.replace(".py", "").replace(os.sep, ".") # moduleName = fileName.replace(".py", "").replace(os.sep, ".")
return importlib.import_module(moduleName, package='..') # return importlib.import_module(moduleName, package='..')
return tdDnodes.import_module(fileName)
def logSql(self, logSql): def logSql(self, logSql):
self._logSql = logSql self._logSql = logSql
...@@ -63,10 +65,11 @@ class TDCases: ...@@ -63,10 +65,11 @@ class TDCases:
tdLog.info("total %d Linux test case(s) executed" % (runNum)) tdLog.info("total %d Linux test case(s) executed" % (runNum))
def runOneLinux(self, conn, fileName): def runOneLinux(self, conn, fileName, testModule):
testModule = self.__dynamicLoadModule(fileName) # testModule = self.__dynamicLoadModule(fileName)
runNum = 0 runNum = 0
if fileName.startswith(".."):
fileName = fileName[3:]
for tmp in self.linuxCases: for tmp in self.linuxCases:
if tmp.name.find(fileName) != -1: if tmp.name.find(fileName) != -1:
case = testModule.TDTestCase() case = testModule.TDTestCase()
...@@ -95,7 +98,7 @@ class TDCases: ...@@ -95,7 +98,7 @@ class TDCases:
tdLog.notice("total %d Windows test case(s) executed" % (runNum)) tdLog.notice("total %d Windows test case(s) executed" % (runNum))
def runOneWindows(self, conn, fileName): def runOneWindows(self, conn, fileName):
testModule = self.__dynamicLoadModule(fileName) # testModule = self.__dynamicLoadModule(fileName)
runNum = 0 runNum = 0
for tmp in self.windowsCases: for tmp in self.windowsCases:
...@@ -130,7 +133,7 @@ class TDCases: ...@@ -130,7 +133,7 @@ class TDCases:
tdLog.notice("total %d Cluster test case(s) executed" % (runNum)) tdLog.notice("total %d Cluster test case(s) executed" % (runNum))
def runOneCluster(self, fileName): def runOneCluster(self, fileName):
testModule = self.__dynamicLoadModule(fileName) # testModule = self.__dynamicLoadModule(fileName)
runNum = 0 runNum = 0
for tmp in self.clusterCases: for tmp in self.clusterCases:
......
...@@ -18,6 +18,7 @@ import platform ...@@ -18,6 +18,7 @@ import platform
import pathlib import pathlib
import shutil import shutil
import subprocess import subprocess
import importlib
from time import sleep from time import sleep
from util.log import * from util.log import *
...@@ -580,6 +581,13 @@ class TDDnodes: ...@@ -580,6 +581,13 @@ class TDDnodes:
if index < 1 or index > 10: if index < 1 or index > 10:
tdLog.exit("index:%d should on a scale of [1, 10]" % (index)) tdLog.exit("index:%d should on a scale of [1, 10]" % (index))
def import_module(self,fileName):
module_dir, module_file = os.path.split(fileName)
module_name, module_ext = os.path.splitext(module_file)
spec = importlib.util.spec_from_file_location(module_name,fileName)
uModule = spec.loader.load_module()
return uModule
def stopAll(self): def stopAll(self):
tdLog.info("stop all dnodes") tdLog.info("stop all dnodes")
for i in range(len(self.dnodes)): for i in range(len(self.dnodes)):
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册