blob: 4d3bafbdd3f43e0516efe524aaf489a8e1b84791 [file] [log] [blame]
Devin Lim142b5342017-07-20 15:22:39 -07001"""
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07002Copyright 2016 Open Networking Foundation ( ONF )
Devin Lim142b5342017-07-20 15:22:39 -07003
4Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>,
5the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>,
6or 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 Ronquillo23fb2162017-09-15 14:59:57 -070011 ( at your option ) any later version.
Devin Lim142b5342017-07-20 15:22:39 -070012
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/>.
20"""
Devin Lim58046fa2017-07-05 16:55:00 -070021import time
22import re
23import imp
24import json
You Wang5da39c82018-04-26 22:55:08 -070025from core import utilities
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -070026
27
Devin Lim58046fa2017-07-05 16:55:00 -070028class Topology:
29
30 def __init__( self ):
31 self.default = ''
Devin Lim142b5342017-07-20 15:22:39 -070032
Devin Lim58046fa2017-07-05 16:55:00 -070033 """
34 These functions can be used for topology comparisons
35 """
Devin Lim142b5342017-07-20 15:22:39 -070036 def getAll( self, function, needRetry=False, kwargs={}, inJson=False ):
Devin Lim58046fa2017-07-05 16:55:00 -070037 """
Devin Lim142b5342017-07-20 15:22:39 -070038 Description:
39 get all devices/links/hosts/ports of the onosCli
40 Required:
41 * function - name of the function
42 * needRetry - it will retry if this is true.
43 * kwargs - kwargs of the function
44 * inJson - True if want it in Json form
45 Returns:
46 Returns the list of the result.
Devin Lim58046fa2017-07-05 16:55:00 -070047 """
Devin Lim142b5342017-07-20 15:22:39 -070048 returnList = []
Devin Lim58046fa2017-07-05 16:55:00 -070049 threads = []
Jon Hallca319892017-06-15 15:25:22 -070050 for ctrl in main.Cluster.active():
Devin Lim142b5342017-07-20 15:22:39 -070051 func = getattr( ctrl.CLI, function )
52 t = main.Thread( target=utilities.retry if needRetry else func,
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -070053 name=function + "-" + str( ctrl ),
Devin Lim142b5342017-07-20 15:22:39 -070054 args=[ func, [ None ] ] if needRetry else [],
Devin Lim58046fa2017-07-05 16:55:00 -070055 kwargs=kwargs )
56 threads.append( t )
57 t.start()
58
59 for t in threads:
60 t.join()
61 if inJson:
62 try:
Devin Lim142b5342017-07-20 15:22:39 -070063 returnList.append( json.loads( t.result ) )
Devin Lim58046fa2017-07-05 16:55:00 -070064 except ( ValueError, TypeError ):
65 main.log.exception( "Error parsing hosts results" )
66 main.log.error( repr( t.result ) )
Devin Lim142b5342017-07-20 15:22:39 -070067 returnList.append( None )
Devin Lim58046fa2017-07-05 16:55:00 -070068 else:
Devin Lim142b5342017-07-20 15:22:39 -070069 returnList.append( t.result )
70 return returnList
Devin Lim58046fa2017-07-05 16:55:00 -070071
72 def compareDevicePort( self, Mininet, controller, mnSwitches, devices, ports ):
Devin Lim142b5342017-07-20 15:22:39 -070073 """
74 Description:
75 compares the devices and port of the onos to the mininet.
76 Required:
77 * Mininet - mininet driver to use
78 * controller - controller position of the devices
79 * mnSwitches - switches of mininet
80 * devices - devices of the onos
81 * ports - ports of the onos
82 Returns:
83 Returns main.TRUE if the results are matching else
84 Returns main.FALSE
85 """
Devin Lim58046fa2017-07-05 16:55:00 -070086 if devices[ controller ] and ports[ controller ] and \
87 "Error" not in devices[ controller ] and \
88 "Error" not in ports[ controller ]:
89 try:
90 currentDevicesResult = Mininet.compareSwitches(
91 mnSwitches,
92 json.loads( devices[ controller ] ),
93 json.loads( ports[ controller ] ) )
Jon Hallca319892017-06-15 15:25:22 -070094 except ( TypeError, ValueError ):
Devin Lim58046fa2017-07-05 16:55:00 -070095 main.log.error(
Jon Hallca319892017-06-15 15:25:22 -070096 "Could not load json: {0} or {1}".format( str( devices[ controller ] ),
97 str( ports[ controller ] ) ) )
Devin Lim58046fa2017-07-05 16:55:00 -070098 currentDevicesResult = main.FALSE
99 else:
100 currentDevicesResult = main.FALSE
101 return currentDevicesResult
102
103 def compareBase( self, compareElem, controller, compareF, compareArg ):
Devin Lim142b5342017-07-20 15:22:39 -0700104 """
105 Description:
106 compares the links/hosts of the onos to the mininet.
107 Required:
108 * compareElem - list of links/hosts of the onos
109 * controller - controller position of the devices
110 * compareF - function of the mininet that will compare the
111 results
112 * compareArg - arg of the compareF.
113 Returns:
114 Returns main.TRUE if the results are matching else
115 Returns main.FALSE
116 """
Devin Lim58046fa2017-07-05 16:55:00 -0700117 if compareElem[ controller ] and "Error" not in compareElem[ controller ]:
118 try:
119 if isinstance( compareArg, list ):
120 compareArg.append( json.loads( compareElem[ controller ] ) )
121 else:
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700122 compareArg = [ compareArg, json.loads( compareElem[ controller ] ) ]
Devin Lim58046fa2017-07-05 16:55:00 -0700123
124 currentCompareResult = compareF( *compareArg )
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700125 except( TypeError, ValueError ):
Devin Lim58046fa2017-07-05 16:55:00 -0700126 main.log.error(
127 "Could not load json: {0} or {1}".format( str( compareElem[ controller ] ) ) )
128 currentCompareResult = main.FALSE
129 else:
130 currentCompareResult = main.FALSE
131
132 return currentCompareResult
133
Devin Lim0c972b72018-02-08 14:53:59 -0800134 def compareTopos( self, Mininet, attempts=1, includeCaseDesc=True ):
Devin Lim142b5342017-07-20 15:22:39 -0700135 """
136 Description:
137 compares the links and hosts and switches of the onos to the mininet.
138 Required:
139 * Mininet - Mininet driver to use.
140 * attempts - number of attempts to compare in case
141 the result is different after a certain time.
142 Returns:
143 Returns main.TRUE if the results are matching else
144 Returns main.FALSE
145 """
Devin Lim0c972b72018-02-08 14:53:59 -0800146 if includeCaseDesc:
147 main.case( "Compare ONOS Topology view to Mininet topology" )
148 main.caseExplanation = "Compare topology elements between Mininet" +\
149 " and ONOS"
Devin Lim58046fa2017-07-05 16:55:00 -0700150 main.log.info( "Gathering topology information from Mininet" )
151 devicesResults = main.FALSE # Overall Boolean for device correctness
152 linksResults = main.FALSE # Overall Boolean for link correctness
153 hostsResults = main.FALSE # Overall Boolean for host correctness
154 deviceFails = [] # Nodes where devices are incorrect
155 linkFails = [] # Nodes where links are incorrect
156 hostFails = [] # Nodes where hosts are incorrect
157
158 mnSwitches = Mininet.getSwitches()
159 mnLinks = Mininet.getLinks()
160 mnHosts = Mininet.getHosts()
161
162 main.step( "Comparing Mininet topology to ONOS topology" )
163
164 while ( attempts >= 0 ) and\
165 ( not devicesResults or not linksResults or not hostsResults ):
166 main.log.info( "Sleeping {} seconds".format( 2 ) )
167 time.sleep( 2 )
168 if not devicesResults:
Devin Lim142b5342017-07-20 15:22:39 -0700169 devices = self.getAll( "devices", False )
170 ports = self.getAll( "ports", False )
Devin Lim58046fa2017-07-05 16:55:00 -0700171 devicesResults = main.TRUE
172 deviceFails = [] # Reset for each failed attempt
173 if not linksResults:
Devin Lim142b5342017-07-20 15:22:39 -0700174 links = self.getAll( "links", False )
Devin Lim58046fa2017-07-05 16:55:00 -0700175 linksResults = main.TRUE
176 linkFails = [] # Reset for each failed attempt
177 if not hostsResults:
Devin Lim142b5342017-07-20 15:22:39 -0700178 hosts = self.getAll( "hosts", False )
Devin Lim58046fa2017-07-05 16:55:00 -0700179 hostsResults = main.TRUE
180 hostFails = [] # Reset for each failed attempt
181
182 # Check for matching topology on each node
Devin Lim142b5342017-07-20 15:22:39 -0700183 for controller in main.Cluster.getRunningPos():
Devin Lim58046fa2017-07-05 16:55:00 -0700184 controllerStr = str( controller + 1 ) # ONOS node number
185 # Compare Devices
186 currentDevicesResult = self.compareDevicePort( Mininet, controller,
Jon Hallca319892017-06-15 15:25:22 -0700187 mnSwitches,
188 devices, ports )
Devin Lim58046fa2017-07-05 16:55:00 -0700189 if not currentDevicesResult:
190 deviceFails.append( controllerStr )
191 devicesResults = devicesResults and currentDevicesResult
192 # Compare Links
193 currentLinksResult = self.compareBase( links, controller,
Jon Hallca319892017-06-15 15:25:22 -0700194 Mininet.compareLinks,
195 [ mnSwitches, mnLinks ] )
Devin Lim58046fa2017-07-05 16:55:00 -0700196 if not currentLinksResult:
197 linkFails.append( controllerStr )
198 linksResults = linksResults and currentLinksResult
199 # Compare Hosts
200 currentHostsResult = self.compareBase( hosts, controller,
201 Mininet.compareHosts,
202 mnHosts )
203 if not currentHostsResult:
204 hostFails.append( controllerStr )
205 hostsResults = hostsResults and currentHostsResult
206 # Decrement Attempts Remaining
207 attempts -= 1
208
209 utilities.assert_equals( expect=[],
210 actual=deviceFails,
211 onpass="ONOS correctly discovered all devices",
212 onfail="ONOS incorrectly discovered devices on nodes: " +
213 str( deviceFails ) )
214 utilities.assert_equals( expect=[],
215 actual=linkFails,
216 onpass="ONOS correctly discovered all links",
217 onfail="ONOS incorrectly discovered links on nodes: " +
218 str( linkFails ) )
219 utilities.assert_equals( expect=[],
220 actual=hostFails,
221 onpass="ONOS correctly discovered all hosts",
222 onfail="ONOS incorrectly discovered hosts on nodes: " +
223 str( hostFails ) )
224 topoResults = hostsResults and linksResults and devicesResults
225 utilities.assert_equals( expect=main.TRUE,
226 actual=topoResults,
227 onpass="ONOS correctly discovered the topology",
228 onfail="ONOS incorrectly discovered the topology" )
Devin Lim142b5342017-07-20 15:22:39 -0700229 return topoResults
You Wang5da39c82018-04-26 22:55:08 -0700230
231 def ping( self, srcList, dstList, ipv6=False, expect=True, wait=1, acceptableFailed=0, collectT3=True ):
232 """
233 Description:
234 Ping from every host in srcList to every host in dstList and
235 verify if ping results are as expected.
236 Pings are executed in parallel from host components
237 Options:
238 src: a list of source host names, e.g. [ "h1", "h2" ]
239 dst: a list of destination host names, e.g. [ "h3", "h4" ]
240 expect: expect ping result to pass if True, otherwise fail
241 acceptableFailed: maximum number of failed pings acceptable for
242 each src-dst host pair
243 collectT3: save t3-troubleshoot output for src and dst host that failed to ping
244 Returns:
245 main.TRUE if all ping results are expected, otherwise main.FALSE
246 """
247 main.log.info( "Pinging from {} to {}, expected result is {}".format( srcList, dstList,
248 "pass" if expect else "fail" ) )
249 # Verify host component has been created
250 srcIpList = {}
251 for src in srcList:
252 if not hasattr( main, src ):
253 main.log.info( "Creating component for host {}".format( src ) )
254 main.Network.createHostComponent( src )
255 hostHandle = getattr( main, src )
256 main.log.info( "Starting CLI on host {}".format( src ) )
257 hostHandle.startHostCli()
258 srcIpList[ src ] = main.Network.getIPAddress( src, proto='IPV6' if ipv6 else 'IPV4' )
You Wang5da39c82018-04-26 22:55:08 -0700259 unexpectedPings = []
260 for dst in dstList:
261 dstIp = main.Network.getIPAddress( dst, proto='IPV6' if ipv6 else 'IPV4' )
262 # Start pings from src hosts in parallel
263 pool = []
264 for src in srcList:
265 srcIp = srcIpList[ src ]
266 if srcIp == dstIp:
267 continue
268 if expect and ( not srcIp or not dstIp ):
269 unexpectedPings.append( [ src, dst, "no IP" ] )
270 continue
271 hostHandle = getattr( main, src )
272 thread = main.Thread( target=utilities.retry,
273 name="{}-{}".format( srcIp, dstIp ),
274 args=[ hostHandle.pingHostSetAlternative, [ main.FALSE ] ],
You Wangc564c6f2018-05-01 15:24:57 -0700275 kwargs={ "args": ( [ dstIp ], wait, ipv6 ),
You Wang5da39c82018-04-26 22:55:08 -0700276 "attempts": acceptableFailed + 1,
277 "sleep": 1 } )
278 pool.append( thread )
279 thread.start()
280 # Wait for threads to finish and check ping result
281 for thread in pool:
282 thread.join( 10 )
283 srcIp, dstIp = thread.name.split( "-" )
284 if expect and not thread.result or not expect and thread.result:
285 unexpectedPings.append( [ srcIp, dstIp, "fail" if expect else "pass" ] )
You Wang85747762018-05-11 15:51:50 -0700286 main.log.info( "Unexpected pings: {}".format( unexpectedPings ) )
You Wang5da39c82018-04-26 22:55:08 -0700287 if collectT3:
288 for unexpectedPing in unexpectedPings:
289 if unexpectedPing[ 2 ] == "no IP":
290 continue
291 srcIp = unexpectedPing[ 0 ]
292 dstIp = unexpectedPing[ 1 ]
293 main.log.debug( "Collecting t3 with source {} and destination {}".format( srcIp, dstIp ) )
294 cmd = main.Cluster.active( 0 ).CLI.composeT3Command( srcIp, dstIp, ipv6 )
295 main.log.debug( "t3 command: {}".format( cmd ) )
296 if cmd:
297 main.ONOSbench.dumpONOSCmd( main.Cluster.active( 0 ).ipAddress, cmd, main.logdir,
298 "t3-CASE{}-{}-{}-".format( main.CurrentTestCaseNumber, srcIp, dstIp ) )
299 return main.FALSE if unexpectedPings else main.TRUE
You Wangc564c6f2018-05-01 15:24:57 -0700300
301 def sendScapyPackets( self, sender, receiver, pktFilter, pkt, sIface=None, dIface=None, expect=True, acceptableFailed=0, collectT3=True, t3Command="" ):
302 """
303 Description:
304 Call sendScapyPacket and retry if neccessary
305 If collectT3 is True and t3Command is specified, collect t3-troubleshoot output on unexpected scapy results
306 Buiid packets on the sender side by calling functions in Scapy CLI driver
307 Options:
308 sender: the component of the host that is sending packets
309 receiver: the component of the host that is receiving packets
310 pktFilter: packet filter used by receiver
311 pkt: keyword that is expected to be conrained in the received packets
312 expect: expect receiver to receive the packet if True, otherwise not receiving the packet
313 acceptableFailed: maximum number of unexpected scapy results acceptable
314 collectT3: save t3-troubleshoot output for unexpected scapy results
315 Returns:
316 main.TRUE if scapy result is expected, otherwise main.FALSE
317 """
318 main.log.info( "Sending scapy packets from {} to {}, expected result is {}".format( sender.name, receiver.name,
319 "pass" if expect else "fail" ) )
320 scapyResult = utilities.retry( self.sendScapyPacket,
321 main.FALSE,
322 args=( sender, receiver, pktFilter, pkt,
323 sIface, dIface, expect ),
324 attempts=acceptableFailed + 1,
325 sleep=1 )
326 if not scapyResult and collectT3 and t3Command:
327 main.log.debug( "Collecting t3 with source {} and destination {}".format( sender.name, receiver.name ) )
328 main.log.debug( "t3 command: {}".format( t3Command ) )
329 main.ONOSbench.dumpONOSCmd( main.Cluster.active( 0 ).ipAddress, t3Command, main.logdir,
330 "t3-CASE{}-{}-{}-".format( main.CurrentTestCaseNumber, sender.name, receiver.name ) )
331 return scapyResult
332
333 def sendScapyPacket( self, sender, receiver, pktFilter, pkt, sIface=None, dIface=None, expect=True ):
334 """
335 Description:
336 Send Scapy packets from sender to receiver and verify if result is as expected
337 Options:
338 sender: the component of the host that is sending packets
339 receiver: the component of the host that is receiving packets
340 pktFilter: packet filter used by receiver
341 pkt: keyword that is expected to be conrained in the received packets
342 expect: expect receiver to receive the packet if True, otherwise not receiving the packet
343 Returns:
344 main.TRUE if scapy result is expected, otherwise main.FALSE
345 """
346 receiver.startFilter( ifaceName=dIface, pktFilter=pktFilter )
347 sender.sendPacket( iface=sIface )
348 finished = receiver.checkFilter()
349 packet = ""
350 if finished:
351 packets = receiver.readPackets()
352 for packet in packets.splitlines():
353 main.log.debug( packet )
354 else:
355 kill = receiver.killFilter()
356 main.log.debug( kill )
357 sender.handle.sendline( "" )
358 sender.handle.expect( sender.scapyPrompt )
359 main.log.debug( sender.handle.before )
360 packetCaptured = True if pkt in packet else False
361 return main.TRUE if packetCaptured == expect else main.FALSE