blob: b55a37156a32dfa6b186721f8ba5a62349416182 [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
36from time import time
37
38class RemoteMininetDriver(Emulator):
39 '''
40 MininetCliDriver is the basic driver which will handle the Mininet functions
41 '''
42 def __init__(self):
43 super(Emulator, self).__init__()
44 self.handle = self
45 self.wrapped = sys.modules[__name__]
46 self.flag = 0
47
48 def connect(self, **connectargs):
49 #,user_name, ip_address, pwd,options):
50 # Here the main is the TestON instance after creating all the log handles.
51 for key in connectargs:
52 vars(self)[key] = connectargs[key]
53
54 self.name = self.options['name']
55 self.handle = super(RemoteMininetDriver, 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
59 # Copying the readme file to process the
60 if self.handle :
61 return main.TRUE
62
63 else :
64 main.log.error("Connection failed to the host "+self.user_name+"@"+self.ip_address)
65 main.log.error("Failed to connect to the Mininet")
66 return main.FALSE
67
68 def pingLong(self,**pingParams):
69 args = utilities.parse_args(["SRC","TARGET"],**pingParams)
70 command = "mininet/util/m " + args["SRC"] + " ping "+args ["TARGET"]+" -i .1 -D -W 1 > /tmp/ping." + args["SRC"] + " &"
71 main.log.info( command )
72 self.execute(cmd=command,prompt="(.*)",timeout=10)
73 return main.TRUE
74
75 def pingstatus(self,**pingParams):
76 #self.handle.sendline("pgrep ping")
77 #self.handle.expect("pgrep")
78 #self.handle.expect("\$")
79 #result = self.handle.before + self.handle.after
80 #if re.search('\d\d+', result ):
81 # return main.TRUE
82 #else:
83 # return main.FALSE
84
85 args = utilities.parse_args(["SRC"],**pingParams)
86 self.handle.sendline("tail /tmp/ping." + args["SRC"])
87 self.handle.expect("tail")
88 self.handle.expect("\$")
89 result = self.handle.before + self.handle.after
90 if re.search('Unreachable', result ):
91 main.log.info("Unreachable found in ping logs...")
92 return main.FALSE
93 elif re.search('64\sbytes', result):
94 main.log.info("Pings look good")
95 return main.TRUE
96 else:
97 main.log.info("No, or faulty ping data...")
98 return main.FALSE
99
100 def pingKill(self):
101 command = "sudo pkill ping"
102 main.log.info( command )
103 self.execute(cmd=command,prompt="(.*)",timeout=10)
104 #command = "sudo pgrep ping"
105 #result1 = self.execute(cmd="\r",prompt="\$",timout=10)
106 #result = self.execute(cmd=command,prompt=self.user_name,timout=10)
107 #if utilities.assert_matches(expect='[^\s]+',actual=result,onpass="match",onfail="no match"):
108 # main.log.info( "pings running as process" )
109 # main.log.info( result )
110 # return main.FALSE
111 main.log.info( "Removing old ping data" )
112 command = "rm /tmp/ping.*"
113 os.popen(command)
114 import time
115 time.sleep(2)
116 main.log.info( "Transferring ping files to TestStation" )
117 command = "scp /tmp/ping.* admin@10.128.7.7:/tmp/"
118 self.execute(cmd=command,prompt="100%",timeout=20)
119 return main.TRUE
120
121 def pingHost(self,**pingParams):
122
123 args = utilities.parse_args(["SRC","TARGET"],**pingParams)
124 command = "mininet/util/m " + args["SRC"] + " ping "+args ["TARGET"]+" -c 4 -W 1 -i .2"
125 main.log.info ( command )
126 response = self.execute(cmd=command,prompt="rtt",timeout=10 )
127 if utilities.assert_matches(expect=',\s0\%\spacket\sloss',actual=response,onpass="No Packet loss",onfail="Host is not reachable"):
128 main.log.info("NO PACKET LOSS, HOST IS REACHABLE")
129 main.last_result = main.TRUE
130 return main.TRUE
131 else :
132 main.log.error("PACKET LOST, HOST IS NOT REACHABLE")
133 main.last_result = main.FALSE
134 return main.FALSE
135
136
137 def checknum(self,num):
138 '''
139 Verifies the correct number of switches are running
140 '''
141 if self.handle :
142 self.handle.sendline("")
143 self.handle.expect("\$")
144 self.handle.sendline('ifconfig -a | grep "sw.. " | wc -l')
145 self.handle.expect("wc")
146 self.handle.expect("\$")
147 response = self.handle.before
148 self.handle.sendline('ps -ef | grep "bash -ms mininet:sw" | grep -v color | wc -l')
149 self.handle.expect("color")
150 self.handle.expect("\$")
151 response2 = self.handle.before
152
153 if re.search(num, response):
154 if re.search(num, response2):
155 return main.TRUE
156 else:
157 return main.FALSE
158 else:
159 return main.FALSE
160 else :
161 main.log.error("Connection failed to the host")
162
163 def ctrl_none(self):
164 self.execute(cmd="~/ONOS/scripts/test-ctrl-none.sh", prompt="\$",timeout=10)
165
166 def ctrl_one(self, ip):
167 self.execute(cmd="~/ONOS/scripts/test-ctrl-one.sh "+ip, prompt="\$",timeout=10)
168
169 def ctrl_local(self):
170 self.execute(cmd="~/ONOS/scripts/test-ctrl-local.sh ", prompt="\$",timeout=10)
171
172 # def verifySSH(self,**connectargs):
173 # response = self.execute(cmd="h1 /usr/sbin/sshd -D&",prompt="mininet>",timeout=10)
174 # response = self.execute(cmd="h4 /usr/sbin/sshd -D&",prompt="mininet>",timeout=10)
175 # for key in connectargs:
176 # vars(self)[key] = connectargs[key]
177 # response = self.execute(cmd="xterm h1 h4 ",prompt="mininet>",timeout=10)
178 # import time
179 # time.sleep(20)
180 # if self.flag == 0:
181 # self.flag = 1
182 # return main.FALSE
183 # else :
184 # return main.TRUE
185 #
186 # def getMacAddress(self,host):
187 # '''
188 # Verifies the host's ip configured or not.
189 # '''
190 # if self.handle :
191 # response = self.execute(cmd=host+" ifconfig",prompt="mininet>",timeout=10)
192
193 # pattern = "HWaddr\s(((\d|\w)+:)+(\d|\w))"
194 # mac_address_search = re.search(pattern, response)
195 # main.log.info("Mac-Address of Host "+host +" is "+mac_address_search.group(1))
196 # return mac_address_search.group(1)
197 # else :
198 # main.log.error("Connection failed to the host")
199 # def getIPAddress(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 = "inet\saddr:(\d+\.\d+\.\d+\.\d+)"
207 # ip_address_search = re.search(pattern, response)
208 # main.log.info("IP-Address of Host "+host +" is "+ip_address_search.group(1))
209 # return ip_address_search.group(1)
210 # else :
211 # main.log.error("Connection failed to the host")
212 #
213 # def dump(self):
214 # main.log.info("Dump node info")
215 # self.execute(cmd = 'dump',prompt = 'mininet>',timeout = 10)
216 # return main.TRUE
217 #
218 # def intfs(self):
219 # main.log.info("List interfaces")
220 # self.execute(cmd = 'intfs',prompt = 'mininet>',timeout = 10)
221 # return main.TRUE
222 #
223 # def net(self):
224 # main.log.info("List network connections")
225 # self.execute(cmd = 'net',prompt = 'mininet>',timeout = 10)
226 # return main.TRUE
227 #
228 # def iperf(self):
229 # main.log.info("Simple iperf TCP test between two (optionally specified) hosts")
230 # self.execute(cmd = 'iperf',prompt = 'mininet>',timeout = 10)
231 # return main.TRUE
232 #
233 # def iperfudp(self):
234 # main.log.info("Simple iperf TCP test between two (optionally specified) hosts")
235 # self.execute(cmd = 'iperfudp',prompt = 'mininet>',timeout = 10)
236 # return main.TRUE
237 #
238 # def nodes(self):
239 # main.log.info("List all nodes.")
240 # self.execute(cmd = 'nodes',prompt = 'mininet>',timeout = 10)
241 # return main.TRUE
242 #
243 # def pingpair(self):
244 # main.log.infoe("Ping between first two hosts")
245 # self.execute(cmd = 'pingpair',prompt = 'mininet>',timeout = 20)
246 #
247 # if utilities.assert_matches(expect='0% packet loss',actual=response,onpass="No Packet loss",onfail="Hosts not reachable"):
248 # main.log.info("Ping between two hosts SUCCESS")
249 # main.last_result = main.TRUE
250 # return main.TRUE
251 # else :
252 # main.log.error("PACKET LOST, HOSTS NOT REACHABLE")
253 # main.last_result = main.FALSE
254 # return main.FALSE
255 #
256 # def link(self,**linkargs):
257 # '''
258 # Bring link(s) between two nodes up or down
259 # '''
260 # main.log.info('Bring link(s) between two nodes up or down')
261 # args = utilities.parse_args(["END1","END2","OPTION"],**linkargs)
262 # end1 = args["END1"] if args["END1"] != None else ""
263 # end2 = args["END2"] if args["END2"] != None else ""
264 # option = args["OPTION"] if args["OPTION"] != None else ""
265 # command = "link "+str(end1) + " " + str(end2)+ " " + str(option)
266 # response = self.execute(cmd=command,prompt="mininet>",timeout=10)
267 # return main.TRUE
268 #
269
270 # def dpctl(self,**dpctlargs):
271 # '''
272 # Run dpctl command on all switches.
273 # '''
274 # main.log.info('Run dpctl command on all switches')
275 # args = utilities.parse_args(["CMD","ARGS"],**dpctlargs)
276 # cmd = args["CMD"] if args["CMD"] != None else ""
277 # cmdargs = args["ARGS"] if args["ARGS"] != None else ""
278 # command = "dpctl "+cmd + " " + str(cmdargs)
279 # response = self.execute(cmd=command,prompt="mininet>",timeout=10)
280 # return main.TRUE
281 #
282 #
283 # def get_version(self):
284 # file_input = path+'/lib/Mininet/INSTALL'
285 # version = super(Mininet, self).get_version()
286 # pattern = 'Mininet\s\w\.\w\.\w\w*'
287 # for line in open(file_input,'r').readlines():
288 # result = re.match(pattern, line)
289 # if result:
290 # version = result.group(0)
291 #
292 #
293 # return version
294
295 def disconnect(self):
296
297 response = ''
298 #print "Disconnecting Mininet"
299 if self.handle:
300 self.handle.sendline("exit")
301 self.handle.expect("exit")
302 self.handle.expect("(.*)")
303 response = self.handle.before
304
305 else :
306 main.log.error("Connection failed to the host")
307 response = main.FALSE
308 return response
309
310if __name__ != "__main__":
311 import sys
312 sys.modules[__name__] = RemoteMininetDriver()