admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | ''' |
| 3 | Created on 22-Oct-2012 |
Jon Hall | 65844a3 | 2015-03-09 19:09:37 -0700 | [diff] [blame] | 4 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 5 | @author: Anil Kumar (anilkumar.s@paxterrasolutions.com) |
| 6 | |
| 7 | |
| 8 | TestON is free software: you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation, either version 2 of the License, or |
| 11 | (at your option) any later version. |
| 12 | |
| 13 | TestON is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
Jon Hall | 65844a3 | 2015-03-09 19:09:37 -0700 | [diff] [blame] | 19 | along with TestON. If not, see <http://www.gnu.org/licenses/>. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 20 | |
| 21 | |
| 22 | |
| 23 | teston is the main module. |
| 24 | |
| 25 | ''' |
| 26 | |
| 27 | import sys |
| 28 | import getpass |
| 29 | import os |
| 30 | import re |
| 31 | import __builtin__ |
| 32 | import new |
| 33 | import xmldict |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 34 | import importlib |
Jon Hall | 5b58673 | 2015-06-11 11:39:39 -0700 | [diff] [blame] | 35 | import threading |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 36 | module = new.module("test") |
| 37 | import openspeak |
Hari Krishna | be4b97b | 2015-07-15 12:19:43 -0700 | [diff] [blame] | 38 | import subprocess |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 39 | global path, drivers_path, core_path, tests_path,logs_path |
Jon Hall | 4450624 | 2015-07-29 17:40:26 -0700 | [diff] [blame] | 40 | location = os.path.abspath( os.path.dirname( __file__ ) ) |
| 41 | path = re.sub( "(core|bin)$", "", location ) |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 42 | drivers_path = path+"drivers" |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 43 | core_path = path+"core" |
| 44 | tests_path = path+"tests" |
| 45 | logs_path = path+"logs/" |
| 46 | config_path = path + "config/" |
Jon Hall | 4450624 | 2015-07-29 17:40:26 -0700 | [diff] [blame] | 47 | sys.path.append( path ) |
| 48 | sys.path.append( drivers_path ) |
| 49 | sys.path.append( core_path ) |
| 50 | sys.path.append( tests_path ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 51 | |
| 52 | from core.utilities import Utilities |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 53 | from core.Thread import Thread |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 54 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 55 | |
| 56 | class TestON: |
| 57 | ''' |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 58 | TestON will initiate the specified test. |
| 59 | The main tasks are : |
| 60 | * Initiate the required Component handles for the test. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 61 | * Create Log file Handles. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 62 | ''' |
| 63 | def __init__(self,options): |
| 64 | ''' |
| 65 | Initialise the component handles specified in the topology file of the specified test. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 66 | ''' |
| 67 | # Initialization of the variables. |
| 68 | __builtin__.main = self |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 69 | __builtin__.path = path |
| 70 | __builtin__.utilities = Utilities() |
| 71 | self.TRUE = 1 |
| 72 | self.FALSE = 0 |
| 73 | self.ERROR = -1 |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 74 | self.NORESULT = 2 |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 75 | self.FAIL = False |
| 76 | self.PASS = True |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 77 | self.CASERESULT = self.ERROR |
| 78 | self.STEPRESULT = self.NORESULT |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 79 | self.init_result = self.TRUE |
| 80 | self.testResult = "Summary" |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 81 | self.stepName = "" |
| 82 | self.stepCache = "" |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 83 | self.EXPERIMENTAL_MODE = False |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 84 | self.test_target = None |
| 85 | self.lastcommand = None |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 86 | self.testDir = tests_path |
| 87 | self.configFile = config_path + "teston.cfg" |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 88 | self.parsingClass = "xmlparser" |
| 89 | self.parserPath = core_path + "/xmlparser" |
| 90 | self.loggerPath = core_path + "/logger" |
| 91 | self.loggerClass = "Logger" |
| 92 | self.logs_path = logs_path |
| 93 | self.driver = '' |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 94 | self.Thread = Thread |
Jon Hall | 5b58673 | 2015-06-11 11:39:39 -0700 | [diff] [blame] | 95 | self.cleanupFlag = False |
| 96 | self.cleanupLock = threading.Lock() |
Jon Hall | 0fc0d45 | 2015-07-14 09:49:58 -0700 | [diff] [blame] | 97 | self.initiated = False |
Jon Hall | 65844a3 | 2015-03-09 19:09:37 -0700 | [diff] [blame] | 98 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 99 | self.configparser() |
| 100 | verifyOptions(options) |
| 101 | load_logger() |
| 102 | self.componentDictionary = {} |
| 103 | self.componentDictionary = self.topology ['COMPONENT'] |
| 104 | self.driversList=[] |
| 105 | if type(self.componentDictionary) == str : |
| 106 | self.componentDictionary = dict(self.componentDictionary) |
Jon Hall | 65844a3 | 2015-03-09 19:09:37 -0700 | [diff] [blame] | 107 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 108 | for component in self.componentDictionary : |
| 109 | self.driversList.append(self.componentDictionary[component]['type']) |
Jon Hall | 65844a3 | 2015-03-09 19:09:37 -0700 | [diff] [blame] | 110 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 111 | self.driversList = list(set(self.driversList)) # Removing duplicates. |
| 112 | # Checking the test_target option set for the component or not |
| 113 | if type(self.componentDictionary) == dict: |
| 114 | for component in self.componentDictionary.keys(): |
| 115 | if 'test_target' in self.componentDictionary[component].keys(): |
| 116 | self.test_target = component |
Jon Hall | 65844a3 | 2015-03-09 19:09:37 -0700 | [diff] [blame] | 117 | |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 118 | # Checking for the openspeak file and test script |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 119 | self.logger.initlog(self) |
| 120 | |
| 121 | # Creating Drivers Handles |
| 122 | initString = "\n"+"*" * 30+"\n CASE INIT \n"+"*" * 30+"\n" |
| 123 | self.log.exact(initString) |
| 124 | self.driverObject = {} |
| 125 | self.random_order = 111 # Random order id to connect the components |
| 126 | components_connect_order = {} |
| 127 | #component_list.append() |
| 128 | if type(self.componentDictionary) == dict: |
| 129 | for component in self.componentDictionary.keys(): |
| 130 | self.componentDictionary[component]['connect_order'] = self.componentDictionary[component]['connect_order'] if ('connect_order' in self.componentDictionary[component].keys()) else str(self.get_random()) |
| 131 | components_connect_order[component] = eval(self.componentDictionary[component]['connect_order']) |
| 132 | #Ordering components based on the connect order. |
| 133 | ordered_component_list =sorted(components_connect_order, key=lambda key: components_connect_order[key]) |
| 134 | print ordered_component_list |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 135 | for component in ordered_component_list: |
| 136 | self.componentInit(component) |
| 137 | |
| 138 | def configparser(self): |
| 139 | ''' |
| 140 | It will parse the config file (teston.cfg) and return as dictionary |
| 141 | ''' |
| 142 | matchFileName = re.match(r'(.*)\.cfg', self.configFile, re.M | re.I) |
| 143 | if matchFileName: |
| 144 | xml = open(self.configFile).read() |
| 145 | try : |
| 146 | self.configDict = xmldict.xml_to_dict(xml) |
| 147 | return self.configDict |
Jon Hall | 1306a56 | 2015-09-04 11:21:24 -0700 | [diff] [blame] | 148 | except IOError: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 149 | print "There is no such file to parse " + self.configFile |
Jon Hall | 1306a56 | 2015-09-04 11:21:24 -0700 | [diff] [blame] | 150 | else: |
| 151 | print "There is no such file to parse " + self.configFile |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 152 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 153 | def componentInit(self,component): |
| 154 | ''' |
| 155 | This method will initialize specified component |
| 156 | ''' |
| 157 | global driver_options |
Jon Hall | 0fc0d45 | 2015-07-14 09:49:58 -0700 | [diff] [blame] | 158 | self.initiated = False |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 159 | self.log.info("Creating component Handle: "+component) |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 160 | driver_options = {} |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 161 | if 'COMPONENTS' in self.componentDictionary[component].keys(): |
| 162 | driver_options =dict(self.componentDictionary[component]['COMPONENTS']) |
| 163 | |
| 164 | driver_options['name']=component |
| 165 | driverName = self.componentDictionary[component]['type'] |
| 166 | driver_options ['type'] = driverName |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 167 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 168 | classPath = self.getDriverPath(driverName.lower()) |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 169 | driverModule = importlib.import_module(classPath) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 170 | driverClass = getattr(driverModule, driverName) |
| 171 | driverObject = driverClass() |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 172 | |
Hari Krishna | be4b97b | 2015-07-15 12:19:43 -0700 | [diff] [blame] | 173 | if ( "OCN" in self.componentDictionary[component]['host'] and main.onoscell ): |
| 174 | self.componentDictionary[component]['host'] = main.mnIP |
| 175 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 176 | connect_result = driverObject.connect(user_name = self.componentDictionary[component]['user'] if ('user' in self.componentDictionary[component].keys()) else getpass.getuser(), |
| 177 | ip_address= self.componentDictionary[component]['host'] if ('host' in self.componentDictionary[component].keys()) else 'localhost', |
| 178 | pwd = self.componentDictionary[component]['password'] if ('password' in self.componentDictionary[component].keys()) else 'changeme', |
| 179 | port = self.componentDictionary[component]['port'] if ('port' in self.componentDictionary[component].keys()) else None, |
| 180 | options = driver_options) |
cameron@onlab.us | 5cc6a37 | 2015-05-11 17:18:07 -0700 | [diff] [blame] | 181 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 182 | if not connect_result: |
Jon Hall | 166e4a4 | 2015-08-10 12:03:41 -0700 | [diff] [blame] | 183 | self.log.error("Exiting from the test execution because the connecting to the "+component+" component failed.") |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 184 | self.exit() |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 185 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 186 | vars(self)[component] = driverObject |
Jon Hall | 0fc0d45 | 2015-07-14 09:49:58 -0700 | [diff] [blame] | 187 | self.initiated = True |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 188 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 189 | def run(self): |
| 190 | ''' |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 191 | The Execution of the test script's cases listed in the Test params file will be done here. |
| 192 | And Update each test case result. |
| 193 | This method will return TRUE if it executed all the test cases successfully, |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 194 | else will retun FALSE |
| 195 | ''' |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 196 | self.testCaseResult = {} |
Jon Hall | a118598 | 2014-09-15 14:55:10 -0700 | [diff] [blame] | 197 | self.TOTAL_TC = 0 |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 198 | self.TOTAL_TC_RUN = 0 |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 199 | self.TOTAL_TC_PLANNED = 0 |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 200 | self.TOTAL_TC_NORESULT = 0 |
| 201 | self.TOTAL_TC_FAIL = 0 |
| 202 | self.TOTAL_TC_PASS = 0 |
Jon Hall | a118598 | 2014-09-15 14:55:10 -0700 | [diff] [blame] | 203 | self.TEST_ITERATION = 0 |
Jon Hall | 5a72b71 | 2015-09-28 12:20:59 -0700 | [diff] [blame] | 204 | self.stepCount = 0 # NOTE: number of main.step statements in the |
| 205 | # outer most level of the test case. used to |
| 206 | # execute code in smaller steps |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 207 | self.CASERESULT = self.NORESULT |
| 208 | |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 209 | import testparser |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 210 | testFile = self.tests_path + "/"+self.TEST + "/"+self.TEST + ".py" |
| 211 | test = testparser.TestParser(testFile) |
| 212 | self.testscript = test.testscript |
| 213 | self.code = test.getStepCode() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 214 | repeat= int(self.params['repeat']) if ('repeat' in self.params) else 1 |
| 215 | self.TOTAL_TC_PLANNED = len(self.testcases_list)*repeat |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 216 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 217 | result = self.TRUE |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 218 | while(repeat): |
Jon Hall | a118598 | 2014-09-15 14:55:10 -0700 | [diff] [blame] | 219 | for self.CurrentTestCaseNumber in self.testcases_list: |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 220 | result = self.runCase(self.CurrentTestCaseNumber) |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 221 | repeat-=1 |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 222 | return result |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 223 | |
Jon Hall | e234cc4 | 2015-08-31 15:26:47 -0700 | [diff] [blame] | 224 | def runCase( self, testCaseNumber ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 225 | self.CurrentTestCaseNumber = testCaseNumber |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 226 | self.CurrentTestCase = "" |
Jon Hall | 5a72b71 | 2015-09-28 12:20:59 -0700 | [diff] [blame] | 227 | self.stepResultsList = [] # List of step results in a case. ANDed together to get the result |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 228 | self.stepName = "" |
Jon Hall | 783bbf9 | 2015-07-23 14:33:19 -0700 | [diff] [blame] | 229 | self.caseExplanation = "" |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 230 | result = self.TRUE |
Jon Hall | 5a72b71 | 2015-09-28 12:20:59 -0700 | [diff] [blame] | 231 | self.stepCount = 0 # NOTE: number of main.step statements in the |
| 232 | # outer most level of the test case. used to |
| 233 | # execute code in smaller steps |
| 234 | self.stepNumber = 0 # NOTE: This is the current number of |
| 235 | # main.step()'s executed in a case. Used for logging. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 236 | self.EXPERIMENTAL_MODE = self.FALSE |
| 237 | self.addCaseHeader() |
Jon Hall | e234cc4 | 2015-08-31 15:26:47 -0700 | [diff] [blame] | 238 | self.testCaseNumber = str( testCaseNumber ) |
| 239 | self.CASERESULT = self.NORESULT |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 240 | stopped = False |
Jon Hall | 5a72b71 | 2015-09-28 12:20:59 -0700 | [diff] [blame] | 241 | try: |
| 242 | self.code[self.testCaseNumber] |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 243 | except KeyError: |
Jon Hall | e234cc4 | 2015-08-31 15:26:47 -0700 | [diff] [blame] | 244 | self.log.error( "There is no Test-Case " + self.testCaseNumber ) |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 245 | return self.FALSE |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 246 | self.stepCount = 0 |
| 247 | while self.stepCount < len(self.code[self.testCaseNumber].keys()): |
Jon Hall | 5a72b71 | 2015-09-28 12:20:59 -0700 | [diff] [blame] | 248 | result = self.runStep(self.code,self.testCaseNumber) |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 249 | if result == self.FALSE: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 250 | break |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 251 | elif result == self.TRUE: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 252 | continue |
Jon Hall | 5a72b71 | 2015-09-28 12:20:59 -0700 | [diff] [blame] | 253 | # stepResults format: ( stepNo[], stepName[], stepResult[], onFail[] ) |
| 254 | stepResults = self.stepResultsList |
Jon Hall | e234cc4 | 2015-08-31 15:26:47 -0700 | [diff] [blame] | 255 | if not stopped: |
| 256 | if self.CASERESULT == self.TRUE or self.CASERESULT == self.FALSE: |
| 257 | # Result was already explitily set somewhere else like skipCase() |
| 258 | pass |
Jon Hall | 5a72b71 | 2015-09-28 12:20:59 -0700 | [diff] [blame] | 259 | elif all( self.TRUE == i for i in stepResults ): |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 260 | # ALL PASSED |
| 261 | self.CASERESULT = self.TRUE |
Jon Hall | 5a72b71 | 2015-09-28 12:20:59 -0700 | [diff] [blame] | 262 | elif self.FALSE in stepResults: |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 263 | # AT LEAST ONE FAILED |
| 264 | self.CASERESULT = self.FALSE |
Jon Hall | 5a72b71 | 2015-09-28 12:20:59 -0700 | [diff] [blame] | 265 | elif self.TRUE in stepResults: |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 266 | # AT LEAST ONE PASSED |
| 267 | self.CASERESULT = self.TRUE |
| 268 | else: |
| 269 | self.CASERESULT = self.NORESULT |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 270 | self.testCaseResult[str(self.CurrentTestCaseNumber)] = self.CASERESULT |
| 271 | self.logger.updateCaseResults(self) |
Jon Hall | 783bbf9 | 2015-07-23 14:33:19 -0700 | [diff] [blame] | 272 | self.log.wiki( "<p>" + self.caseExplanation + "</p>" ) |
| 273 | self.log.summary( self.caseExplanation ) |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 274 | self.log.wiki( "<ul>" ) |
| 275 | for line in self.stepCache.splitlines(): |
| 276 | if re.search( " - PASS$", line ): |
| 277 | self.log.wiki( "<li>" + line + " <ac:emoticon ac:name=\"tick\" /></li>\n" ) |
| 278 | elif re.search( " - FAIL$", line ): |
| 279 | self.log.wiki( "<li>" + line + " <ac:emoticon ac:name=\"cross\" /></li>\n" ) |
| 280 | elif re.search( " - No Result$", line ): |
| 281 | self.log.wiki( "<li>" + line + " <ac:emoticon ac:name=\"warning\" /></li>\n" ) |
Jon Hall | 9062761 | 2015-06-09 14:57:02 -0700 | [diff] [blame] | 282 | else: # Should only be on fail message |
| 283 | self.log.wiki( "<ul><li>" + line + "</li></ul>\n" ) |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 284 | self.log.wiki( "</ul>" ) |
| 285 | self.log.summary( self.stepCache ) |
| 286 | self.stepCache = "" |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 287 | return result |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 288 | |
Jon Hall | 5a72b71 | 2015-09-28 12:20:59 -0700 | [diff] [blame] | 289 | def runStep(self,code,testCaseNumber): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 290 | if not cli.pause: |
Jon Hall | 5a72b71 | 2015-09-28 12:20:59 -0700 | [diff] [blame] | 291 | try: |
| 292 | step = self.stepCount |
| 293 | # stepResults format: ( stepNo, stepName, stepResult, onFail ) |
| 294 | # NOTE: This is needed to catch results of main.step()'s |
| 295 | # called inside functions or loops |
| 296 | self.stepResults = ( [], [], [], [] ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 297 | exec code[testCaseNumber][step] in module.__dict__ |
| 298 | self.stepCount = self.stepCount + 1 |
Jon Hall | 5a72b71 | 2015-09-28 12:20:59 -0700 | [diff] [blame] | 299 | |
| 300 | # Iterate through each of the steps and print them |
| 301 | for index in range( len( self.stepResults[0] ) ): |
| 302 | # stepResults needs ( stepNo, stepName, stepResult, onFail ) |
| 303 | stepNo = self.stepResults[0][ index ] |
| 304 | stepName = self.stepResults[1][ index ] |
| 305 | stepResult = self.stepResults[2][ index ] |
| 306 | onFail = self.stepResults[3][ index ] |
| 307 | self.stepCache += "\t" + str( testCaseNumber ) + "." |
| 308 | self.stepCache += str( stepNo ) + " " |
| 309 | self.stepCache += stepName + " - " |
| 310 | if stepResult == self.TRUE: |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 311 | self.stepCache += "PASS\n" |
Jon Hall | 5a72b71 | 2015-09-28 12:20:59 -0700 | [diff] [blame] | 312 | elif stepResult == self.FALSE: |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 313 | self.stepCache += "FAIL\n" |
Jon Hall | 5a72b71 | 2015-09-28 12:20:59 -0700 | [diff] [blame] | 314 | self.stepCache += "\t\t" + onFail + "\n" |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 315 | else: |
| 316 | self.stepCache += "No Result\n" |
Jon Hall | 5a72b71 | 2015-09-28 12:20:59 -0700 | [diff] [blame] | 317 | self.stepResultsList.append(stepResult) |
Jon Hall | e234cc4 | 2015-08-31 15:26:47 -0700 | [diff] [blame] | 318 | except StopIteration: # Raised in self.skipCase() |
| 319 | self.log.warn( "Skipping the rest of CASE" + |
| 320 | str( testCaseNumber ) ) |
Jon Hall | 5a72b71 | 2015-09-28 12:20:59 -0700 | [diff] [blame] | 321 | self.stepResultsList.append(self.STEPRESULT) |
Jon Hall | e234cc4 | 2015-08-31 15:26:47 -0700 | [diff] [blame] | 322 | self.stepCache += "\t\t" + self.onFailMsg + "\n" |
| 323 | self.stepCount = self.stepCount + 1 |
| 324 | return self.FALSE |
Jon Hall | 5b58673 | 2015-06-11 11:39:39 -0700 | [diff] [blame] | 325 | except StandardError: |
Jon Hall | 5a72b71 | 2015-09-28 12:20:59 -0700 | [diff] [blame] | 326 | stepNo = self.stepResults[0][ self.stepNumber - 1] |
| 327 | stepName = self.stepResults[1][ self.stepNumber - 1 ] |
Jon Hall | 40d2cbd | 2015-06-03 16:24:29 -0700 | [diff] [blame] | 328 | self.log.exception( "\nException in the following section of" + |
Jon Hall | e234cc4 | 2015-08-31 15:26:47 -0700 | [diff] [blame] | 329 | " code: " + str( testCaseNumber ) + "." + |
Jon Hall | 5a72b71 | 2015-09-28 12:20:59 -0700 | [diff] [blame] | 330 | str( stepNo ) + ": " + stepName ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 331 | self.stepCount = self.stepCount + 1 |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 332 | self.logger.updateCaseResults(self) |
| 333 | #WIKI results |
| 334 | self.log.wiki( "<ul>" ) |
| 335 | for line in self.stepCache.splitlines(): |
| 336 | if re.search( " - PASS$", line ): |
| 337 | self.log.wiki( "<li>" + line + " <ac:emoticon ac:name=\"tick\" /></li>\n" ) |
| 338 | elif re.search( " - FAIL$", line ): |
| 339 | self.log.wiki( "<li>" + line + " <ac:emoticon ac:name=\"cross\" /></li>\n" ) |
| 340 | elif re.search( " - No Result$", line ): |
| 341 | self.log.wiki( "<li>" + line + " <ac:emoticon ac:name=\"warning\" /></li>\n" ) |
Jon Hall | 9062761 | 2015-06-09 14:57:02 -0700 | [diff] [blame] | 342 | else: # Should only be on fail message |
| 343 | self.log.wiki( "<ul><li>" + line + "</li></ul>\n" ) |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 344 | self.log.wiki( "</ul>" ) |
| 345 | #summary results |
| 346 | self.log.summary( self.stepCache ) |
| 347 | self.stepCache = "" |
shahshreya | 957feaa | 2015-03-23 16:08:29 -0700 | [diff] [blame] | 348 | self.cleanup() |
Jon Hall | 00539b1 | 2015-04-03 13:55:46 -0700 | [diff] [blame] | 349 | self.exit() |
Jon Hall | e234cc4 | 2015-08-31 15:26:47 -0700 | [diff] [blame] | 350 | return self.TRUE |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 351 | if cli.stop: |
| 352 | cli.stop = False |
| 353 | stopped = True |
| 354 | self.TOTAL_TC_NORESULT = self.TOTAL_TC_NORESULT + 1 |
| 355 | self.testCaseResult[str(self.CurrentTestCaseNumber)] = "Stopped" |
| 356 | self.logger.updateCaseResults(self) |
| 357 | result = self.cleanup() |
Jon Hall | e234cc4 | 2015-08-31 15:26:47 -0700 | [diff] [blame] | 358 | return self.FALSE |
| 359 | |
| 360 | def skipCase( self, result="DEFAULT", msg=None ): |
| 361 | """ |
| 362 | Will skip the rest of the code in a test case. The case results will be |
| 363 | determined as normal based on completed assertions unless the result |
| 364 | argument is given. |
| 365 | |
| 366 | Optional Arguments: |
| 367 | result: Case insensite string. Can be 'PASS' or 'FAIL' and will set |
| 368 | the case result accordingly. |
| 369 | msg: Message to be printed when the case is skipped in the reports. |
| 370 | """ |
| 371 | result = result.upper().strip() |
| 372 | if result == "PASS": |
| 373 | self.CASERESULT = self.TRUE |
| 374 | elif result == "FAIL": |
| 375 | self.CASERESULT = self.FALSE |
| 376 | self.onFailMsg = "Skipping the rest of this case. " |
| 377 | if msg: |
| 378 | self.onFailMsg += str( msg ) |
| 379 | raise StopIteration |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 380 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 381 | def addCaseHeader(self): |
| 382 | caseHeader = "\n"+"*" * 30+"\n Result summary for Testcase"+str(self.CurrentTestCaseNumber)+"\n"+"*" * 30+"\n" |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 383 | self.log.exact(caseHeader) |
| 384 | caseHeader = "\n"+"*" * 40 +"\nStart of Test Case"+str(self.CurrentTestCaseNumber)+" : " |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 385 | for driver in self.componentDictionary.keys(): |
| 386 | vars(self)[driver+'log'].info(caseHeader) |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 387 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 388 | def addCaseFooter(self): |
Jon Hall | 5a72b71 | 2015-09-28 12:20:59 -0700 | [diff] [blame] | 389 | stepNo = self.stepResults[0][-2] |
| 390 | if stepNo > 0 : |
| 391 | previousStep = " "+str(self.CurrentTestCaseNumber)+"."+str(stepNo)+": "+ str(self.stepName) + "" |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 392 | stepHeader = "\n"+"*" * 40+"\nEnd of Step "+previousStep+"\n"+"*" * 40+"\n" |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 393 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 394 | caseFooter = "\n"+"*" * 40+"\nEnd of Test case "+str(self.CurrentTestCaseNumber)+"\n"+"*" * 40+"\n" |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 395 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 396 | for driver in self.driversList: |
| 397 | vars(self)[driver].write(stepHeader+"\n"+caseFooter) |
| 398 | |
| 399 | def cleanup(self): |
| 400 | ''' |
Jon Hall | 5b58673 | 2015-06-11 11:39:39 -0700 | [diff] [blame] | 401 | Print a summary of the current test's results then attempt to release |
| 402 | all the component handles and the close opened file handles. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 403 | |
Jon Hall | 5b58673 | 2015-06-11 11:39:39 -0700 | [diff] [blame] | 404 | This function shouldbe threadsafe such that cleanup will only be |
| 405 | executed once per test. |
| 406 | |
| 407 | This will return TRUE if all the component handles and log handles |
| 408 | closed properly, else return FALSE. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 409 | ''' |
| 410 | result = self.TRUE |
Jon Hall | 5b58673 | 2015-06-11 11:39:39 -0700 | [diff] [blame] | 411 | lock = self.cleanupLock |
| 412 | if lock.acquire( False ): |
| 413 | try: |
| 414 | if self.cleanupFlag is False: # First thread to run this |
| 415 | self.cleanupFlag = True |
Jon Hall | 0fc0d45 | 2015-07-14 09:49:58 -0700 | [diff] [blame] | 416 | if self.initiated: |
| 417 | self.logger.testSummary(self) |
Jon Hall | 5b58673 | 2015-06-11 11:39:39 -0700 | [diff] [blame] | 418 | for component in self.componentDictionary.keys(): |
| 419 | try : |
| 420 | tempObject = vars(self)[component] |
| 421 | print "Disconnecting from " + str(tempObject.name) + ": " + \ |
| 422 | str(tempObject) |
| 423 | tempObject.disconnect() |
Jon Hall | 1306a56 | 2015-09-04 11:21:24 -0700 | [diff] [blame] | 424 | except KeyboardInterrupt: |
| 425 | pass |
| 426 | except KeyError: |
| 427 | # Component not created yet |
| 428 | self.log.warn( "Could not find the component " + |
| 429 | str( component ) ) |
| 430 | except StandardError: |
Jon Hall | 5b58673 | 2015-06-11 11:39:39 -0700 | [diff] [blame] | 431 | self.log.exception( "Exception while disconnecting from " + |
| 432 | str( component ) ) |
| 433 | result = self.FALSE |
| 434 | # Closing all the driver's session files |
| 435 | for driver in self.componentDictionary.keys(): |
| 436 | try: |
| 437 | vars(self)[driver].close_log_handles() |
Jon Hall | 1306a56 | 2015-09-04 11:21:24 -0700 | [diff] [blame] | 438 | except KeyboardInterrupt: |
| 439 | pass |
| 440 | except KeyError: |
| 441 | # Component not created yet |
| 442 | self.log.warn( "Could not find the component " + |
| 443 | str( driver ) + " while trying to" + |
| 444 | " close log file" ) |
| 445 | except StandardError: |
Jon Hall | 5b58673 | 2015-06-11 11:39:39 -0700 | [diff] [blame] | 446 | self.log.exception( "Exception while closing log files for " + |
| 447 | str( driver ) ) |
| 448 | result = self.FALSE |
| 449 | else: |
| 450 | pass # Someone else already ran through this function |
| 451 | finally: |
| 452 | lock.release() |
| 453 | else: # Someone already has a lock |
| 454 | # NOTE: This could cause problems if we don't release the lock |
| 455 | # correctly |
| 456 | lock.acquire() # Wait for the other thread to finish |
| 457 | # NOTE: If we don't wait, exit could be called while the thread |
| 458 | # with the lock is still cleaning up |
| 459 | lock.release() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 460 | return result |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 461 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 462 | def pause(self): |
| 463 | ''' |
| 464 | This function will pause the test's execution, and will continue after user provide 'resume' command. |
| 465 | ''' |
| 466 | __builtin__.testthread.pause() |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 467 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 468 | def onfail(self,*components): |
| 469 | ''' |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 470 | When test step failed, calling all the components onfail. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 471 | ''' |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 472 | if not components: |
| 473 | try : |
| 474 | for component in self.componentDictionary.keys(): |
| 475 | tempObject = vars(self)[component] |
| 476 | result = tempObject.onfail() |
Jon Hall | 1306a56 | 2015-09-04 11:21:24 -0700 | [diff] [blame] | 477 | except StandardError as e: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 478 | print str(e) |
| 479 | result = self.FALSE |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 480 | else: |
| 481 | try : |
| 482 | for component in components: |
| 483 | tempObject = vars(self)[component] |
| 484 | result = tempObject.onfail() |
Jon Hall | 1306a56 | 2015-09-04 11:21:24 -0700 | [diff] [blame] | 485 | except StandardError as e: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 486 | print str(e) |
| 487 | result = self.FALSE |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 488 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 489 | def getDriverPath(self,driverName): |
| 490 | ''' |
| 491 | Based on the component 'type' specified in the params , this method will find the absolute path , |
| 492 | by recursively searching the name of the component. |
| 493 | ''' |
| 494 | import commands |
| 495 | |
| 496 | cmd = "find "+drivers_path+" -name "+driverName+".py" |
| 497 | result = commands.getoutput(cmd) |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 498 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 499 | result_array = str(result).split('\n') |
| 500 | result_count = 0 |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 501 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 502 | for drivers_list in result_array: |
| 503 | result_count = result_count+1 |
| 504 | if result_count > 1 : |
| 505 | print "found "+driverName+" "+ str(result_count) + " times"+str(result_array) |
| 506 | self.exit() |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 507 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 508 | result = re.sub("(.*)drivers","",result) |
Jon Hall | 166e4a4 | 2015-08-10 12:03:41 -0700 | [diff] [blame] | 509 | result = re.sub("\/\/","/",result) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 510 | result = re.sub("\.py","",result) |
| 511 | result = re.sub("\.pyc","",result) |
| 512 | result = re.sub("\/",".",result) |
| 513 | result = "drivers"+result |
| 514 | return result |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 515 | |
| 516 | def step(self,stepDesc): |
| 517 | ''' |
| 518 | The step information of the test-case will append to the logs. |
| 519 | ''' |
Jon Hall | 5a72b71 | 2015-09-28 12:20:59 -0700 | [diff] [blame] | 520 | previousStep = " "+str(self.CurrentTestCaseNumber)+"."+str(self.stepNumber)+": "+ str(self.stepName) + "" |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 521 | self.stepName = stepDesc |
Jon Hall | 5a72b71 | 2015-09-28 12:20:59 -0700 | [diff] [blame] | 522 | self.stepNumber += 1 |
| 523 | self.stepResults[0].append( self.stepNumber ) |
| 524 | self.stepResults[1].append( stepDesc ) |
| 525 | self.stepResults[2].append( self.NORESULT ) |
| 526 | self.stepResults[3].append( "No on fail message given" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 527 | |
Jon Hall | 5a72b71 | 2015-09-28 12:20:59 -0700 | [diff] [blame] | 528 | stepName = " "+str(self.CurrentTestCaseNumber)+"."+str(self.stepNumber)+": "+ str(stepDesc) + "" |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 529 | self.log.step(stepName) |
| 530 | stepHeader = "" |
Jon Hall | 5a72b71 | 2015-09-28 12:20:59 -0700 | [diff] [blame] | 531 | if self.stepNumber > 1 : |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 532 | stepHeader = "\n"+"-"*45+"\nEnd of Step "+previousStep+"\n"+"-"*45+"\n" |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 533 | stepHeader += "\n"+"-"*45+"\nStart of Step"+stepName+"\n"+"-"*45+"\n" |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 534 | for driver in self.componentDictionary.keys(): |
| 535 | vars(self)[driver+'log'].info(stepHeader) |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 536 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 537 | def case(self,testCaseName): |
| 538 | ''' |
| 539 | Test's each test-case information will append to the logs. |
| 540 | ''' |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 541 | self.CurrentTestCase = testCaseName |
Jon Hall | 5a72b71 | 2015-09-28 12:20:59 -0700 | [diff] [blame] | 542 | testCaseName = " " + str(testCaseName) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 543 | self.log.case(testCaseName) |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 544 | caseHeader = testCaseName+"\n"+"*" * 40+"\n" |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 545 | for driver in self.componentDictionary.keys(): |
| 546 | vars(self)[driver+'log'].info(caseHeader) |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 547 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 548 | def testDesc(self,description): |
| 549 | ''' |
| 550 | Test description will append to the logs. |
| 551 | ''' |
| 552 | description = "Test Description : " + str (description) + "" |
| 553 | self.log.info(description) |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 554 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 555 | def _getTest(self): |
| 556 | ''' |
| 557 | This method will parse the test script to find required test information. |
| 558 | ''' |
| 559 | testFile = self.tests_path + "/"+self.TEST + "/"+self.TEST + ".py" |
| 560 | testFileHandler = open(testFile, 'r') |
| 561 | testFileList = testFileHandler.readlines() |
| 562 | testFileHandler.close() |
| 563 | #self.TOTAL_TC_PLANNED = 0 |
| 564 | counter = 0 |
| 565 | for index in range(len(testFileList)): |
| 566 | lineMatch = re.match('\s+def CASE(\d+)(.*):',testFileList[index],0) |
| 567 | if lineMatch: |
| 568 | counter = counter + 1 |
Jon Hall | a118598 | 2014-09-15 14:55:10 -0700 | [diff] [blame] | 569 | self.TC_PLANNED = len(self.testcases_list) |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 570 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 571 | def response_parser(self,response, return_format): |
| 572 | ''' It will load the default response parser ''' |
| 573 | response_dict = {} |
| 574 | response_dict = self.response_to_dict(response, return_format) |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 575 | return_format_string = self.dict_to_return_format(response,return_format,response_dict) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 576 | return return_format_string |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 577 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 578 | def response_to_dict(self,response,return_format): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 579 | response_dict = {} |
| 580 | json_match = re.search('^\s*{', response) |
| 581 | xml_match = re.search('^\s*\<', response) |
| 582 | ini_match = re.search('^\s*\[', response) |
| 583 | if json_match : |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 584 | self.log.info(" Response is in 'JSON' format and Converting to '"+return_format+"' format") |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 585 | # Formatting the json string |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 586 | response = re.sub(r"{\s*'?(\w)", r'{"\1', response) |
| 587 | response = re.sub(r",\s*'?(\w)", r',"\1', response) |
| 588 | response = re.sub(r"(\w)'?\s*:", r'\1":', response) |
| 589 | response = re.sub(r":\s*'(\w)'\s*([,}])", r':"\1"\2', response) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 590 | try : |
| 591 | import json |
| 592 | response_dict = json.loads(response) |
Jon Hall | 1306a56 | 2015-09-04 11:21:24 -0700 | [diff] [blame] | 593 | except StandardError: |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 594 | self.log.exception( "Json Parser is unable to parse the string" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 595 | return response_dict |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 596 | elif ini_match : |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 597 | self.log.info(" Response is in 'INI' format and Converting to '"+return_format+"' format") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 598 | from configobj import ConfigObj |
| 599 | response_file = open("respnse_file.temp",'w') |
| 600 | response_file.write(response) |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 601 | response_file.close() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 602 | response_dict = ConfigObj("respnse_file.temp") |
| 603 | return response_dict |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 604 | elif xml_match : |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 605 | self.log.info(" Response is in 'XML' format and Converting to '"+return_format+"' format") |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 606 | try : |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 607 | response_dict = xmldict.xml_to_dict("<response> "+str(response)+" </response>") |
Jon Hall | 1306a56 | 2015-09-04 11:21:24 -0700 | [diff] [blame] | 608 | except StandardError: |
| 609 | self.log.exception() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 610 | return response_dict |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 611 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 612 | def dict_to_return_format(self,response,return_format,response_dict): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 613 | if return_format =='table' : |
| 614 | ''' Will return in table format''' |
| 615 | to_do = "Call the table output formatter" |
| 616 | global response_table |
| 617 | response_table = '\n' |
| 618 | response_table = response_table +'\t'.join(response_dict)+"\n" |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 619 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 620 | def get_table(value_to_convert): |
| 621 | ''' This will parse the dictionary recusrsively and print as table format''' |
| 622 | table_data = "" |
| 623 | if type(value_to_convert) == dict : |
| 624 | table_data = table_data +'\t'.join(value_to_convert)+"\n" |
| 625 | for temp_val in value_to_convert.values() : |
| 626 | table_data = table_data + get_table(temp_val) |
| 627 | else : |
| 628 | table_data = table_data + str(value_to_convert) +"\t" |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 629 | return table_data |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 630 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 631 | for value in response_dict.values() : |
| 632 | response_table = response_table + get_table(value) |
Jon Hall | 88e498c | 2015-03-06 09:54:35 -0800 | [diff] [blame] | 633 | # response_table = response_table + '\t'.join(response_dict.values()) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 634 | return response_table |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 635 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 636 | elif return_format =='config': |
| 637 | ''' Will return in config format''' |
| 638 | to_do = 'Call dict to config coverter' |
| 639 | response_string = str(response_dict) |
| 640 | print response_string |
| 641 | response_config = re.sub(",", "\n\t", response_string) |
| 642 | response_config = re.sub("u\'", "\'", response_config) |
| 643 | response_config = re.sub("{", "", response_config) |
| 644 | response_config = re.sub("}", "\n", response_config) |
| 645 | response_config = re.sub(":", " =", response_config) |
| 646 | return "[response]\n\t "+response_config |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 647 | elif return_format == 'xml': |
| 648 | ''' Will return in xml format''' |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 649 | response_xml = xmldict.dict_to_xml(response_dict) |
| 650 | response_xml = re.sub(">\s*<", ">\n<", response_xml) |
| 651 | return "\n"+response_xml |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 652 | elif return_format == 'json': |
| 653 | ''' Will return in json format''' |
| 654 | to_do = 'Call dict to xml coverter' |
| 655 | import json |
| 656 | response_json = json.dumps(response_dict) |
| 657 | return response_json |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 658 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 659 | def get_random(self): |
| 660 | self.random_order = self.random_order + 1 |
| 661 | return self.random_order |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 662 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 663 | def exit(self): |
| 664 | __builtin__.testthread = None |
Jon Hall | 5b58673 | 2015-06-11 11:39:39 -0700 | [diff] [blame] | 665 | for thread in threading.enumerate(): |
| 666 | if thread.isAlive(): |
| 667 | try: |
| 668 | thread._Thread__stop() |
| 669 | except: |
Jon Hall | 1306a56 | 2015-09-04 11:21:24 -0700 | [diff] [blame] | 670 | # NOTE: We should catch any exceptions while trying to |
| 671 | # close the thread so that we can try to close the other |
| 672 | # threads as well |
| 673 | print( str( thread.getName() ) + ' could not be terminated' ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 674 | sys.exit() |
| 675 | |
| 676 | def verifyOptions(options): |
| 677 | ''' |
| 678 | This will verify the command line options and set to default values, if any option not given in command line. |
| 679 | ''' |
| 680 | import pprint |
| 681 | pp = pprint.PrettyPrinter(indent=4) |
| 682 | |
Jon Hall | 88e498c | 2015-03-06 09:54:35 -0800 | [diff] [blame] | 683 | # pp.pprint(options) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 684 | verifyTest(options) |
| 685 | verifyExample(options) |
| 686 | verifyTestScript(options) |
| 687 | verifyParams() |
| 688 | verifyLogdir(options) |
| 689 | verifyMail(options) |
| 690 | verifyTestCases(options) |
Hari Krishna | 03f530e | 2015-07-10 17:28:27 -0700 | [diff] [blame] | 691 | verifyOnosCell(options) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 692 | |
| 693 | def verifyTest(options): |
Jon Hall | 4450624 | 2015-07-29 17:40:26 -0700 | [diff] [blame] | 694 | try: |
| 695 | if options.testname: |
| 696 | main.TEST = options.testname |
| 697 | main.classPath = "tests."+main.TEST+"."+main.TEST |
| 698 | main.tests_path = tests_path |
| 699 | elif options.example: |
| 700 | main.TEST = options.example |
| 701 | main.tests_path = path+"/examples/" |
| 702 | main.classPath = "examples."+main.TEST+"."+main.TEST |
| 703 | except AttributeError: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 704 | print "Test or Example not specified please specify the --test <test name > or --example <example name>" |
Jon Hall | 5b58673 | 2015-06-11 11:39:39 -0700 | [diff] [blame] | 705 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 706 | |
| 707 | def verifyExample(options): |
| 708 | if options.example: |
| 709 | main.testDir = path+'/examples/' |
| 710 | main.tests_path = path+"/examples/" |
| 711 | main.classPath = "examples."+main.TEST+"."+main.TEST |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 712 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 713 | def verifyLogdir(options): |
Jon Hall | 88e498c | 2015-03-06 09:54:35 -0800 | [diff] [blame] | 714 | # Verifying Log directory option |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 715 | if options.logdir: |
| 716 | main.logdir = options.logdir |
| 717 | else : |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 718 | main.logdir = main.FALSE |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 719 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 720 | def verifyMail(options): |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 721 | # Checking the mailing list |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 722 | if options.mail: |
| 723 | main.mail = options.mail |
| 724 | elif main.params.has_key('mail'): |
| 725 | main.mail = main.params['mail'] |
| 726 | else : |
| 727 | main.mail = 'paxweb@paxterrasolutions.com' |
| 728 | |
| 729 | def verifyTestCases(options): |
Jon Hall | 88e498c | 2015-03-06 09:54:35 -0800 | [diff] [blame] | 730 | # Getting Test cases list |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 731 | if options.testcases: |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 732 | testcases_list = options.testcases |
Jon Hall | 88e498c | 2015-03-06 09:54:35 -0800 | [diff] [blame] | 733 | # sys.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 734 | testcases_list = re.sub("(\[|\])", "", options.testcases) |
| 735 | main.testcases_list = eval(testcases_list+",") |
| 736 | else : |
| 737 | if 'testcases' in main.params.keys(): |
Jon Hall | a118598 | 2014-09-15 14:55:10 -0700 | [diff] [blame] | 738 | temp = eval(main.params['testcases']+",") |
| 739 | list1=[] |
| 740 | if type(temp[0])==list: |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 741 | for test in temp: |
| 742 | for testcase in test: |
| 743 | if type(testcase)==int: |
| 744 | testcase=[testcase] |
| 745 | list1.extend(testcase) |
| 746 | else : |
| 747 | temp=list(temp) |
| 748 | for testcase in temp: |
| 749 | if type(testcase)==int: |
| 750 | testcase=[testcase] |
| 751 | list1.extend(testcase) |
| 752 | main.testcases_list=list1 |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 753 | else : |
| 754 | print "testcases not specifed in params, please provide in params file or 'testcases' commandline argument" |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 755 | sys.exit() |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 756 | |
Hari Krishna | 03f530e | 2015-07-10 17:28:27 -0700 | [diff] [blame] | 757 | def verifyOnosCell(options): |
Hari Krishna | be4b97b | 2015-07-15 12:19:43 -0700 | [diff] [blame] | 758 | # Verifying onoscell option |
Hari Krishna | 03f530e | 2015-07-10 17:28:27 -0700 | [diff] [blame] | 759 | if options.onoscell: |
| 760 | main.onoscell = options.onoscell |
Hari Krishna | be4b97b | 2015-07-15 12:19:43 -0700 | [diff] [blame] | 761 | main.onosIPs = [] |
| 762 | main.mnIP = "" |
| 763 | cellCMD = ". ~/.profile; cell "+main.onoscell |
| 764 | output=subprocess.check_output( ["bash", '-c', cellCMD] ) |
| 765 | splitOutput = output.splitlines() |
| 766 | for i in range( len(splitOutput) ): |
| 767 | if( re.match( "OCN", splitOutput[i] ) ): |
| 768 | mnNode=splitOutput[i].split("=") |
| 769 | main.mnIP = mnNode[1] |
| 770 | # cell already sorts OC variables in bash, so no need to sort in TestON |
| 771 | if( re.match( "OC[1-9]", splitOutput[i] ) ): |
| 772 | onosNodes = splitOutput[i].split("=") |
| 773 | main.onosIPs.append( onosNodes[1] ) |
Hari Krishna | 03f530e | 2015-07-10 17:28:27 -0700 | [diff] [blame] | 774 | else : |
| 775 | main.onoscell = main.FALSE |
| 776 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 777 | def verifyTestScript(options): |
| 778 | ''' |
| 779 | Verifyies test script. |
| 780 | ''' |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 781 | main.openspeak = openspeak.OpenSpeak() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 782 | openspeakfile = main.testDir+"/" + main.TEST + "/" + main.TEST + ".ospk" |
| 783 | testfile = main.testDir+"/" + main.TEST + "/" + main.TEST + ".py" |
Jon Hall | 4450624 | 2015-07-29 17:40:26 -0700 | [diff] [blame] | 784 | if os.path.exists(openspeakfile): |
| 785 | # Openspeak file found, compiling to python |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 786 | main.openspeak.compiler(openspeakfile=openspeakfile,writetofile=1) |
| 787 | elif os.path.exists(testfile): |
Jon Hall | 4450624 | 2015-07-29 17:40:26 -0700 | [diff] [blame] | 788 | # No openspeak found, using python file instead |
| 789 | pass |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 790 | else: |
Jon Hall | 4450624 | 2015-07-29 17:40:26 -0700 | [diff] [blame] | 791 | print "\nThere is no \""+main.TEST+"\" test script.\nPlease provide a " +\ |
| 792 | "Python or OpenSpeak test script in the tests folder: " +\ |
| 793 | main.testDir+"/" + main.TEST + "/" |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 794 | __builtin__.testthread = None |
| 795 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 796 | try : |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 797 | testModule = __import__(main.classPath, globals(), locals(), [main.TEST], -1) |
Jon Hall | 1306a56 | 2015-09-04 11:21:24 -0700 | [diff] [blame] | 798 | except ImportError: |
Jon Hall | 4450624 | 2015-07-29 17:40:26 -0700 | [diff] [blame] | 799 | print "There was an import error, it might mean that there is no test named "+main.TEST |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 800 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 801 | |
| 802 | testClass = getattr(testModule, main.TEST) |
| 803 | main.testObject = testClass() |
| 804 | load_parser() |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 805 | main.params = main.parser.parseParams(main.classPath) |
| 806 | main.topology = main.parser.parseTopology(main.classPath) |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 807 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 808 | def verifyParams(): |
| 809 | try : |
| 810 | main.params = main.params['PARAMS'] |
Jon Hall | 1306a56 | 2015-09-04 11:21:24 -0700 | [diff] [blame] | 811 | except KeyError: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 812 | print "Error with the params file: Either the file not specified or the format is not correct" |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 813 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 814 | try : |
| 815 | main.topology = main.topology['TOPOLOGY'] |
Jon Hall | 1306a56 | 2015-09-04 11:21:24 -0700 | [diff] [blame] | 816 | except KeyError: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 817 | print "Error with the Topology file: Either the file not specified or the format is not correct" |
| 818 | main.exit() |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 819 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 820 | def load_parser() : |
| 821 | ''' |
| 822 | It facilitates the loading customised parser for topology and params file. |
| 823 | It loads parser mentioned in tab named parser of teston.cfg file. |
| 824 | It also loads default xmlparser if no parser have specified in teston.cfg file. |
| 825 | |
| 826 | ''' |
| 827 | confighash = main.configDict |
| 828 | if 'file' in confighash['config']['parser'] and 'class' in confighash['config']['parser']: |
Jon Hall | 4450624 | 2015-07-29 17:40:26 -0700 | [diff] [blame] | 829 | path = confighash['config']['parser']['file'] |
| 830 | if path != None or confighash['config']['parser']['class']!= None: |
| 831 | try: |
| 832 | module = re.sub( r".py\s*$", "", path ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 833 | moduleList = module.split("/") |
| 834 | newModule = ".".join([moduleList[len(moduleList) - 2],moduleList[len(moduleList) - 1]]) |
Jon Hall | 4450624 | 2015-07-29 17:40:26 -0700 | [diff] [blame] | 835 | parsingClass = confighash['config']['parser']['class'] |
| 836 | parsingModule = __import__(newModule, globals(), locals(), [parsingClass], -1) |
| 837 | parsingClass = getattr(parsingModule, parsingClass) |
| 838 | main.parser = parsingClass() |
| 839 | #hashobj = main.parser.parseParams(main.classPath) |
| 840 | if hasattr(main.parser,"parseParams") and hasattr(main.parser,"parseTopology") and hasattr(main.parser,"parse"): |
| 841 | pass |
| 842 | else: |
| 843 | print "Invalid parser format" |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 844 | main.exit() |
Jon Hall | 4450624 | 2015-07-29 17:40:26 -0700 | [diff] [blame] | 845 | except ImportError: |
| 846 | print "Could not find the file " + path + " using default parser." |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 847 | load_defaultParser() |
Jon Hall | 4450624 | 2015-07-29 17:40:26 -0700 | [diff] [blame] | 848 | elif confighash['config']['parser']['file'] == None or confighash['config']['parser']['class'] == None: |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 849 | load_defaultParser() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 850 | else: |
| 851 | load_defaultParser() |
| 852 | |
| 853 | def load_defaultParser(): |
| 854 | ''' |
| 855 | It will load the default parser which is xml parser to parse the params and topology file. |
| 856 | ''' |
| 857 | moduleList = main.parserPath.split("/") |
| 858 | newModule = ".".join([moduleList[len(moduleList) - 2],moduleList[len(moduleList) - 1]]) |
| 859 | try : |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 860 | parsingClass = main.parsingClass |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 861 | parsingModule = __import__(newModule, globals(), locals(), [parsingClass], -1) |
| 862 | parsingClass = getattr(parsingModule, parsingClass) |
| 863 | main.parser = parsingClass() |
| 864 | if hasattr(main.parser,"parseParams") and hasattr(main.parser,"parseTopology") and hasattr(main.parser,"parse") : |
| 865 | pass |
| 866 | else: |
| 867 | main.exit() |
| 868 | |
| 869 | except ImportError: |
| 870 | print sys.exc_info()[1] |
| 871 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 872 | def load_logger() : |
| 873 | ''' |
| 874 | It facilitates the loading customised parser for topology and params file. |
| 875 | It loads parser mentioned in tab named parser of teston.cfg file. |
| 876 | It also loads default xmlparser if no parser have specified in teston.cfg file. |
| 877 | |
| 878 | ''' |
| 879 | confighash = main.configDict |
| 880 | if 'file' in confighash['config']['logger'] and 'class' in confighash['config']['logger']: |
Jon Hall | 4450624 | 2015-07-29 17:40:26 -0700 | [diff] [blame] | 881 | path = confighash['config']['logger']['file'] |
| 882 | if path != None or confighash['config']['logger']['class']!= None : |
| 883 | try: |
| 884 | module = re.sub( r".py\s*$", "", path ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 885 | moduleList = module.split("/") |
| 886 | newModule = ".".join([moduleList[len(moduleList) - 2],moduleList[len(moduleList) - 1]]) |
Jon Hall | 4450624 | 2015-07-29 17:40:26 -0700 | [diff] [blame] | 887 | loggerClass = confighash['config']['logger']['class'] |
| 888 | loggerModule = __import__(newModule, globals(), locals(), [loggerClass], -1) |
| 889 | loggerClass = getattr(loggerModule, loggerClass) |
| 890 | main.logger = loggerClass() |
| 891 | #hashobj = main.parser.parseParams(main.classPath) |
| 892 | except ImportError: |
| 893 | print "Could not find the file " + path + " using default logger." |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 894 | load_defaultlogger() |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 895 | elif confighash['config']['parser']['file'] == None or confighash['config']['parser']['class'] == None : |
| 896 | load_defaultlogger() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 897 | else: |
| 898 | load_defaultlogger() |
| 899 | |
| 900 | def load_defaultlogger(): |
| 901 | ''' |
| 902 | It will load the default parser which is xml parser to parse the params and topology file. |
| 903 | ''' |
| 904 | moduleList = main.loggerPath.split("/") |
| 905 | newModule = ".".join([moduleList[len(moduleList) - 2],moduleList[len(moduleList) - 1]]) |
| 906 | try : |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 907 | loggerClass = main.loggerClass |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 908 | loggerModule = __import__(newModule, globals(), locals(), [loggerClass], -1) |
| 909 | loggerClass = getattr(loggerModule, loggerClass) |
| 910 | main.logger = loggerClass() |
| 911 | |
| 912 | except ImportError: |
| 913 | print sys.exc_info()[1] |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 914 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 915 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 916 | def _echo(self): |
| 917 | print "THIS IS ECHO" |