blob: 7c8713625ad068198124a0a96ea66846d5c4ac83 [file] [log] [blame]
You Wang84f981d2018-01-12 16:11:50 -08001#!/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
You Wang84f981d2018-01-12 16:11:50 -080025import os
You Wang84f981d2018-01-12 16:11:50 -080026from drivers.common.cli.emulatordriver import Emulator
You Wang84f981d2018-01-12 16:11:50 -080027
28
29class MininetHostDriver( Emulator ):
30 """
31 This class is created as a standalone Mininet host driver. It could
32 be used as driver for a mock physical host for proof-of-concept
33 testing in physical environment.
34 """
35 def __init__( self ):
36 super( MininetHostDriver, self ).__init__()
37 self.handle = self
38 self.name = None
39 self.shortName = None
40 self.home = None
41 self.hostPrompt = "~#"
42
43 def connect( self, **connectargs ):
44 """
45 Creates ssh handle for the Mininet 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 MininetHostDriver,
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:
Jon Hall43060f62020-06-23 13:13:33 -070080 main.log.info( "Connection successful to " +
You Wang84f981d2018-01-12 16:11:50 -080081 self.user_name +
82 "@" +
83 self.ip_address )
Jon Hall43060f62020-06-23 13:13:33 -070084 self.handle.sendline( "bash -i" )
You Wang84f981d2018-01-12 16:11:50 -080085 self.handle.sendline( "~/mininet/util/m " + self.shortName )
86 self.handle.sendline( "cd" )
87 self.handle.expect( self.hostPrompt )
88 self.handle.sendline( "" )
89 self.handle.expect( self.hostPrompt )
90 return main.TRUE
91 else:
92 main.log.error( "Connection failed to " +
93 self.user_name +
94 "@" +
95 self.ip_address )
96 return main.FALSE
97 except pexpect.EOF:
98 main.log.error( self.name + ": EOF exception found" )
99 main.log.error( self.name + ": " + self.handle.before )
100 main.cleanAndExit()
101 except Exception:
102 main.log.exception( self.name + ": Uncaught exception!" )
103 main.cleanAndExit()
104
105 def disconnect( self, **connectargs ):
106 """
107 Called when test is complete to disconnect the handle.
108 """
109 try:
110 self.handle.sendline( '' )
111 i = self.handle.expect( [ self.hostPrompt, pexpect.EOF, pexpect.TIMEOUT ],
112 timeout=2 )
113 if i == 0:
114 return main.TRUE
115 elif i == 1:
116 return main.TRUE
117 else:
118 main.log.error( "Connection failed to the host" )
119 return main.ERROR
120 except pexpect.EOF:
121 main.log.error( self.name + ": EOF exception found" )
122 main.log.error( self.name + ": " + self.handle.before )
123 main.cleanAndExit()
124 except Exception:
125 main.log.exception( self.name + ": Uncaught exception!" )
126 main.cleanAndExit()
127
128 def ping( self, dst, ipv6=False, wait=3 ):
129 """
130 Description:
131 Ping from this host to another
132 Required:
133 dst: IP address of destination host
134 Optional:
135 ipv6: will use ping6 command if True; otherwise use ping command
136 wait: timeout for ping command
137 """
138 try:
139 command = "ping6" if ipv6 else "ping"
140 command += " -c 1 -i 1 -W " + str( wait ) + " " + str( dst )
141 main.log.info( self.name + ": Sending: " + command )
142 self.handle.sendline( command )
143 i = self.handle.expect( [ self.hostPrompt, pexpect.TIMEOUT ],
Jon Halla604fd42018-05-04 14:27:27 -0700144 timeout=wait + 5 )
You Wang84f981d2018-01-12 16:11:50 -0800145 if i == 1:
146 main.log.error(
147 self.name +
148 ": timeout when waiting for response" )
149 main.log.error( "response: " + str( self.handle.before ) )
150 self.handle.sendline( "" )
151 self.handle.expect( self.hostPrompt )
152 response = self.handle.before
153 if re.search( ',\s0\%\spacket\sloss', response ):
154 main.log.info( self.name + ": no packets lost, host is reachable" )
155 return main.TRUE
156 else:
157 main.log.warn(
158 self.name +
159 ": PACKET LOST, HOST IS NOT REACHABLE" )
160 return main.FALSE
161 except pexpect.EOF:
162 main.log.error( self.name + ": EOF exception found" )
163 main.log.error( self.name + ": " + self.handle.before )
164 main.cleanAndExit()
165 except Exception:
166 main.log.exception( self.name + ": Uncaught exception!" )
167 main.cleanAndExit()
168
169 def ifconfig( self, wait=3 ):
170 """
171 Run ifconfig command on host and return output
172 """
173 try:
174 command = "ifconfig"
175 main.log.info( self.name + ": Sending: " + command )
176 self.handle.sendline( command )
177 i = self.handle.expect( [ self.hostPrompt, pexpect.TIMEOUT ],
Jon Halla604fd42018-05-04 14:27:27 -0700178 timeout=wait + 5 )
You Wang84f981d2018-01-12 16:11:50 -0800179 if i == 1:
180 main.log.error(
181 self.name +
182 ": timeout when waiting for response" )
183 main.log.error( "response: " + str( self.handle.before ) )
184 self.handle.sendline( "" )
185 self.handle.expect( self.hostPrompt )
186 response = self.handle.before
187 return response
188 except pexpect.EOF:
189 main.log.error( self.name + ": EOF exception found" )
190 main.log.error( self.name + ": " + self.handle.before )
191 main.cleanAndExit()
192 except Exception:
193 main.log.exception( self.name + ": Uncaught exception!" )
194 main.cleanAndExit()