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