test.py 6.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12
#!/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
13
# pip install src/connector/python/
14 15 16 17

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

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

import taos


if __name__ == "__main__":
    fileName = "all"
    deployPath = ""
    masterIp = ""
    testCluster = False
35
    valgrind = 0
S
Shuduo Sang 已提交
36
    logSql = True
37
    stop = 0
Y
TD-1705  
yihaoDeng 已提交
38
    restart = False
L
liuyq-617 已提交
39
    windows = 0
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54
    opts, args = getopt.gnu_getopt(
        sys.argv[1:],
        "f:p:m:l:scghrw",
        [
            "file=",
            "path=",
            "master",
            "logSql",
            "stop",
            "cluster",
            "valgrind",
            "help",
            "windows",
        ],
    )
55
    for key, value in opts:
56 57 58 59 60 61 62 63 64 65 66
        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")
            tdLog.printNoPrefix("-w taos on windows")
67
            sys.exit(0)
68

69
        if key in ["-r", "--restart"]:
Y
TD-1705  
yihaoDeng 已提交
70 71
            restart = True

72
        if key in ["-f", "--file"]:
73
            fileName = value
74

75
        if key in ["-p", "--path"]:
76
            deployPath = value
77

78
        if key in ["-m", "--master"]:
79
            masterIp = value
80

81 82
        if key in ["-l", "--logSql"]:
            if value.upper() == "TRUE":
S
Shuduo Sang 已提交
83
                logSql = True
84
            elif value.upper() == "FALSE":
S
Shuduo Sang 已提交
85 86 87 88 89
                logSql = False
            else:
                tdLog.printNoPrefix("logSql value %s is invalid" % logSql)
                sys.exit(0)

90
        if key in ["-c", "--cluster"]:
91
            testCluster = True
92

93
        if key in ["-g", "--valgrind"]:
94 95
            valgrind = 1

96
        if key in ["-s", "--stop"]:
97 98
            stop = 1

99
        if key in ["-w", "--windows"]:
L
liuyq-617 已提交
100 101
            windows = 1

102 103
    if stop != 0:
        if valgrind == 0:
104 105 106 107
            toBeKilled = "taosd"
        else:
            toBeKilled = "valgrind.bin"

108 109 110 111
        killCmd = (
            "ps -ef|grep -w %s| grep -v grep | awk '{print $2}' | xargs kill -TERM > /dev/null 2>&1"
            % toBeKilled
        )
112 113 114 115

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

116
        while processID:
117 118 119 120
            os.system(killCmd)
            time.sleep(1)
            processID = subprocess.check_output(psCmd, shell=True)

121 122 123 124 125
        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:
126
                killCmd = "kill -TERM %s" % processID
127 128 129 130 131
                os.system(killCmd)
            fuserCmd = "fuser -k -n tcp %d" % port
            os.system(fuserCmd)
        if valgrind:
            time.sleep(2)
132

133 134
        tdLog.info("stop All dnodes")

135
    if masterIp == "":
136
        host = "127.0.0.1"
137
    else:
138
        host = masterIp
139

140
    tdLog.info("Procedures for tdengine deployed in %s" % (host))
141
    uModule = tdDnodes.import_module(fileName)
L
liuyq-617 已提交
142 143
    if windows:
        tdCases.logSql(logSql)
144
        tdLog.info("Procedures for testing self-deployment")
L
liuyq-617 已提交
145 146
        td_clinet = TDSimClient("C:\\TDengine")
        td_clinet.deploy()
147 148 149 150
        remote_conn = Connection("root@%s" % host)
        with remote_conn.cd(
            "/var/lib/jenkins/workspace/TDinternal/community/tests/pytest"
        ):
151
            remote_conn.run("python3 ./test.py")
152
        conn = taos.connect(host="%s" % (host), config=td_clinet.cfgDir)
L
liuyq-617 已提交
153 154 155 156 157 158 159
        tdCases.runOneWindows(conn, fileName)
    else:
        tdDnodes.init(deployPath)
        tdDnodes.setTestCluster(testCluster)
        tdDnodes.setValgrind(valgrind)
        tdDnodes.stopAll()
        is_test_framework = 0
160
        key_word = "tdCases.addLinux"
L
liuyq-617 已提交
161 162 163 164 165 166 167 168
        try:
            if key_word in open(fileName).read():
                is_test_framework = 1
        except:
            pass
        if is_test_framework:
            try:
                ucase = uModule.TDTestCase()
169 170 171 172
                tdDnodes.deploy(1, ucase.updatecfgDict)
                del uModule
            except:
                tdDnodes.deploy(1, {})
L
liuyq-617 已提交
173 174
        else:
            pass
175
            tdDnodes.deploy(1, {})
L
liuyq-617 已提交
176 177 178 179 180 181 182 183 184 185
        tdDnodes.start(1)

        tdCases.logSql(logSql)

        if testCluster:
            tdLog.info("Procedures for testing cluster")
            if fileName == "all":
                tdCases.runAllCluster()
            else:
                tdCases.runOneCluster(fileName)
186
        else:
L
liuyq-617 已提交
187
            tdLog.info("Procedures for testing self-deployment")
188
            conn = taos.connect(host, config=tdDnodes.getSimCfgPath())
L
liuyq-617 已提交
189 190
            if fileName == "all":
                tdCases.runAllLinux(conn)
Y
TD-1705  
yihaoDeng 已提交
191
            else:
192
                tdCases.runOneLinux(conn, fileName, uModule)
L
liuyq-617 已提交
193 194 195
        if restart:
            if fileName == "all":
                tdLog.info("not need to query ")
196
            else:
L
liuyq-617 已提交
197 198 199 200
                sp = fileName.rsplit(".", 1)
                if len(sp) == 2 and sp[1] == "py":
                    tdDnodes.stopAll()
                    tdDnodes.start(1)
201 202
                    time.sleep(1)
                    conn = taos.connect(host, config=tdDnodes.getSimCfgPath())
L
liuyq-617 已提交
203 204 205 206 207
                    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")
208
    conn.close()