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