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