blob: d0cf5598fa5147b5bb37a615e1df2ebeed9968f3 [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:
122 # Disconnect from the host
You Wangb1665b52019-02-01 15:49:48 -0800123 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 Hall43060f62020-06-23 13:13:33 -0700130 main.log.error( self.name + ": response: " + str( self.handle.before ) )
You Wangb1665b52019-02-01 15:49:48 -0800131 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 Hall43060f62020-06-23 13:13:33 -0700136 main.log.warn( self.name + ": response: " + str( self.handle.before ) )
You Wangb1665b52019-02-01 15:49:48 -0800137 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 Hall43060f62020-06-23 13:13:33 -0700141 main.log.warn( self.name + ": response: " + str( self.handle.before ) )
You Wangb1665b52019-02-01 15:49:48 -0800142 return main.TRUE
Pier6a0c4de2018-03-18 16:01:30 -0700143 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 Wang4cc61912018-08-28 10:10:58 -0700157 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 Wangb1665b52019-02-01 15:49:48 -0800205 self.handle.expect( self.prompt, timeout=2 )
You Wang4cc61912018-08-28 10:10:58 -0700206 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 Hall43060f62020-06-23 13:13:33 -0700210 main.log.error( self.name + ": response: " + str( self.handle.before ) )
You Wang4cc61912018-08-28 10:10:58 -0700211 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 Hallceed0a32021-06-08 11:07:00 -0700221 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 )
Jon Hall32c90f32021-06-24 16:32:44 -0700227 if "Command 'ifconfig' not found" in response:
228 # ip a show dev
229 cmd = "ip addr show %s" % iface if iface else ""
230 response = self.command( cmd )
231 pattern = ''
232 if proto == 'IPV4':
233 pattern = "inet\s(\d+\.\d+\.\d+\.\d+)/\d+"
234 else:
235 pattern = "inet6\s([\w,:]*)/\d+"
Jon Hallceed0a32021-06-08 11:07:00 -0700236 else:
Jon Hall32c90f32021-06-24 16:32:44 -0700237 pattern = ''
238 if proto == 'IPV4':
239 pattern = "inet\s(\d+\.\d+\.\d+\.\d+)\s\snetmask"
240 else:
241 pattern = "inet6\s([\w,:]*)/\d+\s\sprefixlen"
Jon Hallceed0a32021-06-08 11:07:00 -0700242 ipAddressSearch = re.search( pattern, response )
243 if not ipAddressSearch:
244 return None
245 main.log.info(
246 self.name +
247 ": IP-Address is " +
248 ipAddressSearch.group( 1 ) )
249 return ipAddressSearch.group( 1 )
250
Jon Hall43060f62020-06-23 13:13:33 -0700251 def ping( self, dst, ipv6=False, interface=None, wait=3 ):
Pier6a0c4de2018-03-18 16:01:30 -0700252 """
253 Description:
254 Ping from this host to another
255 Required:
256 dst: IP address of destination host
257 Optional:
258 ipv6: will use ping6 command if True; otherwise use ping command
Jon Hall43060f62020-06-23 13:13:33 -0700259 interface: Specify which interface to use for the ping
Pier6a0c4de2018-03-18 16:01:30 -0700260 wait: timeout for ping command
261 """
262 try:
263 command = "ping6" if ipv6 else "ping"
Jon Hall43060f62020-06-23 13:13:33 -0700264 if interface:
265 command += " -I %s " % interface
Pier6a0c4de2018-03-18 16:01:30 -0700266 command += " -c 1 -i 1 -W " + str( wait ) + " " + str( dst )
267 main.log.info( self.name + ": Sending: " + command )
268 self.handle.sendline( command )
269 i = self.handle.expect( [ self.prompt, pexpect.TIMEOUT ],
Jon Halla604fd42018-05-04 14:27:27 -0700270 timeout=wait + 5 )
Jon Hall06fd0df2021-01-25 15:50:06 -0800271 response = self.handle.before
Pier6a0c4de2018-03-18 16:01:30 -0700272 if i == 1:
273 main.log.error(
274 self.name +
275 ": timeout when waiting for response" )
Jon Hall43060f62020-06-23 13:13:33 -0700276 main.log.error( self.name + ": response: " + str( self.handle.before ) )
Pier6a0c4de2018-03-18 16:01:30 -0700277 if re.search( ',\s0\%\spacket\sloss', response ):
278 main.log.info( self.name + ": no packets lost, host is reachable" )
279 return main.TRUE
280 else:
281 main.log.warn(
282 self.name +
283 ": PACKET LOST, HOST IS NOT REACHABLE" )
284 return main.FALSE
285 except pexpect.EOF:
286 main.log.error( self.name + ": EOF exception found" )
287 main.log.error( self.name + ": " + self.handle.before )
288 main.cleanAndExit()
289 except Exception:
290 main.log.exception( self.name + ": Uncaught exception!" )
291 main.cleanAndExit()
292
You Wang0fc21702018-11-02 17:49:18 -0700293 def pingHostSetAlternative( self, dstIPList, wait=1, IPv6=False ):
294 """
295 Description:
296 Ping a set of destination host.
297 Params:
298 dstIPList is a list of destination ip addresses
299 Returns:
300 main.TRUE if the destination host is reachable
301 main.FALSE otherwise
302 """
303 isReachable = main.TRUE
304 wait = int( wait )
305 cmd = "ping"
306 if IPv6:
307 cmd = cmd + "6"
308 cmd = cmd + " -c 1 -i 1 -W " + str( wait )
309 try:
310 for dstIP in dstIPList:
311 pingCmd = cmd + " " + dstIP
312 self.handle.sendline( pingCmd )
313 i = self.handle.expect( [ self.prompt,
314 pexpect.TIMEOUT ],
315 timeout=wait + 5 )
316 if i == 0:
317 response = self.handle.before
318 if not re.search( ',\s0\%\spacket\sloss', response ):
319 main.log.debug( "Ping failed between %s and %s" % ( self.name, dstIP ) )
320 isReachable = main.FALSE
321 elif i == 1:
322 main.log.error( self.name + ": timeout when waiting for response" )
323 isReachable = main.FALSE
324 else:
325 main.log.error( self.name + ": unknown response: " + self.handle.before )
326 isReachable = main.FALSE
327 except pexpect.TIMEOUT:
328 main.log.exception( self.name + ": TIMEOUT exception" )
329 self.exitFromCmd( [ self.prompt ] )
330 isReachable = main.FALSE
331 except pexpect.EOF:
332 main.log.error( self.name + ": EOF exception found" )
333 main.log.error( self.name + ": " + self.handle.before )
334 main.cleanAndExit()
335 except Exception:
336 main.log.exception( self.name + ": Uncaught exception!" )
337 main.cleanAndExit()
338 return isReachable
339
Pier6a0c4de2018-03-18 16:01:30 -0700340 def ifconfig( self, wait=3 ):
341 """
342 Run ifconfig command on host and return output
343 """
344 try:
345 command = "ifconfig"
346 main.log.info( self.name + ": Sending: " + command )
347 self.handle.sendline( command )
Jon Hall32c90f32021-06-24 16:32:44 -0700348 i = self.handle.expect( [ "Command 'ifconfig' not found", self.prompt, pexpect.TIMEOUT ],
Jon Halla604fd42018-05-04 14:27:27 -0700349 timeout=wait + 5 )
Jon Hall32c90f32021-06-24 16:32:44 -0700350 if i == 0:
351 response = self.handle.before
352 self.handle.expect( [ self.prompt, pexpect.TIMEOUT ] )
353 response += str( self.handle.before )
354 main.log.error( self.name + ": Error running ifconfig: %s " % response )
355 return response
356 if i == 2:
Pier6a0c4de2018-03-18 16:01:30 -0700357 main.log.error(
358 self.name +
359 ": timeout when waiting for response" )
Jon Hall43060f62020-06-23 13:13:33 -0700360 main.log.error( self.name + ": response: " + str( self.handle.before ) )
Pier6a0c4de2018-03-18 16:01:30 -0700361 response = self.handle.before
362 return response
363 except pexpect.EOF:
364 main.log.error( self.name + ": EOF exception found" )
365 main.log.error( self.name + ": " + self.handle.before )
366 main.cleanAndExit()
367 except Exception:
You Wang4cc61912018-08-28 10:10:58 -0700368 main.log.exception( self.name + ": uncaught exception!" )
369 main.cleanAndExit()
370
371 def ip( self, options="a", wait=3 ):
372 """
373 Run ip command on host and return output
374 """
375 try:
376 command = "ip {}".format( options )
377 main.log.info( self.name + ": Sending: " + command )
378 self.handle.sendline( command )
379 i = self.handle.expect( [ self.prompt, pexpect.TIMEOUT ],
380 timeout=wait + 5 )
381 if i == 1:
382 main.log.error( self.name + ": timeout when waiting for response" )
Jon Hall43060f62020-06-23 13:13:33 -0700383 main.log.error( self.name + ": response: " + str( self.handle.before ) )
You Wang4cc61912018-08-28 10:10:58 -0700384 response = self.handle.before
385 return response
386 except pexpect.EOF:
387 main.log.error( self.name + ": EOF exception found" )
388 main.log.error( self.name + ": " + self.handle.before )
389 main.cleanAndExit()
390 except Exception:
391 main.log.exception( self.name + ": uncaught exception!" )
392 main.cleanAndExit()
393
Jon Hall43060f62020-06-23 13:13:33 -0700394 def command( self, cmd, wait=3, debug=False):
You Wang4cc61912018-08-28 10:10:58 -0700395 """
396 Run shell command on host and return output
397 Required:
398 cmd: command to run on the host
399 """
400 try:
401 main.log.info( self.name + ": Sending: " + cmd )
402 self.handle.sendline( cmd )
403 i = self.handle.expect( [ self.prompt, pexpect.TIMEOUT ],
404 timeout=wait + 5 )
Jon Hall43060f62020-06-23 13:13:33 -0700405 response = self.handle.before
406 if debug:
407 main.log.debug( response )
You Wang4cc61912018-08-28 10:10:58 -0700408 if i == 1:
409 main.log.error( self.name + ": timeout when waiting for response" )
Jon Hall43060f62020-06-23 13:13:33 -0700410 main.log.error( self.name + ": response: " + str( response ) )
You Wang4cc61912018-08-28 10:10:58 -0700411 return response
412 except pexpect.EOF:
413 main.log.error( self.name + ": EOF exception found" )
414 main.log.error( self.name + ": " + self.handle.before )
415 main.cleanAndExit()
416 except Exception:
417 main.log.exception( self.name + ": uncaught exception!" )
Pier6a0c4de2018-03-18 16:01:30 -0700418 main.cleanAndExit()