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