user_manage.py 7.9 KB
Newer Older
J
jiacy-jcy 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
###################################################################
#           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
#
###################################################################

# -*- coding: utf-8 -*-

import taos
15
from taos.tmq import *
J
jiacy-jcy 已提交
16 17
from util.cases import *
from util.common import *
18 19
from util.log import *
from util.sql import *
J
jiacy-jcy 已提交
20
from util.sqlset import *
21

J
jiacy-jcy 已提交
22 23 24 25 26 27 28 29

class TDTestCase:
    def init(self, conn, logSql, replicaVar=1):
        self.replicaVar = int(replicaVar)
        tdLog.debug("start to execute %s" % __file__)
        tdSql.init(conn.cursor())
        self.setsql = TDSetSql()
        self.stbname = 'stb'
30
        self.binary_length = 20  # the length of binary for column_dict
J
jiacy-jcy 已提交
31 32
        self.nchar_length = 20  # the length of nchar for column_dict
        self.column_dict = {
33
            'ts': 'timestamp',
J
jiacy-jcy 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
            'col1': 'tinyint',
            'col2': 'smallint',
            'col3': 'int',
            'col4': 'bigint',
            'col5': 'tinyint unsigned',
            'col6': 'smallint unsigned',
            'col7': 'int unsigned',
            'col8': 'bigint unsigned',
            'col9': 'float',
            'col10': 'double',
            'col11': 'bool',
            'col12': f'binary({self.binary_length})',
            'col13': f'nchar({self.nchar_length})'
        }
        self.tag_dict = {
49
            'ts_tag': 'timestamp',
J
jiacy-jcy 已提交
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
            't1': 'tinyint',
            't2': 'smallint',
            't3': 'int',
            't4': 'bigint',
            't5': 'tinyint unsigned',
            't6': 'smallint unsigned',
            't7': 'int unsigned',
            't8': 'bigint unsigned',
            't9': 'float',
            't10': 'double',
            't11': 'bool',
            't12': f'binary({self.binary_length})',
            't13': f'nchar({self.nchar_length})'
        }
        self.tag_list = [
            f'now,1,2,3,4,5,6,7,8,9.9,10.1,true,"abcd","涛思数据"'
        ]
        self.values_list = [
            f'now,1,2,3,4,5,6,7,8,9.9,10.1,true,"abcd","涛思数据"'
        ]
        self.tbnum = 1
71

J
jiacy-jcy 已提交
72
    def prepare_data(self):
73
        tdSql.execute(self.setsql.set_create_stable_sql(self.stbname, self.column_dict, self.tag_dict))
J
jiacy-jcy 已提交
74 75 76 77
        for i in range(self.tbnum):
            tdSql.execute(f'create table {self.stbname}_{i} using {self.stbname} tags({self.tag_list[i]})')
            for j in self.values_list:
                tdSql.execute(f'insert into {self.stbname}_{i} values({j})')
78

J
jiacy-jcy 已提交
79
    def create_user(self):
80 81
        for user_name in ['jiacy1_all', 'jiacy1_read', 'jiacy1_write', 'jiacy1_none', 'jiacy0_all', 'jiacy0_read',
                          'jiacy0_write', 'jiacy0_none']:
J
jiacy-jcy 已提交
82 83 84 85
            if 'jiacy1' in user_name.lower():
                tdSql.execute(f'create user {user_name} pass "123" sysinfo 1')
            elif 'jiacy0' in user_name.lower():
                tdSql.execute(f'create user {user_name} pass "123" sysinfo 0')
86
        for user_name in ['jiacy1_all', 'jiacy1_read', 'jiacy0_all', 'jiacy0_read']:
J
jiacy-jcy 已提交
87
            tdSql.execute(f'grant read on db to {user_name}')
88
        for user_name in ['jiacy1_all', 'jiacy1_write', 'jiacy0_all', 'jiacy0_write']:
J
jiacy-jcy 已提交
89 90 91
            tdSql.execute(f'grant write on db to {user_name}')

    def user_privilege_check(self):
92
        jiacy1_read_conn = taos.connect(user='jiacy1_read', password='123')
J
jiacy-jcy 已提交
93 94 95 96 97 98 99 100
        sql = "create table ntb (ts timestamp,c0 int)"
        expectErrNotOccured = True
        try:
            jiacy1_read_conn.execute(sql)
        except BaseException:
            expectErrNotOccured = False
        if expectErrNotOccured:
            caller = inspect.getframeinfo(inspect.stack()[1][0])
101
            tdLog.exit(f"{caller.filename}({caller.lineno}) failed: sql:{sql}, expect error not occured")
J
jiacy-jcy 已提交
102 103 104 105 106 107
        else:
            self.queryRows = 0
            self.queryCols = 0
            self.queryResult = None
            tdLog.info(f"sql:{sql}, expect error occured")
        pass
108

J
jiacy-jcy 已提交
109
    def drop_topic(self):
110 111 112 113 114 115 116 117
        jiacy1_all_conn = taos.connect(user='jiacy1_all', password='123')
        jiacy1_read_conn = taos.connect(user='jiacy1_read', password='123')
        jiacy1_write_conn = taos.connect(user='jiacy1_write', password='123')
        jiacy1_none_conn = taos.connect(user='jiacy1_none', password='123')
        jiacy0_all_conn = taos.connect(user='jiacy0_all', password='123')
        jiacy0_read_conn = taos.connect(user='jiacy0_read', password='123')
        jiacy0_write_conn = taos.connect(user='jiacy0_write', password='123')
        jiacy0_none_conn = taos.connect(user='jiacy0_none', password='123')
118
        tdSql.execute('alter database db wal_retention_period 3600')
J
jiacy-jcy 已提交
119
        tdSql.execute('create topic root_db as select * from db.stb')
120
        for user in [jiacy1_all_conn, jiacy1_read_conn, jiacy0_all_conn, jiacy0_read_conn]:
J
jiacy-jcy 已提交
121 122
            user.execute(f'create topic db_jiacy as select * from db.stb')
            user.execute('drop topic db_jiacy')
123 124
        for user in [jiacy1_write_conn, jiacy1_none_conn, jiacy0_write_conn, jiacy0_none_conn, jiacy1_all_conn,
                     jiacy1_read_conn, jiacy0_all_conn, jiacy0_read_conn]:
J
jiacy-jcy 已提交
125
            sql_list = []
126
            if user in [jiacy1_all_conn, jiacy1_read_conn, jiacy0_all_conn, jiacy0_read_conn]:
J
jiacy-jcy 已提交
127
                sql_list = ['drop topic root_db']
128 129
            elif user in [jiacy1_write_conn, jiacy1_none_conn, jiacy0_write_conn, jiacy0_none_conn]:
                sql_list = ['drop topic root_db', 'create topic db_jiacy as select * from db.stb']
J
jiacy-jcy 已提交
130 131 132 133 134 135 136 137
            for sql in sql_list:
                expectErrNotOccured = True
                try:
                    user.execute(sql)
                except BaseException:
                    expectErrNotOccured = False
                if expectErrNotOccured:
                    caller = inspect.getframeinfo(inspect.stack()[1][0])
138
                    tdLog.exit(f"{caller.filename}({caller.lineno}) failed: sql:{sql}, expect error not occured")
J
jiacy-jcy 已提交
139 140 141 142 143
                else:
                    self.queryRows = 0
                    self.queryCols = 0
                    self.queryResult = None
                    tdLog.info(f"sql:{sql}, expect error occured")
144

J
jiacy-jcy 已提交
145 146
    def tmq_commit_cb_print(tmq, resp, param=None):
        print(f"commit: {resp}, tmq: {tmq}, param: {param}")
147

J
jiacy-jcy 已提交
148 149 150 151 152
    def subscribe_topic(self):
        print("create topic")
        tdSql.execute('create topic db_topic as select * from db.stb')
        tdSql.execute('grant subscribe on db_topic to jiacy1_all')
        print("build consumer")
153 154
        tmq = Consumer({"group.id": "tg2", "td.connect.user": "jiacy1_all", "td.connect.pass": "123",
                        "enable.auto.commit": "true"})
J
jiacy-jcy 已提交
155
        print("build topic list")
156
        tmq.subscribe(["db_topic"])
J
jiacy-jcy 已提交
157 158 159 160 161 162 163 164 165
        print("basic consume loop")
        c = 0
        l = 0
        for i in range(10):
            if c > 10:
                break
            res = tmq.poll(10)
            print(f"loop {l}")
            l += 1
166
            if not res:
J
jiacy-jcy 已提交
167
                print(f"received empty message at loop {l} (committed {c})")
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
                continue
            if res.error():
                print(f"consumer error at loop {l} (committed {c}) {res.error()}")
                continue

            c += 1
            topic = res.topic()
            db = res.database()
            print(f"topic: {topic}\ndb: {db}")

            for row in res:
                print(row.fetchall())
            print("* committed")
            tmq.commit(res)

J
jiacy-jcy 已提交
183 184 185 186 187 188 189
    def run(self):
        tdSql.prepare()
        self.create_user()
        self.prepare_data()
        self.drop_topic()
        self.user_privilege_check()
        self.subscribe_topic()
190

J
jiacy-jcy 已提交
191 192 193 194
    def stop(self):
        tdSql.close()
        tdLog.success("%s successfully executed" % __file__)

195

J
jiacy-jcy 已提交
196
tdCases.addWindows(__file__, TDTestCase())
197
tdCases.addLinux(__file__, TDTestCase())