blob: d5f4399318b6dc5e33f93ddf2e753d77b943c3a1 [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
adminf939f8b2014-04-03 17:22:56 -070064 i=self.handle.expect(['password\sfor\s','Cleanup\scomplete',pexpect.EOF,pexpect.TIMEOUT],120)
adminbae64d82013-08-01 10:50:15 -070065 if i==0:
66 main.log.info("sending sudo password")
adminf939f8b2014-04-03 17:22:56 -070067 self.handle.sendline(self.pwd)
68 i=self.handle.expect(['%s:'%(self.user),'\$',pexpect.EOF,pexpect.TIMEOUT],120)
adminbae64d82013-08-01 10:50:15 -070069 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 #### for reactive/PARP enabled tests
admin07529932013-11-22 14:58:28 -080081 cmdString = "sudo mn " + self.options['arg1'] + " " + self.options['arg2'] + " --mac --controller " + self.options['controller']
adminbeea0032014-01-23 14:54:13 -080082 #### for proactive flow with static ARP entries
83 #cmdString = "sudo mn " + self.options['arg1'] + " " + self.options['arg2'] + " --mac --arp --controller " + self.options['controller']
adminbae64d82013-08-01 10:50:15 -070084 #resultCommand = self.execute(cmd=cmdString,prompt='mininet>',timeout=120)
85 self.handle.sendline(cmdString)
86 self.handle.expect("sudo mn")
87 while 1:
88 i=self.handle.expect(['mininet>','\*\*\*','Exception',pexpect.EOF,pexpect.TIMEOUT],300)
89 if i==0:
90 main.log.info("mininet built")
91 return main.TRUE
92 if i==1:
93 self.handle.expect("\n")
94 main.log.info(self.handle.before)
95 elif i==2:
96 main.log.error("Launching mininet failed...")
97 return main.FALSE
98 elif i==3:
99 main.log.error("Connection timeout")
100 return main.FALSE
101 elif i==4: #timeout
102 main.log.error("Something took too long... " )
103 return main.FALSE
104
105 #if utilities.assert_matches(expect=patterns,actual=resultCommand,onpass="Network is being launched",onfail="Network launching is being failed "):
106 return main.TRUE
107 #else:
108 # return main.FALSE
109
110 else :
111 main.log.error("Connection failed to the host "+self.user_name+"@"+self.ip_address)
112 main.log.error("Failed to connect to the Mininet")
113 return main.FALSE
114
115 def pingall(self):
116 '''
117 Verifies the reachability of the hosts using pingall command.
118 '''
119 if self.handle :
120 main.log.info("Checking reachabilty to the hosts using pingall")
121 response = self.execute(cmd="pingall",prompt="mininet>",timeout=10)
122 pattern = 'Results\:\s0\%\sdropped\s\(0\/\d+\slost\)\s*$'
123 if utilities.assert_matches(expect=pattern,actual=response,onpass="All hosts are reaching",onfail="Unable to reach all the hosts"):
124 return main.TRUE
125 else:
126 return main.FALSE
127 else :
128 main.log.error("Connection failed to the host")
129 return main.FALSE
adminaeedddd2013-08-02 15:14:15 -0700130
131 def fpingHost(self,**pingParams):
132 '''
133 Uses the fping package for faster pinging...
134 *requires fping to be installed on machine running mininet
135 '''
136 args = utilities.parse_args(["SRC","TARGET"],**pingParams)
admin530b4c92013-08-14 16:54:35 -0700137 command = args["SRC"] + " fping -i 100 -t 20 -C 1 -q "+args["TARGET"]
adminaeedddd2013-08-02 15:14:15 -0700138 self.handle.sendline(command)
139 self.handle.expect(args["TARGET"])
140 self.handle.expect("mininet")
141 response = self.handle.before
142 if re.search(":\s-" ,response):
143 main.log.info("Ping fail")
144 return main.FALSE
admin530b4c92013-08-14 16:54:35 -0700145 elif re.search(":\s\d{1,2}\.\d\d", response):
adminaeedddd2013-08-02 15:14:15 -0700146 main.log.info("Ping good!")
147 return main.TRUE
148 main.log.info("Install fping on mininet machine... ")
149 main.log.info("\n---\n"+response)
150 return main.FALSE
adminbae64d82013-08-01 10:50:15 -0700151
152 def pingHost(self,**pingParams):
153
154 args = utilities.parse_args(["SRC","TARGET"],**pingParams)
155 #command = args["SRC"] + " ping -" + args["CONTROLLER"] + " " +args ["TARGET"]
Jon Hallf89c8552014-04-02 13:14:06 -0700156 command = args["SRC"] + " ping "+args ["TARGET"]+" -c 1 -i 1"
adminbae64d82013-08-01 10:50:15 -0700157 response = self.execute(cmd=command,prompt="mininet",timeout=10 )
Jon Hallf89c8552014-04-02 13:14:06 -0700158 main.log.info("Ping Response: "+ response )
adminbae64d82013-08-01 10:50:15 -0700159 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
adminbae64d82013-08-01 10:50:15 -0700167
168 def checkIP(self,host):
169 '''
170 Verifies the host's ip configured or not.
171 '''
172 if self.handle :
173 response = self.execute(cmd=host+" ifconfig",prompt="mininet>",timeout=10)
174
175 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})"
176 #pattern = "inet\saddr:10.0.0.6"
177 if utilities.assert_matches(expect=pattern,actual=response,onpass="Host Ip configured properly",onfail="Host IP not found") :
178 return main.TRUE
179 else:
180 return main.FALSE
181 else :
182 main.log.error("Connection failed to the host")
183
184 def verifySSH(self,**connectargs):
185 response = self.execute(cmd="h1 /usr/sbin/sshd -D&",prompt="mininet>",timeout=10)
186 response = self.execute(cmd="h4 /usr/sbin/sshd -D&",prompt="mininet>",timeout=10)
187 for key in connectargs:
188 vars(self)[key] = connectargs[key]
189 response = self.execute(cmd="xterm h1 h4 ",prompt="mininet>",timeout=10)
190 import time
191 time.sleep(20)
192 if self.flag == 0:
193 self.flag = 1
194 return main.FALSE
195 else :
196 return main.TRUE
197
198 def getMacAddress(self,host):
199 '''
200 Verifies the host's ip configured or not.
201 '''
202 if self.handle :
203 response = self.execute(cmd=host+" ifconfig",prompt="mininet>",timeout=10)
204
Ahmed El-Hassanyf720e202014-04-04 16:11:36 -0700205 pattern = r'HWaddr\s([0-9A-F]{2}[:-]){5}([0-9A-F]{2})'
206 mac_address_search = re.search(pattern, response, re.I)
207 mac_address = mac_address_search.group().split(" ")[1]
208 main.log.info("Mac-Address of Host "+ host + " is " + mac_address)
209 return mac_address
adminbae64d82013-08-01 10:50:15 -0700210 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):
Jon Hallf89c8552014-04-02 13:14:06 -0700337 '''
338 count is only needed if there is more than 1 controller
339 '''
340 args = utilities.parse_args(["COUNT"],**kwargs)
341 count = args["COUNT"] if args!={} else 1
342
343 argstring = "SW"
344 for j in range(count):
345 argstring = argstring + ",IP" + str(j+1) + ",PORT" + str(j+1)
346 args = utilities.parse_args(argstring.split(","),**kwargs)
347
adminbae64d82013-08-01 10:50:15 -0700348 sw = args["SW"] if args["SW"] != None else ""
admin530b4c92013-08-14 16:54:35 -0700349 ptcpA = int(args["PORT1"])+int(sw) if args["PORT1"] != None else ""
Jon Hallf89c8552014-04-02 13:14:06 -0700350 ptcpB = "ptcp:"+str(ptcpA) if ptcpA != "" else ""
351
352 command = "sh ovs-vsctl set-controller s" + str(sw) + " " + ptcpB + " "
353 for j in range(count):
354 i=j+1
355 args = utilities.parse_args(["IP"+str(i),"PORT"+str(i)],**kwargs)
356 ip = args["IP"+str(i)] if args["IP"+str(i)] != None else ""
357 port = args["PORT" + str(i)] if args["PORT" + str(i)] != None else ""
358 tcp = "tcp:" + str(ip) + ":" + str(port) + " " if ip != "" else ""
359 command = command + tcp
adminbae64d82013-08-01 10:50:15 -0700360 self.execute(cmd=command,prompt="mininet>",timeout=5)
361
362 def disconnect(self):
363 main.log.info("Disconnecting mininet...")
364 response = ''
365 if self.handle:
366 response = self.execute(cmd="exit",prompt="(.*)",timeout=120)
367 response = self.execute(cmd="exit",prompt="(.*)",timeout=120)
368
369 else :
370 main.log.error("Connection failed to the host")
371 response = main.FALSE
372 return response
373
374 def ctrl_none(self):
375 #self.execute(cmd="sh ~/ONOS/scripts/test-ctrl-none.sh", prompt="mininet",timeout=20)
376 self.handle.sendline()
377 self.handle.expect("mininet>")
378 self.handle.sendline("sh ~/ONOS/scripts/test-ctrl-none.sh")
379 self.handle.expect("test-ctrl-none")
380 self.handle.expect("mininet>", 20)
381
382 def ctrl_all(self):
383 self.execute(cmd="sh ~/ONOS/scripts/test-ctrl-add-ext.sh", prompt="mininet",timeout=20)
384
385 def ctrl_divide(self):
386 self.execute(cmd="sh ~/ONOS/scripts/ctrl-divide.sh ", prompt="mininet",timeout=20)
387
388 def ctrl_local(self):
389 self.execute(cmd="sh ~/ONOS/scripts/test-ctrl-local.sh ", prompt="mininet",timeout=20)
390
391 def ctrl_one(self, ip):
392 self.execute(cmd="sh ~/ONOS/scripts/ctrl-one.sh "+ip, prompt="mininet",timeout=20)
admin07529932013-11-22 14:58:28 -0800393
394 def arping(self, src, dest, destmac):
395 self.handle.sendline('')
396 self.handle.expect("mininet")
397
398 self.handle.sendline(src + ' arping ' + dest)
399 try:
400 self.handle.expect(destmac)
401 main.log.info("ARP successful")
402 self.handle.expect("mininet")
403 return main.TRUE
404 except:
405 main.log.warn("ARP FAILURE")
406 self.handle.expect("mininet")
407 return main.FALSE
408
409 def decToHex(num):
410 return hex(num).split('x')[1]
adminbae64d82013-08-01 10:50:15 -0700411
412if __name__ != "__main__":
413 import sys
414 sys.modules[__name__] = MininetCliDriver()