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