retentionpolicy.py 3.6 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 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
###################################################################
#           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 sys
import os
import datetime
sys.path.insert(0, os.getcwd())
import taos
from util.log import *
from util.cases import *
from util.sql import *
from util.dnodes import *


class TDTestRetetion:
    def init(self):
        self.queryRows=0
        tdLog.debug("start to execute %s" % __file__)
        tdLog.info("prepare cluster")
        tdDnodes.init("")
        tdDnodes.setTestCluster(False)
        tdDnodes.setValgrind(False)
        tdDnodes.stopAll()
        tdDnodes.deploy(1)
        tdDnodes.start(1)
        print(tdDnodes.getDnodesRootDir())
        self.conn = taos.connect(config=tdDnodes.getSimCfgPath())
        tdSql.init(self.conn.cursor())
        tdSql.execute('reset query cache')
    def checkRows(self, expectRows,sql):
        if self.queryRows == expectRows:
            tdLog.info("sql:%s, queryRows:%d == expect:%d" % (sql, self.queryRows, expectRows))
        else:
            caller = inspect.getframeinfo(inspect.stack()[1][0])
            args = (caller.filename, caller.lineno, sql, self.queryRows, expectRows)
            os.system("timedatectl set-ntp true")
            tdLog.exit("%s(%d) failed: sql:%s, queryRows:%d != expect:%d" % args)

    def run(self):
        
        tdLog.info("=============== step1")
        tdSql.execute('create database test keep 3 days 1;')
        tdSql.execute('use test;')
        tdSql.execute('create table test(ts timestamp,i int);')

        cmd = 'insert into test values(now-2d,11)(now-1d,11)(now,11)(now+1d,11);'
        tdLog.info(cmd)
        tdSql.execute(cmd)
        tdSql.query('select * from test')
        tdSql.checkRows(4)

        tdLog.info("=============== step2")
        tdDnodes.stop(1)
        os.system("timedatectl set-ntp false")
        os.system("date -s $(date -d \"${DATE} 2 days\" \"+%Y%m%d\")")
        tdDnodes.start(1)
        cmd = 'insert into test values(now,11);'
        tdLog.info(cmd)
        tdSql.execute(cmd)
70 71 72 73 74 75
        queryRows=tdSql.query('select * from test')
        if queryRows==4:
            tdSql.checkRows(4)
            return 0
        else:
            tdSql.checkRows(5)
76 77 78 79 80 81 82 83

        tdLog.info("=============== step3")
        tdDnodes.stop(1)
        os.system("date -s $(date -d \"${DATE} 2 days\" \"+%Y%m%d\")")
        tdDnodes.start(1)
        cmd = 'insert into test values(now-1d,11);'
        tdLog.info(cmd)
        tdSql.execute(cmd)
84
        queryRows=tdSql.query('select * from test')
85 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
        tdSql.checkRows(6)
        tdLog.info("=============== step4")
        tdDnodes.stop(1)
        tdDnodes.start(1)
        cmd = 'insert into test values(now,11);'
        tdLog.info(cmd)
        tdSql.execute(cmd)
        tdSql.query('select * from test')
        tdSql.checkRows(7)

        tdLog.info("=============== step5")
        tdDnodes.stop(1)
        tdDnodes.start(1)
        cmd='select * from test where ts > now-1d'
        queryRows=tdSql.query('select * from test where ts > now-1d')
        self.checkRows(1,cmd)

    def stop(self):
        os.system("timedatectl set-ntp true")
        tdSql.close()
        tdLog.success("%s successfully executed" % __file__)


clients = TDTestRetetion()
clients.init()
clients.run()
clients.stop()