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