blob: e4f195b1e443fbf94c68ea6c9b46f33bd75b21cd [file] [log] [blame]
"""
Copyright 2016 Open Networking Foundation ( ONF )
Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>,
the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>,
or the System Testing Guide page at <https://wiki.onosproject.org/x/WYQg>
TestON is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 2 of the License, or
( at your option ) any later version.
TestON is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with TestON. If not, see <http://www.gnu.org/licenses/>.
"""
import time
import re
import imp
import json
from core import utilities
class Topology:
def __init__( self ):
self.default = ''
"""
These functions can be used for topology comparisons
"""
def getAll( self, function, needRetry=False, kwargs={}, inJson=False ):
"""
Description:
get all devices/links/hosts/ports of the onosCli
Required:
* function - name of the function
* needRetry - it will retry if this is true.
* kwargs - kwargs of the function
* inJson - True if want it in Json form
Returns:
Returns the list of the result.
"""
returnList = []
threads = []
for ctrl in main.Cluster.active():
func = getattr( ctrl.CLI, function )
t = main.Thread( target=utilities.retry if needRetry else func,
name=function + "-" + str( ctrl ),
args=[ func, [ None ] ] if needRetry else [],
kwargs=kwargs )
threads.append( t )
t.start()
for t in threads:
t.join()
if inJson:
try:
returnList.append( json.loads( t.result ) )
except ( ValueError, TypeError ):
main.log.exception( "Error parsing hosts results" )
main.log.error( repr( t.result ) )
returnList.append( None )
else:
returnList.append( t.result )
return returnList
def compareDevicePort( self, Mininet, controller, mnSwitches, devices, ports ):
"""
Description:
compares the devices and port of the onos to the mininet.
Required:
* Mininet - mininet driver to use
* controller - controller position of the devices
* mnSwitches - switches of mininet
* devices - devices of the onos
* ports - ports of the onos
Returns:
Returns main.TRUE if the results are matching else
Returns main.FALSE
"""
if devices[ controller ] and ports[ controller ] and \
"Error" not in devices[ controller ] and \
"Error" not in ports[ controller ]:
try:
currentDevicesResult = Mininet.compareSwitches(
mnSwitches,
json.loads( devices[ controller ] ),
json.loads( ports[ controller ] ) )
except ( TypeError, ValueError ):
main.log.error(
"Could not load json: {0} or {1}".format( str( devices[ controller ] ),
str( ports[ controller ] ) ) )
currentDevicesResult = main.FALSE
else:
currentDevicesResult = main.FALSE
return currentDevicesResult
def compareBase( self, compareElem, controller, compareF, compareArg ):
"""
Description:
compares the links/hosts of the onos to the mininet.
Required:
* compareElem - list of links/hosts of the onos
* controller - controller position of the devices
* compareF - function of the mininet that will compare the
results
* compareArg - arg of the compareF.
Returns:
Returns main.TRUE if the results are matching else
Returns main.FALSE
"""
if compareElem[ controller ] and "Error" not in compareElem[ controller ]:
try:
if isinstance( compareArg, list ):
compareArg.append( json.loads( compareElem[ controller ] ) )
else:
compareArg = [ compareArg, json.loads( compareElem[ controller ] ) ]
currentCompareResult = compareF( *compareArg )
except( TypeError, ValueError ):
main.log.error(
"Could not load json: {0} or {1}".format( str( compareElem[ controller ] ) ) )
currentCompareResult = main.FALSE
else:
currentCompareResult = main.FALSE
return currentCompareResult
def compareTopos( self, Mininet, attempts=1, includeCaseDesc=True ):
"""
Description:
compares the links and hosts and switches of the onos to the mininet.
Required:
* Mininet - Mininet driver to use.
* attempts - number of attempts to compare in case
the result is different after a certain time.
Returns:
Returns main.TRUE if the results are matching else
Returns main.FALSE
"""
if includeCaseDesc:
main.case( "Compare ONOS Topology view to Mininet topology" )
main.caseExplanation = "Compare topology elements between Mininet" +\
" and ONOS"
main.log.info( "Gathering topology information from Mininet" )
devicesResults = main.FALSE # Overall Boolean for device correctness
linksResults = main.FALSE # Overall Boolean for link correctness
hostsResults = main.FALSE # Overall Boolean for host correctness
deviceFails = [] # Nodes where devices are incorrect
linkFails = [] # Nodes where links are incorrect
hostFails = [] # Nodes where hosts are incorrect
mnSwitches = Mininet.getSwitches()
mnLinks = Mininet.getLinks()
mnHosts = Mininet.getHosts()
main.step( "Comparing Mininet topology to ONOS topology" )
while ( attempts >= 0 ) and\
( not devicesResults or not linksResults or not hostsResults ):
main.log.info( "Sleeping {} seconds".format( 2 ) )
time.sleep( 2 )
if not devicesResults:
devices = self.getAll( "devices", False )
ports = self.getAll( "ports", False )
devicesResults = main.TRUE
deviceFails = [] # Reset for each failed attempt
if not linksResults:
links = self.getAll( "links", False )
linksResults = main.TRUE
linkFails = [] # Reset for each failed attempt
if not hostsResults:
hosts = self.getAll( "hosts", False )
hostsResults = main.TRUE
hostFails = [] # Reset for each failed attempt
# Check for matching topology on each node
for controller in main.Cluster.getRunningPos():
controllerStr = str( controller + 1 ) # ONOS node number
# Compare Devices
currentDevicesResult = self.compareDevicePort( Mininet, controller,
mnSwitches,
devices, ports )
if not currentDevicesResult:
deviceFails.append( controllerStr )
devicesResults = devicesResults and currentDevicesResult
# Compare Links
currentLinksResult = self.compareBase( links, controller,
Mininet.compareLinks,
[ mnSwitches, mnLinks ] )
if not currentLinksResult:
linkFails.append( controllerStr )
linksResults = linksResults and currentLinksResult
# Compare Hosts
currentHostsResult = self.compareBase( hosts, controller,
Mininet.compareHosts,
mnHosts )
if not currentHostsResult:
hostFails.append( controllerStr )
hostsResults = hostsResults and currentHostsResult
# Decrement Attempts Remaining
attempts -= 1
utilities.assert_equals( expect=[],
actual=deviceFails,
onpass="ONOS correctly discovered all devices",
onfail="ONOS incorrectly discovered devices on nodes: " +
str( deviceFails ) )
utilities.assert_equals( expect=[],
actual=linkFails,
onpass="ONOS correctly discovered all links",
onfail="ONOS incorrectly discovered links on nodes: " +
str( linkFails ) )
utilities.assert_equals( expect=[],
actual=hostFails,
onpass="ONOS correctly discovered all hosts",
onfail="ONOS incorrectly discovered hosts on nodes: " +
str( hostFails ) )
topoResults = hostsResults and linksResults and devicesResults
utilities.assert_equals( expect=main.TRUE,
actual=topoResults,
onpass="ONOS correctly discovered the topology",
onfail="ONOS incorrectly discovered the topology" )
return topoResults
def ping( self, srcList, dstList, ipv6=False, expect=True, wait=1, acceptableFailed=0, collectT3=True ):
"""
Description:
Ping from every host in srcList to every host in dstList and
verify if ping results are as expected.
Pings are executed in parallel from host components
Options:
src: a list of source host names, e.g. [ "h1", "h2" ]
dst: a list of destination host names, e.g. [ "h3", "h4" ]
expect: expect ping result to pass if True, otherwise fail
acceptableFailed: maximum number of failed pings acceptable for
each src-dst host pair
collectT3: save t3-troubleshoot output for src and dst host that failed to ping
Returns:
main.TRUE if all ping results are expected, otherwise main.FALSE
"""
main.log.info( "Pinging from {} to {}, expected result is {}".format( srcList, dstList,
"pass" if expect else "fail" ) )
# Verify host component has been created
srcIpList = {}
for src in srcList:
if not hasattr( main, src ):
main.log.info( "Creating component for host {}".format( src ) )
main.Network.createHostComponent( src )
hostHandle = getattr( main, src )
main.log.info( "Starting CLI on host {}".format( src ) )
hostHandle.startHostCli()
srcIpList[ src ] = main.Network.getIPAddress( src, proto='IPV6' if ipv6 else 'IPV4' )
unexpectedPings = []
for dst in dstList:
dstIp = main.Network.getIPAddress( dst, proto='IPV6' if ipv6 else 'IPV4' )
# Start pings from src hosts in parallel
pool = []
for src in srcList:
srcIp = srcIpList[ src ]
if srcIp == dstIp:
continue
if expect and ( not srcIp or not dstIp ):
unexpectedPings.append( [ src, dst, "no IP" ] )
continue
hostHandle = getattr( main, src )
thread = main.Thread( target=utilities.retry,
name="{}-{}".format( srcIp, dstIp ),
args=[ hostHandle.pingHostSetAlternative, [ main.FALSE ] ],
kwargs={ "args": ( [ dstIp ], wait, ipv6 ),
"attempts": acceptableFailed + 1,
"sleep": 1 } )
pool.append( thread )
thread.start()
# Wait for threads to finish and check ping result
for thread in pool:
thread.join( 10 )
srcIp, dstIp = thread.name.split( "-" )
if expect and not thread.result or not expect and thread.result:
unexpectedPings.append( [ srcIp, dstIp, "fail" if expect else "pass" ] )
utilities.assert_equals( expect=[],
actual=unexpectedPings,
onpass="Pings from {} to {} all as expected".format( srcList, dstList ),
onfail="Unexpected pings: {}".format( unexpectedPings ) )
if collectT3:
for unexpectedPing in unexpectedPings:
if unexpectedPing[ 2 ] == "no IP":
continue
srcIp = unexpectedPing[ 0 ]
dstIp = unexpectedPing[ 1 ]
main.log.debug( "Collecting t3 with source {} and destination {}".format( srcIp, dstIp ) )
cmd = main.Cluster.active( 0 ).CLI.composeT3Command( srcIp, dstIp, ipv6 )
main.log.debug( "t3 command: {}".format( cmd ) )
if cmd:
main.ONOSbench.dumpONOSCmd( main.Cluster.active( 0 ).ipAddress, cmd, main.logdir,
"t3-CASE{}-{}-{}-".format( main.CurrentTestCaseNumber, srcIp, dstIp ) )
return main.FALSE if unexpectedPings else main.TRUE
def sendScapyPackets( self, sender, receiver, pktFilter, pkt, sIface=None, dIface=None, expect=True, acceptableFailed=0, collectT3=True, t3Command="" ):
"""
Description:
Call sendScapyPacket and retry if neccessary
If collectT3 is True and t3Command is specified, collect t3-troubleshoot output on unexpected scapy results
Buiid packets on the sender side by calling functions in Scapy CLI driver
Options:
sender: the component of the host that is sending packets
receiver: the component of the host that is receiving packets
pktFilter: packet filter used by receiver
pkt: keyword that is expected to be conrained in the received packets
expect: expect receiver to receive the packet if True, otherwise not receiving the packet
acceptableFailed: maximum number of unexpected scapy results acceptable
collectT3: save t3-troubleshoot output for unexpected scapy results
Returns:
main.TRUE if scapy result is expected, otherwise main.FALSE
"""
main.log.info( "Sending scapy packets from {} to {}, expected result is {}".format( sender.name, receiver.name,
"pass" if expect else "fail" ) )
scapyResult = utilities.retry( self.sendScapyPacket,
main.FALSE,
args=( sender, receiver, pktFilter, pkt,
sIface, dIface, expect ),
attempts=acceptableFailed + 1,
sleep=1 )
if not scapyResult and collectT3 and t3Command:
main.log.debug( "Collecting t3 with source {} and destination {}".format( sender.name, receiver.name ) )
main.log.debug( "t3 command: {}".format( t3Command ) )
main.ONOSbench.dumpONOSCmd( main.Cluster.active( 0 ).ipAddress, t3Command, main.logdir,
"t3-CASE{}-{}-{}-".format( main.CurrentTestCaseNumber, sender.name, receiver.name ) )
return scapyResult
def sendScapyPacket( self, sender, receiver, pktFilter, pkt, sIface=None, dIface=None, expect=True ):
"""
Description:
Send Scapy packets from sender to receiver and verify if result is as expected
Options:
sender: the component of the host that is sending packets
receiver: the component of the host that is receiving packets
pktFilter: packet filter used by receiver
pkt: keyword that is expected to be conrained in the received packets
expect: expect receiver to receive the packet if True, otherwise not receiving the packet
Returns:
main.TRUE if scapy result is expected, otherwise main.FALSE
"""
receiver.startFilter( ifaceName=dIface, pktFilter=pktFilter )
sender.sendPacket( iface=sIface )
finished = receiver.checkFilter()
packet = ""
if finished:
packets = receiver.readPackets()
for packet in packets.splitlines():
main.log.debug( packet )
else:
kill = receiver.killFilter()
main.log.debug( kill )
sender.handle.sendline( "" )
sender.handle.expect( sender.scapyPrompt )
main.log.debug( sender.handle.before )
packetCaptured = True if pkt in packet else False
return main.TRUE if packetCaptured == expect else main.FALSE