blob: 41284cbdfb5f0c6beae53b800541da030dca6033 [file] [log] [blame]
adminbae64d82013-08-01 10:50:15 -07001#!/usr/bin/env python
kelvin-onlabedcff052015-01-16 12:53:55 -08002"""
adminbae64d82013-08-01 10:50:15 -07003Created on 26-Oct-2012
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00004Copyright 2012 Open Networking Foundation (ONF)
adminbae64d82013-08-01 10:50:15 -07005
Jeremy Songsterae01bba2016-07-11 15:39:17 -07006Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>,
7the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>,
8or the System Testing Guide page at <https://wiki.onosproject.org/x/WYQg>
adminbae64d82013-08-01 10:50:15 -07009
10 TestON is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation, either version 2 of the License, or
kelvin-onlabedcff052015-01-16 12:53:55 -080013 ( at your option ) any later version.
adminbae64d82013-08-01 10:50:15 -070014
15 TestON is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
Jon Hallfbc828e2015-01-06 17:30:19 -080021 along with TestON. If not, see <http://www.gnu.org/licenses/>.
adminbae64d82013-08-01 10:50:15 -070022
23
24MininetCliDriver is the basic driver which will handle the Mininet functions
kelvin-onlabedcff052015-01-16 12:53:55 -080025"""
adminbae64d82013-08-01 10:50:15 -070026import pexpect
adminbae64d82013-08-01 10:50:15 -070027import re
28import sys
kelvin-onlaba4074292015-07-09 15:19:49 -070029import os
adminbae64d82013-08-01 10:50:15 -070030from drivers.common.cli.emulatordriver import Emulator
adminbae64d82013-08-01 10:50:15 -070031
kelvin-onlabedcff052015-01-16 12:53:55 -080032
33class RemoteMininetDriver( Emulator ):
34
35 """
kelvin-onlabd3b64892015-01-20 13:26:24 -080036 RemoteMininetCliDriver is the basic driver which will handle the Mininet
37 functions. The main different between this and the MininetCliDriver is that
38 this one does not build the mininet. It assumes that there is already a
39 mininet running on the target.
kelvin-onlabedcff052015-01-16 12:53:55 -080040 """
41 def __init__( self ):
Devin Limdc78e202017-06-09 18:30:07 -070042 super( RemoteMininetDriver, self ).__init__()
adminbae64d82013-08-01 10:50:15 -070043 self.handle = self
Jon Hallefbd9792015-03-05 16:11:36 -080044 self.name = None
kelvin-onlabedcff052015-01-16 12:53:55 -080045 self.wrapped = sys.modules[ __name__ ]
adminbae64d82013-08-01 10:50:15 -070046 self.flag = 0
47
kelvin-onlabedcff052015-01-16 12:53:55 -080048 def connect( self, **connectargs ):
kelvin-onlab08679eb2015-01-21 16:11:48 -080049 """,user_name, ip_address, pwd,options ):
kelvin-onlabd3b64892015-01-20 13:26:24 -080050 Here the main is the TestON instance after creating all the log
51 handles."""
kelvin-onlaba4074292015-07-09 15:19:49 -070052 try:
Ming Yan Shu404f7e72016-07-22 14:37:03 -070053 for key in connectargs:
54 vars( self )[ key ] = connectargs[ key ]
55
56 self.name = self.options[ 'name' ]
57
58 try:
Jeremy Ronquillo82705492017-10-18 14:19:55 -070059 if os.getenv( str( self.ip_address ) ) is not None:
Ming Yan Shu404f7e72016-07-22 14:37:03 -070060 self.ip_address = os.getenv( str( self.ip_address ) )
61 else:
62 main.log.info( self.name +
63 ": Trying to connect to " +
64 self.ip_address )
65
66 except KeyError:
67 main.log.info( "Invalid host name," +
68 " connecting to local host instead" )
69 self.ip_address = 'localhost'
70 except Exception as inst:
71 main.log.error( "Uncaught exception: " + str( inst ) )
72
73 self.handle = super(
74 RemoteMininetDriver,
75 self ).connect(
76 user_name=self.user_name,
77 ip_address=self.ip_address,
78 port=None,
79 pwd=self.pwd )
80
81 # Copying the readme file to process the
82 if self.handle:
83 return main.TRUE
84
kelvin-onlaba4074292015-07-09 15:19:49 -070085 else:
Ming Yan Shu404f7e72016-07-22 14:37:03 -070086 main.log.error(
87 "Connection failed to the host " +
88 self.user_name +
89 "@" +
90 self.ip_address )
91 main.log.error( "Failed to connect to the Mininet" )
92 return main.FALSE
93 except Exception:
94 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -070095 main.cleanAndExit()
admin98ad0092014-07-23 16:51:07 -070096
kelvin-onlabedcff052015-01-16 12:53:55 -080097 def checkForLoss( self, pingList ):
98 """
Jon Hall6c794f32014-08-14 13:33:13 -070099 Returns main.FALSE for 0% packet loss and
100 Returns main.ERROR if "found multiple mininet" is found and
101 Returns main.TRUE else
kelvin-onlabedcff052015-01-16 12:53:55 -0800102 """
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700103 try:
104 self.handle.sendline( "" )
Devin Limdc78e202017-06-09 18:30:07 -0700105 self.handle.expect( self.prompt )
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700106 self.handle.sendline( "" )
Devin Limdc78e202017-06-09 18:30:07 -0700107 self.handle.expect( self.prompt )
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700108 self.handle.sendline( "cat " + pingList )
109 self.handle.expect( pingList )
Devin Limdc78e202017-06-09 18:30:07 -0700110 self.handle.expect( self.prompt )
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700111 outputs = self.handle.before + self.handle.after
112 if re.search( " 0% packet loss", outputs ):
113 return main.FALSE
114 elif re.search( "found multiple mininet", outputs ):
115 return main.ERROR
116 else:
117 # TODO: Parse for failed pings, give some truncated output
118 main.log.error( "Error, unexpected output in the ping file" )
119 main.log.warn( outputs )
120 return main.TRUE
121 except pexpect.TIMEOUT:
122 main.log.exception( self.name + ": TIMEOUT exception found in checkForLoss" )
123 main.log.error( self.name + ": " + self.handle.before )
admin98ad0092014-07-23 16:51:07 -0700124 return main.FALSE
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700125 except pexpect.EOF:
126 main.log.error( self.name + ": EOF exception found" )
127 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700128 main.cleanAndExit()
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700129 except Exception:
130 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700131 main.cleanAndExit()
shahshreyaf4d4d0c2014-10-10 12:11:10 -0700132
kelvin-onlabedcff052015-01-16 12:53:55 -0800133 def pingLong( self, **pingParams ):
134 """
Jon Hallefbd9792015-03-05 16:11:36 -0800135 Starts a continuous ping on the mininet host outputting
kelvin-onlabd3b64892015-01-20 13:26:24 -0800136 to a file in the /tmp dir.
kelvin-onlabedcff052015-01-16 12:53:55 -0800137 """
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700138 try:
139 self.handle.sendline( "" )
Devin Limdc78e202017-06-09 18:30:07 -0700140 self.handle.expect( self.prompt )
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700141 args = utilities.parse_args(
142 [ "SRC", "TARGET", "PINGTIME" ], **pingParams )
143 precmd = "sudo rm /tmp/ping." + args[ "SRC" ]
144 self.execute( cmd=precmd, prompt="(.*)", timeout=10 )
145 command = "sudo mininet/util/m " + args[ "SRC" ] + " ping " +\
146 args[ "TARGET" ] + " -i .2 -w " +\
147 str( args[ 'PINGTIME' ] ) + " -D > /tmp/ping." +\
148 args[ "SRC" ] + " &"
149 main.log.info( command )
150 self.execute( cmd=command, prompt="(.*)", timeout=10 )
151 self.handle.sendline( "" )
Devin Limdc78e202017-06-09 18:30:07 -0700152 self.handle.expect( self.prompt )
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700153 return main.TRUE
154 except TypeError:
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700155 main.log.exception( self.name + ": Object not as expected" )
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700156 return main.FALSE
157 except pexpect.TIMEOUT:
158 main.log.exception( self.name + ": TIMEOUT exception found in pingLong" )
159 main.log.error( self.name + ": " + self.handle.before )
160 return main.FALSE
161 except pexpect.EOF:
162 main.log.error( self.name + ": EOF exception found" )
163 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700164 main.cleanAndExit()
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700165 except Exception:
166 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700167 main.cleanAndExit()
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700168
kelvin-onlabedcff052015-01-16 12:53:55 -0800169 def pingstatus( self, **pingParams ):
170 """
kelvin-onlabd3b64892015-01-20 13:26:24 -0800171 Tails the respective ping output file and check that
172 there is a moving "64 bytes"
kelvin-onlabedcff052015-01-16 12:53:55 -0800173 """
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700174 try:
175 self.handle.sendline( "" )
Devin Limdc78e202017-06-09 18:30:07 -0700176 self.handle.expect( self.prompt )
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700177 args = utilities.parse_args( [ "SRC" ], **pingParams )
178 self.handle.sendline( "tail /tmp/ping." + args[ "SRC" ] )
179 self.handle.expect( "tail" )
Devin Limdc78e202017-06-09 18:30:07 -0700180 self.handle.expect( self.prompt )
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700181 result = self.handle.before + self.handle.after
182 self.handle.sendline( "" )
Devin Limdc78e202017-06-09 18:30:07 -0700183 self.handle.expect( self.prompt )
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700184 if re.search( 'Unreachable', result ):
185 main.log.info( "Unreachable found in ping logs..." )
186 return main.FALSE
187 elif re.search( '64\sbytes', result ):
188 main.log.info( "Pings look good" )
189 return main.TRUE
190 else:
191 main.log.info( "No, or faulty ping data..." )
192 return main.FALSE
193 except TypeError:
194 main.log.exception( self.name + ": Object not as expected" )
adminbae64d82013-08-01 10:50:15 -0700195 return main.FALSE
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700196 except pexpect.TIMEOUT:
197 main.log.exception( self.name + ": TIMEOUT exception found in pingstatus" )
198 main.log.error( self.name + ": " + self.handle.before )
adminbae64d82013-08-01 10:50:15 -0700199 return main.FALSE
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700200 except pexpect.EOF:
201 main.log.error( self.name + ": EOF exception found" )
202 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700203 main.cleanAndExit()
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700204 except Exception:
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700205 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700206 main.cleanAndExit()
Jon Hallfbc828e2015-01-06 17:30:19 -0800207
kelvin-onlabedcff052015-01-16 12:53:55 -0800208 def pingKill( self, testONUser, testONIP ):
209 """
adminaeedddd2013-08-02 15:14:15 -0700210 Kills all continuous ping processes.
211 Then copies all the ping files to the TestStation.
kelvin-onlabedcff052015-01-16 12:53:55 -0800212 """
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700213 try:
214 self.handle.sendline( "" )
Devin Limdc78e202017-06-09 18:30:07 -0700215 self.handle.expect( self.prompt )
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700216 command = "sudo kill -SIGINT `pgrep ping`"
217 main.log.info( command )
218 self.execute( cmd=command, prompt="(.*)", timeout=10 )
Jon Hallfbc828e2015-01-06 17:30:19 -0800219
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700220 main.log.info( "Transferring ping files to TestStation" )
221 command = "scp /tmp/ping.* " + \
222 str( testONUser ) + "@" + str( testONIP ) + ":/tmp/"
223 self.execute( cmd=command, prompt="100%", timeout=20 )
224 # Make sure the output is cleared
225 self.handle.sendline( "" )
Devin Limdc78e202017-06-09 18:30:07 -0700226 self.handle.expect( self.prompt )
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700227 self.handle.sendline( "" )
Devin Limdc78e202017-06-09 18:30:07 -0700228 self.handle.expect( self.prompt )
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700229 self.handle.sendline( "" )
Devin Limdc78e202017-06-09 18:30:07 -0700230 i = self.handle.expect( [ "password", self.prompt ] )
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700231 if i == 0:
232 main.log.error( "Error, sudo asking for password" )
233 main.log.error( self.handle.before )
234 return main.FALSE
235 else:
236 return main.TRUE
237 except pexpect.TIMEOUT:
238 main.log.error( self.name + ": TIMEOUT exception found in pingKill" )
239 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700240 main.cleanAndExit()
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700241 except pexpect.EOF:
242 main.log.error( self.name + ": EOF exception found" )
243 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700244 main.cleanAndExit()
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700245 except Exception:
246 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700247 main.cleanAndExit()
Jon Hallfbc828e2015-01-06 17:30:19 -0800248
kelvin-onlabedcff052015-01-16 12:53:55 -0800249 def pingLongKill( self ):
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700250 try:
251 self.handle.sendline( "" )
Devin Limdc78e202017-06-09 18:30:07 -0700252 self.handle.expect( self.prompt )
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700253 command = "sudo kill -SIGING `pgrep ping`"
254 main.log.info( command )
255 self.execute( cmd=command, prompt="(.*)", timeout=10 )
256 self.handle.sendline( "" )
Devin Limdc78e202017-06-09 18:30:07 -0700257 self.handle.expect( self.prompt )
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700258 return main.TRUE
259 except pexpect.TIMEOUT:
260 main.log.exception( self.name + ": TIMEOUT exception found in pingLongKill" )
261 main.log.error( self.name + ": " + self.handle.before )
262 return main.FALSE
263 except pexpect.EOF:
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700264 main.log.error( self.name + ": EOF exception found" )
265 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700266 main.cleanAndExit()
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700267 except Exception:
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700268 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700269 main.cleanAndExit()
Jon Hallfbc828e2015-01-06 17:30:19 -0800270
kelvin-onlabedcff052015-01-16 12:53:55 -0800271 def pingHostOptical( self, **pingParams ):
272 """
Jon Hallefbd9792015-03-05 16:11:36 -0800273 This function is only for Packet Optical related ping
kelvin-onlabedcff052015-01-16 12:53:55 -0800274 Use the next pingHost() function for all normal scenarios )
shahshreya28bb18e2014-11-17 10:26:23 -0800275 Ping from one mininet host to another
276 Currently the only supported Params: SRC and TARGET
kelvin-onlabedcff052015-01-16 12:53:55 -0800277 """
kelvin-onlab7d0c9672015-01-20 15:56:22 -0800278 args = utilities.parse_args( [ "SRC", "TARGET" ], **pingParams )
kelvin-onlabedcff052015-01-16 12:53:55 -0800279 command = args[ "SRC" ] + " ping " + \
280 args[ "TARGET" ] + " -c 1 -i 1 -W 8"
shahshreya28bb18e2014-11-17 10:26:23 -0800281 try:
kelvin-onlabedcff052015-01-16 12:53:55 -0800282 main.log.warn( "Sending: " + command )
kelvin-onlabedcff052015-01-16 12:53:55 -0800283 self.handle.sendline( command )
284 i = self.handle.expect( [ command, pexpect.TIMEOUT ] )
shahshreya28bb18e2014-11-17 10:26:23 -0800285 if i == 1:
kelvin-onlabedcff052015-01-16 12:53:55 -0800286 main.log.error(
287 self.name +
288 ": timeout when waiting for response from mininet" )
289 main.log.error( "response: " + str( self.handle.before ) )
290 i = self.handle.expect( [ "mininet>", pexpect.TIMEOUT ] )
shahshreya28bb18e2014-11-17 10:26:23 -0800291 if i == 1:
kelvin-onlabedcff052015-01-16 12:53:55 -0800292 main.log.error(
293 self.name +
294 ": timeout when waiting for response from mininet" )
295 main.log.error( "response: " + str( self.handle.before ) )
shahshreya28bb18e2014-11-17 10:26:23 -0800296 response = self.handle.before
297 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800298 main.log.error( self.name + ": EOF exception found" )
299 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700300 main.cleanAndExit()
kelvin-onlabedcff052015-01-16 12:53:55 -0800301 main.log.info( self.name + ": Ping Response: " + response )
kelvin-onlabedcff052015-01-16 12:53:55 -0800302 if re.search( ',\s0\%\spacket\sloss', response ):
303 main.log.info( self.name + ": no packets lost, host is reachable" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800304 main.lastResult = main.TRUE
shahshreya28bb18e2014-11-17 10:26:23 -0800305 return main.TRUE
kelvin-onlabedcff052015-01-16 12:53:55 -0800306 else:
alisone4121a92016-11-22 16:31:36 -0800307 main.log.info(
kelvin-onlabedcff052015-01-16 12:53:55 -0800308 self.name +
309 ": PACKET LOST, HOST IS NOT REACHABLE" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800310 main.lastResult = main.FALSE
shahshreya28bb18e2014-11-17 10:26:23 -0800311 return main.FALSE
312
kelvin-onlabedcff052015-01-16 12:53:55 -0800313 def pingHost( self, **pingParams ):
314 """
Jon Hallfbc828e2015-01-06 17:30:19 -0800315 Pings between two hosts on remote mininet
kelvin-onlabedcff052015-01-16 12:53:55 -0800316 """
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700317 try:
318 self.handle.sendline( "" )
Devin Limdc78e202017-06-09 18:30:07 -0700319 self.handle.expect( self.prompt )
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700320 args = utilities.parse_args( [ "SRC", "TARGET" ], **pingParams )
321 command = "mininet/util/m " + \
322 args[ "SRC" ] + " ping " + args[ "TARGET" ] + " -c 4 -W 1 -i .2"
323 main.log.info( command )
324 response = self.execute( cmd=command, prompt="rtt", timeout=10 )
325 if utilities.assert_matches(
326 expect=',\s0\%\spacket\sloss',
327 actual=response,
328 onpass="No Packet loss",
329 onfail="Host is not reachable" ):
330 main.log.info( "NO PACKET LOSS, HOST IS REACHABLE" )
331 main.lastResult = main.TRUE
332 return main.TRUE
333 else:
alisone4121a92016-11-22 16:31:36 -0800334 main.log.info( "PACKET LOST, HOST IS NOT REACHABLE" )
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700335 main.lastResult = main.FALSE
336 return main.FALSE
337 except pexpect.EOF:
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700338 main.log.error( self.name + ": EOF exception found" )
339 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700340 main.cleanAndExit()
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700341 except Exception:
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700342 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700343 main.cleanAndExit()
Jon Hallfbc828e2015-01-06 17:30:19 -0800344
kelvin-onlabedcff052015-01-16 12:53:55 -0800345 def checknum( self, num ):
346 """
Jon Hallfbc828e2015-01-06 17:30:19 -0800347 Verifies the correct number of switches are running
kelvin-onlabedcff052015-01-16 12:53:55 -0800348 """
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700349 try:
350 if self.handle:
351 self.handle.sendline( "" )
Devin Limdc78e202017-06-09 18:30:07 -0700352 self.handle.expect( self.prompt )
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700353 self.handle.sendline( 'ifconfig -a | grep "sw.. " | wc -l' )
354 self.handle.expect( "wc" )
Devin Limdc78e202017-06-09 18:30:07 -0700355 self.handle.expect( self.prompt )
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700356 response = self.handle.before
357 self.handle.sendline(
358 'ps -ef | grep "bash -ms mininet:sw" | grep -v color | wc -l' )
359 self.handle.expect( "color" )
Devin Limdc78e202017-06-09 18:30:07 -0700360 self.handle.expect( self.prompt )
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700361 response2 = self.handle.before
adminbae64d82013-08-01 10:50:15 -0700362
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700363 if re.search( num, response ):
364 if re.search( num, response2 ):
365 return main.TRUE
366 else:
367 return main.FALSE
adminbae64d82013-08-01 10:50:15 -0700368 else:
369 return main.FALSE
370 else:
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700371 main.log.error( "Connection failed to the host" )
372 except pexpect.TIMEOUT:
373 main.log.exception( self.name + ": TIMEOUT exception found in checknum" )
374 main.log.error( self.name + ": " + self.handle.before )
375 return main.FALSE
376 except pexpect.EOF:
377 main.log.error( self.name + ": EOF exception found" )
378 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700379 main.cleanAndExit()
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700380 except Exception:
381 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700382 main.cleanAndExit()
adminbae64d82013-08-01 10:50:15 -0700383
kelvin-onlabd3b64892015-01-20 13:26:24 -0800384 def startTcpdump(
kelvin-onlabedcff052015-01-16 12:53:55 -0800385 self,
386 filename,
387 intf="eth0",
Charles Chan029be652015-08-24 01:46:10 +0800388 port="port 6653",
Jon Hall53c5e662016-04-13 16:06:56 -0700389 user="sdn" ):
kelvin-onlabedcff052015-01-16 12:53:55 -0800390 """
Jon Hallefbd9792015-03-05 16:11:36 -0800391 Runs tcpdump on an interface and saves the file
admin2a9548d2014-06-17 14:08:07 -0700392 intf can be specified, or the default eth0 is used
kelvin-onlabedcff052015-01-16 12:53:55 -0800393 """
admin2a9548d2014-06-17 14:08:07 -0700394 try:
kelvin-onlabedcff052015-01-16 12:53:55 -0800395 self.handle.sendline( "" )
396 self.handle.sendline(
397 "sudo tcpdump -n -i " +
398 intf +
399 " " +
400 port +
401 " -w " +
402 filename.strip() +
403 " -Z " +
404 user +
405 " &" )
406 self.handle.sendline( "" )
407 self.handle.sendline( "" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800408 i = self.handle.expect( [ 'No\ssuch\device', 'listening\son',
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700409 pexpect.TIMEOUT, self.prompt ], timeout=10 )
Jon Hall61282e32015-03-19 11:34:11 -0700410 main.log.info( self.handle.before + self.handle.after )
admin2a9548d2014-06-17 14:08:07 -0700411 if i == 0:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800412 main.log.error( self.name + ": tcpdump - No such device exists.\
413 tcpdump attempted on: " + intf )
admin2a9548d2014-06-17 14:08:07 -0700414 return main.FALSE
415 elif i == 1:
kelvin-onlabedcff052015-01-16 12:53:55 -0800416 main.log.info( self.name + ": tcpdump started on " + intf )
admin2a9548d2014-06-17 14:08:07 -0700417 return main.TRUE
418 elif i == 2:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800419 main.log.error( self.name + ": tcpdump command timed out!\
420 Check interface name, given interface was: " + intf )
admin2a9548d2014-06-17 14:08:07 -0700421 return main.FALSE
kelvin-onlabedcff052015-01-16 12:53:55 -0800422 elif i == 3:
423 main.log.info( self.name + ": " + self.handle.before )
admin2a9548d2014-06-17 14:08:07 -0700424 return main.TRUE
425 else:
kelvin-onlabedcff052015-01-16 12:53:55 -0800426 main.log.error( self.name + ": tcpdump - unexpected response" )
admin2a9548d2014-06-17 14:08:07 -0700427 return main.FALSE
428 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800429 main.log.error( self.name + ": EOF exception found" )
430 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700431 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800432 except Exception:
433 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700434 main.cleanAndExit()
admin2a9548d2014-06-17 14:08:07 -0700435
kelvin-onlabd3b64892015-01-20 13:26:24 -0800436 def stopTcpdump( self ):
Jon Hallefbd9792015-03-05 16:11:36 -0800437 """
438 pkills tcpdump"""
admin2a9548d2014-06-17 14:08:07 -0700439 try:
kelvin-onlabedcff052015-01-16 12:53:55 -0800440 self.handle.sendline( "sudo pkill tcpdump" )
441 self.handle.sendline( "" )
Devin Limdc78e202017-06-09 18:30:07 -0700442 self.handle.expect( self.prompt )
admin2a9548d2014-06-17 14:08:07 -0700443 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800444 main.log.error( self.name + ": EOF exception found" )
445 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700446 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800447 except Exception:
448 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700449 main.cleanAndExit()
admin2a9548d2014-06-17 14:08:07 -0700450
Jon Hallca319892017-06-15 15:25:22 -0700451 def runOpticalMnScript( self, name='onos', ctrllerIP=None ):
shahshreya2c57c902015-04-06 20:40:20 -0700452 import time
shahshreya1f119da2015-04-21 17:16:46 -0700453 import types
kelvin-onlabedcff052015-01-16 12:53:55 -0800454 """
shahshreya1f119da2015-04-21 17:16:46 -0700455 Description:
456 This function is only meant for Packet Optical.
457 It runs python script "opticalTest.py" to create the
458 packet layer( mn ) and optical topology
459 Optional:
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000460 name - Name of onos directory. (ONOS | onos)
shahshreya1f119da2015-04-21 17:16:46 -0700461 Required:
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000462 ctrllerIP = Controller(s) IP address
shahshreya1f119da2015-04-21 17:16:46 -0700463 TODO: If no ctrllerIP is provided, a default
shahshreya2c57c902015-04-06 20:40:20 -0700464 $OC1 can be accepted
kelvin-onlabedcff052015-01-16 12:53:55 -0800465 """
shahshreya28bb18e2014-11-17 10:26:23 -0800466 try:
kelvin-onlabedcff052015-01-16 12:53:55 -0800467 self.handle.sendline( "" )
Devin Limdc78e202017-06-09 18:30:07 -0700468 self.handle.expect( self.prompt )
shahshreya1f119da2015-04-21 17:16:46 -0700469 self.handle.sendline( "cd ~/" + name + "/tools/test/topos" )
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700470 self.handle.expect( "topos" + self.prompt )
471 if ctrllerIP is None:
shahshreya2c57c902015-04-06 20:40:20 -0700472 main.log.info( "You need to specify the IP" )
473 return main.FALSE
474 else:
shahshreya1f119da2015-04-21 17:16:46 -0700475 controller = ''
476 if isinstance( ctrllerIP, types.ListType ):
477 for i in xrange( len( ctrllerIP ) ):
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700478 controller += ctrllerIP[ i ] + ' '
shahshreya1f119da2015-04-21 17:16:46 -0700479 main.log.info( "Mininet topology is being loaded with " +
480 "controllers: " + controller )
481 elif isinstance( ctrllerIP, types.StringType ):
482 controller = ctrllerIP
483 main.log.info( "Mininet topology is being loaded with " +
484 "controller: " + controller )
485 else:
486 main.log.info( "You need to specify a valid IP" )
487 return main.FALSE
488 cmd = "sudo -E python opticalTest.py " + controller
489 main.log.info( self.name + ": cmd = " + cmd )
shahshreya2c57c902015-04-06 20:40:20 -0700490 self.handle.sendline( cmd )
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700491 time.sleep( 30 )
shahshreya2c57c902015-04-06 20:40:20 -0700492 self.handle.sendline( "" )
shahshreya3140b1a2015-04-28 14:22:15 -0700493 self.handle.sendline( "" )
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700494 self.handle.expect( "mininet>" )
shahshreya2c57c902015-04-06 20:40:20 -0700495 return main.TRUE
496 except pexpect.EOF:
497 main.log.error( self.name + ": EOF exception found" )
498 main.log.error( self.name + ": " + self.handle.before )
499 return main.FALSE
500
501 def attachLincOESession( self ):
502 """
503 Since executing opticalTest.py will give you mininet
504 prompt, you would at some point require to get onto
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000505 console of LincOE ((linc@onosTestBench)1>) to execute
shahshreya2c57c902015-04-06 20:40:20 -0700506 commands like bring a optical port up or down on a ROADM
507 You can attach to console of Linc-OE session by a cmd:
508 sudo ~/linc-oe/rel/linc/bin/linc attach
509 """
510 try:
511 self.handle.sendline( "" )
Devin Limdc78e202017-06-09 18:30:07 -0700512 self.handle.expect( self.prompt )
shahshreya2c57c902015-04-06 20:40:20 -0700513 self.handle.sendline( "sudo ~/linc-oe/rel/linc/bin/linc attach" )
kelvin-onlabedcff052015-01-16 12:53:55 -0800514 self.handle.expect( ">" )
shahshreya28bb18e2014-11-17 10:26:23 -0800515 return main.TRUE
516 except pexpect.EOF:
kelvin-onlabedcff052015-01-16 12:53:55 -0800517 main.log.error( self.name + ": EOF exception found" )
518 main.log.error( self.name + ": " + self.handle.before )
shahshreya28bb18e2014-11-17 10:26:23 -0800519 return main.FALSE
adminbae64d82013-08-01 10:50:15 -0700520
kelvin-onlabedcff052015-01-16 12:53:55 -0800521 def disconnect( self ):
522 """
Jon Hallfbc828e2015-01-06 17:30:19 -0800523 Called at the end of the test to disconnect the handle.
kelvin-onlabedcff052015-01-16 12:53:55 -0800524 """
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700525 try:
526 if self.handle:
527 # Close the ssh connection
528 self.handle.sendline( "" )
Devin Limdc78e202017-06-09 18:30:07 -0700529 # self.handle.expect( self.prompt )
530 i = self.handle.expect( [ self.prompt, 'mininet>', pexpect.TIMEOUT,
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700531 pexpect.EOF ], timeout=2 )
532 if i == 0:
533 self.handle.sendline( "exit" )
534 self.handle.expect( "closed" )
535 elif i == 1:
536 self.handle.sendline( "exit" )
537 self.handle.expect( "exit" )
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700538 self.handle.expect( self.prompt )
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700539 self.handle.sendline( "exit" )
540 self.handle.expect( "exit" )
541 self.handle.expect( "closed" )
542 else:
543 main.log.error( "Connection failed to the host" )
544 return main.TRUE
545 except pexpect.TIMEOUT:
546 main.log.exception( self.name + ": TIMEOUT exception found in disconnect" )
547 main.log.error( self.name + ": " + self.handle.before )
548 return main.FALSE
549 except pexpect.EOF:
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700550 main.log.error( self.name + ": EOF exception found" )
551 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700552 main.cleanAndExit()
Ming Yan Shu404f7e72016-07-22 14:37:03 -0700553 except Exception:
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700554 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700555 main.cleanAndExit()
adminbae64d82013-08-01 10:50:15 -0700556
kelvin-onlabd3b64892015-01-20 13:26:24 -0800557 def setIpTablesOUTPUT( self, dstIp, dstPort, action='add',
558 packetType='tcp', rule='DROP' ):
kelvin-onlabedcff052015-01-16 12:53:55 -0800559 """
shahshreyaf4d4d0c2014-10-10 12:11:10 -0700560 Description:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800561 add or remove iptables rule to DROP ( default )
562 packets from specific IP and PORT
shahshreyaf4d4d0c2014-10-10 12:11:10 -0700563 Usage:
kelvin-onlabedcff052015-01-16 12:53:55 -0800564 * specify action ( 'add' or 'remove' )
shahshreyaf4d4d0c2014-10-10 12:11:10 -0700565 when removing, pass in the same argument as you would add. It will
Jon Hallfbc828e2015-01-06 17:30:19 -0800566 delete that specific rule.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800567 * specify the destination ip to block with dstIp
568 * specify destination port to block to dstPort
kelvin-onlabedcff052015-01-16 12:53:55 -0800569 * optional packet type to block ( default tcp )
570 * optional iptables rule ( default DROP )
shahshreyaf4d4d0c2014-10-10 12:11:10 -0700571 WARNING:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800572 * This function uses root privilege iptables command which may result
573 in unwanted network errors. USE WITH CAUTION
kelvin-onlabedcff052015-01-16 12:53:55 -0800574 """
shahshreyaf4d4d0c2014-10-10 12:11:10 -0700575 import re
576 import time
577
kelvin-onlabedcff052015-01-16 12:53:55 -0800578 # NOTE*********
shahshreyaf4d4d0c2014-10-10 12:11:10 -0700579 # The strict checking methods of this driver function is intentional
580 # to discourage any misuse or error of iptables, which can cause
581 # severe network errors
kelvin-onlabd3b64892015-01-20 13:26:24 -0800582 # *************
shahshreyaf4d4d0c2014-10-10 12:11:10 -0700583
kelvin-onlabd3b64892015-01-20 13:26:24 -0800584 # NOTE: Sleep needed to give some time
585 # for rule to be added and registered
shahshreyaf4d4d0c2014-10-10 12:11:10 -0700586 # to the instance
kelvin-onlabedcff052015-01-16 12:53:55 -0800587 time.sleep( 5 )
shahshreyaf4d4d0c2014-10-10 12:11:10 -0700588
kelvin-onlabd3b64892015-01-20 13:26:24 -0800589 actionType = action.lower()
590 if actionType != 'add' and actionType != 'remove':
kelvin-onlabedcff052015-01-16 12:53:55 -0800591 main.log.error(
592 "Invalid action type. 'add' or 'remove' table rule" )
shahshreyaf4d4d0c2014-10-10 12:11:10 -0700593 if rule != 'DROP' and rule != 'ACCEPT' and rule != 'LOG':
kelvin-onlabedcff052015-01-16 12:53:55 -0800594 # NOTE: Currently only supports rules DROP, ACCEPT, and LOG
595 main.log.error(
596 "Invalid rule. 'DROP' or 'ACCEPT' or 'LOG' only." )
shahshreyaf4d4d0c2014-10-10 12:11:10 -0700597 return
598 return
599 else:
600
kelvin-onlabedcff052015-01-16 12:53:55 -0800601 # If there is no existing rule in the iptables, we will see an
kelvin-onlabd3b64892015-01-20 13:26:24 -0800602 # 'iptables:'... message. We expect to see this message.
kelvin-onlabedcff052015-01-16 12:53:55 -0800603 # Otherwise, if there IS an existing rule, we will get the prompt
shahshreyaf4d4d0c2014-10-10 12:11:10 -0700604 # back, hence why we expect $ for remove type. We want to remove
605 # an already existing rule
606
kelvin-onlabd3b64892015-01-20 13:26:24 -0800607 if actionType == 'add':
608 # NOTE: "iptables:" expect is a result of
609 # return from the command
610 # iptables -C ...
611 # Any changes by the iptables command return string
612 # will result in failure of the function. ( deemed unlikely
613 # at the time of writing this function )
kelvin-onlabedcff052015-01-16 12:53:55 -0800614 # Check for existing rules on current input
615 self.handle.sendline( "" )
Devin Limdc78e202017-06-09 18:30:07 -0700616 self.handle.expect( self.prompt )
kelvin-onlabedcff052015-01-16 12:53:55 -0800617 self.handle.sendline(
618 "sudo iptables -C OUTPUT -p " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800619 str( packetType ) +
kelvin-onlabedcff052015-01-16 12:53:55 -0800620 " -d " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800621 str( dstIp ) +
kelvin-onlabedcff052015-01-16 12:53:55 -0800622 " --dport " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800623 str( dstPort ) +
kelvin-onlabedcff052015-01-16 12:53:55 -0800624 " -j " +
625 str( rule ) )
Devin Limdc78e202017-06-09 18:30:07 -0700626 i = self.handle.expect( [ "iptables:", self.prompt ] )
shahshreyaf4d4d0c2014-10-10 12:11:10 -0700627 print i
628 print self.handle.before
629 print "after: "
630 print self.handle.after
631
kelvin-onlabd3b64892015-01-20 13:26:24 -0800632 elif actionType == 'remove':
kelvin-onlabedcff052015-01-16 12:53:55 -0800633 # Check for existing rules on current input
634 self.handle.sendline( "" )
Devin Limdc78e202017-06-09 18:30:07 -0700635 self.handle.expect( self.prompt )
kelvin-onlabedcff052015-01-16 12:53:55 -0800636 self.handle.sendline(
637 "sudo iptables -C OUTPUT -p " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800638 str( packetType ) +
kelvin-onlabedcff052015-01-16 12:53:55 -0800639 " -d " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800640 str( dstIp ) +
kelvin-onlabedcff052015-01-16 12:53:55 -0800641 " --dport " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800642 str( dstPort ) +
kelvin-onlabedcff052015-01-16 12:53:55 -0800643 " -j " +
644 str( rule ) )
Devin Limdc78e202017-06-09 18:30:07 -0700645 self.handle.expect( self.prompt )
shahshreyaf4d4d0c2014-10-10 12:11:10 -0700646 print "before: "
647 print self.handle.before
kelvin-onlabd3b64892015-01-20 13:26:24 -0800648 actualString = self.handle.after
649 expectString = "iptables:"
shahshreyaf4d4d0c2014-10-10 12:11:10 -0700650 print "Actual String:"
kelvin-onlabd3b64892015-01-20 13:26:24 -0800651 print actualString
shahshreyaf4d4d0c2014-10-10 12:11:10 -0700652
kelvin-onlabd3b64892015-01-20 13:26:24 -0800653 if re.search( expectString, actualString ):
654 matchResult = main.TRUE
shahshreyaf4d4d0c2014-10-10 12:11:10 -0700655 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800656 matchResult = main.FALSE
657 # If matchResult is main.TRUE, it means there is no matching rule.
shahshreyaf4d4d0c2014-10-10 12:11:10 -0700658
kelvin-onlabd3b64892015-01-20 13:26:24 -0800659 # If tables does not exist and expected prompt is returned,
660 # go ahead and add iptables rule
661 if matchResult == main.TRUE:
kelvin-onlabedcff052015-01-16 12:53:55 -0800662 # Ensure action type is add
kelvin-onlabd3b64892015-01-20 13:26:24 -0800663 if actionType == 'add':
664 # -A is the 'append' action of iptables
665 actionAdd = '-A'
shahshreyaf4d4d0c2014-10-10 12:11:10 -0700666 try:
kelvin-onlabedcff052015-01-16 12:53:55 -0800667 self.handle.sendline( "" )
668 self.handle.sendline(
669 "sudo iptables " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800670 actionAdd +
kelvin-onlabedcff052015-01-16 12:53:55 -0800671 " OUTPUT -p " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800672 str( packetType ) +
kelvin-onlabedcff052015-01-16 12:53:55 -0800673 " -d " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800674 str( dstIp ) +
kelvin-onlabedcff052015-01-16 12:53:55 -0800675 " --dport " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800676 str( dstPort ) +
kelvin-onlabedcff052015-01-16 12:53:55 -0800677 " -j " +
678 str( rule ) )
shahshreyaf4d4d0c2014-10-10 12:11:10 -0700679
kelvin-onlabd3b64892015-01-20 13:26:24 -0800680 infoString = "Rules added to " + str( self.name )
Jon Hallefbd9792015-03-05 16:11:36 -0800681 infoString += "iptables rule added to block IP: " + \
kelvin-onlabd3b64892015-01-20 13:26:24 -0800682 str( dstIp )
683 infoString += "Port: " + \
684 str( dstPort ) + " Rule: " + str( rule )
shahshreyaf4d4d0c2014-10-10 12:11:10 -0700685
kelvin-onlabd3b64892015-01-20 13:26:24 -0800686 main.log.info( infoString )
shahshreyaf4d4d0c2014-10-10 12:11:10 -0700687
kelvin-onlabedcff052015-01-16 12:53:55 -0800688 self.handle.expect(
Devin Limdc78e202017-06-09 18:30:07 -0700689 [ self.prompt, pexpect.EOF, pexpect.TIMEOUT ] )
shahshreyaf4d4d0c2014-10-10 12:11:10 -0700690 except pexpect.TIMEOUT:
kelvin-onlabedcff052015-01-16 12:53:55 -0800691 main.log.error(
692 self.name +
693 ": Timeout exception in setIpTables function" )
Jon Hallfebb1c72015-03-05 13:30:09 -0800694 except Exception:
695 main.log.exception( self.name +
696 ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700697 main.cleanAndExit()
shahshreyaf4d4d0c2014-10-10 12:11:10 -0700698 else:
kelvin-onlabedcff052015-01-16 12:53:55 -0800699 main.log.error(
700 "Given rule already exists, but attempted to add it" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800701 # If matchResult is 0, it means there IS a matching rule provided
702 elif matchResult == main.FALSE:
kelvin-onlabedcff052015-01-16 12:53:55 -0800703 # Ensure action type is remove
kelvin-onlabd3b64892015-01-20 13:26:24 -0800704 if actionType == 'remove':
705 # -D is the 'delete' rule of iptables
706 actionRemove = '-D'
shahshreyaf4d4d0c2014-10-10 12:11:10 -0700707 try:
kelvin-onlabedcff052015-01-16 12:53:55 -0800708 self.handle.sendline( "" )
709 # Delete a specific rule specified into the function
710 self.handle.sendline(
711 "sudo iptables " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800712 actionRemove +
kelvin-onlabedcff052015-01-16 12:53:55 -0800713 " OUTPUT -p " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800714 str( packetType ) +
kelvin-onlabedcff052015-01-16 12:53:55 -0800715 " -d " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800716 str( dstIp ) +
kelvin-onlabedcff052015-01-16 12:53:55 -0800717 " --dport " +
kelvin-onlabd3b64892015-01-20 13:26:24 -0800718 str( dstPort ) +
kelvin-onlabedcff052015-01-16 12:53:55 -0800719 " -j " +
720 str( rule ) )
shahshreyaf4d4d0c2014-10-10 12:11:10 -0700721
kelvin-onlabd3b64892015-01-20 13:26:24 -0800722 infoString = "Rules removed from " + str( self.name )
723 infoString += " iptables rule removed \
724 from blocking IP: " + \
725 str( dstIp )
726 infoString += " Port: " + \
727 str( dstPort ) + " Rule: " + str( rule )
shahshreyaf4d4d0c2014-10-10 12:11:10 -0700728
kelvin-onlabd3b64892015-01-20 13:26:24 -0800729 main.log.info( infoString )
shahshreyaf4d4d0c2014-10-10 12:11:10 -0700730
kelvin-onlabedcff052015-01-16 12:53:55 -0800731 self.handle.expect(
Devin Limdc78e202017-06-09 18:30:07 -0700732 [ self.prompt, pexpect.EOF, pexpect.TIMEOUT ] )
shahshreyaf4d4d0c2014-10-10 12:11:10 -0700733 except pexpect.TIMEOUT:
kelvin-onlabedcff052015-01-16 12:53:55 -0800734 main.log.error(
735 self.name +
736 ": Timeout exception in setIpTables function" )
Jon Hallfebb1c72015-03-05 13:30:09 -0800737 except Exception:
738 main.log.exception( self.name +
739 ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700740 main.cleanAndExit()
shahshreyaf4d4d0c2014-10-10 12:11:10 -0700741 else:
kelvin-onlabedcff052015-01-16 12:53:55 -0800742 main.log.error(
kelvin-onlabd3b64892015-01-20 13:26:24 -0800743 "Given rule does not exist,\
744 but attempted to remove it" )
shahshreyaf4d4d0c2014-10-10 12:11:10 -0700745 else:
kelvin-onlabedcff052015-01-16 12:53:55 -0800746 # NOTE: If a bad usage of this function occurs, exit the entire
747 # test
748 main.log.error( "Bad rule given for iptables. Exiting..." )
Devin Lim44075962017-08-11 10:56:37 -0700749 main.cleanAndExit()
shahshreyaf4d4d0c2014-10-10 12:11:10 -0700750
751
adminbae64d82013-08-01 10:50:15 -0700752if __name__ != "__main__":
753 import sys
kelvin-onlabedcff052015-01-16 12:53:55 -0800754 sys.modules[ __name__ ] = RemoteMininetDriver()