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