blob: 360fd9a61ffcc58028e44900674271e83a377346 [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
25import sys
26import types
27import os
28import time
29from math import pow
30from drivers.common.cli.emulatordriver import Emulator
31from core.graph import Graph
32
33
34class MininetHostDriver( Emulator ):
35 """
36 This class is created as a standalone Mininet host driver. It could
37 be used as driver for a mock physical host for proof-of-concept
38 testing in physical environment.
39 """
40 def __init__( self ):
41 super( MininetHostDriver, self ).__init__()
42 self.handle = self
43 self.name = None
44 self.shortName = None
45 self.home = None
46 self.hostPrompt = "~#"
47
48 def connect( self, **connectargs ):
49 """
50 Creates ssh handle for the Mininet host.
51 NOTE:
52 The ip_address would come from the topo file using the host tag, the
53 value can be an environment variable as well as a "localhost" to get
54 the ip address needed to ssh to the "bench"
55 """
56 try:
57 for key in connectargs:
58 vars( self )[ key ] = connectargs[ key ]
59 self.name = self.options[ 'name' ]
60 self.shortName = self.options[ 'shortName' ]
61
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 MininetHostDriver,
78 self ).connect(
79 user_name=self.user_name,
80 ip_address=self.ip_address,
81 port=None,
82 pwd=self.pwd )
83
84 if self.handle:
85 main.log.info( "Connection successful to the " +
86 self.user_name +
87 "@" +
88 self.ip_address )
89 self.handle.sendline( "~/mininet/util/m " + self.shortName )
90 self.handle.sendline( "cd" )
91 self.handle.expect( self.hostPrompt )
92 self.handle.sendline( "" )
93 self.handle.expect( self.hostPrompt )
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 try:
114 self.handle.sendline( '' )
115 i = self.handle.expect( [ self.hostPrompt, pexpect.EOF, pexpect.TIMEOUT ],
116 timeout=2 )
117 if i == 0:
118 return main.TRUE
119 elif i == 1:
120 return main.TRUE
121 else:
122 main.log.error( "Connection failed to the host" )
123 return main.ERROR
124 except pexpect.EOF:
125 main.log.error( self.name + ": EOF exception found" )
126 main.log.error( self.name + ": " + self.handle.before )
127 main.cleanAndExit()
128 except Exception:
129 main.log.exception( self.name + ": Uncaught exception!" )
130 main.cleanAndExit()
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.hostPrompt, pexpect.TIMEOUT ],
Jon Halla604fd42018-05-04 14:27:27 -0700148 timeout=wait + 5 )
You Wang84f981d2018-01-12 16:11:50 -0800149 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.hostPrompt )
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.hostPrompt, pexpect.TIMEOUT ],
Jon Halla604fd42018-05-04 14:27:27 -0700182 timeout=wait + 5 )
You Wang84f981d2018-01-12 16:11:50 -0800183 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.hostPrompt )
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()