blob: 59fae79923ae508f5f6191d99478bfe7560c99a2 [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 '''
39 MininetCliDriver is the basic driver which will handle the Mininet functions
40 '''
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):
48 #,user_name, ip_address, pwd,options):
49 # Here the main is the TestON instance after creating all the log handles.
50 for key in connectargs:
51 vars(self)[key] = connectargs[key]
52
53 self.name = self.options['name']
54 self.handle = super(MininetCliDriver, self).connect(user_name = self.user_name, ip_address = self.ip_address,port = None, pwd = self.pwd)
55
56 self.ssh_handle = self.handle
57
58 # Copying the readme file to process the
59 if self.handle :
60 #self.handle.logfile = sys.stdout
61 main.log.info("Clearing any residual state or processes")
62 self.handle.sendline("sudo mn -c")
63
64 i=self.handle.expect(['password\sfor\sadmin','Cleanup\scomplete',pexpect.EOF,pexpect.TIMEOUT],60)
65 if i==0:
66 main.log.info("sending sudo password")
67 self.handle.sendline('onos_test')
68 i=self.handle.expect(['admin:','\$',pexpect.EOF,pexpect.TIMEOUT],60)
69 if i==1:
70 main.log.info("Clean")
71
72 elif i==2:
73 main.log.error("Connection timeout")
74 elif i==3: #timeout
75 main.log.error("Something while cleaning MN took too long... " )
76
77 #cmdString = "sudo mn --topo "+self.options['topo']+","+self.options['topocount']+" --mac --switch "+self.options['switch']+" --controller "+self.options['controller']
78 #cmdString = "sudo mn --custom ~/mininet/custom/topo-2sw-2host.py --controller remote --ip 192.168.56.102 --port 6633 --topo mytopo"
79 main.log.info("building fresh mininet")
adminbeea0032014-01-23 14:54:13 -080080
81 #### for reactive/PARP enabled tests
admin07529932013-11-22 14:58:28 -080082 cmdString = "sudo mn " + self.options['arg1'] + " " + self.options['arg2'] + " --mac --controller " + self.options['controller']
adminbeea0032014-01-23 14:54:13 -080083 #### for proactive flow with static ARP entries
84 #cmdString = "sudo mn " + self.options['arg1'] + " " + self.options['arg2'] + " --mac --arp --controller " + self.options['controller']
adminbae64d82013-08-01 10:50:15 -070085 #resultCommand = self.execute(cmd=cmdString,prompt='mininet>',timeout=120)
86 self.handle.sendline(cmdString)
87 self.handle.expect("sudo mn")
88 while 1:
89 i=self.handle.expect(['mininet>','\*\*\*','Exception',pexpect.EOF,pexpect.TIMEOUT],300)
90 if i==0:
91 main.log.info("mininet built")
92 return main.TRUE
93 if i==1:
94 self.handle.expect("\n")
95 main.log.info(self.handle.before)
96 elif i==2:
97 main.log.error("Launching mininet failed...")
98 return main.FALSE
99 elif i==3:
100 main.log.error("Connection timeout")
101 return main.FALSE
102 elif i==4: #timeout
103 main.log.error("Something took too long... " )
104 return main.FALSE
105
106 #if utilities.assert_matches(expect=patterns,actual=resultCommand,onpass="Network is being launched",onfail="Network launching is being failed "):
107 return main.TRUE
108 #else:
109 # return main.FALSE
110
111 else :
112 main.log.error("Connection failed to the host "+self.user_name+"@"+self.ip_address)
113 main.log.error("Failed to connect to the Mininet")
114 return main.FALSE
115
116 def pingall(self):
117 '''
118 Verifies the reachability of the hosts using pingall command.
119 '''
120 if self.handle :
121 main.log.info("Checking reachabilty to the hosts using pingall")
122 response = self.execute(cmd="pingall",prompt="mininet>",timeout=10)
123 pattern = 'Results\:\s0\%\sdropped\s\(0\/\d+\slost\)\s*$'
124 if utilities.assert_matches(expect=pattern,actual=response,onpass="All hosts are reaching",onfail="Unable to reach all the hosts"):
125 return main.TRUE
126 else:
127 return main.FALSE
128 else :
129 main.log.error("Connection failed to the host")
130 return main.FALSE
adminaeedddd2013-08-02 15:14:15 -0700131
132 def fpingHost(self,**pingParams):
133 '''
134 Uses the fping package for faster pinging...
135 *requires fping to be installed on machine running mininet
136 '''
137 args = utilities.parse_args(["SRC","TARGET"],**pingParams)
admin530b4c92013-08-14 16:54:35 -0700138 command = args["SRC"] + " fping -i 100 -t 20 -C 1 -q "+args["TARGET"]
adminaeedddd2013-08-02 15:14:15 -0700139 self.handle.sendline(command)
140 self.handle.expect(args["TARGET"])
141 self.handle.expect("mininet")
142 response = self.handle.before
143 if re.search(":\s-" ,response):
144 main.log.info("Ping fail")
145 return main.FALSE
admin530b4c92013-08-14 16:54:35 -0700146 elif re.search(":\s\d{1,2}\.\d\d", response):
adminaeedddd2013-08-02 15:14:15 -0700147 main.log.info("Ping good!")
148 return main.TRUE
149 main.log.info("Install fping on mininet machine... ")
150 main.log.info("\n---\n"+response)
151 return main.FALSE
adminbae64d82013-08-01 10:50:15 -0700152
153 def pingHost(self,**pingParams):
154
155 args = utilities.parse_args(["SRC","TARGET"],**pingParams)
156 #command = args["SRC"] + " ping -" + args["CONTROLLER"] + " " +args ["TARGET"]
admin530b4c92013-08-14 16:54:35 -0700157 command = args["SRC"] + " ping "+args ["TARGET"]+" -c 1 -i .2"
adminbae64d82013-08-01 10:50:15 -0700158 response = self.execute(cmd=command,prompt="mininet",timeout=10 )
159 if utilities.assert_matches(expect=',\s0\%\spacket\sloss',actual=response,onpass="No Packet loss",onfail="Host is not reachable"):
160 main.log.info("NO PACKET LOSS, HOST IS REACHABLE")
161 main.last_result = main.TRUE
162 return main.TRUE
163 else :
164 main.log.error("PACKET LOST, HOST IS NOT REACHABLE")
165 main.last_result = main.FALSE
166 return main.FALSE
167
168
169 def checkIP(self,host):
170 '''
171 Verifies the host's ip configured or not.
172 '''
173 if self.handle :
174 response = self.execute(cmd=host+" ifconfig",prompt="mininet>",timeout=10)
175
176 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})"
177 #pattern = "inet\saddr:10.0.0.6"
178 if utilities.assert_matches(expect=pattern,actual=response,onpass="Host Ip configured properly",onfail="Host IP not found") :
179 return main.TRUE
180 else:
181 return main.FALSE
182 else :
183 main.log.error("Connection failed to the host")
184
185 def verifySSH(self,**connectargs):
186 response = self.execute(cmd="h1 /usr/sbin/sshd -D&",prompt="mininet>",timeout=10)
187 response = self.execute(cmd="h4 /usr/sbin/sshd -D&",prompt="mininet>",timeout=10)
188 for key in connectargs:
189 vars(self)[key] = connectargs[key]
190 response = self.execute(cmd="xterm h1 h4 ",prompt="mininet>",timeout=10)
191 import time
192 time.sleep(20)
193 if self.flag == 0:
194 self.flag = 1
195 return main.FALSE
196 else :
197 return main.TRUE
198
199 def getMacAddress(self,host):
200 '''
201 Verifies the host's ip configured or not.
202 '''
203 if self.handle :
204 response = self.execute(cmd=host+" ifconfig",prompt="mininet>",timeout=10)
205
206 pattern = "HWaddr\s(((\d|\w)+:)+(\d|\w))"
207 mac_address_search = re.search(pattern, response)
208 main.log.info("Mac-Address of Host "+host +" is "+mac_address_search.group(1))
209 return mac_address_search.group(1)
210 else :
211 main.log.error("Connection failed to the host")
212 def getIPAddress(self,host):
213 '''
214 Verifies the host's ip configured or not.
215 '''
216 if self.handle :
217 response = self.execute(cmd=host+" ifconfig",prompt="mininet>",timeout=10)
218
219 pattern = "inet\saddr:(\d+\.\d+\.\d+\.\d+)"
220 ip_address_search = re.search(pattern, response)
221 main.log.info("IP-Address of Host "+host +" is "+ip_address_search.group(1))
222 return ip_address_search.group(1)
223 else :
224 main.log.error("Connection failed to the host")
225
226 def dump(self):
227 main.log.info("Dump node info")
228 self.execute(cmd = 'dump',prompt = 'mininet>',timeout = 10)
229 return main.TRUE
230
231 def intfs(self):
232 main.log.info("List interfaces")
233 self.execute(cmd = 'intfs',prompt = 'mininet>',timeout = 10)
234 return main.TRUE
235
236 def net(self):
237 main.log.info("List network connections")
238 self.execute(cmd = 'net',prompt = 'mininet>',timeout = 10)
239 return main.TRUE
240
241 def iperf(self):
242 main.log.info("Simple iperf TCP test between two (optionally specified) hosts")
243 self.execute(cmd = 'iperf',prompt = 'mininet>',timeout = 10)
244 return main.TRUE
245
246 def iperfudp(self):
247 main.log.info("Simple iperf TCP test between two (optionally specified) hosts")
248 self.execute(cmd = 'iperfudp',prompt = 'mininet>',timeout = 10)
249 return main.TRUE
250
251 def nodes(self):
252 main.log.info("List all nodes.")
253 self.execute(cmd = 'nodes',prompt = 'mininet>',timeout = 10)
254 return main.TRUE
255
256 def pingpair(self):
257 main.log.infoe("Ping between first two hosts")
258 self.execute(cmd = 'pingpair',prompt = 'mininet>',timeout = 20)
259
260 if utilities.assert_matches(expect='0% packet loss',actual=response,onpass="No Packet loss",onfail="Hosts not reachable"):
261 main.log.info("Ping between two hosts SUCCESS")
262 main.last_result = main.TRUE
263 return main.TRUE
264 else :
265 main.log.error("PACKET LOST, HOSTS NOT REACHABLE")
266 main.last_result = main.FALSE
267 return main.FALSE
268
269 def link(self,**linkargs):
270 '''
271 Bring link(s) between two nodes up or down
272 '''
273 main.log.info('Bring link(s) between two nodes up or down')
274 args = utilities.parse_args(["END1","END2","OPTION"],**linkargs)
275 end1 = args["END1"] if args["END1"] != None else ""
276 end2 = args["END2"] if args["END2"] != None else ""
277 option = args["OPTION"] if args["OPTION"] != None else ""
278 command = "link "+str(end1) + " " + str(end2)+ " " + str(option)
279 response = self.execute(cmd=command,prompt="mininet>",timeout=10)
280 return main.TRUE
281
282
admin530b4c92013-08-14 16:54:35 -0700283 def yank(self,**yankargs):
adminaeedddd2013-08-02 15:14:15 -0700284 '''
admin530b4c92013-08-14 16:54:35 -0700285 yank a mininet switch interface to a host
adminaeedddd2013-08-02 15:14:15 -0700286 '''
admin530b4c92013-08-14 16:54:35 -0700287 main.log.info('Yank the switch interface attached to a host')
288 args = utilities.parse_args(["SW","INTF"],**yankargs)
adminaeedddd2013-08-02 15:14:15 -0700289 sw = args["SW"] if args["SW"] !=None else ""
290 intf = args["INTF"] if args["INTF"] != None else ""
291 command = "py "+ str(sw) + '.detach("' + str(intf) + '")'
292 response = self.execute(cmd=command,prompt="mininet>",timeout=10)
293 return main.TRUE
294
295 def plug(self, **plugargs):
296 '''
297 plug the yanked mininet switch interface to a switch
298 '''
299 main.log.info('Plug the switch interface attached to a switch')
admin530b4c92013-08-14 16:54:35 -0700300 args = utilities.parse_args(["SW","INTF"],**plugargs)
adminaeedddd2013-08-02 15:14:15 -0700301 sw = args["SW"] if args["SW"] !=None else ""
302 intf = args["INTF"] if args["INTF"] != None else ""
303 command = "py "+ str(sw) + '.attach("' + str(intf) + '")'
304 response = self.execute(cmd=command,prompt="mininet>",timeout=10)
305 return main.TRUE
306
307
308
adminbae64d82013-08-01 10:50:15 -0700309 def dpctl(self,**dpctlargs):
310 '''
311 Run dpctl command on all switches.
312 '''
313 main.log.info('Run dpctl command on all switches')
314 args = utilities.parse_args(["CMD","ARGS"],**dpctlargs)
315 cmd = args["CMD"] if args["CMD"] != None else ""
316 cmdargs = args["ARGS"] if args["ARGS"] != None else ""
317 command = "dpctl "+cmd + " " + str(cmdargs)
318 response = self.execute(cmd=command,prompt="mininet>",timeout=10)
319 return main.TRUE
320
321
322 def get_version(self):
323 file_input = path+'/lib/Mininet/INSTALL'
324 version = super(Mininet, self).get_version()
325 pattern = 'Mininet\s\w\.\w\.\w\w*'
326 for line in open(file_input,'r').readlines():
327 result = re.match(pattern, line)
328 if result:
329 version = result.group(0)
330 return version
331
332 def get_sw_controller(self,sw):
333 command = "sh ovs-vsctl get-controller "+str(sw)
334 main.log.info(self.execute(cmd=command,prompt="mininet>",timeout=10))
335
336 def assign_sw_controller(self,**kwargs):
337 args = utilities.parse_args(["SW","IP1","PORT1","IP2","PORT2","IP3","PORT3","IP4","PORT4","IP5","PORT5","IP6","PORT6","IP7","PORT7","IP8","PORT8"],**kwargs)
338 sw = args["SW"] if args["SW"] != None else ""
339 ip1 = args["IP1"] if args["IP1"] != None else ""
340 ip2 = args["IP2"] if args["IP2"] != None else ""
341 ip3 = args["IP3"] if args["IP3"] != None else ""
342 ip4 = args["IP4"] if args["IP4"] != None else ""
343 ip5 = args["IP5"] if args["IP5"] != None else ""
344 ip6 = args["IP6"] if args["IP6"] != None else ""
345 ip7 = args["IP7"] if args["IP7"] != None else ""
346 ip8 = args["IP8"] if args["IP8"] != None else ""
347 # master = args["MASTER"] if args["MASTER"] != None else ""
348 port1 = args["PORT1"] if args["PORT1"] != None else ""
349 port2 = args["PORT2"] if args["PORT2"] != None else ""
350 port3 = args["PORT3"] if args["PORT3"] != None else ""
351 port4 = args["PORT4"] if args["PORT4"] != None else ""
352 port5 = args["PORT5"] if args["PORT5"] != None else ""
353 port6 = args["PORT6"] if args["PORT6"] != None else ""
354 port7 = args["PORT7"] if args["PORT7"] != None else ""
355 port8 = args["PORT8"] if args["PORT8"] != None else ""
admin530b4c92013-08-14 16:54:35 -0700356 ptcpA = int(args["PORT1"])+int(sw) if args["PORT1"] != None else ""
357 ptcpB = "ptcp:"+str(ptcpA) if ip1 != "" else ""
adminbae64d82013-08-01 10:50:15 -0700358 tcp1 = "tcp:"+str(ip1)+":"+str(port1) if ip1 != "" else ""
359 tcp2 = "tcp:"+str(ip2)+":"+str(port2) if ip2 != "" else ""
360 tcp3 = "tcp:"+str(ip3)+":"+str(port3) if ip3 != "" else ""
361 tcp4 = "tcp:"+str(ip4)+":"+str(port4) if ip4 != "" else ""
362 tcp5 = "tcp:"+str(ip5)+":"+str(port5) if ip5 != "" else ""
363 tcp6 = "tcp:"+str(ip6)+":"+str(port6) if ip6 != "" else ""
364 tcp7 = "tcp:"+str(ip7)+":"+str(port7) if ip7 != "" else ""
365 tcp8 = "tcp:"+str(ip8)+":"+str(port8) if ip8 != "" else ""
366 # master1 = tcp1+" role master " if args["MASTER"] == 1 else ""
367 # master2 = tcp2+" role master " if args["MASTER"] == 2 else ""
368 # master3 = tcp3+" role master " if args["MASTER"] == 3 else ""
369 # master4 = tcp4+" role master " if args["MASTER"] == 4 else ""
admin530b4c92013-08-14 16:54:35 -0700370 command = "sh ovs-vsctl set-controller s"+str(sw)+" "+ptcpB+" "+tcp1+" "+tcp2+" "+tcp3+" "+tcp4+" "+tcp5+" "+tcp6+" "+tcp7+" "+tcp8
adminbae64d82013-08-01 10:50:15 -0700371 self.execute(cmd=command,prompt="mininet>",timeout=5)
372
373 def disconnect(self):
374 main.log.info("Disconnecting mininet...")
375 response = ''
376 if self.handle:
377 response = self.execute(cmd="exit",prompt="(.*)",timeout=120)
378 response = self.execute(cmd="exit",prompt="(.*)",timeout=120)
379
380 else :
381 main.log.error("Connection failed to the host")
382 response = main.FALSE
383 return response
384
385 def ctrl_none(self):
386 #self.execute(cmd="sh ~/ONOS/scripts/test-ctrl-none.sh", prompt="mininet",timeout=20)
387 self.handle.sendline()
388 self.handle.expect("mininet>")
389 self.handle.sendline("sh ~/ONOS/scripts/test-ctrl-none.sh")
390 self.handle.expect("test-ctrl-none")
391 self.handle.expect("mininet>", 20)
392
393 def ctrl_all(self):
394 self.execute(cmd="sh ~/ONOS/scripts/test-ctrl-add-ext.sh", prompt="mininet",timeout=20)
395
396 def ctrl_divide(self):
397 self.execute(cmd="sh ~/ONOS/scripts/ctrl-divide.sh ", prompt="mininet",timeout=20)
398
399 def ctrl_local(self):
400 self.execute(cmd="sh ~/ONOS/scripts/test-ctrl-local.sh ", prompt="mininet",timeout=20)
401
402 def ctrl_one(self, ip):
403 self.execute(cmd="sh ~/ONOS/scripts/ctrl-one.sh "+ip, prompt="mininet",timeout=20)
admin07529932013-11-22 14:58:28 -0800404
405 def arping(self, src, dest, destmac):
406 self.handle.sendline('')
407 self.handle.expect("mininet")
408
409 self.handle.sendline(src + ' arping ' + dest)
410 try:
411 self.handle.expect(destmac)
412 main.log.info("ARP successful")
413 self.handle.expect("mininet")
414 return main.TRUE
415 except:
416 main.log.warn("ARP FAILURE")
417 self.handle.expect("mininet")
418 return main.FALSE
419
420 def decToHex(num):
421 return hex(num).split('x')[1]
adminbae64d82013-08-01 10:50:15 -0700422
423if __name__ != "__main__":
424 import sys
425 sys.modules[__name__] = MininetCliDriver()