blob: 2014477c50105e2741d27105f952246f911b5430 [file] [log] [blame]
adminbae64d82013-08-01 10:50:15 -07001#!/usr/bin/env python
2'''
3Created on 26-Oct-2012
4
5@author: Anil Kumar (anilkumar.s@paxterrasolutions.com)
6
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
11 (at your option) any later version.
12
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
21
22MininetCliDriver is the basic driver which will handle the Mininet functions
23'''
24
25import pexpect
26import struct
27import fcntl
28import os
29import signal
30import re
31import sys
32import core.teston
33sys.path.append("../")
34from drivers.common.cli.emulatordriver import Emulator
35from drivers.common.clidriver import CLI
36
37class MininetCliDriver(Emulator):
38 '''
Jon Hall41f40e82014-04-08 16:43:17 -070039 MininetCliDriver is the basic driver which will handle the Mininet functions
adminbae64d82013-08-01 10:50:15 -070040 '''
41 def __init__(self):
42 super(Emulator, self).__init__()
43 self.handle = self
44 self.wrapped = sys.modules[__name__]
45 self.flag = 0
46
47 def connect(self, **connectargs):
Jon Hall41f40e82014-04-08 16:43:17 -070048 '''
49 Here the main is the TestON instance after creating all the log handles.
50 '''
adminbae64d82013-08-01 10:50:15 -070051 for key in connectargs:
52 vars(self)[key] = connectargs[key]
53
54 self.name = self.options['name']
55 self.handle = super(MininetCliDriver, self).connect(user_name = self.user_name, ip_address = self.ip_address,port = None, pwd = self.pwd)
56
57 self.ssh_handle = self.handle
58
adminbae64d82013-08-01 10:50:15 -070059 if self.handle :
adminbae64d82013-08-01 10:50:15 -070060 main.log.info("Clearing any residual state or processes")
61 self.handle.sendline("sudo mn -c")
62
adminf939f8b2014-04-03 17:22:56 -070063 i=self.handle.expect(['password\sfor\s','Cleanup\scomplete',pexpect.EOF,pexpect.TIMEOUT],120)
adminbae64d82013-08-01 10:50:15 -070064 if i==0:
65 main.log.info("sending sudo password")
adminf939f8b2014-04-03 17:22:56 -070066 self.handle.sendline(self.pwd)
67 i=self.handle.expect(['%s:'%(self.user),'\$',pexpect.EOF,pexpect.TIMEOUT],120)
adminbae64d82013-08-01 10:50:15 -070068 if i==1:
69 main.log.info("Clean")
70
71 elif i==2:
72 main.log.error("Connection timeout")
73 elif i==3: #timeout
74 main.log.error("Something while cleaning MN took too long... " )
75
76 #cmdString = "sudo mn --topo "+self.options['topo']+","+self.options['topocount']+" --mac --switch "+self.options['switch']+" --controller "+self.options['controller']
77 #cmdString = "sudo mn --custom ~/mininet/custom/topo-2sw-2host.py --controller remote --ip 192.168.56.102 --port 6633 --topo mytopo"
78 main.log.info("building fresh mininet")
adminbeea0032014-01-23 14:54:13 -080079 #### for reactive/PARP enabled tests
admin07529932013-11-22 14:58:28 -080080 cmdString = "sudo mn " + self.options['arg1'] + " " + self.options['arg2'] + " --mac --controller " + self.options['controller']
adminbeea0032014-01-23 14:54:13 -080081 #### for proactive flow with static ARP entries
82 #cmdString = "sudo mn " + self.options['arg1'] + " " + self.options['arg2'] + " --mac --arp --controller " + self.options['controller']
adminbae64d82013-08-01 10:50:15 -070083 #resultCommand = self.execute(cmd=cmdString,prompt='mininet>',timeout=120)
84 self.handle.sendline(cmdString)
85 self.handle.expect("sudo mn")
86 while 1:
87 i=self.handle.expect(['mininet>','\*\*\*','Exception',pexpect.EOF,pexpect.TIMEOUT],300)
88 if i==0:
89 main.log.info("mininet built")
90 return main.TRUE
91 if i==1:
92 self.handle.expect("\n")
93 main.log.info(self.handle.before)
94 elif i==2:
95 main.log.error("Launching mininet failed...")
96 return main.FALSE
97 elif i==3:
98 main.log.error("Connection timeout")
99 return main.FALSE
100 elif i==4: #timeout
101 main.log.error("Something took too long... " )
102 return main.FALSE
103
104 #if utilities.assert_matches(expect=patterns,actual=resultCommand,onpass="Network is being launched",onfail="Network launching is being failed "):
105 return main.TRUE
106 #else:
107 # return main.FALSE
108
109 else :
110 main.log.error("Connection failed to the host "+self.user_name+"@"+self.ip_address)
111 main.log.error("Failed to connect to the Mininet")
112 return main.FALSE
113
114 def pingall(self):
115 '''
Jon Hall41f40e82014-04-08 16:43:17 -0700116 Verifies the reachability of the hosts using pingall command.
adminbae64d82013-08-01 10:50:15 -0700117 '''
118 if self.handle :
119 main.log.info("Checking reachabilty to the hosts using pingall")
120 response = self.execute(cmd="pingall",prompt="mininet>",timeout=10)
121 pattern = 'Results\:\s0\%\sdropped\s\(0\/\d+\slost\)\s*$'
122 if utilities.assert_matches(expect=pattern,actual=response,onpass="All hosts are reaching",onfail="Unable to reach all the hosts"):
123 return main.TRUE
124 else:
125 return main.FALSE
126 else :
127 main.log.error("Connection failed to the host")
128 return main.FALSE
adminaeedddd2013-08-02 15:14:15 -0700129
130 def fpingHost(self,**pingParams):
131 '''
132 Uses the fping package for faster pinging...
133 *requires fping to be installed on machine running mininet
134 '''
135 args = utilities.parse_args(["SRC","TARGET"],**pingParams)
admin530b4c92013-08-14 16:54:35 -0700136 command = args["SRC"] + " fping -i 100 -t 20 -C 1 -q "+args["TARGET"]
adminaeedddd2013-08-02 15:14:15 -0700137 self.handle.sendline(command)
138 self.handle.expect(args["TARGET"])
139 self.handle.expect("mininet")
140 response = self.handle.before
141 if re.search(":\s-" ,response):
142 main.log.info("Ping fail")
143 return main.FALSE
admin530b4c92013-08-14 16:54:35 -0700144 elif re.search(":\s\d{1,2}\.\d\d", response):
adminaeedddd2013-08-02 15:14:15 -0700145 main.log.info("Ping good!")
146 return main.TRUE
147 main.log.info("Install fping on mininet machine... ")
148 main.log.info("\n---\n"+response)
149 return main.FALSE
adminbae64d82013-08-01 10:50:15 -0700150
151 def pingHost(self,**pingParams):
152
153 args = utilities.parse_args(["SRC","TARGET"],**pingParams)
154 #command = args["SRC"] + " ping -" + args["CONTROLLER"] + " " +args ["TARGET"]
Jon Hallf89c8552014-04-02 13:14:06 -0700155 command = args["SRC"] + " ping "+args ["TARGET"]+" -c 1 -i 1"
adminbae64d82013-08-01 10:50:15 -0700156 response = self.execute(cmd=command,prompt="mininet",timeout=10 )
Jon Hallf89c8552014-04-02 13:14:06 -0700157 main.log.info("Ping Response: "+ response )
adminbae64d82013-08-01 10:50:15 -0700158 if utilities.assert_matches(expect=',\s0\%\spacket\sloss',actual=response,onpass="No Packet loss",onfail="Host is not reachable"):
159 main.log.info("NO PACKET LOSS, HOST IS REACHABLE")
160 main.last_result = main.TRUE
161 return main.TRUE
162 else :
163 main.log.error("PACKET LOST, HOST IS NOT REACHABLE")
164 main.last_result = main.FALSE
165 return main.FALSE
adminbae64d82013-08-01 10:50:15 -0700166
167 def checkIP(self,host):
168 '''
Jon Hall41f40e82014-04-08 16:43:17 -0700169 Verifies the host's ip configured or not.
adminbae64d82013-08-01 10:50:15 -0700170 '''
171 if self.handle :
172 response = self.execute(cmd=host+" ifconfig",prompt="mininet>",timeout=10)
173
174 pattern = "inet\s(addr|Mask):([0-1]{1}[0-9]{1,2}|2[0-4][0-9]|25[0-5]|[0-9]{1,2}).([0-1]{1}[0-9]{1,2}|2[0-4][0-9]|25[0-5]|[0-9]{1,2}).([0-1]{1}[0-9]{1,2}|2[0-4][0-9]|25[0-5]|[0-9]{1,2}).([0-1]{1}[0-9]{1,2}|2[0-4][0-9]|25[0-5]|[0-9]{1,2})"
175 #pattern = "inet\saddr:10.0.0.6"
176 if utilities.assert_matches(expect=pattern,actual=response,onpass="Host Ip configured properly",onfail="Host IP not found") :
177 return main.TRUE
178 else:
179 return main.FALSE
180 else :
181 main.log.error("Connection failed to the host")
182
183 def verifySSH(self,**connectargs):
184 response = self.execute(cmd="h1 /usr/sbin/sshd -D&",prompt="mininet>",timeout=10)
185 response = self.execute(cmd="h4 /usr/sbin/sshd -D&",prompt="mininet>",timeout=10)
186 for key in connectargs:
187 vars(self)[key] = connectargs[key]
188 response = self.execute(cmd="xterm h1 h4 ",prompt="mininet>",timeout=10)
189 import time
190 time.sleep(20)
191 if self.flag == 0:
192 self.flag = 1
193 return main.FALSE
194 else :
195 return main.TRUE
196
197 def getMacAddress(self,host):
198 '''
Jon Hall41f40e82014-04-08 16:43:17 -0700199 Verifies the host's ip configured or not.
adminbae64d82013-08-01 10:50:15 -0700200 '''
201 if self.handle :
202 response = self.execute(cmd=host+" ifconfig",prompt="mininet>",timeout=10)
203
Ahmed El-Hassanyf720e202014-04-04 16:11:36 -0700204 pattern = r'HWaddr\s([0-9A-F]{2}[:-]){5}([0-9A-F]{2})'
205 mac_address_search = re.search(pattern, response, re.I)
206 mac_address = mac_address_search.group().split(" ")[1]
207 main.log.info("Mac-Address of Host "+ host + " is " + mac_address)
208 return mac_address
adminbae64d82013-08-01 10:50:15 -0700209 else :
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700210 main.log.error("Connection failed to the host")
211
212 def getInterfaceMACAddress(self,host, interface):
213 '''
214 Return the IP address of the interface on the given host
215 '''
216 if self.handle :
217 response = self.execute(cmd=host+" ifconfig " + interface,
218 prompt="mininet>",timeout=10)
219
220 pattern = r'HWaddr\s([0-9A-F]{2}[:-]){5}([0-9A-F]{2})'
221 mac_address_search = re.search(pattern, response, re.I)
222 if mac_address_search is None:
223 main.log.info("No mac address found in %s" % response)
224 return main.FALSE
225 mac_address = mac_address_search.group().split(" ")[1]
226 main.log.info("Mac-Address of "+ host + ":"+ interface + " is " + mac_address)
227 return mac_address
228 else:
229 main.log.error("Connection failed to the host")
230
adminbae64d82013-08-01 10:50:15 -0700231 def getIPAddress(self,host):
232 '''
Jon Hall41f40e82014-04-08 16:43:17 -0700233 Verifies the host's ip configured or not.
adminbae64d82013-08-01 10:50:15 -0700234 '''
235 if self.handle :
236 response = self.execute(cmd=host+" ifconfig",prompt="mininet>",timeout=10)
237
238 pattern = "inet\saddr:(\d+\.\d+\.\d+\.\d+)"
239 ip_address_search = re.search(pattern, response)
240 main.log.info("IP-Address of Host "+host +" is "+ip_address_search.group(1))
241 return ip_address_search.group(1)
242 else :
243 main.log.error("Connection failed to the host")
Ahmed El-Hassanyfd329182014-04-10 11:38:16 -0700244
245 def getSwitchDPID(self,switch):
246 '''
247 return the datapath ID of the switch
248 '''
249 if self.handle :
250 cmd = "py %s.dpid" % switch
251 response = self.execute(cmd=cmd,prompt="mininet>",timeout=10)
252 pattern = r'^(?P<dpid>\d)+'
253 result = re.search(pattern, response, re.MULTILINE)
254 if result is None:
255 main.log.info("Couldn't find DPID for switch '', found: %s" % (switch, response))
256 return main.FALSE
257 return int(result.group('dpid'))
258 else:
259 main.log.error("Connection failed to the host")
260
261 def getInterfaces(self, node):
262 '''
263 return information dict about interfaces connected to the node
264 '''
265 if self.handle :
266 cmd = 'py "\\n".join(["name=%s,mac=%s,ip=%s,isUp=%s" % (i.name, i.MAC(), i.IP(), i.isUp())'
267 cmd += ' for i in %s.intfs.values()])' % node
268 response = self.execute(cmd=cmd,prompt="mininet>",timeout=10)
269 return response
270 else:
271 main.log.error("Connection failed to the node")
272
adminbae64d82013-08-01 10:50:15 -0700273 def dump(self):
274 main.log.info("Dump node info")
Ahmed El-Hassanyd1f71702014-04-04 16:12:45 -0700275 response = self.execute(cmd = 'dump',prompt = 'mininet>',timeout = 10)
276 return response
adminbae64d82013-08-01 10:50:15 -0700277
278 def intfs(self):
279 main.log.info("List interfaces")
Jon Hall668ed802014-04-08 17:17:59 -0700280 response = self.execute(cmd = 'intfs',prompt = 'mininet>',timeout = 10)
281 return response
adminbae64d82013-08-01 10:50:15 -0700282
283 def net(self):
284 main.log.info("List network connections")
Jon Hall668ed802014-04-08 17:17:59 -0700285 response = self.execute(cmd = 'net',prompt = 'mininet>',timeout = 10)
286 return response
adminbae64d82013-08-01 10:50:15 -0700287
288 def iperf(self):
289 main.log.info("Simple iperf TCP test between two (optionally specified) hosts")
Jon Hall668ed802014-04-08 17:17:59 -0700290 response = self.execute(cmd = 'iperf',prompt = 'mininet>',timeout = 10)
291 return response
adminbae64d82013-08-01 10:50:15 -0700292
293 def iperfudp(self):
294 main.log.info("Simple iperf TCP test between two (optionally specified) hosts")
Jon Hall668ed802014-04-08 17:17:59 -0700295 response = self.execute(cmd = 'iperfudp',prompt = 'mininet>',timeout = 10)
296 return response
adminbae64d82013-08-01 10:50:15 -0700297
298 def nodes(self):
299 main.log.info("List all nodes.")
Jon Hall668ed802014-04-08 17:17:59 -0700300 response = self.execute(cmd = 'nodes',prompt = 'mininet>',timeout = 10)
301 return response
adminbae64d82013-08-01 10:50:15 -0700302
303 def pingpair(self):
304 main.log.infoe("Ping between first two hosts")
305 self.execute(cmd = 'pingpair',prompt = 'mininet>',timeout = 20)
306
307 if utilities.assert_matches(expect='0% packet loss',actual=response,onpass="No Packet loss",onfail="Hosts not reachable"):
308 main.log.info("Ping between two hosts SUCCESS")
309 main.last_result = main.TRUE
310 return main.TRUE
311 else :
312 main.log.error("PACKET LOST, HOSTS NOT REACHABLE")
313 main.last_result = main.FALSE
314 return main.FALSE
315
316 def link(self,**linkargs):
317 '''
318 Bring link(s) between two nodes up or down
319 '''
320 main.log.info('Bring link(s) between two nodes up or down')
321 args = utilities.parse_args(["END1","END2","OPTION"],**linkargs)
322 end1 = args["END1"] if args["END1"] != None else ""
323 end2 = args["END2"] if args["END2"] != None else ""
324 option = args["OPTION"] if args["OPTION"] != None else ""
325 command = "link "+str(end1) + " " + str(end2)+ " " + str(option)
326 response = self.execute(cmd=command,prompt="mininet>",timeout=10)
327 return main.TRUE
328
329
admin530b4c92013-08-14 16:54:35 -0700330 def yank(self,**yankargs):
adminaeedddd2013-08-02 15:14:15 -0700331 '''
admin530b4c92013-08-14 16:54:35 -0700332 yank a mininet switch interface to a host
adminaeedddd2013-08-02 15:14:15 -0700333 '''
admin530b4c92013-08-14 16:54:35 -0700334 main.log.info('Yank the switch interface attached to a host')
335 args = utilities.parse_args(["SW","INTF"],**yankargs)
adminaeedddd2013-08-02 15:14:15 -0700336 sw = args["SW"] if args["SW"] !=None else ""
337 intf = args["INTF"] if args["INTF"] != None else ""
338 command = "py "+ str(sw) + '.detach("' + str(intf) + '")'
339 response = self.execute(cmd=command,prompt="mininet>",timeout=10)
340 return main.TRUE
341
342 def plug(self, **plugargs):
343 '''
344 plug the yanked mininet switch interface to a switch
345 '''
346 main.log.info('Plug the switch interface attached to a switch')
admin530b4c92013-08-14 16:54:35 -0700347 args = utilities.parse_args(["SW","INTF"],**plugargs)
adminaeedddd2013-08-02 15:14:15 -0700348 sw = args["SW"] if args["SW"] !=None else ""
349 intf = args["INTF"] if args["INTF"] != None else ""
350 command = "py "+ str(sw) + '.attach("' + str(intf) + '")'
351 response = self.execute(cmd=command,prompt="mininet>",timeout=10)
352 return main.TRUE
353
354
355
adminbae64d82013-08-01 10:50:15 -0700356 def dpctl(self,**dpctlargs):
357 '''
Jon Hall41f40e82014-04-08 16:43:17 -0700358 Run dpctl command on all switches.
adminbae64d82013-08-01 10:50:15 -0700359 '''
360 main.log.info('Run dpctl command on all switches')
361 args = utilities.parse_args(["CMD","ARGS"],**dpctlargs)
362 cmd = args["CMD"] if args["CMD"] != None else ""
363 cmdargs = args["ARGS"] if args["ARGS"] != None else ""
364 command = "dpctl "+cmd + " " + str(cmdargs)
365 response = self.execute(cmd=command,prompt="mininet>",timeout=10)
366 return main.TRUE
367
368
369 def get_version(self):
370 file_input = path+'/lib/Mininet/INSTALL'
371 version = super(Mininet, self).get_version()
372 pattern = 'Mininet\s\w\.\w\.\w\w*'
373 for line in open(file_input,'r').readlines():
374 result = re.match(pattern, line)
375 if result:
376 version = result.group(0)
377 return version
378
379 def get_sw_controller(self,sw):
380 command = "sh ovs-vsctl get-controller "+str(sw)
381 main.log.info(self.execute(cmd=command,prompt="mininet>",timeout=10))
382
383 def assign_sw_controller(self,**kwargs):
Jon Hallf89c8552014-04-02 13:14:06 -0700384 '''
385 count is only needed if there is more than 1 controller
386 '''
387 args = utilities.parse_args(["COUNT"],**kwargs)
388 count = args["COUNT"] if args!={} else 1
389
390 argstring = "SW"
391 for j in range(count):
392 argstring = argstring + ",IP" + str(j+1) + ",PORT" + str(j+1)
393 args = utilities.parse_args(argstring.split(","),**kwargs)
394
adminbae64d82013-08-01 10:50:15 -0700395 sw = args["SW"] if args["SW"] != None else ""
admin530b4c92013-08-14 16:54:35 -0700396 ptcpA = int(args["PORT1"])+int(sw) if args["PORT1"] != None else ""
Jon Hallf89c8552014-04-02 13:14:06 -0700397 ptcpB = "ptcp:"+str(ptcpA) if ptcpA != "" else ""
398
399 command = "sh ovs-vsctl set-controller s" + str(sw) + " " + ptcpB + " "
400 for j in range(count):
401 i=j+1
402 args = utilities.parse_args(["IP"+str(i),"PORT"+str(i)],**kwargs)
403 ip = args["IP"+str(i)] if args["IP"+str(i)] != None else ""
404 port = args["PORT" + str(i)] if args["PORT" + str(i)] != None else ""
405 tcp = "tcp:" + str(ip) + ":" + str(port) + " " if ip != "" else ""
406 command = command + tcp
adminbae64d82013-08-01 10:50:15 -0700407 self.execute(cmd=command,prompt="mininet>",timeout=5)
408
409 def disconnect(self):
410 main.log.info("Disconnecting mininet...")
411 response = ''
412 if self.handle:
413 response = self.execute(cmd="exit",prompt="(.*)",timeout=120)
414 response = self.execute(cmd="exit",prompt="(.*)",timeout=120)
415
416 else :
417 main.log.error("Connection failed to the host")
418 response = main.FALSE
419 return response
420
421 def ctrl_none(self):
422 #self.execute(cmd="sh ~/ONOS/scripts/test-ctrl-none.sh", prompt="mininet",timeout=20)
423 self.handle.sendline()
424 self.handle.expect("mininet>")
425 self.handle.sendline("sh ~/ONOS/scripts/test-ctrl-none.sh")
426 self.handle.expect("test-ctrl-none")
427 self.handle.expect("mininet>", 20)
428
429 def ctrl_all(self):
430 self.execute(cmd="sh ~/ONOS/scripts/test-ctrl-add-ext.sh", prompt="mininet",timeout=20)
431
432 def ctrl_divide(self):
433 self.execute(cmd="sh ~/ONOS/scripts/ctrl-divide.sh ", prompt="mininet",timeout=20)
434
435 def ctrl_local(self):
436 self.execute(cmd="sh ~/ONOS/scripts/test-ctrl-local.sh ", prompt="mininet",timeout=20)
437
438 def ctrl_one(self, ip):
439 self.execute(cmd="sh ~/ONOS/scripts/ctrl-one.sh "+ip, prompt="mininet",timeout=20)
admin07529932013-11-22 14:58:28 -0800440
441 def arping(self, src, dest, destmac):
442 self.handle.sendline('')
443 self.handle.expect("mininet")
444
445 self.handle.sendline(src + ' arping ' + dest)
446 try:
447 self.handle.expect(destmac)
448 main.log.info("ARP successful")
449 self.handle.expect("mininet")
450 return main.TRUE
451 except:
452 main.log.warn("ARP FAILURE")
453 self.handle.expect("mininet")
454 return main.FALSE
455
456 def decToHex(num):
457 return hex(num).split('x')[1]
adminbae64d82013-08-01 10:50:15 -0700458
459if __name__ != "__main__":
460 import sys
461 sys.modules[__name__] = MininetCliDriver()