Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 1 | """ |
| 2 | Functions for the vpls tests |
| 3 | """ |
| 4 | import time |
| 5 | import json |
| 6 | |
Jon Hall | ed25823 | 2017-05-24 17:30:18 -0700 | [diff] [blame] | 7 | |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 8 | def sanitizeConfig( config ): |
| 9 | """ |
| 10 | Take a python json object for vpls config and normalize it. |
| 11 | Things it does: |
| 12 | Converts all strings to the same format |
| 13 | Make sure each network has an encapsulation key:value |
| 14 | Makes sure encapsulation type is all uppercase |
| 15 | Make sure an empty list of interfaces is formated consistently |
| 16 | Sorts the list of interfaces |
Jon Hall | 0dccfa5 | 2017-05-18 11:21:11 -0700 | [diff] [blame] | 17 | Sorts the list of networks |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 18 | """ |
| 19 | # Convert to same string formats |
| 20 | config = json.loads( json.dumps( config ) ) |
| 21 | for network in config: |
| 22 | encap = network.get( 'encapsulation', None ) |
| 23 | if encap is None: |
| 24 | encap = "NONE" |
| 25 | network[ 'encapsulation' ] = encap.upper() |
| 26 | ifaces = network.get( 'interfaces' ) |
Jon Hall | ed25823 | 2017-05-24 17:30:18 -0700 | [diff] [blame] | 27 | if ifaces == [ '' ]: |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 28 | ifaces = [] |
| 29 | else: |
| 30 | ifaces = sorted( ifaces ) |
Jon Hall | ed25823 | 2017-05-24 17:30:18 -0700 | [diff] [blame] | 31 | network[ 'interfaces' ] = ifaces |
| 32 | config = sorted( config, key=lambda k: k[ 'name' ] ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 33 | return config |
| 34 | |
Jon Hall | ed25823 | 2017-05-24 17:30:18 -0700 | [diff] [blame] | 35 | |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 36 | def verify( main ): |
| 37 | """ |
| 38 | Runs some tests to verify the vpls configurations. |
| 39 | - Compare sent vpls network configuration to what is stored in each: |
| 40 | - ONOS network configuration |
| 41 | - ONOS VPLS application configuration |
| 42 | - Ping between each pair of hosts to check connectivity |
| 43 | |
| 44 | NOTE: This requires the expected/sent network config json for the vpls |
| 45 | application be stored in main.vplsConfig |
| 46 | """ |
| 47 | # Variables |
Jon Hall | ed25823 | 2017-05-24 17:30:18 -0700 | [diff] [blame] | 48 | app = main.params[ 'vpls' ][ 'name' ] |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 49 | pprint = main.ONOSrest1.pprint |
Jon Hall | ed25823 | 2017-05-24 17:30:18 -0700 | [diff] [blame] | 50 | SLEEP = int( main.params[ 'SLEEP' ][ 'netcfg' ] ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 51 | |
| 52 | main.step( "Check network configurations for vpls application" ) |
| 53 | clusterResult = True |
| 54 | for node in main.RESTs: |
| 55 | result = False |
Jeremy Ronquillo | 4a30ffe | 2017-06-07 11:36:35 -0700 | [diff] [blame^] | 56 | getVPLS = utilities.retry( f=node.getNetCfg, |
| 57 | retValue=False, |
| 58 | kwargs={"subjectClass":"apps", "subjectKey":app}, |
| 59 | sleep=SLEEP ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 60 | onosCfg = json.loads( getVPLS ).get( 'vpls' ).get( 'vplsList' ) |
| 61 | onosCfg = pprint( sanitizeConfig( onosCfg ) ) |
| 62 | sentCfg = pprint( sanitizeConfig( main.vplsConfig ) ) |
| 63 | result = onosCfg == sentCfg |
| 64 | if result: |
| 65 | main.log.info( "ONOS NetCfg matches what was sent" ) |
| 66 | else: |
| 67 | clusterResult = False |
| 68 | main.log.error( "ONOS NetCfg doesn't match what was sent" ) |
| 69 | main.log.debug( "ONOS config: {}".format( onosCfg ) ) |
| 70 | main.log.debug( "Sent config: {}".format( sentCfg ) ) |
| 71 | utilities.assert_equals( expect=True, |
| 72 | actual=clusterResult, |
| 73 | onpass="Net Cfg added for vpls", |
| 74 | onfail="Net Cfg not added for vpls" ) |
| 75 | |
| 76 | main.step( "Check vpls app configurations" ) |
| 77 | clusterResult = True |
| 78 | for node in main.CLIs: |
| 79 | result = False |
| 80 | #TODO Read from vpls show and match to pushed json |
| 81 | vpls = node.parseVplsShow() |
| 82 | parsedVpls = pprint( sanitizeConfig( vpls ) ) |
| 83 | sentVpls = pprint( sanitizeConfig( main.vplsConfig ) ) |
| 84 | result = parsedVpls == sentVpls |
| 85 | if result: |
| 86 | main.log.info( "VPLS config matches sent NetCfg" ) |
| 87 | else: |
| 88 | clusterResult = False |
| 89 | main.log.error( "VPLS config doesn't match sent NetCfg" ) |
| 90 | main.log.debug( "ONOS config: {}".format( parsedVpls ) ) |
| 91 | main.log.debug( "Sent config: {}".format( sentVpls ) ) |
| 92 | utilities.assert_equals( expect=True, |
| 93 | actual=clusterResult, |
| 94 | onpass="VPLS successfully configured", |
| 95 | onfail="VPLS not configured correctly" ) |
| 96 | |
| 97 | # FIXME This doesn't work, some will be withdrawn if interfaces are removed |
| 98 | # TODO: if encapsulation is set, look for that |
| 99 | # TODO: can we look at the intent keys? |
| 100 | """ |
| 101 | main.step( "Check intent states" ) |
| 102 | # Print the intent states |
Jon Hall | ed25823 | 2017-05-24 17:30:18 -0700 | [diff] [blame] | 103 | intents = main.CLIs[ 0 ].intents() |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 104 | count = 0 |
| 105 | while count <= 5: |
| 106 | installedCheck = True |
| 107 | try: |
| 108 | for intent in json.loads( intents ): |
| 109 | state = intent.get( 'state', None ) |
| 110 | if "INSTALLED" not in state: |
| 111 | installedCheck = False |
| 112 | except ( ValueError, TypeError ): |
| 113 | main.log.exception( "Error parsing intents" ) |
| 114 | if installedCheck: |
| 115 | break |
| 116 | count += 1 |
| 117 | utilities.assert_equals( expect=True, |
| 118 | actual=installedCheck , |
| 119 | onpass="All Intents in installed state", |
| 120 | onfail="Not all Intents in installed state" ) |
| 121 | """ |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 122 | main.step( "Check connectivity" ) |
| 123 | connectivityCheck = True |
Jon Hall | ed25823 | 2017-05-24 17:30:18 -0700 | [diff] [blame] | 124 | hosts = int( main.params[ 'vpls' ][ 'hosts' ] ) |
Jon Hall | 2c8959e | 2016-12-16 12:17:34 -0800 | [diff] [blame] | 125 | networks = [] |
| 126 | for network in main.vplsConfig: |
| 127 | nodes = network.get( 'interfaces', None ) |
| 128 | if nodes: |
| 129 | networks.append( nodes ) |
| 130 | for i in range( 1, hosts + 1 ): |
| 131 | src = "h" + str( i ) |
| 132 | for j in range( 1, hosts + 1 ): |
| 133 | if j == i: |
| 134 | continue |
| 135 | dst = "h" + str( j ) |
| 136 | pingResult = main.Mininet1.pingHost( SRC=src, TARGET=dst ) |
| 137 | expected = main.FALSE |
| 138 | for network in networks: |
| 139 | if src in network and dst in network: |
| 140 | expected = main.TRUE |
| 141 | break |
| 142 | if pingResult != expected: |
| 143 | connectivityCheck = False |
| 144 | main.log.error( "%s <-> %s: %s; Expected: %s" % |
| 145 | ( src, dst, pingResult, expected ) ) |
| 146 | utilities.assert_equals( expect=True, |
| 147 | actual=connectivityCheck, |
| 148 | onpass="Connectivity is as expected", |
| 149 | onfail="Connectivity is not as expected" ) |