test.py 9.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
#!/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 -*-
import sys
import getopt
import subprocess
import time
from distutils.log import warn as printf
from fabric2 import Connection

from util.log import *
from util.dnodes import *
from util.cases import *
26
from util.dockerNodes import *
27 28 29 30 31 32 33 34 35 36 37 38 39

import taos


if __name__ == "__main__":
    fileName = "all"
    deployPath = ""
    masterIp = ""
    testCluster = False
    valgrind = 0
    logSql = True
    stop = 0
    restart = False
40 41
    docker = False
    dataDir = "/data"
42
    windows = 0
43 44
    opts, args = getopt.gnu_getopt(sys.argv[1:], 'f:d:p:m:l:scghrw', [
        'file=', 'docker=', 'path=', 'master', 'logSql', 'stop', 'cluster', 'valgrind', 'help', 'windows'])
45 46 47 48 49
    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')
50
            tdLog.printNoPrefix('-d docker cluster test')
51 52 53 54 55 56 57
            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')
58
            tdLog.printNoPrefix('-w taos on windows')            
59 60
            sys.exit(0)

61
        if key in ['-r', '--restart']:
62 63 64
            restart = True

        if key in ['-f', '--file']:
65 66 67
            fileName = os.path.normpath(value)
            print(fileName)
        
68 69 70 71
        if key in ['-d', '--docker']:
            fileName = os.path.normpath(value)            
            docker = True

72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93
        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']:
94
            stop = 1        
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127

        if key in ['-w', '--windows']:
            windows = 1

    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):
            usePortPID = "lsof -i tcp:%d | grep LISTEn | awk '{print $2}'" % port
            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)

        tdLog.info('stop All dnodes')
128

129 130 131
    if masterIp == "":
        host = '127.0.0.1'
    else:
132
        host = masterIp    
133

134
    tdLog.info("Procedures for tdengine deployed in %s" % (host)) 
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
    if docker:        
        tdCases.logSql(logSql)
        tdLog.info("Procedures for testing self-deployment")             
        is_test_framework = 0
        key_word = 'tdCases.addLinux'
        try:
            if key_word in open(fileName).read():
                is_test_framework = 1
        except BaseException:
            pass
        if is_test_framework:
            moduleName = fileName.replace(".py", "").replace(os.sep, ".")
            uModule = importlib.import_module(moduleName)
            try:
                ucase = uModule.TDTestCase()                
                numOfNodes = ucase.updatecfgDict.get('numOfNodes')                
                cluster.init(numOfNodes, dataDir)                    
                cluster.prepardBuild()
153 154 155
                
                startArbitrator = False
                arbitratorHost = "tdnode1"
156 157 158 159 160
                for i in range(numOfNodes):
                    if ucase.updatecfgDict.get('%d' % (i + 1)) != None:
                        config = dict (ucase.updatecfgDict.get('%d' % (i + 1)))
                        print(config)
                        for key, value in config.items():
161 162 163 164
                            if key == "arbitrator":
                                startArbitrator = True
                                arbitratorHost = value
                            cluster.cfg(key, value, i + 1)                      
165
                cluster.run()
166 167 168
                if startArbitrator:
                    hostname=value.split(":")[0]
                    cluster.startArbitrator(hostname)
169 170 171 172 173 174 175
                conn = cluster.conn
            except Exception as e:
                print(e.args)
                print(str(e))
                exit(1)
        tdCases.runOneLinux(conn, fileName)
    elif windows:
176 177 178 179
        tdCases.logSql(logSql)
        tdLog.info("Procedures for testing self-deployment")
        td_clinet = TDSimClient("C:\\TDengine")
        td_clinet.deploy()
180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
        if masterIp == "" or masterIp == "localhost":
            tdDnodes.init(deployPath)
            tdDnodes.setTestCluster(testCluster)
            tdDnodes.setValgrind(valgrind)
            tdDnodes.stopAll()
            is_test_framework = 0
            key_word = 'tdCases.addWindows'
            try:
                if key_word in open(fileName).read():
                    is_test_framework = 1
            except:
                pass
            if is_test_framework:
                moduleName = fileName.replace(".py", "").replace(os.sep, ".")
                uModule = importlib.import_module(moduleName)
                try:
                    ucase = uModule.TDTestCase()
                    tdDnodes.deploy(1,ucase.updatecfgDict)
                except :
                    tdDnodes.deploy(1,{})
            else:
                pass
                tdDnodes.deploy(1,{})
            tdDnodes.startWin(1)
        else:
            remote_conn = Connection("root@%s"%host)
            with remote_conn.cd('/var/lib/jenkins/workspace/TDinternal/community/tests/pytest'):
                remote_conn.run("python3 ./test.py")
208
        conn = taos.connect(
209
            host="%s" % (host),
210 211
            config=td_clinet.cfgDir)
        tdCases.runOneWindows(conn, fileName)
212 213
        tdCases.logSql(logSql)

214 215 216 217 218 219 220 221 222 223
    else:
        tdDnodes.init(deployPath)
        tdDnodes.setTestCluster(testCluster)
        tdDnodes.setValgrind(valgrind)
        tdDnodes.stopAll()
        is_test_framework = 0
        key_word = 'tdCases.addLinux'
        try:
            if key_word in open(fileName).read():
                is_test_framework = 1
224
        except BaseException:
225 226
            pass
        if is_test_framework:
227
            moduleName = fileName.replace(".py", "").replace(os.sep, ".")
228 229 230
            uModule = importlib.import_module(moduleName)
            try:
                ucase = uModule.TDTestCase()
231 232 233
                tdDnodes.deploy(1, ucase.updatecfgDict)
            except BaseException:
                tdDnodes.deploy(1, {})
234 235
        else:
            pass
236
            tdDnodes.deploy(1, {})
237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
        tdDnodes.start(1)

        tdCases.logSql(logSql)

        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")
            conn = taos.connect(
                host,
                config=tdDnodes.getSimCfgPath())
            if fileName == "all":
                tdCases.runAllLinux(conn)
            else:
255
                tdCases.runOneLinux(conn, fileName)
256 257 258
        if restart:
            if fileName == "all":
                tdLog.info("not need to query ")
259
            else:
260 261 262 263
                sp = fileName.rsplit(".", 1)
                if len(sp) == 2 and sp[1] == "py":
                    tdDnodes.stopAll()
                    tdDnodes.start(1)
264 265 266 267 268
                    time.sleep(1)
                    conn = taos.connect(host, config=tdDnodes.getSimCfgPath())
                    tdLog.info(
                        "Procedures for tdengine deployed in %s" %
                        (host))
269 270 271 272 273
                    tdLog.info("query test after taosd restart")
                    tdCases.runOneLinux(conn, sp[0] + "_" + "restart.py")
                else:
                    tdLog.info("not need to query")
    conn.close()