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