blob: 286b1c83b0784e596ee6ddb2ac81521952810be7 [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")
admin07529932013-11-22 14:58:28 -080080 cmdString = "sudo mn " + self.options['arg1'] + " " + self.options['arg2'] + " --mac --controller " + self.options['controller']
adminbae64d82013-08-01 10:50:15 -070081 #resultCommand = self.execute(cmd=cmdString,prompt='mininet>',timeout=120)
82 self.handle.sendline(cmdString)
83 self.handle.expect("sudo mn")
84 while 1:
85 i=self.handle.expect(['mininet>','\*\*\*','Exception',pexpect.EOF,pexpect.TIMEOUT],300)
86 if i==0:
87 main.log.info("mininet built")
88 return main.TRUE
89 if i==1:
90 self.handle.expect("\n")
91 main.log.info(self.handle.before)
92 elif i==2:
93 main.log.error("Launching mininet failed...")
94 return main.FALSE
95 elif i==3:
96 main.log.error("Connection timeout")
97 return main.FALSE
98 elif i==4: #timeout
99 main.log.error("Something took too long... " )
100 return main.FALSE
101
102 #if utilities.assert_matches(expect=patterns,actual=resultCommand,onpass="Network is being launched",onfail="Network launching is being failed "):
103 return main.TRUE
104 #else:
105 # return main.FALSE
106
107 else :
108 main.log.error("Connection failed to the host "+self.user_name+"@"+self.ip_address)
109 main.log.error("Failed to connect to the Mininet")
110 return main.FALSE
111
112 def pingall(self):
113 '''
114 Verifies the reachability of the hosts using pingall command.
115 '''
116 if self.handle :
117 main.log.info("Checking reachabilty to the hosts using pingall")
118 response = self.execute(cmd="pingall",prompt="mininet>",timeout=10)
119 pattern = 'Results\:\s0\%\sdropped\s\(0\/\d+\slost\)\s*$'
120 if utilities.assert_matches(expect=pattern,actual=response,onpass="All hosts are reaching",onfail="Unable to reach all the hosts"):
121 return main.TRUE
122 else:
123 return main.FALSE
124 else :
125 main.log.error("Connection failed to the host")
126 return main.FALSE
adminaeedddd2013-08-02 15:14:15 -0700127
128 def fpingHost(self,**pingParams):
129 '''
130 Uses the fping package for faster pinging...
131 *requires fping to be installed on machine running mininet
132 '''
133 args = utilities.parse_args(["SRC","TARGET"],**pingParams)
admin530b4c92013-08-14 16:54:35 -0700134 command = args["SRC"] + " fping -i 100 -t 20 -C 1 -q "+args["TARGET"]
adminaeedddd2013-08-02 15:14:15 -0700135 self.handle.sendline(command)
136 self.handle.expect(args["TARGET"])
137 self.handle.expect("mininet")
138 response = self.handle.before
139 if re.search(":\s-" ,response):
140 main.log.info("Ping fail")
141 return main.FALSE
admin530b4c92013-08-14 16:54:35 -0700142 elif re.search(":\s\d{1,2}\.\d\d", response):
adminaeedddd2013-08-02 15:14:15 -0700143 main.log.info("Ping good!")
144 return main.TRUE
145 main.log.info("Install fping on mininet machine... ")
146 main.log.info("\n---\n"+response)
147 return main.FALSE
adminbae64d82013-08-01 10:50:15 -0700148
149 def pingHost(self,**pingParams):
150
151 args = utilities.parse_args(["SRC","TARGET"],**pingParams)
152 #command = args["SRC"] + " ping -" + args["CONTROLLER"] + " " +args ["TARGET"]
admin530b4c92013-08-14 16:54:35 -0700153 command = args["SRC"] + " ping "+args ["TARGET"]+" -c 1 -i .2"
adminbae64d82013-08-01 10:50:15 -0700154 response = self.execute(cmd=command,prompt="mininet",timeout=10 )
155 if utilities.assert_matches(expect=',\s0\%\spacket\sloss',actual=response,onpass="No Packet loss",onfail="Host is not reachable"):
156 main.log.info("NO PACKET LOSS, HOST IS REACHABLE")
157 main.last_result = main.TRUE
158 return main.TRUE
159 else :
160 main.log.error("PACKET LOST, HOST IS NOT REACHABLE")
161 main.last_result = main.FALSE
162 return main.FALSE
163
164
165 def checkIP(self,host):
166 '''
167 Verifies the host's ip configured or not.
168 '''
169 if self.handle :
170 response = self.execute(cmd=host+" ifconfig",prompt="mininet>",timeout=10)
171
172 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})"
173 #pattern = "inet\saddr:10.0.0.6"
174 if utilities.assert_matches(expect=pattern,actual=response,onpass="Host Ip configured properly",onfail="Host IP not found") :
175 return main.TRUE
176 else:
177 return main.FALSE
178 else :
179 main.log.error("Connection failed to the host")
180
181 def verifySSH(self,**connectargs):
182 response = self.execute(cmd="h1 /usr/sbin/sshd -D&",prompt="mininet>",timeout=10)
183 response = self.execute(cmd="h4 /usr/sbin/sshd -D&",prompt="mininet>",timeout=10)
184 for key in connectargs:
185 vars(self)[key] = connectargs[key]
186 response = self.execute(cmd="xterm h1 h4 ",prompt="mininet>",timeout=10)
187 import time
188 time.sleep(20)
189 if self.flag == 0:
190 self.flag = 1
191 return main.FALSE
192 else :
193 return main.TRUE
194
195 def getMacAddress(self,host):
196 '''
197 Verifies the host's ip configured or not.
198 '''
199 if self.handle :
200 response = self.execute(cmd=host+" ifconfig",prompt="mininet>",timeout=10)
201
202 pattern = "HWaddr\s(((\d|\w)+:)+(\d|\w))"
203 mac_address_search = re.search(pattern, response)
204 main.log.info("Mac-Address of Host "+host +" is "+mac_address_search.group(1))
205 return mac_address_search.group(1)
206 else :
207 main.log.error("Connection failed to the host")
208 def getIPAddress(self,host):
209 '''
210 Verifies the host's ip configured or not.
211 '''
212 if self.handle :
213 response = self.execute(cmd=host+" ifconfig",prompt="mininet>",timeout=10)
214
215 pattern = "inet\saddr:(\d+\.\d+\.\d+\.\d+)"
216 ip_address_search = re.search(pattern, response)
217 main.log.info("IP-Address of Host "+host +" is "+ip_address_search.group(1))
218 return ip_address_search.group(1)
219 else :
220 main.log.error("Connection failed to the host")
221
222 def dump(self):
223 main.log.info("Dump node info")
224 self.execute(cmd = 'dump',prompt = 'mininet>',timeout = 10)
225 return main.TRUE
226
227 def intfs(self):
228 main.log.info("List interfaces")
229 self.execute(cmd = 'intfs',prompt = 'mininet>',timeout = 10)
230 return main.TRUE
231
232 def net(self):
233 main.log.info("List network connections")
234 self.execute(cmd = 'net',prompt = 'mininet>',timeout = 10)
235 return main.TRUE
236
237 def iperf(self):
238 main.log.info("Simple iperf TCP test between two (optionally specified) hosts")
239 self.execute(cmd = 'iperf',prompt = 'mininet>',timeout = 10)
240 return main.TRUE
241
242 def iperfudp(self):
243 main.log.info("Simple iperf TCP test between two (optionally specified) hosts")
244 self.execute(cmd = 'iperfudp',prompt = 'mininet>',timeout = 10)
245 return main.TRUE
246
247 def nodes(self):
248 main.log.info("List all nodes.")
249 self.execute(cmd = 'nodes',prompt = 'mininet>',timeout = 10)
250 return main.TRUE
251
252 def pingpair(self):
253 main.log.infoe("Ping between first two hosts")
254 self.execute(cmd = 'pingpair',prompt = 'mininet>',timeout = 20)
255
256 if utilities.assert_matches(expect='0% packet loss',actual=response,onpass="No Packet loss",onfail="Hosts not reachable"):
257 main.log.info("Ping between two hosts SUCCESS")
258 main.last_result = main.TRUE
259 return main.TRUE
260 else :
261 main.log.error("PACKET LOST, HOSTS NOT REACHABLE")
262 main.last_result = main.FALSE
263 return main.FALSE
264
265 def link(self,**linkargs):
266 '''
267 Bring link(s) between two nodes up or down
268 '''
269 main.log.info('Bring link(s) between two nodes up or down')
270 args = utilities.parse_args(["END1","END2","OPTION"],**linkargs)
271 end1 = args["END1"] if args["END1"] != None else ""
272 end2 = args["END2"] if args["END2"] != None else ""
273 option = args["OPTION"] if args["OPTION"] != None else ""
274 command = "link "+str(end1) + " " + str(end2)+ " " + str(option)
275 response = self.execute(cmd=command,prompt="mininet>",timeout=10)
276 return main.TRUE
277
278
admin530b4c92013-08-14 16:54:35 -0700279 def yank(self,**yankargs):
adminaeedddd2013-08-02 15:14:15 -0700280 '''
admin530b4c92013-08-14 16:54:35 -0700281 yank a mininet switch interface to a host
adminaeedddd2013-08-02 15:14:15 -0700282 '''
admin530b4c92013-08-14 16:54:35 -0700283 main.log.info('Yank the switch interface attached to a host')
284 args = utilities.parse_args(["SW","INTF"],**yankargs)
adminaeedddd2013-08-02 15:14:15 -0700285 sw = args["SW"] if args["SW"] !=None else ""
286 intf = args["INTF"] if args["INTF"] != None else ""
287 command = "py "+ str(sw) + '.detach("' + str(intf) + '")'
288 response = self.execute(cmd=command,prompt="mininet>",timeout=10)
289 return main.TRUE
290
291 def plug(self, **plugargs):
292 '''
293 plug the yanked mininet switch interface to a switch
294 '''
295 main.log.info('Plug the switch interface attached to a switch')
admin530b4c92013-08-14 16:54:35 -0700296 args = utilities.parse_args(["SW","INTF"],**plugargs)
adminaeedddd2013-08-02 15:14:15 -0700297 sw = args["SW"] if args["SW"] !=None else ""
298 intf = args["INTF"] if args["INTF"] != None else ""
299 command = "py "+ str(sw) + '.attach("' + str(intf) + '")'
300 response = self.execute(cmd=command,prompt="mininet>",timeout=10)
301 return main.TRUE
302
303
304
adminbae64d82013-08-01 10:50:15 -0700305 def dpctl(self,**dpctlargs):
306 '''
307 Run dpctl command on all switches.
308 '''
309 main.log.info('Run dpctl command on all switches')
310 args = utilities.parse_args(["CMD","ARGS"],**dpctlargs)
311 cmd = args["CMD"] if args["CMD"] != None else ""
312 cmdargs = args["ARGS"] if args["ARGS"] != None else ""
313 command = "dpctl "+cmd + " " + str(cmdargs)
314 response = self.execute(cmd=command,prompt="mininet>",timeout=10)
315 return main.TRUE
316
317
318 def get_version(self):
319 file_input = path+'/lib/Mininet/INSTALL'
320 version = super(Mininet, self).get_version()
321 pattern = 'Mininet\s\w\.\w\.\w\w*'
322 for line in open(file_input,'r').readlines():
323 result = re.match(pattern, line)
324 if result:
325 version = result.group(0)
326 return version
327
328 def get_sw_controller(self,sw):
329 command = "sh ovs-vsctl get-controller "+str(sw)
330 main.log.info(self.execute(cmd=command,prompt="mininet>",timeout=10))
331
332 def assign_sw_controller(self,**kwargs):
333 args = utilities.parse_args(["SW","IP1","PORT1","IP2","PORT2","IP3","PORT3","IP4","PORT4","IP5","PORT5","IP6","PORT6","IP7","PORT7","IP8","PORT8"],**kwargs)
334 sw = args["SW"] if args["SW"] != None else ""
335 ip1 = args["IP1"] if args["IP1"] != None else ""
336 ip2 = args["IP2"] if args["IP2"] != None else ""
337 ip3 = args["IP3"] if args["IP3"] != None else ""
338 ip4 = args["IP4"] if args["IP4"] != None else ""
339 ip5 = args["IP5"] if args["IP5"] != None else ""
340 ip6 = args["IP6"] if args["IP6"] != None else ""
341 ip7 = args["IP7"] if args["IP7"] != None else ""
342 ip8 = args["IP8"] if args["IP8"] != None else ""
343 # master = args["MASTER"] if args["MASTER"] != None else ""
344 port1 = args["PORT1"] if args["PORT1"] != None else ""
345 port2 = args["PORT2"] if args["PORT2"] != None else ""
346 port3 = args["PORT3"] if args["PORT3"] != None else ""
347 port4 = args["PORT4"] if args["PORT4"] != None else ""
348 port5 = args["PORT5"] if args["PORT5"] != None else ""
349 port6 = args["PORT6"] if args["PORT6"] != None else ""
350 port7 = args["PORT7"] if args["PORT7"] != None else ""
351 port8 = args["PORT8"] if args["PORT8"] != None else ""
admin530b4c92013-08-14 16:54:35 -0700352 ptcpA = int(args["PORT1"])+int(sw) if args["PORT1"] != None else ""
353 ptcpB = "ptcp:"+str(ptcpA) if ip1 != "" else ""
adminbae64d82013-08-01 10:50:15 -0700354 tcp1 = "tcp:"+str(ip1)+":"+str(port1) if ip1 != "" else ""
355 tcp2 = "tcp:"+str(ip2)+":"+str(port2) if ip2 != "" else ""
356 tcp3 = "tcp:"+str(ip3)+":"+str(port3) if ip3 != "" else ""
357 tcp4 = "tcp:"+str(ip4)+":"+str(port4) if ip4 != "" else ""
358 tcp5 = "tcp:"+str(ip5)+":"+str(port5) if ip5 != "" else ""
359 tcp6 = "tcp:"+str(ip6)+":"+str(port6) if ip6 != "" else ""
360 tcp7 = "tcp:"+str(ip7)+":"+str(port7) if ip7 != "" else ""
361 tcp8 = "tcp:"+str(ip8)+":"+str(port8) if ip8 != "" else ""
362 # master1 = tcp1+" role master " if args["MASTER"] == 1 else ""
363 # master2 = tcp2+" role master " if args["MASTER"] == 2 else ""
364 # master3 = tcp3+" role master " if args["MASTER"] == 3 else ""
365 # master4 = tcp4+" role master " if args["MASTER"] == 4 else ""
admin530b4c92013-08-14 16:54:35 -0700366 command = "sh ovs-vsctl set-controller s"+str(sw)+" "+ptcpB+" "+tcp1+" "+tcp2+" "+tcp3+" "+tcp4+" "+tcp5+" "+tcp6+" "+tcp7+" "+tcp8
adminbae64d82013-08-01 10:50:15 -0700367 self.execute(cmd=command,prompt="mininet>",timeout=5)
368
369 def disconnect(self):
370 main.log.info("Disconnecting mininet...")
371 response = ''
372 if self.handle:
373 response = self.execute(cmd="exit",prompt="(.*)",timeout=120)
374 response = self.execute(cmd="exit",prompt="(.*)",timeout=120)
375
376 else :
377 main.log.error("Connection failed to the host")
378 response = main.FALSE
379 return response
380
381 def ctrl_none(self):
382 #self.execute(cmd="sh ~/ONOS/scripts/test-ctrl-none.sh", prompt="mininet",timeout=20)
383 self.handle.sendline()
384 self.handle.expect("mininet>")
385 self.handle.sendline("sh ~/ONOS/scripts/test-ctrl-none.sh")
386 self.handle.expect("test-ctrl-none")
387 self.handle.expect("mininet>", 20)
388
389 def ctrl_all(self):
390 self.execute(cmd="sh ~/ONOS/scripts/test-ctrl-add-ext.sh", prompt="mininet",timeout=20)
391
392 def ctrl_divide(self):
393 self.execute(cmd="sh ~/ONOS/scripts/ctrl-divide.sh ", prompt="mininet",timeout=20)
394
395 def ctrl_local(self):
396 self.execute(cmd="sh ~/ONOS/scripts/test-ctrl-local.sh ", prompt="mininet",timeout=20)
397
398 def ctrl_one(self, ip):
399 self.execute(cmd="sh ~/ONOS/scripts/ctrl-one.sh "+ip, prompt="mininet",timeout=20)
admin07529932013-11-22 14:58:28 -0800400
401 def arping(self, src, dest, destmac):
402 self.handle.sendline('')
403 self.handle.expect("mininet")
404
405 self.handle.sendline(src + ' arping ' + dest)
406 try:
407 self.handle.expect(destmac)
408 main.log.info("ARP successful")
409 self.handle.expect("mininet")
410 return main.TRUE
411 except:
412 main.log.warn("ARP FAILURE")
413 self.handle.expect("mininet")
414 return main.FALSE
415
416 def decToHex(num):
417 return hex(num).split('x')[1]
adminbae64d82013-08-01 10:50:15 -0700418
419if __name__ != "__main__":
420 import sys
421 sys.modules[__name__] = MininetCliDriver()