test.py 5.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
#!/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/linux/python2/

# -*- coding: utf-8 -*-
import sys
import getopt
18
import subprocess
Y
TD-1705  
yihaoDeng 已提交
19
import time
20 21
from distutils.log import warn as printf

22 23 24 25 26 27 28 29 30 31 32 33
from util.log import *
from util.dnodes import *
from util.cases import *

import taos


if __name__ == "__main__":
    fileName = "all"
    deployPath = ""
    masterIp = ""
    testCluster = False
34
    valgrind = 0
S
Shuduo Sang 已提交
35
    logSql = True
36
    stop = 0
Y
TD-1705  
yihaoDeng 已提交
37 38
    restart = False
    opts, args = getopt.gnu_getopt(sys.argv[1:], 'f:p:m:l:scghr', [
S
Shuduo Sang 已提交
39
        'file=', 'path=', 'master', 'logSql', 'stop', 'cluster', 'valgrind', 'help'])
40 41 42 43 44 45 46
    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')
S
Shuduo Sang 已提交
47
            tdLog.printNoPrefix('-l <True:False> logSql Flag')
48
            tdLog.printNoPrefix('-s stop All dnodes')
S
Shuduo Sang 已提交
49 50
            tdLog.printNoPrefix('-c Test Cluster Flag')
            tdLog.printNoPrefix('-g valgrind Test Flag')
Y
TD-1705  
yihaoDeng 已提交
51
            tdLog.printNoPrefix('-r taosd restart test')
52
            sys.exit(0)
53

Y
TD-1705  
yihaoDeng 已提交
54 55 56
        if key in ['-r', '--restart']: 
            restart = True

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

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

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

S
Shuduo Sang 已提交
66 67 68 69 70 71 72 73 74
        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)

75 76
        if key in ['-c', '--cluster']:
            testCluster = True
77 78 79 80

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

81
        if key in ['-s', '--stop']:
82 83 84 85 86 87 88 89
            stop = 1

    if (stop != 0):
        if (valgrind == 0):
            toBeKilled = "taosd"
        else:
            toBeKilled = "valgrind.bin"

90
        killCmd = "ps -ef|grep -w %s| grep -v grep | awk '{print $2}' | xargs kill -HUP > /dev/null 2>&1" % toBeKilled
91 92 93 94

        psCmd = "ps -ef|grep -w %s| grep -v grep | awk '{print $2}'" % toBeKilled
        processID = subprocess.check_output(psCmd, shell=True)

95
        while(processID):
96 97 98 99
            os.system(killCmd)
            time.sleep(1)
            processID = subprocess.check_output(psCmd, shell=True)

100 101 102 103 104
        for port in range(6030, 6041):
            usePortPID = "lsof -i tcp:%d | grep LISTEn | awk '{print $2}'" % port
            processID = subprocess.check_output(usePortPID, shell=True)

            if processID:
105
                killCmd = "kill -TERM %s" % processID
106 107 108 109 110
                os.system(killCmd)
            fuserCmd = "fuser -k -n tcp %d" % port
            os.system(fuserCmd)
        if valgrind:
            time.sleep(2)
111

S
Shuduo Sang 已提交
112 113
        tdLog.info('stop All dnodes')
        sys.exit(0)
114

115 116 117 118 119 120 121 122
    tdDnodes.init(deployPath)
    tdDnodes.setTestCluster(testCluster)
    tdDnodes.setValgrind(valgrind)

    tdDnodes.stopAll()
    tdDnodes.deploy(1)
    tdDnodes.start(1)

123
    if masterIp == "":
124
        host = '127.0.0.1'
125
    else:
126
        host = masterIp
127

128
    tdLog.info("Procedures for tdengine deployed in %s" % (host))
129

S
Shuduo Sang 已提交
130 131
    tdCases.logSql(logSql)

132
    if testCluster:
133
        tdLog.info("Procedures for testing cluster")
134 135
        if fileName == "all":
            tdCases.runAllCluster()
136
        else:
137
            tdCases.runOneCluster(fileName)
138
    else:
139
        tdLog.info("Procedures for testing self-deployment")
140
        conn = taos.connect(
141 142
            host,
            config=tdDnodes.getSimCfgPath())
143
        if fileName == "all":
144
            tdCases.runAllLinux(conn)
145
        else:
146
            tdCases.runOneLinux(conn, fileName)
Y
TD-1705  
yihaoDeng 已提交
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161
    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)            
                conn = taos.connect( host, config=tdDnodes.getSimCfgPath())
                tdLog.info("Procedures for tdengine deployed in %s" % (host))
                tdLog.info("query test after taosd restart")
                tdCases.runOneLinux(conn, sp[0] + "_" + "restart.py")
            else:
                tdLog.info("not need to query")
162
    conn.close()