blob: 7c7cd80386b76a7487af0f6053af1a3ee08c1eaa [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
25import sys
26import types
27import os
28import time
29from math import pow
You Wang4cc61912018-08-28 10:10:58 -070030from drivers.common.cli.emulator.scapyclidriver import ScapyCliDriver
Pier6a0c4de2018-03-18 16:01:30 -070031
You Wang4cc61912018-08-28 10:10:58 -070032class HostDriver( ScapyCliDriver ):
Pier6a0c4de2018-03-18 16:01:30 -070033 """
34 This class is created as a standalone host driver.
35 """
36 def __init__( self ):
37 super( HostDriver, self ).__init__()
38 self.handle = self
39 self.name = None
40 self.shortName = None
You Wangb1665b52019-02-01 15:49:48 -080041 self.interfaces = []
Pier6a0c4de2018-03-18 16:01:30 -070042 self.home = None
You Wang4cc61912018-08-28 10:10:58 -070043 self.inband = False
44 self.prompt = "\$"
45 self.scapyPrompt = ">>>"
Pier6a0c4de2018-03-18 16:01:30 -070046
47 def connect( self, **connectargs ):
48 """
49 Creates ssh handle for host.
50 NOTE:
51 The ip_address would come from the topo file using the host tag, the
52 value can be an environment variable as well as a "localhost" to get
53 the ip address needed to ssh to the "bench"
54 """
55 try:
56 for key in connectargs:
57 vars( self )[ key ] = connectargs[ key ]
58 self.name = self.options[ 'name' ]
59 self.shortName = self.options[ 'shortName' ]
You Wangb1665b52019-02-01 15:49:48 -080060 self.interfaces.append( { 'ips': [ self.options[ 'ip' ] ],
61 'isUp': True,
62 'mac': self.options[ 'mac' ],
63 'name': None } )
Pier6a0c4de2018-03-18 16:01:30 -070064
65 try:
66 if os.getenv( str( self.ip_address ) ) is not None:
67 self.ip_address = os.getenv( str( self.ip_address ) )
68 else:
69 main.log.info( self.name +
70 ": Trying to connect to " +
71 self.ip_address )
72 except KeyError:
73 main.log.info( "Invalid host name," +
74 " connecting to local host instead" )
75 self.ip_address = 'localhost'
76 except Exception as inst:
77 main.log.error( "Uncaught exception: " + str( inst ) )
78
79 self.handle = super(
80 HostDriver,
81 self ).connect(
82 user_name=self.user_name,
83 ip_address=self.ip_address,
84 port=None,
85 pwd=self.pwd )
86
87 if self.handle:
88 main.log.info( "Connection successful to the " +
89 self.user_name +
90 "@" +
91 self.ip_address )
92 self.handle.sendline( "" )
93 self.handle.expect( self.prompt )
94 return main.TRUE
95 else:
96 main.log.error( "Connection failed to " +
97 self.user_name +
98 "@" +
99 self.ip_address )
100 return main.FALSE
101 except pexpect.EOF:
102 main.log.error( self.name + ": EOF exception found" )
103 main.log.error( self.name + ": " + self.handle.before )
104 main.cleanAndExit()
105 except Exception:
106 main.log.exception( self.name + ": Uncaught exception!" )
107 main.cleanAndExit()
108
109 def disconnect( self, **connectargs ):
110 """
111 Called when test is complete to disconnect the handle.
112 """
113 response = main.TRUE
114 try:
115 if self.handle:
116 # Disconnect from the host
You Wangb1665b52019-02-01 15:49:48 -0800117 if not self.options[ 'inband' ] == 'True':
118 self.handle.sendline( "" )
119 self.handle.expect( self.prompt )
120 self.handle.sendline( "exit" )
121 i = self.handle.expect( [ "closed", pexpect.TIMEOUT ] )
122 if i == 1:
123 main.log.error( self.name + ": timeout when waiting for response" )
124 main.log.error( "response: " + str( self.handle.before ) )
125 else:
126 self.handle.sendline( "" )
127 i = self.handle.expect( [ self.prompt, pexpect.TIMEOUT ], timeout=2 )
128 if i == 1:
129 main.log.warn( self.name + ": timeout when waiting for response" )
130 main.log.warn( "response: " + str( self.handle.before ) )
131 self.handle.sendline( "exit" )
132 i = self.handle.expect( [ "closed", pexpect.TIMEOUT ], timeout=2 )
133 if i == 1:
134 main.log.warn( self.name + ": timeout when waiting for response" )
135 main.log.warn( "response: " + str( self.handle.before ) )
136 return main.TRUE
Pier6a0c4de2018-03-18 16:01:30 -0700137 except TypeError:
138 main.log.exception( self.name + ": Object not as expected" )
139 response = main.FALSE
140 except pexpect.EOF:
141 main.log.error( self.name + ": EOF exception found" )
142 main.log.error( self.name + ": " + self.handle.before )
143 except ValueError:
144 main.log.exception( "Exception in disconnect of " + self.name )
145 response = main.TRUE
146 except Exception:
147 main.log.exception( self.name + ": Connection failed to the host" )
148 response = main.FALSE
149 return response
150
You Wang4cc61912018-08-28 10:10:58 -0700151 def connectInband( self ):
152 """
153 ssh to the host using its data plane IP
154 """
155 try:
156 if not self.options[ 'inband' ] == 'True':
157 main.log.info( "Skip connecting the host via data plane" )
158 return main.TRUE
159 self.handle.sendline( "" )
160 self.handle.expect( self.prompt )
161 self.handle.sendline( "ssh {}@{}".format( self.options[ 'username' ],
162 self.options[ 'ip' ] ) )
163 i = self.handle.expect( [ "password:|Password:", self.prompt, pexpect.TIMEOUT ], timeout=30 )
164 if i == 0:
165 self.handle.sendline( self.options[ 'password' ] )
166 j = self.handle.expect( [ "password:|Password:", self.prompt, pexpect.TIMEOUT ], timeout=10 )
167 if j != 1:
168 main.log.error( "Incorrect password" )
169 return main.FALSE
170 elif i == 1:
171 main.log.info( "Password not required logged in" )
172 else:
173 main.log.error( "Failed to connect to the host" )
174 return main.FALSE
175 self.inband = True
176 return main.TRUE
177 except KeyError:
178 main.log.error( self.name + ": host component not as expected" )
179 main.log.error( self.name + ": " + self.handle.before )
180 return main.FALSE
181 except pexpect.EOF:
182 main.log.error( self.name + ": EOF exception found" )
183 main.log.error( self.name + ": " + self.handle.before )
184 return main.FALSE
185 except Exception:
186 main.log.exception( self.name + ": Uncaught exception!" )
187 main.log.error( self.name + ": " + self.handle.before )
188 return main.FALSE
189
190 def disconnectInband( self ):
191 """
192 Terminate the ssh connection to the host's data plane IP
193 """
194 try:
195 if not self.options[ 'inband' ] == 'True':
196 main.log.info( "Skip disconnecting the host via data plane" )
197 return main.TRUE
198 self.handle.sendline( "" )
You Wangb1665b52019-02-01 15:49:48 -0800199 self.handle.expect( self.prompt, timeout=2 )
You Wang4cc61912018-08-28 10:10:58 -0700200 self.handle.sendline( "exit" )
201 i = self.handle.expect( [ "closed", pexpect.TIMEOUT ], timeout=2 )
202 if i == 1:
203 main.log.error( self.name + ": timeout when waiting for response" )
204 main.log.error( "response: " + str( self.handle.before ) )
205 return main.TRUE
206 except pexpect.EOF:
207 main.log.error( self.name + ": EOF exception found" )
208 main.log.error( self.name + ": " + self.handle.before )
209 return main.FALSE
210 except Exception:
211 main.log.exception( self.name + ": Uncaught exception!" )
212 main.log.error( self.name + ": " + self.handle.before )
213 return main.FALSE
214
Pier6a0c4de2018-03-18 16:01:30 -0700215 def ping( self, dst, ipv6=False, wait=3 ):
216 """
217 Description:
218 Ping from this host to another
219 Required:
220 dst: IP address of destination host
221 Optional:
222 ipv6: will use ping6 command if True; otherwise use ping command
223 wait: timeout for ping command
224 """
225 try:
226 command = "ping6" if ipv6 else "ping"
227 command += " -c 1 -i 1 -W " + str( wait ) + " " + str( dst )
228 main.log.info( self.name + ": Sending: " + command )
229 self.handle.sendline( command )
230 i = self.handle.expect( [ self.prompt, pexpect.TIMEOUT ],
Jon Halla604fd42018-05-04 14:27:27 -0700231 timeout=wait + 5 )
Pier6a0c4de2018-03-18 16:01:30 -0700232 if i == 1:
233 main.log.error(
234 self.name +
235 ": timeout when waiting for response" )
236 main.log.error( "response: " + str( self.handle.before ) )
237 self.handle.sendline( "" )
238 self.handle.expect( self.prompt )
239 response = self.handle.before
240 if re.search( ',\s0\%\spacket\sloss', response ):
241 main.log.info( self.name + ": no packets lost, host is reachable" )
242 return main.TRUE
243 else:
244 main.log.warn(
245 self.name +
246 ": PACKET LOST, HOST IS NOT REACHABLE" )
247 return main.FALSE
248 except pexpect.EOF:
249 main.log.error( self.name + ": EOF exception found" )
250 main.log.error( self.name + ": " + self.handle.before )
251 main.cleanAndExit()
252 except Exception:
253 main.log.exception( self.name + ": Uncaught exception!" )
254 main.cleanAndExit()
255
You Wang0fc21702018-11-02 17:49:18 -0700256 def pingHostSetAlternative( self, dstIPList, wait=1, IPv6=False ):
257 """
258 Description:
259 Ping a set of destination host.
260 Params:
261 dstIPList is a list of destination ip addresses
262 Returns:
263 main.TRUE if the destination host is reachable
264 main.FALSE otherwise
265 """
266 isReachable = main.TRUE
267 wait = int( wait )
268 cmd = "ping"
269 if IPv6:
270 cmd = cmd + "6"
271 cmd = cmd + " -c 1 -i 1 -W " + str( wait )
272 try:
273 for dstIP in dstIPList:
274 pingCmd = cmd + " " + dstIP
275 self.handle.sendline( pingCmd )
276 i = self.handle.expect( [ self.prompt,
277 pexpect.TIMEOUT ],
278 timeout=wait + 5 )
279 if i == 0:
280 response = self.handle.before
281 if not re.search( ',\s0\%\spacket\sloss', response ):
282 main.log.debug( "Ping failed between %s and %s" % ( self.name, dstIP ) )
283 isReachable = main.FALSE
284 elif i == 1:
285 main.log.error( self.name + ": timeout when waiting for response" )
286 isReachable = main.FALSE
287 else:
288 main.log.error( self.name + ": unknown response: " + self.handle.before )
289 isReachable = main.FALSE
290 except pexpect.TIMEOUT:
291 main.log.exception( self.name + ": TIMEOUT exception" )
292 self.exitFromCmd( [ self.prompt ] )
293 isReachable = main.FALSE
294 except pexpect.EOF:
295 main.log.error( self.name + ": EOF exception found" )
296 main.log.error( self.name + ": " + self.handle.before )
297 main.cleanAndExit()
298 except Exception:
299 main.log.exception( self.name + ": Uncaught exception!" )
300 main.cleanAndExit()
301 return isReachable
302
Pier6a0c4de2018-03-18 16:01:30 -0700303 def ifconfig( self, wait=3 ):
304 """
305 Run ifconfig command on host and return output
306 """
307 try:
308 command = "ifconfig"
309 main.log.info( self.name + ": Sending: " + command )
310 self.handle.sendline( command )
311 i = self.handle.expect( [ self.prompt, pexpect.TIMEOUT ],
Jon Halla604fd42018-05-04 14:27:27 -0700312 timeout=wait + 5 )
Pier6a0c4de2018-03-18 16:01:30 -0700313 if i == 1:
314 main.log.error(
315 self.name +
316 ": timeout when waiting for response" )
317 main.log.error( "response: " + str( self.handle.before ) )
Pier6a0c4de2018-03-18 16:01:30 -0700318 response = self.handle.before
319 return response
320 except pexpect.EOF:
321 main.log.error( self.name + ": EOF exception found" )
322 main.log.error( self.name + ": " + self.handle.before )
323 main.cleanAndExit()
324 except Exception:
You Wang4cc61912018-08-28 10:10:58 -0700325 main.log.exception( self.name + ": uncaught exception!" )
326 main.cleanAndExit()
327
328 def ip( self, options="a", wait=3 ):
329 """
330 Run ip command on host and return output
331 """
332 try:
333 command = "ip {}".format( options )
334 main.log.info( self.name + ": Sending: " + command )
335 self.handle.sendline( command )
336 i = self.handle.expect( [ self.prompt, pexpect.TIMEOUT ],
337 timeout=wait + 5 )
338 if i == 1:
339 main.log.error( self.name + ": timeout when waiting for response" )
340 main.log.error( "response: " + str( self.handle.before ) )
341 response = self.handle.before
342 return response
343 except pexpect.EOF:
344 main.log.error( self.name + ": EOF exception found" )
345 main.log.error( self.name + ": " + self.handle.before )
346 main.cleanAndExit()
347 except Exception:
348 main.log.exception( self.name + ": uncaught exception!" )
349 main.cleanAndExit()
350
351 def command( self, cmd, wait=3 ):
352 """
353 Run shell command on host and return output
354 Required:
355 cmd: command to run on the host
356 """
357 try:
358 main.log.info( self.name + ": Sending: " + cmd )
359 self.handle.sendline( cmd )
360 i = self.handle.expect( [ self.prompt, pexpect.TIMEOUT ],
361 timeout=wait + 5 )
362 if i == 1:
363 main.log.error( self.name + ": timeout when waiting for response" )
364 main.log.error( "response: " + str( self.handle.before ) )
365 response = self.handle.before
366 return response
367 except pexpect.EOF:
368 main.log.error( self.name + ": EOF exception found" )
369 main.log.error( self.name + ": " + self.handle.before )
370 main.cleanAndExit()
371 except Exception:
372 main.log.exception( self.name + ": uncaught exception!" )
Pier6a0c4de2018-03-18 16:01:30 -0700373 main.cleanAndExit()