blob: 238721e57af24b0f302976dfa4622bc1a711eb70 [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
30from drivers.common.clidriver import CLI
31
32class HostDriver( CLI ):
33 """
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
41 self.home = None
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' ]
56
57 try:
58 if os.getenv( str( self.ip_address ) ) is not None:
59 self.ip_address = os.getenv( str( self.ip_address ) )
60 else:
61 main.log.info( self.name +
62 ": Trying to connect to " +
63 self.ip_address )
64 except KeyError:
65 main.log.info( "Invalid host name," +
66 " connecting to local host instead" )
67 self.ip_address = 'localhost'
68 except Exception as inst:
69 main.log.error( "Uncaught exception: " + str( inst ) )
70
71 self.handle = super(
72 HostDriver,
73 self ).connect(
74 user_name=self.user_name,
75 ip_address=self.ip_address,
76 port=None,
77 pwd=self.pwd )
78
79 if self.handle:
80 main.log.info( "Connection successful to the " +
81 self.user_name +
82 "@" +
83 self.ip_address )
84 self.handle.sendline( "" )
85 self.handle.expect( self.prompt )
86 return main.TRUE
87 else:
88 main.log.error( "Connection failed to " +
89 self.user_name +
90 "@" +
91 self.ip_address )
92 return main.FALSE
93 except pexpect.EOF:
94 main.log.error( self.name + ": EOF exception found" )
95 main.log.error( self.name + ": " + self.handle.before )
96 main.cleanAndExit()
97 except Exception:
98 main.log.exception( self.name + ": Uncaught exception!" )
99 main.cleanAndExit()
100
101 def disconnect( self, **connectargs ):
102 """
103 Called when test is complete to disconnect the handle.
104 """
105 response = main.TRUE
106 try:
107 if self.handle:
108 # Disconnect from the host
109 self.handle.sendline( "" )
110 self.handle.expect( self.prompt )
111 self.handle.sendline( "exit" )
112 i = self.handle.expect( [ "closed", pexpect.TIMEOUT ], timeout=2 )
113 if i == 1:
114 main.log.error(
115 self.name +
116 ": timeout when waiting for response" )
117 main.log.error( "response: " + str( self.handle.before ) )
118 except TypeError:
119 main.log.exception( self.name + ": Object not as expected" )
120 response = main.FALSE
121 except pexpect.EOF:
122 main.log.error( self.name + ": EOF exception found" )
123 main.log.error( self.name + ": " + self.handle.before )
124 except ValueError:
125 main.log.exception( "Exception in disconnect of " + self.name )
126 response = main.TRUE
127 except Exception:
128 main.log.exception( self.name + ": Connection failed to the host" )
129 response = main.FALSE
130 return response
131
132 def ping( self, dst, ipv6=False, wait=3 ):
133 """
134 Description:
135 Ping from this host to another
136 Required:
137 dst: IP address of destination host
138 Optional:
139 ipv6: will use ping6 command if True; otherwise use ping command
140 wait: timeout for ping command
141 """
142 try:
143 command = "ping6" if ipv6 else "ping"
144 command += " -c 1 -i 1 -W " + str( wait ) + " " + str( dst )
145 main.log.info( self.name + ": Sending: " + command )
146 self.handle.sendline( command )
147 i = self.handle.expect( [ self.prompt, pexpect.TIMEOUT ],
148 timeout=wait + 1 )
149 if i == 1:
150 main.log.error(
151 self.name +
152 ": timeout when waiting for response" )
153 main.log.error( "response: " + str( self.handle.before ) )
154 self.handle.sendline( "" )
155 self.handle.expect( self.prompt )
156 response = self.handle.before
157 if re.search( ',\s0\%\spacket\sloss', response ):
158 main.log.info( self.name + ": no packets lost, host is reachable" )
159 return main.TRUE
160 else:
161 main.log.warn(
162 self.name +
163 ": PACKET LOST, HOST IS NOT REACHABLE" )
164 return main.FALSE
165 except pexpect.EOF:
166 main.log.error( self.name + ": EOF exception found" )
167 main.log.error( self.name + ": " + self.handle.before )
168 main.cleanAndExit()
169 except Exception:
170 main.log.exception( self.name + ": Uncaught exception!" )
171 main.cleanAndExit()
172
173 def ifconfig( self, wait=3 ):
174 """
175 Run ifconfig command on host and return output
176 """
177 try:
178 command = "ifconfig"
179 main.log.info( self.name + ": Sending: " + command )
180 self.handle.sendline( command )
181 i = self.handle.expect( [ self.prompt, pexpect.TIMEOUT ],
182 timeout=wait + 1 )
183 if i == 1:
184 main.log.error(
185 self.name +
186 ": timeout when waiting for response" )
187 main.log.error( "response: " + str( self.handle.before ) )
188 self.handle.sendline( "" )
189 self.handle.expect( self.prompt )
190 response = self.handle.before
191 return response
192 except pexpect.EOF:
193 main.log.error( self.name + ": EOF exception found" )
194 main.log.error( self.name + ": " + self.handle.before )
195 main.cleanAndExit()
196 except Exception:
197 main.log.exception( self.name + ": Uncaught exception!" )
198 main.cleanAndExit()