blob: aa02ed162eddc03e863400bfb2098e323b8591ed [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")
80 cmdString = "sudo mn " + self.options['arg1'] + " " + self.options['arg2'] + " --arp --mac --controller " + self.options['controller']
81 #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
127
128 def pingHost(self,**pingParams):
129
130 args = utilities.parse_args(["SRC","TARGET"],**pingParams)
131 #command = args["SRC"] + " ping -" + args["CONTROLLER"] + " " +args ["TARGET"]
132 command = args["SRC"] + " ping "+args ["TARGET"]+" -c 2 -i .2"
133 response = self.execute(cmd=command,prompt="mininet",timeout=10 )
134 if utilities.assert_matches(expect=',\s0\%\spacket\sloss',actual=response,onpass="No Packet loss",onfail="Host is not reachable"):
135 main.log.info("NO PACKET LOSS, HOST IS REACHABLE")
136 main.last_result = main.TRUE
137 return main.TRUE
138 else :
139 main.log.error("PACKET LOST, HOST IS NOT REACHABLE")
140 main.last_result = main.FALSE
141 return main.FALSE
142
143
144 def checkIP(self,host):
145 '''
146 Verifies the host's ip configured or not.
147 '''
148 if self.handle :
149 response = self.execute(cmd=host+" ifconfig",prompt="mininet>",timeout=10)
150
151 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})"
152 #pattern = "inet\saddr:10.0.0.6"
153 if utilities.assert_matches(expect=pattern,actual=response,onpass="Host Ip configured properly",onfail="Host IP not found") :
154 return main.TRUE
155 else:
156 return main.FALSE
157 else :
158 main.log.error("Connection failed to the host")
159
160 def verifySSH(self,**connectargs):
161 response = self.execute(cmd="h1 /usr/sbin/sshd -D&",prompt="mininet>",timeout=10)
162 response = self.execute(cmd="h4 /usr/sbin/sshd -D&",prompt="mininet>",timeout=10)
163 for key in connectargs:
164 vars(self)[key] = connectargs[key]
165 response = self.execute(cmd="xterm h1 h4 ",prompt="mininet>",timeout=10)
166 import time
167 time.sleep(20)
168 if self.flag == 0:
169 self.flag = 1
170 return main.FALSE
171 else :
172 return main.TRUE
173
174 def getMacAddress(self,host):
175 '''
176 Verifies the host's ip configured or not.
177 '''
178 if self.handle :
179 response = self.execute(cmd=host+" ifconfig",prompt="mininet>",timeout=10)
180
181 pattern = "HWaddr\s(((\d|\w)+:)+(\d|\w))"
182 mac_address_search = re.search(pattern, response)
183 main.log.info("Mac-Address of Host "+host +" is "+mac_address_search.group(1))
184 return mac_address_search.group(1)
185 else :
186 main.log.error("Connection failed to the host")
187 def getIPAddress(self,host):
188 '''
189 Verifies the host's ip configured or not.
190 '''
191 if self.handle :
192 response = self.execute(cmd=host+" ifconfig",prompt="mininet>",timeout=10)
193
194 pattern = "inet\saddr:(\d+\.\d+\.\d+\.\d+)"
195 ip_address_search = re.search(pattern, response)
196 main.log.info("IP-Address of Host "+host +" is "+ip_address_search.group(1))
197 return ip_address_search.group(1)
198 else :
199 main.log.error("Connection failed to the host")
200
201 def dump(self):
202 main.log.info("Dump node info")
203 self.execute(cmd = 'dump',prompt = 'mininet>',timeout = 10)
204 return main.TRUE
205
206 def intfs(self):
207 main.log.info("List interfaces")
208 self.execute(cmd = 'intfs',prompt = 'mininet>',timeout = 10)
209 return main.TRUE
210
211 def net(self):
212 main.log.info("List network connections")
213 self.execute(cmd = 'net',prompt = 'mininet>',timeout = 10)
214 return main.TRUE
215
216 def iperf(self):
217 main.log.info("Simple iperf TCP test between two (optionally specified) hosts")
218 self.execute(cmd = 'iperf',prompt = 'mininet>',timeout = 10)
219 return main.TRUE
220
221 def iperfudp(self):
222 main.log.info("Simple iperf TCP test between two (optionally specified) hosts")
223 self.execute(cmd = 'iperfudp',prompt = 'mininet>',timeout = 10)
224 return main.TRUE
225
226 def nodes(self):
227 main.log.info("List all nodes.")
228 self.execute(cmd = 'nodes',prompt = 'mininet>',timeout = 10)
229 return main.TRUE
230
231 def pingpair(self):
232 main.log.infoe("Ping between first two hosts")
233 self.execute(cmd = 'pingpair',prompt = 'mininet>',timeout = 20)
234
235 if utilities.assert_matches(expect='0% packet loss',actual=response,onpass="No Packet loss",onfail="Hosts not reachable"):
236 main.log.info("Ping between two hosts SUCCESS")
237 main.last_result = main.TRUE
238 return main.TRUE
239 else :
240 main.log.error("PACKET LOST, HOSTS NOT REACHABLE")
241 main.last_result = main.FALSE
242 return main.FALSE
243
244 def link(self,**linkargs):
245 '''
246 Bring link(s) between two nodes up or down
247 '''
248 main.log.info('Bring link(s) between two nodes up or down')
249 args = utilities.parse_args(["END1","END2","OPTION"],**linkargs)
250 end1 = args["END1"] if args["END1"] != None else ""
251 end2 = args["END2"] if args["END2"] != None else ""
252 option = args["OPTION"] if args["OPTION"] != None else ""
253 command = "link "+str(end1) + " " + str(end2)+ " " + str(option)
254 response = self.execute(cmd=command,prompt="mininet>",timeout=10)
255 return main.TRUE
256
257
258 def dpctl(self,**dpctlargs):
259 '''
260 Run dpctl command on all switches.
261 '''
262 main.log.info('Run dpctl command on all switches')
263 args = utilities.parse_args(["CMD","ARGS"],**dpctlargs)
264 cmd = args["CMD"] if args["CMD"] != None else ""
265 cmdargs = args["ARGS"] if args["ARGS"] != None else ""
266 command = "dpctl "+cmd + " " + str(cmdargs)
267 response = self.execute(cmd=command,prompt="mininet>",timeout=10)
268 return main.TRUE
269
270
271 def get_version(self):
272 file_input = path+'/lib/Mininet/INSTALL'
273 version = super(Mininet, self).get_version()
274 pattern = 'Mininet\s\w\.\w\.\w\w*'
275 for line in open(file_input,'r').readlines():
276 result = re.match(pattern, line)
277 if result:
278 version = result.group(0)
279 return version
280
281 def get_sw_controller(self,sw):
282 command = "sh ovs-vsctl get-controller "+str(sw)
283 main.log.info(self.execute(cmd=command,prompt="mininet>",timeout=10))
284
285 def assign_sw_controller(self,**kwargs):
286 args = utilities.parse_args(["SW","IP1","PORT1","IP2","PORT2","IP3","PORT3","IP4","PORT4","IP5","PORT5","IP6","PORT6","IP7","PORT7","IP8","PORT8"],**kwargs)
287 sw = args["SW"] if args["SW"] != None else ""
288 ip1 = args["IP1"] if args["IP1"] != None else ""
289 ip2 = args["IP2"] if args["IP2"] != None else ""
290 ip3 = args["IP3"] if args["IP3"] != None else ""
291 ip4 = args["IP4"] if args["IP4"] != None else ""
292 ip5 = args["IP5"] if args["IP5"] != None else ""
293 ip6 = args["IP6"] if args["IP6"] != None else ""
294 ip7 = args["IP7"] if args["IP7"] != None else ""
295 ip8 = args["IP8"] if args["IP8"] != None else ""
296 # master = args["MASTER"] if args["MASTER"] != None else ""
297 port1 = args["PORT1"] if args["PORT1"] != None else ""
298 port2 = args["PORT2"] if args["PORT2"] != None else ""
299 port3 = args["PORT3"] if args["PORT3"] != None else ""
300 port4 = args["PORT4"] if args["PORT4"] != None else ""
301 port5 = args["PORT5"] if args["PORT5"] != None else ""
302 port6 = args["PORT6"] if args["PORT6"] != None else ""
303 port7 = args["PORT7"] if args["PORT7"] != None else ""
304 port8 = args["PORT8"] if args["PORT8"] != None else ""
305 tcp1 = "tcp:"+str(ip1)+":"+str(port1) if ip1 != "" else ""
306 tcp2 = "tcp:"+str(ip2)+":"+str(port2) if ip2 != "" else ""
307 tcp3 = "tcp:"+str(ip3)+":"+str(port3) if ip3 != "" else ""
308 tcp4 = "tcp:"+str(ip4)+":"+str(port4) if ip4 != "" else ""
309 tcp5 = "tcp:"+str(ip5)+":"+str(port5) if ip5 != "" else ""
310 tcp6 = "tcp:"+str(ip6)+":"+str(port6) if ip6 != "" else ""
311 tcp7 = "tcp:"+str(ip7)+":"+str(port7) if ip7 != "" else ""
312 tcp8 = "tcp:"+str(ip8)+":"+str(port8) if ip8 != "" else ""
313 # master1 = tcp1+" role master " if args["MASTER"] == 1 else ""
314 # master2 = tcp2+" role master " if args["MASTER"] == 2 else ""
315 # master3 = tcp3+" role master " if args["MASTER"] == 3 else ""
316 # master4 = tcp4+" role master " if args["MASTER"] == 4 else ""
317 command = "sh ovs-vsctl set-controller "+str(sw)+" "+tcp1+" "+tcp2+" "+tcp3+" "+tcp4+" "+tcp5+" "+tcp6+" "+tcp7+" "+tcp8
318 self.execute(cmd=command,prompt="mininet>",timeout=5)
319
320 def disconnect(self):
321 main.log.info("Disconnecting mininet...")
322 response = ''
323 if self.handle:
324 response = self.execute(cmd="exit",prompt="(.*)",timeout=120)
325 response = self.execute(cmd="exit",prompt="(.*)",timeout=120)
326
327 else :
328 main.log.error("Connection failed to the host")
329 response = main.FALSE
330 return response
331
332 def ctrl_none(self):
333 #self.execute(cmd="sh ~/ONOS/scripts/test-ctrl-none.sh", prompt="mininet",timeout=20)
334 self.handle.sendline()
335 self.handle.expect("mininet>")
336 self.handle.sendline("sh ~/ONOS/scripts/test-ctrl-none.sh")
337 self.handle.expect("test-ctrl-none")
338 self.handle.expect("mininet>", 20)
339
340 def ctrl_all(self):
341 self.execute(cmd="sh ~/ONOS/scripts/test-ctrl-add-ext.sh", prompt="mininet",timeout=20)
342
343 def ctrl_divide(self):
344 self.execute(cmd="sh ~/ONOS/scripts/ctrl-divide.sh ", prompt="mininet",timeout=20)
345
346 def ctrl_local(self):
347 self.execute(cmd="sh ~/ONOS/scripts/test-ctrl-local.sh ", prompt="mininet",timeout=20)
348
349 def ctrl_one(self, ip):
350 self.execute(cmd="sh ~/ONOS/scripts/ctrl-one.sh "+ip, prompt="mininet",timeout=20)
351
352if __name__ != "__main__":
353 import sys
354 sys.modules[__name__] = MininetCliDriver()