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