admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | ''' |
| 3 | Created on 22-Oct-2012 |
| 4 | |
| 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 |
| 19 | along with TestON. If not, see <http://www.gnu.org/licenses/>. |
| 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 |
| 34 | module = new.module("test") |
| 35 | import openspeak |
| 36 | global path, drivers_path, core_path, tests_path,logs_path |
| 37 | path = re.sub("(core|bin)$", "", os.getcwd()) |
| 38 | drivers_path = path+"drivers/" |
| 39 | core_path = path+"core" |
| 40 | tests_path = path+"tests" |
| 41 | logs_path = path+"logs/" |
| 42 | config_path = path + "config/" |
| 43 | sys.path.append(path) |
| 44 | sys.path.append( drivers_path) |
| 45 | sys.path.append(core_path ) |
| 46 | sys.path.append(tests_path) |
| 47 | |
| 48 | from core.utilities import Utilities |
| 49 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 50 | |
| 51 | class TestON: |
| 52 | ''' |
| 53 | |
| 54 | TestON will initiate the specified test. |
| 55 | The main tasks are : |
| 56 | * Initiate the required Component handles for the test. |
| 57 | * Create Log file Handles. |
| 58 | |
| 59 | ''' |
| 60 | def __init__(self,options): |
| 61 | ''' |
| 62 | Initialise the component handles specified in the topology file of the specified test. |
| 63 | |
| 64 | ''' |
| 65 | # Initialization of the variables. |
| 66 | __builtin__.main = self |
Jon Hall | a118598 | 2014-09-15 14:55:10 -0700 | [diff] [blame] | 67 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 68 | __builtin__.path = path |
| 69 | __builtin__.utilities = Utilities() |
| 70 | self.TRUE = 1 |
| 71 | self.FALSE = 0 |
| 72 | self.ERROR = -1 |
| 73 | self.FAIL = False |
| 74 | self.PASS = True |
| 75 | self.CASERESULT = self.TRUE |
| 76 | self.init_result = self.TRUE |
| 77 | self.testResult = "Summary" |
| 78 | self.stepName ="" |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 79 | self.EXPERIMENTAL_MODE = False |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 80 | self.test_target = None |
| 81 | self.lastcommand = None |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 82 | self.testDir = tests_path |
| 83 | self.configFile = config_path + "teston.cfg" |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 84 | self.parsingClass = "xmlparser" |
| 85 | self.parserPath = core_path + "/xmlparser" |
| 86 | self.loggerPath = core_path + "/logger" |
| 87 | self.loggerClass = "Logger" |
| 88 | self.logs_path = logs_path |
| 89 | self.driver = '' |
Jon Hall | a118598 | 2014-09-15 14:55:10 -0700 | [diff] [blame] | 90 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 91 | |
| 92 | self.configparser() |
| 93 | verifyOptions(options) |
| 94 | load_logger() |
| 95 | self.componentDictionary = {} |
| 96 | self.componentDictionary = self.topology ['COMPONENT'] |
| 97 | self.driversList=[] |
| 98 | if type(self.componentDictionary) == str : |
| 99 | self.componentDictionary = dict(self.componentDictionary) |
| 100 | |
| 101 | for component in self.componentDictionary : |
| 102 | self.driversList.append(self.componentDictionary[component]['type']) |
| 103 | |
| 104 | self.driversList = list(set(self.driversList)) # Removing duplicates. |
| 105 | # Checking the test_target option set for the component or not |
| 106 | if type(self.componentDictionary) == dict: |
| 107 | for component in self.componentDictionary.keys(): |
| 108 | if 'test_target' in self.componentDictionary[component].keys(): |
| 109 | self.test_target = component |
| 110 | |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 111 | # Checking for the openspeak file and test script |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 112 | self.logger.initlog(self) |
| 113 | |
| 114 | # Creating Drivers Handles |
| 115 | initString = "\n"+"*" * 30+"\n CASE INIT \n"+"*" * 30+"\n" |
| 116 | self.log.exact(initString) |
| 117 | self.driverObject = {} |
| 118 | self.random_order = 111 # Random order id to connect the components |
| 119 | components_connect_order = {} |
| 120 | #component_list.append() |
| 121 | if type(self.componentDictionary) == dict: |
| 122 | for component in self.componentDictionary.keys(): |
| 123 | self.componentDictionary[component]['connect_order'] = self.componentDictionary[component]['connect_order'] if ('connect_order' in self.componentDictionary[component].keys()) else str(self.get_random()) |
| 124 | components_connect_order[component] = eval(self.componentDictionary[component]['connect_order']) |
| 125 | #Ordering components based on the connect order. |
| 126 | ordered_component_list =sorted(components_connect_order, key=lambda key: components_connect_order[key]) |
| 127 | print ordered_component_list |
| 128 | |
| 129 | for component in ordered_component_list: |
| 130 | self.componentInit(component) |
| 131 | |
| 132 | def configparser(self): |
| 133 | ''' |
| 134 | It will parse the config file (teston.cfg) and return as dictionary |
| 135 | ''' |
| 136 | matchFileName = re.match(r'(.*)\.cfg', self.configFile, re.M | re.I) |
| 137 | if matchFileName: |
| 138 | xml = open(self.configFile).read() |
| 139 | try : |
| 140 | self.configDict = xmldict.xml_to_dict(xml) |
| 141 | return self.configDict |
| 142 | except : |
| 143 | print "There is no such file to parse " + self.configFile |
| 144 | |
| 145 | def componentInit(self,component): |
| 146 | ''' |
| 147 | This method will initialize specified component |
| 148 | ''' |
Jon Hall | eb3a684 | 2015-02-18 15:17:51 -0800 | [diff] [blame] | 149 | import importlib |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 150 | global driver_options |
| 151 | self.log.info("Creating component Handle: "+component) |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 152 | driver_options = {} |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 153 | if 'COMPONENTS' in self.componentDictionary[component].keys(): |
| 154 | driver_options =dict(self.componentDictionary[component]['COMPONENTS']) |
| 155 | |
| 156 | driver_options['name']=component |
| 157 | driverName = self.componentDictionary[component]['type'] |
| 158 | driver_options ['type'] = driverName |
| 159 | |
| 160 | classPath = self.getDriverPath(driverName.lower()) |
Jon Hall | eb3a684 | 2015-02-18 15:17:51 -0800 | [diff] [blame] | 161 | driverModule = importlib.import_module(classPath) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 162 | driverClass = getattr(driverModule, driverName) |
| 163 | driverObject = driverClass() |
| 164 | |
| 165 | connect_result = driverObject.connect(user_name = self.componentDictionary[component]['user'] if ('user' in self.componentDictionary[component].keys()) else getpass.getuser(), |
| 166 | ip_address= self.componentDictionary[component]['host'] if ('host' in self.componentDictionary[component].keys()) else 'localhost', |
| 167 | pwd = self.componentDictionary[component]['password'] if ('password' in self.componentDictionary[component].keys()) else 'changeme', |
| 168 | port = self.componentDictionary[component]['port'] if ('port' in self.componentDictionary[component].keys()) else None, |
| 169 | options = driver_options) |
| 170 | if not connect_result: |
| 171 | self.log.error("Exiting form the test execution because the connecting to the "+component+" component failed.") |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 172 | self.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 173 | |
| 174 | vars(self)[component] = driverObject |
| 175 | |
| 176 | def run(self): |
| 177 | ''' |
| 178 | The Execution of the test script's cases listed in the Test params file will be done here. |
| 179 | And Update each test case result. |
| 180 | This method will return TRUE if it executed all the test cases successfully, |
| 181 | else will retun FALSE |
| 182 | ''' |
| 183 | |
| 184 | self.testCaseResult = {} |
Jon Hall | a118598 | 2014-09-15 14:55:10 -0700 | [diff] [blame] | 185 | self.TOTAL_TC = 0 |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 186 | self.TOTAL_TC_RUN = 0 |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 187 | self.TOTAL_TC_PLANNED = 0 |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 188 | self.TOTAL_TC_NORESULT = 0 |
| 189 | self.TOTAL_TC_FAIL = 0 |
| 190 | self.TOTAL_TC_PASS = 0 |
Jon Hall | a118598 | 2014-09-15 14:55:10 -0700 | [diff] [blame] | 191 | self.TEST_ITERATION = 0 |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 192 | self.stepCount = 0 |
| 193 | self.CASERESULT = self.TRUE |
| 194 | |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 195 | import testparser |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 196 | testFile = self.tests_path + "/"+self.TEST + "/"+self.TEST + ".py" |
| 197 | test = testparser.TestParser(testFile) |
| 198 | self.testscript = test.testscript |
| 199 | self.code = test.getStepCode() |
Jon Hall | a118598 | 2014-09-15 14:55:10 -0700 | [diff] [blame] | 200 | repeat= int(self.params['repeat']) if ('repeat' in self.params) else 1 |
| 201 | main.TOTAL_TC_PLANNED = len(self.testcases_list)*repeat |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 202 | |
| 203 | result = self.TRUE |
Jon Hall | a118598 | 2014-09-15 14:55:10 -0700 | [diff] [blame] | 204 | while(repeat): |
| 205 | for self.CurrentTestCaseNumber in self.testcases_list: |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 206 | result = self.runCase(self.CurrentTestCaseNumber) |
| 207 | repeat-=1 |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 208 | return result |
| 209 | |
| 210 | def runCase(self,testCaseNumber): |
| 211 | self.CurrentTestCaseNumber = testCaseNumber |
| 212 | result = self.TRUE |
| 213 | self.stepCount = 0 |
| 214 | self.EXPERIMENTAL_MODE = self.FALSE |
| 215 | self.addCaseHeader() |
| 216 | self.testCaseNumber = str(testCaseNumber) |
| 217 | stopped = False |
| 218 | try : |
| 219 | self.stepList = self.code[self.testCaseNumber].keys() |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 220 | except KeyError: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 221 | main.log.error("There is no Test-Case "+ self.testCaseNumber) |
| 222 | return main.FALSE |
| 223 | |
| 224 | self.stepCount = 0 |
| 225 | while self.stepCount < len(self.code[self.testCaseNumber].keys()): |
| 226 | result = self.runStep(self.stepList,self.code,self.testCaseNumber) |
| 227 | if result == main.FALSE: |
| 228 | break |
| 229 | elif result == main.TRUE : |
| 230 | continue |
| 231 | |
| 232 | if not stopped : |
| 233 | self.testCaseResult[str(self.CurrentTestCaseNumber)] = self.CASERESULT |
| 234 | self.logger.updateCaseResults(self) |
| 235 | return result |
| 236 | |
| 237 | def runStep(self,stepList,code,testCaseNumber): |
| 238 | if not cli.pause: |
| 239 | try : |
| 240 | step = stepList[self.stepCount] |
| 241 | exec code[testCaseNumber][step] in module.__dict__ |
| 242 | self.stepCount = self.stepCount + 1 |
| 243 | except TypeError,e: |
Jon Hall | 20ead01 | 2015-02-18 14:00:42 -0800 | [diff] [blame] | 244 | print "Exception in the following section of code:" |
| 245 | print code[testCaseNumber][step] |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 246 | self.stepCount = self.stepCount + 1 |
Jon Hall | 20ead01 | 2015-02-18 14:00:42 -0800 | [diff] [blame] | 247 | self.log.exception(e) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 248 | return main.TRUE |
| 249 | |
| 250 | if cli.stop: |
| 251 | cli.stop = False |
| 252 | stopped = True |
| 253 | self.TOTAL_TC_NORESULT = self.TOTAL_TC_NORESULT + 1 |
| 254 | self.testCaseResult[str(self.CurrentTestCaseNumber)] = "Stopped" |
| 255 | self.logger.updateCaseResults(self) |
| 256 | result = self.cleanup() |
| 257 | return main.FALSE |
| 258 | |
| 259 | def addCaseHeader(self): |
| 260 | 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] | 261 | self.log.exact(caseHeader) |
| 262 | caseHeader = "\n"+"*" * 40 +"\nStart of Test Case"+str(self.CurrentTestCaseNumber)+" : " |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 263 | for driver in self.componentDictionary.keys(): |
| 264 | vars(self)[driver+'log'].info(caseHeader) |
| 265 | |
| 266 | def addCaseFooter(self): |
| 267 | if self.stepCount-1 > 0 : |
| 268 | previousStep = " "+str(self.CurrentTestCaseNumber)+"."+str(self.stepCount-1)+": "+ str(self.stepName) + "" |
| 269 | stepHeader = "\n"+"*" * 40+"\nEnd of Step "+previousStep+"\n"+"*" * 40+"\n" |
| 270 | |
| 271 | caseFooter = "\n"+"*" * 40+"\nEnd of Test case "+str(self.CurrentTestCaseNumber)+"\n"+"*" * 40+"\n" |
| 272 | |
| 273 | for driver in self.driversList: |
| 274 | vars(self)[driver].write(stepHeader+"\n"+caseFooter) |
| 275 | |
| 276 | def cleanup(self): |
| 277 | ''' |
| 278 | Release all the component handles and the close opened file handles. |
| 279 | This will return TRUE if all the component handles and log handles closed properly, |
| 280 | else return FALSE |
| 281 | |
| 282 | ''' |
| 283 | result = self.TRUE |
| 284 | self.logger.testSummary(self) |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 285 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 286 | #self.reportFile.close() |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 287 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 288 | |
admin | 2c7034f | 2013-08-02 15:09:17 -0700 | [diff] [blame] | 289 | #utilities.send_mail() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 290 | try : |
| 291 | for component in self.componentDictionary.keys(): |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 292 | tempObject = vars(self)[component] |
| 293 | print "Disconnecting " + str(tempObject) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 294 | tempObject.disconnect() |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 295 | #tempObject.execute(cmd="exit",prompt="(.*)",timeout=120) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 296 | |
| 297 | except(Exception): |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 298 | self.log.exception( "Exception while disconnecting from " + |
| 299 | str( component ) ) |
Jon Hall | a118598 | 2014-09-15 14:55:10 -0700 | [diff] [blame] | 300 | #print " There is an error with closing hanldes" |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 301 | result = self.FALSE |
| 302 | # Closing all the driver's session files |
| 303 | for driver in self.componentDictionary.keys(): |
| 304 | vars(self)[driver].close_log_handles() |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 305 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 306 | return result |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 307 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 308 | def pause(self): |
| 309 | ''' |
| 310 | This function will pause the test's execution, and will continue after user provide 'resume' command. |
| 311 | ''' |
| 312 | __builtin__.testthread.pause() |
| 313 | |
| 314 | def onfail(self,*components): |
| 315 | ''' |
| 316 | When test step failed, calling all the components onfail. |
| 317 | ''' |
| 318 | |
| 319 | if not components: |
| 320 | try : |
| 321 | for component in self.componentDictionary.keys(): |
| 322 | tempObject = vars(self)[component] |
| 323 | result = tempObject.onfail() |
| 324 | except(Exception),e: |
| 325 | print str(e) |
| 326 | result = self.FALSE |
| 327 | |
| 328 | else: |
| 329 | try : |
| 330 | for component in components: |
| 331 | tempObject = vars(self)[component] |
| 332 | result = tempObject.onfail() |
| 333 | except(Exception),e: |
| 334 | print str(e) |
| 335 | result = self.FALSE |
| 336 | |
| 337 | |
| 338 | def getDriverPath(self,driverName): |
| 339 | ''' |
| 340 | Based on the component 'type' specified in the params , this method will find the absolute path , |
| 341 | by recursively searching the name of the component. |
| 342 | ''' |
| 343 | import commands |
| 344 | |
| 345 | cmd = "find "+drivers_path+" -name "+driverName+".py" |
| 346 | result = commands.getoutput(cmd) |
| 347 | |
| 348 | result_array = str(result).split('\n') |
| 349 | result_count = 0 |
| 350 | |
| 351 | for drivers_list in result_array: |
| 352 | result_count = result_count+1 |
| 353 | if result_count > 1 : |
| 354 | print "found "+driverName+" "+ str(result_count) + " times"+str(result_array) |
| 355 | self.exit() |
| 356 | |
| 357 | result = re.sub("(.*)drivers","",result) |
| 358 | result = re.sub("\.py","",result) |
| 359 | result = re.sub("\.pyc","",result) |
| 360 | result = re.sub("\/",".",result) |
| 361 | result = "drivers"+result |
| 362 | return result |
| 363 | |
| 364 | |
| 365 | def step(self,stepDesc): |
| 366 | ''' |
| 367 | The step information of the test-case will append to the logs. |
| 368 | ''' |
| 369 | previousStep = " "+str(self.CurrentTestCaseNumber)+"."+str(self.stepCount-1)+": "+ str(self.stepName) + "" |
| 370 | self.stepName = stepDesc |
| 371 | |
| 372 | stepName = " "+str(self.CurrentTestCaseNumber)+"."+str(self.stepCount)+": "+ str(stepDesc) + "" |
| 373 | try : |
| 374 | if self.stepCount == 0: |
| 375 | stepName = " INIT : Initializing the test case :"+self.CurrentTestCase |
| 376 | except AttributeError: |
| 377 | stepName = " INIT : Initializing the test case :"+str(self.CurrentTestCaseNumber) |
| 378 | |
| 379 | self.log.step(stepName) |
| 380 | stepHeader = "" |
| 381 | if self.stepCount > 1 : |
| 382 | stepHeader = "\n"+"-"*45+"\nEnd of Step "+previousStep+"\n"+"-"*45+"\n" |
| 383 | |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 384 | stepHeader += "\n"+"-"*45+"\nStart of Step"+stepName+"\n"+"-"*45+"\n" |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 385 | for driver in self.componentDictionary.keys(): |
| 386 | vars(self)[driver+'log'].info(stepHeader) |
| 387 | |
| 388 | def case(self,testCaseName): |
| 389 | ''' |
| 390 | Test's each test-case information will append to the logs. |
| 391 | ''' |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 392 | self.CurrentTestCase = testCaseName |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 393 | testCaseName = " " + str(testCaseName) + "" |
| 394 | self.log.case(testCaseName) |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 395 | caseHeader = testCaseName+"\n"+"*" * 40+"\n" |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 396 | for driver in self.componentDictionary.keys(): |
| 397 | vars(self)[driver+'log'].info(caseHeader) |
| 398 | |
| 399 | def testDesc(self,description): |
| 400 | ''' |
| 401 | Test description will append to the logs. |
| 402 | ''' |
| 403 | description = "Test Description : " + str (description) + "" |
| 404 | self.log.info(description) |
| 405 | |
| 406 | def _getTest(self): |
| 407 | ''' |
| 408 | This method will parse the test script to find required test information. |
| 409 | ''' |
| 410 | testFile = self.tests_path + "/"+self.TEST + "/"+self.TEST + ".py" |
| 411 | testFileHandler = open(testFile, 'r') |
| 412 | testFileList = testFileHandler.readlines() |
| 413 | testFileHandler.close() |
| 414 | #self.TOTAL_TC_PLANNED = 0 |
| 415 | counter = 0 |
| 416 | for index in range(len(testFileList)): |
| 417 | lineMatch = re.match('\s+def CASE(\d+)(.*):',testFileList[index],0) |
| 418 | if lineMatch: |
| 419 | counter = counter + 1 |
Jon Hall | a118598 | 2014-09-15 14:55:10 -0700 | [diff] [blame] | 420 | self.TC_PLANNED = len(self.testcases_list) |
| 421 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 422 | |
| 423 | def response_parser(self,response, return_format): |
| 424 | ''' It will load the default response parser ''' |
| 425 | response_dict = {} |
| 426 | response_dict = self.response_to_dict(response, return_format) |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 427 | return_format_string = self.dict_to_return_format(response,return_format,response_dict) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 428 | return return_format_string |
| 429 | |
| 430 | def response_to_dict(self,response,return_format): |
| 431 | |
| 432 | response_dict = {} |
| 433 | json_match = re.search('^\s*{', response) |
| 434 | xml_match = re.search('^\s*\<', response) |
| 435 | ini_match = re.search('^\s*\[', response) |
| 436 | if json_match : |
| 437 | main.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] | 438 | # Formatting the json string |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 439 | |
| 440 | response = re.sub(r"{\s*'?(\w)", r'{"\1', response) |
| 441 | response = re.sub(r",\s*'?(\w)", r',"\1', response) |
| 442 | response = re.sub(r"(\w)'?\s*:", r'\1":', response) |
| 443 | response = re.sub(r":\s*'(\w)'\s*([,}])", r':"\1"\2', response) |
| 444 | |
| 445 | try : |
| 446 | import json |
| 447 | response_dict = json.loads(response) |
Jon Hall | 20ead01 | 2015-02-18 14:00:42 -0800 | [diff] [blame] | 448 | except Exception, e: |
| 449 | main.log.exception(e) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 450 | main.log.error("Json Parser is unable to parse the string") |
| 451 | return response_dict |
| 452 | |
| 453 | elif ini_match : |
| 454 | main.log.info(" Response is in 'INI' format and Converting to '"+return_format+"' format") |
| 455 | from configobj import ConfigObj |
| 456 | response_file = open("respnse_file.temp",'w') |
| 457 | response_file.write(response) |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 458 | response_file.close() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 459 | response_dict = ConfigObj("respnse_file.temp") |
| 460 | return response_dict |
| 461 | |
| 462 | elif xml_match : |
| 463 | main.log.info(" Response is in 'XML' format and Converting to '"+return_format+"' format") |
| 464 | try : |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 465 | response_dict = xmldict.xml_to_dict("<response> "+str(response)+" </response>") |
| 466 | except Exception, e: |
Jon Hall | 20ead01 | 2015-02-18 14:00:42 -0800 | [diff] [blame] | 467 | main.log.exception(e) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 468 | return response_dict |
| 469 | |
| 470 | def dict_to_return_format(self,response,return_format,response_dict): |
| 471 | |
| 472 | if return_format =='table' : |
| 473 | ''' Will return in table format''' |
| 474 | to_do = "Call the table output formatter" |
| 475 | global response_table |
| 476 | response_table = '\n' |
| 477 | response_table = response_table +'\t'.join(response_dict)+"\n" |
| 478 | |
| 479 | def get_table(value_to_convert): |
| 480 | ''' This will parse the dictionary recusrsively and print as table format''' |
| 481 | table_data = "" |
| 482 | if type(value_to_convert) == dict : |
| 483 | table_data = table_data +'\t'.join(value_to_convert)+"\n" |
| 484 | for temp_val in value_to_convert.values() : |
| 485 | table_data = table_data + get_table(temp_val) |
| 486 | else : |
| 487 | table_data = table_data + str(value_to_convert) +"\t" |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 488 | return table_data |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 489 | |
| 490 | for value in response_dict.values() : |
| 491 | response_table = response_table + get_table(value) |
| 492 | |
| 493 | |
| 494 | |
| 495 | #response_table = response_table + '\t'.join(response_dict.values()) |
| 496 | |
| 497 | return response_table |
| 498 | |
| 499 | elif return_format =='config': |
| 500 | ''' Will return in config format''' |
| 501 | to_do = 'Call dict to config coverter' |
| 502 | response_string = str(response_dict) |
| 503 | print response_string |
| 504 | response_config = re.sub(",", "\n\t", response_string) |
| 505 | response_config = re.sub("u\'", "\'", response_config) |
| 506 | response_config = re.sub("{", "", response_config) |
| 507 | response_config = re.sub("}", "\n", response_config) |
| 508 | response_config = re.sub(":", " =", response_config) |
| 509 | return "[response]\n\t "+response_config |
| 510 | |
| 511 | elif return_format == 'xml': |
| 512 | ''' Will return in xml format''' |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 513 | response_xml = xmldict.dict_to_xml(response_dict) |
| 514 | response_xml = re.sub(">\s*<", ">\n<", response_xml) |
| 515 | return "\n"+response_xml |
| 516 | |
| 517 | elif return_format == 'json': |
| 518 | ''' Will return in json format''' |
| 519 | to_do = 'Call dict to xml coverter' |
| 520 | import json |
| 521 | response_json = json.dumps(response_dict) |
| 522 | return response_json |
| 523 | |
| 524 | def get_random(self): |
| 525 | self.random_order = self.random_order + 1 |
| 526 | return self.random_order |
| 527 | |
| 528 | def exit(self): |
| 529 | __builtin__.testthread = None |
| 530 | sys.exit() |
| 531 | |
| 532 | def verifyOptions(options): |
| 533 | ''' |
| 534 | This will verify the command line options and set to default values, if any option not given in command line. |
| 535 | ''' |
| 536 | import pprint |
| 537 | pp = pprint.PrettyPrinter(indent=4) |
| 538 | |
| 539 | #pp.pprint(options) |
| 540 | verifyTest(options) |
| 541 | verifyExample(options) |
| 542 | verifyTestScript(options) |
| 543 | verifyParams() |
| 544 | verifyLogdir(options) |
| 545 | verifyMail(options) |
| 546 | verifyTestCases(options) |
| 547 | |
| 548 | def verifyTest(options): |
| 549 | if options.testname: |
| 550 | main.TEST = options.testname |
| 551 | main.classPath = "tests."+main.TEST+"."+main.TEST |
| 552 | main.tests_path = tests_path |
| 553 | elif options.example : |
| 554 | main.TEST = options.example |
| 555 | main.tests_path = path+"/examples/" |
| 556 | main.classPath = "examples."+main.TEST+"."+main.TEST |
| 557 | else : |
| 558 | print "Test or Example not specified please specify the --test <test name > or --example <example name>" |
| 559 | self.exit() |
| 560 | |
| 561 | def verifyExample(options): |
| 562 | if options.example: |
| 563 | main.testDir = path+'/examples/' |
| 564 | main.tests_path = path+"/examples/" |
| 565 | main.classPath = "examples."+main.TEST+"."+main.TEST |
| 566 | |
| 567 | def verifyLogdir(options): |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 568 | #Verifying Log directory option |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 569 | if options.logdir: |
| 570 | main.logdir = options.logdir |
| 571 | else : |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 572 | main.logdir = main.FALSE |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 573 | |
| 574 | def verifyMail(options): |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 575 | # Checking the mailing list |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 576 | if options.mail: |
| 577 | main.mail = options.mail |
| 578 | elif main.params.has_key('mail'): |
| 579 | main.mail = main.params['mail'] |
| 580 | else : |
| 581 | main.mail = 'paxweb@paxterrasolutions.com' |
| 582 | |
| 583 | def verifyTestCases(options): |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 584 | #Getting Test cases list |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 585 | if options.testcases: |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 586 | testcases_list = options.testcases |
| 587 | #sys.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 588 | testcases_list = re.sub("(\[|\])", "", options.testcases) |
| 589 | main.testcases_list = eval(testcases_list+",") |
| 590 | else : |
| 591 | if 'testcases' in main.params.keys(): |
Jon Hall | a118598 | 2014-09-15 14:55:10 -0700 | [diff] [blame] | 592 | temp = eval(main.params['testcases']+",") |
| 593 | list1=[] |
| 594 | if type(temp[0])==list: |
| 595 | for test in temp: |
| 596 | for testcase in test: |
| 597 | if type(testcase)==int: |
| 598 | testcase=[testcase] |
| 599 | list1.extend(testcase) |
| 600 | else : |
| 601 | temp=list(temp) |
| 602 | for testcase in temp: |
| 603 | if type(testcase)==int: |
| 604 | testcase=[testcase] |
| 605 | list1.extend(testcase) |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 606 | main.testcases_list=list1 |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 607 | else : |
| 608 | 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] | 609 | sys.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 610 | |
| 611 | def verifyTestScript(options): |
| 612 | ''' |
| 613 | Verifyies test script. |
| 614 | ''' |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 615 | main.openspeak = openspeak.OpenSpeak() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 616 | openspeakfile = main.testDir+"/" + main.TEST + "/" + main.TEST + ".ospk" |
| 617 | testfile = main.testDir+"/" + main.TEST + "/" + main.TEST + ".py" |
| 618 | if os.path.exists(openspeakfile) : |
| 619 | main.openspeak.compiler(openspeakfile=openspeakfile,writetofile=1) |
| 620 | elif os.path.exists(testfile): |
| 621 | print '' |
| 622 | else: |
| 623 | print "\nThere is no :\""+main.TEST+"\" test, Please Provide OpenSpeak Script/ test script" |
| 624 | __builtin__.testthread = None |
| 625 | main.exit() |
| 626 | |
| 627 | try : |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 628 | testModule = __import__(main.classPath, globals(), locals(), [main.TEST], -1) |
| 629 | except(ImportError): |
Jon Hall | f8ecf73 | 2014-12-02 21:14:16 -0500 | [diff] [blame] | 630 | print "There was an import error, it might mean that there is no test like "+main.TEST |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 631 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 632 | |
| 633 | testClass = getattr(testModule, main.TEST) |
| 634 | main.testObject = testClass() |
| 635 | load_parser() |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 636 | main.params = main.parser.parseParams(main.classPath) |
| 637 | main.topology = main.parser.parseTopology(main.classPath) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 638 | |
| 639 | def verifyParams(): |
| 640 | try : |
| 641 | main.params = main.params['PARAMS'] |
| 642 | except(KeyError): |
| 643 | 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] | 644 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 645 | |
| 646 | try : |
| 647 | main.topology = main.topology['TOPOLOGY'] |
| 648 | except(KeyError): |
| 649 | print "Error with the Topology file: Either the file not specified or the format is not correct" |
| 650 | main.exit() |
| 651 | |
| 652 | def load_parser() : |
| 653 | ''' |
| 654 | It facilitates the loading customised parser for topology and params file. |
| 655 | It loads parser mentioned in tab named parser of teston.cfg file. |
| 656 | It also loads default xmlparser if no parser have specified in teston.cfg file. |
| 657 | |
| 658 | ''' |
| 659 | confighash = main.configDict |
| 660 | if 'file' in confighash['config']['parser'] and 'class' in confighash['config']['parser']: |
| 661 | if confighash['config']['parser']['file'] != None or confighash['config']['parser']['class']!= None : |
| 662 | if os.path.exists(confighash['config']['parser']['file']) : |
| 663 | module = re.sub(r".py\s*$","",confighash['config']['parser']['file']) |
| 664 | moduleList = module.split("/") |
| 665 | newModule = ".".join([moduleList[len(moduleList) - 2],moduleList[len(moduleList) - 1]]) |
| 666 | try : |
| 667 | parsingClass = confighash['config']['parser']['class'] |
| 668 | parsingModule = __import__(newModule, globals(), locals(), [parsingClass], -1) |
| 669 | parsingClass = getattr(parsingModule, parsingClass) |
| 670 | main.parser = parsingClass() |
| 671 | #hashobj = main.parser.parseParams(main.classPath) |
| 672 | if hasattr(main.parser,"parseParams") and hasattr(main.parser,"parseTopology") and hasattr(main.parser,"parse") : |
| 673 | |
| 674 | pass |
| 675 | else: |
| 676 | main.exit() |
| 677 | |
| 678 | except ImportError: |
| 679 | print sys.exc_info()[1] |
| 680 | main.exit() |
| 681 | else : |
| 682 | print "No Such File Exists !!"+ confighash['config']['parser']['file'] +"using default parser" |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 683 | load_defaultParser() |
| 684 | elif confighash['config']['parser']['file'] == None or confighash['config']['parser']['class'] == None : |
| 685 | load_defaultParser() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 686 | else: |
| 687 | load_defaultParser() |
| 688 | |
| 689 | def load_defaultParser(): |
| 690 | ''' |
| 691 | It will load the default parser which is xml parser to parse the params and topology file. |
| 692 | ''' |
| 693 | moduleList = main.parserPath.split("/") |
| 694 | newModule = ".".join([moduleList[len(moduleList) - 2],moduleList[len(moduleList) - 1]]) |
| 695 | try : |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 696 | parsingClass = main.parsingClass |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 697 | parsingModule = __import__(newModule, globals(), locals(), [parsingClass], -1) |
| 698 | parsingClass = getattr(parsingModule, parsingClass) |
| 699 | main.parser = parsingClass() |
| 700 | if hasattr(main.parser,"parseParams") and hasattr(main.parser,"parseTopology") and hasattr(main.parser,"parse") : |
| 701 | pass |
| 702 | else: |
| 703 | main.exit() |
| 704 | |
| 705 | except ImportError: |
| 706 | print sys.exc_info()[1] |
| 707 | |
| 708 | |
| 709 | def load_logger() : |
| 710 | ''' |
| 711 | It facilitates the loading customised parser for topology and params file. |
| 712 | It loads parser mentioned in tab named parser of teston.cfg file. |
| 713 | It also loads default xmlparser if no parser have specified in teston.cfg file. |
| 714 | |
| 715 | ''' |
| 716 | confighash = main.configDict |
| 717 | if 'file' in confighash['config']['logger'] and 'class' in confighash['config']['logger']: |
| 718 | if confighash['config']['logger']['file'] != None or confighash['config']['logger']['class']!= None : |
| 719 | if os.path.exists(confighash['config']['logger']['file']) : |
| 720 | module = re.sub(r".py\s*$","",confighash['config']['logger']['file']) |
| 721 | moduleList = module.split("/") |
| 722 | newModule = ".".join([moduleList[len(moduleList) - 2],moduleList[len(moduleList) - 1]]) |
| 723 | try : |
| 724 | loggerClass = confighash['config']['logger']['class'] |
| 725 | loggerModule = __import__(newModule, globals(), locals(), [loggerClass], -1) |
| 726 | loggerClass = getattr(loggerModule, loggerClass) |
| 727 | main.logger = loggerClass() |
| 728 | #hashobj = main.parser.parseParams(main.classPath) |
| 729 | |
| 730 | except ImportError: |
| 731 | print sys.exc_info()[1] |
| 732 | else : |
| 733 | print "No Such File Exists !!"+confighash['config']['logger']['file']+ "Using default logger" |
| 734 | load_defaultlogger() |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 735 | elif confighash['config']['parser']['file'] == None or confighash['config']['parser']['class'] == None : |
| 736 | load_defaultlogger() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 737 | else: |
| 738 | load_defaultlogger() |
| 739 | |
| 740 | def load_defaultlogger(): |
| 741 | ''' |
| 742 | It will load the default parser which is xml parser to parse the params and topology file. |
| 743 | ''' |
| 744 | moduleList = main.loggerPath.split("/") |
| 745 | newModule = ".".join([moduleList[len(moduleList) - 2],moduleList[len(moduleList) - 1]]) |
| 746 | try : |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 747 | loggerClass = main.loggerClass |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 748 | loggerModule = __import__(newModule, globals(), locals(), [loggerClass], -1) |
| 749 | loggerClass = getattr(loggerModule, loggerClass) |
| 750 | main.logger = loggerClass() |
| 751 | |
| 752 | except ImportError: |
| 753 | print sys.exc_info()[1] |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 754 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 755 | |
| 756 | def load_defaultlogger(): |
| 757 | ''' |
| 758 | It will load the default parser which is xml parser to parse the params and topology file. |
| 759 | ''' |
| 760 | moduleList = main.loggerPath.split("/") |
| 761 | newModule = ".".join([moduleList[len(moduleList) - 2],moduleList[len(moduleList) - 1]]) |
| 762 | try : |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 763 | loggerClass = main.loggerClass |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 764 | loggerModule = __import__(newModule, globals(), locals(), [loggerClass], -1) |
| 765 | loggerClass = getattr(loggerModule, loggerClass) |
| 766 | main.logger = loggerClass() |
| 767 | |
| 768 | except ImportError: |
| 769 | print sys.exc_info()[1] |
| 770 | main.exit() |
| 771 | |
| 772 | |
| 773 | |
| 774 | |
| 775 | def _echo(self): |
| 776 | print "THIS IS ECHO" |