taosShell.py 13.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13

import taos
import sys
import time
import socket
import pexpect
import os

from util.log import *
from util.sql import *
from util.cases import *
from util.dnodes import *

P
plum-lihui 已提交
14
def taos_command (key, value, expectString, cfgDir, sqlString='', key1='', value1=''):
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
    if len(key) == 0:
        tdLog.exit("taos test key is null!")

    if len(cfgDir) != 0:
        taosCmd = 'taos -c ' + cfgDir + ' -' + key

    if len(value) != 0:
        if key == 'p':
            taosCmd = taosCmd + value
        else:
            taosCmd = taosCmd + ' ' + value

    if len(key1) != 0:
        taosCmd = taosCmd + ' -' + key1
        if key1 == 'p':
            taosCmd = taosCmd + value1
        else:
            if len(value1) != 0:
                taosCmd = taosCmd + ' ' + value1

    tdLog.info ("taos cmd: %s" % taosCmd)

    child = pexpect.spawn(taosCmd, timeout=3)
    #output = child.readline()
    #print (output.decode())
P
plum-lihui 已提交
40 41 42 43 44
    if len(expectString) != 0:
        i = child.expect([expectString, pexpect.TIMEOUT, pexpect.EOF], timeout=6)
    else:
        i = child.expect([pexpect.TIMEOUT, pexpect.EOF], timeout=6)

45 46 47 48 49
    retResult = child.before.decode()
    print(retResult)
    #print(child.after.decode())
    if i == 0:
        print ('taos login success! Here can run sql, taos> ')
P
plum-lihui 已提交
50 51
        if len(sqlString) != 0:
            child.sendline (sqlString)
52 53 54 55 56 57
            w = child.expect(["Query OK", pexpect.TIMEOUT, pexpect.EOF], timeout=1)
            if w == 0:
                return "TAOS_OK"
            else:
                return "TAOS_FAIL"
        else:
P
plum-lihui 已提交
58 59 60 61
            if key == 'A' or key1 == 'A' or key == 'C' or key1 == 'C':
                return "TAOS_OK", retResult
            else:
                return  "TAOS_OK"
62
    else:
P
plum-lihui 已提交
63
        if key == 'A' or key1 == 'A' or key == 'C' or key1 == 'C':
64 65 66 67 68
            return "TAOS_OK", retResult
        else:
            return "TAOS_FAIL"

class TDTestCase:
P
plum-lihui 已提交
69 70 71 72 73 74 75 76 77
    #updatecfgDict = {'clientCfg': {'serverPort': 7080, 'firstEp': 'trd02:7080', 'secondEp':'trd02:7080'},\
    #                 'serverPort': 7080, 'firstEp': 'trd02:7080'}
    hostname = socket.gethostname()
    serverPort = '7080'
    clientCfgDict = {'serverPort': '', 'firstEp': '', 'secondEp':''}
    clientCfgDict["serverPort"] = serverPort
    clientCfgDict["firstEp"]    = hostname + ':' + serverPort
    clientCfgDict["secondEp"]   = hostname + ':' + serverPort

78

P
plum-lihui 已提交
79 80 81 82 83 84 85
    updatecfgDict = {'clientCfg': {}, 'serverPort': '', 'firstEp': '', 'secondEp':''}
    updatecfgDict["clientCfg"]  = clientCfgDict
    updatecfgDict["serverPort"] = serverPort
    updatecfgDict["firstEp"]    = hostname + ':' + serverPort
    updatecfgDict["secondEp"]   = hostname + ':' + serverPort

    print ("===================: ", updatecfgDict)
86 87 88 89 90 91 92 93 94 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 128 129 130 131 132 133

    def init(self, conn, logSql):
        tdLog.debug(f"start to excute {__file__}")
        tdSql.init(conn.cursor())

    def getBuildPath(self):
        selfPath = os.path.dirname(os.path.realpath(__file__))

        if ("community" in selfPath):
            projPath = selfPath[:selfPath.find("community")]
        else:
            projPath = selfPath[:selfPath.find("tests")]

        for root, dirs, files in os.walk(projPath):
            if ("taosd" in files):
                rootRealPath = os.path.dirname(os.path.realpath(root))
                if ("packaging" not in rootRealPath):
                    buildPath = root[:len(root) - len("/build/bin")]
                    break
        return buildPath

    def run(self):  # sourcery skip: extract-duplicate-method, remove-redundant-fstring
        tdSql.prepare()
        # time.sleep(2)
        tdSql.query("create user testpy pass 'testpy'")

        hostname = socket.gethostname()
        tdLog.info ("hostname: %s" % hostname)

        buildPath = self.getBuildPath()
        if (buildPath == ""):
            tdLog.exit("taosd not found!")
        else:
            tdLog.info("taosd found in %s" % buildPath)
        cfgPath = buildPath + "/../sim/psim/cfg"
        tdLog.info("cfgPath: %s" % cfgPath)

        checkNetworkStatus = ['0: unavailable', '1: network ok', '2: service ok', '3: service degraded', '4: exiting']
        netrole            = ['client', 'server']

        keyDict = {'h':'', 'P':'6030', 'p':'testpy', 'u':'testpy', 'a':'', 'A':'', 'c':'', 'C':'', 's':'', 'r':'', 'f':'', \
                   'k':'', 't':'', 'n':'', 'l':'1024', 'N':'100', 'V':'', 'd':'db', 'w':'30', '-help':'', '-usage':'', '?':''}

        keyDict['h'] = hostname
        keyDict['c'] = cfgPath

        tdLog.printNoPrefix("================================ parameter: -h")
        newDbName="dbh"
P
plum-lihui 已提交
134 135
        sqlString = 'create database ' + newDbName + ';'
        retCode = taos_command("h", keyDict['h'], "taos>", keyDict['c'], sqlString)
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
        if retCode != "TAOS_OK":
            tdLog.exit("taos -h %s fail"%keyDict['h'])
        else:
            #dataDbName = ["information_schema", "performance_schema", "db", newDbName]
            tdSql.query("show databases")
            #tdSql.getResult("show databases")
            for i in range(tdSql.queryRows):
                if tdSql.getData(i, 0) == newDbName:
                    break
            else:
                tdLog.exit("create db fail after taos -h %s fail"%keyDict['h'])

            tdSql.query('drop database %s'%newDbName)

        tdLog.printNoPrefix("================================ parameter: -P")
        #tdDnodes.stop(1)
        #sleep(3)
        #tdDnodes.start(1)
        #sleep(3)
        #keyDict['P'] = 6030
        newDbName = "dbpp"
P
plum-lihui 已提交
157 158
        sqlString = 'create database ' + newDbName + ';'
        retCode = taos_command("P", keyDict['P'], "taos>", keyDict['c'], sqlString)
159 160 161 162 163 164 165 166 167 168 169 170 171 172
        if retCode != "TAOS_OK":
            tdLog.exit("taos -P %s fail"%keyDict['P'])
        else:
            tdSql.query("show databases")
            for i in range(tdSql.queryRows):
                if tdSql.getData(i, 0) == newDbName:
                    break
            else:
                tdLog.exit("create db fail after taos -P %s fail"%keyDict['P'])

            tdSql.query('drop database %s'%newDbName)

        tdLog.printNoPrefix("================================ parameter: -u")
        newDbName="dbu"
P
plum-lihui 已提交
173 174
        sqlString = 'create database ' + newDbName + ';'
        retCode = taos_command("u", keyDict['u'], "taos>", keyDict['c'], sqlString, "p", keyDict['p'])
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
        if retCode != "TAOS_OK":
            tdLog.exit("taos -u %s -p%s fail"%(keyDict['u'], keyDict['p']))
        else:
            tdSql.query("show databases")
            for i in range(tdSql.queryRows):
                if tdSql.getData(i, 0) == newDbName:
                    break
            else:
                tdLog.exit("create db fail after taos -u %s -p%s fail"%(keyDict['u'], keyDict['p']))

            tdSql.query('drop database %s'%newDbName)

        tdLog.printNoPrefix("================================ parameter: -A")
        newDbName="dbaa"
        retCode, retVal = taos_command("p", keyDict['p'], "taos>", keyDict['c'], '', "A", '')
        if retCode != "TAOS_OK":
            tdLog.exit("taos -A fail")
P
plum-lihui 已提交
192 193 194
                
        sqlString = 'create database ' + newDbName + ';'
        retCode = taos_command("u", keyDict['u'], "taos>", keyDict['c'], sqlString, 'a', retVal)
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 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
        if retCode != "TAOS_OK":
            tdLog.exit("taos -u %s -a %s"%(keyDict['u'], retVal))

        tdSql.query("show databases")
        for i in range(tdSql.queryRows):
            if tdSql.getData(i, 0) == newDbName:
                break
        else:
            tdLog.exit("create db fail after taos -u %s -a %s fail"%(keyDict['u'], retVal))

        tdSql.query('drop database %s'%newDbName)

        tdLog.printNoPrefix("================================ parameter: -s")
        newDbName="dbss"
        keyDict['s'] = "\"create database " + newDbName + "\""
        retCode = taos_command("s", keyDict['s'], "Query OK", keyDict['c'], '', '', '')
        if retCode != "TAOS_OK":
            tdLog.exit("taos -s fail")

        print ("========== check new db ==========")
        tdSql.query("show databases")        
        for i in range(tdSql.queryRows):
            if tdSql.getData(i, 0) == newDbName:
                break
        else:
            tdLog.exit("create db fail after taos -s %s fail"%(keyDict['s']))

        keyDict['s'] = "\"create table " + newDbName + ".stb (ts timestamp, c int) tags (t int)\""
        retCode = taos_command("s", keyDict['s'], "Query OK", keyDict['c'], '', '', '')
        if retCode != "TAOS_OK":
            tdLog.exit("taos -s create table fail")

        keyDict['s'] = "\"create table " + newDbName + ".ctb0 using " + newDbName + ".stb tags (0) " + newDbName + ".ctb1 using " + newDbName + ".stb tags (1)\""
        retCode = taos_command("s", keyDict['s'], "Query OK", keyDict['c'], '', '', '')
        if retCode != "TAOS_OK":
            tdLog.exit("taos -s create table fail")

        keyDict['s'] = "\"insert into " + newDbName + ".ctb0 values('2021-04-01 08:00:00.000', 10)('2021-04-01 08:00:01.000', 20) " + newDbName + ".ctb1 values('2021-04-01 08:00:00.000', 11)('2021-04-01 08:00:01.000', 21)\""
        retCode = taos_command("s", keyDict['s'], "Query OK", keyDict['c'], '', '', '')
        if retCode != "TAOS_OK":
            tdLog.exit("taos -s insert data fail")

        sqlString = "select * from " + newDbName + ".ctb0"    
        tdSql.query(sqlString)
        tdSql.checkData(0, 0, '2021-04-01 08:00:00.000')
        tdSql.checkData(0, 1, 10)
        tdSql.checkData(1, 0, '2021-04-01 08:00:01.000')
        tdSql.checkData(1, 1, 20)
        sqlString = "select * from " + newDbName + ".ctb1"    
        tdSql.query(sqlString)
        tdSql.checkData(0, 0, '2021-04-01 08:00:00.000')
        tdSql.checkData(0, 1, 11)
        tdSql.checkData(1, 0, '2021-04-01 08:00:01.000')
        tdSql.checkData(1, 1, 21)
P
plum-lihui 已提交
249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296
        
        keyDict['s'] = "\"select * from " + newDbName + ".ctb0\""
        retCode = taos_command("s", keyDict['s'], "2021-04-01 08:00:01.000", keyDict['c'], '', '', '')
        if retCode != "TAOS_OK":
            tdLog.exit("taos -r show fail")
        
        tdLog.printNoPrefix("================================ parameter: -r")
        keyDict['s'] = "\"select * from " + newDbName + ".ctb0\""
        retCode = taos_command("s", keyDict['s'], "1617235200000", keyDict['c'], '', 'r', '')
        if retCode != "TAOS_OK":
            tdLog.exit("taos -r show fail")

        keyDict['s'] = "\"select * from " + newDbName + ".ctb1\""
        retCode = taos_command("s", keyDict['s'], "1617235201000", keyDict['c'], '', 'r', '')
        if retCode != "TAOS_OK":
            tdLog.exit("taos -r show fail")
        
        tdSql.query('drop database %s'%newDbName)
 
        tdLog.printNoPrefix("================================ parameter: -f")
        pwd=os.getcwd()
        newDbName="dbf"
        sqlFile = pwd + "/0-others/sql.txt"
        sql1 = "echo 'create database " + newDbName + "' > " + sqlFile
        sql2 = "echo 'use " + newDbName + "' >> " + sqlFile
        sql3 = "echo 'create table ntbf (ts timestamp, c binary(40))' >> " + sqlFile
        sql4 = "echo 'insert into ntbf values (\"2021-04-01 08:00:00.000\", \"test taos -f1\")(\"2021-04-01 08:00:01.000\", \"test taos -f2\")' >> " + sqlFile 
        sql5 = "echo 'show databases' >> " + sqlFile       
        os.system(sql1)       
        os.system(sql2)       
        os.system(sql3)       
        os.system(sql4)      
        os.system(sql5)

        keyDict['f'] = pwd + "/0-others/sql.txt"
        retCode = taos_command("f", keyDict['f'], 'performance_schema', keyDict['c'], '', '', '')
        print("============ ret code: ", retCode)
        if retCode != "TAOS_OK":
            tdLog.exit("taos -s fail")

        print ("========== check new db ==========")
        tdSql.query("show databases")        
        for i in range(tdSql.queryRows):
            #print ("dbseq: %d, dbname: %s"%(i, tdSql.getData(i, 0)))
            if tdSql.getData(i, 0) == newDbName:
                break
        else:
            tdLog.exit("create db fail after taos -f fail")
297

P
plum-lihui 已提交
298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
        sqlString = "select * from " + newDbName + ".ntbf"    
        tdSql.query(sqlString)
        tdSql.checkData(0, 0, '2021-04-01 08:00:00.000')
        tdSql.checkData(0, 1, 'test taos -f1')
        tdSql.checkData(1, 0, '2021-04-01 08:00:01.000')
        tdSql.checkData(1, 1, 'test taos -f2')
        
        shellCmd = "rm -f " + sqlFile
        os.system(shellCmd)
        tdSql.query('drop database %s'%newDbName)

        tdLog.printNoPrefix("================================ parameter: -C")
        newDbName="dbcc"
        retCode, retVal = taos_command("C", keyDict['C'], "buildinfo", keyDict['c'], '', '', '')
        if retCode != "TAOS_OK":
            tdLog.exit("taos -C fail")
314

P
plum-lihui 已提交
315 316
        print ("-C return content:\n ", retVal)
                
317 318 319 320 321 322 323 324


    def stop(self):
        tdSql.close()
        tdLog.success(f"{__file__} successfully executed")

tdCases.addLinux(__file__, TDTestCase())
tdCases.addWindows(__file__, TDTestCase())