test.py 24.0 KB
Newer Older
P
plum-lihui 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#!/usr/bin/python
###################################################################
#           Copyright (c) 2016 by TAOS Technologies, Inc.
#                     All rights reserved.
#
#  This file is proprietary and confidential to TAOS Technologies.
#  No part of this file may be reproduced, stored, transmitted,
#  disclosed or used in any form or by any means other than as
#  expressly provided by the written permission from Jianhui Tao
#
###################################################################
# install pip
# pip install src/connector/python/

# -*- coding: utf-8 -*-
16
import os
P
plum-lihui 已提交
17 18 19 20
import sys
import getopt
import subprocess
import time
wafwerar's avatar
wafwerar 已提交
21 22
import base64
import json
wafwerar's avatar
wafwerar 已提交
23
import platform
wafwerar's avatar
wafwerar 已提交
24
import socket
wafwerar's avatar
wafwerar 已提交
25
import threading
26
import importlib
C
cpwu 已提交
27 28

import toml
P
plum-lihui 已提交
29 30 31 32
sys.path.append("../pytest")
from util.log import *
from util.dnodes import *
from util.cases import *
haoranc's avatar
haoranc 已提交
33
from util.cluster import *
C
cpwu 已提交
34
from util.taosadapter import *
P
plum-lihui 已提交
35 36

import taos
C
cpwu 已提交
37
import taosrest
P
plum-lihui 已提交
38

wafwerar's avatar
wafwerar 已提交
39 40
def checkRunTimeError():
    import win32gui
wafwerar's avatar
wafwerar 已提交
41
    timeCount = 0
wafwerar's avatar
wafwerar 已提交
42 43
    while 1:
        time.sleep(1)
wafwerar's avatar
wafwerar 已提交
44
        timeCount = timeCount + 1
wafwerar's avatar
wafwerar 已提交
45
        print("checkRunTimeError",timeCount)
wafwerar's avatar
wafwerar 已提交
46
        if (timeCount>600):
wafwerar's avatar
wafwerar 已提交
47
            print("stop the test.")
wafwerar's avatar
wafwerar 已提交
48 49 50
            os.system("TASKKILL /F /IM taosd.exe")
            os.system("TASKKILL /F /IM taos.exe")
            os.system("TASKKILL /F /IM tmq_sim.exe")
wafwerar's avatar
wafwerar 已提交
51
            os.system("TASKKILL /F /IM mintty.exe")
wafwerar's avatar
wafwerar 已提交
52
            os.system("TASKKILL /F /IM python.exe")
wafwerar's avatar
wafwerar 已提交
53
            quit(0)
wafwerar's avatar
wafwerar 已提交
54 55 56
        hwnd = win32gui.FindWindow(None, "Microsoft Visual C++ Runtime Library")
        if hwnd:
            os.system("TASKKILL /F /IM taosd.exe")
P
plum-lihui 已提交
57

58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
# 
# run case on previous cluster
#
def runOnPreviousCluster(host, config, fileName):
    print("enter run on previeous")
    
    # load case module
    sep = "/"
    if platform.system().lower() == 'windows':
        sep = os.sep
    moduleName = fileName.replace(".py", "").replace(sep, ".")
    uModule = importlib.import_module(moduleName)
    case = uModule.TDTestCase()

    # create conn
    conn = taos.connect(host, config)

    # run case
A
Alex Duan 已提交
76
    case.init(conn, False)
77 78 79 80 81 82 83 84 85
    try:
        case.run()
    except Exception as e:
        tdLog.notice(repr(e))
        tdLog.exit("%s failed" % (fileName))
    # stop
    case.stop()


P
plum-lihui 已提交
86
if __name__ == "__main__":
C
cpwu 已提交
87

88 89 90
    #
    #  analysis paramaters
    #
P
plum-lihui 已提交
91 92 93 94 95
    fileName = "all"
    deployPath = ""
    masterIp = ""
    testCluster = False
    valgrind = 0
96
    killValgrind = 1
P
plum-lihui 已提交
97 98 99
    logSql = True
    stop = 0
    restart = False
100
    dnodeNums = 1
haoranc's avatar
haoranc 已提交
101
    mnodeNums = 0
wafwerar's avatar
wafwerar 已提交
102
    updateCfgDict = {}
C
cpwu 已提交
103
    adapter_cfg_dict = {}
wafwerar's avatar
wafwerar 已提交
104
    execCmd = ""
haoranc's avatar
haoranc 已提交
105
    queryPolicy = 1
haoranc's avatar
haoranc 已提交
106
    createDnodeNums = 1
C
cpwu 已提交
107
    restful = False
108
    replicaVar = 1
S
Shengliang Guan 已提交
109
    asan = False
110
    independentMnode = True
111
    previousCluster = False
A
Alex Duan 已提交
112
    opts, args = getopt.gnu_getopt(sys.argv[1:], 'f:p:m:l:scghrd:k:e:N:M:Q:C:RD:n:i:aP', [
113
        'file=', 'path=', 'master', 'logSql', 'stop', 'cluster', 'valgrind', 'help', 'restart', 'updateCfgDict', 'killv', 'execCmd','dnodeNums','mnodeNums','queryPolicy','createDnodeNums','restful','adaptercfgupdate','replicaVar','independentMnode','previous'])
P
plum-lihui 已提交
114 115 116 117 118 119 120 121 122 123 124 125
    for key, value in opts:
        if key in ['-h', '--help']:
            tdLog.printNoPrefix(
                'A collection of test cases written using Python')
            tdLog.printNoPrefix('-f Name of test case file written by Python')
            tdLog.printNoPrefix('-p Deploy Path for Simulator')
            tdLog.printNoPrefix('-m Master Ip for Simulator')
            tdLog.printNoPrefix('-l <True:False> logSql Flag')
            tdLog.printNoPrefix('-s stop All dnodes')
            tdLog.printNoPrefix('-c Test Cluster Flag')
            tdLog.printNoPrefix('-g valgrind Test Flag')
            tdLog.printNoPrefix('-r taosd restart test')
wafwerar's avatar
wafwerar 已提交
126
            tdLog.printNoPrefix('-d update cfg dict, base64 json str')
127
            tdLog.printNoPrefix('-k not kill valgrind processer')
wafwerar's avatar
wafwerar 已提交
128
            tdLog.printNoPrefix('-e eval str to run')
haoranc's avatar
haoranc 已提交
129
            tdLog.printNoPrefix('-N start dnodes numbers in clusters')
haoranc's avatar
haoranc 已提交
130
            tdLog.printNoPrefix('-M create mnode numbers in clusters')
haoranc's avatar
haoranc 已提交
131
            tdLog.printNoPrefix('-Q set queryPolicy in one dnode')
haoranc's avatar
haoranc 已提交
132
            tdLog.printNoPrefix('-C create Dnode Numbers in one cluster')
C
cpwu 已提交
133
            tdLog.printNoPrefix('-R restful realization form')
C
cpwu 已提交
134
            tdLog.printNoPrefix('-D taosadapter update cfg dict ')
135
            tdLog.printNoPrefix('-n the number of replicas')
136
            tdLog.printNoPrefix('-i independentMnode Mnode')
S
Shengliang Guan 已提交
137
            tdLog.printNoPrefix('-a address sanitizer mode')
138
            tdLog.printNoPrefix('-P run case with [P]revious cluster, do not create new cluster to run case.')
139

P
plum-lihui 已提交
140 141
            sys.exit(0)

C
cpwu 已提交
142
        if key in ['-r', '--restart']:
P
plum-lihui 已提交
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
            restart = True

        if key in ['-f', '--file']:
            fileName = value

        if key in ['-p', '--path']:
            deployPath = value

        if key in ['-m', '--master']:
            masterIp = value

        if key in ['-l', '--logSql']:
            if (value.upper() == "TRUE"):
                logSql = True
            elif (value.upper() == "FALSE"):
                logSql = False
            else:
                tdLog.printNoPrefix("logSql value %s is invalid" % logSql)
                sys.exit(0)

        if key in ['-c', '--cluster']:
            testCluster = True

        if key in ['-g', '--valgrind']:
            valgrind = 1

        if key in ['-s', '--stop']:
            stop = 1

wafwerar's avatar
wafwerar 已提交
172 173 174 175 176 177
        if key in ['-d', '--updateCfgDict']:
            try:
                updateCfgDict = eval(base64.b64decode(value.encode()).decode())
            except:
                print('updateCfgDict convert fail.')
                sys.exit(0)
wafwerar's avatar
wafwerar 已提交
178

179 180 181
        if key in ['-k', '--killValgrind']:
            killValgrind = 0

wafwerar's avatar
wafwerar 已提交
182 183 184 185
        if key in ['-e', '--execCmd']:
            try:
                execCmd = base64.b64decode(value.encode()).decode()
            except:
C
cpwu 已提交
186
                print('execCmd run fail.')
wafwerar's avatar
wafwerar 已提交
187 188
                sys.exit(0)

haoranc's avatar
haoranc 已提交
189 190 191
        if key in ['-N', '--dnodeNums']:
            dnodeNums = value

haoranc's avatar
haoranc 已提交
192 193 194
        if key in ['-M', '--mnodeNums']:
            mnodeNums = value

haoranc's avatar
haoranc 已提交
195 196 197
        if key in ['-Q', '--queryPolicy']:
            queryPolicy = value

haoranc's avatar
haoranc 已提交
198 199 200
        if key in ['-C', '--createDnodeNums']:
            createDnodeNums = value

201
        if key in ['-i', '--independentMnode']:
haoranc's avatar
haoranc 已提交
202
            independentMnode = False
203

C
cpwu 已提交
204 205 206
        if key in ['-R', '--restful']:
            restful = True

S
Shengliang Guan 已提交
207 208 209
        if key in ['-a', '--asan']:
            asan = True

C
cpwu 已提交
210 211 212 213 214 215 216
        if key in ['-D', '--adaptercfgupdate']:
            try:
                adaptercfgupdate = eval(base64.b64decode(value.encode()).decode())
            except:
                print('adapter cfg update convert fail.')
                sys.exit(0)

217 218 219
        if key in ['-n', '--replicaVar']:
            replicaVar = value

220 221 222 223 224 225
        if key in ['-P', '--previous']:
            previousCluster = True

    #
    # do exeCmd command
    #
wafwerar's avatar
wafwerar 已提交
226
    if not execCmd == "":
C
cpwu 已提交
227 228 229 230
        if restful:
            tAdapter.init(deployPath)
        else:
            tdDnodes.init(deployPath)
wafwerar's avatar
wafwerar 已提交
231
        print(execCmd)
wafwerar's avatar
wafwerar 已提交
232 233 234
        exec(execCmd)
        quit()

235 236 237
    #
    # do stop option
    #
P
plum-lihui 已提交
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
    if (stop != 0):
        if (valgrind == 0):
            toBeKilled = "taosd"
        else:
            toBeKilled = "valgrind.bin"

        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
        processID = subprocess.check_output(psCmd, shell=True)

        while(processID):
            os.system(killCmd)
            time.sleep(1)
            processID = subprocess.check_output(psCmd, shell=True)

        for port in range(6030, 6041):
C
cpwu 已提交
255
            usePortPID = "lsof -i tcp:%d | grep LISTEN | awk '{print $2}'" % port
P
plum-lihui 已提交
256 257 258 259 260 261 262 263 264 265
            processID = subprocess.check_output(usePortPID, shell=True)

            if processID:
                killCmd = "kill -TERM %s" % processID
                os.system(killCmd)
            fuserCmd = "fuser -k -n tcp %d" % port
            os.system(fuserCmd)
        if valgrind:
            time.sleep(2)

C
cpwu 已提交
266
        if restful:
sangshuduo's avatar
sangshuduo 已提交
267
            toBeKilled = "taosadapter"
C
cpwu 已提交
268

C
cpwu 已提交
269 270
            # killCmd = "ps -ef|grep -w %s| grep -v grep | awk '{print $2}' | xargs kill -TERM > /dev/null 2>&1" % toBeKilled
            killCmd = f"pkill {toBeKilled}"
C
cpwu 已提交
271 272

            psCmd = "ps -ef|grep -w %s| grep -v grep | awk '{print $2}'" % toBeKilled
C
cpwu 已提交
273
            # psCmd = f"pgrep {toBeKilled}"
C
cpwu 已提交
274 275 276 277 278 279 280
            processID = subprocess.check_output(psCmd, shell=True)

            while(processID):
                os.system(killCmd)
                time.sleep(1)
                processID = subprocess.check_output(psCmd, shell=True)

C
cpwu 已提交
281 282 283
            port = 6041
            usePortPID = f"lsof -i tcp:{port} | grep LISTEN | awk '{{print $2}}'"
            processID = subprocess.check_output(usePortPID, shell=True)
C
cpwu 已提交
284

C
cpwu 已提交
285 286 287 288 289
            if processID:
                killCmd = f"kill -TERM {processID}"
                os.system(killCmd)
            fuserCmd = f"fuser -k -n tcp {port}"
            os.system(fuserCmd)
C
cpwu 已提交
290 291 292

            tdLog.info('stop taosadapter')

P
plum-lihui 已提交
293
        tdLog.info('stop All dnodes')
C
cpwu 已提交
294

295 296 297
    #
    # get hostname
    #
P
plum-lihui 已提交
298
    if masterIp == "":
wafwerar's avatar
wafwerar 已提交
299
        host = socket.gethostname()
P
plum-lihui 已提交
300
    else:
wafwerar's avatar
wafwerar 已提交
301 302 303 304 305
        try:
            config = eval(masterIp)
            host = config["host"]
        except Exception as r:
            host = masterIp
P
plum-lihui 已提交
306
    tdLog.info("Procedures for tdengine deployed in %s" % (host))
307 308 309 310 311 312 313 314 315 316 317 318 319

    #
    # do previousCluster option
    #
    if previousCluster:
        tdDnodes.init(deployPath, masterIp)
        runOnPreviousCluster(host, tdDnodes.getSimCfgPath(), fileName)
        tdLog.info("run on previous cluster end.")
        quit()

    #
    # windows run
    #
wafwerar's avatar
wafwerar 已提交
320
    if platform.system().lower() == 'windows':
wafwerar's avatar
wafwerar 已提交
321
        fileName = fileName.replace("/", os.sep)
wafwerar's avatar
wafwerar 已提交
322
        if (masterIp == "" and not fileName == "0-others\\udf_create.py"):
wafwerar's avatar
wafwerar 已提交
323
            threading.Thread(target=checkRunTimeError,daemon=True).start()
P
plum-lihui 已提交
324
        tdLog.info("Procedures for testing self-deployment")
wafwerar's avatar
wafwerar 已提交
325
        tdDnodes.init(deployPath, masterIp)
wafwerar's avatar
wafwerar 已提交
326 327 328 329 330 331
        tdDnodes.setTestCluster(testCluster)
        tdDnodes.setValgrind(valgrind)
        tdDnodes.stopAll()
        key_word = 'tdCases.addWindows'
        is_test_framework = 0
        try:
wafwerar's avatar
wafwerar 已提交
332
            if key_word in open(fileName, encoding='UTF-8').read():
wafwerar's avatar
wafwerar 已提交
333
                is_test_framework = 1
wafwerar's avatar
wafwerar 已提交
334 335
        except Exception as r:
            print(r)
wafwerar's avatar
wafwerar 已提交
336
        updateCfgDictStr = ''
C
cpwu 已提交
337
        # adapter_cfg_dict_str = ''
wafwerar's avatar
wafwerar 已提交
338 339 340 341 342
        if is_test_framework:
            moduleName = fileName.replace(".py", "").replace(os.sep, ".")
            uModule = importlib.import_module(moduleName)
            try:
                ucase = uModule.TDTestCase()
wafwerar's avatar
wafwerar 已提交
343
                if ((json.dumps(updateCfgDict) == '{}') and hasattr(ucase, 'updatecfgDict')):
wafwerar's avatar
wafwerar 已提交
344 345
                    updateCfgDict = ucase.updatecfgDict
                    updateCfgDictStr = "-d %s"%base64.b64encode(json.dumps(updateCfgDict).encode()).decode()
C
cpwu 已提交
346 347 348
                if ((json.dumps(adapter_cfg_dict) == '{}') and hasattr(ucase, 'taosadapter_cfg_dict')):
                    adapter_cfg_dict = ucase.taosadapter_cfg_dict
                    # adapter_cfg_dict_str = f"-D {base64.b64encode(toml.dumps(adapter_cfg_dict).encode()).decode()}"
wafwerar's avatar
wafwerar 已提交
349 350
            except Exception as r:
                print(r)
wafwerar's avatar
wafwerar 已提交
351 352
        else:
            pass
X
Xuefeng Tan 已提交
353 354 355
        # if restful:
        tAdapter.init(deployPath, masterIp)
        tAdapter.stop(force_kill=True)
C
cpwu 已提交
356

wafwerar's avatar
wafwerar 已提交
357 358 359 360
        if dnodeNums == 1 :
            tdDnodes.deploy(1,updateCfgDict)
            tdDnodes.start(1)
            tdCases.logSql(logSql)
C
cpwu 已提交
361 362 363 364
            if restful:
                tAdapter.deploy(adapter_cfg_dict)
                tAdapter.start()

wafwerar's avatar
wafwerar 已提交
365 366
            if queryPolicy != 1:
                queryPolicy=int(queryPolicy)
C
cpwu 已提交
367
                if restful:
X
Xuefeng Tan 已提交
368
                    conn = taosrest.connect(url=f"http://{host}:6041",timezone="utc")
C
cpwu 已提交
369 370 371 372 373 374 375 376 377 378 379
                else:
                    conn = taos.connect(host,config=tdDnodes.getSimCfgPath())

                cursor = conn.cursor()
                cursor.execute("create qnode on dnode 1")
                cursor.execute(f'alter local "queryPolicy" "{queryPolicy}"')
                cursor.execute("show local variables")
                res = cursor.fetchall()
                for i in range(cursor.rowcount):
                    if res[i][0] == "queryPolicy" :
                        if int(res[i][1]) == int(queryPolicy):
haoranc's avatar
haoranc 已提交
380
                            tdLog.info(f'alter queryPolicy to {queryPolicy} successfully')
C
cpwu 已提交
381 382 383
                        else:
                            tdLog.debug(res)
                            tdLog.exit(f"alter queryPolicy to  {queryPolicy} failed")
wafwerar's avatar
wafwerar 已提交
384 385
        else :
            tdLog.debug("create an cluster  with %s nodes and make %s dnode as independent mnode"%(dnodeNums,mnodeNums))
386
            dnodeslist = cluster.configure_cluster(dnodeNums=dnodeNums, mnodeNums=mnodeNums, independentMnode=independentMnode)
wafwerar's avatar
wafwerar 已提交
387 388 389 390 391 392 393 394 395 396
            tdDnodes = ClusterDnodes(dnodeslist)
            tdDnodes.init(deployPath, masterIp)
            tdDnodes.setTestCluster(testCluster)
            tdDnodes.setValgrind(valgrind)
            tdDnodes.stopAll()
            for dnode in tdDnodes.dnodes:
                tdDnodes.deploy(dnode.index,{})
            for dnode in tdDnodes.dnodes:
                tdDnodes.starttaosd(dnode.index)
            tdCases.logSql(logSql)
397
                            
C
cpwu 已提交
398 399 400 401
            if restful:
                tAdapter.deploy(adapter_cfg_dict)
                tAdapter.start()

C
cpwu 已提交
402
            if not restful:
C
cpwu 已提交
403
                conn = taos.connect(host,config=tdDnodes.getSimCfgPath())
C
cpwu 已提交
404
            else:
X
Xuefeng Tan 已提交
405
                conn = taosrest.connect(url=f"http://{host}:6041",timezone="utc")
406
            # tdLog.info(tdDnodes.getSimCfgPath(),host)
haoranc's avatar
haoranc 已提交
407 408 409 410 411
            if createDnodeNums == 1:
                createDnodeNums=dnodeNums
            else:
                createDnodeNums=createDnodeNums
            cluster.create_dnode(conn,createDnodeNums)
412
            cluster.create_mnode(conn,mnodeNums)
wafwerar's avatar
wafwerar 已提交
413 414 415 416 417
            try:
                if cluster.check_dnode(conn) :
                    print("check dnode ready")
            except Exception as r:
                print(r)
418 419 420
            if queryPolicy != 1:
                queryPolicy=int(queryPolicy)
                if restful:
X
Xuefeng Tan 已提交
421
                    conn = taosrest.connect(url=f"http://{host}:6041",timezone="utc")
422 423 424 425 426 427 428 429 430 431 432
                else:
                    conn = taos.connect(host,config=tdDnodes.getSimCfgPath())

                cursor = conn.cursor()
                cursor.execute("create qnode on dnode 1")
                cursor.execute(f'alter local "queryPolicy" "{queryPolicy}"')
                cursor.execute("show local variables")
                res = cursor.fetchall()
                for i in range(cursor.rowcount):
                    if res[i][0] == "queryPolicy" :
                        if int(res[i][1]) == int(queryPolicy):
haoranc's avatar
haoranc 已提交
433
                            tdLog.info(f'alter queryPolicy to {queryPolicy} successfully')
434 435 436 437
                        else:
                            tdLog.debug(res)
                            tdLog.exit(f"alter queryPolicy to  {queryPolicy} failed")
                            
wafwerar's avatar
wafwerar 已提交
438 439 440
        if ucase is not None and hasattr(ucase, 'noConn') and ucase.noConn == True:
            conn = None
        else:
C
cpwu 已提交
441
            if not restful:
C
cpwu 已提交
442
                conn = taos.connect(host="%s"%(host), config=tdDnodes.sim.getCfgDir())
C
cpwu 已提交
443
            else:
X
Xuefeng Tan 已提交
444 445 446 447 448 449 450 451
                conn = taosrest.connect(url=f"http://{host}:6041",timezone="utc")

        if testCluster:
            tdLog.info("Procedures for testing cluster")
            if fileName == "all":
                tdCases.runAllCluster()
            else:
                tdCases.runOneCluster(fileName)
wafwerar's avatar
wafwerar 已提交
452
        else:
X
Xuefeng Tan 已提交
453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481
            tdLog.info("Procedures for testing self-deployment")
            if not restful:
                conn = taos.connect(host,config=tdDnodes.getSimCfgPath())
            else:
                conn = taosrest.connect(url=f"http://{host}:6041",timezone="utc")

            if fileName == "all":
                tdCases.runAllWindows(conn)
            else:
                tdCases.runOneWindows(conn, fileName, replicaVar)

        if restart:
            if fileName == "all":
                tdLog.info("not need to query ")
            else:
                sp = fileName.rsplit(".", 1)
                if len(sp) == 2 and sp[1] == "py":
                    tdDnodes.stopAll()
                    tdDnodes.start(1)
                    time.sleep(1)
                    if not restful:
                        conn = taos.connect( host, config=tdDnodes.getSimCfgPath())
                    else:
                        conn = taosrest.connect(url=f"http://{host}:6041",timezone="utc")
                    tdLog.info("Procedures for tdengine deployed in %s" % (host))
                    tdLog.info("query test after taosd restart")
                    tdCases.runOneWindows(conn, sp[0] + "_" + "restart.py", replicaVar)
                else:
                    tdLog.info("not need to query")
P
plum-lihui 已提交
482
    else:
483
        tdDnodes.setKillValgrind(killValgrind)
wafwerar's avatar
wafwerar 已提交
484
        tdDnodes.init(deployPath, masterIp)
P
plum-lihui 已提交
485 486
        tdDnodes.setTestCluster(testCluster)
        tdDnodes.setValgrind(valgrind)
S
Shengliang Guan 已提交
487
        tdDnodes.setAsan(asan)
P
plum-lihui 已提交
488 489 490 491 492 493 494 495 496 497 498 499 500
        tdDnodes.stopAll()
        is_test_framework = 0
        key_word = 'tdCases.addLinux'
        try:
            if key_word in open(fileName).read():
                is_test_framework = 1
        except:
            pass
        if is_test_framework:
            moduleName = fileName.replace(".py", "").replace("/", ".")
            uModule = importlib.import_module(moduleName)
            try:
                ucase = uModule.TDTestCase()
wafwerar's avatar
wafwerar 已提交
501 502
                if (json.dumps(updateCfgDict) == '{}'):
                    updateCfgDict = ucase.updatecfgDict
C
cpwu 已提交
503 504
                if (json.dumps(adapter_cfg_dict) == '{}'):
                    adapter_cfg_dict = ucase.taosadapter_cfg_dict
wafwerar's avatar
wafwerar 已提交
505 506
            except:
                pass
C
cpwu 已提交
507 508 509 510 511

        if restful:
            tAdapter.init(deployPath, masterIp)
            tAdapter.stop(force_kill=True)

haoranc's avatar
haoranc 已提交
512
        if dnodeNums == 1 :
513
            # dnode is one
haoranc's avatar
haoranc 已提交
514 515 516
            tdDnodes.deploy(1,updateCfgDict)
            tdDnodes.start(1)
            tdCases.logSql(logSql)
C
cpwu 已提交
517 518 519 520 521

            if restful:
                tAdapter.deploy(adapter_cfg_dict)
                tAdapter.start()

haoranc's avatar
haoranc 已提交
522 523
            if queryPolicy != 1:
                queryPolicy=int(queryPolicy)
C
cpwu 已提交
524
                if not restful:
C
cpwu 已提交
525
                    conn = taos.connect(host,config=tdDnodes.getSimCfgPath())
C
cpwu 已提交
526
                else:
X
Xuefeng Tan 已提交
527
                    conn = taosrest.connect(url=f"http://{host}:6041",timezone="utc")
C
cpwu 已提交
528 529 530 531 532 533 534
                # tdSql.init(conn.cursor())
                # tdSql.execute("create qnode on dnode 1")
                # tdSql.execute('alter local "queryPolicy" "%d"'%queryPolicy)
                # tdSql.query("show local variables;")
                # for i in range(tdSql.queryRows):
                #     if tdSql.queryResult[i][0] == "queryPolicy" :
                #         if int(tdSql.queryResult[i][1]) == int(queryPolicy):
haoranc's avatar
haoranc 已提交
535
                #             tdLog.info('alter queryPolicy to %d successfully'%queryPolicy)
C
cpwu 已提交
536 537 538 539 540 541 542 543 544 545 546 547
                #         else :
                #             tdLog.debug(tdSql.queryResult)
                #             tdLog.exit("alter queryPolicy to  %d failed"%queryPolicy)

                cursor = conn.cursor()
                cursor.execute("create qnode on dnode 1")
                cursor.execute(f'alter local "queryPolicy" "{queryPolicy}"')
                cursor.execute("show local variables")
                res = cursor.fetchall()
                for i in range(cursor.rowcount):
                    if res[i][0] == "queryPolicy" :
                        if int(res[i][1]) == int(queryPolicy):
haoranc's avatar
haoranc 已提交
548
                            tdLog.info(f'alter queryPolicy to {queryPolicy} successfully')
C
cpwu 已提交
549 550 551 552
                        else:
                            tdLog.debug(res)
                            tdLog.exit(f"alter queryPolicy to  {queryPolicy} failed")

haoranc's avatar
haoranc 已提交
553
        else :
554
            # dnode > 1 cluster
haoranc's avatar
haoranc 已提交
555
            tdLog.debug("create an cluster  with %s nodes and make %s dnode as independent mnode"%(dnodeNums,mnodeNums))
556
            dnodeslist = cluster.configure_cluster(dnodeNums=dnodeNums, mnodeNums=mnodeNums, independentMnode=independentMnode)
haoranc's avatar
haoranc 已提交
557 558 559 560
            tdDnodes = ClusterDnodes(dnodeslist)
            tdDnodes.init(deployPath, masterIp)
            tdDnodes.setTestCluster(testCluster)
            tdDnodes.setValgrind(valgrind)
S
Shengliang Guan 已提交
561
            tdDnodes.setAsan(asan)
haoranc's avatar
haoranc 已提交
562 563 564 565 566 567
            tdDnodes.stopAll()
            for dnode in tdDnodes.dnodes:
                tdDnodes.deploy(dnode.index,{})
            for dnode in tdDnodes.dnodes:
                tdDnodes.starttaosd(dnode.index)
            tdCases.logSql(logSql)
C
cpwu 已提交
568 569 570 571 572

            if restful:
                tAdapter.deploy(adapter_cfg_dict)
                tAdapter.start()

573
            # create taos connect
C
cpwu 已提交
574
            if not restful:
C
cpwu 已提交
575
                conn = taos.connect(host,config=tdDnodes.getSimCfgPath())
C
cpwu 已提交
576
            else:
X
Xuefeng Tan 已提交
577
                conn = taosrest.connect(url=f"http://{host}:6041",timezone="utc")
haoranc's avatar
haoranc 已提交
578
            print(tdDnodes.getSimCfgPath(),host)
haoranc's avatar
haoranc 已提交
579 580 581 582 583
            if createDnodeNums == 1:
                createDnodeNums=dnodeNums
            else:
                createDnodeNums=createDnodeNums
            cluster.create_dnode(conn,createDnodeNums)
584 585
            cluster.create_mnode(conn,mnodeNums)

haoranc's avatar
haoranc 已提交
586 587 588 589 590
            try:
                if cluster.check_dnode(conn) :
                    print("check dnode ready")
            except Exception as r:
                print(r)
C
cpwu 已提交
591

592
            # do queryPolicy option
593 594 595
            if queryPolicy != 1:
                queryPolicy=int(queryPolicy)
                if restful:
X
Xuefeng Tan 已提交
596
                    conn = taosrest.connect(url=f"http://{host}:6041",timezone="utc")
597 598 599 600 601 602 603 604 605 606 607
                else:
                    conn = taos.connect(host,config=tdDnodes.getSimCfgPath())

                cursor = conn.cursor()
                cursor.execute("create qnode on dnode 1")
                cursor.execute(f'alter local "queryPolicy" "{queryPolicy}"')
                cursor.execute("show local variables")
                res = cursor.fetchall()
                for i in range(cursor.rowcount):
                    if res[i][0] == "queryPolicy" :
                        if int(res[i][1]) == int(queryPolicy):
haoranc's avatar
haoranc 已提交
608
                            tdLog.info(f'alter queryPolicy to {queryPolicy} successfully')
609 610 611 612
                        else:
                            tdLog.debug(res)
                            tdLog.exit(f"alter queryPolicy to  {queryPolicy} failed")
                            
C
cpwu 已提交
613

614
        # run case
P
plum-lihui 已提交
615 616 617 618 619 620 621 622
        if testCluster:
            tdLog.info("Procedures for testing cluster")
            if fileName == "all":
                tdCases.runAllCluster()
            else:
                tdCases.runOneCluster(fileName)
        else:
            tdLog.info("Procedures for testing self-deployment")
C
cpwu 已提交
623
            if not restful:
C
cpwu 已提交
624
                conn = taos.connect(host,config=tdDnodes.getSimCfgPath())
C
cpwu 已提交
625
            else:
X
Xuefeng Tan 已提交
626
                conn = taosrest.connect(url=f"http://{host}:6041",timezone="utc")
C
cpwu 已提交
627

P
plum-lihui 已提交
628 629 630
            if fileName == "all":
                tdCases.runAllLinux(conn)
            else:
631
                tdCases.runOneLinux(conn, fileName, replicaVar)
C
cpwu 已提交
632

633
        # do restart option
P
plum-lihui 已提交
634 635 636
        if restart:
            if fileName == "all":
                tdLog.info("not need to query ")
C
cpwu 已提交
637
            else:
P
plum-lihui 已提交
638 639 640 641
                sp = fileName.rsplit(".", 1)
                if len(sp) == 2 and sp[1] == "py":
                    tdDnodes.stopAll()
                    tdDnodes.start(1)
C
cpwu 已提交
642
                    time.sleep(1)
C
cpwu 已提交
643 644 645
                    if not restful:
                        conn = taos.connect( host, config=tdDnodes.getSimCfgPath())
                    else:
X
Xuefeng Tan 已提交
646
                        conn = taosrest.connect(url=f"http://{host}:6041",timezone="utc")
P
plum-lihui 已提交
647 648
                    tdLog.info("Procedures for tdengine deployed in %s" % (host))
                    tdLog.info("query test after taosd restart")
649
                    tdCases.runOneLinux(conn, sp[0] + "_" + "restart.py", replicaVar)
P
plum-lihui 已提交
650 651
                else:
                    tdLog.info("not need to query")
C
cpwu 已提交
652

653
    # close for end
wafwerar's avatar
wafwerar 已提交
654 655
    if conn is not None:
        conn.close()
S
Shengliang Guan 已提交
656 657
    if asan:
        tdDnodes.StopAllSigint()
S
Shengliang Guan 已提交
658
        tdLog.info("Address sanitizer mode finished")
T
tangfangzhi 已提交
659
    sys.exit(0)