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