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