taosdShell.py 9.4 KB
Newer Older
haoranc's avatar
haoranc 已提交
1 2 3 4

import taos
import sys
import time
5
from datetime import datetime
haoranc's avatar
haoranc 已提交
6 7 8 9 10 11 12 13 14 15 16 17
import socket
import os
import platform
if platform.system().lower() == 'windows':
    import wexpect as taosExpect
else:
    import pexpect as taosExpect

from util.log import *
from util.sql import *
from util.cases import *
from util.dnodes import *
18
from util.cluster import *
haoranc's avatar
haoranc 已提交
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47

class TDTestCase:
    #updatecfgDict = {'clientCfg': {'serverPort': 7080, 'firstEp': 'trd02:7080', 'secondEp':'trd02:7080'},\
    #                 'serverPort': 7080, 'firstEp': 'trd02:7080'}
    # hostname = socket.gethostname()
    # if (platform.system().lower() == 'windows' and not tdDnodes.dnodes[0].remoteIP == ""):
    #     try:
    #         config = eval(tdDnodes.dnodes[0].remoteIP)
    #         hostname = config["host"]
    #     except Exception:
    #         hostname = tdDnodes.dnodes[0].remoteIP
    # serverPort = '7080'
    # rpcDebugFlagVal = '143'
    # clientCfgDict = {'serverPort': '', 'firstEp': '', 'secondEp':'', 'rpcDebugFlag':'135', 'fqdn':''}
    # clientCfgDict["serverPort"]    = serverPort
    # clientCfgDict["firstEp"]       = hostname + ':' + serverPort
    # clientCfgDict["secondEp"]      = hostname + ':' + serverPort
    # clientCfgDict["rpcDebugFlag"]  = rpcDebugFlagVal
    # clientCfgDict["fqdn"] = hostname

    # updatecfgDict = {'clientCfg': {}, 'serverPort': '', 'firstEp': '', 'secondEp':'', 'rpcDebugFlag':'135', 'fqdn':''}
    # updatecfgDict["clientCfg"]  = clientCfgDict
    # updatecfgDict["serverPort"] = serverPort
    # updatecfgDict["firstEp"]    = hostname + ':' + serverPort
    # updatecfgDict["secondEp"]   = hostname + ':' + serverPort
    # updatecfgDict["fqdn"] = hostname

    # print ("===================: ", updatecfgDict)

48
    def init(self, conn, logSql, replicaVar=1):
49
        self.replicaVar = int(replicaVar)
haoranc's avatar
haoranc 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
        tdLog.debug(f"start to excute {__file__}")
        tdSql.init(conn.cursor())

    def getBuildPath(self):
        selfPath = os.path.dirname(os.path.realpath(__file__))

        if ("community" in selfPath):
            projPath = selfPath[:selfPath.find("community")]
        else:
            projPath = selfPath[:selfPath.find("tests")]

        for root, dirs, files in os.walk(projPath):
            if ("taosd" in files or "taosd.exe" in files):
                rootRealPath = os.path.dirname(os.path.realpath(root))
                if ("packaging" not in rootRealPath):
                    buildPath = root[:len(root) - len("/build/bin")]
                    break
        return buildPath

    def get_process_pid(self,processname):
        #origin artical link:https://blog.csdn.net/weixin_45623536/article/details/122099062
        process_info_list = []
        process = os.popen('ps -A | grep %s'% processname)
        process_info = process.read()
        for i in process_info.split(' '):
            if i != "":
                process_info_list.append(i)
        print(process_info_list)
        if len(process_info_list) != 0 :
            pid = int(process_info_list[0])
        else :
            pid = 0
        return pid

    def checkAndstopPro(self,processName,startAction):
        i = 1
        count = 10
        for i in range(count):
            taosdPid=self.get_process_pid(processName)
            if taosdPid != 0  and   taosdPid != ""  :
                tdLog.info("stop taosd %s ,kill pid :%s "%(startAction,taosdPid))
                os.system("kill -9 %d"%taosdPid) 
                break
            else:
                tdLog.info( "wait start taosd ,times: %d "%i)
H
Hui Li 已提交
95
            time.sleep(1)
haoranc's avatar
haoranc 已提交
96 97 98 99 100 101 102 103
            i+= 1
        else :
            tdLog.exit("taosd %s is not running "%startAction)    

    def taosdCommandStop(self,startAction,taosdCmdRun):
        processName="taosd"
        taosdCmd = taosdCmdRun + startAction
        tdLog.printNoPrefix("%s"%taosdCmd)
104 105
        logTime=datetime.now().strftime('%Y%m%d_%H%M%S_%f')
        os.system(f"nohup {taosdCmd}  >  {logTime}.log  2>&1 &  ")
haoranc's avatar
haoranc 已提交
106
        self.checkAndstopPro(processName,startAction)
107 108
        os.system(f"rm -rf  {logTime}.log")

haoranc's avatar
haoranc 已提交
109 110 111 112 113 114

    def taosdCommandExe(self,startAction,taosdCmdRun):
        taosdCmd = taosdCmdRun + startAction
        tdLog.printNoPrefix("%s"%taosdCmd)
        os.system(f"{taosdCmd}")

115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
    def preData(self):
        # database\stb\tb\chiild-tb\rows\topics
        tdSql.execute("create user testpy pass 'testpy'")
        tdSql.execute("drop database if exists db0;")
        tdSql.execute("create database db0;")
        tdSql.execute("use db0;")
        tdSql.execute("create table if not exists db0.stb (ts timestamp, c1 int, c2 float, c3 double) tags (t1 int unsigned);")
        tdSql.execute("create table db0.ct1 using db0.stb tags(1000);")
        tdSql.execute("create table db0.ct2 using db0.stb tags(2000);")
        tdSql.execute("create table if not exists db0.ntb (ts timestamp, c1 int, c2 float, c3 double) ;")
        tdSql.query("show db0.stables;")
        tdSql.execute("insert into db0.ct1 values(now+0s, 10, 2.0, 3.0);")
        tdSql.execute("insert into db0.ct1 values(now+1s, 11, 2.1, 3.1)(now+2s, 12, 2.2, 3.2)(now+3s, 13, 2.3, 3.3);")
        tdSql.execute("insert into db0.ntb values(now+2s, 10, 2.0, 3.0);")
        tdSql.execute("create sma index sma_index_name1 on db0.stb function(max(c1),max(c2),min(c1)) interval(6m,10s) sliding(6m);")
        tdSql.execute("create topic tpc1 as select * from db0.ct2; ")


        #stream
        tdSql.execute("drop database if exists source_db;")
        tdSql.query("create database source_db vgroups 3;")
        tdSql.query("use source_db")
        tdSql.query("create table if not exists source_db.stb (ts timestamp, k int) tags (a int);")
        tdSql.query("create table source_db.ct1 using source_db.stb tags(1000);create table source_db.ct2 using source_db.stb tags(2000);create table source_db.ct3 using source_db.stb tags(3000);")
        tdSql.query("create stream s1 into source_db.output_stb as select _wstart AS start, min(k), max(k), sum(k) from source_db.stb interval(10m);")


haoranc's avatar
haoranc 已提交
142 143
        #TD-19944 -Q=3 
        tdsqlN=tdCom.newTdSql()
144

haoranc's avatar
haoranc 已提交
145 146
        tdsqlN.query("select * from source_db.stb")
        tdsqlN.query("select * from db0.stb")
147

haoranc's avatar
haoranc 已提交
148
    def run(self):  
149
        # tdSql.prepare()
haoranc's avatar
haoranc 已提交
150
        # time.sleep(2)
151
        self.preData()
haoranc's avatar
haoranc 已提交
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
        #tdLog.info ("hostname: %s" % hostname)

        buildPath = self.getBuildPath()
        if (buildPath == ""):
            tdLog.exit("taosd not found!")
        else:
            tdLog.info("taosd found in %s" % buildPath)
        cfgPath = buildPath + "/../sim/psim/cfg"
        taosdCfgPath = buildPath + "/../sim/dnode1/cfg"


        taosdCmdRun= buildPath + '/build/bin/taosd'
        tdLog.info("cfgPath: %s" % cfgPath)
        # keyDict['h'] = self.hostname
        # keyDict['c'] = cfgPath
        # keyDict['P'] = self.serverPort
168
        tdDnodes=cluster.dnodes
169
        for i in range(len(tdDnodes)):
170 171 172
            tdDnodes[i].stoptaosd()  
        
        
173
        startAction = " -s -c " + taosdCfgPath 
haoranc's avatar
haoranc 已提交
174 175
        tdLog.printNoPrefix("================================ parameter: %s"%startAction)
        self.taosdCommandExe(startAction,taosdCmdRun)
haoranc's avatar
haoranc 已提交
176
        os.system(" rm -rf sdb.json ") 
haoranc's avatar
haoranc 已提交
177 178


179 180 181
        startAction = " --help"
        tdLog.printNoPrefix("================================ parameter: %s"%startAction)
        self.taosdCommandExe(startAction,taosdCmdRun)
haoranc's avatar
haoranc 已提交
182

183 184 185
        startAction = " -h"
        tdLog.printNoPrefix("================================ parameter: %s"%startAction)
        self.taosdCommandExe(startAction,taosdCmdRun)
haoranc's avatar
haoranc 已提交
186

187 188 189 190
        startAction=" -a  jsonFile:./taosdCaseTmp.json"
        tdLog.printNoPrefix("================================ parameter: %s"%startAction)
        os.system("echo \'{\"queryPolicy\":\"3\"}\' > taosdCaseTmp.json")
        self.taosdCommandStop(startAction,taosdCmdRun)
haoranc's avatar
haoranc 已提交
191

192 193 194
        startAction = " -a  jsonFile:./taosdCaseTmp.json -C "
        tdLog.printNoPrefix("================================ parameter: %s"%startAction)
        self.taosdCommandExe(startAction,taosdCmdRun)
haoranc's avatar
haoranc 已提交
195

196
        os.system("rm -rf  taosdCaseTmp.json") 
haoranc's avatar
haoranc 已提交
197

198 199 200
        startAction = " -c " + taosdCfgPath 
        tdLog.printNoPrefix("================================ parameter: %s"%startAction)
        self.taosdCommandStop(startAction,taosdCmdRun)
haoranc's avatar
haoranc 已提交
201 202 203



204 205 206
        startAction = " -e  TAOS_QUERY_POLICY=2 "
        tdLog.printNoPrefix("================================ parameter: %s"%startAction)
        self.taosdCommandStop(startAction,taosdCmdRun)
haoranc's avatar
haoranc 已提交
207

208

209 210
        startAction=" -E taosdCaseTmp/.env"
        tdLog.printNoPrefix("================================ parameter: %s"%startAction)
haoranc's avatar
haoranc 已提交
211
        os.system(" mkdir -p taosdCaseTmp ") 
212 213
        os.system("echo \'TAOS_QUERY_POLICY=3\' > taosdCaseTmp/.env ")
        self.taosdCommandStop(startAction,taosdCmdRun)
214
        os.system(" rm -rf taosdCaseTmp ") 
215

216 217 218
        startAction = " -V"
        tdLog.printNoPrefix("================================ parameter: %s"%startAction)
        self.taosdCommandExe(startAction,taosdCmdRun)
219

220 221 222
        startAction = " -k"
        tdLog.printNoPrefix("================================ parameter: %s"%startAction)
        self.taosdCommandExe(startAction,taosdCmdRun)
haoranc's avatar
haoranc 已提交
223
    
haoranc's avatar
haoranc 已提交
224 225 226 227 228 229
    def stop(self):
        tdSql.close()
        tdLog.success(f"{__file__} successfully executed")

tdCases.addLinux(__file__, TDTestCase())
tdCases.addWindows(__file__, TDTestCase())