blob: 7111c7469886ecd1ae4184da1c3ecb4d990b2b3a [file] [log] [blame]
Pier6a0c4de2018-03-18 16:01:30 -07001#!/usr/bin/env python
2"""
3Copyright 2018 Open Networking Foundation (ONF)
4
5Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>,
6the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>,
7or the System Testing Guide page at <https://wiki.onosproject.org/x/WYQg>
8
9TestON is free software: you can redistribute it and/or modify
10it under the terms of the GNU General Public License as published by
11the Free Software Foundation, either version 2 of the License, or
12( at your option ) any later version.
13
14TestON is distributed in the hope that it will be useful,
15but WITHOUT ANY WARRANTY; without even the implied warranty of
16MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17GNU General Public License for more details.
18
19You should have received a copy of the GNU General Public License
20along with TestON. If not, see <http://www.gnu.org/licenses/>.
21"""
22
23import pexpect
24import re
Pier6a0c4de2018-03-18 16:01:30 -070025import os
You Wang4cc61912018-08-28 10:10:58 -070026from drivers.common.cli.emulator.scapyclidriver import ScapyCliDriver
Pier6a0c4de2018-03-18 16:01:30 -070027
You Wang4cc61912018-08-28 10:10:58 -070028class HostDriver( ScapyCliDriver ):
Pier6a0c4de2018-03-18 16:01:30 -070029 """
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 Wangb1665b52019-02-01 15:49:48 -080037 self.interfaces = []
Pier6a0c4de2018-03-18 16:01:30 -070038 self.home = None
You Wang4cc61912018-08-28 10:10:58 -070039 self.inband = False
40 self.prompt = "\$"
41 self.scapyPrompt = ">>>"
Pier6a0c4de2018-03-18 16:01:30 -070042
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 Wangb1665b52019-02-01 15:49:48 -080056 self.interfaces.append( { 'ips': [ self.options[ 'ip' ] ],
57 'isUp': True,
58 'mac': self.options[ 'mac' ],
Jon Hall4d956de2021-06-09 10:09:20 -070059 'dhcp': self.options.get( 'dhcp', "False" ),
Jon Hall43060f62020-06-23 13:13:33 -070060 'name': self.options.get( 'interfaceName', None ) } )
Pier6a0c4de2018-03-18 16:01:30 -070061
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 Hallceed0a32021-06-08 11:07:00 -070084 # 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
Pier6a0c4de2018-03-18 16:01:30 -070093 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:
Jon Hall49d781c2021-07-29 11:23:46 -0700122 self.preDisconnect()
Pier6a0c4de2018-03-18 16:01:30 -0700123 # Disconnect from the host
You Wangb1665b52019-02-01 15:49:48 -0800124 if not self.options[ 'inband' ] == 'True':
125 self.handle.sendline( "" )
126 self.handle.expect( self.prompt )
127 self.handle.sendline( "exit" )
128 i = self.handle.expect( [ "closed", pexpect.TIMEOUT ] )
129 if i == 1:
130 main.log.error( self.name + ": timeout when waiting for response" )
Jon Hall43060f62020-06-23 13:13:33 -0700131 main.log.error( self.name + ": response: " + str( self.handle.before ) )
You Wangb1665b52019-02-01 15:49:48 -0800132 else:
133 self.handle.sendline( "" )
134 i = self.handle.expect( [ self.prompt, pexpect.TIMEOUT ], timeout=2 )
135 if i == 1:
136 main.log.warn( self.name + ": timeout when waiting for response" )
Jon Hall43060f62020-06-23 13:13:33 -0700137 main.log.warn( self.name + ": response: " + str( self.handle.before ) )
You Wangb1665b52019-02-01 15:49:48 -0800138 self.handle.sendline( "exit" )
139 i = self.handle.expect( [ "closed", pexpect.TIMEOUT ], timeout=2 )
140 if i == 1:
141 main.log.warn( self.name + ": timeout when waiting for response" )
Jon Hall43060f62020-06-23 13:13:33 -0700142 main.log.warn( self.name + ": response: " + str( self.handle.before ) )
You Wangb1665b52019-02-01 15:49:48 -0800143 return main.TRUE
Pier6a0c4de2018-03-18 16:01:30 -0700144 except TypeError:
145 main.log.exception( self.name + ": Object not as expected" )
146 response = main.FALSE
147 except pexpect.EOF:
148 main.log.error( self.name + ": EOF exception found" )
149 main.log.error( self.name + ": " + self.handle.before )
150 except ValueError:
151 main.log.exception( "Exception in disconnect of " + self.name )
152 response = main.TRUE
153 except Exception:
154 main.log.exception( self.name + ": Connection failed to the host" )
155 response = main.FALSE
156 return response
157
You Wang4cc61912018-08-28 10:10:58 -0700158 def connectInband( self ):
159 """
160 ssh to the host using its data plane IP
161 """
162 try:
163 if not self.options[ 'inband' ] == 'True':
164 main.log.info( "Skip connecting the host via data plane" )
165 return main.TRUE
166 self.handle.sendline( "" )
167 self.handle.expect( self.prompt )
168 self.handle.sendline( "ssh {}@{}".format( self.options[ 'username' ],
169 self.options[ 'ip' ] ) )
170 i = self.handle.expect( [ "password:|Password:", self.prompt, pexpect.TIMEOUT ], timeout=30 )
171 if i == 0:
172 self.handle.sendline( self.options[ 'password' ] )
173 j = self.handle.expect( [ "password:|Password:", self.prompt, pexpect.TIMEOUT ], timeout=10 )
174 if j != 1:
175 main.log.error( "Incorrect password" )
176 return main.FALSE
177 elif i == 1:
178 main.log.info( "Password not required logged in" )
179 else:
180 main.log.error( "Failed to connect to the host" )
181 return main.FALSE
182 self.inband = True
183 return main.TRUE
184 except KeyError:
185 main.log.error( self.name + ": host component not as expected" )
186 main.log.error( self.name + ": " + self.handle.before )
187 return main.FALSE
188 except pexpect.EOF:
189 main.log.error( self.name + ": EOF exception found" )
190 main.log.error( self.name + ": " + self.handle.before )
191 return main.FALSE
192 except Exception:
193 main.log.exception( self.name + ": Uncaught exception!" )
194 main.log.error( self.name + ": " + self.handle.before )
195 return main.FALSE
196
197 def disconnectInband( self ):
198 """
199 Terminate the ssh connection to the host's data plane IP
200 """
201 try:
202 if not self.options[ 'inband' ] == 'True':
203 main.log.info( "Skip disconnecting the host via data plane" )
204 return main.TRUE
205 self.handle.sendline( "" )
You Wangb1665b52019-02-01 15:49:48 -0800206 self.handle.expect( self.prompt, timeout=2 )
You Wang4cc61912018-08-28 10:10:58 -0700207 self.handle.sendline( "exit" )
208 i = self.handle.expect( [ "closed", pexpect.TIMEOUT ], timeout=2 )
209 if i == 1:
210 main.log.error( self.name + ": timeout when waiting for response" )
Jon Hall43060f62020-06-23 13:13:33 -0700211 main.log.error( self.name + ": response: " + str( self.handle.before ) )
You Wang4cc61912018-08-28 10:10:58 -0700212 return main.TRUE
213 except pexpect.EOF:
214 main.log.error( self.name + ": EOF exception found" )
215 main.log.error( self.name + ": " + self.handle.before )
216 return main.FALSE
217 except Exception:
218 main.log.exception( self.name + ": Uncaught exception!" )
219 main.log.error( self.name + ": " + self.handle.before )
220 return main.FALSE
221
Jon Hallceed0a32021-06-08 11:07:00 -0700222 def getIPAddress( self, iface=None, proto='IPV4' ):
223 """
224 Returns IP address of the host
225 """
226 cmd = "ifconfig %s" % iface if iface else ""
227 response = self.command( cmd )
Jon Hall32c90f32021-06-24 16:32:44 -0700228 if "Command 'ifconfig' not found" in response:
229 # ip a show dev
230 cmd = "ip addr show %s" % iface if iface else ""
231 response = self.command( cmd )
232 pattern = ''
233 if proto == 'IPV4':
234 pattern = "inet\s(\d+\.\d+\.\d+\.\d+)/\d+"
235 else:
236 pattern = "inet6\s([\w,:]*)/\d+"
Jon Hallceed0a32021-06-08 11:07:00 -0700237 else:
Jon Hall32c90f32021-06-24 16:32:44 -0700238 pattern = ''
239 if proto == 'IPV4':
240 pattern = "inet\s(\d+\.\d+\.\d+\.\d+)\s\snetmask"
241 else:
242 pattern = "inet6\s([\w,:]*)/\d+\s\sprefixlen"
Jon Hallceed0a32021-06-08 11:07:00 -0700243 ipAddressSearch = re.search( pattern, response )
244 if not ipAddressSearch:
245 return None
246 main.log.info(
247 self.name +
248 ": IP-Address is " +
249 ipAddressSearch.group( 1 ) )
250 return ipAddressSearch.group( 1 )
251
Jon Hall43060f62020-06-23 13:13:33 -0700252 def ping( self, dst, ipv6=False, interface=None, wait=3 ):
Pier6a0c4de2018-03-18 16:01:30 -0700253 """
254 Description:
255 Ping from this host to another
256 Required:
257 dst: IP address of destination host
258 Optional:
259 ipv6: will use ping6 command if True; otherwise use ping command
Jon Hall43060f62020-06-23 13:13:33 -0700260 interface: Specify which interface to use for the ping
Pier6a0c4de2018-03-18 16:01:30 -0700261 wait: timeout for ping command
262 """
263 try:
264 command = "ping6" if ipv6 else "ping"
Jon Hall43060f62020-06-23 13:13:33 -0700265 if interface:
266 command += " -I %s " % interface
Pier6a0c4de2018-03-18 16:01:30 -0700267 command += " -c 1 -i 1 -W " + str( wait ) + " " + str( dst )
268 main.log.info( self.name + ": Sending: " + command )
269 self.handle.sendline( command )
270 i = self.handle.expect( [ self.prompt, pexpect.TIMEOUT ],
Jon Halla604fd42018-05-04 14:27:27 -0700271 timeout=wait + 5 )
Jon Hall06fd0df2021-01-25 15:50:06 -0800272 response = self.handle.before
Pier6a0c4de2018-03-18 16:01:30 -0700273 if i == 1:
274 main.log.error(
275 self.name +
276 ": timeout when waiting for response" )
Jon Hall43060f62020-06-23 13:13:33 -0700277 main.log.error( self.name + ": response: " + str( self.handle.before ) )
Pier6a0c4de2018-03-18 16:01:30 -0700278 if re.search( ',\s0\%\spacket\sloss', response ):
279 main.log.info( self.name + ": no packets lost, host is reachable" )
280 return main.TRUE
281 else:
282 main.log.warn(
283 self.name +
284 ": PACKET LOST, HOST IS NOT REACHABLE" )
285 return main.FALSE
286 except pexpect.EOF:
287 main.log.error( self.name + ": EOF exception found" )
288 main.log.error( self.name + ": " + self.handle.before )
289 main.cleanAndExit()
290 except Exception:
291 main.log.exception( self.name + ": Uncaught exception!" )
292 main.cleanAndExit()
293
You Wang0fc21702018-11-02 17:49:18 -0700294 def pingHostSetAlternative( self, dstIPList, wait=1, IPv6=False ):
295 """
296 Description:
297 Ping a set of destination host.
298 Params:
299 dstIPList is a list of destination ip addresses
300 Returns:
301 main.TRUE if the destination host is reachable
302 main.FALSE otherwise
303 """
304 isReachable = main.TRUE
305 wait = int( wait )
306 cmd = "ping"
307 if IPv6:
308 cmd = cmd + "6"
309 cmd = cmd + " -c 1 -i 1 -W " + str( wait )
310 try:
311 for dstIP in dstIPList:
312 pingCmd = cmd + " " + dstIP
313 self.handle.sendline( pingCmd )
314 i = self.handle.expect( [ self.prompt,
315 pexpect.TIMEOUT ],
316 timeout=wait + 5 )
317 if i == 0:
318 response = self.handle.before
319 if not re.search( ',\s0\%\spacket\sloss', response ):
320 main.log.debug( "Ping failed between %s and %s" % ( self.name, dstIP ) )
321 isReachable = main.FALSE
322 elif i == 1:
323 main.log.error( self.name + ": timeout when waiting for response" )
324 isReachable = main.FALSE
325 else:
326 main.log.error( self.name + ": unknown response: " + self.handle.before )
327 isReachable = main.FALSE
328 except pexpect.TIMEOUT:
329 main.log.exception( self.name + ": TIMEOUT exception" )
330 self.exitFromCmd( [ self.prompt ] )
331 isReachable = main.FALSE
332 except pexpect.EOF:
333 main.log.error( self.name + ": EOF exception found" )
334 main.log.error( self.name + ": " + self.handle.before )
335 main.cleanAndExit()
336 except Exception:
337 main.log.exception( self.name + ": Uncaught exception!" )
338 main.cleanAndExit()
339 return isReachable
340
Pier6a0c4de2018-03-18 16:01:30 -0700341 def ifconfig( self, wait=3 ):
342 """
343 Run ifconfig command on host and return output
344 """
345 try:
346 command = "ifconfig"
347 main.log.info( self.name + ": Sending: " + command )
348 self.handle.sendline( command )
Jon Hall32c90f32021-06-24 16:32:44 -0700349 i = self.handle.expect( [ "Command 'ifconfig' not found", self.prompt, pexpect.TIMEOUT ],
Jon Halla604fd42018-05-04 14:27:27 -0700350 timeout=wait + 5 )
Jon Hall32c90f32021-06-24 16:32:44 -0700351 if i == 0:
352 response = self.handle.before
353 self.handle.expect( [ self.prompt, pexpect.TIMEOUT ] )
354 response += str( self.handle.before )
355 main.log.error( self.name + ": Error running ifconfig: %s " % response )
356 return response
357 if i == 2:
Pier6a0c4de2018-03-18 16:01:30 -0700358 main.log.error(
359 self.name +
360 ": timeout when waiting for response" )
Jon Hall43060f62020-06-23 13:13:33 -0700361 main.log.error( self.name + ": response: " + str( self.handle.before ) )
Pier6a0c4de2018-03-18 16:01:30 -0700362 response = self.handle.before
363 return response
364 except pexpect.EOF:
365 main.log.error( self.name + ": EOF exception found" )
366 main.log.error( self.name + ": " + self.handle.before )
367 main.cleanAndExit()
368 except Exception:
You Wang4cc61912018-08-28 10:10:58 -0700369 main.log.exception( self.name + ": uncaught exception!" )
370 main.cleanAndExit()
371
372 def ip( self, options="a", wait=3 ):
373 """
374 Run ip command on host and return output
375 """
376 try:
377 command = "ip {}".format( options )
378 main.log.info( self.name + ": Sending: " + command )
379 self.handle.sendline( command )
380 i = self.handle.expect( [ self.prompt, pexpect.TIMEOUT ],
381 timeout=wait + 5 )
382 if i == 1:
383 main.log.error( self.name + ": timeout when waiting for response" )
Jon Hall43060f62020-06-23 13:13:33 -0700384 main.log.error( self.name + ": response: " + str( self.handle.before ) )
You Wang4cc61912018-08-28 10:10:58 -0700385 response = self.handle.before
386 return response
387 except pexpect.EOF:
388 main.log.error( self.name + ": EOF exception found" )
389 main.log.error( self.name + ": " + self.handle.before )
390 main.cleanAndExit()
391 except Exception:
392 main.log.exception( self.name + ": uncaught exception!" )
393 main.cleanAndExit()
394
Jon Hall43060f62020-06-23 13:13:33 -0700395 def command( self, cmd, wait=3, debug=False):
You Wang4cc61912018-08-28 10:10:58 -0700396 """
397 Run shell command on host and return output
398 Required:
399 cmd: command to run on the host
400 """
401 try:
402 main.log.info( self.name + ": Sending: " + cmd )
403 self.handle.sendline( cmd )
404 i = self.handle.expect( [ self.prompt, pexpect.TIMEOUT ],
405 timeout=wait + 5 )
Jon Hall43060f62020-06-23 13:13:33 -0700406 response = self.handle.before
407 if debug:
408 main.log.debug( response )
You Wang4cc61912018-08-28 10:10:58 -0700409 if i == 1:
410 main.log.error( self.name + ": timeout when waiting for response" )
Jon Hall43060f62020-06-23 13:13:33 -0700411 main.log.error( self.name + ": response: " + str( response ) )
You Wang4cc61912018-08-28 10:10:58 -0700412 return response
413 except pexpect.EOF:
414 main.log.error( self.name + ": EOF exception found" )
415 main.log.error( self.name + ": " + self.handle.before )
416 main.cleanAndExit()
417 except Exception:
418 main.log.exception( self.name + ": uncaught exception!" )
Pier6a0c4de2018-03-18 16:01:30 -0700419 main.cleanAndExit()