Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | """ |
| 3 | Copyright 2018 Open Networking Foundation (ONF) |
| 4 | |
| 5 | Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>, |
| 6 | the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>, |
| 7 | or the System Testing Guide page at <https://wiki.onosproject.org/x/WYQg> |
| 8 | |
| 9 | TestON is free software: you can redistribute it and/or modify |
| 10 | it under the terms of the GNU General Public License as published by |
| 11 | the Free Software Foundation, either version 2 of the License, or |
| 12 | ( at your option ) any later version. |
| 13 | |
| 14 | TestON is distributed in the hope that it will be useful, |
| 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | GNU General Public License for more details. |
| 18 | |
| 19 | You should have received a copy of the GNU General Public License |
| 20 | along with TestON. If not, see <http://www.gnu.org/licenses/>. |
| 21 | """ |
| 22 | |
| 23 | import pexpect |
| 24 | import re |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 25 | import os |
You Wang | 4cc6191 | 2018-08-28 10:10:58 -0700 | [diff] [blame] | 26 | from drivers.common.cli.emulator.scapyclidriver import ScapyCliDriver |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 27 | |
You Wang | 4cc6191 | 2018-08-28 10:10:58 -0700 | [diff] [blame] | 28 | class HostDriver( ScapyCliDriver ): |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 29 | """ |
| 30 | This class is created as a standalone host driver. |
| 31 | """ |
| 32 | def __init__( self ): |
| 33 | super( HostDriver, self ).__init__() |
| 34 | self.handle = self |
| 35 | self.name = None |
| 36 | self.shortName = None |
You Wang | b1665b5 | 2019-02-01 15:49:48 -0800 | [diff] [blame] | 37 | self.interfaces = [] |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 38 | self.home = None |
You Wang | 4cc6191 | 2018-08-28 10:10:58 -0700 | [diff] [blame] | 39 | self.inband = False |
| 40 | self.prompt = "\$" |
| 41 | self.scapyPrompt = ">>>" |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 42 | |
| 43 | def connect( self, **connectargs ): |
| 44 | """ |
| 45 | Creates ssh handle for host. |
| 46 | NOTE: |
| 47 | The ip_address would come from the topo file using the host tag, the |
| 48 | value can be an environment variable as well as a "localhost" to get |
| 49 | the ip address needed to ssh to the "bench" |
| 50 | """ |
| 51 | try: |
| 52 | for key in connectargs: |
| 53 | vars( self )[ key ] = connectargs[ key ] |
| 54 | self.name = self.options[ 'name' ] |
| 55 | self.shortName = self.options[ 'shortName' ] |
You Wang | b1665b5 | 2019-02-01 15:49:48 -0800 | [diff] [blame] | 56 | self.interfaces.append( { 'ips': [ self.options[ 'ip' ] ], |
| 57 | 'isUp': True, |
| 58 | 'mac': self.options[ 'mac' ], |
Jon Hall | ceed0a3 | 2021-06-08 11:07:00 -0700 | [diff] [blame^] | 59 | 'dhcp': self.options[ 'dhcp' ], |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 60 | 'name': self.options.get( 'interfaceName', None ) } ) |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 61 | |
| 62 | try: |
| 63 | if os.getenv( str( self.ip_address ) ) is not None: |
| 64 | self.ip_address = os.getenv( str( self.ip_address ) ) |
| 65 | else: |
| 66 | main.log.info( self.name + |
| 67 | ": Trying to connect to " + |
| 68 | self.ip_address ) |
| 69 | except KeyError: |
| 70 | main.log.info( "Invalid host name," + |
| 71 | " connecting to local host instead" ) |
| 72 | self.ip_address = 'localhost' |
| 73 | except Exception as inst: |
| 74 | main.log.error( "Uncaught exception: " + str( inst ) ) |
| 75 | |
| 76 | self.handle = super( |
| 77 | HostDriver, |
| 78 | self ).connect( |
| 79 | user_name=self.user_name, |
| 80 | ip_address=self.ip_address, |
| 81 | port=None, |
| 82 | pwd=self.pwd ) |
| 83 | |
Jon Hall | ceed0a3 | 2021-06-08 11:07:00 -0700 | [diff] [blame^] | 84 | # Update IP of interfaces if using dhcp |
| 85 | for intf in self.interfaces: |
| 86 | if intf[ 'dhcp' ].lower() == "true": |
| 87 | ip = self.getIPAddress( iface=intf[ 'name' ] ) |
| 88 | if ip: |
| 89 | intf['ips'] = [ ip ] |
| 90 | else: |
| 91 | main.log.warn( self.name + ": Could not find IP of %s" % intf[ 'name' ] ) |
| 92 | |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 93 | if self.handle: |
| 94 | main.log.info( "Connection successful to the " + |
| 95 | self.user_name + |
| 96 | "@" + |
| 97 | self.ip_address ) |
| 98 | self.handle.sendline( "" ) |
| 99 | self.handle.expect( self.prompt ) |
| 100 | return main.TRUE |
| 101 | else: |
| 102 | main.log.error( "Connection failed to " + |
| 103 | self.user_name + |
| 104 | "@" + |
| 105 | self.ip_address ) |
| 106 | return main.FALSE |
| 107 | except pexpect.EOF: |
| 108 | main.log.error( self.name + ": EOF exception found" ) |
| 109 | main.log.error( self.name + ": " + self.handle.before ) |
| 110 | main.cleanAndExit() |
| 111 | except Exception: |
| 112 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 113 | main.cleanAndExit() |
| 114 | |
| 115 | def disconnect( self, **connectargs ): |
| 116 | """ |
| 117 | Called when test is complete to disconnect the handle. |
| 118 | """ |
| 119 | response = main.TRUE |
| 120 | try: |
| 121 | if self.handle: |
| 122 | # Disconnect from the host |
You Wang | b1665b5 | 2019-02-01 15:49:48 -0800 | [diff] [blame] | 123 | if not self.options[ 'inband' ] == 'True': |
| 124 | self.handle.sendline( "" ) |
| 125 | self.handle.expect( self.prompt ) |
| 126 | self.handle.sendline( "exit" ) |
| 127 | i = self.handle.expect( [ "closed", pexpect.TIMEOUT ] ) |
| 128 | if i == 1: |
| 129 | main.log.error( self.name + ": timeout when waiting for response" ) |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 130 | main.log.error( self.name + ": response: " + str( self.handle.before ) ) |
You Wang | b1665b5 | 2019-02-01 15:49:48 -0800 | [diff] [blame] | 131 | else: |
| 132 | self.handle.sendline( "" ) |
| 133 | i = self.handle.expect( [ self.prompt, pexpect.TIMEOUT ], timeout=2 ) |
| 134 | if i == 1: |
| 135 | main.log.warn( self.name + ": timeout when waiting for response" ) |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 136 | main.log.warn( self.name + ": response: " + str( self.handle.before ) ) |
You Wang | b1665b5 | 2019-02-01 15:49:48 -0800 | [diff] [blame] | 137 | self.handle.sendline( "exit" ) |
| 138 | i = self.handle.expect( [ "closed", pexpect.TIMEOUT ], timeout=2 ) |
| 139 | if i == 1: |
| 140 | main.log.warn( self.name + ": timeout when waiting for response" ) |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 141 | main.log.warn( self.name + ": response: " + str( self.handle.before ) ) |
You Wang | b1665b5 | 2019-02-01 15:49:48 -0800 | [diff] [blame] | 142 | return main.TRUE |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 143 | except TypeError: |
| 144 | main.log.exception( self.name + ": Object not as expected" ) |
| 145 | response = main.FALSE |
| 146 | except pexpect.EOF: |
| 147 | main.log.error( self.name + ": EOF exception found" ) |
| 148 | main.log.error( self.name + ": " + self.handle.before ) |
| 149 | except ValueError: |
| 150 | main.log.exception( "Exception in disconnect of " + self.name ) |
| 151 | response = main.TRUE |
| 152 | except Exception: |
| 153 | main.log.exception( self.name + ": Connection failed to the host" ) |
| 154 | response = main.FALSE |
| 155 | return response |
| 156 | |
You Wang | 4cc6191 | 2018-08-28 10:10:58 -0700 | [diff] [blame] | 157 | def connectInband( self ): |
| 158 | """ |
| 159 | ssh to the host using its data plane IP |
| 160 | """ |
| 161 | try: |
| 162 | if not self.options[ 'inband' ] == 'True': |
| 163 | main.log.info( "Skip connecting the host via data plane" ) |
| 164 | return main.TRUE |
| 165 | self.handle.sendline( "" ) |
| 166 | self.handle.expect( self.prompt ) |
| 167 | self.handle.sendline( "ssh {}@{}".format( self.options[ 'username' ], |
| 168 | self.options[ 'ip' ] ) ) |
| 169 | i = self.handle.expect( [ "password:|Password:", self.prompt, pexpect.TIMEOUT ], timeout=30 ) |
| 170 | if i == 0: |
| 171 | self.handle.sendline( self.options[ 'password' ] ) |
| 172 | j = self.handle.expect( [ "password:|Password:", self.prompt, pexpect.TIMEOUT ], timeout=10 ) |
| 173 | if j != 1: |
| 174 | main.log.error( "Incorrect password" ) |
| 175 | return main.FALSE |
| 176 | elif i == 1: |
| 177 | main.log.info( "Password not required logged in" ) |
| 178 | else: |
| 179 | main.log.error( "Failed to connect to the host" ) |
| 180 | return main.FALSE |
| 181 | self.inband = True |
| 182 | return main.TRUE |
| 183 | except KeyError: |
| 184 | main.log.error( self.name + ": host component not as expected" ) |
| 185 | main.log.error( self.name + ": " + self.handle.before ) |
| 186 | return main.FALSE |
| 187 | except pexpect.EOF: |
| 188 | main.log.error( self.name + ": EOF exception found" ) |
| 189 | main.log.error( self.name + ": " + self.handle.before ) |
| 190 | return main.FALSE |
| 191 | except Exception: |
| 192 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 193 | main.log.error( self.name + ": " + self.handle.before ) |
| 194 | return main.FALSE |
| 195 | |
| 196 | def disconnectInband( self ): |
| 197 | """ |
| 198 | Terminate the ssh connection to the host's data plane IP |
| 199 | """ |
| 200 | try: |
| 201 | if not self.options[ 'inband' ] == 'True': |
| 202 | main.log.info( "Skip disconnecting the host via data plane" ) |
| 203 | return main.TRUE |
| 204 | self.handle.sendline( "" ) |
You Wang | b1665b5 | 2019-02-01 15:49:48 -0800 | [diff] [blame] | 205 | self.handle.expect( self.prompt, timeout=2 ) |
You Wang | 4cc6191 | 2018-08-28 10:10:58 -0700 | [diff] [blame] | 206 | self.handle.sendline( "exit" ) |
| 207 | i = self.handle.expect( [ "closed", pexpect.TIMEOUT ], timeout=2 ) |
| 208 | if i == 1: |
| 209 | main.log.error( self.name + ": timeout when waiting for response" ) |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 210 | main.log.error( self.name + ": response: " + str( self.handle.before ) ) |
You Wang | 4cc6191 | 2018-08-28 10:10:58 -0700 | [diff] [blame] | 211 | return main.TRUE |
| 212 | except pexpect.EOF: |
| 213 | main.log.error( self.name + ": EOF exception found" ) |
| 214 | main.log.error( self.name + ": " + self.handle.before ) |
| 215 | return main.FALSE |
| 216 | except Exception: |
| 217 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 218 | main.log.error( self.name + ": " + self.handle.before ) |
| 219 | return main.FALSE |
| 220 | |
Jon Hall | ceed0a3 | 2021-06-08 11:07:00 -0700 | [diff] [blame^] | 221 | def getIPAddress( self, iface=None, proto='IPV4' ): |
| 222 | """ |
| 223 | Returns IP address of the host |
| 224 | """ |
| 225 | cmd = "ifconfig %s" % iface if iface else "" |
| 226 | response = self.command( cmd ) |
| 227 | pattern = '' |
| 228 | if proto == 'IPV4': |
| 229 | pattern = "inet\s(\d+\.\d+\.\d+\.\d+)\s\snetmask" |
| 230 | else: |
| 231 | pattern = "inet6\s([\w,:]*)/\d+\s\sprefixlen" |
| 232 | ipAddressSearch = re.search( pattern, response ) |
| 233 | if not ipAddressSearch: |
| 234 | return None |
| 235 | main.log.info( |
| 236 | self.name + |
| 237 | ": IP-Address is " + |
| 238 | ipAddressSearch.group( 1 ) ) |
| 239 | return ipAddressSearch.group( 1 ) |
| 240 | |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 241 | def ping( self, dst, ipv6=False, interface=None, wait=3 ): |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 242 | """ |
| 243 | Description: |
| 244 | Ping from this host to another |
| 245 | Required: |
| 246 | dst: IP address of destination host |
| 247 | Optional: |
| 248 | ipv6: will use ping6 command if True; otherwise use ping command |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 249 | interface: Specify which interface to use for the ping |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 250 | wait: timeout for ping command |
| 251 | """ |
| 252 | try: |
| 253 | command = "ping6" if ipv6 else "ping" |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 254 | if interface: |
| 255 | command += " -I %s " % interface |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 256 | command += " -c 1 -i 1 -W " + str( wait ) + " " + str( dst ) |
| 257 | main.log.info( self.name + ": Sending: " + command ) |
| 258 | self.handle.sendline( command ) |
| 259 | i = self.handle.expect( [ self.prompt, pexpect.TIMEOUT ], |
Jon Hall | a604fd4 | 2018-05-04 14:27:27 -0700 | [diff] [blame] | 260 | timeout=wait + 5 ) |
Jon Hall | 06fd0df | 2021-01-25 15:50:06 -0800 | [diff] [blame] | 261 | response = self.handle.before |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 262 | if i == 1: |
| 263 | main.log.error( |
| 264 | self.name + |
| 265 | ": timeout when waiting for response" ) |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 266 | main.log.error( self.name + ": response: " + str( self.handle.before ) ) |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 267 | if re.search( ',\s0\%\spacket\sloss', response ): |
| 268 | main.log.info( self.name + ": no packets lost, host is reachable" ) |
| 269 | return main.TRUE |
| 270 | else: |
| 271 | main.log.warn( |
| 272 | self.name + |
| 273 | ": PACKET LOST, HOST IS NOT REACHABLE" ) |
| 274 | return main.FALSE |
| 275 | except pexpect.EOF: |
| 276 | main.log.error( self.name + ": EOF exception found" ) |
| 277 | main.log.error( self.name + ": " + self.handle.before ) |
| 278 | main.cleanAndExit() |
| 279 | except Exception: |
| 280 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 281 | main.cleanAndExit() |
| 282 | |
You Wang | 0fc2170 | 2018-11-02 17:49:18 -0700 | [diff] [blame] | 283 | def pingHostSetAlternative( self, dstIPList, wait=1, IPv6=False ): |
| 284 | """ |
| 285 | Description: |
| 286 | Ping a set of destination host. |
| 287 | Params: |
| 288 | dstIPList is a list of destination ip addresses |
| 289 | Returns: |
| 290 | main.TRUE if the destination host is reachable |
| 291 | main.FALSE otherwise |
| 292 | """ |
| 293 | isReachable = main.TRUE |
| 294 | wait = int( wait ) |
| 295 | cmd = "ping" |
| 296 | if IPv6: |
| 297 | cmd = cmd + "6" |
| 298 | cmd = cmd + " -c 1 -i 1 -W " + str( wait ) |
| 299 | try: |
| 300 | for dstIP in dstIPList: |
| 301 | pingCmd = cmd + " " + dstIP |
| 302 | self.handle.sendline( pingCmd ) |
| 303 | i = self.handle.expect( [ self.prompt, |
| 304 | pexpect.TIMEOUT ], |
| 305 | timeout=wait + 5 ) |
| 306 | if i == 0: |
| 307 | response = self.handle.before |
| 308 | if not re.search( ',\s0\%\spacket\sloss', response ): |
| 309 | main.log.debug( "Ping failed between %s and %s" % ( self.name, dstIP ) ) |
| 310 | isReachable = main.FALSE |
| 311 | elif i == 1: |
| 312 | main.log.error( self.name + ": timeout when waiting for response" ) |
| 313 | isReachable = main.FALSE |
| 314 | else: |
| 315 | main.log.error( self.name + ": unknown response: " + self.handle.before ) |
| 316 | isReachable = main.FALSE |
| 317 | except pexpect.TIMEOUT: |
| 318 | main.log.exception( self.name + ": TIMEOUT exception" ) |
| 319 | self.exitFromCmd( [ self.prompt ] ) |
| 320 | isReachable = main.FALSE |
| 321 | except pexpect.EOF: |
| 322 | main.log.error( self.name + ": EOF exception found" ) |
| 323 | main.log.error( self.name + ": " + self.handle.before ) |
| 324 | main.cleanAndExit() |
| 325 | except Exception: |
| 326 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 327 | main.cleanAndExit() |
| 328 | return isReachable |
| 329 | |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 330 | def ifconfig( self, wait=3 ): |
| 331 | """ |
| 332 | Run ifconfig command on host and return output |
| 333 | """ |
| 334 | try: |
| 335 | command = "ifconfig" |
| 336 | main.log.info( self.name + ": Sending: " + command ) |
| 337 | self.handle.sendline( command ) |
| 338 | i = self.handle.expect( [ self.prompt, pexpect.TIMEOUT ], |
Jon Hall | a604fd4 | 2018-05-04 14:27:27 -0700 | [diff] [blame] | 339 | timeout=wait + 5 ) |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 340 | if i == 1: |
| 341 | main.log.error( |
| 342 | self.name + |
| 343 | ": timeout when waiting for response" ) |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 344 | main.log.error( self.name + ": response: " + str( self.handle.before ) ) |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 345 | response = self.handle.before |
| 346 | return response |
| 347 | except pexpect.EOF: |
| 348 | main.log.error( self.name + ": EOF exception found" ) |
| 349 | main.log.error( self.name + ": " + self.handle.before ) |
| 350 | main.cleanAndExit() |
| 351 | except Exception: |
You Wang | 4cc6191 | 2018-08-28 10:10:58 -0700 | [diff] [blame] | 352 | main.log.exception( self.name + ": uncaught exception!" ) |
| 353 | main.cleanAndExit() |
| 354 | |
| 355 | def ip( self, options="a", wait=3 ): |
| 356 | """ |
| 357 | Run ip command on host and return output |
| 358 | """ |
| 359 | try: |
| 360 | command = "ip {}".format( options ) |
| 361 | main.log.info( self.name + ": Sending: " + command ) |
| 362 | self.handle.sendline( command ) |
| 363 | i = self.handle.expect( [ self.prompt, pexpect.TIMEOUT ], |
| 364 | timeout=wait + 5 ) |
| 365 | if i == 1: |
| 366 | main.log.error( self.name + ": timeout when waiting for response" ) |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 367 | main.log.error( self.name + ": response: " + str( self.handle.before ) ) |
You Wang | 4cc6191 | 2018-08-28 10:10:58 -0700 | [diff] [blame] | 368 | response = self.handle.before |
| 369 | return response |
| 370 | except pexpect.EOF: |
| 371 | main.log.error( self.name + ": EOF exception found" ) |
| 372 | main.log.error( self.name + ": " + self.handle.before ) |
| 373 | main.cleanAndExit() |
| 374 | except Exception: |
| 375 | main.log.exception( self.name + ": uncaught exception!" ) |
| 376 | main.cleanAndExit() |
| 377 | |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 378 | def command( self, cmd, wait=3, debug=False): |
You Wang | 4cc6191 | 2018-08-28 10:10:58 -0700 | [diff] [blame] | 379 | """ |
| 380 | Run shell command on host and return output |
| 381 | Required: |
| 382 | cmd: command to run on the host |
| 383 | """ |
| 384 | try: |
| 385 | main.log.info( self.name + ": Sending: " + cmd ) |
| 386 | self.handle.sendline( cmd ) |
| 387 | i = self.handle.expect( [ self.prompt, pexpect.TIMEOUT ], |
| 388 | timeout=wait + 5 ) |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 389 | response = self.handle.before |
| 390 | if debug: |
| 391 | main.log.debug( response ) |
You Wang | 4cc6191 | 2018-08-28 10:10:58 -0700 | [diff] [blame] | 392 | if i == 1: |
| 393 | main.log.error( self.name + ": timeout when waiting for response" ) |
Jon Hall | 43060f6 | 2020-06-23 13:13:33 -0700 | [diff] [blame] | 394 | main.log.error( self.name + ": response: " + str( response ) ) |
You Wang | 4cc6191 | 2018-08-28 10:10:58 -0700 | [diff] [blame] | 395 | return response |
| 396 | except pexpect.EOF: |
| 397 | main.log.error( self.name + ": EOF exception found" ) |
| 398 | main.log.error( self.name + ": " + self.handle.before ) |
| 399 | main.cleanAndExit() |
| 400 | except Exception: |
| 401 | main.log.exception( self.name + ": uncaught exception!" ) |
Pier | 6a0c4de | 2018-03-18 16:01:30 -0700 | [diff] [blame] | 402 | main.cleanAndExit() |