cases.py 4.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
###################################################################
#           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 time
import datetime
18 19
import inspect
import importlib
20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
from util.log import *


class TDCase:
    def __init__(self, name, case):
        self.name = name
        self.case = case


class TDCases:
    def __init__(self):
        self.linuxCases = []
        self.windowsCases = []
        self.clusterCases = []

35 36 37 38
    def __dynamicLoadModule(self, fileName):
        moduleName = fileName.replace(".py", "").replace("/", ".")
        return importlib.import_module(moduleName, package='..')

39 40 41 42 43 44 45 46 47 48
    def addWindows(self, name, case):
        self.windowsCases.append(TDCase(name, case))

    def addLinux(self, name, case):
        self.linuxCases.append(TDCase(name, case))

    def addCluster(self, name, case):
        self.clusterCases.append(TDCase(name, case))

    def runAllLinux(self, conn):
49 50 51 52 53 54 55 56 57 58 59 60
        # TODO: load all Linux cases here
        runNum = 0
        for tmp in self.linuxCases:
            if tmp.name.find(fileName) != -1:
                case = testModule.TDTestCase()
                case.init(conn)
                case.run()
                case.stop()
                runNum += 1
                continue

        tdLog.notice("total %d Linux test case(s) executed" % (runNum))
61 62

    def runOneLinux(self, conn, fileName):
63 64
        testModule = self.__dynamicLoadModule(fileName)

65
        runNum = 0
66 67 68 69
        for tmp in self.linuxCases:
            if tmp.name.find(fileName) != -1:
                case = testModule.TDTestCase()
                case.init(conn)
70 71 72 73 74
                try:
                    case.run()
                except Exception as e:
                    tdLog.notice(repr(e))
                    tdLog.exit("failed: %s" % fileName)
75
                case.stop()
76
                runNum += 1
77 78
                continue

79
        tdLog.success("total %d Linux test case(s) executed" % (runNum))
80 81

    def runAllWindows(self, conn):
82 83 84 85 86 87 88 89 90 91 92 93
        # TODO: load all Windows cases here
        runNum = 0
        for tmp in self.windowsCases:
            if tmp.name.find(fileName) != -1:
                case = testModule.TDTestCase()
                case.init(conn)
                case.run()
                case.stop()
                runNum += 1
                continue

        tdLog.notice("total %d Windows test case(s) executed" % (runNum))
94 95

    def runOneWindows(self, conn, fileName):
96 97
        testModule = self.__dynamicLoadModule(fileName)

98
        runNum = 0
99 100 101 102 103 104
        for tmp in self.windowsCases:
            if tmp.name.find(fileName) != -1:
                case = testModule.TDTestCase()
                case.init(conn)
                case.run()
                case.stop()
105
                runNum += 1
106 107
                continue
        tdLog.notice("total %d Windows case(s) executed" % (runNum))
108 109

    def runAllCluster(self):
110 111 112 113 114 115 116 117 118 119 120 121 122 123
        # TODO: load all cluster case module here

        runNum = 0
        for tmp in self.clusterCases:
            if tmp.name.find(fileName) != -1:
                tdLog.notice("run cases like %s" % (fileName))
                case = testModule.TDTestCase()
                case.init()
                case.run()
                case.stop()
                runNum += 1
                continue

        tdLog.notice("total %d Cluster test case(s) executed" % (runNum))
124 125

    def runOneCluster(self, fileName):
126 127
        testModule = self.__dynamicLoadModule(fileName)

128
        runNum = 0
129 130 131 132 133 134 135
        for tmp in self.clusterCases:
            if tmp.name.find(fileName) != -1:
                tdLog.notice("run cases like %s" % (fileName))
                case = testModule.TDTestCase()
                case.init()
                case.run()
                case.stop()
136
                runNum += 1
137 138 139
                continue

        tdLog.notice("total %d Cluster test case(s) executed" % (runNum))
140 141 142


tdCases = TDCases()