zhanghaoyu | 451c139 | 2015-08-07 19:21:16 +0800 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | |
| 3 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 4 | Copyright 2015 Open Networking Foundation (ONF) |
Jeremy Ronquillo | b27ce4c | 2017-07-17 12:41:28 -0700 | [diff] [blame] | 5 | |
| 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> |
| 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 |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 13 | (at your option) any later version. |
Jeremy Ronquillo | b27ce4c | 2017-07-17 12:41:28 -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 |
| 21 | along with TestON. If not, see <http://www.gnu.org/licenses/>. |
| 22 | |
| 23 | """ |
Jeremy Ronquillo | 4d5f1d0 | 2017-10-13 20:23:57 +0000 | [diff] [blame] | 24 | |
Jeremy Ronquillo | b27ce4c | 2017-07-17 12:41:28 -0700 | [diff] [blame] | 25 | """ |
zhanghaoyu | 451c139 | 2015-08-07 19:21:16 +0800 | [diff] [blame] | 26 | drivers for ovsdb commands. |
| 27 | |
| 28 | zhanghaoyu7@huawei.com |
| 29 | AUG 10 2015 |
| 30 | """ |
| 31 | import pexpect |
| 32 | import re |
| 33 | import json |
| 34 | import types |
| 35 | import time |
| 36 | import os |
| 37 | from drivers.common.clidriver import CLI |
| 38 | |
| 39 | |
| 40 | class OvsdbDriver( CLI ): |
| 41 | |
| 42 | def __init__( self ): |
| 43 | """ |
| 44 | Initialize client |
| 45 | """ |
| 46 | self.name = None |
| 47 | self.home = None |
| 48 | self.handle = None |
Devin Lim | dc78e20 | 2017-06-09 18:30:07 -0700 | [diff] [blame] | 49 | super( OvsdbDriver, self ).__init__() |
zhanghaoyu | 451c139 | 2015-08-07 19:21:16 +0800 | [diff] [blame] | 50 | |
| 51 | def connect( self, **connectargs ): |
| 52 | try: |
| 53 | for key in connectargs: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 54 | vars( self )[ key ] = connectargs[ key ] |
zhanghaoyu | 451c139 | 2015-08-07 19:21:16 +0800 | [diff] [blame] | 55 | |
| 56 | self.name = self.options[ 'name' ] |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 57 | if os.getenv( str( self.ip_address ) ) is not None: |
| 58 | self.ip_address = os.getenv( str( self.ip_address ) ) |
zhanghaoyu | 451c139 | 2015-08-07 19:21:16 +0800 | [diff] [blame] | 59 | else: |
| 60 | main.log.info( self.name + ": Trying to connect to " + |
| 61 | self.ip_address ) |
| 62 | self.handle = super( OvsdbDriver, self ).connect( |
| 63 | user_name=self.user_name, |
| 64 | ip_address=self.ip_address, |
| 65 | port=self.port, |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 66 | pwd=self.pwd ) |
zhanghaoyu | 451c139 | 2015-08-07 19:21:16 +0800 | [diff] [blame] | 67 | |
| 68 | if self.handle: |
You Wang | 90038fb | 2016-10-26 13:55:29 -0700 | [diff] [blame] | 69 | main.log.info( "Connection successful to the ovsdb node " + |
zhanghaoyu | 451c139 | 2015-08-07 19:21:16 +0800 | [diff] [blame] | 70 | self.name ) |
alison | 6acef9f | 2016-09-28 12:29:08 -0700 | [diff] [blame] | 71 | return self.handle |
zhanghaoyu | 451c139 | 2015-08-07 19:21:16 +0800 | [diff] [blame] | 72 | else: |
| 73 | main.log.error( "Connection failed to the ovsdb node " + |
| 74 | self.name ) |
| 75 | except pexpect.EOF: |
| 76 | main.log.error( self.name + ": EOF exception found" ) |
| 77 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 78 | main.cleanAndExit() |
zhanghaoyu | 451c139 | 2015-08-07 19:21:16 +0800 | [diff] [blame] | 79 | except Exception: |
| 80 | main.log.exception( self.name + ": Uncaught exception!" ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 81 | main.cleanAndExit() |
zhanghaoyu | 451c139 | 2015-08-07 19:21:16 +0800 | [diff] [blame] | 82 | |
| 83 | def disconnect( self ): |
| 84 | try: |
| 85 | self.handle.sendline( "exit" ) |
| 86 | self.handle.expect( "closed" ) |
| 87 | response = main.TRUE |
| 88 | except pexpect.ExceptionPexpect: |
| 89 | response = main.FALSE |
| 90 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 91 | return response |
| 92 | |
| 93 | def setManager( self, ip, port, delaytime="5" ): |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 94 | command = "sudo ovs-vsctl set-manager tcp:" + str( ip ) + ":" + str( port ) |
zhanghaoyu | 451c139 | 2015-08-07 19:21:16 +0800 | [diff] [blame] | 95 | try: |
| 96 | handle = self.execute( |
| 97 | cmd=command, |
| 98 | timeout=10 ) |
| 99 | if re.search( "Error", handle ): |
| 100 | main.log.error( "Error in set ovsdb manager" ) |
| 101 | main.log.error( handle ) |
| 102 | return main.FALSE |
| 103 | else: |
| 104 | main.log.info( "Ovsdb manager " + str( ip ) + " set" ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 105 | # delay time for ovsdb connection create |
zhanghaoyu | 451c139 | 2015-08-07 19:21:16 +0800 | [diff] [blame] | 106 | main.log.info( "Wait " + str( delaytime ) + " seconds for ovsdb connection create" ) |
| 107 | time.sleep( int( delaytime ) ) |
| 108 | return main.TRUE |
| 109 | except pexpect.EOF: |
| 110 | main.log.error( self.name + ": EOF exception found" ) |
| 111 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 112 | main.cleanAndExit() |
zhanghaoyu | 451c139 | 2015-08-07 19:21:16 +0800 | [diff] [blame] | 113 | |
| 114 | def delManager( self, delaytime="5" ): |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 115 | command = "sudo ovs-vsctl del-manager" |
zhanghaoyu | 451c139 | 2015-08-07 19:21:16 +0800 | [diff] [blame] | 116 | try: |
| 117 | handle = self.execute( |
| 118 | cmd=command, |
| 119 | timeout=10 ) |
| 120 | if re.search( "Error", handle ): |
| 121 | main.log.error( "Error in delete ovsdb manager" ) |
| 122 | main.log.error( handle ) |
| 123 | return main.FALSE |
| 124 | else: |
| 125 | main.log.info( "Ovsdb manager delete" ) |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 126 | # delay time for ovsdb connection delete |
zhanghaoyu | 451c139 | 2015-08-07 19:21:16 +0800 | [diff] [blame] | 127 | main.log.info( "Wait " + str( delaytime ) + " seconds for ovsdb connection delete" ) |
| 128 | time.sleep( int( delaytime ) ) |
| 129 | return main.TRUE |
| 130 | except pexpect.EOF: |
| 131 | main.log.error( self.name + ": EOF exception found" ) |
| 132 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 133 | main.cleanAndExit() |
zhanghaoyu | 451c139 | 2015-08-07 19:21:16 +0800 | [diff] [blame] | 134 | |
| 135 | def getManager( self ): |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 136 | command = "sudo ovs-vsctl get-manager" |
zhanghaoyu | 451c139 | 2015-08-07 19:21:16 +0800 | [diff] [blame] | 137 | try: |
| 138 | response = self.execute( |
| 139 | cmd=command, |
| 140 | timeout=10 ) |
| 141 | return response |
| 142 | except pexpect.EOF: |
| 143 | main.log.error( self.name + ": EOF exception found" ) |
| 144 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 145 | main.cleanAndExit() |
zhanghaoyu | 451c139 | 2015-08-07 19:21:16 +0800 | [diff] [blame] | 146 | |
| 147 | def listBr( self ): |
| 148 | """ |
| 149 | Parameters: |
| 150 | none |
| 151 | Return: |
| 152 | The output of the command from the linux |
| 153 | or main.FALSE on timeout |
| 154 | """ |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 155 | command = "sudo ovs-vsctl list-br" |
zhanghaoyu | 451c139 | 2015-08-07 19:21:16 +0800 | [diff] [blame] | 156 | try: |
| 157 | response = self.execute( |
| 158 | cmd=command, |
| 159 | timeout=10 ) |
| 160 | if response: |
| 161 | return response |
| 162 | else: |
| 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 ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 167 | main.cleanAndExit() |
zhanghaoyu | 451c139 | 2015-08-07 19:21:16 +0800 | [diff] [blame] | 168 | |
| 169 | def listPorts( self, sw ): |
| 170 | """ |
| 171 | Parameters: |
| 172 | sw: The name of an OVS switch. Example "s1" |
| 173 | Return: |
| 174 | The output of the command from the linux |
| 175 | or main.FALSE on timeout |
| 176 | """ |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 177 | command = "sudo ovs-vsctl list-ports " + str( sw ) |
zhanghaoyu | 451c139 | 2015-08-07 19:21:16 +0800 | [diff] [blame] | 178 | try: |
| 179 | response = self.execute( |
| 180 | cmd=command, |
| 181 | timeout=10 ) |
| 182 | if response: |
| 183 | return response |
| 184 | else: |
| 185 | return main.FALSE |
| 186 | except pexpect.EOF: |
| 187 | main.log.error( self.name + ": EOF exception found" ) |
| 188 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 189 | main.cleanAndExit() |
zhanghaoyu | 451c139 | 2015-08-07 19:21:16 +0800 | [diff] [blame] | 190 | |
| 191 | def getController( self, sw ): |
| 192 | """ |
| 193 | Parameters: |
| 194 | sw: The name of an OVS switch. Example "s1" |
| 195 | Return: |
| 196 | The output of the command from the mininet cli |
| 197 | or main.FALSE on timeout""" |
| 198 | command = "sudo ovs-vsctl get-controller " + str( sw ) |
| 199 | try: |
| 200 | response = self.execute( |
| 201 | cmd=command, |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 202 | timeout=10 ) |
zhanghaoyu | 451c139 | 2015-08-07 19:21:16 +0800 | [diff] [blame] | 203 | if response: |
| 204 | return response |
| 205 | else: |
| 206 | return main.FALSE |
| 207 | except pexpect.EOF: |
| 208 | main.log.error( self.name + ": EOF exception found" ) |
| 209 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 210 | main.cleanAndExit() |
zhanghaoyu7 | 474d8c6 | 2015-08-26 14:53:28 +0800 | [diff] [blame] | 211 | |
zhanghaoyu | 451c139 | 2015-08-07 19:21:16 +0800 | [diff] [blame] | 212 | def show( self ): |
| 213 | """ |
| 214 | Parameters: |
| 215 | none |
| 216 | Return: |
| 217 | The output of the command from the linux |
| 218 | or main.FALSE on timeout""" |
| 219 | command = "sudo ovs-vsctl show " |
| 220 | try: |
| 221 | response = self.execute( |
| 222 | cmd=command, |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 223 | timeout=10 ) |
zhanghaoyu | 451c139 | 2015-08-07 19:21:16 +0800 | [diff] [blame] | 224 | if response: |
| 225 | return response |
| 226 | else: |
| 227 | return main.FALSE |
| 228 | except pexpect.EOF: |
| 229 | main.log.error( self.name + ": EOF exception found" ) |
| 230 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 231 | main.cleanAndExit() |
zhanghaoyu7 | 474d8c6 | 2015-08-26 14:53:28 +0800 | [diff] [blame] | 232 | |
| 233 | def dumpFlows( self, sw, protocols=None ): |
| 234 | """ |
| 235 | Parameters: |
| 236 | sw: The name of an OVS switch. Example "s1" |
| 237 | Return: |
| 238 | The output of the command from the linux |
| 239 | or main.FALSE on timeout""" |
| 240 | if protocols: |
| 241 | command = "sudo ovs-ofctl -O " + \ |
| 242 | protocols + " dump-flows " + str( sw ) |
| 243 | else: |
| 244 | command = "sudo ovs-ofctl dump-flows " + str( sw ) |
| 245 | try: |
| 246 | response = self.execute( |
| 247 | cmd=command, |
| 248 | timeout=10 ) |
| 249 | if response: |
| 250 | return response |
| 251 | else: |
| 252 | return main.FALSE |
| 253 | except pexpect.EOF: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 254 | main.log.error( self.name + ": EOF exception found" ) |
| 255 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 256 | main.cleanAndExit() |
zhanghaoyu7 | 474d8c6 | 2015-08-26 14:53:28 +0800 | [diff] [blame] | 257 | |
| 258 | def createHost( self, hostname ): |
| 259 | command = "sudo ip netns add " + str( hostname ) |
| 260 | try: |
| 261 | handle = self.execute( |
| 262 | cmd=command, |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 263 | timeout=10 ) |
zhanghaoyu7 | 474d8c6 | 2015-08-26 14:53:28 +0800 | [diff] [blame] | 264 | if re.search( "Error", handle ): |
| 265 | main.log.error( "Error in create host" + str( hostname ) ) |
| 266 | main.log.error( handle ) |
| 267 | return main.FALSE |
| 268 | else: |
| 269 | main.log.info( "Create " + str( hostname ) + " sucess" ) |
| 270 | return main.TRUE |
| 271 | except pexpect.EOF: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 272 | main.log.error( self.name + ": EOF exception found" ) |
| 273 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 274 | main.cleanAndExit() |
zhanghaoyu7 | 474d8c6 | 2015-08-26 14:53:28 +0800 | [diff] [blame] | 275 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 276 | def createHostport( self, hostname="host1", hostport="host1-eth0", ovsport="port1", hostportmac="000000000001" ): |
| 277 | command = "sudo ip link add " + str( hostport ) + " type veth peer name " + str( ovsport ) |
| 278 | command += ";" + "sudo ip link set " + str( hostport ) + " up" |
| 279 | command += ";" + "sudo ip link set " + str( ovsport ) + " up" |
| 280 | command += ";" + " sudo ifconfig " + str( hostport ) + " hw ether " + str( hostportmac ) |
| 281 | command += ";" + " sudo ip link set " + str( hostport ) + " netns " + str( hostname ) |
zhanghaoyu7 | 474d8c6 | 2015-08-26 14:53:28 +0800 | [diff] [blame] | 282 | try: |
| 283 | handle = self.execute( |
| 284 | cmd=command, |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 285 | timeout=10 ) |
zhanghaoyu7 | 474d8c6 | 2015-08-26 14:53:28 +0800 | [diff] [blame] | 286 | if re.search( "Error", handle ): |
| 287 | main.log.error( "Error in create host port " + str( hostport ) + " on " + str( hostname ) ) |
| 288 | main.log.error( handle ) |
| 289 | return main.FALSE |
| 290 | else: |
| 291 | main.log.info( "Create host port " + str( hostport ) + " on " + str( hostname ) + " sucess" ) |
| 292 | return main.TRUE |
| 293 | except pexpect.EOF: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 294 | main.log.error( self.name + ": EOF exception found" ) |
| 295 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 296 | main.cleanAndExit() |
zhanghaoyu7 | 474d8c6 | 2015-08-26 14:53:28 +0800 | [diff] [blame] | 297 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 298 | def addPortToOvs( self, ifaceId, attachedMac, vmuuid, port="port1", ovsname="br-int" ): |
| 299 | command = "sudo ovs-vsctl add-port " + str( ovsname ) + " " + str( port ) |
zhanghaoyu7 | 474d8c6 | 2015-08-26 14:53:28 +0800 | [diff] [blame] | 300 | if ifaceId: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 301 | command += " -- set Interface " + str( port ) + " external-ids:iface-id=" + str( ifaceId ) + " external-ids:iface-status=active" |
zhanghaoyu7 | 474d8c6 | 2015-08-26 14:53:28 +0800 | [diff] [blame] | 302 | if attachedMac: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 303 | command += " external-ids:attached-mac=" + str( attachedMac ) |
zhanghaoyu7 | 474d8c6 | 2015-08-26 14:53:28 +0800 | [diff] [blame] | 304 | if vmuuid: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 305 | command += " external-ids:vm-uuid=" + str( vmuuid ) |
zhanghaoyu7 | 474d8c6 | 2015-08-26 14:53:28 +0800 | [diff] [blame] | 306 | try: |
| 307 | handle = self.execute( |
| 308 | cmd=command, |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 309 | timeout=10 ) |
zhanghaoyu7 | 474d8c6 | 2015-08-26 14:53:28 +0800 | [diff] [blame] | 310 | if re.search( "Error", handle ): |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 311 | main.log.error( "Error in add port " + str( port ) + " to ovs " + str( ovsname ) ) |
zhanghaoyu7 | 474d8c6 | 2015-08-26 14:53:28 +0800 | [diff] [blame] | 312 | main.log.error( handle ) |
| 313 | return main.FALSE |
| 314 | else: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 315 | main.log.info( "Add port " + str( port ) + " to ovs " + str( ovsname ) + " sucess" ) |
zhanghaoyu7 | 474d8c6 | 2015-08-26 14:53:28 +0800 | [diff] [blame] | 316 | return main.TRUE |
| 317 | except pexpect.EOF: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 318 | main.log.error( self.name + ": EOF exception found" ) |
| 319 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 320 | main.cleanAndExit() |
zhanghaoyu7 | 474d8c6 | 2015-08-26 14:53:28 +0800 | [diff] [blame] | 321 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 322 | def setHostportIp( self, ip, hostname="host1", hostport1="host1-eth0" ): |
| 323 | command = "sudo ip netns exec " + str( hostname ) + " ifconfig " + str( hostport1 ) + " " + str( ip ) |
zhanghaoyu7 | 474d8c6 | 2015-08-26 14:53:28 +0800 | [diff] [blame] | 324 | try: |
| 325 | handle = self.execute( |
| 326 | cmd=command, |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 327 | timeout=10 ) |
zhanghaoyu7 | 474d8c6 | 2015-08-26 14:53:28 +0800 | [diff] [blame] | 328 | if re.search( "Error", handle ): |
| 329 | main.log.error( "Error in set host ip for " + str( hostport1 ) + " on host " + str( hostname ) ) |
| 330 | main.log.error( handle ) |
| 331 | return main.FALSE |
| 332 | else: |
| 333 | main.log.info( "Set host ip for " + str( hostport1 ) + " on host " + str( hostname ) + " sucess" ) |
| 334 | return main.TRUE |
| 335 | except pexpect.EOF: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 336 | main.log.error( self.name + ": EOF exception found" ) |
| 337 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 338 | main.cleanAndExit() |
zhanghaoyu7 | 474d8c6 | 2015-08-26 14:53:28 +0800 | [diff] [blame] | 339 | |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 340 | def hostPing( self, src, target, hostname="host1" ): |
zhanghaoyu7 | 474d8c6 | 2015-08-26 14:53:28 +0800 | [diff] [blame] | 341 | if src: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 342 | command = "sudo ip netns exec " + str( hostname ) + " ping -c 1 -S " +\ |
| 343 | str( src ) + " " + str( target ) |
zhanghaoyu7 | 474d8c6 | 2015-08-26 14:53:28 +0800 | [diff] [blame] | 344 | else: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 345 | command = "sudo ip netns exec " + str( hostname ) + " ping -c 1 " + str( target ) |
zhanghaoyu7 | 474d8c6 | 2015-08-26 14:53:28 +0800 | [diff] [blame] | 346 | try: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 347 | for i in range( 1, 5 ): |
zhanghaoyu7 | 474d8c6 | 2015-08-26 14:53:28 +0800 | [diff] [blame] | 348 | handle = self.execute( |
| 349 | cmd=command, |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 350 | timeout=10 ) |
| 351 | if re.search( ',\s0\%\spacket\sloss', handle ): |
| 352 | main.log.info( self.name + ": no packets lost, host is reachable" ) |
zhanghaoyu7 | 474d8c6 | 2015-08-26 14:53:28 +0800 | [diff] [blame] | 353 | return main.TRUE |
| 354 | break |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 355 | time.sleep( 5 ) |
zhanghaoyu7 | 474d8c6 | 2015-08-26 14:53:28 +0800 | [diff] [blame] | 356 | else: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 357 | main.log.info( self.name + ": packets lost, host is unreachable" ) |
zhanghaoyu7 | 474d8c6 | 2015-08-26 14:53:28 +0800 | [diff] [blame] | 358 | return main.FALSE |
| 359 | except pexpect.EOF: |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 360 | main.log.error( self.name + ": EOF exception found" ) |
| 361 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 362 | main.cleanAndExit() |
zhanghaoyu7 | 474d8c6 | 2015-08-26 14:53:28 +0800 | [diff] [blame] | 363 | |
| 364 | def delBr( self, sw ): |
| 365 | """ |
| 366 | Parameters: |
| 367 | sw: The name of an OVS switch. Example "br-int" |
| 368 | Return: |
| 369 | Delete sucess return main.TRUE or main.FALSE on delete failed |
| 370 | """ |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 371 | command = "sudo ovs-vsctl del-br " + str( sw ) |
zhanghaoyu7 | 474d8c6 | 2015-08-26 14:53:28 +0800 | [diff] [blame] | 372 | try: |
| 373 | response = self.execute( |
| 374 | cmd=command, |
| 375 | timeout=10 ) |
| 376 | if response: |
| 377 | return main.TRUE |
| 378 | else: |
| 379 | return main.FALSE |
| 380 | except pexpect.EOF: |
| 381 | main.log.error( self.name + ": EOF exception found" ) |
| 382 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 383 | main.cleanAndExit() |
zhanghaoyu7 | 474d8c6 | 2015-08-26 14:53:28 +0800 | [diff] [blame] | 384 | |
| 385 | def delHost( self, hostname ): |
| 386 | """ |
| 387 | Parameters: |
| 388 | hostname: The name of an ip netns name. Example "host1" |
| 389 | Return: |
| 390 | Delete sucess return main.TRUE or main.FALSE on delete failed |
| 391 | """ |
Jeremy Ronquillo | 8270549 | 2017-10-18 14:19:55 -0700 | [diff] [blame] | 392 | command = "sudo ip netns delete " + str( hostname ) |
zhanghaoyu7 | 474d8c6 | 2015-08-26 14:53:28 +0800 | [diff] [blame] | 393 | try: |
| 394 | response = self.execute( |
| 395 | cmd=command, |
| 396 | timeout=10 ) |
| 397 | if response: |
| 398 | return main.TRUE |
| 399 | else: |
| 400 | return main.FALSE |
| 401 | except pexpect.EOF: |
| 402 | main.log.error( self.name + ": EOF exception found" ) |
| 403 | main.log.error( self.name + ": " + self.handle.before ) |
Devin Lim | 4407596 | 2017-08-11 10:56:37 -0700 | [diff] [blame] | 404 | main.cleanAndExit() |