admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 2 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 3 | Created on 26-Oct-2012 |
Jeremy Songster | ae01bba | 2016-07-11 15:39:17 -0700 | [diff] [blame] | 4 | Modified 2016 by ON.Lab |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 5 | |
Jeremy Songster | ae01bba | 2016-07-11 15:39:17 -0700 | [diff] [blame] | 6 | Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>, |
| 7 | the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>, |
| 8 | or the System Testing Guide page at <https://wiki.onosproject.org/x/WYQg> |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 9 | |
| 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-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 13 | ( at your option ) any later version. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 14 | |
| 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 Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 21 | along with TestON. If not, see <http://www.gnu.org/licenses/>. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 22 | |
| 23 | |
| 24 | MininetCliDriver is the basic driver which will handle the Mininet functions |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 25 | """ |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 26 | import pexpect |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 27 | import re |
| 28 | import sys |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 29 | import os |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 30 | from drivers.common.cli.emulatordriver import Emulator |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 31 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 32 | |
| 33 | class RemoteMininetDriver( Emulator ): |
| 34 | |
| 35 | """ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 36 | 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-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 40 | """ |
| 41 | def __init__( self ): |
| 42 | super( Emulator, self ).__init__() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 43 | self.handle = self |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 44 | self.name = None |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 45 | self.wrapped = sys.modules[ __name__ ] |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 46 | self.flag = 0 |
| 47 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 48 | def connect( self, **connectargs ): |
kelvin-onlab | 08679eb | 2015-01-21 16:11:48 -0800 | [diff] [blame] | 49 | """,user_name, ip_address, pwd,options ): |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 50 | Here the main is the TestON instance after creating all the log |
| 51 | handles.""" |
kelvin-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 52 | try: |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 53 | for key in connectargs: |
| 54 | vars( self )[ key ] = connectargs[ key ] |
| 55 | |
| 56 | self.name = self.options[ 'name' ] |
| 57 | |
| 58 | try: |
| 59 | if os.getenv( str( self.ip_address ) ) != None: |
| 60 | 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-onlab | a407429 | 2015-07-09 15:19:49 -0700 | [diff] [blame] | 85 | else: |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 86 | 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!" ) |
| 95 | main.cleanup() |
| 96 | main.exit() |
admin | 98ad009 | 2014-07-23 16:51:07 -0700 | [diff] [blame] | 97 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 98 | def checkForLoss( self, pingList ): |
| 99 | """ |
Jon Hall | 6c794f3 | 2014-08-14 13:33:13 -0700 | [diff] [blame] | 100 | Returns main.FALSE for 0% packet loss and |
| 101 | Returns main.ERROR if "found multiple mininet" is found and |
| 102 | Returns main.TRUE else |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 103 | """ |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 104 | try: |
| 105 | self.handle.sendline( "" ) |
| 106 | self.handle.expect( "\$" ) |
| 107 | self.handle.sendline( "" ) |
| 108 | self.handle.expect( "\$" ) |
| 109 | self.handle.sendline( "cat " + pingList ) |
| 110 | self.handle.expect( pingList ) |
| 111 | self.handle.expect( "\$" ) |
| 112 | outputs = self.handle.before + self.handle.after |
| 113 | if re.search( " 0% packet loss", outputs ): |
| 114 | return main.FALSE |
| 115 | elif re.search( "found multiple mininet", outputs ): |
| 116 | return main.ERROR |
| 117 | else: |
| 118 | # TODO: Parse for failed pings, give some truncated output |
| 119 | main.log.error( "Error, unexpected output in the ping file" ) |
| 120 | main.log.warn( outputs ) |
| 121 | return main.TRUE |
| 122 | except pexpect.TIMEOUT: |
| 123 | main.log.exception( self.name + ": TIMEOUT exception found in checkForLoss" ) |
| 124 | main.log.error( self.name + ": " + self.handle.before ) |
admin | 98ad009 | 2014-07-23 16:51:07 -0700 | [diff] [blame] | 125 | return main.FALSE |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 126 | except pexpect.EOF: |
| 127 | main.log.error( self.name + ": EOF exception found" ) |
| 128 | main.log.error( self.name + ": " + self.handle.before ) |
| 129 | main.cleanup() |
| 130 | main.exit() |
| 131 | except Exception: |
| 132 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 133 | main.cleanup() |
| 134 | main.exit() |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 135 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 136 | def pingLong( self, **pingParams ): |
| 137 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 138 | Starts a continuous ping on the mininet host outputting |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 139 | to a file in the /tmp dir. |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 140 | """ |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 141 | try: |
| 142 | self.handle.sendline( "" ) |
| 143 | self.handle.expect( "\$" ) |
| 144 | args = utilities.parse_args( |
| 145 | [ "SRC", "TARGET", "PINGTIME" ], **pingParams ) |
| 146 | precmd = "sudo rm /tmp/ping." + args[ "SRC" ] |
| 147 | self.execute( cmd=precmd, prompt="(.*)", timeout=10 ) |
| 148 | command = "sudo mininet/util/m " + args[ "SRC" ] + " ping " +\ |
| 149 | args[ "TARGET" ] + " -i .2 -w " +\ |
| 150 | str( args[ 'PINGTIME' ] ) + " -D > /tmp/ping." +\ |
| 151 | args[ "SRC" ] + " &" |
| 152 | main.log.info( command ) |
| 153 | self.execute( cmd=command, prompt="(.*)", timeout=10 ) |
| 154 | self.handle.sendline( "" ) |
| 155 | self.handle.expect( "\$" ) |
| 156 | return main.TRUE |
| 157 | except TypeError: |
| 158 | main.log.exception(self.name + ": Object not as expected") |
| 159 | return main.FALSE |
| 160 | except pexpect.TIMEOUT: |
| 161 | main.log.exception( self.name + ": TIMEOUT exception found in pingLong" ) |
| 162 | main.log.error( self.name + ": " + self.handle.before ) |
| 163 | return main.FALSE |
| 164 | except pexpect.EOF: |
| 165 | main.log.error( self.name + ": EOF exception found" ) |
| 166 | main.log.error( self.name + ": " + self.handle.before ) |
| 167 | main.cleanup() |
| 168 | main.exit() |
| 169 | except Exception: |
| 170 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 171 | main.cleanup() |
| 172 | main.exit() |
| 173 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 174 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 175 | def pingstatus( self, **pingParams ): |
| 176 | """ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 177 | Tails the respective ping output file and check that |
| 178 | there is a moving "64 bytes" |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 179 | """ |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 180 | try: |
| 181 | self.handle.sendline( "" ) |
| 182 | self.handle.expect( "\$" ) |
| 183 | args = utilities.parse_args( [ "SRC" ], **pingParams ) |
| 184 | self.handle.sendline( "tail /tmp/ping." + args[ "SRC" ] ) |
| 185 | self.handle.expect( "tail" ) |
| 186 | self.handle.expect( "\$" ) |
| 187 | result = self.handle.before + self.handle.after |
| 188 | self.handle.sendline( "" ) |
| 189 | self.handle.expect( "\$" ) |
| 190 | if re.search( 'Unreachable', result ): |
| 191 | main.log.info( "Unreachable found in ping logs..." ) |
| 192 | return main.FALSE |
| 193 | elif re.search( '64\sbytes', result ): |
| 194 | main.log.info( "Pings look good" ) |
| 195 | return main.TRUE |
| 196 | else: |
| 197 | main.log.info( "No, or faulty ping data..." ) |
| 198 | return main.FALSE |
| 199 | except TypeError: |
| 200 | main.log.exception( self.name + ": Object not as expected" ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 201 | return main.FALSE |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 202 | except pexpect.TIMEOUT: |
| 203 | main.log.exception( self.name + ": TIMEOUT exception found in pingstatus" ) |
| 204 | main.log.error( self.name + ": " + self.handle.before ) |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 205 | return main.FALSE |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 206 | except pexpect.EOF: |
| 207 | main.log.error( self.name + ": EOF exception found" ) |
| 208 | main.log.error( self.name + ": " + self.handle.before ) |
| 209 | main.cleanup() |
| 210 | main.exit() |
| 211 | except Exception: |
| 212 | main.log.exception(self.name + ": Uncaught exception!") |
| 213 | main.cleanup() |
| 214 | main.exit() |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 215 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 216 | def pingKill( self, testONUser, testONIP ): |
| 217 | """ |
admin | aeedddd | 2013-08-02 15:14:15 -0700 | [diff] [blame] | 218 | Kills all continuous ping processes. |
| 219 | Then copies all the ping files to the TestStation. |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 220 | """ |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 221 | try: |
| 222 | self.handle.sendline( "" ) |
| 223 | self.handle.expect( "\$" ) |
| 224 | command = "sudo kill -SIGINT `pgrep ping`" |
| 225 | main.log.info( command ) |
| 226 | self.execute( cmd=command, prompt="(.*)", timeout=10 ) |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 227 | |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 228 | main.log.info( "Transferring ping files to TestStation" ) |
| 229 | command = "scp /tmp/ping.* " + \ |
| 230 | str( testONUser ) + "@" + str( testONIP ) + ":/tmp/" |
| 231 | self.execute( cmd=command, prompt="100%", timeout=20 ) |
| 232 | # Make sure the output is cleared |
| 233 | self.handle.sendline( "" ) |
| 234 | self.handle.expect( "\$" ) |
| 235 | self.handle.sendline( "" ) |
| 236 | self.handle.expect( "\$" ) |
| 237 | self.handle.sendline( "" ) |
| 238 | i = self.handle.expect( [ "password", "\$" ] ) |
| 239 | if i == 0: |
| 240 | main.log.error( "Error, sudo asking for password" ) |
| 241 | main.log.error( self.handle.before ) |
| 242 | return main.FALSE |
| 243 | else: |
| 244 | return main.TRUE |
| 245 | except pexpect.TIMEOUT: |
| 246 | main.log.error( self.name + ": TIMEOUT exception found in pingKill" ) |
| 247 | main.log.error( self.name + ": " + self.handle.before ) |
| 248 | main.cleanup() |
| 249 | main.exit() |
| 250 | except pexpect.EOF: |
| 251 | main.log.error( self.name + ": EOF exception found" ) |
| 252 | main.log.error( self.name + ": " + self.handle.before ) |
| 253 | main.cleanup() |
| 254 | main.exit() |
| 255 | except Exception: |
| 256 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 257 | main.cleanup() |
| 258 | main.exit() |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 259 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 260 | def pingLongKill( self ): |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 261 | try: |
| 262 | self.handle.sendline( "" ) |
| 263 | self.handle.expect( "\$" ) |
| 264 | command = "sudo kill -SIGING `pgrep ping`" |
| 265 | main.log.info( command ) |
| 266 | self.execute( cmd=command, prompt="(.*)", timeout=10 ) |
| 267 | self.handle.sendline( "" ) |
| 268 | self.handle.expect( "\$" ) |
| 269 | return main.TRUE |
| 270 | except pexpect.TIMEOUT: |
| 271 | main.log.exception( self.name + ": TIMEOUT exception found in pingLongKill" ) |
| 272 | main.log.error( self.name + ": " + self.handle.before ) |
| 273 | return main.FALSE |
| 274 | except pexpect.EOF: |
| 275 | main.log.error(self.name + ": EOF exception found") |
| 276 | main.log.error(self.name + ": " + self.handle.before) |
| 277 | main.cleanup() |
| 278 | main.exit() |
| 279 | except Exception: |
| 280 | main.log.exception(self.name + ": Uncaught exception!") |
| 281 | main.cleanup() |
| 282 | main.exit() |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 283 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 284 | def pingHostOptical( self, **pingParams ): |
| 285 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 286 | This function is only for Packet Optical related ping |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 287 | Use the next pingHost() function for all normal scenarios ) |
shahshreya | 28bb18e | 2014-11-17 10:26:23 -0800 | [diff] [blame] | 288 | Ping from one mininet host to another |
| 289 | Currently the only supported Params: SRC and TARGET |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 290 | """ |
kelvin-onlab | 7d0c967 | 2015-01-20 15:56:22 -0800 | [diff] [blame] | 291 | args = utilities.parse_args( [ "SRC", "TARGET" ], **pingParams ) |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 292 | command = args[ "SRC" ] + " ping " + \ |
| 293 | args[ "TARGET" ] + " -c 1 -i 1 -W 8" |
shahshreya | 28bb18e | 2014-11-17 10:26:23 -0800 | [diff] [blame] | 294 | try: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 295 | main.log.warn( "Sending: " + command ) |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 296 | self.handle.sendline( command ) |
| 297 | i = self.handle.expect( [ command, pexpect.TIMEOUT ] ) |
shahshreya | 28bb18e | 2014-11-17 10:26:23 -0800 | [diff] [blame] | 298 | if i == 1: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 299 | main.log.error( |
| 300 | self.name + |
| 301 | ": timeout when waiting for response from mininet" ) |
| 302 | main.log.error( "response: " + str( self.handle.before ) ) |
| 303 | i = self.handle.expect( [ "mininet>", pexpect.TIMEOUT ] ) |
shahshreya | 28bb18e | 2014-11-17 10:26:23 -0800 | [diff] [blame] | 304 | if i == 1: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 305 | main.log.error( |
| 306 | self.name + |
| 307 | ": timeout when waiting for response from mininet" ) |
| 308 | main.log.error( "response: " + str( self.handle.before ) ) |
shahshreya | 28bb18e | 2014-11-17 10:26:23 -0800 | [diff] [blame] | 309 | response = self.handle.before |
| 310 | except pexpect.EOF: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 311 | main.log.error( self.name + ": EOF exception found" ) |
| 312 | main.log.error( self.name + ": " + self.handle.before ) |
shahshreya | 28bb18e | 2014-11-17 10:26:23 -0800 | [diff] [blame] | 313 | main.cleanup() |
| 314 | main.exit() |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 315 | main.log.info( self.name + ": Ping Response: " + response ) |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 316 | if re.search( ',\s0\%\spacket\sloss', response ): |
| 317 | main.log.info( self.name + ": no packets lost, host is reachable" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 318 | main.lastResult = main.TRUE |
shahshreya | 28bb18e | 2014-11-17 10:26:23 -0800 | [diff] [blame] | 319 | return main.TRUE |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 320 | else: |
| 321 | main.log.error( |
| 322 | self.name + |
| 323 | ": PACKET LOST, HOST IS NOT REACHABLE" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 324 | main.lastResult = main.FALSE |
shahshreya | 28bb18e | 2014-11-17 10:26:23 -0800 | [diff] [blame] | 325 | return main.FALSE |
| 326 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 327 | def pingHost( self, **pingParams ): |
| 328 | """ |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 329 | Pings between two hosts on remote mininet |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 330 | """ |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 331 | try: |
| 332 | self.handle.sendline( "" ) |
| 333 | self.handle.expect( "\$" ) |
| 334 | args = utilities.parse_args( [ "SRC", "TARGET" ], **pingParams ) |
| 335 | command = "mininet/util/m " + \ |
| 336 | args[ "SRC" ] + " ping " + args[ "TARGET" ] + " -c 4 -W 1 -i .2" |
| 337 | main.log.info( command ) |
| 338 | response = self.execute( cmd=command, prompt="rtt", timeout=10 ) |
| 339 | if utilities.assert_matches( |
| 340 | expect=',\s0\%\spacket\sloss', |
| 341 | actual=response, |
| 342 | onpass="No Packet loss", |
| 343 | onfail="Host is not reachable" ): |
| 344 | main.log.info( "NO PACKET LOSS, HOST IS REACHABLE" ) |
| 345 | main.lastResult = main.TRUE |
| 346 | return main.TRUE |
| 347 | else: |
| 348 | main.log.error( "PACKET LOST, HOST IS NOT REACHABLE" ) |
| 349 | main.lastResult = main.FALSE |
| 350 | return main.FALSE |
| 351 | except pexpect.EOF: |
| 352 | main.log.error(self.name + ": EOF exception found") |
| 353 | main.log.error(self.name + ": " + self.handle.before) |
| 354 | main.cleanup() |
| 355 | main.exit() |
| 356 | except Exception: |
| 357 | main.log.exception(self.name + ": Uncaught exception!") |
| 358 | main.cleanup() |
| 359 | main.exit() |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 360 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 361 | def checknum( self, num ): |
| 362 | """ |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 363 | Verifies the correct number of switches are running |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 364 | """ |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 365 | try: |
| 366 | if self.handle: |
| 367 | self.handle.sendline( "" ) |
| 368 | self.handle.expect( "\$" ) |
| 369 | self.handle.sendline( 'ifconfig -a | grep "sw.. " | wc -l' ) |
| 370 | self.handle.expect( "wc" ) |
| 371 | self.handle.expect( "\$" ) |
| 372 | response = self.handle.before |
| 373 | self.handle.sendline( |
| 374 | 'ps -ef | grep "bash -ms mininet:sw" | grep -v color | wc -l' ) |
| 375 | self.handle.expect( "color" ) |
| 376 | self.handle.expect( "\$" ) |
| 377 | response2 = self.handle.before |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 378 | |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 379 | if re.search( num, response ): |
| 380 | if re.search( num, response2 ): |
| 381 | return main.TRUE |
| 382 | else: |
| 383 | return main.FALSE |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 384 | else: |
| 385 | return main.FALSE |
| 386 | else: |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 387 | main.log.error( "Connection failed to the host" ) |
| 388 | except pexpect.TIMEOUT: |
| 389 | main.log.exception( self.name + ": TIMEOUT exception found in checknum" ) |
| 390 | main.log.error( self.name + ": " + self.handle.before ) |
| 391 | return main.FALSE |
| 392 | except pexpect.EOF: |
| 393 | main.log.error( self.name + ": EOF exception found" ) |
| 394 | main.log.error( self.name + ": " + self.handle.before ) |
| 395 | main.cleanup() |
| 396 | main.exit() |
| 397 | except Exception: |
| 398 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 399 | main.cleanup() |
| 400 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 401 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 402 | def startTcpdump( |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 403 | self, |
| 404 | filename, |
| 405 | intf="eth0", |
Charles Chan | 029be65 | 2015-08-24 01:46:10 +0800 | [diff] [blame] | 406 | port="port 6653", |
Jon Hall | 53c5e66 | 2016-04-13 16:06:56 -0700 | [diff] [blame] | 407 | user="sdn" ): |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 408 | """ |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 409 | Runs tcpdump on an interface and saves the file |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 410 | intf can be specified, or the default eth0 is used |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 411 | """ |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 412 | try: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 413 | self.handle.sendline( "" ) |
| 414 | self.handle.sendline( |
| 415 | "sudo tcpdump -n -i " + |
| 416 | intf + |
| 417 | " " + |
| 418 | port + |
| 419 | " -w " + |
| 420 | filename.strip() + |
| 421 | " -Z " + |
| 422 | user + |
| 423 | " &" ) |
| 424 | self.handle.sendline( "" ) |
| 425 | self.handle.sendline( "" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 426 | i = self.handle.expect( [ 'No\ssuch\device', 'listening\son', |
| 427 | pexpect.TIMEOUT, "\$" ], timeout=10 ) |
Jon Hall | 61282e3 | 2015-03-19 11:34:11 -0700 | [diff] [blame] | 428 | main.log.info( self.handle.before + self.handle.after ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 429 | if i == 0: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 430 | main.log.error( self.name + ": tcpdump - No such device exists.\ |
| 431 | tcpdump attempted on: " + intf ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 432 | return main.FALSE |
| 433 | elif i == 1: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 434 | main.log.info( self.name + ": tcpdump started on " + intf ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 435 | return main.TRUE |
| 436 | elif i == 2: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 437 | main.log.error( self.name + ": tcpdump command timed out!\ |
| 438 | Check interface name, given interface was: " + intf ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 439 | return main.FALSE |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 440 | elif i == 3: |
| 441 | main.log.info( self.name + ": " + self.handle.before ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 442 | return main.TRUE |
| 443 | else: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 444 | main.log.error( self.name + ": tcpdump - unexpected response" ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 445 | return main.FALSE |
| 446 | except pexpect.EOF: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 447 | main.log.error( self.name + ": EOF exception found" ) |
| 448 | main.log.error( self.name + ": " + self.handle.before ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 449 | main.cleanup() |
| 450 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 451 | except Exception: |
| 452 | main.log.exception( self.name + ": Uncaught exception!" ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 453 | main.cleanup() |
| 454 | main.exit() |
| 455 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 456 | def stopTcpdump( self ): |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 457 | """ |
| 458 | pkills tcpdump""" |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 459 | try: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 460 | self.handle.sendline( "sudo pkill tcpdump" ) |
| 461 | self.handle.sendline( "" ) |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 462 | self.handle.expect( "\$" ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 463 | except pexpect.EOF: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 464 | main.log.error( self.name + ": EOF exception found" ) |
| 465 | main.log.error( self.name + ": " + self.handle.before ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 466 | main.cleanup() |
| 467 | main.exit() |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 468 | except Exception: |
| 469 | main.log.exception( self.name + ": Uncaught exception!" ) |
admin | 2a9548d | 2014-06-17 14:08:07 -0700 | [diff] [blame] | 470 | main.cleanup() |
| 471 | main.exit() |
| 472 | |
shahshreya | 1f119da | 2015-04-21 17:16:46 -0700 | [diff] [blame] | 473 | def runOpticalMnScript( self,name = 'onos', ctrllerIP = None ): |
shahshreya | 2c57c90 | 2015-04-06 20:40:20 -0700 | [diff] [blame] | 474 | import time |
shahshreya | 1f119da | 2015-04-21 17:16:46 -0700 | [diff] [blame] | 475 | import types |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 476 | """ |
shahshreya | 1f119da | 2015-04-21 17:16:46 -0700 | [diff] [blame] | 477 | Description: |
| 478 | This function is only meant for Packet Optical. |
| 479 | It runs python script "opticalTest.py" to create the |
| 480 | packet layer( mn ) and optical topology |
| 481 | Optional: |
| 482 | name - Name of onos directory. (ONOS | onos) |
| 483 | Required: |
| 484 | ctrllerIP = Controller(s) IP address |
| 485 | TODO: If no ctrllerIP is provided, a default |
shahshreya | 2c57c90 | 2015-04-06 20:40:20 -0700 | [diff] [blame] | 486 | $OC1 can be accepted |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 487 | """ |
shahshreya | 28bb18e | 2014-11-17 10:26:23 -0800 | [diff] [blame] | 488 | try: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 489 | self.handle.sendline( "" ) |
| 490 | self.handle.expect( "\$" ) |
shahshreya | 1f119da | 2015-04-21 17:16:46 -0700 | [diff] [blame] | 491 | self.handle.sendline( "cd ~/" + name + "/tools/test/topos" ) |
shahshreya | 2c57c90 | 2015-04-06 20:40:20 -0700 | [diff] [blame] | 492 | self.handle.expect( "topos\$" ) |
| 493 | if ctrllerIP == None: |
| 494 | main.log.info( "You need to specify the IP" ) |
| 495 | return main.FALSE |
| 496 | else: |
shahshreya | 1f119da | 2015-04-21 17:16:46 -0700 | [diff] [blame] | 497 | controller = '' |
| 498 | if isinstance( ctrllerIP, types.ListType ): |
| 499 | for i in xrange( len( ctrllerIP ) ): |
| 500 | controller += ctrllerIP[i] + ' ' |
| 501 | main.log.info( "Mininet topology is being loaded with " + |
| 502 | "controllers: " + controller ) |
| 503 | elif isinstance( ctrllerIP, types.StringType ): |
| 504 | controller = ctrllerIP |
| 505 | main.log.info( "Mininet topology is being loaded with " + |
| 506 | "controller: " + controller ) |
| 507 | else: |
| 508 | main.log.info( "You need to specify a valid IP" ) |
| 509 | return main.FALSE |
| 510 | cmd = "sudo -E python opticalTest.py " + controller |
| 511 | main.log.info( self.name + ": cmd = " + cmd ) |
shahshreya | 2c57c90 | 2015-04-06 20:40:20 -0700 | [diff] [blame] | 512 | self.handle.sendline( cmd ) |
shahshreya | d524a94 | 2015-04-21 09:55:16 -0700 | [diff] [blame] | 513 | time.sleep(30) |
shahshreya | 2c57c90 | 2015-04-06 20:40:20 -0700 | [diff] [blame] | 514 | self.handle.sendline( "" ) |
shahshreya | 3140b1a | 2015-04-28 14:22:15 -0700 | [diff] [blame] | 515 | self.handle.sendline( "" ) |
shahshreya | 2c57c90 | 2015-04-06 20:40:20 -0700 | [diff] [blame] | 516 | self.handle.expect("mininet>") |
| 517 | return main.TRUE |
| 518 | except pexpect.EOF: |
| 519 | main.log.error( self.name + ": EOF exception found" ) |
| 520 | main.log.error( self.name + ": " + self.handle.before ) |
| 521 | return main.FALSE |
| 522 | |
| 523 | def attachLincOESession( self ): |
| 524 | """ |
| 525 | Since executing opticalTest.py will give you mininet |
| 526 | prompt, you would at some point require to get onto |
| 527 | console of LincOE ((linc@onosTestBench)1>) to execute |
| 528 | commands like bring a optical port up or down on a ROADM |
| 529 | You can attach to console of Linc-OE session by a cmd: |
| 530 | sudo ~/linc-oe/rel/linc/bin/linc attach |
| 531 | """ |
| 532 | try: |
| 533 | self.handle.sendline( "" ) |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 534 | self.handle.expect( "\$" ) |
shahshreya | 2c57c90 | 2015-04-06 20:40:20 -0700 | [diff] [blame] | 535 | self.handle.sendline( "sudo ~/linc-oe/rel/linc/bin/linc attach" ) |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 536 | self.handle.expect( ">" ) |
shahshreya | 28bb18e | 2014-11-17 10:26:23 -0800 | [diff] [blame] | 537 | return main.TRUE |
| 538 | except pexpect.EOF: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 539 | main.log.error( self.name + ": EOF exception found" ) |
| 540 | main.log.error( self.name + ": " + self.handle.before ) |
shahshreya | 28bb18e | 2014-11-17 10:26:23 -0800 | [diff] [blame] | 541 | return main.FALSE |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 542 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 543 | def disconnect( self ): |
| 544 | """ |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 545 | Called at the end of the test to disconnect the handle. |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 546 | """ |
Ming Yan Shu | 404f7e7 | 2016-07-22 14:37:03 -0700 | [diff] [blame] | 547 | try: |
| 548 | if self.handle: |
| 549 | # Close the ssh connection |
| 550 | self.handle.sendline( "" ) |
| 551 | # self.handle.expect( "\$" ) |
| 552 | i = self.handle.expect( [ '\$', 'mininet>', pexpect.TIMEOUT, |
| 553 | pexpect.EOF ], timeout=2 ) |
| 554 | if i == 0: |
| 555 | self.handle.sendline( "exit" ) |
| 556 | self.handle.expect( "closed" ) |
| 557 | elif i == 1: |
| 558 | self.handle.sendline( "exit" ) |
| 559 | self.handle.expect( "exit" ) |
| 560 | self.handle.expect('\$') |
| 561 | self.handle.sendline( "exit" ) |
| 562 | self.handle.expect( "exit" ) |
| 563 | self.handle.expect( "closed" ) |
| 564 | else: |
| 565 | main.log.error( "Connection failed to the host" ) |
| 566 | return main.TRUE |
| 567 | except pexpect.TIMEOUT: |
| 568 | main.log.exception( self.name + ": TIMEOUT exception found in disconnect" ) |
| 569 | main.log.error( self.name + ": " + self.handle.before ) |
| 570 | return main.FALSE |
| 571 | except pexpect.EOF: |
| 572 | main.log.error(self.name + ": EOF exception found") |
| 573 | main.log.error(self.name + ": " + self.handle.before) |
| 574 | main.cleanup() |
| 575 | main.exit() |
| 576 | except Exception: |
| 577 | main.log.exception(self.name + ": Uncaught exception!") |
| 578 | main.cleanup() |
| 579 | main.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 580 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 581 | def setIpTablesOUTPUT( self, dstIp, dstPort, action='add', |
| 582 | packetType='tcp', rule='DROP' ): |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 583 | """ |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 584 | Description: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 585 | add or remove iptables rule to DROP ( default ) |
| 586 | packets from specific IP and PORT |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 587 | Usage: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 588 | * specify action ( 'add' or 'remove' ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 589 | when removing, pass in the same argument as you would add. It will |
Jon Hall | fbc828e | 2015-01-06 17:30:19 -0800 | [diff] [blame] | 590 | delete that specific rule. |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 591 | * specify the destination ip to block with dstIp |
| 592 | * specify destination port to block to dstPort |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 593 | * optional packet type to block ( default tcp ) |
| 594 | * optional iptables rule ( default DROP ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 595 | WARNING: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 596 | * This function uses root privilege iptables command which may result |
| 597 | in unwanted network errors. USE WITH CAUTION |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 598 | """ |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 599 | import re |
| 600 | import time |
| 601 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 602 | # NOTE********* |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 603 | # The strict checking methods of this driver function is intentional |
| 604 | # to discourage any misuse or error of iptables, which can cause |
| 605 | # severe network errors |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 606 | # ************* |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 607 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 608 | # NOTE: Sleep needed to give some time |
| 609 | # for rule to be added and registered |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 610 | # to the instance |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 611 | time.sleep( 5 ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 612 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 613 | actionType = action.lower() |
| 614 | if actionType != 'add' and actionType != 'remove': |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 615 | main.log.error( |
| 616 | "Invalid action type. 'add' or 'remove' table rule" ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 617 | if rule != 'DROP' and rule != 'ACCEPT' and rule != 'LOG': |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 618 | # NOTE: Currently only supports rules DROP, ACCEPT, and LOG |
| 619 | main.log.error( |
| 620 | "Invalid rule. 'DROP' or 'ACCEPT' or 'LOG' only." ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 621 | return |
| 622 | return |
| 623 | else: |
| 624 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 625 | # If there is no existing rule in the iptables, we will see an |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 626 | # 'iptables:'... message. We expect to see this message. |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 627 | # Otherwise, if there IS an existing rule, we will get the prompt |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 628 | # back, hence why we expect $ for remove type. We want to remove |
| 629 | # an already existing rule |
| 630 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 631 | if actionType == 'add': |
| 632 | # NOTE: "iptables:" expect is a result of |
| 633 | # return from the command |
| 634 | # iptables -C ... |
| 635 | # Any changes by the iptables command return string |
| 636 | # will result in failure of the function. ( deemed unlikely |
| 637 | # at the time of writing this function ) |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 638 | # Check for existing rules on current input |
| 639 | self.handle.sendline( "" ) |
| 640 | self.handle.expect( "\$" ) |
| 641 | self.handle.sendline( |
| 642 | "sudo iptables -C OUTPUT -p " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 643 | str( packetType ) + |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 644 | " -d " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 645 | str( dstIp ) + |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 646 | " --dport " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 647 | str( dstPort ) + |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 648 | " -j " + |
| 649 | str( rule ) ) |
| 650 | i = self.handle.expect( [ "iptables:", "\$" ] ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 651 | print i |
| 652 | print self.handle.before |
| 653 | print "after: " |
| 654 | print self.handle.after |
| 655 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 656 | elif actionType == 'remove': |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 657 | # Check for existing rules on current input |
| 658 | self.handle.sendline( "" ) |
| 659 | self.handle.expect( "\$" ) |
| 660 | self.handle.sendline( |
| 661 | "sudo iptables -C OUTPUT -p " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 662 | str( packetType ) + |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 663 | " -d " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 664 | str( dstIp ) + |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 665 | " --dport " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 666 | str( dstPort ) + |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 667 | " -j " + |
| 668 | str( rule ) ) |
| 669 | self.handle.expect( "\$" ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 670 | print "before: " |
| 671 | print self.handle.before |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 672 | actualString = self.handle.after |
| 673 | expectString = "iptables:" |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 674 | print "Actual String:" |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 675 | print actualString |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 676 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 677 | if re.search( expectString, actualString ): |
| 678 | matchResult = main.TRUE |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 679 | else: |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 680 | matchResult = main.FALSE |
| 681 | # If matchResult is main.TRUE, it means there is no matching rule. |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 682 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 683 | # If tables does not exist and expected prompt is returned, |
| 684 | # go ahead and add iptables rule |
| 685 | if matchResult == main.TRUE: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 686 | # Ensure action type is add |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 687 | if actionType == 'add': |
| 688 | # -A is the 'append' action of iptables |
| 689 | actionAdd = '-A' |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 690 | try: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 691 | self.handle.sendline( "" ) |
| 692 | self.handle.sendline( |
| 693 | "sudo iptables " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 694 | actionAdd + |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 695 | " OUTPUT -p " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 696 | str( packetType ) + |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 697 | " -d " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 698 | str( dstIp ) + |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 699 | " --dport " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 700 | str( dstPort ) + |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 701 | " -j " + |
| 702 | str( rule ) ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 703 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 704 | infoString = "Rules added to " + str( self.name ) |
Jon Hall | efbd979 | 2015-03-05 16:11:36 -0800 | [diff] [blame] | 705 | infoString += "iptables rule added to block IP: " + \ |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 706 | str( dstIp ) |
| 707 | infoString += "Port: " + \ |
| 708 | str( dstPort ) + " Rule: " + str( rule ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 709 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 710 | main.log.info( infoString ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 711 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 712 | self.handle.expect( |
| 713 | [ "\$", pexpect.EOF, pexpect.TIMEOUT ] ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 714 | except pexpect.TIMEOUT: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 715 | main.log.error( |
| 716 | self.name + |
| 717 | ": Timeout exception in setIpTables function" ) |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 718 | except Exception: |
| 719 | main.log.exception( self.name + |
| 720 | ": Uncaught exception!" ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 721 | main.cleanup() |
| 722 | main.exit() |
| 723 | else: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 724 | main.log.error( |
| 725 | "Given rule already exists, but attempted to add it" ) |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 726 | # If matchResult is 0, it means there IS a matching rule provided |
| 727 | elif matchResult == main.FALSE: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 728 | # Ensure action type is remove |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 729 | if actionType == 'remove': |
| 730 | # -D is the 'delete' rule of iptables |
| 731 | actionRemove = '-D' |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 732 | try: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 733 | self.handle.sendline( "" ) |
| 734 | # Delete a specific rule specified into the function |
| 735 | self.handle.sendline( |
| 736 | "sudo iptables " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 737 | actionRemove + |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 738 | " OUTPUT -p " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 739 | str( packetType ) + |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 740 | " -d " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 741 | str( dstIp ) + |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 742 | " --dport " + |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 743 | str( dstPort ) + |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 744 | " -j " + |
| 745 | str( rule ) ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 746 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 747 | infoString = "Rules removed from " + str( self.name ) |
| 748 | infoString += " iptables rule removed \ |
| 749 | from blocking IP: " + \ |
| 750 | str( dstIp ) |
| 751 | infoString += " Port: " + \ |
| 752 | str( dstPort ) + " Rule: " + str( rule ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 753 | |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 754 | main.log.info( infoString ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 755 | |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 756 | self.handle.expect( |
| 757 | [ "\$", pexpect.EOF, pexpect.TIMEOUT ] ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 758 | except pexpect.TIMEOUT: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 759 | main.log.error( |
| 760 | self.name + |
| 761 | ": Timeout exception in setIpTables function" ) |
Jon Hall | febb1c7 | 2015-03-05 13:30:09 -0800 | [diff] [blame] | 762 | except Exception: |
| 763 | main.log.exception( self.name + |
| 764 | ": Uncaught exception!" ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 765 | main.cleanup() |
| 766 | main.exit() |
| 767 | else: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 768 | main.log.error( |
kelvin-onlab | d3b6489 | 2015-01-20 13:26:24 -0800 | [diff] [blame] | 769 | "Given rule does not exist,\ |
| 770 | but attempted to remove it" ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 771 | else: |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 772 | # NOTE: If a bad usage of this function occurs, exit the entire |
| 773 | # test |
| 774 | main.log.error( "Bad rule given for iptables. Exiting..." ) |
shahshreya | f4d4d0c | 2014-10-10 12:11:10 -0700 | [diff] [blame] | 775 | main.cleanup() |
| 776 | main.exit() |
| 777 | |
| 778 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 779 | if __name__ != "__main__": |
| 780 | import sys |
kelvin-onlab | edcff05 | 2015-01-16 12:53:55 -0800 | [diff] [blame] | 781 | sys.modules[ __name__ ] = RemoteMininetDriver() |