blob: 3159dca608cea463e36c87fb6b670892a45e2afb [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 :
210 main.log.error("Connection failed to the host")
211 def getIPAddress(self,host):
212 '''
Jon Hall41f40e82014-04-08 16:43:17 -0700213 Verifies the host's ip configured or not.
adminbae64d82013-08-01 10:50:15 -0700214 '''
215 if self.handle :
216 response = self.execute(cmd=host+" ifconfig",prompt="mininet>",timeout=10)
217
218 pattern = "inet\saddr:(\d+\.\d+\.\d+\.\d+)"
219 ip_address_search = re.search(pattern, response)
220 main.log.info("IP-Address of Host "+host +" is "+ip_address_search.group(1))
221 return ip_address_search.group(1)
222 else :
223 main.log.error("Connection failed to the host")
224
225 def dump(self):
226 main.log.info("Dump node info")
Ahmed El-Hassanyd1f71702014-04-04 16:12:45 -0700227 response = self.execute(cmd = 'dump',prompt = 'mininet>',timeout = 10)
228 return response
adminbae64d82013-08-01 10:50:15 -0700229
230 def intfs(self):
231 main.log.info("List interfaces")
Jon Hall668ed802014-04-08 17:17:59 -0700232 response = self.execute(cmd = 'intfs',prompt = 'mininet>',timeout = 10)
233 return response
adminbae64d82013-08-01 10:50:15 -0700234
235 def net(self):
236 main.log.info("List network connections")
Jon Hall668ed802014-04-08 17:17:59 -0700237 response = self.execute(cmd = 'net',prompt = 'mininet>',timeout = 10)
238 return response
adminbae64d82013-08-01 10:50:15 -0700239
240 def iperf(self):
241 main.log.info("Simple iperf TCP test between two (optionally specified) hosts")
Jon Hall668ed802014-04-08 17:17:59 -0700242 response = self.execute(cmd = 'iperf',prompt = 'mininet>',timeout = 10)
243 return response
adminbae64d82013-08-01 10:50:15 -0700244
245 def iperfudp(self):
246 main.log.info("Simple iperf TCP test between two (optionally specified) hosts")
Jon Hall668ed802014-04-08 17:17:59 -0700247 response = self.execute(cmd = 'iperfudp',prompt = 'mininet>',timeout = 10)
248 return response
adminbae64d82013-08-01 10:50:15 -0700249
250 def nodes(self):
251 main.log.info("List all nodes.")
Jon Hall668ed802014-04-08 17:17:59 -0700252 response = self.execute(cmd = 'nodes',prompt = 'mininet>',timeout = 10)
253 return response
adminbae64d82013-08-01 10:50:15 -0700254
255 def pingpair(self):
256 main.log.infoe("Ping between first two hosts")
257 self.execute(cmd = 'pingpair',prompt = 'mininet>',timeout = 20)
258
259 if utilities.assert_matches(expect='0% packet loss',actual=response,onpass="No Packet loss",onfail="Hosts not reachable"):
260 main.log.info("Ping between two hosts SUCCESS")
261 main.last_result = main.TRUE
262 return main.TRUE
263 else :
264 main.log.error("PACKET LOST, HOSTS NOT REACHABLE")
265 main.last_result = main.FALSE
266 return main.FALSE
267
268 def link(self,**linkargs):
269 '''
270 Bring link(s) between two nodes up or down
271 '''
272 main.log.info('Bring link(s) between two nodes up or down')
273 args = utilities.parse_args(["END1","END2","OPTION"],**linkargs)
274 end1 = args["END1"] if args["END1"] != None else ""
275 end2 = args["END2"] if args["END2"] != None else ""
276 option = args["OPTION"] if args["OPTION"] != None else ""
277 command = "link "+str(end1) + " " + str(end2)+ " " + str(option)
278 response = self.execute(cmd=command,prompt="mininet>",timeout=10)
279 return main.TRUE
280
281
admin530b4c92013-08-14 16:54:35 -0700282 def yank(self,**yankargs):
adminaeedddd2013-08-02 15:14:15 -0700283 '''
admin530b4c92013-08-14 16:54:35 -0700284 yank a mininet switch interface to a host
adminaeedddd2013-08-02 15:14:15 -0700285 '''
admin530b4c92013-08-14 16:54:35 -0700286 main.log.info('Yank the switch interface attached to a host')
287 args = utilities.parse_args(["SW","INTF"],**yankargs)
adminaeedddd2013-08-02 15:14:15 -0700288 sw = args["SW"] if args["SW"] !=None else ""
289 intf = args["INTF"] if args["INTF"] != None else ""
290 command = "py "+ str(sw) + '.detach("' + str(intf) + '")'
291 response = self.execute(cmd=command,prompt="mininet>",timeout=10)
292 return main.TRUE
293
294 def plug(self, **plugargs):
295 '''
296 plug the yanked mininet switch interface to a switch
297 '''
298 main.log.info('Plug the switch interface attached to a switch')
admin530b4c92013-08-14 16:54:35 -0700299 args = utilities.parse_args(["SW","INTF"],**plugargs)
adminaeedddd2013-08-02 15:14:15 -0700300 sw = args["SW"] if args["SW"] !=None else ""
301 intf = args["INTF"] if args["INTF"] != None else ""
302 command = "py "+ str(sw) + '.attach("' + str(intf) + '")'
303 response = self.execute(cmd=command,prompt="mininet>",timeout=10)
304 return main.TRUE
305
306
307
adminbae64d82013-08-01 10:50:15 -0700308 def dpctl(self,**dpctlargs):
309 '''
Jon Hall41f40e82014-04-08 16:43:17 -0700310 Run dpctl command on all switches.
adminbae64d82013-08-01 10:50:15 -0700311 '''
312 main.log.info('Run dpctl command on all switches')
313 args = utilities.parse_args(["CMD","ARGS"],**dpctlargs)
314 cmd = args["CMD"] if args["CMD"] != None else ""
315 cmdargs = args["ARGS"] if args["ARGS"] != None else ""
316 command = "dpctl "+cmd + " " + str(cmdargs)
317 response = self.execute(cmd=command,prompt="mininet>",timeout=10)
318 return main.TRUE
319
320
321 def get_version(self):
322 file_input = path+'/lib/Mininet/INSTALL'
323 version = super(Mininet, self).get_version()
324 pattern = 'Mininet\s\w\.\w\.\w\w*'
325 for line in open(file_input,'r').readlines():
326 result = re.match(pattern, line)
327 if result:
328 version = result.group(0)
329 return version
330
331 def get_sw_controller(self,sw):
332 command = "sh ovs-vsctl get-controller "+str(sw)
333 main.log.info(self.execute(cmd=command,prompt="mininet>",timeout=10))
334
335 def assign_sw_controller(self,**kwargs):
Jon Hallf89c8552014-04-02 13:14:06 -0700336 '''
337 count is only needed if there is more than 1 controller
338 '''
339 args = utilities.parse_args(["COUNT"],**kwargs)
340 count = args["COUNT"] if args!={} else 1
341
342 argstring = "SW"
343 for j in range(count):
344 argstring = argstring + ",IP" + str(j+1) + ",PORT" + str(j+1)
345 args = utilities.parse_args(argstring.split(","),**kwargs)
346
adminbae64d82013-08-01 10:50:15 -0700347 sw = args["SW"] if args["SW"] != None else ""
admin530b4c92013-08-14 16:54:35 -0700348 ptcpA = int(args["PORT1"])+int(sw) if args["PORT1"] != None else ""
Jon Hallf89c8552014-04-02 13:14:06 -0700349 ptcpB = "ptcp:"+str(ptcpA) if ptcpA != "" else ""
350
351 command = "sh ovs-vsctl set-controller s" + str(sw) + " " + ptcpB + " "
352 for j in range(count):
353 i=j+1
354 args = utilities.parse_args(["IP"+str(i),"PORT"+str(i)],**kwargs)
355 ip = args["IP"+str(i)] if args["IP"+str(i)] != None else ""
356 port = args["PORT" + str(i)] if args["PORT" + str(i)] != None else ""
357 tcp = "tcp:" + str(ip) + ":" + str(port) + " " if ip != "" else ""
358 command = command + tcp
adminbae64d82013-08-01 10:50:15 -0700359 self.execute(cmd=command,prompt="mininet>",timeout=5)
360
361 def disconnect(self):
362 main.log.info("Disconnecting mininet...")
363 response = ''
364 if self.handle:
365 response = self.execute(cmd="exit",prompt="(.*)",timeout=120)
366 response = self.execute(cmd="exit",prompt="(.*)",timeout=120)
367
368 else :
369 main.log.error("Connection failed to the host")
370 response = main.FALSE
371 return response
372
373 def ctrl_none(self):
374 #self.execute(cmd="sh ~/ONOS/scripts/test-ctrl-none.sh", prompt="mininet",timeout=20)
375 self.handle.sendline()
376 self.handle.expect("mininet>")
377 self.handle.sendline("sh ~/ONOS/scripts/test-ctrl-none.sh")
378 self.handle.expect("test-ctrl-none")
379 self.handle.expect("mininet>", 20)
380
381 def ctrl_all(self):
382 self.execute(cmd="sh ~/ONOS/scripts/test-ctrl-add-ext.sh", prompt="mininet",timeout=20)
383
384 def ctrl_divide(self):
385 self.execute(cmd="sh ~/ONOS/scripts/ctrl-divide.sh ", prompt="mininet",timeout=20)
386
387 def ctrl_local(self):
388 self.execute(cmd="sh ~/ONOS/scripts/test-ctrl-local.sh ", prompt="mininet",timeout=20)
389
390 def ctrl_one(self, ip):
391 self.execute(cmd="sh ~/ONOS/scripts/ctrl-one.sh "+ip, prompt="mininet",timeout=20)
admin07529932013-11-22 14:58:28 -0800392
393 def arping(self, src, dest, destmac):
394 self.handle.sendline('')
395 self.handle.expect("mininet")
396
397 self.handle.sendline(src + ' arping ' + dest)
398 try:
399 self.handle.expect(destmac)
400 main.log.info("ARP successful")
401 self.handle.expect("mininet")
402 return main.TRUE
403 except:
404 main.log.warn("ARP FAILURE")
405 self.handle.expect("mininet")
406 return main.FALSE
407
408 def decToHex(num):
409 return hex(num).split('x')[1]
adminbae64d82013-08-01 10:50:15 -0700410
411if __name__ != "__main__":
412 import sys
413 sys.modules[__name__] = MininetCliDriver()