test.py 8.4 KB
Newer Older
P
plum-lihui 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#!/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
wafwerar's avatar
wafwerar 已提交
20 21
import base64
import json
wafwerar's avatar
wafwerar 已提交
22
import platform
wafwerar's avatar
wafwerar 已提交
23
import socket
P
plum-lihui 已提交
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
from distutils.log import warn as printf
from fabric2 import Connection
sys.path.append("../pytest")
from util.log import *
from util.dnodes import *
from util.cases import *

import taos


if __name__ == "__main__":
    
    fileName = "all"
    deployPath = ""
    masterIp = ""
    testCluster = False
    valgrind = 0
41
    killValgrind = 1
P
plum-lihui 已提交
42 43 44 45
    logSql = True
    stop = 0
    restart = False
    windows = 0
wafwerar's avatar
wafwerar 已提交
46 47
    if platform.system().lower() == 'windows':
        windows = 1
wafwerar's avatar
wafwerar 已提交
48
    updateCfgDict = {}
wafwerar's avatar
wafwerar 已提交
49
    execCmd = ""
50 51
    opts, args = getopt.gnu_getopt(sys.argv[1:], 'f:p:m:l:scghrd:k:e:', [
        'file=', 'path=', 'master', 'logSql', 'stop', 'cluster', 'valgrind', 'help', 'restart', 'updateCfgDict', 'killv', 'execCmd'])
P
plum-lihui 已提交
52 53 54 55 56 57 58 59 60 61 62 63
    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 已提交
64
            tdLog.printNoPrefix('-d update cfg dict, base64 json str')
65
            tdLog.printNoPrefix('-k not kill valgrind processer')
wafwerar's avatar
wafwerar 已提交
66
            tdLog.printNoPrefix('-e eval str to run')
P
plum-lihui 已提交
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 95 96 97 98
            sys.exit(0)

        if key in ['-r', '--restart']: 
            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 已提交
99 100 101 102 103 104
        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 已提交
105

106 107 108
        if key in ['-k', '--killValgrind']:
            killValgrind = 0

wafwerar's avatar
wafwerar 已提交
109 110 111 112 113 114 115 116 117
        if key in ['-e', '--execCmd']:
            try:
                execCmd = base64.b64decode(value.encode()).decode()
            except:
                print('updateCfgDict convert fail.')
                sys.exit(0)

    if not execCmd == "":
        tdDnodes.init(deployPath)
wafwerar's avatar
wafwerar 已提交
118
        print(execCmd)
wafwerar's avatar
wafwerar 已提交
119 120 121
        exec(execCmd)
        quit()

P
plum-lihui 已提交
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
    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')
    
    if masterIp == "":
wafwerar's avatar
wafwerar 已提交
153
        host = socket.gethostname()
P
plum-lihui 已提交
154
    else:
wafwerar's avatar
wafwerar 已提交
155 156 157 158 159
        try:
            config = eval(masterIp)
            host = config["host"]
        except Exception as r:
            host = masterIp
P
plum-lihui 已提交
160 161 162 163 164

    tdLog.info("Procedures for tdengine deployed in %s" % (host))
    if windows:
        tdCases.logSql(logSql)
        tdLog.info("Procedures for testing self-deployment")
wafwerar's avatar
wafwerar 已提交
165
        tdDnodes.init(deployPath, masterIp)
wafwerar's avatar
wafwerar 已提交
166 167 168 169 170 171
        tdDnodes.setTestCluster(testCluster)
        tdDnodes.setValgrind(valgrind)
        tdDnodes.stopAll()
        key_word = 'tdCases.addWindows'
        is_test_framework = 0
        try:
wafwerar's avatar
wafwerar 已提交
172
            if key_word in open(fileName, encoding='UTF-8').read():
wafwerar's avatar
wafwerar 已提交
173
                is_test_framework = 1
wafwerar's avatar
wafwerar 已提交
174 175
        except Exception as r:
            print(r)
wafwerar's avatar
wafwerar 已提交
176 177 178 179 180 181 182 183 184
        updateCfgDictStr = ''
        if is_test_framework:
            moduleName = fileName.replace(".py", "").replace(os.sep, ".")
            uModule = importlib.import_module(moduleName)
            try:
                ucase = uModule.TDTestCase()
                if ((json.dumps(updateCfgDict) == '{}') and (ucase.updatecfgDict is not None)):
                    updateCfgDict = ucase.updatecfgDict
                    updateCfgDictStr = "-d %s"%base64.b64encode(json.dumps(updateCfgDict).encode()).decode()
wafwerar's avatar
wafwerar 已提交
185 186
            except Exception as r:
                print(r)
wafwerar's avatar
wafwerar 已提交
187 188 189
        else:
            pass
        tdDnodes.deploy(1,updateCfgDict)
wafwerar's avatar
wafwerar 已提交
190
        tdDnodes.start(1)
P
plum-lihui 已提交
191 192
        conn = taos.connect(
            host="%s"%(host),
wafwerar's avatar
wafwerar 已提交
193 194 195 196 197
            config=tdDnodes.sim.getCfgDir())
        if is_test_framework:
            tdCases.runOneWindows(conn, fileName)
        else:
            tdCases.runAllWindows(conn)
P
plum-lihui 已提交
198
    else:
199
        tdDnodes.setKillValgrind(killValgrind)
wafwerar's avatar
wafwerar 已提交
200
        tdDnodes.init(deployPath, masterIp)
P
plum-lihui 已提交
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
        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
        except:
            pass
        if is_test_framework:
            moduleName = fileName.replace(".py", "").replace("/", ".")
            uModule = importlib.import_module(moduleName)
            try:
                ucase = uModule.TDTestCase()
wafwerar's avatar
wafwerar 已提交
216 217 218 219 220
                if (json.dumps(updateCfgDict) == '{}'):
                    updateCfgDict = ucase.updatecfgDict
            except:
                pass
        tdDnodes.deploy(1,updateCfgDict)
P
plum-lihui 已提交
221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
        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:
                tdCases.runOneLinux(conn, fileName)
        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")
    conn.close()