Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -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 07-Jan-2013 |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 4 | Modified 2015 by Open Networking Foundation |
Jon Hall | 81d3d39 | 2015-04-24 14:40:35 -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 | 81d3d39 | 2015-04-24 14:40:35 -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 | |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 24 | ''' |
| 25 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 26 | import logging |
| 27 | import datetime |
| 28 | import re |
| 29 | import os |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 30 | import json |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 31 | class Logger: |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 32 | ''' |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 33 | Add continuous logs and reports of the test. |
Jon Hall | 81d3d39 | 2015-04-24 14:40:35 -0700 | [diff] [blame] | 34 | |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 35 | @author: Raghav Kashyap(raghavkashyap@paxterrasolutions.com) |
| 36 | ''' |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 37 | def _printHeader( self, main ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 38 | ''' |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 39 | Log's header will be append to the Log file |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 40 | ''' |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 41 | logmsg = "\n" + " " * 32 + "+----------------+\n" + "-" * 30 + " { Script And Files } " + "-" * 30 + "\n" + " " * 32 + "+----------------+\n" |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 42 | logmsg = logmsg + "\n\tScript Log File : " + main.LogFileName + "" |
| 43 | logmsg = logmsg + "\n\tReport Log File : " + main.ReportFileName + "" |
| 44 | for component in main.componentDictionary.keys(): |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 45 | logmsg = logmsg + "\n\t" + component + " Session Log : " + main.logdir + "/" + component + ".session" + "" |
Jon Hall | 81d3d39 | 2015-04-24 14:40:35 -0700 | [diff] [blame] | 46 | |
Jon Hall | d74d295 | 2018-03-01 13:26:39 -0800 | [diff] [blame] | 47 | logmsg = logmsg + "\n\tTest Script : " + main.testFile + "" |
| 48 | logmsg = logmsg + "\n\tTest Params : " + main.testDir + "/" + main.paramsFile + "" |
| 49 | logmsg = logmsg + "\n\tTopology : " + main.testDir + "/" + main.topoFile + "" |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 50 | logmsg = logmsg + "\n" + " " * 30 + "+" + "-" * 18 + "+" + "\n" + "-" * 27 + " { Script Exec Params } " + "-" * 27 + "\n" + " " * 30 + "+" + "-" * 18 + "+\n" |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 51 | logmsg += "\n" + json.dumps( main.params, sort_keys=True, indent=4 ) |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 52 | logmsg = logmsg + "\n\n" + " " * 31 + "+---------------+\n" + "-" * 29 + " { Components Used } " + "-" * 29 + "\n" + " " * 31 + "+---------------+\n" |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 53 | component_list = [] |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 54 | component_list.append( None ) |
Jon Hall | 81d3d39 | 2015-04-24 14:40:35 -0700 | [diff] [blame] | 55 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 56 | # Listing the components in the order of test_target component should be first. |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 57 | if type( main.componentDictionary ) == dict: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 58 | for key in main.componentDictionary.keys(): |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 59 | if main.test_target == key: |
| 60 | component_list[ 0 ] = key + "-Test Target" |
| 61 | else: |
| 62 | component_list.append( key ) |
Jon Hall | 81d3d39 | 2015-04-24 14:40:35 -0700 | [diff] [blame] | 63 | |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 64 | for index in range( len( component_list ) ): |
| 65 | if index == 0: |
| 66 | if component_list[ index ]: |
| 67 | logmsg += "\t" + component_list[ index ] + "\n" |
| 68 | elif index > 0: |
| 69 | logmsg += "\t" + str( component_list[ index ] ) + "\n" |
Jon Hall | 81d3d39 | 2015-04-24 14:40:35 -0700 | [diff] [blame] | 70 | |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 71 | logmsg = logmsg + "\n\n" + " " * 30 + "+--------+\n" + "-" * 28 + " { Topology } " + "-" * 28 + "\n" + " " * 30 + "+--------+\n" |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 72 | sortedComponents = sorted( main.topology['COMPONENT'].items(), key=lambda (k, v): v[ 'connect_order' ] ) |
| 73 | logmsg += "\n" + json.dumps( sortedComponents, indent=4 ) |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 74 | logmsg = logmsg + "\n" + "-" * 60 + "\n" |
Jon Hall | 81d3d39 | 2015-04-24 14:40:35 -0700 | [diff] [blame] | 75 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 76 | # enter into log file all headers |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 77 | logfile = open( main.LogFileName, "w+" ) |
| 78 | logfile.write( logmsg ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 79 | print logmsg |
| 80 | main.logHeader = logmsg |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 81 | logfile.close() |
Jon Hall | 81d3d39 | 2015-04-24 14:40:35 -0700 | [diff] [blame] | 82 | |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 83 | # enter into report file all headers |
| 84 | main.reportFile = open( main.ReportFileName, "w+" ) |
| 85 | main.reportFile.write( logmsg ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 86 | main.reportFile.close() |
Jon Hall | 81d3d39 | 2015-04-24 14:40:35 -0700 | [diff] [blame] | 87 | |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 88 | # Sumamry file header |
| 89 | currentTime = str( main.STARTTIME.strftime( "%d %b %Y %H:%M:%S" ) ) |
Jon Hall | 79bec22 | 2015-04-30 16:23:30 -0700 | [diff] [blame] | 90 | main.summaryFile = open( main.SummaryFileName, "w+" ) |
| 91 | main.summaryFile.write( main.TEST + " at " + currentTime + "\n" ) |
| 92 | main.summaryFile.close() |
| 93 | |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 94 | # wiki file header |
| 95 | currentTime = str( main.STARTTIME.strftime( "%d %b %Y %H:%M:%S" ) ) |
Jon Hall | 81d3d39 | 2015-04-24 14:40:35 -0700 | [diff] [blame] | 96 | main.wikiFile = open( main.WikiFileName, "w+" ) |
Jon Hall | 79bec22 | 2015-04-30 16:23:30 -0700 | [diff] [blame] | 97 | main.wikiFile.write( main.TEST + " at " + currentTime + "<p></p>\n" ) |
Jon Hall | 81d3d39 | 2015-04-24 14:40:35 -0700 | [diff] [blame] | 98 | main.wikiFile.close() |
| 99 | |
Jon Hall | 026bd71 | 2021-08-31 16:30:09 -0700 | [diff] [blame] | 100 | # TAP file header |
| 101 | main.TAPFile = open( main.TAPFileName, "w+" ) |
| 102 | main.TAPFile.write( "TAP version 13\n" ) |
| 103 | main.TAPFile.close() |
| 104 | |
| 105 | |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 106 | def initlog( self, main ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 107 | ''' |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 108 | Initialise all the log handles. |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 109 | ''' |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 110 | main._getTest() |
Jon Hall | 81d3d39 | 2015-04-24 14:40:35 -0700 | [diff] [blame] | 111 | main.STARTTIME = datetime.datetime.now() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 112 | |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 113 | currentTime = re.sub( "-|\s|:|\.", "_", str( main.STARTTIME.strftime( "%d %b %Y %H:%M:%S" ) ) ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 114 | if main.logdir: |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 115 | main.logdir = main.logdir + "/" + main.TEST + "_" + currentTime |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 116 | else: |
| 117 | main.logdir = main.logs_path + main.TEST + "_" + currentTime |
Jon Hall | 81d3d39 | 2015-04-24 14:40:35 -0700 | [diff] [blame] | 118 | |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 119 | os.mkdir( main.logdir ) |
Jon Hall | 81d3d39 | 2015-04-24 14:40:35 -0700 | [diff] [blame] | 120 | |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 121 | main.LogFileName = main.logdir + "/" + main.TEST + "_" + str( currentTime ) + ".log" |
| 122 | main.ReportFileName = main.logdir + "/" + main.TEST + "_" + str( currentTime ) + ".rpt" |
Jon Hall | 79bec22 | 2015-04-30 16:23:30 -0700 | [diff] [blame] | 123 | main.WikiFileName = main.logdir + "/" + main.TEST + "Wiki.txt" |
Jon Hall | 026bd71 | 2021-08-31 16:30:09 -0700 | [diff] [blame] | 124 | main.TAPFileName = main.logdir + "/" + main.TEST + ".tap" |
Jon Hall | 79bec22 | 2015-04-30 16:23:30 -0700 | [diff] [blame] | 125 | main.SummaryFileName = main.logdir + "/" + main.TEST + "Summary.txt" |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 126 | main.JenkinsCSV = main.logdir + "/" + main.TEST + ".csv" |
Devin Lim | 90803a8 | 2017-08-29 13:41:44 -0700 | [diff] [blame] | 127 | main.resultFile = main.logdir + "/" + main.TEST + "Result.txt" |
You Wang | 28cb855 | 2019-02-19 16:11:52 -0800 | [diff] [blame] | 128 | main.alarmFileName = main.logdir + "/" + main.TEST + "Alarm.txt" |
Devin Lim | 90803a8 | 2017-08-29 13:41:44 -0700 | [diff] [blame] | 129 | |
You Wang | c2e22b2 | 2019-02-28 17:11:11 -0800 | [diff] [blame] | 130 | main.TOTAL_TC_SUCCESS_PERCENT = 0 |
Jon Hall | 81d3d39 | 2015-04-24 14:40:35 -0700 | [diff] [blame] | 131 | |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 132 | # Add log-level - Report |
| 133 | logging.addLevelName( 9, "REPORT" ) |
| 134 | logging.addLevelName( 7, "EXACT" ) |
| 135 | logging.addLevelName( 11, "CASE" ) |
| 136 | logging.addLevelName( 12, "STEP" ) |
| 137 | main.log = logging.getLogger( main.TEST ) |
| 138 | |
| 139 | def report( msg ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 140 | ''' |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 141 | Will append the report message to the logs. |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 142 | ''' |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 143 | main.log._log( 9, msg, "OpenFlowAutoMattion", "OFAutoMation" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 144 | currentTime = datetime.datetime.now() |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 145 | currentTime = currentTime.strftime( "%d %b %Y %H:%M:%S" ) |
| 146 | newmsg = "\n[REPORT] " + "[" + str( currentTime ) + "] " + msg |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 147 | print newmsg |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 148 | main.reportFile = open( main.ReportFileName, "a+" ) |
| 149 | main.reportFile.write( newmsg ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 150 | main.reportFile.close() |
Jon Hall | 81d3d39 | 2015-04-24 14:40:35 -0700 | [diff] [blame] | 151 | |
| 152 | main.log.report = report |
| 153 | |
Jon Hall | 79bec22 | 2015-04-30 16:23:30 -0700 | [diff] [blame] | 154 | def summary( msg ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 155 | ''' |
Jon Hall | 79bec22 | 2015-04-30 16:23:30 -0700 | [diff] [blame] | 156 | Will append the message to the txt file for the summary. |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 157 | ''' |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 158 | main.log._log( 6, msg, "OpenFlowAutoMattion", "OFAutoMation" ) |
| 159 | main.summaryFile = open( main.SummaryFileName, "a+" ) |
| 160 | main.summaryFile.write( msg + "\n" ) |
Jon Hall | 79bec22 | 2015-04-30 16:23:30 -0700 | [diff] [blame] | 161 | main.summaryFile.close() |
| 162 | |
| 163 | main.log.summary = summary |
| 164 | |
Jon Hall | 81d3d39 | 2015-04-24 14:40:35 -0700 | [diff] [blame] | 165 | def wiki( msg ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 166 | ''' |
Jon Hall | 81d3d39 | 2015-04-24 14:40:35 -0700 | [diff] [blame] | 167 | Will append the message to the txt file for the wiki. |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 168 | ''' |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 169 | main.log._log( 6, msg, "OpenFlowAutoMattion", "OFAutoMation" ) |
| 170 | main.wikiFile = open( main.WikiFileName, "a+" ) |
| 171 | main.wikiFile.write( msg + "\n" ) |
Jon Hall | 81d3d39 | 2015-04-24 14:40:35 -0700 | [diff] [blame] | 172 | main.wikiFile.close() |
| 173 | |
| 174 | main.log.wiki = wiki |
| 175 | |
Jon Hall | 026bd71 | 2021-08-31 16:30:09 -0700 | [diff] [blame] | 176 | def TAP( msg ): |
| 177 | ''' |
| 178 | Will append the message to the txt file for TAP. |
| 179 | ''' |
| 180 | main.log._log( 6, msg, "OpenFlowAutoMattion", "OFAutoMation" ) |
| 181 | main.TAPFile = open( main.TAPFileName, "a+" ) |
| 182 | main.TAPFile.write( msg + "\n" ) |
| 183 | main.TAPFile.close() |
| 184 | |
| 185 | main.log.TAP = TAP |
| 186 | |
| 187 | |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 188 | def exact( exmsg ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 189 | ''' |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 190 | Will append the raw formatted message to the logs |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 191 | ''' |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 192 | main.log._log( 7, exmsg, "OpenFlowAutoMattion", "OFAutoMation" ) |
| 193 | main.reportFile = open( main.ReportFileName, "a+" ) |
| 194 | main.reportFile.write( exmsg ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 195 | main.reportFile.close() |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 196 | logfile = open( main.LogFileName, "a" ) |
| 197 | logfile.write( "\n" + str( exmsg ) + "\n" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 198 | logfile.close() |
| 199 | print exmsg |
Jon Hall | 81d3d39 | 2015-04-24 14:40:35 -0700 | [diff] [blame] | 200 | |
| 201 | main.log.exact = exact |
| 202 | |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 203 | def case( msg ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 204 | ''' |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 205 | Format of the case type log defined here. |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 206 | ''' |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 207 | main.log._log( 9, msg, "OpenFlowAutoMattion", "OFAutoMation" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 208 | currentTime = datetime.datetime.now() |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 209 | newmsg = "[" + str( currentTime ) + "] " + "[" + main.TEST + "] " + "[CASE] " + msg |
| 210 | logfile = open( main.LogFileName, "a" ) |
| 211 | logfile.write( "\n" + str( newmsg ) + "\n" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 212 | logfile.close() |
| 213 | print newmsg |
Jon Hall | 81d3d39 | 2015-04-24 14:40:35 -0700 | [diff] [blame] | 214 | |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 215 | main.log.case = case |
Jon Hall | 81d3d39 | 2015-04-24 14:40:35 -0700 | [diff] [blame] | 216 | |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 217 | def step( msg ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 218 | ''' |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 219 | Format of the step type log defined here. |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 220 | ''' |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 221 | main.log._log( 9, msg, "OpenFlowAutoMattion", "OFAutoMation" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 222 | currentTime = datetime.datetime.now() |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 223 | newmsg = "[" + str( currentTime ) + "] " + "[" + main.TEST + "] " + "[STEP] " + msg |
| 224 | logfile = open( main.LogFileName, "a" ) |
| 225 | logfile.write( "\n" + str( newmsg ) + "\n" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 226 | logfile.close() |
| 227 | print newmsg |
Jon Hall | 81d3d39 | 2015-04-24 14:40:35 -0700 | [diff] [blame] | 228 | |
| 229 | main.log.step = step |
| 230 | |
You Wang | 28cb855 | 2019-02-19 16:11:52 -0800 | [diff] [blame] | 231 | def alarm( msg ): |
| 232 | ''' |
| 233 | Format of the alarm type log defined here. |
| 234 | ''' |
| 235 | main.log._log( 6, msg, "OpenFlowAutoMattion", "OFAutoMation" ) |
| 236 | main.alarmFile = open( main.alarmFileName, "a+" ) |
| 237 | main.alarmFile.write( msg + "\n" ) |
| 238 | main.alarmFile.close() |
| 239 | |
| 240 | main.log.alarm = alarm |
| 241 | |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 242 | main.LogFileHandler = logging.FileHandler( main.LogFileName ) |
| 243 | self._printHeader( main ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 244 | |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 245 | # initializing logging module and settig log level |
| 246 | main.log.setLevel( logging.INFO ) |
| 247 | main.log.setLevel( logging.DEBUG ) # Temporary |
| 248 | main.LogFileHandler.setLevel( logging.INFO ) |
Jon Hall | 81d3d39 | 2015-04-24 14:40:35 -0700 | [diff] [blame] | 249 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 250 | # create console handler with a higher log level |
| 251 | main.ConsoleHandler = logging.StreamHandler() |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 252 | main.ConsoleHandler.setLevel( logging.INFO ) |
| 253 | main.ConsoleHandler.setLevel( logging.DEBUG ) # Temporary |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 254 | # create formatter and add it to the handlers |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 255 | # formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') |
| 256 | |
Jon Hall | 0bde9ba | 2015-03-19 11:32:57 -0700 | [diff] [blame] | 257 | class MyFormatter( logging.Formatter ): |
| 258 | colors = { 'cyan': '\033[96m', 'purple': '\033[95m', |
| 259 | 'blue': '\033[94m', 'green': '\033[92m', |
| 260 | 'yellow': '\033[93m', 'red': '\033[91m', |
| 261 | 'end': '\033[0m' } |
| 262 | |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 263 | FORMATS = { 'DEFAULT': '%(asctime)s - %(name)s - %(levelname)s - %(message)s' } |
Jon Hall | 0bde9ba | 2015-03-19 11:32:57 -0700 | [diff] [blame] | 264 | if COLORS: # NOTE:colors will only be loaded if command is run from one line |
| 265 | # IE: './cli.py run testname' |
| 266 | # This is to prevent issues with Jenkins parsing |
| 267 | # TODO: Make colors configurable |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 268 | levels = { logging.ERROR: colors[ 'red' ] + |
| 269 | FORMATS[ 'DEFAULT' ] + |
| 270 | colors[ 'end' ], |
| 271 | logging.WARN: colors[ 'yellow' ] + |
| 272 | FORMATS[ 'DEFAULT' ] + |
| 273 | colors[ 'end' ], |
| 274 | logging.DEBUG: colors[ 'purple' ] + |
| 275 | FORMATS[ 'DEFAULT' ] + |
| 276 | colors[ 'end' ] } |
Jon Hall | 0bde9ba | 2015-03-19 11:32:57 -0700 | [diff] [blame] | 277 | FORMATS.update( levels ) |
| 278 | |
| 279 | def format( self, record ): |
| 280 | self._fmt = self.FORMATS.get( record.levelno, |
| 281 | self.FORMATS[ 'DEFAULT' ] ) |
| 282 | return logging.Formatter.format( self, record ) |
| 283 | formatter = MyFormatter() |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 284 | main.ConsoleHandler.setFormatter( formatter ) |
| 285 | main.LogFileHandler.setFormatter( formatter ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 286 | |
| 287 | # add the handlers to logger |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 288 | main.log.addHandler( main.ConsoleHandler ) |
| 289 | main.log.addHandler( main.LogFileHandler ) |
Jon Hall | 81d3d39 | 2015-04-24 14:40:35 -0700 | [diff] [blame] | 290 | |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 291 | def testSummary( self, main ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 292 | ''' |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 293 | testSummary will take care about the Summary of test. |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 294 | ''' |
| 295 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 296 | main.ENDTIME = datetime.datetime.now() |
| 297 | main.EXECTIME = main.ENDTIME - main.STARTTIME |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 298 | if ( main.TOTAL_TC_PASS == 0 ): |
You Wang | c2e22b2 | 2019-02-28 17:11:11 -0800 | [diff] [blame] | 299 | main.TOTAL_TC_SUCCESS_PERCENT = 0 |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 300 | else: |
You Wang | c2e22b2 | 2019-02-28 17:11:11 -0800 | [diff] [blame] | 301 | main.TOTAL_TC_SUCCESS_PERCENT = main.TOTAL_TC_PASS * 100 / main.TOTAL_TC_RUN |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 302 | if ( main.TOTAL_TC_RUN == 0 ): |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 303 | main.TOTAL_TC_EXECPERCENT = 0 |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 304 | else: |
| 305 | main.TOTAL_TC_EXECPERCENT = str( ( main.TOTAL_TC_RUN*100 )/main.TOTAL_TC_PLANNED ) |
| 306 | testResult = "\n\n" + "*" * 37 + "\n" + "\tTest Execution Summary\n" + "\n" + "*" * 37 + " \n" |
| 307 | testResult = testResult + "\n Test Start : " + str( main.STARTTIME.strftime( "%d %b %Y %H:%M:%S" ) ) |
| 308 | testResult = testResult + "\n Test End : " + str( main.ENDTIME.strftime( "%d %b %Y %H:%M:%S" ) ) |
| 309 | testResult = testResult + "\n Execution Time : " + str( main.EXECTIME ) |
You Wang | c2e22b2 | 2019-02-28 17:11:11 -0800 | [diff] [blame] | 310 | testResult = testResult + "\n Total Tests Planned : " + str( main.TOTAL_TC_PLANNED ) |
| 311 | testResult = testResult + "\n Total Tests Run : " + str( main.TOTAL_TC_RUN ) |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 312 | testResult = testResult + "\n Total Pass : " + str( main.TOTAL_TC_PASS ) |
| 313 | testResult = testResult + "\n Total Fail : " + str( main.TOTAL_TC_FAIL ) |
| 314 | testResult = testResult + "\n Total No Result : " + str( main.TOTAL_TC_NORESULT ) |
You Wang | c2e22b2 | 2019-02-28 17:11:11 -0800 | [diff] [blame] | 315 | testResult = testResult + "\n Success Percentage : " + str( main.TOTAL_TC_SUCCESS_PERCENT ) + "%" |
| 316 | testResult = testResult + "\n Execution Percentage : " + str( main.TOTAL_TC_EXECPERCENT ) + "%\n" |
Devin Lim | ec98979 | 2017-08-15 15:57:55 -0700 | [diff] [blame] | 317 | if main.failedCase: |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 318 | testResult = testResult + "\n Case Failed : " + str( main.failedCase ) |
Devin Lim | ec98979 | 2017-08-15 15:57:55 -0700 | [diff] [blame] | 319 | if main.noResultCase: |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 320 | testResult = testResult + "\n Case NoResult : " + str( main.noResultCase ) |
| 321 | testResult = testResult + "\n Case Executed : " + str( main.executedCase ) |
| 322 | testResult = testResult + "\n Case Not Executed : " + str( main.leftCase ) |
| 323 | # main.log.report(testResult) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 324 | main.testResult = testResult |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 325 | main.log.exact( testResult ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 326 | |
You Wang | c2e22b2 | 2019-02-28 17:11:11 -0800 | [diff] [blame] | 327 | # Write to alarm log if Success Percentage is lower than expected |
| 328 | if 'ALARM' in main.params.keys() and 'minPassPercent' in main.params[ 'ALARM' ].keys(): |
| 329 | minPassPercent = int( main.params[ 'ALARM' ][ 'minPassPercent' ] ) |
| 330 | if main.TOTAL_TC_SUCCESS_PERCENT < minPassPercent: |
| 331 | main.log.alarm( 'Success percentage: {}% < {}%'.format( main.TOTAL_TC_SUCCESS_PERCENT, minPassPercent ) ) |
| 332 | |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 333 | # CSV output needed for Jenkin's plot plugin |
| 334 | # NOTE: the elements were orded based on the colors assigned to the data |
| 335 | logfile = open( main.JenkinsCSV , "w" ) |
| 336 | logfile.write( ",".join( [ 'Tests Failed', 'Tests Passed', 'Tests Planned' ] ) + "\n" ) |
| 337 | logfile.write( ",".join( [ str( int( main.TOTAL_TC_FAIL ) ), str( int( main.TOTAL_TC_PASS ) ), str( int( main.TOTAL_TC_PLANNED ) ) ] ) + "\n" ) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 338 | logfile.close() |
| 339 | |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 340 | executedStatus = open( main.resultFile, "w" ) |
Devin Lim | 90803a8 | 2017-08-29 13:41:44 -0700 | [diff] [blame] | 341 | if main.TOTAL_TC_FAIL == 0 and main.TOTAL_TC_NORESULT + main.TOTAL_TC_PASS == main.TOTAL_TC_PLANNED: |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 342 | executedStatus.write( "1\n" ) |
Devin Lim | 90803a8 | 2017-08-29 13:41:44 -0700 | [diff] [blame] | 343 | else: |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 344 | executedStatus.write( "0\n" ) |
Devin Lim | 79af50f | 2017-10-26 14:26:47 -0700 | [diff] [blame] | 345 | executedStatus.write( "[Total]:" + str( main.TOTAL_TC_PLANNED ) + " [Executed]:" + str( main.TOTAL_TC_RUN ) + " [Failed]:" + str( main.TOTAL_TC_FAIL ) + "\n" ) |
Devin Lim | 90803a8 | 2017-08-29 13:41:44 -0700 | [diff] [blame] | 346 | executedStatus.close() |
| 347 | |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 348 | def updateCaseResults( self, main ): |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 349 | ''' |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 350 | Update the case result based on the steps execution and asserting each step in the test-case |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 351 | ''' |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 352 | case = str( main.CurrentTestCaseNumber ) |
| 353 | currentResult = main.testCaseResult.get( case, 2 ) |
Jon Hall | 81d3d39 | 2015-04-24 14:40:35 -0700 | [diff] [blame] | 354 | |
Jon Hall | 79bec22 | 2015-04-30 16:23:30 -0700 | [diff] [blame] | 355 | if currentResult == 2: |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 356 | main.TOTAL_TC_RUN = main.TOTAL_TC_RUN + 1 |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 357 | main.TOTAL_TC_NORESULT = main.TOTAL_TC_NORESULT + 1 |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 358 | main.log.exact( "\n " + "*" * 29 + "\n" + "\n Result: No Assertion Called \n" + "*" * 29 + "\n" ) |
| 359 | line = "Case " + case + ": " + main.CurrentTestCase + " - No Result" |
Jon Hall | 026bd71 | 2021-08-31 16:30:09 -0700 | [diff] [blame] | 360 | main.log.TAP( "ok - %s # TODO No assert called" % line ) |
Jon Hall | 79bec22 | 2015-04-30 16:23:30 -0700 | [diff] [blame] | 361 | elif currentResult == 1: |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 362 | main.TOTAL_TC_RUN = main.TOTAL_TC_RUN + 1 |
| 363 | main.TOTAL_TC_PASS = main.TOTAL_TC_PASS + 1 |
| 364 | main.log.exact( "\n" + "*" * 29 + "\n Result: Pass \n" + "*" * 29 + "\n" ) |
| 365 | line = "Case " + case + ": " + main.CurrentTestCase + " - PASS" |
Jon Hall | 026bd71 | 2021-08-31 16:30:09 -0700 | [diff] [blame] | 366 | main.log.TAP( "ok - %s" % line ) |
Jon Hall | 79bec22 | 2015-04-30 16:23:30 -0700 | [diff] [blame] | 367 | elif currentResult == 0: |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 368 | main.TOTAL_TC_RUN = main.TOTAL_TC_RUN + 1 |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 369 | main.TOTAL_TC_FAIL = main.TOTAL_TC_FAIL + 1 |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 370 | main.log.exact( "\n" + "*" * 29 + "\n Result: Failed \n" + "*" * 29 + "\n" ) |
| 371 | line = "Case " + case + ": " + main.CurrentTestCase + " - FAIL" |
Jon Hall | 026bd71 | 2021-08-31 16:30:09 -0700 | [diff] [blame] | 372 | main.log.TAP( "not ok - %s" % line ) |
Jon Hall | 79bec22 | 2015-04-30 16:23:30 -0700 | [diff] [blame] | 373 | else: |
| 374 | main.log.error( " Unknown result of case " + case + |
| 375 | ". Result was: " + currentResult ) |
Jeremy Ronquillo | 696f426 | 2017-10-17 10:56:26 -0700 | [diff] [blame] | 376 | line = "Case " + case + ": " + main.CurrentTestCase + " - ERROR" |
Jon Hall | 026bd71 | 2021-08-31 16:30:09 -0700 | [diff] [blame] | 377 | main.log.TAP( "not ok - %s" % line ) |
Jon Hall | 79bec22 | 2015-04-30 16:23:30 -0700 | [diff] [blame] | 378 | main.log.wiki( "<h3>" + line + "</h3>" ) |
| 379 | main.log.summary( line ) |