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 23-Oct-2012 |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 4 | Copyright 2012 Open Networking Foundation (ONF) |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -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 | 4ba53f0 | 2015-07-29 13:07:41 -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 | |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 23 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 24 | Utilities will take care about the basic functions like : |
| 25 | * Extended assertion, |
| 26 | * parse_args for key-value pair handling |
| 27 | * Parsing the params or topology file. |
| 28 | |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 29 | ''' |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 30 | import re |
| 31 | from configobj import ConfigObj |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 32 | from core import ast as ast |
| 33 | import smtplib |
| 34 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 35 | import email |
| 36 | import os |
| 37 | import email.mime.application |
Jon Hall | 095730a | 2015-12-17 14:57:45 -0800 | [diff] [blame] | 38 | import time |
| 39 | import random |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 40 | |
| 41 | class Utilities: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 42 | ''' |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 43 | Utilities will take care about the basic functions like : |
| 44 | * Extended assertion, |
| 45 | * parse_args for key-value pair handling |
| 46 | * Parsing the params or topology file. |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 47 | ''' |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 48 | |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 49 | def __init__( self ): |
| 50 | self.wrapped = sys.modules[ __name__ ] |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 51 | |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 52 | def __getattr__( self, name ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 53 | ''' |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 54 | This will invoke, if the attribute wasn't found the usual ways. |
| 55 | Here it will look for assert_attribute and will execute when AttributeError occurs. |
| 56 | It will return the result of the assert_attribute. |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 57 | ''' |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 58 | try: |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 59 | return getattr( self.wrapped, name ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 60 | except AttributeError: |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 61 | def assertHandling( **kwargs ): |
| 62 | nameVar = re.match( "^assert", name, flags=0 ) |
| 63 | matchVar = re.match( "assert(_not_|_)(equals|matches|greater|lesser)", name, flags=0 ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 64 | notVar = 0 |
| 65 | operators = "" |
| 66 | |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 67 | try: |
| 68 | if matchVar.group( 1 ) == "_not_" and matchVar.group( 2 ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 69 | notVar = 1 |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 70 | operators = matchVar.group( 2 ) |
| 71 | elif matchVar.group( 1 ) == "_" and matchVar.group( 2 ): |
| 72 | operators = matchVar.group( 2 ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 73 | except AttributeError: |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 74 | if matchVar is None and nameVar: |
| 75 | operators = 'equals' |
| 76 | result = self._assert( NOT=notVar, operator=operators, **kwargs ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 77 | if result == main.TRUE: |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 78 | main.log.info( "Assertion Passed" ) |
Jon Hall | 79bec22 | 2015-04-30 16:23:30 -0700 | [diff] [blame] | 79 | main.STEPRESULT = main.TRUE |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 80 | elif result == main.FALSE: |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 81 | main.log.warn( "Assertion Failed" ) |
Jon Hall | 79bec22 | 2015-04-30 16:23:30 -0700 | [diff] [blame] | 82 | main.STEPRESULT = main.FALSE |
| 83 | else: |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 84 | main.log.error( "There is an Error in Assertion" ) |
Jon Hall | 79bec22 | 2015-04-30 16:23:30 -0700 | [diff] [blame] | 85 | main.STEPRESULT = main.ERROR |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 86 | return result |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 87 | return assertHandling |
Jon Hall | 79bec22 | 2015-04-30 16:23:30 -0700 | [diff] [blame] | 88 | |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 89 | def _assert( self, **assertParam ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 90 | ''' |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 91 | It will take the arguments : |
Jon Hall | 8ce34e8 | 2015-06-05 10:41:45 -0700 | [diff] [blame] | 92 | expect:'Expected output' |
| 93 | actual:'Actual output' |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 94 | onpass:'Action or string to be triggered or displayed respectively when the assert passed' |
| 95 | onfail:'Action or string to be triggered or displayed respectively when the assert failed' |
| 96 | not:'optional argument to specify the negation of the each assertion type' |
| 97 | operator:'assertion type will be defined by using operator. Like equal , greater, lesser, matches.' |
Jon Hall | 8ce34e8 | 2015-06-05 10:41:45 -0700 | [diff] [blame] | 98 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 99 | It will return the assertion result. |
Jon Hall | 8ce34e8 | 2015-06-05 10:41:45 -0700 | [diff] [blame] | 100 | |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 101 | ''' |
| 102 | |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 103 | arguments = self.parse_args( [ "EXPECT", "ACTUAL", "ONPASS", "ONFAIL", "NOT", "OPERATOR" ], **assertParam ) |
Jon Hall | 8ce34e8 | 2015-06-05 10:41:45 -0700 | [diff] [blame] | 104 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 105 | result = 0 |
| 106 | valuetype = '' |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 107 | operation = "not " + str( arguments[ "OPERATOR" ] ) if arguments[ 'NOT' ] and arguments[ 'NOT' ] == 1 else arguments[ "OPERATOR" ] |
| 108 | operators = { 'equals': { 'STR': '==', 'NUM': '==' }, 'matches': '=~', 'greater': '>' , 'lesser': '<' } |
Jon Hall | 8ce34e8 | 2015-06-05 10:41:45 -0700 | [diff] [blame] | 109 | |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 110 | expectMatch = re.match( '^\s*[+-]?0(e0)?\s*$', str( arguments[ "EXPECT" ] ), re.I + re.M ) |
| 111 | if not( ( not expectMatch ) and ( arguments[ "EXPECT" ] == 0 ) ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 112 | valuetype = 'NUM' |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 113 | else: |
| 114 | if arguments[ "OPERATOR" ] == 'greater' or arguments[ "OPERATOR" ] == 'lesser': |
| 115 | main.log.error( "Numeric comparison on strings is not possibele" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 116 | return main.ERROR |
Jon Hall | 8ce34e8 | 2015-06-05 10:41:45 -0700 | [diff] [blame] | 117 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 118 | valuetype = 'STR' |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 119 | arguments[ "ACTUAL" ] = str( arguments[ "ACTUAL" ] ) |
| 120 | if arguments[ "OPERATOR" ] != 'matches': |
| 121 | arguments[ "EXPECT" ] = str( arguments[ "EXPECT" ] ) |
Jon Hall | 8ce34e8 | 2015-06-05 10:41:45 -0700 | [diff] [blame] | 122 | |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 123 | try: |
| 124 | opcode = operators[ str( arguments[ "OPERATOR" ] ) ][ valuetype ] if arguments[ "OPERATOR" ] == 'equals' else operators[ str( arguments[ "OPERATOR" ] ) ] |
Jon Hall | 8ce34e8 | 2015-06-05 10:41:45 -0700 | [diff] [blame] | 125 | |
Jon Hall | 1306a56 | 2015-09-04 11:21:24 -0700 | [diff] [blame] | 126 | except KeyError as e: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 127 | print "Key Error in assertion" |
Jon Hall | 1306a56 | 2015-09-04 11:21:24 -0700 | [diff] [blame] | 128 | print e |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 129 | return main.FALSE |
Jon Hall | 8ce34e8 | 2015-06-05 10:41:45 -0700 | [diff] [blame] | 130 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 131 | if opcode == '=~': |
| 132 | try: |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 133 | assert re.search( str( arguments[ "EXPECT" ] ), str( arguments[ "ACTUAL" ] ) ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 134 | result = main.TRUE |
| 135 | except AssertionError: |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 136 | try: |
| 137 | assert re.match( str( arguments[ "EXPECT" ] ), str( arguments[ "ACTUAL" ] ) ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 138 | result = main.TRUE |
| 139 | except AssertionError: |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 140 | main.log.error( "Assertion Failed" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 141 | result = main.FALSE |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 142 | else: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 143 | try: |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 144 | if str( opcode ) == "==": |
| 145 | main.log.info( "Verifying the Expected is equal to the actual or not using assert_equal" ) |
| 146 | if( arguments[ "EXPECT" ] == arguments[ "ACTUAL" ] ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 147 | result = main.TRUE |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 148 | else: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 149 | result = main.FALSE |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 150 | elif str( opcode ) == ">": |
| 151 | main.log.info( "Verifying the Expected is Greater than the actual or not using assert_greater" ) |
| 152 | if( ast.literal_eval( arguments[ "EXPECT" ] ) > ast.literal_eval( arguments[ "ACTUAL" ] ) ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 153 | result = main.TRUE |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 154 | else: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 155 | result = main.FALSE |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 156 | elif str( opcode ) == "<": |
| 157 | main.log.info( "Verifying the Expected is Lesser than the actual or not using assert_lesser" ) |
| 158 | if( ast.literal_eval( arguments[ "EXPECT" ] ) < ast.literal_eval( arguments[ "ACTUAL" ] ) ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 159 | result = main.TRUE |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 160 | else: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 161 | result = main.FALSE |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 162 | except AssertionError: |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 163 | main.log.error( "Assertion Failed" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 164 | result = main.FALSE |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 165 | result = result if result else 0 |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 166 | result = not result if arguments[ "NOT" ] and arguments[ "NOT" ] == 1 else result |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 167 | resultString = "" |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 168 | if result: |
| 169 | resultString = str( resultString ) + "PASS" |
| 170 | main.log.info( arguments[ "ONPASS" ] ) |
| 171 | else: |
| 172 | resultString = str( resultString ) + "FAIL" |
| 173 | if not isinstance( arguments[ "ONFAIL" ], str ): |
| 174 | eval( str( arguments[ "ONFAIL" ] ) ) |
| 175 | else: |
| 176 | main.log.error( arguments[ "ONFAIL" ] ) |
| 177 | main.log.report( arguments[ "ONFAIL" ] ) |
Jon Hall | 9062761 | 2015-06-09 14:57:02 -0700 | [diff] [blame] | 178 | main.onFailMsg = arguments[ 'ONFAIL' ] |
Jon Hall | 8ce34e8 | 2015-06-05 10:41:45 -0700 | [diff] [blame] | 179 | |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 180 | msg = arguments[ "ON" + str( resultString ) ] |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 181 | |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 182 | if not isinstance( msg, str ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 183 | try: |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 184 | eval( str( msg ) ) |
Jon Hall | 1306a56 | 2015-09-04 11:21:24 -0700 | [diff] [blame] | 185 | except SyntaxError as e: |
| 186 | print "function definition is not right" |
| 187 | print e |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 188 | |
| 189 | main.last_result = result |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 190 | if main.stepResults[ 2 ]: |
| 191 | main.stepResults[ 2 ][ -1 ] = result |
Jon Hall | 80577e7 | 2015-09-29 14:07:25 -0700 | [diff] [blame] | 192 | try: |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 193 | main.stepResults[ 3 ][ -1 ] = arguments[ 'ONFAIL' ] |
Jon Hall | 80577e7 | 2015-09-29 14:07:25 -0700 | [diff] [blame] | 194 | except AttributeError: |
| 195 | pass |
| 196 | else: |
| 197 | main.log.warn( "Assertion called before a test step" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 198 | return result |
Jon Hall | 8ce34e8 | 2015-06-05 10:41:45 -0700 | [diff] [blame] | 199 | |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 200 | def parse_args( self, args, **kwargs ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 201 | ''' |
| 202 | It will accept the (key,value) pair and will return the (key,value) pairs with keys in uppercase. |
| 203 | ''' |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 204 | newArgs = {} |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 205 | for key, value in kwargs.iteritems(): |
| 206 | if isinstance( args, list ) and str.upper( key ) in args: |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 207 | for each in args: |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 208 | if each == str.upper( key ): |
| 209 | newArgs[ str( each ) ] = value |
| 210 | elif each != str.upper( key ) and str( each ) not in newArgs: |
| 211 | newArgs[ str( each ) ] = None |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 212 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 213 | return newArgs |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 214 | |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 215 | def send_mail( self ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 216 | # Create a text/plain message |
| 217 | msg = email.mime.Multipart.MIMEMultipart() |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 218 | try: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 219 | if main.test_target: |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 220 | sub = "Result summary of \"" + main.TEST + "\" run on component \"" + \ |
| 221 | main.test_target + "\" Version \"" + \ |
| 222 | vars( main )[ main.test_target ].get_version() + "\": " + \ |
You Wang | c2e22b2 | 2019-02-28 17:11:11 -0800 | [diff] [blame] | 223 | str( main.TOTAL_TC_SUCCESS_PERCENT ) + "% Passed" |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 224 | else: |
| 225 | sub = "Result summary of \"" + main.TEST + "\": " + \ |
You Wang | c2e22b2 | 2019-02-28 17:11:11 -0800 | [diff] [blame] | 226 | str( main.TOTAL_TC_SUCCESS_PERCENT ) + "% Passed" |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 227 | except( KeyError, AttributeError ): |
| 228 | sub = "Result summary of \"" + main.TEST + "\": " + \ |
You Wang | c2e22b2 | 2019-02-28 17:11:11 -0800 | [diff] [blame] | 229 | str( main.TOTAL_TC_SUCCESS_PERCENT ) + "% Passed" |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 230 | |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 231 | msg[ 'Subject' ] = sub |
| 232 | msg[ 'From' ] = main.sender |
| 233 | msg[ 'To' ] = main.mail |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 234 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 235 | # The main body is just another attachment |
Jon Hall | 2507978 | 2015-10-13 13:54:39 -0700 | [diff] [blame] | 236 | body = email.mime.Text.MIMEText( main.logHeader + "\n" + |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 237 | main.testResult ) |
Jon Hall | 2507978 | 2015-10-13 13:54:39 -0700 | [diff] [blame] | 238 | msg.attach( body ) |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 239 | |
Jon Hall | 2507978 | 2015-10-13 13:54:39 -0700 | [diff] [blame] | 240 | # Attachments |
| 241 | for filename in os.listdir( main.logdir ): |
| 242 | filepath = main.logdir + "/" + filename |
| 243 | fp = open( filepath, 'rb' ) |
| 244 | att = email.mime.application.MIMEApplication( fp.read(), |
| 245 | _subtype="" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 246 | fp.close() |
Jon Hall | 2507978 | 2015-10-13 13:54:39 -0700 | [diff] [blame] | 247 | att.add_header( 'Content-Disposition', |
| 248 | 'attachment', |
| 249 | filename=filename ) |
| 250 | msg.attach( att ) |
| 251 | try: |
| 252 | smtp = smtplib.SMTP( main.smtp ) |
| 253 | smtp.starttls() |
| 254 | smtp.login( main.sender, main.senderPwd ) |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 255 | smtp.sendmail( msg[ 'From' ], [ msg[ 'To' ]], msg.as_string() ) |
Jon Hall | 2507978 | 2015-10-13 13:54:39 -0700 | [diff] [blame] | 256 | smtp.quit() |
| 257 | except Exception: |
| 258 | main.log.exception( "Error sending email" ) |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 259 | return main.TRUE |
| 260 | |
Jon Hall | 2507978 | 2015-10-13 13:54:39 -0700 | [diff] [blame] | 261 | def send_warning_email( self, subject=None ): |
| 262 | try: |
| 263 | if not subject: |
| 264 | subject = main.TEST + " PAUSED!" |
| 265 | # Create a text/plain message |
| 266 | msg = email.mime.Multipart.MIMEMultipart() |
| 267 | |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 268 | msg[ 'Subject' ] = subject |
| 269 | msg[ 'From' ] = main.sender |
| 270 | msg[ 'To' ] = main.mail |
Jon Hall | 2507978 | 2015-10-13 13:54:39 -0700 | [diff] [blame] | 271 | |
| 272 | smtp = smtplib.SMTP( main.smtp ) |
| 273 | smtp.starttls() |
| 274 | smtp.login( main.sender, main.senderPwd ) |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 275 | smtp.sendmail( msg[ 'From' ], [ msg[ 'To' ]], msg.as_string() ) |
Jon Hall | 2507978 | 2015-10-13 13:54:39 -0700 | [diff] [blame] | 276 | smtp.quit() |
| 277 | except Exception: |
| 278 | main.log.exception( "" ) |
| 279 | return main.FALSE |
| 280 | return main.TRUE |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 281 | |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 282 | def parse( self, fileName ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 283 | ''' |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 284 | This will parse the params or topo or cfg file and return content in the file as Dictionary |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 285 | ''' |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 286 | self.fileName = fileName |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 287 | matchFileName = re.match( r'(.*)\.(cfg|params|topo)', self.fileName, re.M | re.I ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 288 | if matchFileName: |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 289 | try: |
| 290 | parsedInfo = ConfigObj( self.fileName ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 291 | return parsedInfo |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 292 | except StandardError: |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 293 | print "There is no such file to parse " + fileName |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 294 | else: |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 295 | return 0 |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 296 | |
Jon Hall | 095730a | 2015-12-17 14:57:45 -0800 | [diff] [blame] | 297 | def retry( self, f, retValue, args=(), kwargs={}, |
Devin Lim | a4f95bc | 2017-08-11 11:13:03 -0700 | [diff] [blame] | 298 | sleep=1, attempts=2, randomTime=False, |
| 299 | getRetryingTime=False ): |
Jon Hall | 095730a | 2015-12-17 14:57:45 -0800 | [diff] [blame] | 300 | """ |
| 301 | Given a function and bad return values, retry will retry a function |
| 302 | until successful or give up after a certain number of attempts. |
| 303 | |
| 304 | Arguments: |
| 305 | f - a callable object |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 306 | retValue - Return value(s) of f to retry on. This can be a list or an |
Jon Hall | 095730a | 2015-12-17 14:57:45 -0800 | [diff] [blame] | 307 | object. |
| 308 | args - A tuple containing the arguments of f. |
| 309 | kwargs - A dictionary containing the keyword arguments of f. |
| 310 | sleep - Time in seconds to sleep between retries. If random is True, |
| 311 | this is the max time to wait. Defaults to 1 second. |
| 312 | attempts - Max number of attempts before returning. If set to 1, |
| 313 | f will only be called once. Defaults to 2 trys. |
| 314 | random - Boolean indicating if the wait time is random between 0 |
| 315 | and sleep or exactly sleep seconds. Defaults to False. |
| 316 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 317 | # TODO: be able to pass in a conditional statement(s). For example: |
Jon Hall | 095730a | 2015-12-17 14:57:45 -0800 | [diff] [blame] | 318 | # retCondition = "< 7" |
| 319 | # Then we do something like 'if eval( "ret " + retCondition ):break' |
| 320 | try: |
| 321 | assert attempts > 0, "attempts must be more than 1" |
| 322 | assert sleep >= 0, "sleep must be >= 0" |
| 323 | if not isinstance( retValue, list ): |
| 324 | retValue = [ retValue ] |
Devin Lim | a4f95bc | 2017-08-11 11:13:03 -0700 | [diff] [blame] | 325 | if getRetryingTime: |
| 326 | startTime = time.time() |
Jon Hall | 095730a | 2015-12-17 14:57:45 -0800 | [diff] [blame] | 327 | for i in range( 0, attempts ): |
| 328 | ret = f( *args, **kwargs ) |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 329 | if ret not in retValue: # NOTE that False in [ 0 ] == True |
Jon Hall | 095730a | 2015-12-17 14:57:45 -0800 | [diff] [blame] | 330 | break |
| 331 | if randomTime: |
| 332 | sleeptime = random.randint( 0, sleep ) |
| 333 | else: |
| 334 | sleeptime = sleep |
| 335 | time.sleep( sleeptime ) |
Jon Hall | 2ad817e | 2017-05-18 11:13:10 -0700 | [diff] [blame] | 336 | if i > 0: |
| 337 | main.log.debug( str( f ) + " was retried " + str( i ) + " times." ) |
Devin Lim | a4f95bc | 2017-08-11 11:13:03 -0700 | [diff] [blame] | 338 | if getRetryingTime: |
| 339 | main.log.debug( "Took " + str( time.time() - startTime ) + " seconds for retrying." ) |
Jon Hall | 095730a | 2015-12-17 14:57:45 -0800 | [diff] [blame] | 340 | return ret |
| 341 | except AssertionError: |
| 342 | main.log.exception( "Invalid arguements for retry: " ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 343 | main.cleanAndExit() |
Jon Hall | 095730a | 2015-12-17 14:57:45 -0800 | [diff] [blame] | 344 | except Exception: |
| 345 | main.log.exception( "Uncaught exception in retry: " ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 346 | main.cleanAndExit() |
Jon Hall | 095730a | 2015-12-17 14:57:45 -0800 | [diff] [blame] | 347 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 348 | |
| 349 | if __name__ != "__main__": |
| 350 | import sys |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 351 | |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 352 | sys.modules[ __name__ ] = Utilities() |