Exemplo n.º 1
0
    def _ListToXml(self, name, list):
        node = self.doc.createElement(name)
        for item in list:
            subnode = self._ObjToXml(classUtility.getType(item), item)
            node.appendChild(subnode)

        return node
Exemplo n.º 2
0
    def __EventAttribByDeviceSpecial(self, expData, actData):
        newData = {}
        for key in expData.keys():
            newObj = classTranslation.classTranslation().doTranslate(
                expData[key])
            newData[key] = newObj
        newActData = {}
        deviceTypes = self.restApiHandler.getData('deviceType')
        rekeyTypes = testUtility.rekeyKey('Attribute-id', deviceTypes)
        for key in actData.keys():
            if hasattr(actData[key],
                       'eventType') and actData[key].eventType != None:
                eventType = actData[key].eventType.split('$')[-1]
                actData[key].eventType = eventType
            else:
                eventType = ''
            t = actData[key]
            if classUtility.getType(actData[key]) == 'list':
                dType = rekeyTypes[actData[key][0].deviceType.split('@')[-1]]
                Li = []
                for sub in actData[key]:
                    Li.append(sub)
                newActData[dType + '-' + eventType] = Li
            else:
                dType = rekeyTypes[actData[key].deviceType.split('@')[-1]]
                actData[key].deviceType = dType
                newActData[dType + '-' + eventType] = actData[key]

        return newData, newActData
Exemplo n.º 3
0
    def compare(self, name, testFile, expData, actData):
        """This method will compare expect data dictionary with actual data dictionary."""
        suiteObj = TestSuiteResult()
        suiteObj.testFileName = testFile
        suiteObj.name = name
        missKey, extraKey, commKey = testUtility.processDictKeys(
            expData, actData)
        if missKey:
            for key in missKey:
                caseObj = TestCaseResult()
                caseObj.name = key
                caseObj.status = 'NoReturn'
                suiteObj.caseList.append(caseObj)

        for key in commKey:
            type = classUtility.getType(expData[key])
            mapList = self.__compareObject(key, type, expData[key],
                                           actData[key])
            caseObj = TestCaseResult()
            caseObj.name = key
            failed = False
            for item in mapList:
                oldList = getattr(caseObj, item['status'])
                oldList.append(item)
                setattr(caseObj, item['status'], oldList)
                caseObj.status = self.__setCaseStatus(caseObj)
            suiteObj.caseList.append(caseObj)
        for case in suiteObj.caseList:
            suiteObj.totalRun += 1
            value = getattr(suiteObj, 'total' + case.status) + 1
            setattr(suiteObj, 'total' + case.status, value)

        return suiteObj
Exemplo n.º 4
0
def __complexReader(complex, server, option):
    startPos = []
    endPos = []
    for t in re.finditer(r"\(", complex):
        startPos.append(t.start())
    for t in re.finditer(r"\)", complex):
        endPos.append(t.end())
    myComplex = classLocator.getClassObj('complexConstriant',
                                         module='constriant')
    if startPos:
        simple = complex[:startPos[0]] + ' ' + complex[(endPos[-1] + 1):]
        if simple:
            mySimple = __simpleReader(simple.replace('"', ''), server, option)
            myComplex.subConstriants.extend(mySimple.subConstriants)
            myComplex.operator = mySimple.operator

        if (len(startPos) == 1
                and len(endPos) == 1) or (startPos[0] + 1 == startPos[1]
                                          and endPos[-2] + 1 == endPos[-1]):
            sub_complex = complex[(startPos[0] + 1):(endPos[-1] - 1)]
            subComplex = __complexReader(sub_complex, server, option)
            myComplex.subConstriants.append(subComplex)
        elif startPos[1] > endPos[0]:
            sub_list = []
            oper = complex[endPos[0] + 1:startPos[1] - 1]
            for i in range(len(startPos)):
                sub_list.append(complex[startPos[i] + 1:endPos[i] - 1])
            myComplex.operator = oper
            for item in sub_list:
                subComplex = __complexReader(item, server, option)
                myComplex.subConstriants.append(subComplex)

    else:
        mySimple = __simpleReader(complex.replace('"', ''), server, option)
        myComplex.subConstriants.extend(mySimple.subConstriants)
        myComplex.operator = mySimple.operator

    #remove dubious complex contriant which is actually simple contriant
    if classUtility.getType(myComplex) == 'complexConstriant':
        if myComplex.operator == 'None' and len(
                myComplex.subConstriants) == 1 and classUtility.getType(
                    myComplex.subConstriants[0]) != 'complexConstriant':
            myNew = myComplex.subConstriants[0]
            myComplex = myNew

    return myComplex
Exemplo n.º 5
0
def getDataFromFile(type, path, keyAttrs, orderAttrs, asCsv=True):
    """This method will read data from a CSV file."""
    if asCsv:
        if '.csv' not in path:
            path += '.csv'
    objData = {}
    reader = csv.DictReader(open(path),
                            fieldnames=orderAttrs,
                            restkey='Extra',
                            restval='Miss')
    keyword = []
    for line in reader:
        if not orderAttrs:
            orderAttrs = line.keys()
            if 'Extra' in orderAttrs:
                orderAttrs.remove('Extra')
            if 'Miss' in orderAttrs:
                orderAttrs.remove('Miss')
        if 'Extra' in line.keys():
            line[orderAttrs[-1]] += ', ' + ','.join(line['Extra'])
            del line['Extra']
        if __metCondition(line):
            if '$' in line[orderAttrs[0]]:
                keyword = []
                keyword.append(line[orderAttrs[0]].split('$')[-1])
            elif line[orderAttrs[0]] in csv_device_info:
                keyword.append(line[orderAttrs[1]])
            else:
                obj = getClassObj(type)
                for key in line.keys():
                    if key in classUtility.getAttrList(obj):
                        setattr(obj, key, line[key])
                map = {}
                if keyAttrs and keyword:
                    if classUtility.getType(keyAttrs) == 'list':
                        map = testUtility.listToHash(keyAttrs, keyword)
                    elif len(keyword) > 1:
                        keyVal = '-'.join(keyword)
                        map[keyAttrs] = keyVal
                    else:
                        map[keyAttrs] = keyword[0]
                if map:
                    for newKey in map.keys():
                        setattr(obj, newKey, map[newKey])
                indexKey = testUtility.getKey(type, file=True)
                indexValue = classUtility.getIndexValue(obj, indexKey)
                objData[indexValue] = obj

    return objData
Exemplo n.º 6
0
    def __getCmd(self, type, param=False):
        if param:
            myParam=TestConstant.sql_values[type]['param']
            if classUtility.getType(myParam)=='list':
                myStr=' where '
                for i in range(len(myParam)):
                    myStr+=myParam[i]+' = '+"'"+param[i]+"'"+' AND '
                myStr=myStr[0:-5]
            else:
                myStr=' where '+myParam+' = '+"'"+param+"'"
            cmd=TestConstant.sql_query % (', '.join(TestConstant.sql_values[type]['values']), TestConstant.sql_values[type]['tableName'])+myStr
        else:
            cmd=TestConstant.sql_query % (', '.join(TestConstant.sql_values[type]['values']), TestConstant.sql_values[type]['tableName'])

        return cmd
Exemplo n.º 7
0
    def getModulePreData(self, module):
        myPath=upgrade_path % (module+'-'+self.server.replace('.', '_')+'.xml')
        if module in obj_name_trans.keys():
            fiMod=obj_name_trans[module]
        else:
            fiMod=module
        if fiMod in obj_xml_wrap.keys():
            fiKey=obj_xml_wrap[fiMod]
        else:
            fiKey=generalUtility.getPlural(fiMod)
        oriData=XmlHandler().XmlFileToObj(myPath, keyword=fiKey)
        indexData={}
        for ori in oriData:
            if classUtility.getType(ori)!='NoneType':
                indexData[ori.attribute['naturalId']]=ori

        return indexData
Exemplo n.º 8
0
def getRules(server):
    restApiHandler = restApiDataHandler(server)
    rawtestMap = restApiHandler.getData('rule')
    testMap = {}
    for mapKey in rawtestMap.keys():
        if rawtestMap[mapKey].active == 'true':
            if 'dataCreationType' in rawtestMap[mapKey].attribute.keys(
            ) and rawtestMap[mapKey].attribute['dataCreationType'] == "SYSTEM":
                testMap[mapKey] = rawtestMap[mapKey]

    indexMap = {}
    for key in testMap.keys():
        incidentType = testMap[key].incidentType.split('$')[-1]
        indexMap[incidentType] = testMap[key]
    myFile = open('ruleInfo.txt', 'w')
    for key in indexMap.keys():

        myFile.write(key + '\n')
        print key
        groupCon = None
        groupby = None
        if classUtility.getType(
                indexMap[key].eventFilters[0].groupConstraint) != 'NoneType':
            if type in correct_grammer_cases.keys():
                groupCon = correct_grammer_cases[type]
            else:
                groupCon = indexMap[key].eventFilters[0].groupConstraint
        """if classUtility.getType(indexMap[key].eventFilters[0].groupBy)!='NoneType':
            groupby=indexMap[key].eventFilters[0].groupBy
        else:
            groupby=None"""
        #single=indexMap[key].eventFilters[0].singleConstraint
        #valueMap=constraintUtility.processConstraint(single, groupBy=groupby, groupConstr=groupCon)
        #valueMap=expressionReader.expressionReader(groupCon, server)
        if groupCon:
            print groupCon
            myFile.write(groupCon + '\n')
        #myFile.write(str(valueMap['SingleConstriant'])+'\n')
        #print valueMap['SingleConstriant']
        """if groupCon:
            print groupCon
            print valueMap['GroupConstriant']
            myFile.write(groupCon+'\n')
            myFile.write(str(valueMap['GroupConstriant'])+'\n')   """
    myFile.close()
Exemplo n.º 9
0
    def __compareDict(self, name, expDict, actDict):
        params = []
        missKey, extraKey, commKey = testUtility.processDictKeys(
            expDict, actDict)
        if missKey:
            for key in missKey:
                map = self.__setResultMap(paramName=name + '.' + key,
                                          expValue=expDict[key])
                params.append(map)
        if extraKey:
            for key in extraKey:
                if key not in TestConstant.compare_skip_extras:
                    map = self.__setResultMap(paramName=name + '.' + key,
                                              actValue=actDict[key])
                    params.append(map)
        for key in commKey:
            value = expDict[key]
            type = classUtility.getType(value)
            params.extend(self.__compareObject(key, type, value, actDict[key]))

        return params
Exemplo n.º 10
0
    def __compareClass(self, name, expClass, actClass):
        params = []
        missKey, extraKey, commKey = testUtility.processClassAttrs(
            expClass, actClass)
        if missKey:
            for key in missKey:
                map = self.__setResultMap(paramName=name + '.' + key,
                                          expValue=getattr(expClass, key))
                params.append(map)
        if extraKey:
            for key in extraKey:
                if key not in TestConstant.compare_skip_extras:
                    map = self.__setResultMap(paramName=name + '.' + key,
                                              actValue=getattr(actClass.key))
                    params.append(map)
        for key in commKey:
            subobj = getattr(expClass, key)
            type = classUtility.getType(subobj)
            params.extend(
                self.__compareObject(key, type, subobj, getattr(actClass,
                                                                key)))

        return params
Exemplo n.º 11
0
    def generateReport(self, autoResult):
        """This method generate the reports."""
        if autoResult.testType=='Official':
            result_file=TestConstant.official_result_file
        else:
            result_file=TestConstant.unofficial_result_file
        msgMap={'dateTime':autoResult.runTime,
                'buildVersion':autoResult.runVersion,
                'runTotal':autoResult.totalRun,
                'passTotal':autoResult.totalPass,
                'noReturnTotal':autoResult.totalNoReturn,
                'failTotal':autoResult.totalFail,
                'missTotal':autoResult.totalMissing,
                'extraTotal':autoResult.totalExtra}
        msg=self.substitute(MSG, msgMap)
        localhostIp=autoResult.localhost
        linkText=autoResult.testFolder.split('../Results/')[-1]
        myFile=open(result_file, 'a')
        myFile.write(linkText+'@')
        myFile.write(msg+'\n')
        myFile.close()
        self.__generateFiles(autoResult)
        report=self.__generateHtml(autoResult)
        self.__generateSummary(autoResult)
        if classUtility.getType(autoResult.sendEmail)!='NoneType':
            linkMap={'localhostIp':localhostIp, 'linkText':linkText}
            link=self.substitute(LINK, linkMap)
            mailMap={'localhostIp':localhostIp, 'linkText':linkText, 'report':report, 'link':link}
            mailContent=self.substitute(MAIL, mailMap)
            mail_name=''
            if autoResult.batch:
                mail_name=autoResult.name+'-'+autoResult.batch
            else:
                mail_name=autoResult.name
            self.__sendEmailout(mailContent, mail_name, autoResult.runVersion.split('(',1)[0].strip(), autoResult.recipient.split(','))

        return TestConstant.report_msg
Exemplo n.º 12
0
    def getData(self,
                datatype,
                index=True,
                pickle=True,
                noKey=False,
                module='config',
                arg={}):
        """This method will call GET to return a dictinary of indexed objects."""
        data = ''
        if pickle:
            data = self.__getData(datatype, module, noKey=noKey, args=arg)
        else:
            data = self.__getData(datatype, module, pickle=False, args=arg)
        if data:
            if module == 'namedValue':
                datatype = 'entityValue'
            if type(data) == types.ListType:
                if pickle and index:
                    indexData = {}
                    if datatype in TestConstant.obj_name_trans.keys():
                        fiType = TestConstant.obj_name_trans[datatype]
                    else:
                        fiType = datatype
                    keyItem = testUtility.getKey(fiType)
                    for item in data:
                        if item is not None:
                            if 'Attribute-' in keyItem:
                                key = item.attribute[keyItem.split('-')[-1]]
                            elif classUtility.getType(keyItem) == 'list':
                                values = []
                                for subKey in keyItem:
                                    subVal = getattr(item, subKey)
                                    if subVal == None:
                                        subVal = ''
                                    values.append(subVal)
                                key = '@'.join(values)
                            else:
                                key = getattr(item, keyItem)
                            if key not in indexData.keys():
                                indexData[key] = item
                            else:
                                oldData = indexData[key]
                                Li = []
                                if classUtility.getType(oldData) == 'list':
                                    Li = oldData
                                    Li.append(item)
                                else:
                                    Li.append(oldData)
                                    Li.append(item)
                                indexData[key] = Li
                    if datatype in TestConstant.rest_special_handling:
                        finalData = self.__specialHandling(datatype, indexData)
                        return finalData
                    else:
                        return indexData
                else:
                    return data

            else:
                return data
        else:
            return data
Exemplo n.º 13
0
def autoTest(file, server=None):
    """starting point to run automation test."""
    testConfig = getTestConfig(file, src=server)
    if testConfig.threadPool is not None:
        thread_pool = testConfig.threadPool
    else:
        thread_pool = default_threads_pool
    thread_wait = 5
    if testConfig.threadWaitTime is not None:
        thread_wait = int(testConfig.threadWaitTime)
    else:
        thread_wait = default_thread_wait
    threads = []
    taskNames = []
    queue = Queue.Queue()
    return_queue = Queue.Queue()
    for task in testConfig.testTask:
        taskNames.append(task.taskName)
        if not task.taskName in supported_tasks:
            print 'test task: %s is not supported at current time.' % task.taskName
            exit()
        testObj = tokenDict[task.taskName](task, testConfig)
        id = 1
        commData = testObj.getCommonData(task.taskFiles)
        if commData:
            setattr(testConfig, 'commonData', commData)
        globalData = testObj.getGlobalData()
        if globalData:
            setattr(testConfig, 'globalData', globalData)
        tests = testObj.getTestList(task.taskFiles)

        if not tests:
            print 'no tests performed.'
            exit()
        miss, extra, common = processList(tests.keys(), commData.keys())
        miss_map = {}
        if miss:
            print 'NOT implemented:\n'
            for item in miss:
                if item not in not_miss:
                    if type(tests[item]).__name__ == 'unicode':
                        type_name = tests[item]
                    else:
                        type_name = tests[item].name
                    print 'Id: %s Name: %s\n' % (item, type_name)
                    miss_map[item] = type_name

        for testKey in common:
            map = {}
            map['name'] = task.taskName
            map['task'] = tests[testKey]
            map['key'] = testKey
            map['obj'] = tokenDict[task.taskName](task, testConfig)
            queue.put(map)

        if len(common) > thread_pool:
            thread_size = thread_pool
        else:
            thread_size = len(common)

        for i in range(thread_size):
            thread = testThread(queue, return_queue)
            thread.setDaemon(True)
            thread.start()
            threads.append(thread)
            if thread_wait:
                time.sleep(thread_wait)

    queue.join()
    fullResult = []
    while not return_queue.empty():
        item = return_queue.get_nowait()
        if item:
            fullResult.append(item)

    print "Main Test Thread Exiting"
    if testConfig.ruleTest and testConfig.ruleTestSupport:
        #check testRule for incident testing
        if testConfig.testTask[0].taskName == 'Incident':
            for item in fullResult:
                if item.caseList[
                        0].status == 'NoReturn' and item.testMethod == 'syslog':
                    myMap = {
                        'ruleId': item.ruleId,
                        'rawMsg': item.rawMsg,
                        'reportIp': item.reptDevIpAddr
                    }
                    status, msg = testRule(testConfig.testServer.appServer,
                                           ruleData=myMap)
                    reason = ''
                    if status == "Pass":
                        item.caseList[0].status = 'Pass'
                    elif status == 'Failure':
                        matched = ''
                        for key in test_rule_exps.keys():
                            match = test_rule_exps[key].search(msg)
                            if match:
                                matched = key
                                break
                        reason = 'testRule triggers failure: %s' % matched
                    elif status == 'Unfinish':
                        rasson = 'testRule triggers unfinished after timeout 10 minutes'
                    item.testRuleResultSummary = reason
                    item.testRuleResultDetail = msg

    autoRet = AutoTestResult()
    autoRet.name = '-'.join(taskNames)
    if hasattr(testConfig, 'batch'):
        autoRet.batch = testConfig.batch
    else:
        autoRet.batch = ''
    autoRet.testType = testConfig.testType
    autoRet.runTime = testConfig.runTime
    autoRet.runVersion = testConfig.buildVersion
    autoRet.localhost = testConfig.localhost
    autoRet.recipient = testConfig.recipient
    autoRet.testFolder = TestConstant.default_result_path + testConfig.name
    if miss_map:
        setattr(autoRet, 'miss', miss_map)
    if classUtility.getType(testConfig.sendEmail) != 'NoneType':
        autoRet.sendEmail = True
    for item in fullResult:
        for count in TestConstant.test_result_counters:
            value = getattr(autoRet, count)
            value += getattr(item, count)
            setattr(autoRet, count, value)
        autoRet.suiteList.append(item)

    msg = testReport().generateReport(autoRet)

    return msg
Exemplo n.º 14
0
 def _ObjToXml(self, name, obj):
     type = classUtility.getType(obj)
     if type not in self.toXmlToken.keys():
         type = 'default'
     return self.toXmlToken[type](self, name, obj)
Exemplo n.º 15
0
    def XmlObjToString(self, obj):
        """This method takes clss object and produce a xml string."""
        node = self._ObjToXml(classUtility.getType(obj), obj)

        return node.toxml()
Exemplo n.º 16
0
 def XmlObjToFile(self, obj, path):
     """This method takes class object and produce a xml file."""
     output = open(path, 'w')
     data = self._ObjToXml(classUtility.getType(obj), obj)
     data.writexml(output)
     output.close()
Exemplo n.º 17
0
    def _XmlToObj(self, node, wrapTag=None, filter=None):
        if wrapTag:
            subnode = ''
            nodes = node.getElementsByTagName(wrapTag)
            if not nodes:
                return None
            else:
                subnode = nodes[0]
                nodelist = self._getElementChilds(subnode)
                if nodelist:
                    if nodelist[0][0] == nodelist[-1][0]:
                        return self._XmlToObjList(nodelist, condition=filter)
                    else:
                        return self._XmlToObj(subnode)
                elif subnode.childNodes:
                    return subnode.childNodes[0].nodeValue.strip()
                else:
                    return None
        else:
            if hasattr(node, 'tagName'):
                tagName = node.tagName
            else:
                tagName = node.nodeName
            obj = getClassObj(tagName)
            objAttrs = classUtility.getAttrList(obj)
            if 'attribute' in objAttrs:
                self._setAttributes(node, obj)
            if 'namedValues' in objAttrs:
                obj = self._setNamedValues(node, obj)
            if tagName in xml_2_obj_special.keys():
                attr = node.attributes.getNamedItem(
                    xml_2_obj_special[tagName]['attr']).nodeValue
                setattr(obj, xml_2_obj_special[tagName]['attr'], attr)
                value = ''
                if len(node.childNodes):
                    value = node.childNodes[0].nodeValue.strip()
                    setattr(obj, xml_2_obj_special[tagName]['text'], value)
            else:
                elementChilds = self._getElementChilds(node)
                for name, element in elementChilds:
                    if name in objAttrs:
                        my_type = classUtility.getType(getattr(obj, name))
                        if my_type == 'NoneType':
                            if len(element.childNodes):
                                value = element.childNodes[0].nodeValue
                                if value:
                                    setattr(obj, name, value.strip())
                        elif my_type == 'list':
                            li = getattr(obj, name)
                            subE = self._getElementChilds(element)
                            subENames = [na[0] for na in subE]
                            subObj = ''
                            if subENames:
                                if generalUtility.isWrap(subENames[0], name):
                                    if subE:
                                        subObj = self._XmlToObjList(subE)
                                else:
                                    if subE:
                                        subObj = self._XmlToObj(element)
                            if subObj:
                                if classUtility.getType(subObj) == 'list':
                                    li.extend(subObj)
                                else:
                                    li.append(subObj)
                            setattr(obj, name, li)
                        else:
                            subObj = self._XmlToObj(element)
                            setattr(obj, name, subObj)

        return obj