未验证 提交 3d843d32 编写于 作者: S slguan 提交者: GitHub

Merge pull request #1686 from taosdata/make-python-test-dynamically-load-module

make python test framework dynamically load module
...@@ -17,6 +17,7 @@ from util.log import * ...@@ -17,6 +17,7 @@ from util.log import *
from util.cases import * from util.cases import *
from util.sql import * from util.sql import *
class TDTestCase: class TDTestCase:
def init(self, conn): def init(self, conn):
tdLog.debug("start to execute %s" % __file__) tdLog.debug("start to execute %s" % __file__)
...@@ -24,25 +25,24 @@ class TDTestCase: ...@@ -24,25 +25,24 @@ class TDTestCase:
def run(self): def run(self):
tdSql.prepare() tdSql.prepare()
tdSql.execute('show databases')
tdSql.execute('drop database if exists db') ret = tdSql.execute('create table tb (ts timestamp, speed int)')
tdSql.execute('create database db')
tdSql.execute('use db')
tdSql.execute('create table tb (ts timestamp, speed int)')
insertRows = 10 insertRows = 10
tdLog.info("insert %d rows" % (insertRows)) tdLog.info("insert %d rows" % (insertRows))
for i in range(0, insertRows): for i in range(0, insertRows):
tdSql.execute('insert into tb values (now + %dm, %d)' % (i, i)) ret = tdSql.execute(
'insert into tb values (now + %dm, %d)' %
(i, i))
# tdLog.info("insert earlier data") tdLog.info("insert earlier data")
# tdSql.execute('insert into tb values (now - 5m , 10)') tdSql.execute('insert into tb values (now - 5m , 10)')
# tdSql.execute('insert into tb values (now - 6m , 10)') tdSql.execute('insert into tb values (now - 6m , 10)')
# tdSql.execute('insert into tb values (now - 7m , 10)') tdSql.execute('insert into tb values (now - 7m , 10)')
# tdSql.execute('insert into tb values (now - 8m , 10)') tdSql.execute('insert into tb values (now - 8m , 10)')
tdSql.query("select * from tb") tdSql.query("select * from tb")
tdSql.checkRows(insertRows) tdSql.checkRows(insertRows + 4)
def stop(self): def stop(self):
tdSql.close() tdSql.close()
......
#!/bin/bash #!/bin/bash
python2 ./test.py -f insert/basic.py $1 python3 ./test.py -f insert/basic.py $1
python2 ./test.py -s $1 python3 ./test.py -s $1
...@@ -24,8 +24,6 @@ from util.cases import * ...@@ -24,8 +24,6 @@ from util.cases import *
import taos import taos
# add testcase here:
from insert.basic import *
if __name__ == "__main__": if __name__ == "__main__":
fileName = "all" fileName = "all"
...@@ -35,7 +33,7 @@ if __name__ == "__main__": ...@@ -35,7 +33,7 @@ if __name__ == "__main__":
valgrind = 0 valgrind = 0
stop = 0 stop = 0
opts, args = getopt.gnu_getopt(sys.argv[1:], 'f:p:m:scgh', [ opts, args = getopt.gnu_getopt(sys.argv[1:], 'f:p:m:scgh', [
'file=', 'path=', 'master', 'stop', 'cluster', 'valgrind', 'help']) 'file=', 'path=', 'master', 'stop', 'cluster', 'valgrind', 'help'])
for key, value in opts: for key, value in opts:
if key in ['-h', '--help']: if key in ['-h', '--help']:
tdLog.printNoPrefix( tdLog.printNoPrefix(
...@@ -72,13 +70,13 @@ if __name__ == "__main__": ...@@ -72,13 +70,13 @@ if __name__ == "__main__":
toBeKilled = "valgrind.bin" toBeKilled = "valgrind.bin"
killCmd = "ps -ef|grep -w %s| grep -v grep | awk '{print $2}' | xargs kill -HUP " % toBeKilled killCmd = "ps -ef|grep -w %s| grep -v grep | awk '{print $2}' | xargs kill -HUP " % toBeKilled
os.system(killCmd) # os.system(killCmd)
time.sleep(1) # time.sleep(1)
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)
...@@ -87,6 +85,7 @@ if __name__ == "__main__": ...@@ -87,6 +85,7 @@ if __name__ == "__main__":
if masterIp == "": if masterIp == "":
tdDnodes.init(deployPath) tdDnodes.init(deployPath)
tdDnodes.setTestCluster(testCluster)
tdDnodes.setValgrind(valgrind) tdDnodes.setValgrind(valgrind)
if testCluster: if testCluster:
......
...@@ -15,6 +15,8 @@ import sys ...@@ -15,6 +15,8 @@ import sys
import os import os
import time import time
import datetime import datetime
import inspect
import importlib
from util.log import * from util.log import *
...@@ -30,6 +32,10 @@ class TDCases: ...@@ -30,6 +32,10 @@ class TDCases:
self.windowsCases = [] self.windowsCases = []
self.clusterCases = [] self.clusterCases = []
def __dynamicLoadModule(self, fileName):
moduleName = fileName.replace(".py", "").replace("/", ".")
return importlib.import_module(moduleName, package='..')
def addWindows(self, name, case): def addWindows(self, name, case):
self.windowsCases.append(TDCase(name, case)) self.windowsCases.append(TDCase(name, case))
...@@ -40,64 +46,93 @@ class TDCases: ...@@ -40,64 +46,93 @@ class TDCases:
self.clusterCases.append(TDCase(name, case)) self.clusterCases.append(TDCase(name, case))
def runAllLinux(self, conn): def runAllLinux(self, conn):
tdLog.notice("run total %d cases" % (len(self.linuxCases))) # TODO: load all Linux cases here
for case in self.linuxCases: runNum = 0
case.case.init(conn) for tmp in self.linuxCases:
case.case.run() if tmp.name.find(fileName) != -1:
case.case.stop() case = testModule.TDTestCase()
tdLog.notice("total %d cases executed" % (len(self.linuxCases))) case.init(conn)
case.run()
case.stop()
runNum += 1
continue
tdLog.notice("total %d Linux test case(s) executed" % (runNum))
def runOneLinux(self, conn, fileName): def runOneLinux(self, conn, fileName):
tdLog.notice("run cases like %s" % (fileName)) testModule = self.__dynamicLoadModule(fileName)
runNum = 0 runNum = 0
for case in self.linuxCases: for tmp in self.linuxCases:
if case.name.find(fileName) != -1: if tmp.name.find(fileName) != -1:
case.case.init(conn) case = testModule.TDTestCase()
case.case.run() case.init(conn)
case.case.stop() case.run()
time.sleep(5) case.stop()
runNum += 1 runNum += 1
tdLog.notice("total %d cases executed" % (runNum)) continue
tdLog.notice("total %d Linux test case(s) executed" % (runNum))
def runAllWindows(self, conn): def runAllWindows(self, conn):
tdLog.notice("run total %d cases" % (len(self.windowsCases))) # TODO: load all Windows cases here
for case in self.windowsCases: runNum = 0
case.case.init(conn) for tmp in self.windowsCases:
case.case.run() if tmp.name.find(fileName) != -1:
case.case.stop() case = testModule.TDTestCase()
tdLog.notice("total %d cases executed" % (len(self.windowsCases))) case.init(conn)
case.run()
case.stop()
runNum += 1
continue
tdLog.notice("total %d Windows test case(s) executed" % (runNum))
def runOneWindows(self, conn, fileName): def runOneWindows(self, conn, fileName):
tdLog.notice("run cases like %s" % (fileName)) testModule = self.__dynamicLoadModule(fileName)
runNum = 0 runNum = 0
for case in self.windowsCases: for tmp in self.windowsCases:
if case.name.find(fileName) != -1: if tmp.name.find(fileName) != -1:
case.case.init(conn) case = testModule.TDTestCase()
case.case.run() case.init(conn)
case.case.stop() case.run()
time.sleep(2) case.stop()
runNum += 1 runNum += 1
tdLog.notice("total %d cases executed" % (runNum)) continue
tdLog.notice("total %d Windows case(s) executed" % (runNum))
def runAllCluster(self): def runAllCluster(self):
tdLog.notice("run total %d cases" % (len(self.clusterCases))) # TODO: load all cluster case module here
for case in self.clusterCases:
case.case.init() runNum = 0
case.case.run() for tmp in self.clusterCases:
case.case.stop() if tmp.name.find(fileName) != -1:
tdLog.notice("total %d cases executed" % (len(self.clusterCases))) tdLog.notice("run cases like %s" % (fileName))
case = testModule.TDTestCase()
case.init()
case.run()
case.stop()
runNum += 1
continue
tdLog.notice("total %d Cluster test case(s) executed" % (runNum))
def runOneCluster(self, fileName): def runOneCluster(self, fileName):
tdLog.notice("run cases like %s" % (fileName)) testModule = self.__dynamicLoadModule(fileName)
runNum = 0 runNum = 0
for case in self.clusterCases: for tmp in self.clusterCases:
if case.name.find(fileName) != -1: if tmp.name.find(fileName) != -1:
case.case.init() tdLog.notice("run cases like %s" % (fileName))
case.case.run() case = testModule.TDTestCase()
case.case.stop() case.init()
time.sleep(2) case.run()
case.stop()
runNum += 1 runNum += 1
tdLog.notice("total %d cases executed" % (runNum)) continue
tdLog.notice("total %d Cluster test case(s) executed" % (runNum))
tdCases = TDCases() tdCases = TDCases()
...@@ -30,9 +30,6 @@ class TDSimClient: ...@@ -30,9 +30,6 @@ class TDSimClient:
if os.system(cmd) != 0: if os.system(cmd) != 0:
tdLog.exit(cmd) tdLog.exit(cmd)
def setValgrind(self, value):
self.valgrind = value
def deploy(self): def deploy(self):
self.logDir = "%s/sim/psim/log" % (self.path,) self.logDir = "%s/sim/psim/log" % (self.path,)
self.cfgDir = "%s/sim/psim/cfg" % (self.path) self.cfgDir = "%s/sim/psim/cfg" % (self.path)
...@@ -82,11 +79,15 @@ class TDDnode: ...@@ -82,11 +79,15 @@ class TDDnode:
self.index = index self.index = index
self.running = 0 self.running = 0
self.deployed = 0 self.deployed = 0
self.testCluster = False
self.valgrind = 0 self.valgrind = 0
def init(self, path): def init(self, path):
self.path = path self.path = path
def setTestCluster(self, value):
self.testCluster = value
def setValgrind(self, value): def setValgrind(self, value):
self.valgrind = value self.valgrind = value
...@@ -124,7 +125,9 @@ class TDDnode: ...@@ -124,7 +125,9 @@ class TDDnode:
if os.system(cmd) != 0: if os.system(cmd) != 0:
tdLog.exit(cmd) tdLog.exit(cmd)
self.startIP() if self.testCluster:
self.startIP()
self.cfg("masterIp", "192.168.0.1") self.cfg("masterIp", "192.168.0.1")
self.cfg("secondIp", "192.168.0.2") self.cfg("secondIp", "192.168.0.2")
self.cfg("publicIp", "192.168.0.%d" % (self.index)) self.cfg("publicIp", "192.168.0.%d" % (self.index))
...@@ -292,11 +295,15 @@ class TDDnodes: ...@@ -292,11 +295,15 @@ class TDDnodes:
self.sim.init(self.path) self.sim.init(self.path)
self.sim.deploy() self.sim.deploy()
def setTestCluster(self, value):
self.testCluster = value
def setValgrind(self, value): def setValgrind(self, value):
self.valgrind = value self.valgrind = value
def deploy(self, index): def deploy(self, index):
self.check(index) self.check(index)
self.dnodes[index - 1].setTestCluster(self.testCluster)
self.dnodes[index - 1].setValgrind(self.valgrind) self.dnodes[index - 1].setValgrind(self.valgrind)
self.dnodes[index - 1].deploy() self.dnodes[index - 1].deploy()
...@@ -318,11 +325,15 @@ class TDDnodes: ...@@ -318,11 +325,15 @@ class TDDnodes:
def startIP(self, index): def startIP(self, index):
self.check(index) self.check(index)
self.dnodes[index - 1].startIP()
if self.testCluster:
self.dnodes[index - 1].startIP()
def stopIP(self, index): def stopIP(self, index):
self.check(index) self.check(index)
self.dnodes[index - 1].stopIP()
if self.dnodes[index - 1].testCluster:
self.dnodes[index - 1].stopIP()
def check(self, index): def check(self, index):
if index < 1 or index > 10: if index < 1 or index > 10:
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册