admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | ''' |
| 3 | Created on 22-Oct-2012 |
Jeremy Ronquillo | b27ce4c | 2017-07-17 12:41:28 -0700 | [diff] [blame] | 4 | Copyright 2012 Open Networking Foundation (ONF) |
Jon Hall | 65844a3 | 2015-03-09 19:09:37 -0700 | [diff] [blame] | 5 | |
Jeremy Songster | ae01bba | 2016-07-11 15:39:17 -0700 | [diff] [blame] | 6 | Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>, |
| 7 | the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>, |
| 8 | or the System Testing Guide page at <https://wiki.onosproject.org/x/WYQg> |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 9 | |
| 10 | TestON is free software: you can redistribute it and/or modify |
| 11 | it under the terms of the GNU General Public License as published by |
| 12 | the Free Software Foundation, either version 2 of the License, or |
| 13 | (at your option) any later version. |
| 14 | |
| 15 | TestON is distributed in the hope that it will be useful, |
| 16 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 18 | GNU General Public License for more details. |
| 19 | |
| 20 | You should have received a copy of the GNU General Public License |
Jon Hall | 65844a3 | 2015-03-09 19:09:37 -0700 | [diff] [blame] | 21 | along with TestON. If not, see <http://www.gnu.org/licenses/>. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 22 | |
| 23 | |
| 24 | |
| 25 | teston is the main module. |
| 26 | |
| 27 | ''' |
| 28 | |
| 29 | import sys |
| 30 | import getpass |
| 31 | import os |
| 32 | import re |
| 33 | import __builtin__ |
| 34 | import new |
| 35 | import xmldict |
Jon Hall | 30b82fa | 2015-03-04 17:15:43 -0800 | [diff] [blame] | 36 | import importlib |
Jon Hall | 5b58673 | 2015-06-11 11:39:39 -0700 | [diff] [blame] | 37 | import threading |
Jeremy Ronquillo | 15ff107 | 2017-07-17 10:55:46 -0700 | [diff] [blame] | 38 | import pdb |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 39 | module = new.module( "test" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 40 | import openspeak |
Hari Krishna | be4b97b | 2015-07-15 12:19:43 -0700 | [diff] [blame] | 41 | import subprocess |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 42 | global path, drivers_path, core_path, tests_path, logs_path |
Jon Hall | 4450624 | 2015-07-29 17:40:26 -0700 | [diff] [blame] | 43 | location = os.path.abspath( os.path.dirname( __file__ ) ) |
| 44 | path = re.sub( "(core|bin)$", "", location ) |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 45 | drivers_path = path + "drivers" |
| 46 | core_path = path + "core" |
| 47 | tests_path = path + "tests" |
| 48 | logs_path = path + "logs/" |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 49 | config_path = path + "config/" |
Jon Hall | 4450624 | 2015-07-29 17:40:26 -0700 | [diff] [blame] | 50 | sys.path.append( path ) |
| 51 | sys.path.append( drivers_path ) |
| 52 | sys.path.append( core_path ) |
| 53 | sys.path.append( tests_path ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 54 | |
| 55 | from core.utilities import Utilities |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 56 | from core.Thread import Thread |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 57 | |
Jon Hall | ca31989 | 2017-06-15 15:25:22 -0700 | [diff] [blame] | 58 | class SkipCase( Exception ): |
| 59 | pass |
| 60 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 61 | class TestON: |
| 62 | ''' |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 63 | TestON will initiate the specified test. |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 64 | The main tasks are: |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 65 | * Initiate the required Component handles for the test. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 66 | * Create Log file Handles. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 67 | ''' |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 68 | def __init__( self, options ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 69 | ''' |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 70 | Initialise the component handles specified in the topology file of |
| 71 | the specified test. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 72 | ''' |
| 73 | # Initialization of the variables. |
| 74 | __builtin__.main = self |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 75 | __builtin__.path = path |
| 76 | __builtin__.utilities = Utilities() |
| 77 | self.TRUE = 1 |
| 78 | self.FALSE = 0 |
| 79 | self.ERROR = -1 |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 80 | self.NORESULT = 2 |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 81 | self.FAIL = False |
| 82 | self.PASS = True |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 83 | self.CASERESULT = self.ERROR |
| 84 | self.STEPRESULT = self.NORESULT |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 85 | self.init_result = self.TRUE |
| 86 | self.testResult = "Summary" |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 87 | self.stepName = "" |
| 88 | self.stepCache = "" |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 89 | self.EXPERIMENTAL_MODE = False |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 90 | self.test_target = None |
| 91 | self.lastcommand = None |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 92 | self.testDir = tests_path |
| 93 | self.configFile = config_path + "teston.cfg" |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 94 | self.parsingClass = "xmlparser" |
| 95 | self.parserPath = core_path + "/xmlparser" |
| 96 | self.loggerPath = core_path + "/logger" |
| 97 | self.loggerClass = "Logger" |
| 98 | self.logs_path = logs_path |
| 99 | self.driver = '' |
kelvin-onlab | fb52166 | 2015-02-27 09:52:40 -0800 | [diff] [blame] | 100 | self.Thread = Thread |
Jon Hall | 5b58673 | 2015-06-11 11:39:39 -0700 | [diff] [blame] | 101 | self.cleanupFlag = False |
| 102 | self.cleanupLock = threading.Lock() |
Jon Hall | 0fc0d45 | 2015-07-14 09:49:58 -0700 | [diff] [blame] | 103 | self.initiated = False |
Jon Hall | 65844a3 | 2015-03-09 19:09:37 -0700 | [diff] [blame] | 104 | |
Jon Hall | 2507978 | 2015-10-13 13:54:39 -0700 | [diff] [blame] | 105 | self.config = self.configparser() |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 106 | verifyOptions( options ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 107 | load_logger() |
| 108 | self.componentDictionary = {} |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 109 | self.componentDictionary = self.topology['COMPONENT'] |
| 110 | self.driversList = [] |
| 111 | if isinstance( self.componentDictionary, str): |
| 112 | self.componentDictionary = dict( self.componentDictionary ) |
Jon Hall | 65844a3 | 2015-03-09 19:09:37 -0700 | [diff] [blame] | 113 | |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 114 | for component in self.componentDictionary: |
| 115 | self.driversList.append( self.componentDictionary[component]['type'] ) |
Jon Hall | 65844a3 | 2015-03-09 19:09:37 -0700 | [diff] [blame] | 116 | |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 117 | self.driversList = list( set( self.driversList ) ) # Removing duplicates. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 118 | # Checking the test_target option set for the component or not |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 119 | if isinstance( self.componentDictionary, dict ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 120 | for component in self.componentDictionary.keys(): |
| 121 | if 'test_target' in self.componentDictionary[component].keys(): |
| 122 | self.test_target = component |
Jon Hall | 65844a3 | 2015-03-09 19:09:37 -0700 | [diff] [blame] | 123 | |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 124 | # Checking for the openspeak file and test script |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 125 | self.logger.initlog( self ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 126 | |
| 127 | # Creating Drivers Handles |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 128 | initString = "\n" + "*" * 30 + "\n CASE INIT \n" + "*" * 30 + "\n" |
| 129 | self.log.exact( initString ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 130 | self.driverObject = {} |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 131 | self.random_order = 111 # Random order id to connect the components |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 132 | components_connect_order = {} |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 133 | if isinstance( self.componentDictionary, dict ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 134 | for component in self.componentDictionary.keys(): |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 135 | if 'connect_order' not in self.componentDictionary[component].keys(): |
| 136 | self.componentDictionary[component]['connect_order'] = str( self.get_random() ) |
| 137 | components_connect_order[component] = eval( self.componentDictionary[component]['connect_order'] ) |
| 138 | # Ordering components based on the connect order. |
| 139 | ordered_component_list = sorted( components_connect_order, |
| 140 | key=lambda key: components_connect_order[key] ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 141 | print ordered_component_list |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 142 | for component in ordered_component_list: |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 143 | self.componentInit( component ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 144 | |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 145 | def configparser( self ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 146 | ''' |
| 147 | It will parse the config file (teston.cfg) and return as dictionary |
| 148 | ''' |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 149 | matchFileName = re.match( r'(.*)\.cfg', self.configFile, re.M | re.I ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 150 | if matchFileName: |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 151 | xml = open( self.configFile ).read() |
| 152 | try: |
| 153 | self.configDict = xmldict.xml_to_dict( xml ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 154 | return self.configDict |
Jon Hall | 1306a56 | 2015-09-04 11:21:24 -0700 | [diff] [blame] | 155 | except IOError: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 156 | print "There is no such file to parse " + self.configFile |
Jon Hall | 1306a56 | 2015-09-04 11:21:24 -0700 | [diff] [blame] | 157 | else: |
| 158 | print "There is no such file to parse " + self.configFile |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 159 | |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 160 | def componentInit( self, component ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 161 | ''' |
| 162 | This method will initialize specified component |
| 163 | ''' |
| 164 | global driver_options |
Jon Hall | 0fc0d45 | 2015-07-14 09:49:58 -0700 | [diff] [blame] | 165 | self.initiated = False |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 166 | self.log.info( "Creating component Handle: " + component ) |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 167 | driver_options = {} |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 168 | if 'COMPONENTS' in self.componentDictionary[component].keys(): |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 169 | driver_options = dict( self.componentDictionary[component]['COMPONENTS'] ) |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 170 | driver_options['name'] = component |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 171 | driverName = self.componentDictionary[component]['type'] |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 172 | driver_options['type'] = driverName |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 173 | |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 174 | classPath = self.getDriverPath( driverName.lower() ) |
| 175 | driverModule = importlib.import_module( classPath ) |
| 176 | driverClass = getattr( driverModule, driverName ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 177 | driverObject = driverClass() |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 178 | |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 179 | if "OCN" in self.componentDictionary[component]['host'] and\ |
| 180 | main.onoscell: |
Hari Krishna | be4b97b | 2015-07-15 12:19:43 -0700 | [diff] [blame] | 181 | self.componentDictionary[component]['host'] = main.mnIP |
| 182 | |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 183 | user_name = self.componentDictionary[component].get( 'user', |
| 184 | getpass.getuser() ) |
| 185 | ip_address = self.componentDictionary[component].get( 'host', |
| 186 | 'localhost' ) |
| 187 | pwd = self.componentDictionary[component].get( 'password', |
| 188 | 'changeme' ) |
| 189 | port = self.componentDictionary[component].get( 'port' ) |
| 190 | connect_result = driverObject.connect( user_name=user_name, |
| 191 | ip_address=ip_address, |
| 192 | pwd=pwd, |
| 193 | port=port, |
| 194 | options=driver_options) |
cameron@onlab.us | 5cc6a37 | 2015-05-11 17:18:07 -0700 | [diff] [blame] | 195 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 196 | if not connect_result: |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 197 | self.log.error( "Exiting from the test execution because connecting to the " + |
| 198 | component + " component failed." ) |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 199 | self.exit() |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 200 | |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 201 | vars( self )[component] = driverObject |
Jon Hall | 0fc0d45 | 2015-07-14 09:49:58 -0700 | [diff] [blame] | 202 | self.initiated = True |
Jon Hall | ca31989 | 2017-06-15 15:25:22 -0700 | [diff] [blame] | 203 | return driverObject |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 204 | |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 205 | def run( self ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 206 | ''' |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 207 | The Execution of the test script's cases listed in the Test params |
| 208 | file will be done here then update each test case result. |
| 209 | This method will return main.TRUE if it executed all the test cases |
| 210 | successfully, else will retun main.FALSE |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 211 | ''' |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 212 | self.testCaseResult = {} |
Jon Hall | a118598 | 2014-09-15 14:55:10 -0700 | [diff] [blame] | 213 | self.TOTAL_TC = 0 |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 214 | self.TOTAL_TC_RUN = 0 |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 215 | self.TOTAL_TC_PLANNED = 0 |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 216 | self.TOTAL_TC_NORESULT = 0 |
| 217 | self.TOTAL_TC_FAIL = 0 |
| 218 | self.TOTAL_TC_PASS = 0 |
Jon Hall | a118598 | 2014-09-15 14:55:10 -0700 | [diff] [blame] | 219 | self.TEST_ITERATION = 0 |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 220 | |
| 221 | # NOTE: number of main.step statements in the |
| 222 | # outer most level of the test case. used to |
| 223 | # execute code in smaller steps |
| 224 | self.stepCount = 0 |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 225 | self.CASERESULT = self.NORESULT |
| 226 | |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 227 | import testparser |
Jon Hall | 53c5e66 | 2016-04-13 16:06:56 -0700 | [diff] [blame] | 228 | test = testparser.TestParser( main.testFile ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 229 | self.testscript = test.testscript |
| 230 | self.code = test.getStepCode() |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 231 | repeat = int( self.params.get( 'repeat', 1 ) ) |
| 232 | self.TOTAL_TC_PLANNED = len( self.testcases_list ) * repeat |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 233 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 234 | result = self.TRUE |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 235 | while repeat: |
Jon Hall | a118598 | 2014-09-15 14:55:10 -0700 | [diff] [blame] | 236 | for self.CurrentTestCaseNumber in self.testcases_list: |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 237 | result = self.runCase( self.CurrentTestCaseNumber ) |
| 238 | repeat -= 1 |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 239 | return result |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 240 | |
Jon Hall | e234cc4 | 2015-08-31 15:26:47 -0700 | [diff] [blame] | 241 | def runCase( self, testCaseNumber ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 242 | self.CurrentTestCaseNumber = testCaseNumber |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 243 | self.CurrentTestCase = "" |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 244 | |
| 245 | # List of step results in a case. ANDed together to get the result |
| 246 | self.stepResultsList = [] |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 247 | self.stepName = "" |
Jon Hall | 783bbf9 | 2015-07-23 14:33:19 -0700 | [diff] [blame] | 248 | self.caseExplanation = "" |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 249 | result = self.TRUE |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 250 | |
| 251 | # NOTE: number of main.step statements in the |
| 252 | # outer most level of the test case. used to |
| 253 | # execute code in smaller steps |
| 254 | self.stepCount = 0 |
| 255 | |
| 256 | # NOTE: This is the current number of main.step()'s executed |
| 257 | # in a case. Used for logging. |
| 258 | self.stepNumber = 0 |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 259 | self.EXPERIMENTAL_MODE = self.FALSE |
| 260 | self.addCaseHeader() |
Jon Hall | e234cc4 | 2015-08-31 15:26:47 -0700 | [diff] [blame] | 261 | self.testCaseNumber = str( testCaseNumber ) |
| 262 | self.CASERESULT = self.NORESULT |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 263 | stopped = False |
Jon Hall | 5a72b71 | 2015-09-28 12:20:59 -0700 | [diff] [blame] | 264 | try: |
| 265 | self.code[self.testCaseNumber] |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 266 | except KeyError: |
Jon Hall | e234cc4 | 2015-08-31 15:26:47 -0700 | [diff] [blame] | 267 | self.log.error( "There is no Test-Case " + self.testCaseNumber ) |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 268 | return self.FALSE |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 269 | self.stepCount = 0 |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 270 | while self.stepCount < len( self.code[self.testCaseNumber].keys() ): |
| 271 | result = self.runStep( self.code, self.testCaseNumber ) |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 272 | if result == self.FALSE: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 273 | break |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 274 | elif result == self.TRUE: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 275 | continue |
Jon Hall | 5a72b71 | 2015-09-28 12:20:59 -0700 | [diff] [blame] | 276 | # stepResults format: ( stepNo[], stepName[], stepResult[], onFail[] ) |
| 277 | stepResults = self.stepResultsList |
Jon Hall | e234cc4 | 2015-08-31 15:26:47 -0700 | [diff] [blame] | 278 | if not stopped: |
| 279 | if self.CASERESULT == self.TRUE or self.CASERESULT == self.FALSE: |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 280 | # Result was already explitily set somewhere else like |
| 281 | # in skipCase() |
Jon Hall | e234cc4 | 2015-08-31 15:26:47 -0700 | [diff] [blame] | 282 | pass |
Jon Hall | 5a72b71 | 2015-09-28 12:20:59 -0700 | [diff] [blame] | 283 | elif all( self.TRUE == i for i in stepResults ): |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 284 | # ALL PASSED |
| 285 | self.CASERESULT = self.TRUE |
Jon Hall | 5a72b71 | 2015-09-28 12:20:59 -0700 | [diff] [blame] | 286 | elif self.FALSE in stepResults: |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 287 | # AT LEAST ONE FAILED |
| 288 | self.CASERESULT = self.FALSE |
Jon Hall | 5a72b71 | 2015-09-28 12:20:59 -0700 | [diff] [blame] | 289 | elif self.TRUE in stepResults: |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 290 | # AT LEAST ONE PASSED |
| 291 | self.CASERESULT = self.TRUE |
| 292 | else: |
| 293 | self.CASERESULT = self.NORESULT |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 294 | self.testCaseResult[str( self.CurrentTestCaseNumber )] = self.CASERESULT |
| 295 | self.logger.updateCaseResults( self ) |
Jon Hall | 783bbf9 | 2015-07-23 14:33:19 -0700 | [diff] [blame] | 296 | self.log.wiki( "<p>" + self.caseExplanation + "</p>" ) |
| 297 | self.log.summary( self.caseExplanation ) |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 298 | self.log.wiki( "<ul>" ) |
acsmars | e2d1ed1 | 2015-10-05 13:51:17 -0700 | [diff] [blame] | 299 | subcaseMessage = False |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 300 | for line in self.stepCache.splitlines(): |
acsmars | e2d1ed1 | 2015-10-05 13:51:17 -0700 | [diff] [blame] | 301 | if re.search( "[0-9]\.[0-9]", line ): # Step |
| 302 | if subcaseMessage: # End of Failure Message Printout |
| 303 | self.log.wiki( "</ul>\n" ) |
| 304 | subcaseMessage = False |
| 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" ) |
| 311 | else: # Substep |
| 312 | if not subcaseMessage: # Open Failure Message Printout |
| 313 | self.log.wiki( "<ul><li>" + line + "</li>\n" ) |
| 314 | subcaseMessage = True |
| 315 | else: # Add to Failure Message Printout |
| 316 | self.log.wiki( "<li>" + line + "</li>\n" ) |
acsmars | 27e62dd | 2015-10-06 11:35:47 -0700 | [diff] [blame] | 317 | if subcaseMessage: # End of Failure Message Printout for last item |
| 318 | self.log.wiki( "</ul>\n" ) |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 319 | self.log.wiki( "</ul>" ) |
| 320 | self.log.summary( self.stepCache ) |
| 321 | self.stepCache = "" |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 322 | return result |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 323 | |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 324 | def runStep( self, code, testCaseNumber ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 325 | if not cli.pause: |
Jon Hall | 5a72b71 | 2015-09-28 12:20:59 -0700 | [diff] [blame] | 326 | try: |
| 327 | step = self.stepCount |
| 328 | # stepResults format: ( stepNo, stepName, stepResult, onFail ) |
| 329 | # NOTE: This is needed to catch results of main.step()'s |
| 330 | # called inside functions or loops |
| 331 | self.stepResults = ( [], [], [], [] ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 332 | exec code[testCaseNumber][step] in module.__dict__ |
| 333 | self.stepCount = self.stepCount + 1 |
Jon Hall | 96b816f | 2015-11-03 12:00:56 -0800 | [diff] [blame] | 334 | self.parseStepResults( testCaseNumber ) |
Jon Hall | ca31989 | 2017-06-15 15:25:22 -0700 | [diff] [blame] | 335 | except SkipCase: # Raised in self.skipCase() |
Jon Hall | e234cc4 | 2015-08-31 15:26:47 -0700 | [diff] [blame] | 336 | self.log.warn( "Skipping the rest of CASE" + |
| 337 | str( testCaseNumber ) ) |
Jon Hall | 96b816f | 2015-11-03 12:00:56 -0800 | [diff] [blame] | 338 | self.parseStepResults( testCaseNumber ) |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 339 | self.stepResultsList.append( self.STEPRESULT ) |
Jon Hall | e234cc4 | 2015-08-31 15:26:47 -0700 | [diff] [blame] | 340 | self.stepCache += "\t\t" + self.onFailMsg + "\n" |
| 341 | self.stepCount = self.stepCount + 1 |
| 342 | return self.FALSE |
Jon Hall | c160635 | 2015-10-06 14:51:36 -0700 | [diff] [blame] | 343 | except StandardError as e: |
| 344 | try: |
| 345 | stepNo = self.stepResults[0][ self.stepNumber - 1 ] |
| 346 | except IndexError: |
| 347 | stepNo = "<IndexError>" |
| 348 | main.log.warn( "Error trying to get step number. " + |
| 349 | "It is likely between step " + |
Jon Hall | 6e70975 | 2016-02-01 13:38:46 -0800 | [diff] [blame] | 350 | str( self.stepNumber ) + " and step " + |
Jon Hall | c160635 | 2015-10-06 14:51:36 -0700 | [diff] [blame] | 351 | str( self.stepNumber + 1 ) ) |
| 352 | try: |
| 353 | stepName = self.stepResults[1][ self.stepNumber - 1 ] |
| 354 | except IndexError: |
| 355 | stepName = "<IndexError>" |
| 356 | self.log.error( "\nException in the following section of" + |
| 357 | " code: " + str( testCaseNumber ) + "." + |
| 358 | str( stepNo ) + ": " + stepName ) |
Jeremy | d9e4eb1 | 2016-04-13 12:09:06 -0700 | [diff] [blame] | 359 | self.log.error( str( e.__class__ ) + str( e.message ) ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 360 | self.stepCount = self.stepCount + 1 |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 361 | self.logger.updateCaseResults( self ) |
| 362 | # WIKI results |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 363 | self.log.wiki( "<ul>" ) |
| 364 | for line in self.stepCache.splitlines(): |
| 365 | if re.search( " - PASS$", line ): |
| 366 | self.log.wiki( "<li>" + line + " <ac:emoticon ac:name=\"tick\" /></li>\n" ) |
| 367 | elif re.search( " - FAIL$", line ): |
| 368 | self.log.wiki( "<li>" + line + " <ac:emoticon ac:name=\"cross\" /></li>\n" ) |
| 369 | elif re.search( " - No Result$", line ): |
| 370 | self.log.wiki( "<li>" + line + " <ac:emoticon ac:name=\"warning\" /></li>\n" ) |
Jon Hall | 9062761 | 2015-06-09 14:57:02 -0700 | [diff] [blame] | 371 | else: # Should only be on fail message |
| 372 | self.log.wiki( "<ul><li>" + line + "</li></ul>\n" ) |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 373 | self.log.wiki( "</ul>" ) |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 374 | # summary results |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 375 | self.log.summary( self.stepCache ) |
| 376 | self.stepCache = "" |
shahshreya | 957feaa | 2015-03-23 16:08:29 -0700 | [diff] [blame] | 377 | self.cleanup() |
Jon Hall | 00539b1 | 2015-04-03 13:55:46 -0700 | [diff] [blame] | 378 | self.exit() |
Jon Hall | e234cc4 | 2015-08-31 15:26:47 -0700 | [diff] [blame] | 379 | return self.TRUE |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 380 | if cli.stop: |
| 381 | cli.stop = False |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 382 | self.TOTAL_TC_NORESULT = self.TOTAL_TC_NORESULT + 1 |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 383 | self.testCaseResult[str( self.CurrentTestCaseNumber )] = "Stopped" |
| 384 | self.logger.updateCaseResults( self ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 385 | result = self.cleanup() |
Jon Hall | e234cc4 | 2015-08-31 15:26:47 -0700 | [diff] [blame] | 386 | return self.FALSE |
| 387 | |
Jon Hall | 96b816f | 2015-11-03 12:00:56 -0800 | [diff] [blame] | 388 | def parseStepResults( self, testCaseNumber ): |
| 389 | """ |
| 390 | Parse throught the step results for the wiki |
| 391 | """ |
| 392 | try: |
| 393 | # Iterate through each of the steps and print them |
| 394 | for index in range( len( self.stepResults[0] ) ): |
| 395 | # stepResults = ( stepNo, stepName, stepResult, onFail ) |
| 396 | stepNo = self.stepResults[0][ index ] |
| 397 | stepName = self.stepResults[1][ index ] |
| 398 | stepResult = self.stepResults[2][ index ] |
| 399 | onFail = self.stepResults[3][ index ] |
| 400 | self.stepCache += "\t" + str( testCaseNumber ) + "." |
| 401 | self.stepCache += str( stepNo ) + " " |
| 402 | self.stepCache += stepName + " - " |
| 403 | if stepResult == self.TRUE: |
| 404 | self.stepCache += "PASS\n" |
| 405 | elif stepResult == self.FALSE: |
| 406 | self.stepCache += "FAIL\n" |
| 407 | self.stepCache += "\t\t" + onFail + "\n" |
| 408 | else: |
| 409 | self.stepCache += "No Result\n" |
| 410 | self.stepResultsList.append( stepResult ) |
| 411 | except Exception: |
| 412 | self.log.exception( "Error parsing step results" ) |
| 413 | |
Jon Hall | e234cc4 | 2015-08-31 15:26:47 -0700 | [diff] [blame] | 414 | def skipCase( self, result="DEFAULT", msg=None ): |
| 415 | """ |
| 416 | Will skip the rest of the code in a test case. The case results will be |
| 417 | determined as normal based on completed assertions unless the result |
| 418 | argument is given. |
| 419 | |
| 420 | Optional Arguments: |
Jon Hall | 7c4f430 | 2016-07-15 14:39:02 -0700 | [diff] [blame] | 421 | result: Case insensitive string. Can be 'PASS' or 'FAIL' and will set |
Jon Hall | e234cc4 | 2015-08-31 15:26:47 -0700 | [diff] [blame] | 422 | the case result accordingly. |
| 423 | msg: Message to be printed when the case is skipped in the reports. |
| 424 | """ |
| 425 | result = result.upper().strip() |
| 426 | if result == "PASS": |
| 427 | self.CASERESULT = self.TRUE |
| 428 | elif result == "FAIL": |
| 429 | self.CASERESULT = self.FALSE |
| 430 | self.onFailMsg = "Skipping the rest of this case. " |
| 431 | if msg: |
| 432 | self.onFailMsg += str( msg ) |
Jon Hall | ca31989 | 2017-06-15 15:25:22 -0700 | [diff] [blame] | 433 | raise SkipCase |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 434 | |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 435 | def addCaseHeader( self ): |
| 436 | caseHeader = "\n" + "*" * 30 + "\n Result summary for Testcase" +\ |
| 437 | str( self.CurrentTestCaseNumber ) + "\n" + "*" * 30 + "\n" |
| 438 | self.log.exact( caseHeader ) |
| 439 | caseHeader = "\n" + "*" * 40 + "\nStart of Test Case" +\ |
| 440 | str( self.CurrentTestCaseNumber ) + " : " |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 441 | for driver in self.componentDictionary.keys(): |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 442 | vars( self )[driver + 'log'].info( caseHeader ) |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 443 | |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 444 | def addCaseFooter( self ): |
Jon Hall | 5a72b71 | 2015-09-28 12:20:59 -0700 | [diff] [blame] | 445 | stepNo = self.stepResults[0][-2] |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 446 | if stepNo > 0: |
| 447 | previousStep = " " + str( self.CurrentTestCaseNumber ) + "." +\ |
| 448 | str( stepNo ) + ": " + str( self.stepName ) |
| 449 | stepHeader = "\n" + "*" * 40 + "\nEnd of Step " + previousStep +\ |
| 450 | "\n" + "*" * 40 + "\n" |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 451 | |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 452 | caseFooter = "\n" + "*" * 40 + "\nEnd of Test case " +\ |
| 453 | str( self.CurrentTestCaseNumber ) + "\n" + "*" * 40 + "\n" |
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 | for driver in self.driversList: |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 456 | vars( self )[driver].write( stepHeader + "\n" + caseFooter ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 457 | |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 458 | def cleanup( self ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 459 | ''' |
Jon Hall | 5b58673 | 2015-06-11 11:39:39 -0700 | [diff] [blame] | 460 | Print a summary of the current test's results then attempt to release |
| 461 | all the component handles and the close opened file handles. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 462 | |
Jon Hall | 5b58673 | 2015-06-11 11:39:39 -0700 | [diff] [blame] | 463 | This function shouldbe threadsafe such that cleanup will only be |
| 464 | executed once per test. |
| 465 | |
| 466 | This will return TRUE if all the component handles and log handles |
| 467 | closed properly, else return FALSE. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 468 | ''' |
| 469 | result = self.TRUE |
Jon Hall | 5b58673 | 2015-06-11 11:39:39 -0700 | [diff] [blame] | 470 | lock = self.cleanupLock |
| 471 | if lock.acquire( False ): |
| 472 | try: |
| 473 | if self.cleanupFlag is False: # First thread to run this |
| 474 | self.cleanupFlag = True |
Jon Hall | 0fc0d45 | 2015-07-14 09:49:58 -0700 | [diff] [blame] | 475 | if self.initiated: |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 476 | self.logger.testSummary( self ) |
Jon Hall | 892818c | 2015-10-20 17:58:34 -0700 | [diff] [blame] | 477 | components = self.componentDictionary |
| 478 | for component in sorted( components, |
| 479 | key=lambda item: components[item]['connect_order'], |
| 480 | reverse=True ): |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 481 | try: |
| 482 | tempObject = vars( self )[component] |
| 483 | print "Disconnecting from " + str( tempObject.name ) +\ |
| 484 | ": " + str( tempObject.__class__) |
Jon Hall | 5b58673 | 2015-06-11 11:39:39 -0700 | [diff] [blame] | 485 | tempObject.disconnect() |
Jon Hall | 1306a56 | 2015-09-04 11:21:24 -0700 | [diff] [blame] | 486 | except KeyboardInterrupt: |
| 487 | pass |
| 488 | except KeyError: |
| 489 | # Component not created yet |
| 490 | self.log.warn( "Could not find the component " + |
| 491 | str( component ) ) |
| 492 | except StandardError: |
Jon Hall | 5b58673 | 2015-06-11 11:39:39 -0700 | [diff] [blame] | 493 | self.log.exception( "Exception while disconnecting from " + |
| 494 | str( component ) ) |
| 495 | result = self.FALSE |
| 496 | # Closing all the driver's session files |
| 497 | for driver in self.componentDictionary.keys(): |
| 498 | try: |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 499 | vars( self )[driver].close_log_handles() |
Jon Hall | 1306a56 | 2015-09-04 11:21:24 -0700 | [diff] [blame] | 500 | except KeyboardInterrupt: |
| 501 | pass |
| 502 | except KeyError: |
| 503 | # Component not created yet |
| 504 | self.log.warn( "Could not find the component " + |
| 505 | str( driver ) + " while trying to" + |
| 506 | " close log file" ) |
| 507 | except StandardError: |
Jon Hall | 5b58673 | 2015-06-11 11:39:39 -0700 | [diff] [blame] | 508 | self.log.exception( "Exception while closing log files for " + |
| 509 | str( driver ) ) |
| 510 | result = self.FALSE |
| 511 | else: |
| 512 | pass # Someone else already ran through this function |
| 513 | finally: |
| 514 | lock.release() |
| 515 | else: # Someone already has a lock |
| 516 | # NOTE: This could cause problems if we don't release the lock |
| 517 | # correctly |
| 518 | lock.acquire() # Wait for the other thread to finish |
| 519 | # NOTE: If we don't wait, exit could be called while the thread |
| 520 | # with the lock is still cleaning up |
| 521 | lock.release() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 522 | return result |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 523 | |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 524 | def pause( self ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 525 | ''' |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 526 | This function will pause the test's execution, and will continue after |
| 527 | user provide 'resume' command. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 528 | ''' |
| 529 | __builtin__.testthread.pause() |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 530 | |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 531 | def onfail( self, *components ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 532 | ''' |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 533 | When test step failed, calling all the components onfail. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 534 | ''' |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 535 | if not components: |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 536 | try: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 537 | for component in self.componentDictionary.keys(): |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 538 | tempObject = vars( self )[component] |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 539 | result = tempObject.onfail() |
Jon Hall | 1306a56 | 2015-09-04 11:21:24 -0700 | [diff] [blame] | 540 | except StandardError as e: |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 541 | print str( e ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 542 | result = self.FALSE |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 543 | else: |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 544 | try: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 545 | for component in components: |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 546 | tempObject = vars( self )[component] |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 547 | result = tempObject.onfail() |
Jon Hall | 1306a56 | 2015-09-04 11:21:24 -0700 | [diff] [blame] | 548 | except StandardError as e: |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 549 | print str( e ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 550 | result = self.FALSE |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 551 | |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 552 | def getDriverPath( self, driverName ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 553 | ''' |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 554 | Based on the component 'type' specified in the params , this method |
| 555 | will find the absolute path, by recursively searching the name of |
| 556 | the component. |
| 557 | |
| 558 | NOTE: This function requires the linux 'find' command. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 559 | ''' |
| 560 | import commands |
| 561 | |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 562 | cmd = "find " + drivers_path + " -name " + driverName + ".py" |
| 563 | result = commands.getoutput( cmd ) |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 564 | |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 565 | result_array = str( result ).split( '\n' ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 566 | result_count = 0 |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 567 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 568 | for drivers_list in result_array: |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 569 | result_count = result_count + 1 |
| 570 | if result_count > 1: |
| 571 | print "Found " + driverName + " " + str( result_count ) + " times:" |
| 572 | print str( result_array ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 573 | self.exit() |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 574 | |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 575 | result = re.sub( "(.*)drivers", "", result ) |
| 576 | result = re.sub( "\/\/", "/", result ) |
| 577 | result = re.sub( "\.py", "", result ) |
| 578 | result = re.sub( "\.pyc", "", result ) |
| 579 | result = re.sub( "\/", ".", result ) |
| 580 | result = "drivers" + result |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 581 | return result |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 582 | |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 583 | def step( self, stepDesc ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 584 | ''' |
| 585 | The step information of the test-case will append to the logs. |
| 586 | ''' |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 587 | previousStep = " " + str( self.CurrentTestCaseNumber ) + "." +\ |
| 588 | str( self.stepNumber ) + ": " + str( self.stepName ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 589 | self.stepName = stepDesc |
Jon Hall | 5a72b71 | 2015-09-28 12:20:59 -0700 | [diff] [blame] | 590 | self.stepNumber += 1 |
| 591 | self.stepResults[0].append( self.stepNumber ) |
| 592 | self.stepResults[1].append( stepDesc ) |
| 593 | self.stepResults[2].append( self.NORESULT ) |
| 594 | self.stepResults[3].append( "No on fail message given" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 595 | |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 596 | stepName = " " + str( self.CurrentTestCaseNumber ) + "." +\ |
| 597 | str( self.stepNumber ) + ": " + str( stepDesc ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 598 | self.log.step(stepName) |
| 599 | stepHeader = "" |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 600 | line = "\n" + "-" * 45 + "\n" |
| 601 | if self.stepNumber > 1: |
| 602 | stepHeader = line + "End of Step " + previousStep + line |
| 603 | stepHeader += line + "Start of Step" + stepName + line |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 604 | for driver in self.componentDictionary.keys(): |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 605 | vars( self )[driver + 'log'].info( stepHeader ) |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 606 | |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 607 | def case( self, testCaseName ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 608 | ''' |
| 609 | Test's each test-case information will append to the logs. |
| 610 | ''' |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 611 | self.CurrentTestCase = testCaseName |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 612 | testCaseName = " " + str( testCaseName ) |
| 613 | self.log.case( testCaseName ) |
| 614 | caseHeader = testCaseName + "\n" + "*" * 40 + "\n" |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 615 | for driver in self.componentDictionary.keys(): |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 616 | vars( self )[driver + 'log'].info( caseHeader ) |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 617 | |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 618 | def testDesc( self, description ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 619 | ''' |
| 620 | Test description will append to the logs. |
| 621 | ''' |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 622 | description = "Test Description : " + str( description ) |
| 623 | self.log.info( description ) |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 624 | |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 625 | def _getTest( self ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 626 | ''' |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 627 | This method will parse the test script to find required test |
| 628 | information. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 629 | ''' |
Jon Hall | 53c5e66 | 2016-04-13 16:06:56 -0700 | [diff] [blame] | 630 | testFileHandler = open( main.testFile, 'r' ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 631 | testFileList = testFileHandler.readlines() |
| 632 | testFileHandler.close() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 633 | counter = 0 |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 634 | for index in range( len( testFileList ) ): |
| 635 | lineMatch = re.match( '\s+def CASE(\d+)(.*):', |
| 636 | testFileList[index], |
| 637 | 0 ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 638 | if lineMatch: |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 639 | counter = counter + 1 |
| 640 | self.TC_PLANNED = len( self.testcases_list ) |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 641 | |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 642 | def response_parser( self, response, return_format ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 643 | ''' It will load the default response parser ''' |
| 644 | response_dict = {} |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 645 | response_dict = self.response_to_dict( response, return_format ) |
| 646 | return_format_string = self.dict_to_return_format( response, |
| 647 | return_format, |
| 648 | response_dict ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 649 | return return_format_string |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 650 | |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 651 | def response_to_dict( self, response, return_format ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 652 | response_dict = {} |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 653 | json_match = re.search( '^\s*{', response ) |
| 654 | xml_match = re.search( '^\s*\<', response ) |
| 655 | ini_match = re.search( '^\s*\[', response ) |
| 656 | if json_match: |
| 657 | self.log.info( "Response is in 'JSON' format, converting to '" + |
| 658 | return_format + "' format" ) |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 659 | # Formatting the json string |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 660 | response = re.sub( r"{\s*'?(\w)", r'{"\1', response ) |
| 661 | response = re.sub( r",\s*'?(\w)", r',"\1', response ) |
| 662 | response = re.sub( r"(\w)'?\s*:", r'\1":', response ) |
| 663 | response = re.sub( r":\s*'(\w)'\s*([,}])", r':"\1"\2', response ) |
| 664 | try: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 665 | import json |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 666 | response_dict = json.loads( response ) |
Jon Hall | 1306a56 | 2015-09-04 11:21:24 -0700 | [diff] [blame] | 667 | except StandardError: |
Jon Hall | 2a5002c | 2015-08-21 16:49:11 -0700 | [diff] [blame] | 668 | self.log.exception( "Json Parser is unable to parse the string" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 669 | return response_dict |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 670 | elif ini_match: |
| 671 | self.log.info( "Response is in 'INI' format, converting to '" + |
| 672 | return_format + "' format" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 673 | from configobj import ConfigObj |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 674 | response_file = open( "respnse_file.temp", 'w' ) |
| 675 | response_file.write( response ) |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 676 | response_file.close() |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 677 | response_dict = ConfigObj( "respnse_file.temp" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 678 | return response_dict |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 679 | elif xml_match: |
| 680 | self.log.info( "Response is in 'XML' format, converting to '" + |
| 681 | return_format + "' format" ) |
| 682 | try: |
| 683 | response_dict = xmldict.xml_to_dict( "<response> " + |
| 684 | str( response ) + |
| 685 | " </response>" ) |
Jon Hall | 1306a56 | 2015-09-04 11:21:24 -0700 | [diff] [blame] | 686 | except StandardError: |
| 687 | self.log.exception() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 688 | return response_dict |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 689 | |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 690 | def dict_to_return_format( self, response, return_format, response_dict ): |
| 691 | if return_format == 'table': |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 692 | ''' Will return in table format''' |
| 693 | to_do = "Call the table output formatter" |
| 694 | global response_table |
| 695 | response_table = '\n' |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 696 | response_table = response_table + '\t'.join( response_dict ) + "\n" |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 697 | |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 698 | def get_table( value_to_convert ): |
| 699 | ''' This will parse the dictionary recusrsively and print as |
| 700 | table format''' |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 701 | table_data = "" |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 702 | if isinstance( value_to_convert, dict ): |
| 703 | table_data = table_data + '\t'.join( value_to_convert ) +\ |
| 704 | "\n" |
| 705 | for temp_val in value_to_convert.values(): |
| 706 | table_data = table_data + get_table( temp_val ) |
| 707 | else: |
| 708 | table_data = table_data + str( value_to_convert ) + "\t" |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 709 | return table_data |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 710 | |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 711 | for value in response_dict.values(): |
| 712 | response_table = response_table + get_table( value ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 713 | return response_table |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 714 | |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 715 | elif return_format == 'config': |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 716 | ''' Will return in config format''' |
| 717 | to_do = 'Call dict to config coverter' |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 718 | response_string = str( response_dict ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 719 | print response_string |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 720 | response_config = re.sub( ",", "\n\t", response_string ) |
| 721 | response_config = re.sub( "u\'", "\'", response_config ) |
| 722 | response_config = re.sub( "{", "", response_config ) |
| 723 | response_config = re.sub( "}", "\n", response_config ) |
| 724 | response_config = re.sub( ":", " =", response_config ) |
| 725 | return "[response]\n\t " + response_config |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 726 | elif return_format == 'xml': |
| 727 | ''' Will return in xml format''' |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 728 | response_xml = xmldict.dict_to_xml( response_dict ) |
| 729 | response_xml = re.sub( ">\s*<", ">\n<", response_xml ) |
| 730 | return "\n" + response_xml |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 731 | elif return_format == 'json': |
| 732 | ''' Will return in json format''' |
| 733 | to_do = 'Call dict to xml coverter' |
| 734 | import json |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 735 | response_json = json.dumps( response_dict ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 736 | return response_json |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 737 | |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 738 | def get_random( self ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 739 | self.random_order = self.random_order + 1 |
| 740 | return self.random_order |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 741 | |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 742 | def exit( self ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 743 | __builtin__.testthread = None |
Jon Hall | 5b58673 | 2015-06-11 11:39:39 -0700 | [diff] [blame] | 744 | for thread in threading.enumerate(): |
| 745 | if thread.isAlive(): |
| 746 | try: |
| 747 | thread._Thread__stop() |
| 748 | except: |
Jon Hall | 1306a56 | 2015-09-04 11:21:24 -0700 | [diff] [blame] | 749 | # NOTE: We should catch any exceptions while trying to |
| 750 | # close the thread so that we can try to close the other |
| 751 | # threads as well |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 752 | print str( thread.getName() ) +\ |
| 753 | ' could not be terminated' |
Jeremy Ronquillo | 15ff107 | 2017-07-17 10:55:46 -0700 | [diff] [blame] | 754 | os.system( "stty sane" ) # fix format if necessary |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 755 | sys.exit() |
| 756 | |
Jeremy Ronquillo | 15ff107 | 2017-07-17 10:55:46 -0700 | [diff] [blame] | 757 | |
Jon Hall | cd3d2a3 | 2015-10-01 11:07:28 -0700 | [diff] [blame] | 758 | def stop( self, email=False ): |
| 759 | """ |
| 760 | Stop the test until Ctrl-D is entered. |
| 761 | Ctrl-C will kill the test |
Jon Hall | 2507978 | 2015-10-13 13:54:39 -0700 | [diff] [blame] | 762 | |
| 763 | Optional arguments: |
| 764 | email can either be a bool, or you can specify the email address |
| 765 | to send the email to |
Jon Hall | cd3d2a3 | 2015-10-01 11:07:28 -0700 | [diff] [blame] | 766 | """ |
| 767 | try: |
| 768 | if email: |
Jon Hall | 2507978 | 2015-10-13 13:54:39 -0700 | [diff] [blame] | 769 | if '@' in email: |
| 770 | main.mail = email |
| 771 | utilities.send_warning_email() |
Jeremy Ronquillo | 15ff107 | 2017-07-17 10:55:46 -0700 | [diff] [blame] | 772 | self.log.debug( "Test execution suspended. \n" |
| 773 | "- Type 'c' to resume the test.\n" |
| 774 | "- Type Ctrl-C to exit the test.\n" |
| 775 | "- Enter interactive python interpreter commands.\n" |
| 776 | "\t- ie: main.Mininet1.pingall()\n" |
| 777 | "- Type 'help' for help with pdb." ) |
| 778 | pdb.set_trace() |
Jon Hall | cd3d2a3 | 2015-10-01 11:07:28 -0700 | [diff] [blame] | 779 | except EOFError: |
| 780 | return |
| 781 | # Pass all other exceptions up to caller |
| 782 | |
| 783 | |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 784 | def verifyOptions( options ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 785 | ''' |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 786 | This will verify the command line options and set to default values, |
| 787 | if any option not given in command line. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 788 | ''' |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 789 | verifyTest( options ) |
| 790 | verifyExample( options ) |
| 791 | verifyTestScript( options ) |
YPZhang | 1c89e76 | 2016-06-29 10:43:58 -0700 | [diff] [blame] | 792 | verifyParams( options ) |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 793 | verifyLogdir( options ) |
| 794 | verifyMail( options ) |
| 795 | verifyTestCases( options ) |
| 796 | verifyOnosCell( options ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 797 | |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 798 | def verifyTest( options ): |
Jon Hall | 4450624 | 2015-07-29 17:40:26 -0700 | [diff] [blame] | 799 | try: |
| 800 | if options.testname: |
| 801 | main.TEST = options.testname |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 802 | main.classPath = "tests." + main.TEST + "." + main.TEST |
Jon Hall | 4450624 | 2015-07-29 17:40:26 -0700 | [diff] [blame] | 803 | main.tests_path = tests_path |
| 804 | elif options.example: |
| 805 | main.TEST = options.example |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 806 | main.tests_path = path + "/examples/" |
| 807 | main.classPath = "examples." + main.TEST + "." + main.TEST |
Jon Hall | 4450624 | 2015-07-29 17:40:26 -0700 | [diff] [blame] | 808 | except AttributeError: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 809 | 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] | 810 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 811 | |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 812 | def verifyExample( options ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 813 | if options.example: |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 814 | main.testDir = path + '/examples/' |
| 815 | main.tests_path = path + "/examples/" |
| 816 | main.classPath = "examples." + main.TEST + "." + main.TEST |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 817 | |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 818 | def verifyLogdir( options ): |
Jon Hall | 88e498c | 2015-03-06 09:54:35 -0800 | [diff] [blame] | 819 | # Verifying Log directory option |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 820 | if options.logdir: |
| 821 | main.logdir = options.logdir |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 822 | else: |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 823 | main.logdir = main.FALSE |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 824 | |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 825 | def verifyMail( options ): |
Jon Hall | 2507978 | 2015-10-13 13:54:39 -0700 | [diff] [blame] | 826 | # Mail-To: field |
| 827 | if options.mail: # Test run specific |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 828 | main.mail = options.mail |
Jon Hall | 2507978 | 2015-10-13 13:54:39 -0700 | [diff] [blame] | 829 | elif main.params.get('mail'): # Test suite specific |
| 830 | main.mail = main.params.get( 'mail' ) |
| 831 | else: # TestON specific |
| 832 | main.mail = main.config['config'].get( 'mail_to' ) |
| 833 | # Mail-From: field |
| 834 | main.sender = main.config['config'].get( 'mail_from' ) |
| 835 | # Mail smtp server |
| 836 | main.smtp = main.config['config'].get( 'mail_server' ) |
| 837 | # Mail-From account password |
| 838 | main.senderPwd = main.config['config'].get( 'mail_pass' ) |
| 839 | |
Devin Lim | 2df68a1 | 2017-06-30 15:39:05 -0700 | [diff] [blame] | 840 | def evalTestCase( tempList ): |
| 841 | tList = [] |
| 842 | for tcase in tempList: |
| 843 | if isinstance( tcase, list ): |
| 844 | tList.extend( evalTestCase( tcase ) ) |
| 845 | else: |
| 846 | tList.extend( [tcase] ) |
| 847 | return tList |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 848 | |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 849 | def verifyTestCases( options ): |
Jon Hall | 88e498c | 2015-03-06 09:54:35 -0800 | [diff] [blame] | 850 | # Getting Test cases list |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 851 | if options.testcases: |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 852 | testcases_list = options.testcases |
Jon Hall | 88e498c | 2015-03-06 09:54:35 -0800 | [diff] [blame] | 853 | # sys.exit() |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 854 | testcases_list = re.sub( "(\[|\])", "", options.testcases ) |
| 855 | main.testcases_list = eval( testcases_list + "," ) |
| 856 | else: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 857 | if 'testcases' in main.params.keys(): |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 858 | temp = eval( main.params['testcases'] + "," ) |
Devin Lim | 2df68a1 | 2017-06-30 15:39:05 -0700 | [diff] [blame] | 859 | main.testcases_list = evalTestCase( list( temp ) ) |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 860 | else: |
| 861 | print "Testcases not specifed in params, please provide in " +\ |
| 862 | "params file or 'testcases' commandline argument" |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 863 | sys.exit() |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 864 | |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 865 | def verifyOnosCell( options ): |
Hari Krishna | be4b97b | 2015-07-15 12:19:43 -0700 | [diff] [blame] | 866 | # Verifying onoscell option |
Hari Krishna | 03f530e | 2015-07-10 17:28:27 -0700 | [diff] [blame] | 867 | if options.onoscell: |
| 868 | main.onoscell = options.onoscell |
Devin Lim | 58046fa | 2017-07-05 16:55:00 -0700 | [diff] [blame] | 869 | main.ONOSip = [] |
Hari Krishna | be4b97b | 2015-07-15 12:19:43 -0700 | [diff] [blame] | 870 | main.mnIP = "" |
Devin Lim | 58046fa | 2017-07-05 16:55:00 -0700 | [diff] [blame] | 871 | cellCMD = ". ~/onos/tools/dev/bash_profile; cell " + main.onoscell |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 872 | output = subprocess.check_output( ["bash", '-c', cellCMD] ) |
Hari Krishna | be4b97b | 2015-07-15 12:19:43 -0700 | [diff] [blame] | 873 | splitOutput = output.splitlines() |
Devin Lim | 58046fa | 2017-07-05 16:55:00 -0700 | [diff] [blame] | 874 | main.apps = "" |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 875 | for i in range( len( splitOutput ) ): |
| 876 | if re.match( "OCN", splitOutput[i] ): |
| 877 | mnNode = splitOutput[i].split( "=" ) |
Hari Krishna | be4b97b | 2015-07-15 12:19:43 -0700 | [diff] [blame] | 878 | main.mnIP = mnNode[1] |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 879 | # cell already sorts OC variables in bash, so no need to |
| 880 | # sort in TestON |
Devin Lim | 58046fa | 2017-07-05 16:55:00 -0700 | [diff] [blame] | 881 | elif re.match( "OC[1-9]", splitOutput[i] ): |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 882 | onosNodes = splitOutput[i].split( "=" ) |
Devin Lim | 58046fa | 2017-07-05 16:55:00 -0700 | [diff] [blame] | 883 | main.ONOSip.append( onosNodes[1] ) |
| 884 | elif re.match( "ONOS_APPS", splitOutput[i] ): |
| 885 | main.apps = ( splitOutput[i].split( "=" ) )[1] |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 886 | else: |
Hari Krishna | 03f530e | 2015-07-10 17:28:27 -0700 | [diff] [blame] | 887 | main.onoscell = main.FALSE |
| 888 | |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 889 | def verifyTestScript( options ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 890 | ''' |
| 891 | Verifyies test script. |
| 892 | ''' |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 893 | main.openspeak = openspeak.OpenSpeak() |
Jon Hall | 53c5e66 | 2016-04-13 16:06:56 -0700 | [diff] [blame] | 894 | directory = main.testDir + "/" + main.TEST |
| 895 | if os.path.exists( directory ): |
| 896 | pass |
| 897 | else: |
| 898 | directory = "" |
| 899 | for root, dirs, files in os.walk( main.testDir, topdown=True): |
| 900 | if not directory: |
| 901 | for name in dirs: |
| 902 | if name == main.TEST: |
| 903 | directory = ( os.path.join( root, name ) ) |
| 904 | index = directory.find( "/tests/" ) + 1 |
| 905 | main.classPath = directory[index:].replace( '/', '.' ) + "." + main.TEST |
| 906 | break |
| 907 | openspeakfile = directory + "/" + main.TEST + ".ospk" |
| 908 | main.testFile = directory + "/" + main.TEST + ".py" |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 909 | if os.path.exists( openspeakfile ): |
Jon Hall | 4450624 | 2015-07-29 17:40:26 -0700 | [diff] [blame] | 910 | # Openspeak file found, compiling to python |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 911 | main.openspeak.compiler( openspeakfile=openspeakfile, writetofile=1 ) |
Jon Hall | 53c5e66 | 2016-04-13 16:06:56 -0700 | [diff] [blame] | 912 | elif os.path.exists( main.testFile ): |
Jon Hall | 4450624 | 2015-07-29 17:40:26 -0700 | [diff] [blame] | 913 | # No openspeak found, using python file instead |
| 914 | pass |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 915 | else: |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 916 | print "\nThere is no \"" + main.TEST + "\" test script.\nPlease provide a " +\ |
Jon Hall | 4450624 | 2015-07-29 17:40:26 -0700 | [diff] [blame] | 917 | "Python or OpenSpeak test script in the tests folder: " +\ |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 918 | main.testDir + "/" + main.TEST + "/" |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 919 | __builtin__.testthread = None |
| 920 | main.exit() |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 921 | try: |
| 922 | testModule = __import__( main.classPath, |
| 923 | globals(), |
| 924 | locals(), |
| 925 | [main.TEST], |
| 926 | -1 ) |
Jon Hall | 1306a56 | 2015-09-04 11:21:24 -0700 | [diff] [blame] | 927 | except ImportError: |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 928 | print "There was an import error, it might mean that there is " +\ |
| 929 | "no test named " + main.TEST |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 930 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 931 | |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 932 | testClass = getattr( testModule, main.TEST ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 933 | main.testObject = testClass() |
| 934 | load_parser() |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 935 | main.params = main.parser.parseParams( main.classPath ) |
| 936 | main.topology = main.parser.parseTopology( main.classPath ) |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 937 | |
YPZhang | 1c89e76 | 2016-06-29 10:43:58 -0700 | [diff] [blame] | 938 | def verifyParams( options ): |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 939 | try: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 940 | main.params = main.params['PARAMS'] |
Jon Hall | 1306a56 | 2015-09-04 11:21:24 -0700 | [diff] [blame] | 941 | except KeyError: |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 942 | print "Error with the params file: Either the file not specified " +\ |
| 943 | "or the format is not correct" |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 944 | main.exit() |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 945 | try: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 946 | main.topology = main.topology['TOPOLOGY'] |
Jon Hall | 1306a56 | 2015-09-04 11:21:24 -0700 | [diff] [blame] | 947 | except KeyError: |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 948 | print "Error with the Topology file: Either the file not specified " +\ |
| 949 | "or the format is not correct" |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 950 | main.exit() |
YPZhang | 1c89e76 | 2016-06-29 10:43:58 -0700 | [diff] [blame] | 951 | # Overwrite existing params variables if they are specified from command line |
| 952 | if len(options.params) > 0: |
| 953 | # Some params variables are specified from command line |
| 954 | for param in options.params: |
| 955 | if not re.search( ".=.", param ): |
| 956 | print( "Error when parsing params: params should follow key=value format" ) |
| 957 | continue |
Jon Hall | 7c4f430 | 2016-07-15 14:39:02 -0700 | [diff] [blame] | 958 | # Split the param string to catch nested keys and the value |
YPZhang | 1c89e76 | 2016-06-29 10:43:58 -0700 | [diff] [blame] | 959 | [ keys, value ] = param.split( "=" ) |
| 960 | # Split the nested keys according to its hierarchy |
| 961 | keyList = keys.split( "/" ) |
| 962 | # Get the outermost dictionary |
| 963 | paramDict = main.params |
| 964 | # Get the innermost dictionary |
| 965 | try: |
| 966 | while len( keyList ) > 1: |
| 967 | key = keyList.pop(0) |
| 968 | assert isinstance( paramDict[ key ], dict ) |
| 969 | paramDict = paramDict[ key ] |
| 970 | except KeyError: |
| 971 | print( "Error when parsing params: key \"" + key + "\" not found in main.params" ) |
| 972 | main.exit() |
| 973 | except AssertionError: |
| 974 | print( "Error when parsing params: \"" + key + "\" is already the innermost level in main.params" ) |
| 975 | main.exit() |
| 976 | # Change the value |
| 977 | if not paramDict.has_key( keyList[0] ): |
| 978 | print( "Error when parsing params: key \"" + keyList[0] + "\" not found in main.params" ) |
| 979 | main.exit() |
| 980 | elif isinstance( paramDict[ keyList[0] ], dict ): |
| 981 | print( "Error when parsing params: more levels under key \"" + keyList[0] + "\" in main.params" ) |
| 982 | main.exit() |
| 983 | else: |
| 984 | paramDict[ keyList[0] ] = value |
kelvin-onlab | f70fd54 | 2015-05-07 18:41:40 -0700 | [diff] [blame] | 985 | |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 986 | def load_parser(): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 987 | ''' |
| 988 | It facilitates the loading customised parser for topology and params file. |
| 989 | It loads parser mentioned in tab named parser of teston.cfg file. |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 990 | It also loads default xmlparser if no parser have specified in teston.cfg |
| 991 | file. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 992 | |
| 993 | ''' |
| 994 | confighash = main.configDict |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 995 | if 'file' in confighash['config']['parser'] and\ |
| 996 | 'class' in confighash['config']['parser']: |
Jon Hall | 4450624 | 2015-07-29 17:40:26 -0700 | [diff] [blame] | 997 | path = confighash['config']['parser']['file'] |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 998 | if path is not None or\ |
| 999 | confighash['config']['parser']['class'] is not None: |
Jon Hall | 4450624 | 2015-07-29 17:40:26 -0700 | [diff] [blame] | 1000 | try: |
| 1001 | module = re.sub( r".py\s*$", "", path ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1002 | moduleList = module.split("/") |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 1003 | newModule = ".".join( moduleList[-2:] ) |
Jon Hall | 4450624 | 2015-07-29 17:40:26 -0700 | [diff] [blame] | 1004 | parsingClass = confighash['config']['parser']['class'] |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 1005 | parsingModule = __import__( newModule, |
| 1006 | globals(), |
| 1007 | locals(), |
| 1008 | [parsingClass], |
| 1009 | -1 ) |
| 1010 | parsingClass = getattr( parsingModule, parsingClass ) |
Jon Hall | 4450624 | 2015-07-29 17:40:26 -0700 | [diff] [blame] | 1011 | main.parser = parsingClass() |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 1012 | if hasattr( main.parser, "parseParams" ) and\ |
| 1013 | hasattr( main.parser, "parseTopology" ) and\ |
| 1014 | hasattr( main.parser, "parse" ): |
Jon Hall | 4450624 | 2015-07-29 17:40:26 -0700 | [diff] [blame] | 1015 | pass |
| 1016 | else: |
| 1017 | print "Invalid parser format" |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1018 | main.exit() |
Jon Hall | 4450624 | 2015-07-29 17:40:26 -0700 | [diff] [blame] | 1019 | except ImportError: |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 1020 | print "Could not find the file " + path +\ |
| 1021 | " using default parser." |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 1022 | load_defaultParser() |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 1023 | elif confighash['config']['parser']['file'] is None or\ |
| 1024 | confighash['config']['parser']['class'] is None: |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 1025 | load_defaultParser() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1026 | else: |
| 1027 | load_defaultParser() |
| 1028 | |
| 1029 | def load_defaultParser(): |
| 1030 | ''' |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 1031 | It will load the default parser which is xml parser to parse the params and |
| 1032 | topology file. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1033 | ''' |
| 1034 | moduleList = main.parserPath.split("/") |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 1035 | newModule = ".".join( moduleList[-2:] ) |
| 1036 | try: |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 1037 | parsingClass = main.parsingClass |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 1038 | parsingModule = __import__( newModule, |
| 1039 | globals(), |
| 1040 | locals(), |
| 1041 | [parsingClass], |
| 1042 | -1 ) |
| 1043 | parsingClass = getattr( parsingModule, parsingClass ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1044 | main.parser = parsingClass() |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 1045 | if hasattr( main.parser, "parseParams" ) and\ |
| 1046 | hasattr( main.parser, "parseTopology" ) and\ |
| 1047 | hasattr( main.parser, "parse" ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1048 | pass |
| 1049 | else: |
| 1050 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1051 | except ImportError: |
| 1052 | print sys.exc_info()[1] |
| 1053 | |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 1054 | def load_logger(): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1055 | ''' |
| 1056 | It facilitates the loading customised parser for topology and params file. |
| 1057 | It loads parser mentioned in tab named parser of teston.cfg file. |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 1058 | It also loads default xmlparser if no parser have specified in teston.cfg |
| 1059 | file. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1060 | ''' |
| 1061 | confighash = main.configDict |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 1062 | if 'file' in confighash['config']['logger'] and\ |
| 1063 | 'class' in confighash['config']['logger']: |
Jon Hall | 4450624 | 2015-07-29 17:40:26 -0700 | [diff] [blame] | 1064 | path = confighash['config']['logger']['file'] |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 1065 | if path is not None or\ |
| 1066 | confighash['config']['logger']['class'] is not None: |
Jon Hall | 4450624 | 2015-07-29 17:40:26 -0700 | [diff] [blame] | 1067 | try: |
| 1068 | module = re.sub( r".py\s*$", "", path ) |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 1069 | moduleList = module.split( "/" ) |
| 1070 | newModule = ".".join( moduleList[-2:] ) |
Jon Hall | 4450624 | 2015-07-29 17:40:26 -0700 | [diff] [blame] | 1071 | loggerClass = confighash['config']['logger']['class'] |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 1072 | loggerModule = __import__( newModule, |
| 1073 | globals(), |
| 1074 | locals(), |
| 1075 | [loggerClass], |
| 1076 | -1 ) |
| 1077 | loggerClass = getattr( loggerModule, loggerClass ) |
Jon Hall | 4450624 | 2015-07-29 17:40:26 -0700 | [diff] [blame] | 1078 | main.logger = loggerClass() |
Jon Hall | 4450624 | 2015-07-29 17:40:26 -0700 | [diff] [blame] | 1079 | except ImportError: |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 1080 | print "Could not find the file " + path +\ |
| 1081 | " using default logger." |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1082 | load_defaultlogger() |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 1083 | elif confighash['config']['parser']['file'] is None or\ |
| 1084 | confighash['config']['parser']['class'] is None: |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 1085 | load_defaultlogger() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1086 | else: |
| 1087 | load_defaultlogger() |
| 1088 | |
| 1089 | def load_defaultlogger(): |
| 1090 | ''' |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 1091 | It will load the default parser which is xml parser to parse the params and |
| 1092 | topology file. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1093 | ''' |
| 1094 | moduleList = main.loggerPath.split("/") |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 1095 | newModule = ".".join( moduleList[-2:] ) |
| 1096 | try: |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 1097 | loggerClass = main.loggerClass |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 1098 | loggerModule = __import__( newModule, |
| 1099 | globals(), |
| 1100 | locals(), |
| 1101 | [loggerClass], |
| 1102 | -1 ) |
| 1103 | loggerClass = getattr( loggerModule, loggerClass ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1104 | main.logger = loggerClass() |
| 1105 | |
| 1106 | except ImportError: |
| 1107 | print sys.exc_info()[1] |
Jon Hall | d61331b | 2015-02-17 16:35:47 -0800 | [diff] [blame] | 1108 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1109 | |
Jon Hall | 714eeba | 2015-09-29 17:53:10 -0700 | [diff] [blame] | 1110 | def _echo( self ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1111 | print "THIS IS ECHO" |