taosShellError.py 13.0 KB
Newer Older
1 2 3 4 5

import taos
import sys
import time
import socket
wafwerar's avatar
wafwerar 已提交
6 7 8 9 10
import platform
if platform.system().lower() == 'windows':
    import wexpect as taosExpect
else:
    import pexpect as taosExpect
11 12 13 14 15 16 17 18 19 20
import os

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

def taos_command (buildPath, key, value, expectString, cfgDir, sqlString='', key1='', value1=''):
    if len(key) == 0:
        tdLog.exit("taos test key is null!")
G
Ganlin Zhao 已提交
21

wafwerar's avatar
wafwerar 已提交
22 23 24 25 26
    if platform.system().lower() == 'windows':
        taosCmd = buildPath + '\\build\\bin\\taos.exe '
        taosCmd = taosCmd.replace('\\','\\\\')
    else:
        taosCmd = buildPath + '/build/bin/taos '
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46
    if len(cfgDir) != 0:
        taosCmd = taosCmd + '-c ' + cfgDir

    taosCmd = taosCmd + ' -' + 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)

wafwerar's avatar
wafwerar 已提交
47
    child = taosExpect.spawn(taosCmd, timeout=3)
48 49 50
    #output = child.readline()
    #print (output.decode())
    if len(expectString) != 0:
P
plum-lihui 已提交
51
        i = child.expect([expectString, taosExpect.TIMEOUT, taosExpect.EOF], timeout=20)
52
    else:
P
plum-lihui 已提交
53
        i = child.expect([taosExpect.TIMEOUT, taosExpect.EOF], timeout=20)
54

wafwerar's avatar
wafwerar 已提交
55 56 57 58
    if platform.system().lower() == 'windows':
        retResult = child.before
    else:
        retResult = child.before.decode()
59
    print("cmd return result:\n%s\n"%retResult)
P
plum-lihui 已提交
60
    # print(child.after.decode())
61 62 63 64
    if i == 0:
        print ('taos login success! Here can run sql, taos> ')
        if len(sqlString) != 0:
            child.sendline (sqlString)
P
plum-lihui 已提交
65
            w = child.expect(["Query OK", taosExpect.TIMEOUT, taosExpect.EOF], timeout=10)
wafwerar's avatar
wafwerar 已提交
66 67 68 69
            if platform.system().lower() == 'windows':
                retResult = child.before
            else:
                retResult = child.before.decode()
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
            if w == 0:
                return "TAOS_OK", retResult
            else:
                return "TAOS_FAIL", retResult
        else:
            if key == 'A' or key1 == 'A' or key == 'C' or key1 == 'C' or key == 'V' or key1 == 'V':
                return "TAOS_OK", retResult
            else:
                return  "TAOS_OK", retResult
    else:
        if key == 'A' or key1 == 'A' or key == 'C' or key1 == 'C' or key == 'V' or key1 == 'V':
            return "TAOS_OK", retResult
        else:
            return "TAOS_FAIL", retResult

class TDTestCase:
    #updatecfgDict = {'clientCfg': {'serverPort': 7080, 'firstEp': 'trd02:7080', 'secondEp':'trd02:7080'},\
    #                 'serverPort': 7080, 'firstEp': 'trd02:7080'}
    hostname = socket.gethostname()
wafwerar's avatar
wafwerar 已提交
89
    if (platform.system().lower() == 'windows' and not tdDnodes.dnodes[0].remoteIP == ""):
wafwerar's avatar
wafwerar 已提交
90 91 92 93 94
        try:
            config = eval(tdDnodes.dnodes[0].remoteIP)
            hostname = config["host"]
        except Exception:
            hostname = tdDnodes.dnodes[0].remoteIP
95 96
    serverPort = '7080'
    rpcDebugFlagVal = '143'
P
plum-lihui 已提交
97
    clientCfgDict = {'serverPort': '', 'firstEp': '', 'secondEp':'', 'rpcDebugFlag':'135', 'fqdn':''}
98 99 100 101
    clientCfgDict["serverPort"]    = serverPort
    clientCfgDict["firstEp"]       = hostname + ':' + serverPort
    clientCfgDict["secondEp"]      = hostname + ':' + serverPort
    clientCfgDict["rpcDebugFlag"]  = rpcDebugFlagVal
P
plum-lihui 已提交
102
    clientCfgDict["fqdn"] = hostname
103

P
plum-lihui 已提交
104
    updatecfgDict = {'clientCfg': {}, 'serverPort': '', 'firstEp': '', 'secondEp':'', 'rpcDebugFlag':'135', 'fqdn':''}
105 106 107 108
    updatecfgDict["clientCfg"]  = clientCfgDict
    updatecfgDict["serverPort"] = serverPort
    updatecfgDict["firstEp"]    = hostname + ':' + serverPort
    updatecfgDict["secondEp"]   = hostname + ':' + serverPort
P
plum-lihui 已提交
109
    updatecfgDict["fqdn"] = hostname
110 111 112

    print ("===================: ", updatecfgDict)

113
    def init(self, conn, logSql, replicaVar=1):
114 115 116 117 118 119 120 121 122 123 124 125
        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):
wafwerar's avatar
wafwerar 已提交
126
            if ("taosd" in files or "taosd.exe" in files):
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 153 154 155 156 157 158 159 160 161 162 163 164
                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'] = self.hostname
        keyDict['c'] = cfgPath
        keyDict['P'] = self.serverPort

        tdLog.printNoPrefix("================================ parameter: -h wiht error value")
        #newDbName="dbh"
        #sqlString = 'create database ' + newDbName + ';'
        keyDict['h'] = 'abc'
        retCode, retVal = taos_command(buildPath, "h", keyDict['h'], "taos>", keyDict['c'], '')
        if (retCode == "TAOS_FAIL") and ("Unable to establish connection" in retVal):
C
Cary Xu 已提交
165
            tdLog.info("taos -h %s test success"%keyDict['h'])
166 167 168 169 170 171
        else:
            tdLog.exit("taos -h %s fail"%keyDict['h'])

        keyDict['h'] = '\'abc\''
        retCode, retVal = taos_command(buildPath, "h", keyDict['h'], "taos>", keyDict['c'], '')
        if (retCode == "TAOS_FAIL") and ("Unable to establish connection" in retVal):
C
Cary Xu 已提交
172
            tdLog.info("taos -h %s test success"%keyDict['h'])
173 174 175 176 177 178
        else:
            tdLog.exit("taos -h %s fail"%keyDict['h'])

        keyDict['h'] = '3'
        retCode, retVal = taos_command(buildPath, "h", keyDict['h'], "taos>", keyDict['c'], '')
        if (retCode == "TAOS_FAIL") and ("Unable to establish connection" in retVal):
C
Cary Xu 已提交
179
            tdLog.info("taos -h %s test success"%keyDict['h'])
180 181 182 183 184 185
        else:
            tdLog.exit("taos -h %s fail"%keyDict['h'])

        keyDict['h'] = '\'3\''
        retCode, retVal = taos_command(buildPath, "h", keyDict['h'], "taos>", keyDict['c'], '')
        if (retCode == "TAOS_FAIL") and ("Unable to establish connection" in retVal):
C
Cary Xu 已提交
186
            tdLog.info("taos -h %s test success"%keyDict['h'])
187 188 189 190 191 192 193 194 195
        else:
            tdLog.exit("taos -h %s fail"%keyDict['h'])

        tdLog.printNoPrefix("================================ parameter: -P wiht error value")
        #newDbName="dbh"
        #sqlString = 'create database ' + newDbName + ';'
        keyDict['P'] = 'abc'
        retCode, retVal = taos_command(buildPath, "P", keyDict['P'], "taos>", keyDict['c'], '')
        if (retCode == "TAOS_FAIL") and ("Invalid port" in retVal):
C
Cary Xu 已提交
196
            tdLog.info("taos -P %s test success"%keyDict['P'])
197 198 199 200 201 202
        else:
            tdLog.exit("taos -P %s fail"%keyDict['P'])

        keyDict['P'] = '\'abc\''
        retCode, retVal = taos_command(buildPath, "P", keyDict['P'], "taos>", keyDict['c'], '')
        if (retCode == "TAOS_FAIL") and ("Invalid port" in retVal):
C
Cary Xu 已提交
203
            tdLog.info("taos -P %s test success"%keyDict['P'])
204 205 206 207 208 209
        else:
            tdLog.exit("taos -P %s fail"%keyDict['P'])

        keyDict['P'] = '3'
        retCode, retVal = taos_command(buildPath, "P", keyDict['P'], "taos>", keyDict['c'], '')
        if (retCode == "TAOS_FAIL") and ("Unable to establish connection" in retVal):
C
Cary Xu 已提交
210
            tdLog.info("taos -P %s test success"%keyDict['P'])
211 212 213 214 215 216
        else:
            tdLog.exit("taos -P %s fail"%keyDict['P'])

        keyDict['P'] = '\'3\''
        retCode, retVal = taos_command(buildPath, "P", keyDict['P'], "taos>", keyDict['c'], '')
        if (retCode == "TAOS_FAIL") and ("Unable to establish connection" in retVal):
C
Cary Xu 已提交
217
            tdLog.info("taos -P %s test success"%keyDict['P'])
218 219 220 221 222 223
        else:
            tdLog.exit("taos -P %s fail"%keyDict['P'])

        keyDict['P'] = '12ab'
        retCode, retVal = taos_command(buildPath, "P", keyDict['P'], "taos>", keyDict['c'], '')
        if (retCode == "TAOS_FAIL") and ("Unable to establish connection" in retVal):
C
Cary Xu 已提交
224
            tdLog.info("taos -P %s test success"%keyDict['P'])
225 226 227 228 229 230
        else:
            tdLog.exit("taos -P %s fail"%keyDict['P'])

        keyDict['P'] = '\'12ab\''
        retCode, retVal = taos_command(buildPath, "P", keyDict['P'], "taos>", keyDict['c'], '')
        if (retCode == "TAOS_FAIL") and ("Unable to establish connection" in retVal):
C
Cary Xu 已提交
231
            tdLog.info("taos -P %s test success"%keyDict['P'])
232 233
        else:
            tdLog.exit("taos -P %s fail"%keyDict['P'])
G
Ganlin Zhao 已提交
234

235 236 237 238
        tdLog.printNoPrefix("================================ parameter: -f with error sql ")
        pwd=os.getcwd()
        newDbName="dbf"
        sqlFile = pwd + "/0-others/sql.txt"
wafwerar's avatar
wafwerar 已提交
239 240
        sql1 = "echo create database " + newDbName + " > " + sqlFile
        sql2 = "echo use " + newDbName + " >> " + sqlFile
wafwerar's avatar
wafwerar 已提交
241 242
        if platform.system().lower() == 'windows':
            sql3 = "echo create table ntbf (ts timestamp, c binary(40)) no this item >> " + sqlFile
G
Ganlin Zhao 已提交
243
            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
wafwerar's avatar
wafwerar 已提交
244
        else:
wafwerar's avatar
wafwerar 已提交
245
            sql3 = "echo 'create table ntbf (ts timestamp, c binary(40)) no this item' >> " + sqlFile
G
Ganlin Zhao 已提交
246
            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
X
Xiaoyu Wang 已提交
247
        sql5 = "echo show databases >> " + sqlFile
G
Ganlin Zhao 已提交
248 249 250 251
        os.system(sql1)
        os.system(sql2)
        os.system(sql3)
        os.system(sql4)
252 253 254 255 256 257 258 259 260
        os.system(sql5)

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

        print ("========== check new db ==========")
X
Xiaoyu Wang 已提交
261
        tdSql.query("select * from information_schema.ins_databases")
262 263 264 265 266 267 268
        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")

G
Ganlin Zhao 已提交
269
        sqlString = "select * from " + newDbName + ".ntbf"
270
        tdSql.error(sqlString)
G
Ganlin Zhao 已提交
271

272 273 274 275 276 277 278 279 280 281 282 283
        shellCmd = "rm -f " + sqlFile
        os.system(shellCmd)

        keyDict['f'] = pwd + "/0-others/noexistfile.txt"
        retCode, retVal = taos_command(buildPath, "f", keyDict['f'], 'failed to open file', keyDict['c'], '', '', '')
        #print("============ ret code: ", retCode)
        if retCode != "TAOS_OK":
            tdLog.exit("taos -f fail")

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

        tdLog.printNoPrefix("================================ parameter: -a with error value")
G
Ganlin Zhao 已提交
284 285
        #newDbName="dba"
        errorPassword = 'errorPassword'
286 287 288 289 290 291
        sqlString = 'create database ' + newDbName + ';'
        retCode, retVal = taos_command(buildPath, "u", keyDict['u'], "taos>", keyDict['c'], sqlString, 'a', errorPassword)
        if retCode != "TAOS_FAIL":
            tdLog.exit("taos -u %s -a %s"%(keyDict['u'], errorPassword))

        tdLog.printNoPrefix("================================ parameter: -p with error value")
G
Ganlin Zhao 已提交
292 293
        #newDbName="dba"
        keyDict['p'] = 'errorPassword'
294 295
        retCode, retVal = taos_command(buildPath, "u", keyDict['u'], "taos>", keyDict['c'], sqlString, 'p', keyDict['p'])
        if retCode == "TAOS_FAIL" and "Authentication failure" in retVal:
C
Cary Xu 已提交
296
            tdLog.info("taos -p %s test success"%keyDict['p'])
297 298 299 300 301 302 303 304 305 306
        else:
            tdLog.exit("taos -u %s -p %s"%(keyDict['u'], keyDict['p']))


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

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