Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 1 | """ |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 2 | Copyright 2016 Open Networking Foundation ( ONF ) |
Jeremy Ronquillo | b27ce4c | 2017-07-17 12:41:28 -0700 | [diff] [blame] | 3 | |
| 4 | Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>, |
| 5 | the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>, |
| 6 | or the System Testing Guide page at <https://wiki.onosproject.org/x/WYQg> |
| 7 | |
| 8 | TestON is free software: you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation, either version 2 of the License, or |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 11 | ( at your option ) any later version. |
Jeremy Ronquillo | b27ce4c | 2017-07-17 12:41:28 -0700 | [diff] [blame] | 12 | |
| 13 | TestON is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
| 17 | |
| 18 | You should have received a copy of the GNU General Public License |
| 19 | along with TestON. If not, see <http://www.gnu.org/licenses/>. |
Jeremy Ronquillo | b27ce4c | 2017-07-17 12:41:28 -0700 | [diff] [blame] | 20 | |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 21 | Functions for the vpls tests |
| 22 | """ |
| 23 | import time |
| 24 | import json |
| 25 | |
Jon Hall | ed25823 | 2017-05-24 17:30:18 -0700 | [diff] [blame] | 26 | |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 27 | def sanitizeConfig( config ): |
| 28 | """ |
| 29 | Take a python json object for vpls config and normalize it. |
| 30 | Things it does: |
| 31 | Converts all strings to the same format |
| 32 | Make sure each network has an encapsulation key:value |
| 33 | Makes sure encapsulation type is all uppercase |
| 34 | Make sure an empty list of interfaces is formated consistently |
| 35 | Sorts the list of interfaces |
Jon Hall | 0dccfa5 | 2017-05-18 11:21:11 -0700 | [diff] [blame] | 36 | Sorts the list of networks |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 37 | """ |
| 38 | # Convert to same string formats |
| 39 | config = json.loads( json.dumps( config ) ) |
| 40 | for network in config: |
| 41 | encap = network.get( 'encapsulation', None ) |
| 42 | if encap is None: |
| 43 | encap = "NONE" |
| 44 | network[ 'encapsulation' ] = encap.upper() |
| 45 | ifaces = network.get( 'interfaces' ) |
Jon Hall | ed25823 | 2017-05-24 17:30:18 -0700 | [diff] [blame] | 46 | if ifaces == [ '' ]: |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 47 | ifaces = [] |
| 48 | else: |
| 49 | ifaces = sorted( ifaces ) |
Jon Hall | ed25823 | 2017-05-24 17:30:18 -0700 | [diff] [blame] | 50 | network[ 'interfaces' ] = ifaces |
| 51 | config = sorted( config, key=lambda k: k[ 'name' ] ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 52 | return config |
| 53 | |
Jon Hall | ed25823 | 2017-05-24 17:30:18 -0700 | [diff] [blame] | 54 | |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 55 | def verify( main ): |
| 56 | """ |
| 57 | Runs some tests to verify the vpls configurations. |
| 58 | - Compare sent vpls network configuration to what is stored in each: |
| 59 | - ONOS network configuration |
| 60 | - ONOS VPLS application configuration |
| 61 | - Ping between each pair of hosts to check connectivity |
| 62 | |
| 63 | NOTE: This requires the expected/sent network config json for the vpls |
| 64 | application be stored in main.vplsConfig |
| 65 | """ |
| 66 | # Variables |
Jon Hall | ed25823 | 2017-05-24 17:30:18 -0700 | [diff] [blame] | 67 | app = main.params[ 'vpls' ][ 'name' ] |
Devin Lim | 6301b74 | 2017-08-07 11:00:21 -0700 | [diff] [blame] | 68 | pprint = main.Cluster.active( 0 ).REST.pprint |
Jon Hall | ed25823 | 2017-05-24 17:30:18 -0700 | [diff] [blame] | 69 | SLEEP = int( main.params[ 'SLEEP' ][ 'netcfg' ] ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 70 | |
| 71 | main.step( "Check network configurations for vpls application" ) |
| 72 | clusterResult = True |
Devin Lim | 6301b74 | 2017-08-07 11:00:21 -0700 | [diff] [blame] | 73 | for ctrl in main.Cluster.active(): |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 74 | result = False |
Devin Lim | 6301b74 | 2017-08-07 11:00:21 -0700 | [diff] [blame] | 75 | getVPLS = utilities.retry( f=ctrl.REST.getNetCfg, |
Jeremy Ronquillo | 4a30ffe | 2017-06-07 11:36:35 -0700 | [diff] [blame] | 76 | retValue=False, |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 77 | kwargs={ "subjectClass": "apps", "subjectKey": app }, |
Jeremy Ronquillo | 4a30ffe | 2017-06-07 11:36:35 -0700 | [diff] [blame] | 78 | sleep=SLEEP ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 79 | onosCfg = json.loads( getVPLS ).get( 'vpls' ).get( 'vplsList' ) |
| 80 | onosCfg = pprint( sanitizeConfig( onosCfg ) ) |
| 81 | sentCfg = pprint( sanitizeConfig( main.vplsConfig ) ) |
| 82 | result = onosCfg == sentCfg |
| 83 | if result: |
| 84 | main.log.info( "ONOS NetCfg matches what was sent" ) |
| 85 | else: |
| 86 | clusterResult = False |
| 87 | main.log.error( "ONOS NetCfg doesn't match what was sent" ) |
| 88 | main.log.debug( "ONOS config: {}".format( onosCfg ) ) |
| 89 | main.log.debug( "Sent config: {}".format( sentCfg ) ) |
| 90 | utilities.assert_equals( expect=True, |
| 91 | actual=clusterResult, |
| 92 | onpass="Net Cfg added for vpls", |
| 93 | onfail="Net Cfg not added for vpls" ) |
| 94 | |
| 95 | main.step( "Check vpls app configurations" ) |
| 96 | clusterResult = True |
Devin Lim | 6301b74 | 2017-08-07 11:00:21 -0700 | [diff] [blame] | 97 | for ctrl in main.Cluster.active(): |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 98 | result = False |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 99 | # TODO Read from vpls show and match to pushed json |
Devin Lim | 6301b74 | 2017-08-07 11:00:21 -0700 | [diff] [blame] | 100 | vpls = ctrl.CLI.parseVplsShow() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 101 | parsedVpls = pprint( sanitizeConfig( vpls ) ) |
| 102 | sentVpls = pprint( sanitizeConfig( main.vplsConfig ) ) |
| 103 | result = parsedVpls == sentVpls |
| 104 | if result: |
| 105 | main.log.info( "VPLS config matches sent NetCfg" ) |
| 106 | else: |
| 107 | clusterResult = False |
| 108 | main.log.error( "VPLS config doesn't match sent NetCfg" ) |
| 109 | main.log.debug( "ONOS config: {}".format( parsedVpls ) ) |
| 110 | main.log.debug( "Sent config: {}".format( sentVpls ) ) |
| 111 | utilities.assert_equals( expect=True, |
| 112 | actual=clusterResult, |
| 113 | onpass="VPLS successfully configured", |
| 114 | onfail="VPLS not configured correctly" ) |
| 115 | |
Jeremy Ronquillo | 3008aa3 | 2017-07-07 15:38:57 -0700 | [diff] [blame] | 116 | checkIntentState( main ) |
| 117 | |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 118 | main.step( "Check connectivity" ) |
| 119 | connectivityCheck = True |
Jon Hall | ed25823 | 2017-05-24 17:30:18 -0700 | [diff] [blame] | 120 | hosts = int( main.params[ 'vpls' ][ 'hosts' ] ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 121 | networks = [] |
| 122 | for network in main.vplsConfig: |
| 123 | nodes = network.get( 'interfaces', None ) |
| 124 | if nodes: |
| 125 | networks.append( nodes ) |
| 126 | for i in range( 1, hosts + 1 ): |
| 127 | src = "h" + str( i ) |
| 128 | for j in range( 1, hosts + 1 ): |
| 129 | if j == i: |
| 130 | continue |
| 131 | dst = "h" + str( j ) |
| 132 | pingResult = main.Mininet1.pingHost( SRC=src, TARGET=dst ) |
| 133 | expected = main.FALSE |
| 134 | for network in networks: |
| 135 | if src in network and dst in network: |
| 136 | expected = main.TRUE |
| 137 | break |
| 138 | if pingResult != expected: |
| 139 | connectivityCheck = False |
| 140 | main.log.error( "%s <-> %s: %s; Expected: %s" % |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 141 | ( src, dst, pingResult, expected ) ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 142 | utilities.assert_equals( expect=True, |
| 143 | actual=connectivityCheck, |
| 144 | onpass="Connectivity is as expected", |
| 145 | onfail="Connectivity is not as expected" ) |
Jeremy Ronquillo | 3008aa3 | 2017-07-07 15:38:57 -0700 | [diff] [blame] | 146 | |
Jeremy Ronquillo | 3008aa3 | 2017-07-07 15:38:57 -0700 | [diff] [blame] | 147 | # TODO: if encapsulation is set, look for that |
| 148 | # TODO: can we look at the intent keys? |
| 149 | |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 150 | |
| 151 | def checkIntentState( main, bl=[] ): |
Jeremy Ronquillo | 3008aa3 | 2017-07-07 15:38:57 -0700 | [diff] [blame] | 152 | # Print the intent states |
Devin Lim | 6301b74 | 2017-08-07 11:00:21 -0700 | [diff] [blame] | 153 | intents = main.Cluster.active( 0 ).CLI.intents() |
Jeremy Ronquillo | 3008aa3 | 2017-07-07 15:38:57 -0700 | [diff] [blame] | 154 | count = 0 |
| 155 | while count <= 5: |
| 156 | installedCheck = True |
| 157 | try: |
| 158 | i = 1 |
| 159 | for intent in json.loads( intents ): |
| 160 | state = intent.get( 'state', None ) |
| 161 | if "INSTALLED" not in state or ( "WITHDRAWN" not in state and "h" + str( i ) in bl ): |
| 162 | installedCheck = False |
| 163 | i += 1 |
| 164 | except ( ValueError, TypeError ): |
| 165 | main.log.exception( "Error parsing intents" ) |
| 166 | if installedCheck: |
| 167 | break |
| 168 | count += 1 |
| 169 | return installedCheck |
| 170 | |
| 171 | |
| 172 | def getVplsHashtable( main, bl=[] ): |
| 173 | """ |
| 174 | Returns a hashtable of vpls to hosts |
| 175 | """ |
| 176 | result = {} |
| 177 | vplsConfig = main.vplsConfig |
| 178 | for v in vplsConfig: |
Jeremy Ronquillo | 23fb216 | 2017-09-15 14:59:57 -0700 | [diff] [blame] | 179 | interfaces = v[ 'interfaces' ][ : ] |
Jeremy Ronquillo | 3008aa3 | 2017-07-07 15:38:57 -0700 | [diff] [blame] | 180 | for i in bl: |
| 181 | if i in interfaces: |
| 182 | interfaces.remove( i ) |
| 183 | result[ v[ 'name' ] ] = interfaces |
| 184 | return result |
| 185 | |
| 186 | |
| 187 | def testConnectivityVpls( main, blacklist=[], isNodeUp=True ): |
| 188 | |
| 189 | # Can't do intent check when onos node is stopped/killed yet |
| 190 | if isNodeUp: |
| 191 | main.step( "Check intent states" ) |
| 192 | intentsCheck = utilities.retry( f=checkIntentState, |
| 193 | retValue=False, |
| 194 | args=( main, blacklist ), |
| 195 | sleep=main.timeSleep, |
| 196 | attempts=main.numAttempts ) |
| 197 | |
| 198 | utilities.assert_equals( expect=True, |
| 199 | actual=intentsCheck, |
| 200 | onpass="All Intents in installed state", |
| 201 | onfail="Not all Intents in installed state" ) |
| 202 | |
| 203 | main.step( "Testing connectivity..." ) |
| 204 | |
| 205 | vplsHashtable = getVplsHashtable( main, blacklist ) |
| 206 | main.log.debug( "vplsHashtable: " + str( vplsHashtable ) ) |
| 207 | result = True |
| 208 | for key in vplsHashtable: |
| 209 | pingResult = utilities.retry( f=main.Mininet1.pingallHosts, |
| 210 | retValue=False, |
| 211 | args=( vplsHashtable[ key ], ), |
| 212 | sleep=main.timeSleep, |
| 213 | attempts=main.numAttempts ) |
| 214 | result = result and pingResult |
| 215 | |
| 216 | utilities.assert_equals( expect=main.TRUE, actual=result, |
| 217 | onpass="Connectivity succeeded.", |
| 218 | onfail="Connectivity failed." ) |
| 219 | return result |
| 220 | |
| 221 | |
| 222 | def compareApps( main ): |
| 223 | result = True |
| 224 | first = None |
Devin Lim | 6301b74 | 2017-08-07 11:00:21 -0700 | [diff] [blame] | 225 | for ctrl in main.Cluster.active(): |
| 226 | currentApps = ctrl.CLI.apps( summary=True, active=True ) |
Jeremy Ronquillo | 3008aa3 | 2017-07-07 15:38:57 -0700 | [diff] [blame] | 227 | if not result: |
| 228 | first = currentApps |
| 229 | else: |
| 230 | result = result and ( currentApps == first ) |
| 231 | return result |