blob: cdbcf61fb9a82b73a37601335f2c5a0be79cb247 [file] [log] [blame]
Jon Hall05b2b432014-10-08 19:53:25 -04001#!/usr/bin/env python
andrewonlabe8e56fd2014-10-09 17:12:44 -04002
kelvin8ec71442015-01-15 16:57:00 -08003"""
Jeremy Ronquilloec916a42018-02-02 13:05:57 -08004Copyright 2014 Open Networking Foundation (ONF)
5
kelvin8ec71442015-01-15 16:57:00 -08006This driver interacts with ONOS bench, the OSGi platform
7that configures the ONOS nodes. ( aka ONOS-next )
andrewonlabe8e56fd2014-10-09 17:12:44 -04008
kelvin8ec71442015-01-15 16:57:00 -08009Please follow the coding style demonstrated by existing
andrewonlabe8e56fd2014-10-09 17:12:44 -040010functions and document properly.
11
12If you are a contributor to the driver, please
13list your email here for future contact:
14
15jhall@onlab.us
16andrew@onlab.us
17
18OCT 9 2014
Jeremy Songsterae01bba2016-07-11 15:39:17 -070019Modified 2016 by ON.Lab
20
21Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>,
22the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>,
23or the System Testing Guide page at <https://wiki.onosproject.org/x/WYQg>
andrewonlabe8e56fd2014-10-09 17:12:44 -040024
kelvin8ec71442015-01-15 16:57:00 -080025"""
Jon Hall05b2b432014-10-08 19:53:25 -040026import time
Jon Hall6801cda2015-07-15 14:13:45 -070027import types
Jon Hall05b2b432014-10-08 19:53:25 -040028import pexpect
kelvin-onlaba4074292015-07-09 15:19:49 -070029import os
Jon Hall6c44c0b2016-04-20 15:21:00 -070030import re
31import subprocess
pingping-lin6d23d9e2015-02-02 16:54:24 -080032from requests.models import Response
Jon Hall05b2b432014-10-08 19:53:25 -040033from drivers.common.clidriver import CLI
34
kelvin8ec71442015-01-15 16:57:00 -080035class OnosDriver( CLI ):
Jon Hall05b2b432014-10-08 19:53:25 -040036
kelvin8ec71442015-01-15 16:57:00 -080037 def __init__( self ):
38 """
39 Initialize client
40 """
Jon Hallefbd9792015-03-05 16:11:36 -080041 self.name = None
42 self.home = None
43 self.handle = None
Jon Hall6c44c0b2016-04-20 15:21:00 -070044 self.nicAddr = None
Devin Limdc78e202017-06-09 18:30:07 -070045 super( OnosDriver, self ).__init__()
kelvin8ec71442015-01-15 16:57:00 -080046
47 def connect( self, **connectargs ):
48 """
Jon Hall05b2b432014-10-08 19:53:25 -040049 Creates ssh handle for ONOS "bench".
kelvin-onlaba4074292015-07-09 15:19:49 -070050 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"
kelvin8ec71442015-01-15 16:57:00 -080054 """
Jon Hall05b2b432014-10-08 19:53:25 -040055 try:
Devin Limdc78e202017-06-09 18:30:07 -070056
Jon Hall05b2b432014-10-08 19:53:25 -040057 for key in connectargs:
kelvin8ec71442015-01-15 16:57:00 -080058 vars( self )[ key ] = connectargs[ key ]
andrew@onlab.us3b087132015-03-11 15:00:08 -070059 self.home = "~/onos"
Jon Hall05b2b432014-10-08 19:53:25 -040060 for key in self.options:
61 if key == "home":
kelvin8ec71442015-01-15 16:57:00 -080062 self.home = self.options[ 'home' ]
Jon Hall05b2b432014-10-08 19:53:25 -040063 break
Jon Hall274b6642015-02-17 11:57:17 -080064 if self.home is None or self.home == "":
Jon Halle94919c2015-03-23 11:42:57 -070065 self.home = "~/onos"
Jon Hall21270ac2015-02-16 17:59:55 -080066
kelvin8ec71442015-01-15 16:57:00 -080067 self.name = self.options[ 'name' ]
kelvin-onlaba4074292015-07-09 15:19:49 -070068
kelvin-onlabc2b79102015-07-14 11:41:20 -070069 # The 'nodes' tag is optional and it is not required in .topo file
kelvin-onlaba4074292015-07-09 15:19:49 -070070 for key in self.options:
71 if key == "nodes":
kelvin-onlabc2b79102015-07-14 11:41:20 -070072 # Maximum number of ONOS nodes to run, if there is any
kelvin-onlaba4074292015-07-09 15:19:49 -070073 self.maxNodes = int( self.options[ 'nodes' ] )
74 break
75 self.maxNodes = None
76
Jeremy Ronquillo82705492017-10-18 14:19:55 -070077 if self.maxNodes is None or self.maxNodes == "":
kelvin-onlabc2b79102015-07-14 11:41:20 -070078 self.maxNodes = 100
kelvin-onlaba4074292015-07-09 15:19:49 -070079
kelvin-onlabc2b79102015-07-14 11:41:20 -070080 # Grabs all OC environment variables based on max number of nodes
kelvin-onlaba4074292015-07-09 15:19:49 -070081 self.onosIps = {} # Dictionary of all possible ONOS ip
82
83 try:
84 if self.maxNodes:
kelvin-onlaba4074292015-07-09 15:19:49 -070085 for i in range( self.maxNodes ):
86 envString = "OC" + str( i + 1 )
kelvin-onlabc2b79102015-07-14 11:41:20 -070087 # If there is no more OC# then break the loop
88 if os.getenv( envString ):
89 self.onosIps[ envString ] = os.getenv( envString )
90 else:
91 self.maxNodes = len( self.onosIps )
92 main.log.info( self.name +
93 ": Created cluster data with " +
94 str( self.maxNodes ) +
95 " maximum number" +
96 " of nodes" )
97 break
kelvin-onlaba4074292015-07-09 15:19:49 -070098
99 if not self.onosIps:
100 main.log.info( "Could not read any environment variable"
101 + " please load a cell file with all" +
102 " onos IP" )
Jon Hall5cf14d52015-07-16 12:15:19 -0700103 self.maxNodes = None
kelvin-onlaba4074292015-07-09 15:19:49 -0700104 else:
105 main.log.info( self.name + ": Found " +
106 str( self.onosIps.values() ) +
107 " ONOS IPs" )
kelvin-onlaba4074292015-07-09 15:19:49 -0700108 except KeyError:
109 main.log.info( "Invalid environment variable" )
110 except Exception as inst:
111 main.log.error( "Uncaught exception: " + str( inst ) )
112
113 try:
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700114 if os.getenv( str( self.ip_address ) ) is not None:
kelvin-onlaba4074292015-07-09 15:19:49 -0700115 self.ip_address = os.getenv( str( self.ip_address ) )
116 else:
117 main.log.info( self.name +
118 ": Trying to connect to " +
119 self.ip_address )
kelvin-onlaba4074292015-07-09 15:19:49 -0700120 except KeyError:
121 main.log.info( "Invalid host name," +
122 " connecting to local host instead" )
123 self.ip_address = 'localhost'
124 except Exception as inst:
125 main.log.error( "Uncaught exception: " + str( inst ) )
126
kelvin8ec71442015-01-15 16:57:00 -0800127 self.handle = super( OnosDriver, self ).connect(
128 user_name=self.user_name,
kelvin-onlab08679eb2015-01-21 16:11:48 -0800129 ip_address=self.ip_address,
kelvin-onlabd3b64892015-01-20 13:26:24 -0800130 port=self.port,
131 pwd=self.pwd,
132 home=self.home )
Jon Hall05b2b432014-10-08 19:53:25 -0400133
Jon Hall05b2b432014-10-08 19:53:25 -0400134 if self.handle:
Jon Hall0fc0d452015-07-14 09:49:58 -0700135 self.handle.sendline( "cd " + self.home )
Devin Limc20e79a2017-06-07 10:29:57 -0700136 self.handle.expect( self.prompt )
Jon Hall05b2b432014-10-08 19:53:25 -0400137 return self.handle
kelvin8ec71442015-01-15 16:57:00 -0800138 else:
Jon Hall0fc0d452015-07-14 09:49:58 -0700139 main.log.info( "Failed to create ONOS handle" )
Jon Hall05b2b432014-10-08 19:53:25 -0400140 return main.FALSE
141 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800142 main.log.error( self.name + ": EOF exception found" )
143 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700144 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800145 except Exception:
146 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700147 main.cleanAndExit()
Jon Hall05b2b432014-10-08 19:53:25 -0400148
kelvin8ec71442015-01-15 16:57:00 -0800149 def disconnect( self ):
150 """
Jon Hall05b2b432014-10-08 19:53:25 -0400151 Called when Test is complete to disconnect the ONOS handle.
kelvin8ec71442015-01-15 16:57:00 -0800152 """
Jon Halld61331b2015-02-17 16:35:47 -0800153 response = main.TRUE
Jon Hall05b2b432014-10-08 19:53:25 -0400154 try:
Jon Hall61282e32015-03-19 11:34:11 -0700155 if self.handle:
156 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -0700157 self.handle.expect( self.prompt )
Jon Hall61282e32015-03-19 11:34:11 -0700158 self.handle.sendline( "exit" )
159 self.handle.expect( "closed" )
Jon Hall05b2b432014-10-08 19:53:25 -0400160 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800161 main.log.error( self.name + ": EOF exception found" )
162 main.log.error( self.name + ": " + self.handle.before )
Jon Hall61282e32015-03-19 11:34:11 -0700163 except ValueError:
Jon Hall1a77a1e2015-04-06 10:41:13 -0700164 main.log.exception( "Exception in disconnect of " + self.name )
Jon Hall61282e32015-03-19 11:34:11 -0700165 response = main.TRUE
Jon Hallfebb1c72015-03-05 13:30:09 -0800166 except Exception:
167 main.log.exception( self.name + ": Connection failed to the host" )
Jon Hall05b2b432014-10-08 19:53:25 -0400168 response = main.FALSE
169 return response
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400170
andrew@onlab.usaee415a2015-06-05 14:38:58 -0400171 def getEpochMs( self ):
172 """
173 Returns milliseconds since epoch
Jon Hall4ba53f02015-07-29 13:07:41 -0700174
175 When checking multiple nodes in a for loop,
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000176 around a hundred milliseconds of difference (ascending) is
Jon Hall4ba53f02015-07-29 13:07:41 -0700177 generally acceptable due to calltime of the function.
178 Few seconds, however, is not and it means clocks
179 are off sync.
andrew@onlab.usaee415a2015-06-05 14:38:58 -0400180 """
181 try:
182 self.handle.sendline( 'date +%s.%N' )
183 self.handle.expect( 'date \+\%s\.\%N' )
Devin Limdc78e202017-06-09 18:30:07 -0700184 self.handle.expect( self.prompt )
andrew@onlab.usaee415a2015-06-05 14:38:58 -0400185 epochMs = self.handle.before
186 return epochMs
187 except Exception:
188 main.log.exception( 'Uncaught exception getting epoch time' )
Devin Lim44075962017-08-11 10:56:37 -0700189 main.cleanAndExit()
andrew@onlab.usaee415a2015-06-05 14:38:58 -0400190
Jon Hall6c44c0b2016-04-20 15:21:00 -0700191 def onosPackage( self, opTimeout=180 ):
kelvin8ec71442015-01-15 16:57:00 -0800192 """
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400193 Produce a self-contained tar.gz file that can be deployed
Jon Hall64af8502015-12-15 10:09:33 -0800194 and executed on any platform with Java 8 JRE.
kelvin8ec71442015-01-15 16:57:00 -0800195 """
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400196 try:
Jon Hall64af8502015-12-15 10:09:33 -0800197 ret = main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800198 self.handle.sendline( "onos-package" )
199 self.handle.expect( "onos-package" )
Jon Hall96451092016-05-04 09:42:30 -0700200 while True:
201 i = self.handle.expect( [ "Downloading",
202 "Unknown options",
203 "No such file or directory",
204 "tar.gz",
Devin Limc20e79a2017-06-07 10:29:57 -0700205 self.prompt ],
Jon Hall96451092016-05-04 09:42:30 -0700206 opTimeout )
207 handle = str( self.handle.before + self.handle.after )
208 if i == 0:
209 # Give more time to download the file
210 continue # expect again
211 elif i == 1:
212 # Incorrect usage
213 main.log.error( "onos-package does not recognize the given options" )
214 ret = main.FALSE
215 continue # expect again
216 elif i == 2:
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000217 # File(s) not found
Jon Hall96451092016-05-04 09:42:30 -0700218 main.log.error( "onos-package could not find a file or directory" )
219 ret = main.FALSE
220 continue # expect again
221 elif i == 3:
222 # tar.gz
223 continue # expect again
224 elif i == 4:
225 # Prompt returned
226 break
Jon Hallc6793552016-01-19 14:18:37 -0800227 main.log.info( "onos-package command returned: " + handle )
kelvin8ec71442015-01-15 16:57:00 -0800228 # As long as the sendline does not time out,
229 # return true. However, be careful to interpret
230 # the results of the onos-package command return
Jon Hall64af8502015-12-15 10:09:33 -0800231 return ret
232 except pexpect.TIMEOUT:
233 main.log.exception( self.name + ": TIMEOUT exception found in onosPackage" )
234 main.log.error( self.name + ": " + self.handle.before )
You Wang141b43b2018-06-26 16:50:18 -0700235 self.handle.send( "\x03" ) # Control-C
236 self.handle.expect( self.prompt )
Jon Hall64af8502015-12-15 10:09:33 -0800237 return main.FALSE
andrewonlab7735d852014-10-09 13:02:47 -0400238 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800239 main.log.error( self.name + ": EOF exception found" )
240 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700241 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800242 except Exception:
243 main.log.exception( "Failed to package ONOS" )
Devin Lim44075962017-08-11 10:56:37 -0700244 main.cleanAndExit()
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400245
kelvin-onlabd3b64892015-01-20 13:26:24 -0800246 def onosBuild( self ):
kelvin8ec71442015-01-15 16:57:00 -0800247 """
andrewonlab8790abb2014-11-06 13:51:54 -0500248 Use the pre defined script to build onos via mvn
kelvin8ec71442015-01-15 16:57:00 -0800249 """
andrewonlab8790abb2014-11-06 13:51:54 -0500250 try:
kelvin8ec71442015-01-15 16:57:00 -0800251 self.handle.sendline( "onos-build" )
252 self.handle.expect( "onos-build" )
253 i = self.handle.expect( [
kelvin-onlabd3b64892015-01-20 13:26:24 -0800254 "BUILD SUCCESS",
255 "ERROR",
256 "BUILD FAILED" ],
257 timeout=120 )
kelvin8ec71442015-01-15 16:57:00 -0800258 handle = str( self.handle.before )
Devin Limc20e79a2017-06-07 10:29:57 -0700259 self.handle.expect( self.prompt )
andrewonlab8790abb2014-11-06 13:51:54 -0500260
kelvin8ec71442015-01-15 16:57:00 -0800261 main.log.info( "onos-build command returned: " +
262 handle )
andrewonlab8790abb2014-11-06 13:51:54 -0500263
264 if i == 0:
265 return main.TRUE
266 else:
267 return handle
268
269 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800270 main.log.error( self.name + ": EOF exception found" )
271 main.log.error( self.name + ": " + self.handle.before )
Jon Hallfebb1c72015-03-05 13:30:09 -0800272 except Exception:
273 main.log.exception( "Failed to build ONOS" )
Devin Lim44075962017-08-11 10:56:37 -0700274 main.cleanAndExit()
andrewonlab8790abb2014-11-06 13:51:54 -0500275
shahshreya9f531fe2015-06-10 12:03:51 -0700276 def cleanInstall( self, skipTest=False, mciTimeout=600 ):
kelvin8ec71442015-01-15 16:57:00 -0800277 """
278 Runs mvn clean install in the root of the ONOS directory.
279 This will clean all ONOS artifacts then compile each module
shahshreya9f531fe2015-06-10 12:03:51 -0700280 Optional:
281 skipTest - Does "-DskipTests -Dcheckstyle.skip -U -T 1C" which
282 skip the test. This will make the building faster.
283 Disregarding the credibility of the build
kelvin8ec71442015-01-15 16:57:00 -0800284 Returns: main.TRUE on success
Jon Hallde9d9aa2014-10-08 20:36:02 -0400285 On Failure, exits the test
kelvin8ec71442015-01-15 16:57:00 -0800286 """
Jon Hallde9d9aa2014-10-08 20:36:02 -0400287 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800288 main.log.info( "Running 'mvn clean install' on " +
289 str( self.name ) +
kelvin8ec71442015-01-15 16:57:00 -0800290 ". This may take some time." )
291 self.handle.sendline( "cd " + self.home )
Devin Limc20e79a2017-06-07 10:29:57 -0700292 self.handle.expect( self.prompt )
Jon Hallea7818b2014-10-09 14:30:59 -0400293
kelvin8ec71442015-01-15 16:57:00 -0800294 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -0700295 self.handle.expect( self.prompt )
shahshreya9f531fe2015-06-10 12:03:51 -0700296
297 if not skipTest:
298 self.handle.sendline( "mvn clean install" )
299 self.handle.expect( "mvn clean install" )
300 else:
301 self.handle.sendline( "mvn clean install -DskipTests" +
302 " -Dcheckstyle.skip -U -T 1C" )
303 self.handle.expect( "mvn clean install -DskipTests" +
304 " -Dcheckstyle.skip -U -T 1C" )
kelvin8ec71442015-01-15 16:57:00 -0800305 while True:
306 i = self.handle.expect( [
Jon Hall274b6642015-02-17 11:57:17 -0800307 'There\sis\sinsufficient\smemory\sfor\sthe\sJava\s' +
Jon Hallefbd9792015-03-05 16:11:36 -0800308 'Runtime\sEnvironment\sto\scontinue',
Jon Hallde9d9aa2014-10-08 20:36:02 -0400309 'BUILD\sFAILURE',
310 'BUILD\sSUCCESS',
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700311 'onos' + self.prompt, # TODO: fix this to be more generic?
Devin Limdc78e202017-06-09 18:30:07 -0700312 'ONOS' + self.prompt,
pingping-lin57a56ce2015-05-20 16:43:48 -0700313 pexpect.TIMEOUT ], mciTimeout )
Jon Hallde9d9aa2014-10-08 20:36:02 -0400314 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -0800315 main.log.error( self.name + ":There is insufficient memory \
316 for the Java Runtime Environment to continue." )
317 # return main.FALSE
Devin Lim44075962017-08-11 10:56:37 -0700318
319 main.cleanAndExit()
Jon Hallde9d9aa2014-10-08 20:36:02 -0400320 if i == 1:
kelvin8ec71442015-01-15 16:57:00 -0800321 main.log.error( self.name + ": Build failure!" )
322 # return main.FALSE
Devin Lim44075962017-08-11 10:56:37 -0700323
324 main.cleanAndExit()
Jon Hallde9d9aa2014-10-08 20:36:02 -0400325 elif i == 2:
kelvin8ec71442015-01-15 16:57:00 -0800326 main.log.info( self.name + ": Build success!" )
Jon Halle94919c2015-03-23 11:42:57 -0700327 elif i == 3 or i == 4:
kelvin8ec71442015-01-15 16:57:00 -0800328 main.log.info( self.name + ": Build complete" )
329 # Print the build time
Jon Hallf8ef52c2014-10-09 19:37:33 -0400330 for line in self.handle.before.splitlines():
331 if "Total time:" in line:
kelvin8ec71442015-01-15 16:57:00 -0800332 main.log.info( line )
333 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -0700334 self.handle.expect( self.prompt, timeout=60 )
Jon Hallde9d9aa2014-10-08 20:36:02 -0400335 return main.TRUE
Jon Halle94919c2015-03-23 11:42:57 -0700336 elif i == 5:
kelvin8ec71442015-01-15 16:57:00 -0800337 main.log.error(
338 self.name +
339 ": mvn clean install TIMEOUT!" )
340 # return main.FALSE
Devin Lim44075962017-08-11 10:56:37 -0700341
342 main.cleanAndExit()
Jon Hallde9d9aa2014-10-08 20:36:02 -0400343 else:
Jon Hall274b6642015-02-17 11:57:17 -0800344 main.log.error( self.name + ": unexpected response from " +
Jon Hallefbd9792015-03-05 16:11:36 -0800345 "mvn clean install" )
kelvin8ec71442015-01-15 16:57:00 -0800346 # return main.FALSE
Devin Lim44075962017-08-11 10:56:37 -0700347
348 main.cleanAndExit()
Jon Hallde9d9aa2014-10-08 20:36:02 -0400349 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800350 main.log.error( self.name + ": EOF exception found" )
351 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700352 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800353 except Exception:
354 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700355 main.cleanAndExit()
Jon Hallacabffd2014-10-09 12:36:53 -0400356
Jon Hall3576f572016-08-23 10:01:07 -0700357 def buckBuild( self, timeout=180 ):
358 """
359 Build onos using buck.
360 """
361 try:
362 ret = main.TRUE
363 self.handle.sendline( "buck build onos" )
364 self.handle.expect( "buck build onos" )
365 output = ""
366 while True:
367 i = self.handle.expect( [ "This does not appear to be the root of a Buck project.",
368 "\n",
369 "BUILD FAILED",
Jon Halld9066132018-03-01 14:52:53 -0800370 "no buck in",
Devin Limc20e79a2017-06-07 10:29:57 -0700371 self.prompt ],
Jon Hall3576f572016-08-23 10:01:07 -0700372 timeout=timeout )
373 output += str( self.handle.before + self.handle.after )
374 if i == 0:
375 main.log.error( "Wrong location" )
376 ret = main.FALSE
377 elif i == 1:
378 # end of a line, buck is still printing output
379 pass
380 elif i == 2:
381 # Build failed
382 main.log.error( "Build failed" )
383 ret = main.FALSE
384 elif i == 3:
Jon Halld9066132018-03-01 14:52:53 -0800385 main.log.error( "Could not find buck in your PATH." )
386 ret = main.FALSE
387 elif i == 4:
Jon Hall3576f572016-08-23 10:01:07 -0700388 # Prompt returned
389 break
390 main.log.debug( output )
391 return ret
392 except pexpect.TIMEOUT:
393 main.log.exception( self.name + ": TIMEOUT exception found" )
394 main.log.error( self.name + ": " + self.handle.before )
You Wang141b43b2018-06-26 16:50:18 -0700395 self.handle.send( "\x03" ) # Control-C
396 self.handle.expect( self.prompt )
Jon Hall3576f572016-08-23 10:01:07 -0700397 return main.FALSE
398 except pexpect.EOF:
399 main.log.error( self.name + ": EOF exception found" )
400 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700401 main.cleanAndExit()
Jon Hall3576f572016-08-23 10:01:07 -0700402 except Exception:
403 main.log.exception( "Failed to build and package ONOS" )
Devin Lim44075962017-08-11 10:56:37 -0700404 main.cleanAndExit()
Jon Hall3576f572016-08-23 10:01:07 -0700405
You Wangd54c7052018-08-07 16:06:27 -0700406 def bazelBuild( self, timeout=180 ):
407 """
408 Build onos using bazel.
409 """
410 try:
411 ret = main.TRUE
412 self.handle.sendline( "bazel build onos" )
413 self.handle.expect( "bazel build onos" )
414 output = ""
415 while True:
416 i = self.handle.expect( [ "command is only supported from within a workspace",
417 "\n",
418 "FAILED",
419 "ERROR",
420 "command not found",
421 self.prompt ],
422 timeout=timeout )
423 output += str( self.handle.before + self.handle.after )
424 if i == 0:
425 main.log.error( "Please run the build from root of ONOS project" )
426 ret = main.FALSE
427 elif i == 1:
428 # end of a line, buck is still printing output
429 pass
430 elif i == 2:
431 # Build failed
432 main.log.error( "Build failed" )
433 ret = main.FALSE
434 elif i == 3:
435 # Build failed
436 main.log.error( "Build failed" )
437 ret = main.FALSE
438 elif i == 4:
439 main.log.error( "Command not found" )
440 ret = main.FALSE
441 elif i == 5:
442 # Prompt returned
443 break
444 main.log.debug( output )
445 return ret
446 except pexpect.TIMEOUT:
447 main.log.exception( self.name + ": TIMEOUT exception found" )
448 main.log.error( self.name + ": " + self.handle.before )
449 self.handle.send( "\x03" ) # Control-C
450 self.handle.expect( self.prompt )
451 return main.FALSE
452 except pexpect.EOF:
453 main.log.error( self.name + ": EOF exception found" )
454 main.log.error( self.name + ": " + self.handle.before )
455 main.cleanAndExit()
456 except Exception:
457 main.log.exception( "Failed to build and package ONOS" )
458 main.cleanAndExit()
459
Jon Hall61282e32015-03-19 11:34:11 -0700460 def gitPull( self, comp1="", fastForward=True ):
kelvin8ec71442015-01-15 16:57:00 -0800461 """
Jon Hallacabffd2014-10-09 12:36:53 -0400462 Assumes that "git pull" works without login
Jon Hall47a93fb2015-01-06 16:46:06 -0800463
Jon Hall61282e32015-03-19 11:34:11 -0700464 If the fastForward boolean is set to true, only git pulls that can
465 be fast forwarded will be performed. IE if you have not local commits
466 in your branch.
467
Jon Hallacabffd2014-10-09 12:36:53 -0400468 This function will perform a git pull on the ONOS instance.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800469 If used as gitPull( "NODE" ) it will do git pull + NODE. This is
Jon Hallacabffd2014-10-09 12:36:53 -0400470 for the purpose of pulling from other nodes if necessary.
471
Jon Hall47a93fb2015-01-06 16:46:06 -0800472 Otherwise, this function will perform a git pull in the
Jon Hallacabffd2014-10-09 12:36:53 -0400473 ONOS repository. If it has any problems, it will return main.ERROR
kelvin-onlabd3b64892015-01-20 13:26:24 -0800474 If it successfully does a gitPull, it will return a 1 ( main.TRUE )
Shreya Shahee15f6c2014-10-28 18:12:30 -0400475 If it has no updates, it will return 3.
Jon Hallacabffd2014-10-09 12:36:53 -0400476
kelvin8ec71442015-01-15 16:57:00 -0800477 """
Jon Hallacabffd2014-10-09 12:36:53 -0400478 try:
kelvin8ec71442015-01-15 16:57:00 -0800479 self.handle.sendline( "cd " + self.home )
Devin Limc20e79a2017-06-07 10:29:57 -0700480 self.handle.expect( self.prompt )
Jon Hall61282e32015-03-19 11:34:11 -0700481 cmd = "git pull"
482 if comp1 != "":
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700483 cmd += ' ' + comp1
Jon Hall61282e32015-03-19 11:34:11 -0700484 if fastForward:
485 cmd += ' ' + " --ff-only"
486 self.handle.sendline( cmd )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800487 i = self.handle.expect(
488 [
489 'fatal',
490 'Username\sfor\s(.*):\s',
491 '\sfile(s*) changed,\s',
492 'Already up-to-date',
493 'Aborting',
494 'You\sare\snot\scurrently\son\sa\sbranch',
Jon Hall274b6642015-02-17 11:57:17 -0800495 'You asked me to pull without telling me which branch you',
496 'Pull is not possible because you have unmerged files',
Jon Hall61282e32015-03-19 11:34:11 -0700497 'Please enter a commit message to explain why this merge',
498 'Found a swap file by the name',
499 'Please, commit your changes before you can merge.',
kelvin-onlabd3b64892015-01-20 13:26:24 -0800500 pexpect.TIMEOUT ],
501 timeout=300 )
kelvin8ec71442015-01-15 16:57:00 -0800502 if i == 0:
Jon Hall61282e32015-03-19 11:34:11 -0700503 main.log.error( self.name + ": Git pull had some issue" )
504 output = self.handle.after
Devin Limdc78e202017-06-09 18:30:07 -0700505 self.handle.expect( self.prompt )
Jon Hall61282e32015-03-19 11:34:11 -0700506 output += self.handle.before
507 main.log.warn( output )
Jon Hallacabffd2014-10-09 12:36:53 -0400508 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800509 elif i == 1:
510 main.log.error(
511 self.name +
512 ": Git Pull Asking for username. " )
Jon Hallacabffd2014-10-09 12:36:53 -0400513 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800514 elif i == 2:
515 main.log.info(
516 self.name +
517 ": Git Pull - pulling repository now" )
Devin Limc20e79a2017-06-07 10:29:57 -0700518 self.handle.expect( self.prompt, 120 )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800519 # So that only when git pull is done, we do mvn clean compile
520 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800521 elif i == 3:
522 main.log.info( self.name + ": Git Pull - Already up to date" )
Devin Limc20e79a2017-06-07 10:29:57 -0700523 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800524 elif i == 4:
525 main.log.info(
526 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800527 ": Git Pull - Aborting..." +
528 "Are there conflicting git files?" )
Jon Hallacabffd2014-10-09 12:36:53 -0400529 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800530 elif i == 5:
531 main.log.info(
532 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800533 ": Git Pull - You are not currently " +
534 "on a branch so git pull failed!" )
Jon Hallacabffd2014-10-09 12:36:53 -0400535 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800536 elif i == 6:
537 main.log.info(
538 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800539 ": Git Pull - You have not configured an upstream " +
540 "branch to pull from. Git pull failed!" )
Jon Hallacabffd2014-10-09 12:36:53 -0400541 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800542 elif i == 7:
543 main.log.info(
544 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800545 ": Git Pull - Pull is not possible because " +
546 "you have unmerged files." )
Jon Hallacabffd2014-10-09 12:36:53 -0400547 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800548 elif i == 8:
Jon Hall61282e32015-03-19 11:34:11 -0700549 # NOTE: abandoning test since we can't reliably handle this
550 # there could be different default text editors and we
551 # also don't know if we actually want to make the commit
552 main.log.error( "Git pull resulted in a merge commit message" +
553 ". Exiting test!" )
Devin Lim44075962017-08-11 10:56:37 -0700554
555 main.cleanAndExit()
Jon Hall61282e32015-03-19 11:34:11 -0700556 elif i == 9: # Merge commit message but swap file exists
557 main.log.error( "Git pull resulted in a merge commit message" +
558 " but a swap file exists." )
559 try:
560 self.handle.send( 'A' ) # Abort
Devin Limc20e79a2017-06-07 10:29:57 -0700561 self.handle.expect( self.prompt )
Jon Hall61282e32015-03-19 11:34:11 -0700562 return main.ERROR
563 except Exception:
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700564 main.log.exception( "Couldn't exit editor prompt!" )
Devin Lim44075962017-08-11 10:56:37 -0700565
566 main.cleanAndExit()
Jon Hall61282e32015-03-19 11:34:11 -0700567 elif i == 10: # In the middle of a merge commit
568 main.log.error( "Git branch is in the middle of a merge. " )
569 main.log.warn( self.handle.before + self.handle.after )
570 return main.ERROR
571 elif i == 11:
kelvin8ec71442015-01-15 16:57:00 -0800572 main.log.error( self.name + ": Git Pull - TIMEOUT" )
573 main.log.error(
574 self.name + " Response was: " + str(
575 self.handle.before ) )
You Wang141b43b2018-06-26 16:50:18 -0700576 self.handle.send( "\x03" ) # Control-C
577 self.handle.expect( self.prompt )
Jon Hallacabffd2014-10-09 12:36:53 -0400578 return main.ERROR
579 else:
kelvin8ec71442015-01-15 16:57:00 -0800580 main.log.error(
581 self.name +
582 ": Git Pull - Unexpected response, check for pull errors" )
Jon Hallacabffd2014-10-09 12:36:53 -0400583 return main.ERROR
584 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800585 main.log.error( self.name + ": EOF exception found" )
586 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700587 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800588 except Exception:
589 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700590 main.cleanAndExit()
Jon Hallacabffd2014-10-09 12:36:53 -0400591
kelvin-onlabd3b64892015-01-20 13:26:24 -0800592 def gitCheckout( self, branch="master" ):
kelvin8ec71442015-01-15 16:57:00 -0800593 """
Jon Hallacabffd2014-10-09 12:36:53 -0400594 Assumes that "git pull" works without login
kelvin8ec71442015-01-15 16:57:00 -0800595
Jon Hallacabffd2014-10-09 12:36:53 -0400596 This function will perform a git git checkout on the ONOS instance.
kelvin-onlabd3b64892015-01-20 13:26:24 -0800597 If used as gitCheckout( "branch" ) it will do git checkout
598 of the "branch".
Jon Hallacabffd2014-10-09 12:36:53 -0400599
600 Otherwise, this function will perform a git checkout of the master
kelvin8ec71442015-01-15 16:57:00 -0800601 branch of the ONOS repository. If it has any problems, it will return
602 main.ERROR.
603 If the branch was already the specified branch, or the git checkout was
Jon Hallacabffd2014-10-09 12:36:53 -0400604 successful then the function will return main.TRUE.
605
kelvin8ec71442015-01-15 16:57:00 -0800606 """
Jon Hallacabffd2014-10-09 12:36:53 -0400607 try:
kelvin8ec71442015-01-15 16:57:00 -0800608 self.handle.sendline( "cd " + self.home )
Devin Limc20e79a2017-06-07 10:29:57 -0700609 self.handle.expect( self.prompt )
Jon Hall274b6642015-02-17 11:57:17 -0800610 main.log.info( self.name +
611 ": Checking out git branch/ref: " + branch + "..." )
kelvin8ec71442015-01-15 16:57:00 -0800612 cmd = "git checkout " + branch
613 self.handle.sendline( cmd )
614 self.handle.expect( cmd )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800615 i = self.handle.expect(
Jon Hall274b6642015-02-17 11:57:17 -0800616 [ 'fatal',
Jon Hall61282e32015-03-19 11:34:11 -0700617 'Username for (.*): ',
618 'Already on \'',
Jon Hall7a8354f2015-06-10 15:37:00 -0700619 'Switched to (a new )?branch \'' + str( branch ),
Jon Hall274b6642015-02-17 11:57:17 -0800620 pexpect.TIMEOUT,
621 'error: Your local changes to the following files' +
Jon Hallefbd9792015-03-05 16:11:36 -0800622 'would be overwritten by checkout:',
Jon Hall274b6642015-02-17 11:57:17 -0800623 'error: you need to resolve your current index first',
624 "You are in 'detached HEAD' state.",
625 "HEAD is now at " ],
kelvin-onlabd3b64892015-01-20 13:26:24 -0800626 timeout=60 )
kelvin8ec71442015-01-15 16:57:00 -0800627 if i == 0:
628 main.log.error(
629 self.name +
630 ": Git checkout had some issue..." )
631 main.log.error( self.name + ": " + self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400632 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800633 elif i == 1:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800634 main.log.error(
635 self.name +
636 ": Git checkout asking for username." +
637 " Please configure your local git repository to be able " +
638 "to access your remote repository passwordlessly" )
Jon Hall274b6642015-02-17 11:57:17 -0800639 # TODO add support for authenticating
Jon Hallacabffd2014-10-09 12:36:53 -0400640 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800641 elif i == 2:
642 main.log.info(
643 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800644 ": Git Checkout %s : Already on this branch" % branch )
Devin Limc20e79a2017-06-07 10:29:57 -0700645 self.handle.expect( self.prompt )
kelvin8ec71442015-01-15 16:57:00 -0800646 # main.log.info( "DEBUG: after checkout cmd = "+
647 # self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400648 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800649 elif i == 3:
650 main.log.info(
651 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800652 ": Git checkout %s - Switched to this branch" % branch )
Devin Limc20e79a2017-06-07 10:29:57 -0700653 self.handle.expect( self.prompt )
kelvin8ec71442015-01-15 16:57:00 -0800654 # main.log.info( "DEBUG: after checkout cmd = "+
655 # self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400656 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -0800657 elif i == 4:
658 main.log.error( self.name + ": Git Checkout- TIMEOUT" )
659 main.log.error(
Jon Hall274b6642015-02-17 11:57:17 -0800660 self.name + " Response was: " + str( self.handle.before ) )
You Wang141b43b2018-06-26 16:50:18 -0700661 self.handle.send( "\x03" ) # Control-C
662 self.handle.expect( self.prompt )
Jon Hallacabffd2014-10-09 12:36:53 -0400663 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800664 elif i == 5:
665 self.handle.expect( "Aborting" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800666 main.log.error(
667 self.name +
668 ": Git checkout error: \n" +
Jon Hall274b6642015-02-17 11:57:17 -0800669 "Your local changes to the following files would" +
670 " be overwritten by checkout:" +
671 str( self.handle.before ) )
Devin Limc20e79a2017-06-07 10:29:57 -0700672 self.handle.expect( self.prompt )
Jon Hall81e29af2014-11-04 20:41:23 -0500673 return main.ERROR
kelvin8ec71442015-01-15 16:57:00 -0800674 elif i == 6:
Jon Hall274b6642015-02-17 11:57:17 -0800675 main.log.error(
676 self.name +
677 ": Git checkout error: \n" +
678 "You need to resolve your current index first:" +
679 str( self.handle.before ) )
Devin Limc20e79a2017-06-07 10:29:57 -0700680 self.handle.expect( self.prompt )
Jon Hall81e29af2014-11-04 20:41:23 -0500681 return main.ERROR
Jon Hall274b6642015-02-17 11:57:17 -0800682 elif i == 7:
683 main.log.info(
684 self.name +
685 ": Git checkout " + str( branch ) +
686 " - You are in 'detached HEAD' state. HEAD is now at " +
687 str( branch ) )
Devin Limc20e79a2017-06-07 10:29:57 -0700688 self.handle.expect( self.prompt )
Jon Hall274b6642015-02-17 11:57:17 -0800689 return main.TRUE
690 elif i == 8: # Already in detached HEAD on the specified commit
691 main.log.info(
692 self.name +
693 ": Git Checkout %s : Already on commit" % branch )
Devin Limc20e79a2017-06-07 10:29:57 -0700694 self.handle.expect( self.prompt )
Jon Hall274b6642015-02-17 11:57:17 -0800695 return main.TRUE
Jon Hallacabffd2014-10-09 12:36:53 -0400696 else:
kelvin8ec71442015-01-15 16:57:00 -0800697 main.log.error(
698 self.name +
Jon Hall274b6642015-02-17 11:57:17 -0800699 ": Git Checkout - Unexpected response, " +
700 "check for pull errors" )
kelvin8ec71442015-01-15 16:57:00 -0800701 main.log.error( self.name + ": " + self.handle.before )
Jon Hallacabffd2014-10-09 12:36:53 -0400702 return main.ERROR
703
704 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800705 main.log.error( self.name + ": EOF exception found" )
706 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700707 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800708 except Exception:
709 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700710 main.cleanAndExit()
andrewonlab95ca1462014-10-09 14:04:24 -0400711
pingping-lin6d23d9e2015-02-02 16:54:24 -0800712 def getBranchName( self ):
You Wang9cdf9a22017-05-01 13:44:18 -0700713 import re
714 try:
715 main.log.info( "self.home = " )
716 main.log.info( self.home )
717 self.handle.sendline( "cd " + self.home )
Devin Limc20e79a2017-06-07 10:29:57 -0700718 self.handle.expect( self.prompt )
You Wang9cdf9a22017-05-01 13:44:18 -0700719 self.handle.sendline( "git name-rev --name-only HEAD" )
720 self.handle.expect( "git name-rev --name-only HEAD" )
Devin Limc20e79a2017-06-07 10:29:57 -0700721 self.handle.expect( self.prompt )
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700722 lines = self.handle.before.splitlines()
723 if lines[ 1 ] == "master" or re.search( "^onos-\d+(\.\d+)+$", lines[ 1 ] ):
724 return lines[ 1 ]
You Wang9cdf9a22017-05-01 13:44:18 -0700725 else:
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700726 main.log.info( lines[ 1 ] )
You Wang9cdf9a22017-05-01 13:44:18 -0700727 return "unexpected ONOS branch"
728 except pexpect.EOF:
729 main.log.error( self.name + ": EOF exception found" )
730 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700731 main.cleanAndExit()
You Wang9cdf9a22017-05-01 13:44:18 -0700732 except pexpect.TIMEOUT:
733 main.log.error( self.name + ": TIMEOUT exception found" )
734 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700735 main.cleanAndExit()
You Wang9cdf9a22017-05-01 13:44:18 -0700736 except Exception:
737 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700738 main.cleanAndExit()
pingping-lin6d23d9e2015-02-02 16:54:24 -0800739
kelvin-onlabd3b64892015-01-20 13:26:24 -0800740 def getVersion( self, report=False ):
kelvin8ec71442015-01-15 16:57:00 -0800741 """
Jon Hall274b6642015-02-17 11:57:17 -0800742 Writes the COMMIT number to the report to be parsed
Jon Hallefbd9792015-03-05 16:11:36 -0800743 by Jenkins data collector.
kelvin8ec71442015-01-15 16:57:00 -0800744 """
Jon Hall45ec0922014-10-10 19:33:49 -0400745 try:
kelvin8ec71442015-01-15 16:57:00 -0800746 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -0700747 self.handle.expect( self.prompt )
kelvin8ec71442015-01-15 16:57:00 -0800748 self.handle.sendline(
749 "cd " +
750 self.home +
Jon Hall274b6642015-02-17 11:57:17 -0800751 "; git log -1 --pretty=fuller --decorate=short | grep -A 6 " +
752 " \"commit\" --color=never" )
kelvin8ec71442015-01-15 16:57:00 -0800753 # NOTE: for some reason there are backspaces inserted in this
754 # phrase when run from Jenkins on some tests
755 self.handle.expect( "never" )
Devin Limc20e79a2017-06-07 10:29:57 -0700756 self.handle.expect( self.prompt )
kelvin8ec71442015-01-15 16:57:00 -0800757 response = ( self.name + ": \n" + str(
758 self.handle.before + self.handle.after ) )
759 self.handle.sendline( "cd " + self.home )
Devin Limc20e79a2017-06-07 10:29:57 -0700760 self.handle.expect( self.prompt )
kelvin8ec71442015-01-15 16:57:00 -0800761 lines = response.splitlines()
Jon Hall45ec0922014-10-10 19:33:49 -0400762 for line in lines:
Jon Hallfd191202014-11-07 18:36:09 -0500763 print line
764 if report:
pingping-lin763ee042015-05-20 17:45:30 -0700765 main.log.wiki( "<blockquote>" )
kelvin8ec71442015-01-15 16:57:00 -0800766 for line in lines[ 2:-1 ]:
767 # Bracket replacement is for Wiki-compliant
768 # formatting. '<' or '>' are interpreted
769 # as xml specific tags that cause errors
770 line = line.replace( "<", "[" )
771 line = line.replace( ">", "]" )
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700772 # main.log.wiki( "\t" + line )
pingping-lin763ee042015-05-20 17:45:30 -0700773 main.log.wiki( line + "<br /> " )
774 main.log.summary( line )
775 main.log.wiki( "</blockquote>" )
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700776 main.log.summary( "\n" )
kelvin8ec71442015-01-15 16:57:00 -0800777 return lines[ 2 ]
Jon Hall45ec0922014-10-10 19:33:49 -0400778 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800779 main.log.error( self.name + ": EOF exception found" )
780 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700781 main.cleanAndExit()
Jon Hall368769f2014-11-19 15:43:35 -0800782 except pexpect.TIMEOUT:
kelvin8ec71442015-01-15 16:57:00 -0800783 main.log.error( self.name + ": TIMEOUT exception found" )
784 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700785 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800786 except Exception:
787 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700788 main.cleanAndExit()
Jon Hall45ec0922014-10-10 19:33:49 -0400789
kelvin-onlabd3b64892015-01-20 13:26:24 -0800790 def createCellFile( self, benchIp, fileName, mnIpAddrs,
Jon Hall810b67d2016-12-05 10:19:01 -0800791 appString, onosIpAddrs, onosUser="sdn", useSSH=True ):
kelvin8ec71442015-01-15 16:57:00 -0800792 """
andrewonlab94282092014-10-10 13:00:11 -0400793 Creates a cell file based on arguments
794 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800795 * Bench IP address ( benchIp )
andrewonlab94282092014-10-10 13:00:11 -0400796 - Needed to copy the cell file over
kelvin-onlabd3b64892015-01-20 13:26:24 -0800797 * File name of the cell file ( fileName )
798 * Mininet IP address ( mnIpAddrs )
kelvin8ec71442015-01-15 16:57:00 -0800799 - Note that only 1 ip address is
andrewonlab94282092014-10-10 13:00:11 -0400800 supported currently
kelvin-onlabd3b64892015-01-20 13:26:24 -0800801 * ONOS IP addresses ( onosIpAddrs )
andrewonlab94282092014-10-10 13:00:11 -0400802 - Must be passed in as last arguments
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000803 * ONOS USER (onosUser)
Flavio Castrocc38a542016-03-03 13:15:46 -0800804 - optional argument to set ONOS_USER environment variable
kelvin8ec71442015-01-15 16:57:00 -0800805
andrewonlab94282092014-10-10 13:00:11 -0400806 NOTE: Assumes cells are located at:
807 ~/<self.home>/tools/test/cells/
kelvin8ec71442015-01-15 16:57:00 -0800808 """
andrewonlab94282092014-10-10 13:00:11 -0400809 try:
Devin Lim461f0872017-06-05 16:49:33 -0700810
Jon Hall2c8959e2016-12-16 12:17:34 -0800811 # Variable initialization
812 cellDirectory = self.home + "/tools/test/cells/"
813 # We want to create the cell file in the dependencies directory
814 # of TestON first, then copy over to ONOS bench
815 tempDirectory = "/tmp/"
816 # Create the cell file in the directory for writing ( w+ )
817 cellFile = open( tempDirectory + fileName, 'w+' )
818 if isinstance( onosIpAddrs, types.StringType ):
819 onosIpAddrs = [ onosIpAddrs ]
820
821 # App string is hardcoded environment variables
822 # That you may wish to use by default on startup.
823 # Note that you may not want certain apps listed
824 # on here.
825 appString = "export ONOS_APPS=" + appString
826 onosGroup = "export ONOS_GROUP=" + onosUser
827 onosUser = "export ONOS_USER=" + onosUser
828 if useSSH:
829 onosUseSSH = "export ONOS_USE_SSH=true"
830 mnString = "export OCN="
831 if mnIpAddrs == "":
832 mnString = ""
833 onosString = "export OC"
834 tempCount = 1
835
836 # Create ONOSNIC ip address prefix
837 tempOnosIp = str( onosIpAddrs[ 0 ] )
838 tempList = []
839 tempList = tempOnosIp.split( "." )
840 # Omit last element of list to format for NIC
841 tempList = tempList[ :-1 ]
842 # Structure the nic string ip
843 nicAddr = ".".join( tempList ) + ".*"
844 self.nicAddr = nicAddr
845 onosNicString = "export ONOS_NIC=" + nicAddr
846
kelvin8ec71442015-01-15 16:57:00 -0800847 # Start writing to file
kelvin-onlabd3b64892015-01-20 13:26:24 -0800848 cellFile.write( onosNicString + "\n" )
andrewonlab94282092014-10-10 13:00:11 -0400849
kelvin-onlabd3b64892015-01-20 13:26:24 -0800850 for arg in onosIpAddrs:
851 # For each argument in onosIpAddrs, write to file
kelvin8ec71442015-01-15 16:57:00 -0800852 # Output should look like the following:
andrewonlabd4940492014-10-24 12:21:27 -0400853 # export OC1="10.128.20.11"
854 # export OC2="10.128.20.12"
kelvin-onlabd3b64892015-01-20 13:26:24 -0800855 cellFile.write( onosString + str( tempCount ) +
Jon Hall6f665652015-09-18 10:08:07 -0700856 "=\"" + arg + "\"\n" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800857 tempCount = tempCount + 1
kelvin8ec71442015-01-15 16:57:00 -0800858
Jon Hall6f665652015-09-18 10:08:07 -0700859 cellFile.write( "export OCI=$OC1\n" )
Jon Hallab611372018-02-21 15:26:05 -0800860 if mnString:
861 cellFile.write( mnString + "\"" + str( mnIpAddrs ) + "\"\n" )
cameron@onlab.us75900962015-03-30 13:22:49 -0700862 cellFile.write( appString + "\n" )
Flavio Castrocc38a542016-03-03 13:15:46 -0800863 cellFile.write( onosGroup + "\n" )
864 cellFile.write( onosUser + "\n" )
Pier88189b62016-09-07 17:01:53 -0700865 if useSSH:
866 cellFile.write( onosUseSSH + "\n" )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800867 cellFile.close()
andrewonlab94282092014-10-10 13:00:11 -0400868
kelvin8ec71442015-01-15 16:57:00 -0800869 # We use os.system to send the command to TestON cluster
870 # to account for the case in which TestON is not located
871 # on the same cluster as the ONOS bench
872 # Note that even if TestON is located on the same cluster
873 # as ONOS bench, you must setup passwordless ssh
874 # between TestON and ONOS bench in order to automate the test.
kelvin-onlabc2b79102015-07-14 11:41:20 -0700875 os.system( "scp " + tempDirectory + fileName + " " +
876 self.user_name + "@" + self.ip_address + ":" + cellDirectory )
andrewonlab94282092014-10-10 13:00:11 -0400877
andrewonlab2a6c9342014-10-16 13:40:15 -0400878 return main.TRUE
879
andrewonlab94282092014-10-10 13:00:11 -0400880 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800881 main.log.error( self.name + ": EOF exception found" )
882 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700883 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800884 except Exception:
885 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700886 main.cleanAndExit()
andrewonlab94282092014-10-10 13:00:11 -0400887
kelvin-onlabd3b64892015-01-20 13:26:24 -0800888 def setCell( self, cellname ):
kelvin8ec71442015-01-15 16:57:00 -0800889 """
andrewonlab95ca1462014-10-09 14:04:24 -0400890 Calls 'cell <name>' to set the environment variables on ONOSbench
kelvin8ec71442015-01-15 16:57:00 -0800891 """
andrewonlab95ca1462014-10-09 14:04:24 -0400892 try:
893 if not cellname:
You Wang1cdc5f52017-12-19 16:47:51 -0800894 main.log.error( self.name + ": Must define cellname" )
Devin Lim44075962017-08-11 10:56:37 -0700895 main.cleanAndExit()
andrewonlab95ca1462014-10-09 14:04:24 -0400896 else:
kelvin8ec71442015-01-15 16:57:00 -0800897 self.handle.sendline( "cell " + str( cellname ) )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800898 # Expect the cellname in the ONOSCELL variable.
kelvin8ec71442015-01-15 16:57:00 -0800899 # Note that this variable name is subject to change
andrewonlab95ca1462014-10-09 14:04:24 -0400900 # and that this driver will have to change accordingly
Jon Hall3b489db2015-10-05 14:38:37 -0700901 self.handle.expect( str( cellname ) )
Jon Hallab611372018-02-21 15:26:05 -0800902 response = self.handle.before + self.handle.after
You Wang1cdc5f52017-12-19 16:47:51 -0800903 i = self.handle.expect( [ "No such cell",
Jon Hallab611372018-02-21 15:26:05 -0800904 "command not found",
905 self.prompt ], timeout=10 )
906 response += self.handle.before + self.handle.after
You Wang1cdc5f52017-12-19 16:47:51 -0800907 if i == 0:
Jon Hallab611372018-02-21 15:26:05 -0800908 main.log.error( self.name + ": No such cell. Response: " + str( response ) )
You Wang1cdc5f52017-12-19 16:47:51 -0800909 main.cleanAndExit()
910 elif i == 1:
Jon Hallab611372018-02-21 15:26:05 -0800911 main.log.error( self.name + ": Error setting cell. Response: " + str( response ) )
912 main.cleanAndExit()
You Wang1cdc5f52017-12-19 16:47:51 -0800913 elif i == 2:
Jon Hallab611372018-02-21 15:26:05 -0800914 main.log.info( self.name + ": Successfully set cell: " + str( response ) )
andrewonlab95ca1462014-10-09 14:04:24 -0400915 return main.TRUE
You Wang1cdc5f52017-12-19 16:47:51 -0800916 except pexpect.TIMEOUT:
917 main.log.error( self.name + ": TIMEOUT exception found" )
918 main.log.error( self.name + ": " + self.handle.before )
919 main.cleanAndExit()
andrewonlab95ca1462014-10-09 14:04:24 -0400920 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -0800921 main.log.error( self.name + ": EOF exception found" )
922 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700923 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800924 except Exception:
925 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700926 main.cleanAndExit()
andrewonlab95ca1462014-10-09 14:04:24 -0400927
kelvin-onlabd3b64892015-01-20 13:26:24 -0800928 def verifyCell( self ):
kelvin8ec71442015-01-15 16:57:00 -0800929 """
andrewonlabc03bf6c2014-10-09 14:56:18 -0400930 Calls 'onos-verify-cell' to check for cell installation
kelvin8ec71442015-01-15 16:57:00 -0800931 """
932 # TODO: Add meaningful expect value
andrewonlab8d0d7d72014-10-09 16:33:15 -0400933
andrewonlabc03bf6c2014-10-09 14:56:18 -0400934 try:
kelvin8ec71442015-01-15 16:57:00 -0800935 # Clean handle by sending empty and expecting $
936 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -0700937 self.handle.expect( self.prompt )
kelvin8ec71442015-01-15 16:57:00 -0800938 self.handle.sendline( "onos-verify-cell" )
Devin Limc20e79a2017-06-07 10:29:57 -0700939 self.handle.expect( self.prompt )
kelvin-onlabd3b64892015-01-20 13:26:24 -0800940 handleBefore = self.handle.before
941 handleAfter = self.handle.after
kelvin-onlabd3b64892015-01-20 13:26:24 -0800942 main.log.info( "Verify cell returned: " + handleBefore +
Jon Hall3b489db2015-10-05 14:38:37 -0700943 handleAfter )
andrewonlabc03bf6c2014-10-09 14:56:18 -0400944 return main.TRUE
Jon Halla5cb6172015-02-23 09:28:28 -0800945 except pexpect.ExceptionPexpect as e:
Jon Hall3b489db2015-10-05 14:38:37 -0700946 main.log.exception( self.name + ": Pexpect exception found: " )
kelvin8ec71442015-01-15 16:57:00 -0800947 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700948 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -0800949 except Exception:
950 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700951 main.cleanAndExit()
Jon Hall7993bfc2014-10-09 16:30:14 -0400952
jenkins1e99e7b2015-04-02 18:15:39 -0700953 def onosCfgSet( self, ONOSIp, configName, configParam ):
954 """
955 Uses 'onos <node-ip> cfg set' to change a parameter value of an
Jon Hall4ba53f02015-07-29 13:07:41 -0700956 application.
957
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +0000958 ex)
jenkins1e99e7b2015-04-02 18:15:39 -0700959 onos 10.0.0.1 cfg set org.onosproject.myapp appSetting 1
jenkins1e99e7b2015-04-02 18:15:39 -0700960 ONOSIp = '10.0.0.1'
961 configName = 'org.onosproject.myapp'
962 configParam = 'appSetting 1'
jenkins1e99e7b2015-04-02 18:15:39 -0700963 """
Jon Hall72280bc2016-01-25 14:29:05 -0800964 try:
965 cfgStr = "onos {} cfg set {} {}".format( ONOSIp,
966 configName,
967 configParam )
968 self.handle.sendline( "" )
969 self.handle.expect( ":~" )
970 self.handle.sendline( cfgStr )
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700971 self.handle.expect( "cfg set" )
Jon Hall72280bc2016-01-25 14:29:05 -0800972 self.handle.expect( ":~" )
jenkins1e99e7b2015-04-02 18:15:39 -0700973
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700974 paramValue = configParam.split( " " )[ 1 ]
975 paramName = configParam.split( " " )[ 0 ]
Jon Hall4ba53f02015-07-29 13:07:41 -0700976
Jon Hall72280bc2016-01-25 14:29:05 -0800977 checkStr = 'onos {} cfg get " {} {} " '.format( ONOSIp, configName, paramName )
Jon Hall4ba53f02015-07-29 13:07:41 -0700978
Jon Hall72280bc2016-01-25 14:29:05 -0800979 self.handle.sendline( checkStr )
980 self.handle.expect( ":~" )
jenkins1e99e7b2015-04-02 18:15:39 -0700981
Jon Hall72280bc2016-01-25 14:29:05 -0800982 if "value=" + paramValue + "," in self.handle.before:
Jeremy Ronquillo82705492017-10-18 14:19:55 -0700983 main.log.info( "cfg " + configName + " successfully set to " + configParam )
Jon Hall72280bc2016-01-25 14:29:05 -0800984 return main.TRUE
985 except pexpect.ExceptionPexpect as e:
986 main.log.exception( self.name + ": Pexpect exception found: " )
987 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -0700988 main.cleanAndExit()
Jon Hall72280bc2016-01-25 14:29:05 -0800989 except Exception:
990 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700991 main.cleanAndExit()
Jon Hall4ba53f02015-07-29 13:07:41 -0700992
You Wang54b1d672018-06-11 16:44:13 -0700993 def onosCli( self, ONOSIp, cmdstr, timeout=60 ):
kelvin8ec71442015-01-15 16:57:00 -0800994 """
andrewonlab05e362f2014-10-10 00:40:57 -0400995 Uses 'onos' command to send various ONOS CLI arguments.
996 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -0800997 * ONOSIp: specify the ip of the cell machine
andrewonlab94282092014-10-10 13:00:11 -0400998 * cmdstr: specify the command string to send
You Wang54b1d672018-06-11 16:44:13 -0700999 Optional:
1000 * timeout: pexpect timeout for running the command
kelvin8ec71442015-01-15 16:57:00 -08001001
1002 This function is intended to expose the entire karaf
andrewonlab6e20c342014-10-10 18:08:48 -04001003 CLI commands for ONOS. Try to use this function first
1004 before attempting to write a ONOS CLI specific driver
kelvin8ec71442015-01-15 16:57:00 -08001005 function.
1006 You can see a list of available 'cmdstr' arguments
andrewonlab6e20c342014-10-10 18:08:48 -04001007 by starting onos, and typing in 'onos' to enter the
1008 onos> CLI. Then, type 'help' to see the list of
kelvin8ec71442015-01-15 16:57:00 -08001009 available commands.
1010 """
andrewonlab05e362f2014-10-10 00:40:57 -04001011 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001012 if not ONOSIp:
kelvin8ec71442015-01-15 16:57:00 -08001013 main.log.error( "You must specify the IP address" )
andrewonlab05e362f2014-10-10 00:40:57 -04001014 return main.FALSE
1015 if not cmdstr:
kelvin8ec71442015-01-15 16:57:00 -08001016 main.log.error( "You must specify the command string" )
andrewonlab05e362f2014-10-10 00:40:57 -04001017 return main.FALSE
1018
kelvin8ec71442015-01-15 16:57:00 -08001019 cmdstr = str( cmdstr )
1020 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001021 self.handle.expect( self.prompt )
andrewonlab05e362f2014-10-10 00:40:57 -04001022
You Wangdd3dae52018-02-15 13:31:25 -08001023 self.handle.sendline( "onos-wait-for-start " + ONOSIp )
1024 self.handle.expect( self.prompt )
1025
1026 self.handle.sendline( "onos " + ONOSIp + " " + cmdstr )
You Wang54b1d672018-06-11 16:44:13 -07001027 i = self.handle.expect( [ self.prompt, pexpect.TIMEOUT ], timeout=timeout )
1028 if i == 0:
1029 handleBefore = self.handle.before
1030 main.log.info( "Command sent successfully" )
1031 # Obtain return handle that consists of result from
1032 # the onos command. The string may need to be
1033 # configured further.
1034 returnString = handleBefore
1035 return returnString
1036 elif i == 1:
1037 main.log.error( self.name + ": Timeout when sending " + cmdstr )
You Wang141b43b2018-06-26 16:50:18 -07001038 self.handle.send( "\x03" ) # Control-C
You Wang54b1d672018-06-11 16:44:13 -07001039 self.handle.expect( self.prompt )
1040 return main.FALSE
You Wangd66de192018-04-30 17:30:12 -07001041 except pexpect.TIMEOUT:
1042 main.log.exception( self.name + ": Timeout when sending " + cmdstr )
1043 return main.FALSE
andrewonlab05e362f2014-10-10 00:40:57 -04001044 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001045 main.log.error( self.name + ": EOF exception found" )
1046 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001047 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001048 except Exception:
1049 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001050 main.cleanAndExit()
Jon Hall7993bfc2014-10-09 16:30:14 -04001051
Jeremy Ronquillo82705492017-10-18 14:19:55 -07001052 def onosSecureSSH( self, userName="onos", userPWD="rocks", node="" ):
Pier88189b62016-09-07 17:01:53 -07001053 """
1054 Enables secure access to ONOS console
1055 by removing default users & keys.
1056
1057 onos-secure-ssh -u onos -p rocks node
1058
1059 Returns: main.TRUE on success and main.FALSE on failure
1060 """
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00001061
Pier88189b62016-09-07 17:01:53 -07001062 try:
Chiyu Chengef109502016-11-21 15:51:38 -08001063 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001064 self.handle.expect( self.prompt )
Pier88189b62016-09-07 17:01:53 -07001065 self.handle.sendline( " onos-secure-ssh -u " + userName + " -p " + userPWD + " " + node )
1066
1067 # NOTE: this timeout may need to change depending on the network
1068 # and size of ONOS
1069 # TODO: Handle the other possible error
Jeremy Ronquillo82705492017-10-18 14:19:55 -07001070 i = self.handle.expect( [ "Network\sis\sunreachable",
1071 self.prompt,
1072 pexpect.TIMEOUT ], timeout=180 )
Pier88189b62016-09-07 17:01:53 -07001073 if i == 0:
1074 # can't reach ONOS node
1075 main.log.warn( "Network is unreachable" )
Devin Limc20e79a2017-06-07 10:29:57 -07001076 self.handle.expect( self.prompt )
Pier88189b62016-09-07 17:01:53 -07001077 return main.FALSE
1078 elif i == 1:
1079 # Process started
1080 main.log.info(
Jeremy Ronquillo82705492017-10-18 14:19:55 -07001081 "Secure SSH performed on " +
1082 node )
Pier88189b62016-09-07 17:01:53 -07001083 return main.TRUE
1084 except pexpect.EOF:
1085 main.log.error( self.name + ": EOF exception found" )
1086 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001087 main.cleanAndExit()
Pier88189b62016-09-07 17:01:53 -07001088 except Exception:
1089 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001090 main.cleanAndExit()
Pier88189b62016-09-07 17:01:53 -07001091
kelvin-onlabd3b64892015-01-20 13:26:24 -08001092 def onosInstall( self, options="-f", node="" ):
kelvin8ec71442015-01-15 16:57:00 -08001093 """
Jon Hall7993bfc2014-10-09 16:30:14 -04001094 Installs ONOS bits on the designated cell machine.
kelvin8ec71442015-01-15 16:57:00 -08001095 If -f option is provided, it also forces an uninstall.
1096 Presently, install also includes onos-push-bits and
Jon Hall7993bfc2014-10-09 16:30:14 -04001097 onos-config within.
kelvin8ec71442015-01-15 16:57:00 -08001098 The node option allows you to selectively only push the jar
Jon Hall7993bfc2014-10-09 16:30:14 -04001099 files to certain onos nodes
1100
1101 Returns: main.TRUE on success and main.FALSE on failure
kelvin8ec71442015-01-15 16:57:00 -08001102 """
Jon Hall7993bfc2014-10-09 16:30:14 -04001103 try:
andrewonlab114768a2014-11-14 12:44:44 -05001104 if options:
kelvin8ec71442015-01-15 16:57:00 -08001105 self.handle.sendline( "onos-install " + options + " " + node )
andrewonlab114768a2014-11-14 12:44:44 -05001106 else:
kelvin8ec71442015-01-15 16:57:00 -08001107 self.handle.sendline( "onos-install " + node )
1108 self.handle.expect( "onos-install " )
1109 # NOTE: this timeout may need to change depending on the network
1110 # and size of ONOS
1111 i = self.handle.expect( [ "Network\sis\sunreachable",
kelvin-onlabd3b64892015-01-20 13:26:24 -08001112 "onos\sstart/running,\sprocess",
kelvin8ec71442015-01-15 16:57:00 -08001113 "ONOS\sis\salready\sinstalled",
Jeremyc72b2582016-02-26 18:27:38 -08001114 "already\sup-to-date",
Jon Hall3576f572016-08-23 10:01:07 -07001115 "does not exist",
Devin Limc20e79a2017-06-07 10:29:57 -07001116 self.prompt,
Jon Hall6c44c0b2016-04-20 15:21:00 -07001117 pexpect.TIMEOUT ], timeout=180 )
Jon Hall7993bfc2014-10-09 16:30:14 -04001118 if i == 0:
Jon Hall3576f572016-08-23 10:01:07 -07001119 # can't reach ONOS node
kelvin8ec71442015-01-15 16:57:00 -08001120 main.log.warn( "Network is unreachable" )
Devin Limc20e79a2017-06-07 10:29:57 -07001121 self.handle.expect( self.prompt )
Jon Hall7993bfc2014-10-09 16:30:14 -04001122 return main.FALSE
1123 elif i == 1:
Jon Hall3576f572016-08-23 10:01:07 -07001124 # Process started
kelvin8ec71442015-01-15 16:57:00 -08001125 main.log.info(
1126 "ONOS was installed on " +
1127 node +
1128 " and started" )
Devin Limc20e79a2017-06-07 10:29:57 -07001129 self.handle.expect( self.prompt )
Jon Hall7993bfc2014-10-09 16:30:14 -04001130 return main.TRUE
Jon Hall3576f572016-08-23 10:01:07 -07001131 elif i == 2 or i == 3:
1132 # same bits are already on ONOS node
Jeremyc72b2582016-02-26 18:27:38 -08001133 main.log.info( "ONOS is already installed on " + node )
Devin Limc20e79a2017-06-07 10:29:57 -07001134 self.handle.expect( self.prompt )
Jeremyc72b2582016-02-26 18:27:38 -08001135 return main.TRUE
1136 elif i == 4:
Jon Hall3576f572016-08-23 10:01:07 -07001137 # onos not packaged
1138 main.log.error( "ONOS package not found." )
Devin Limc20e79a2017-06-07 10:29:57 -07001139 self.handle.expect( self.prompt )
Jon Hall3576f572016-08-23 10:01:07 -07001140 return main.FALSE
1141 elif i == 5:
1142 # prompt
Jeremyc72b2582016-02-26 18:27:38 -08001143 main.log.info( "ONOS was installed on " + node )
1144 return main.TRUE
Jon Hall3576f572016-08-23 10:01:07 -07001145 elif i == 6:
1146 # timeout
kelvin8ec71442015-01-15 16:57:00 -08001147 main.log.info(
1148 "Installation of ONOS on " +
1149 node +
1150 " timed out" )
Devin Limc20e79a2017-06-07 10:29:57 -07001151 self.handle.expect( self.prompt )
Jon Hall53c5e662016-04-13 16:06:56 -07001152 main.log.warn( self.handle.before )
You Wang141b43b2018-06-26 16:50:18 -07001153 self.handle.send( "\x03" ) # Control-C
1154 self.handle.expect( self.prompt )
Jon Hall7993bfc2014-10-09 16:30:14 -04001155 return main.FALSE
andrewonlabc03bf6c2014-10-09 14:56:18 -04001156 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001157 main.log.error( self.name + ": EOF exception found" )
1158 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001159 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001160 except Exception:
1161 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001162 main.cleanAndExit()
andrewonlab95ca1462014-10-09 14:04:24 -04001163
kelvin-onlabd3b64892015-01-20 13:26:24 -08001164 def onosStart( self, nodeIp ):
kelvin8ec71442015-01-15 16:57:00 -08001165 """
andrewonlab8d0d7d72014-10-09 16:33:15 -04001166 Calls onos command: 'onos-service [<node-ip>] start'
andrewonlabe8e56fd2014-10-09 17:12:44 -04001167 This command is a remote management of the ONOS upstart daemon
kelvin8ec71442015-01-15 16:57:00 -08001168 """
andrewonlab8d0d7d72014-10-09 16:33:15 -04001169 try:
kelvin8ec71442015-01-15 16:57:00 -08001170 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001171 self.handle.expect( self.prompt )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001172 self.handle.sendline( "onos-service " + str( nodeIp ) +
kelvin8ec71442015-01-15 16:57:00 -08001173 " start" )
1174 i = self.handle.expect( [
andrewonlab8d0d7d72014-10-09 16:33:15 -04001175 "Job\sis\salready\srunning",
1176 "start/running",
Devin Limc20e79a2017-06-07 10:29:57 -07001177 self.prompt,
andrewonlab8d0d7d72014-10-09 16:33:15 -04001178 "Unknown\sinstance",
Jeremy Songster14c13572016-04-21 17:34:03 -07001179 pexpect.TIMEOUT ], timeout=180 )
andrewonlab8d0d7d72014-10-09 16:33:15 -04001180 if i == 0:
Devin Limc20e79a2017-06-07 10:29:57 -07001181 self.handle.expect( self.prompt )
kelvin8ec71442015-01-15 16:57:00 -08001182 main.log.info( "Service is already running" )
andrewonlab8d0d7d72014-10-09 16:33:15 -04001183 return main.TRUE
1184 elif i == 1:
Devin Limc20e79a2017-06-07 10:29:57 -07001185 self.handle.expect( self.prompt )
kelvin8ec71442015-01-15 16:57:00 -08001186 main.log.info( "ONOS service started" )
andrewonlab8d0d7d72014-10-09 16:33:15 -04001187 return main.TRUE
Jeremyd0e9a6d2016-03-02 11:28:52 -08001188 elif i == 2:
1189 main.log.info( "ONOS service started" )
1190 return main.TRUE
andrewonlab8d0d7d72014-10-09 16:33:15 -04001191 else:
Devin Limc20e79a2017-06-07 10:29:57 -07001192 self.handle.expect( self.prompt )
kelvin8ec71442015-01-15 16:57:00 -08001193 main.log.error( "ONOS service failed to start" )
Devin Lim44075962017-08-11 10:56:37 -07001194
1195 main.cleanAndExit()
andrewonlab8d0d7d72014-10-09 16:33:15 -04001196 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001197 main.log.error( self.name + ": EOF exception found" )
1198 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001199 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001200 except Exception:
1201 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001202 main.cleanAndExit()
andrewonlab8d0d7d72014-10-09 16:33:15 -04001203
kelvin-onlabd3b64892015-01-20 13:26:24 -08001204 def onosStop( self, nodeIp ):
kelvin8ec71442015-01-15 16:57:00 -08001205 """
andrewonlab2b30bd32014-10-09 16:48:55 -04001206 Calls onos command: 'onos-service [<node-ip>] stop'
andrewonlabe8e56fd2014-10-09 17:12:44 -04001207 This command is a remote management of the ONOS upstart daemon
kelvin8ec71442015-01-15 16:57:00 -08001208 """
andrewonlab2b30bd32014-10-09 16:48:55 -04001209 try:
kelvin8ec71442015-01-15 16:57:00 -08001210 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001211 self.handle.expect( self.prompt )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001212 self.handle.sendline( "onos-service " + str( nodeIp ) +
kelvin8ec71442015-01-15 16:57:00 -08001213 " stop" )
1214 i = self.handle.expect( [
andrewonlab2b30bd32014-10-09 16:48:55 -04001215 "stop/waiting",
Jon Hall61282e32015-03-19 11:34:11 -07001216 "Could not resolve hostname",
andrewonlab2b30bd32014-10-09 16:48:55 -04001217 "Unknown\sinstance",
Devin Limc20e79a2017-06-07 10:29:57 -07001218 self.prompt,
Jeremy Songster14c13572016-04-21 17:34:03 -07001219 pexpect.TIMEOUT ], timeout=180 )
andrewonlab2b30bd32014-10-09 16:48:55 -04001220 if i == 0:
Devin Limc20e79a2017-06-07 10:29:57 -07001221 self.handle.expect( self.prompt )
kelvin8ec71442015-01-15 16:57:00 -08001222 main.log.info( "ONOS service stopped" )
andrewonlab2b30bd32014-10-09 16:48:55 -04001223 return main.TRUE
1224 elif i == 1:
Devin Limc20e79a2017-06-07 10:29:57 -07001225 self.handle.expect( self.prompt )
Jon Hall65844a32015-03-09 19:09:37 -07001226 main.log.info( "onosStop() Unknown ONOS instance specified: " +
kelvin-onlabd3b64892015-01-20 13:26:24 -08001227 str( nodeIp ) )
andrewonlab2b30bd32014-10-09 16:48:55 -04001228 return main.FALSE
Jon Hall61282e32015-03-19 11:34:11 -07001229 elif i == 2:
Devin Limc20e79a2017-06-07 10:29:57 -07001230 self.handle.expect( self.prompt )
Jon Hall61282e32015-03-19 11:34:11 -07001231 main.log.warn( "ONOS wasn't running" )
1232 return main.TRUE
YPZhang77badfc2016-03-09 10:28:59 -08001233 elif i == 3:
1234 main.log.info( "ONOS service stopped" )
1235 return main.TRUE
andrewonlab2b30bd32014-10-09 16:48:55 -04001236 else:
kelvin8ec71442015-01-15 16:57:00 -08001237 main.log.error( "ONOS service failed to stop" )
andrewonlab2b30bd32014-10-09 16:48:55 -04001238 return main.FALSE
andrewonlab2b30bd32014-10-09 16:48:55 -04001239 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001240 main.log.error( self.name + ": EOF exception found" )
1241 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001242 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001243 except Exception:
1244 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001245 main.cleanAndExit()
kelvin8ec71442015-01-15 16:57:00 -08001246
kelvin-onlabd3b64892015-01-20 13:26:24 -08001247 def onosUninstall( self, nodeIp="" ):
kelvin8ec71442015-01-15 16:57:00 -08001248 """
andrewonlabc8d47972014-10-09 16:52:36 -04001249 Calls the command: 'onos-uninstall'
kelvin8ec71442015-01-15 16:57:00 -08001250 Uninstalls ONOS from the designated cell machine, stopping
andrewonlabe8e56fd2014-10-09 17:12:44 -04001251 if needed
kelvin8ec71442015-01-15 16:57:00 -08001252 """
andrewonlabc8d47972014-10-09 16:52:36 -04001253 try:
kelvin8ec71442015-01-15 16:57:00 -08001254 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001255 self.handle.expect( self.prompt, timeout=180 )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001256 self.handle.sendline( "onos-uninstall " + str( nodeIp ) )
Devin Limc20e79a2017-06-07 10:29:57 -07001257 self.handle.expect( self.prompt, timeout=180 )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001258 main.log.info( "ONOS " + nodeIp + " was uninstalled" )
kelvin8ec71442015-01-15 16:57:00 -08001259 # onos-uninstall command does not return any text
andrewonlabc8d47972014-10-09 16:52:36 -04001260 return main.TRUE
pingping-lin763ee042015-05-20 17:45:30 -07001261 except pexpect.TIMEOUT:
1262 main.log.exception( self.name + ": Timeout in onosUninstall" )
You Wang141b43b2018-06-26 16:50:18 -07001263 self.handle.send( "\x03" ) # Control-C
1264 self.handle.expect( self.prompt )
pingping-lin763ee042015-05-20 17:45:30 -07001265 return main.FALSE
andrewonlabc8d47972014-10-09 16:52:36 -04001266 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001267 main.log.error( self.name + ": EOF exception found" )
1268 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001269 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001270 except Exception:
1271 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001272 main.cleanAndExit()
andrewonlab2b30bd32014-10-09 16:48:55 -04001273
kelvin-onlabd3b64892015-01-20 13:26:24 -08001274 def onosDie( self, nodeIp ):
kelvin8ec71442015-01-15 16:57:00 -08001275 """
andrewonlabaedc8332014-12-04 12:43:03 -05001276 Issues the command 'onos-die <node-ip>'
1277 This command calls onos-kill and also stops the node
kelvin8ec71442015-01-15 16:57:00 -08001278 """
andrewonlabaedc8332014-12-04 12:43:03 -05001279 try:
kelvin8ec71442015-01-15 16:57:00 -08001280 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001281 self.handle.expect( self.prompt )
Jeremyf0aecdb2016-03-30 13:19:57 -07001282 cmdStr = "onos-die " + str( nodeIp )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001283 self.handle.sendline( cmdStr )
kelvin8ec71442015-01-15 16:57:00 -08001284 i = self.handle.expect( [
andrewonlabaedc8332014-12-04 12:43:03 -05001285 "Killing\sONOS",
1286 "ONOS\sprocess\sis\snot\srunning",
Jeremy Songster14c13572016-04-21 17:34:03 -07001287 pexpect.TIMEOUT ], timeout=60 )
andrewonlabaedc8332014-12-04 12:43:03 -05001288 if i == 0:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001289 main.log.info( "ONOS instance " + str( nodeIp ) +
kelvin8ec71442015-01-15 16:57:00 -08001290 " was killed and stopped" )
Jon Hall53c5e662016-04-13 16:06:56 -07001291 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001292 self.handle.expect( self.prompt )
andrewonlabaedc8332014-12-04 12:43:03 -05001293 return main.TRUE
1294 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -08001295 main.log.info( "ONOS process was not running" )
Jon Hall53c5e662016-04-13 16:06:56 -07001296 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001297 self.handle.expect( self.prompt )
andrewonlabaedc8332014-12-04 12:43:03 -05001298 return main.FALSE
1299 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001300 main.log.error( self.name + ": EOF exception found" )
1301 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001302 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001303 except Exception:
1304 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001305 main.cleanAndExit()
andrewonlabaedc8332014-12-04 12:43:03 -05001306
kelvin-onlabd3b64892015-01-20 13:26:24 -08001307 def onosKill( self, nodeIp ):
kelvin8ec71442015-01-15 16:57:00 -08001308 """
andrewonlabe8e56fd2014-10-09 17:12:44 -04001309 Calls the command: 'onos-kill [<node-ip>]'
1310 "Remotely, and unceremoniously kills the ONOS instance running on
1311 the specified cell machine" - Tom V
kelvin8ec71442015-01-15 16:57:00 -08001312 """
andrewonlabe8e56fd2014-10-09 17:12:44 -04001313 try:
kelvin8ec71442015-01-15 16:57:00 -08001314 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001315 self.handle.expect( self.prompt )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001316 self.handle.sendline( "onos-kill " + str( nodeIp ) )
kelvin8ec71442015-01-15 16:57:00 -08001317 i = self.handle.expect( [
Devin Limc20e79a2017-06-07 10:29:57 -07001318 self.prompt,
andrewonlabe8e56fd2014-10-09 17:12:44 -04001319 "No\sroute\sto\shost",
1320 "password:",
Jeremy Songster14c13572016-04-21 17:34:03 -07001321 pexpect.TIMEOUT ], timeout=60 )
kelvin8ec71442015-01-15 16:57:00 -08001322
andrewonlabe8e56fd2014-10-09 17:12:44 -04001323 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -08001324 main.log.info(
1325 "ONOS instance " + str(
kelvin-onlabd3b64892015-01-20 13:26:24 -08001326 nodeIp ) + " was killed" )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001327 return main.TRUE
1328 elif i == 1:
kelvin8ec71442015-01-15 16:57:00 -08001329 main.log.info( "No route to host" )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001330 return main.FALSE
1331 elif i == 2:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001332 main.log.info(
1333 "Passwordless login for host: " +
1334 str( nodeIp ) +
1335 " not configured" )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001336 return main.FALSE
1337 else:
Jon Hallefbd9792015-03-05 16:11:36 -08001338 main.log.info( "ONOS instance was not killed" )
andrewonlabe8e56fd2014-10-09 17:12:44 -04001339 return main.FALSE
kelvin8ec71442015-01-15 16:57:00 -08001340
andrewonlabe8e56fd2014-10-09 17:12:44 -04001341 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001342 main.log.error( self.name + ": EOF exception found" )
1343 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001344 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001345 except Exception:
1346 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001347 main.cleanAndExit()
andrewonlabe8e56fd2014-10-09 17:12:44 -04001348
kelvin-onlabd3b64892015-01-20 13:26:24 -08001349 def onosRemoveRaftLogs( self ):
kelvin8ec71442015-01-15 16:57:00 -08001350 """
andrewonlab19fbdca2014-11-14 12:55:59 -05001351 Removes Raft / Copy cat files from ONOS to ensure
Jon Hallfcc88622014-11-25 13:09:54 -05001352 a cleaner environment.
1353
andrewonlab19fbdca2014-11-14 12:55:59 -05001354 Description:
Jon Hallfcc88622014-11-25 13:09:54 -05001355 Stops all ONOS defined in the cell,
andrewonlab19fbdca2014-11-14 12:55:59 -05001356 wipes the raft / copycat log files
kelvin8ec71442015-01-15 16:57:00 -08001357 """
andrewonlab19fbdca2014-11-14 12:55:59 -05001358 try:
kelvin8ec71442015-01-15 16:57:00 -08001359 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001360 self.handle.expect( self.prompt )
kelvin8ec71442015-01-15 16:57:00 -08001361 self.handle.sendline( "onos-remove-raft-logs" )
1362 # Sometimes this command hangs
Devin Limc20e79a2017-06-07 10:29:57 -07001363 i = self.handle.expect( [ self.prompt, pexpect.TIMEOUT ],
kelvin8ec71442015-01-15 16:57:00 -08001364 timeout=120 )
Jon Hallfcc88622014-11-25 13:09:54 -05001365 if i == 1:
Devin Limc20e79a2017-06-07 10:29:57 -07001366 i = self.handle.expect( [ self.prompt, pexpect.TIMEOUT ],
kelvin8ec71442015-01-15 16:57:00 -08001367 timeout=120 )
Jon Hallfcc88622014-11-25 13:09:54 -05001368 if i == 1:
1369 return main.FALSE
andrewonlab19fbdca2014-11-14 12:55:59 -05001370 return main.TRUE
andrewonlab19fbdca2014-11-14 12:55:59 -05001371 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001372 main.log.error( self.name + ": EOF exception found" )
1373 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001374 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001375 except Exception:
1376 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001377 main.cleanAndExit()
Jon Hallfcc88622014-11-25 13:09:54 -05001378
kelvin-onlabd3b64892015-01-20 13:26:24 -08001379 def onosStartNetwork( self, mntopo ):
kelvin8ec71442015-01-15 16:57:00 -08001380 """
1381 Calls the command 'onos-start-network [ <mininet-topo> ]
1382 "remotely starts the specified topology on the cell's
andrewonlab94282092014-10-10 13:00:11 -04001383 mininet machine against all controllers configured in the
kelvin8ec71442015-01-15 16:57:00 -08001384 cell."
andrewonlab94282092014-10-10 13:00:11 -04001385 * Specify mininet topology file name for mntopo
1386 * Topo files should be placed at:
1387 ~/<your-onos-directory>/tools/test/topos
kelvin8ec71442015-01-15 16:57:00 -08001388
andrewonlab94282092014-10-10 13:00:11 -04001389 NOTE: This function will take you to the mininet prompt
kelvin8ec71442015-01-15 16:57:00 -08001390 """
andrewonlab94282092014-10-10 13:00:11 -04001391 try:
1392 if not mntopo:
kelvin8ec71442015-01-15 16:57:00 -08001393 main.log.error( "You must specify a topo file to execute" )
andrewonlab94282092014-10-10 13:00:11 -04001394 return main.FALSE
andrewonlab94282092014-10-10 13:00:11 -04001395
kelvin8ec71442015-01-15 16:57:00 -08001396 mntopo = str( mntopo )
1397 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001398 self.handle.expect( self.prompt )
andrewonlab94282092014-10-10 13:00:11 -04001399
kelvin8ec71442015-01-15 16:57:00 -08001400 self.handle.sendline( "onos-start-network " + mntopo )
1401 self.handle.expect( "mininet>" )
1402 main.log.info( "Network started, entered mininet prompt" )
1403
1404 # TODO: Think about whether return is necessary or not
andrewonlab94282092014-10-10 13:00:11 -04001405
1406 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001407 main.log.error( self.name + ": EOF exception found" )
1408 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001409 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001410 except Exception:
1411 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001412 main.cleanAndExit()
andrewonlab94282092014-10-10 13:00:11 -04001413
Jeremy Songster14c13572016-04-21 17:34:03 -07001414 def isup( self, node="", timeout=240 ):
kelvin8ec71442015-01-15 16:57:00 -08001415 """
1416 Run's onos-wait-for-start which only returns once ONOS is at run
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00001417 level 100(ready for use)
andrewonlab8d0d7d72014-10-09 16:33:15 -04001418
Jon Hall7993bfc2014-10-09 16:30:14 -04001419 Returns: main.TRUE if ONOS is running and main.FALSE on timeout
kelvin8ec71442015-01-15 16:57:00 -08001420 """
Jon Hall7993bfc2014-10-09 16:30:14 -04001421 try:
Jon Hall3b489db2015-10-05 14:38:37 -07001422 self.handle.sendline( "onos-wait-for-start " + node )
1423 self.handle.expect( "onos-wait-for-start" )
kelvin8ec71442015-01-15 16:57:00 -08001424 # NOTE: this timeout is arbitrary"
Jon Hallcababf72018-02-05 12:05:19 -08001425 i = self.handle.expect( [ self.prompt, pexpect.TIMEOUT, "Password:" ], timeout )
Jon Hall7993bfc2014-10-09 16:30:14 -04001426 if i == 0:
kelvin8ec71442015-01-15 16:57:00 -08001427 main.log.info( self.name + ": " + node + " is up" )
Jon Hall7993bfc2014-10-09 16:30:14 -04001428 return main.TRUE
Jon Hallcababf72018-02-05 12:05:19 -08001429 elif i == 1 or i == 2:
kelvin8ec71442015-01-15 16:57:00 -08001430 # NOTE: since this function won't return until ONOS is ready,
Jon Hall7993bfc2014-10-09 16:30:14 -04001431 # we will kill it on timeout
Jon Hallcababf72018-02-05 12:05:19 -08001432 if i == 1:
1433 main.log.error( "ONOS has not started yet" )
1434 elif i == 2:
1435 main.log.error( "Cannot login to ONOS CLI, try using onos-secure-ssh" )
kelvin8ec71442015-01-15 16:57:00 -08001436 self.handle.send( "\x03" ) # Control-C
Devin Limc20e79a2017-06-07 10:29:57 -07001437 self.handle.expect( self.prompt )
Jon Hall7993bfc2014-10-09 16:30:14 -04001438 return main.FALSE
1439 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001440 main.log.error( self.name + ": EOF exception found" )
1441 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001442 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001443 except Exception:
1444 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001445 main.cleanAndExit()
andrewonlab05e362f2014-10-10 00:40:57 -04001446
Devin Lim142b5342017-07-20 15:22:39 -07001447 def preventAutoRespawn( self ):
1448 """
1449 Description:
1450 This will prevent ONOSservice to automatically
1451 respawn.
1452 """
1453 try:
1454 self.handle.sendline( "sed -i -e 's/^respawn$/#respawn/g' tools/package/init/onos.conf" )
1455 self.handle.expect( "\$" ) # $ from the command
1456 self.handle.sendline( "sed -i -e 's/^Restart=always/Restart=no/g' tools/package/init/onos.service" )
1457 self.handle.expect( "\$" ) # $ from the command
1458 self.handle.expect( "\$" ) # $ from the prompt
1459 except pexpect.EOF:
1460 main.log.error( self.name + ": EOF exception found" )
1461 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001462 main.cleanAndExit()
Devin Lim142b5342017-07-20 15:22:39 -07001463 except Exception:
1464 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001465 main.cleanAndExit()
Devin Lim142b5342017-07-20 15:22:39 -07001466
kelvin-onlabd3b64892015-01-20 13:26:24 -08001467 def pushTestIntentsShell(
1468 self,
1469 dpidSrc,
1470 dpidDst,
1471 numIntents,
1472 dirFile,
1473 onosIp,
1474 numMult="",
1475 appId="",
1476 report=True,
1477 options="" ):
kelvin8ec71442015-01-15 16:57:00 -08001478 """
andrewonlabb66dfa12014-12-02 15:51:10 -05001479 Description:
kelvin8ec71442015-01-15 16:57:00 -08001480 Use the linux prompt to push test intents to
andrewonlabb66dfa12014-12-02 15:51:10 -05001481 better parallelize the results than the CLI
1482 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001483 * dpidSrc: specify source dpid
1484 * dpidDst: specify destination dpid
1485 * numIntents: specify number of intents to push
1486 * dirFile: specify directory and file name to save
andrewonlabb66dfa12014-12-02 15:51:10 -05001487 results
kelvin-onlabd3b64892015-01-20 13:26:24 -08001488 * onosIp: specify the IP of ONOS to install on
kelvin8ec71442015-01-15 16:57:00 -08001489 NOTE:
andrewonlabb66dfa12014-12-02 15:51:10 -05001490 You must invoke this command at linux shell prompt
kelvin8ec71442015-01-15 16:57:00 -08001491 """
1492 try:
1493 # Create the string to sendline
andrewonlabaedc8332014-12-04 12:43:03 -05001494 if options:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001495 baseCmd = "onos " + str( onosIp ) + " push-test-intents " +\
kelvin8ec71442015-01-15 16:57:00 -08001496 options + " "
andrewonlabaedc8332014-12-04 12:43:03 -05001497 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001498 baseCmd = "onos " + str( onosIp ) + " push-test-intents "
kelvin8ec71442015-01-15 16:57:00 -08001499
kelvin-onlabd3b64892015-01-20 13:26:24 -08001500 addDpid = baseCmd + str( dpidSrc ) + " " + str( dpidDst )
1501 if not numMult:
1502 addIntents = addDpid + " " + str( numIntents )
1503 elif numMult:
1504 addIntents = addDpid + " " + str( numIntents ) + " " +\
1505 str( numMult )
1506 if appId:
1507 addApp = addIntents + " " + str( appId )
andrewonlabb66dfa12014-12-02 15:51:10 -05001508 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001509 addApp = addIntents
andrewonlabb66dfa12014-12-02 15:51:10 -05001510
andrewonlabaedc8332014-12-04 12:43:03 -05001511 if report:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001512 sendCmd = addApp + " > " + str( dirFile ) + " &"
andrewonlabaedc8332014-12-04 12:43:03 -05001513 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001514 sendCmd = addApp + " &"
1515 main.log.info( "Send cmd: " + sendCmd )
andrewonlabb66dfa12014-12-02 15:51:10 -05001516
kelvin-onlabd3b64892015-01-20 13:26:24 -08001517 self.handle.sendline( sendCmd )
andrewonlabb66dfa12014-12-02 15:51:10 -05001518
1519 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001520 main.log.error( self.name + ": EOF exception found" )
1521 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001522 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001523 except Exception:
1524 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001525 main.cleanAndExit()
andrewonlab05e362f2014-10-10 00:40:57 -04001526
kelvin-onlabd3b64892015-01-20 13:26:24 -08001527 def tsharkPcap( self, interface, dirFile ):
kelvin8ec71442015-01-15 16:57:00 -08001528 """
andrewonlab970399c2014-11-07 13:09:32 -05001529 Capture all packet activity and store in specified
1530 directory/file
1531
1532 Required:
1533 * interface: interface to capture
1534 * dir: directory/filename to store pcap
kelvin8ec71442015-01-15 16:57:00 -08001535 """
Jon Hallfebb1c72015-03-05 13:30:09 -08001536 try:
1537 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001538 self.handle.expect( self.prompt )
andrewonlab970399c2014-11-07 13:09:32 -05001539
cameron@onlab.us21106ea2015-07-23 15:32:51 -07001540 self.handle.sendline( "tshark -i " + str( interface ) + " -t e -w " + str( dirFile ) + " &" )
Jon Hall5ec6b1b2015-09-17 18:20:14 -07001541 self.handle.sendline( "\n" )
Jon Hallfebb1c72015-03-05 13:30:09 -08001542 self.handle.expect( "Capturing on" )
Jon Hall5ec6b1b2015-09-17 18:20:14 -07001543 self.handle.sendline( "\n" )
Devin Limc20e79a2017-06-07 10:29:57 -07001544 self.handle.expect( self.prompt )
andrewonlab970399c2014-11-07 13:09:32 -05001545
Jon Hallfebb1c72015-03-05 13:30:09 -08001546 main.log.info( "Tshark started capturing files on " +
1547 str( interface ) + " and saving to directory: " +
1548 str( dirFile ) )
1549 except pexpect.EOF:
1550 main.log.error( self.name + ": EOF exception found" )
1551 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001552 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001553 except Exception:
1554 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001555 main.cleanAndExit()
Shreya Shaha73aaad2014-10-27 18:03:09 -04001556
kelvin-onlabd9e23de2015-08-06 10:34:44 -07001557 def onosTopoCfg( self, onosIp, jsonFile ):
kelvin8ec71442015-01-15 16:57:00 -08001558 """
kelvin-onlabd9e23de2015-08-06 10:34:44 -07001559 Description:
1560 Execute onos-topo-cfg command
1561 Required:
1562 onosIp - IP of the onos node you want to send the json to
1563 jsonFile - File path of the json file
1564 Return:
1565 Returns main.TRUE if the command is successfull; Returns
1566 main.FALSE if there was an error
kelvin8ec71442015-01-15 16:57:00 -08001567 """
shahshreyae6c7cf42014-11-26 16:39:01 -08001568 try:
kelvin8ec71442015-01-15 16:57:00 -08001569 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001570 self.handle.expect( self.prompt )
kelvin-onlabd9e23de2015-08-06 10:34:44 -07001571 cmd = "onos-topo-cfg "
1572 self.handle.sendline( cmd + str( onosIp ) + " " + jsonFile )
1573 handle = self.handle.before
1574 print handle
1575 if "Error" in handle:
1576 main.log.error( self.name + ": " + self.handle.before )
1577 return main.FALSE
1578 else:
Devin Limc20e79a2017-06-07 10:29:57 -07001579 self.handle.expect( self.prompt )
kelvin-onlabd9e23de2015-08-06 10:34:44 -07001580 return main.TRUE
1581
Jon Hallfebb1c72015-03-05 13:30:09 -08001582 except pexpect.EOF:
1583 main.log.error( self.name + ": EOF exception found" )
1584 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001585 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001586 except Exception:
1587 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001588 main.cleanAndExit()
kelvin8ec71442015-01-15 16:57:00 -08001589
jenkins1e99e7b2015-04-02 18:15:39 -07001590 def tsharkGrep( self, grep, directory, interface='eth0', grepOptions='' ):
kelvin8ec71442015-01-15 16:57:00 -08001591 """
andrewonlabba44bcf2014-10-16 16:54:41 -04001592 Required:
kelvin8ec71442015-01-15 16:57:00 -08001593 * grep string
andrewonlabba44bcf2014-10-16 16:54:41 -04001594 * directory to store results
1595 Optional:
1596 * interface - default: eth0
Jon Hall4ba53f02015-07-29 13:07:41 -07001597 * grepOptions - options for grep
andrewonlabba44bcf2014-10-16 16:54:41 -04001598 Description:
1599 Uses tshark command to grep specific group of packets
1600 and stores the results to specified directory.
kelvin8ec71442015-01-15 16:57:00 -08001601 The timestamp is hardcoded to be in epoch
1602 """
Jon Hallfebb1c72015-03-05 13:30:09 -08001603 try:
1604 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001605 self.handle.expect( self.prompt )
Jon Hallfebb1c72015-03-05 13:30:09 -08001606 self.handle.sendline( "" )
jenkins1e99e7b2015-04-02 18:15:39 -07001607 if grepOptions:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07001608 grepStr = "grep " + str( grepOptions )
jenkins1e99e7b2015-04-02 18:15:39 -07001609 else:
1610 grepStr = "grep"
Jon Hall4ba53f02015-07-29 13:07:41 -07001611
cameron@onlab.us21106ea2015-07-23 15:32:51 -07001612 cmd = (
1613 "sudo tshark -i " +
Jon Hallfebb1c72015-03-05 13:30:09 -08001614 str( interface ) +
jenkins1e99e7b2015-04-02 18:15:39 -07001615 " -t e | " +
1616 grepStr + " --line-buffered \"" +
Jeremy Ronquillo82705492017-10-18 14:19:55 -07001617 str( grep ) +
Jon Hallfebb1c72015-03-05 13:30:09 -08001618 "\" >" +
1619 directory +
1620 " &" )
Jeremy Ronquillo82705492017-10-18 14:19:55 -07001621 self.handle.sendline( cmd )
1622 main.log.info( cmd )
Jon Hallfebb1c72015-03-05 13:30:09 -08001623 self.handle.expect( "Capturing on" )
cameron@onlab.us21106ea2015-07-23 15:32:51 -07001624 self.handle.sendline( "\n" )
Devin Limc20e79a2017-06-07 10:29:57 -07001625 self.handle.expect( self.prompt )
Jon Hallfebb1c72015-03-05 13:30:09 -08001626 except pexpect.EOF:
1627 main.log.error( self.name + ": EOF exception found" )
1628 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001629 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001630 except Exception:
1631 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001632 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001633
kelvin-onlabd3b64892015-01-20 13:26:24 -08001634 def tsharkStop( self ):
kelvin8ec71442015-01-15 16:57:00 -08001635 """
andrewonlabba44bcf2014-10-16 16:54:41 -04001636 Removes wireshark files from /tmp and kills all tshark processes
kelvin8ec71442015-01-15 16:57:00 -08001637 """
1638 # Remove all pcap from previous captures
Jon Hallfebb1c72015-03-05 13:30:09 -08001639 try:
1640 self.execute( cmd="sudo rm /tmp/wireshark*" )
1641 self.handle.sendline( "" )
Jon Hallefbd9792015-03-05 16:11:36 -08001642 self.handle.sendline( "sudo kill -9 `ps -ef | grep \"tshark -i\"" +
1643 " | grep -v grep | awk '{print $2}'`" )
Jon Hallfebb1c72015-03-05 13:30:09 -08001644 self.handle.sendline( "" )
1645 main.log.info( "Tshark stopped" )
1646 except pexpect.EOF:
1647 main.log.error( self.name + ": EOF exception found" )
1648 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001649 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001650 except Exception:
1651 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001652 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001653
kelvin8ec71442015-01-15 16:57:00 -08001654 def ptpd( self, args ):
1655 """
andrewonlab0c38a4a2014-10-28 18:35:35 -04001656 Initiate ptp with user-specified args.
1657 Required:
1658 * args: specify string of args after command
1659 'sudo ptpd'
kelvin8ec71442015-01-15 16:57:00 -08001660 """
andrewonlab0c38a4a2014-10-28 18:35:35 -04001661 try:
kelvin8ec71442015-01-15 16:57:00 -08001662 self.handle.sendline( "sudo ptpd " + str( args ) )
1663 i = self.handle.expect( [
andrewonlab0c38a4a2014-10-28 18:35:35 -04001664 "Multiple",
1665 "Error",
Devin Limc20e79a2017-06-07 10:29:57 -07001666 self.prompt ] )
1667 self.handle.expect( self.prompt )
andrewonlabba44bcf2014-10-16 16:54:41 -04001668
andrewonlab0c38a4a2014-10-28 18:35:35 -04001669 if i == 0:
1670 handle = self.handle.before
kelvin8ec71442015-01-15 16:57:00 -08001671 main.log.info( "ptpd returned an error: " +
1672 str( handle ) )
andrewonlab0c38a4a2014-10-28 18:35:35 -04001673 return handle
1674 elif i == 1:
1675 handle = self.handle.before
kelvin8ec71442015-01-15 16:57:00 -08001676 main.log.error( "ptpd returned an error: " +
1677 str( handle ) )
andrewonlab0c38a4a2014-10-28 18:35:35 -04001678 return handle
1679 else:
1680 return main.TRUE
kelvin8ec71442015-01-15 16:57:00 -08001681
andrewonlab0c38a4a2014-10-28 18:35:35 -04001682 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001683 main.log.error( self.name + ": EOF exception found" )
1684 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001685 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001686 except Exception:
1687 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001688 main.cleanAndExit()
andrewonlabba44bcf2014-10-16 16:54:41 -04001689
You Wang54b1d672018-06-11 16:44:13 -07001690 def dumpONOSCmd( self, ONOSIp, CMD, destDir, filename, options="", timeout=60 ):
Flavio Castrod2ffffa2016-04-26 15:56:56 -07001691 """
Pier50f0bc62016-09-07 17:53:40 -07001692 Dump Cmd to a desired directory.
Flavio Castrod2ffffa2016-04-26 15:56:56 -07001693 For debugging purposes, you may want to use
Pier50f0bc62016-09-07 17:53:40 -07001694 this function to capture Cmd at a given point in time.
Flavio Castrod2ffffa2016-04-26 15:56:56 -07001695 Localtime will be attached to the filename
1696
1697 Required:
1698 * ONOSIp: the IP of the target ONOS instance
Pier50f0bc62016-09-07 17:53:40 -07001699 * CMD: the command to dump;
Flavio Castrod2ffffa2016-04-26 15:56:56 -07001700 * destDir: specify directory to copy to.
1701 ex ) /tmp/
Flavio Castrod2ffffa2016-04-26 15:56:56 -07001702 * fileName: Name of the file
You Wang54b1d672018-06-11 16:44:13 -07001703 Optional:
Pier50f0bc62016-09-07 17:53:40 -07001704 * options: Options for ONOS command
You Wang54b1d672018-06-11 16:44:13 -07001705 * timeout: pexpect timeout for running the ONOS command
Flavio Castrod2ffffa2016-04-26 15:56:56 -07001706 """
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00001707
Flavio Castrod2ffffa2016-04-26 15:56:56 -07001708 localtime = time.strftime( '%x %X' )
1709 localtime = localtime.replace( "/", "" )
1710 localtime = localtime.replace( " ", "_" )
1711 localtime = localtime.replace( ":", "" )
1712 if destDir[ -1: ] != "/":
1713 destDir += "/"
Jeremy Ronquillo82705492017-10-18 14:19:55 -07001714 cmd = CMD + " " + options + " > " + str( destDir ) + str( filename ) + localtime
You Wang54b1d672018-06-11 16:44:13 -07001715 return self.onosCli( ONOSIp, cmd, timeout=timeout )
Flavio Castrob7718952016-05-18 08:53:41 -07001716
kelvin-onlabd3b64892015-01-20 13:26:24 -08001717 def cpLogsToDir( self, logToCopy,
Jon Hallefbd9792015-03-05 16:11:36 -08001718 destDir, copyFileName="" ):
kelvin8ec71442015-01-15 16:57:00 -08001719 """
1720 Copies logs to a desired directory.
andrewonlab5d7a8f32014-11-10 13:07:56 -05001721 Current implementation of ONOS deletes its karaf
1722 logs on every iteration. For debugging purposes,
kelvin8ec71442015-01-15 16:57:00 -08001723 you may want to use this function to capture
1724 certain karaf logs. ( or any other logs if needed )
andrewonlab5d7a8f32014-11-10 13:07:56 -05001725 Localtime will be attached to the filename
1726
1727 Required:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001728 * logToCopy: specify directory and log name to
andrewonlab5d7a8f32014-11-10 13:07:56 -05001729 copy.
kelvin8ec71442015-01-15 16:57:00 -08001730 ex ) /opt/onos/log/karaf.log.1
kelvin-onlabd3b64892015-01-20 13:26:24 -08001731 For copying multiple files, leave copyFileName
1732 empty and only specify destDir -
kelvin8ec71442015-01-15 16:57:00 -08001733 ex ) /opt/onos/log/karaf*
kelvin-onlabd3b64892015-01-20 13:26:24 -08001734 * destDir: specify directory to copy to.
kelvin8ec71442015-01-15 16:57:00 -08001735 ex ) /tmp/
1736 Optional:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001737 * copyFileName: If you want to rename the log
1738 file, specify copyFileName. This will not work
andrewonlab5d7a8f32014-11-10 13:07:56 -05001739 with multiple file copying
kelvin8ec71442015-01-15 16:57:00 -08001740 """
andrewonlab5d7a8f32014-11-10 13:07:56 -05001741 try:
Flavio Castro09ab59d2016-05-25 17:01:35 -07001742 localtime = time.strftime( '%H %M' )
kelvin8ec71442015-01-15 16:57:00 -08001743 localtime = localtime.replace( "/", "" )
1744 localtime = localtime.replace( " ", "_" )
1745 localtime = localtime.replace( ":", "" )
kelvin-onlabd3b64892015-01-20 13:26:24 -08001746 if destDir[ -1: ] != "/":
1747 destDir += "/"
andrewonlab5d7a8f32014-11-10 13:07:56 -05001748
kelvin-onlabd3b64892015-01-20 13:26:24 -08001749 if copyFileName:
Jon Hallfebb1c72015-03-05 13:30:09 -08001750 self.handle.sendline( "cp " + str( logToCopy ) + " " +
1751 str( destDir ) + str( copyFileName ) +
1752 localtime )
kelvin8ec71442015-01-15 16:57:00 -08001753 self.handle.expect( "cp" )
Devin Limc20e79a2017-06-07 10:29:57 -07001754 self.handle.expect( self.prompt )
andrewonlab5d7a8f32014-11-10 13:07:56 -05001755 else:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001756 self.handle.sendline( "cp " + str( logToCopy ) +
1757 " " + str( destDir ) )
kelvin8ec71442015-01-15 16:57:00 -08001758 self.handle.expect( "cp" )
Devin Limc20e79a2017-06-07 10:29:57 -07001759 self.handle.expect( self.prompt )
andrewonlab5d7a8f32014-11-10 13:07:56 -05001760
kelvin8ec71442015-01-15 16:57:00 -08001761 return self.handle.before
1762
1763 except pexpect.EOF:
1764 main.log.error( "Copying files failed" )
1765 main.log.error( self.name + ": EOF exception found" )
1766 main.log.error( self.name + ": " + self.handle.before )
Jon Hallfebb1c72015-03-05 13:30:09 -08001767 except Exception:
1768 main.log.exception( "Copying files failed" )
1769
Jeremy Ronquillo82705492017-10-18 14:19:55 -07001770 def checkLogs( self, onosIp, restart=False ):
kelvin8ec71442015-01-15 16:57:00 -08001771 """
Jon Hall94fd0472014-12-08 11:52:42 -08001772 runs onos-check-logs on the given onos node
Jon Hall80daded2015-05-27 16:07:00 -07001773 If restart is True, use the old version of onos-check-logs which
1774 does not print the full stacktrace, but shows the entire log file,
1775 including across restarts
Jon Hall94fd0472014-12-08 11:52:42 -08001776 returns the response
kelvin8ec71442015-01-15 16:57:00 -08001777 """
Jon Hall94fd0472014-12-08 11:52:42 -08001778 try:
kelvin-onlabd3b64892015-01-20 13:26:24 -08001779 cmd = "onos-check-logs " + str( onosIp )
Jon Hall16b72c42015-05-20 10:23:36 -07001780 if restart:
1781 cmd += " old"
kelvin8ec71442015-01-15 16:57:00 -08001782 self.handle.sendline( cmd )
1783 self.handle.expect( cmd )
Devin Limdc78e202017-06-09 18:30:07 -07001784 self.handle.expect( self.prompt + " " )
Jon Hall94fd0472014-12-08 11:52:42 -08001785 response = self.handle.before
1786 return response
1787 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001788 main.log.error( "Lost ssh connection" )
1789 main.log.error( self.name + ": EOF exception found" )
1790 main.log.error( self.name + ": " + self.handle.before )
Jon Hallfebb1c72015-03-05 13:30:09 -08001791 except Exception:
1792 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001793 main.cleanAndExit()
Jon Hall94fd0472014-12-08 11:52:42 -08001794
kelvin-onlabd3b64892015-01-20 13:26:24 -08001795 def onosStatus( self, node="" ):
kelvin8ec71442015-01-15 16:57:00 -08001796 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001797 Calls onos command: 'onos-service [<node-ip>] status'
kelvin8ec71442015-01-15 16:57:00 -08001798 """
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001799 try:
kelvin8ec71442015-01-15 16:57:00 -08001800 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001801 self.handle.expect( self.prompt )
kelvin8ec71442015-01-15 16:57:00 -08001802 self.handle.sendline( "onos-service " + str( node ) +
1803 " status" )
1804 i = self.handle.expect( [
You Wangef1e6572016-03-08 12:53:18 -08001805 "start/running",
You Wang7bd83062016-03-01 11:50:00 -08001806 "Running ...",
You Wangef1e6572016-03-08 12:53:18 -08001807 "stop/waiting",
You Wang7bd83062016-03-01 11:50:00 -08001808 "Not Running ...",
kelvin8ec71442015-01-15 16:57:00 -08001809 pexpect.TIMEOUT ], timeout=120 )
YPZhangfebf7302016-05-24 16:45:56 -07001810 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001811 self.handle.expect( self.prompt )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001812
You Wangef1e6572016-03-08 12:53:18 -08001813 if i == 0 or i == 1:
kelvin8ec71442015-01-15 16:57:00 -08001814 main.log.info( "ONOS is running" )
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001815 return main.TRUE
You Wangef1e6572016-03-08 12:53:18 -08001816 elif i == 2 or i == 3:
kelvin8ec71442015-01-15 16:57:00 -08001817 main.log.info( "ONOS is stopped" )
kelvin8ec71442015-01-15 16:57:00 -08001818 main.log.error( "ONOS service failed to check the status" )
Devin Lim44075962017-08-11 10:56:37 -07001819
1820 main.cleanAndExit()
Hari Krishnaa43d4e92014-12-19 13:22:40 -08001821 except pexpect.EOF:
kelvin8ec71442015-01-15 16:57:00 -08001822 main.log.error( self.name + ": EOF exception found" )
1823 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001824 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001825 except Exception:
1826 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001827 main.cleanAndExit()
Jon Hall21270ac2015-02-16 17:59:55 -08001828
Jon Hall63604932015-02-26 17:09:50 -08001829 def setIpTables( self, ip, port='', action='add', packet_type='',
1830 direction='INPUT', rule='DROP', states=True ):
Jon Hallefbd9792015-03-05 16:11:36 -08001831 """
Jon Hall21270ac2015-02-16 17:59:55 -08001832 Description:
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00001833 add or remove iptables rule to DROP (default) packets from
Jon Hall21270ac2015-02-16 17:59:55 -08001834 specific IP and PORT
1835 Usage:
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00001836 * specify action ('add' or 'remove')
Jon Hall21270ac2015-02-16 17:59:55 -08001837 when removing, pass in the same argument as you would add. It will
1838 delete that specific rule.
1839 * specify the ip to block
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00001840 * specify the destination port to block (defaults to all ports)
1841 * optional packet type to block (default tcp)
1842 * optional iptables rule (default DROP)
1843 * optional direction to block (default 'INPUT')
Jon Hall63604932015-02-26 17:09:50 -08001844 * States boolean toggles adding all supported tcp states to the
1845 firewall rule
Jon Hall21270ac2015-02-16 17:59:55 -08001846 Returns:
1847 main.TRUE on success or
1848 main.FALSE if given invalid input or
1849 main.ERROR if there is an error in response from iptables
1850 WARNING:
1851 * This function uses root privilege iptables command which may result
1852 in unwanted network errors. USE WITH CAUTION
Jon Hallefbd9792015-03-05 16:11:36 -08001853 """
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00001854
Jon Hall21270ac2015-02-16 17:59:55 -08001855 # NOTE*********
1856 # The strict checking methods of this driver function is intentional
1857 # to discourage any misuse or error of iptables, which can cause
1858 # severe network errors
1859 # *************
1860
1861 # NOTE: Sleep needed to give some time for rule to be added and
1862 # registered to the instance. If you are calling this function
1863 # multiple times this sleep will prevent any errors.
1864 # DO NOT REMOVE
Jon Hall63604932015-02-26 17:09:50 -08001865 # time.sleep( 5 )
Jon Hall21270ac2015-02-16 17:59:55 -08001866 try:
1867 # input validation
1868 action_type = action.lower()
1869 rule = rule.upper()
1870 direction = direction.upper()
1871 if action_type != 'add' and action_type != 'remove':
1872 main.log.error( "Invalid action type. Use 'add' or "
1873 "'remove' table rule" )
1874 if rule != 'DROP' and rule != 'ACCEPT' and rule != 'LOG':
1875 # NOTE Currently only supports rules DROP, ACCEPT, and LOG
1876 main.log.error( "Invalid rule. Valid rules are 'DROP' or "
1877 "'ACCEPT' or 'LOG' only." )
1878 if direction != 'INPUT' and direction != 'OUTPUT':
1879 # NOTE currently only supports rules INPUT and OUPTUT
1880 main.log.error( "Invalid rule. Valid directions are"
1881 " 'OUTPUT' or 'INPUT'" )
1882 return main.FALSE
1883 return main.FALSE
1884 return main.FALSE
1885 if action_type == 'add':
1886 # -A is the 'append' action of iptables
1887 actionFlag = '-A'
1888 elif action_type == 'remove':
1889 # -D is the 'delete' rule of iptables
1890 actionFlag = '-D'
1891 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001892 self.handle.expect( self.prompt )
Jon Hall21270ac2015-02-16 17:59:55 -08001893 cmd = "sudo iptables " + actionFlag + " " +\
1894 direction +\
Jon Hall21270ac2015-02-16 17:59:55 -08001895 " -s " + str( ip )
Jeremy Ronquillo82705492017-10-18 14:19:55 -07001896 # " -p " + str( packet_type ) +\
Jon Hall63604932015-02-26 17:09:50 -08001897 if packet_type:
1898 cmd += " -p " + str( packet_type )
Jon Hall21270ac2015-02-16 17:59:55 -08001899 if port:
1900 cmd += " --dport " + str( port )
Jon Hall63604932015-02-26 17:09:50 -08001901 if states:
1902 cmd += " -m state --state="
Jeremy Ronquillo82705492017-10-18 14:19:55 -07001903 # FIXME- Allow user to configure which states to block
Jon Hall63604932015-02-26 17:09:50 -08001904 cmd += "INVALID,ESTABLISHED,NEW,RELATED,UNTRACKED"
Jon Hall21270ac2015-02-16 17:59:55 -08001905 cmd += " -j " + str( rule )
1906
1907 self.handle.sendline( cmd )
Devin Limc20e79a2017-06-07 10:29:57 -07001908 self.handle.expect( self.prompt )
Jon Hall21270ac2015-02-16 17:59:55 -08001909 main.log.warn( self.handle.before )
1910
1911 info_string = "On " + str( self.name )
1912 info_string += " " + str( action_type )
1913 info_string += " iptable rule [ "
1914 info_string += " IP: " + str( ip )
1915 info_string += " Port: " + str( port )
1916 info_string += " Rule: " + str( rule )
1917 info_string += " Direction: " + str( direction ) + " ]"
1918 main.log.info( info_string )
1919 return main.TRUE
1920 except pexpect.TIMEOUT:
1921 main.log.exception( self.name + ": Timeout exception in "
1922 "setIpTables function" )
You Wang141b43b2018-06-26 16:50:18 -07001923 self.handle.send( "\x03" ) # Control-C
1924 self.handle.expect( self.prompt )
Jon Hall21270ac2015-02-16 17:59:55 -08001925 return main.ERROR
1926 except pexpect.EOF:
1927 main.log.error( self.name + ": EOF exception found" )
1928 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001929 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001930 except Exception:
1931 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001932 main.cleanAndExit()
Jon Hall21270ac2015-02-16 17:59:55 -08001933
Jeremy Ronquillo82705492017-10-18 14:19:55 -07001934 def detailed_status( self, log_filename ):
Jon Hallefbd9792015-03-05 16:11:36 -08001935 """
Jon Hall0468b042015-02-19 19:08:21 -08001936 This method is used by STS to check the status of the controller
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00001937 Reports RUNNING, STARTING, STOPPED, FROZEN, ERROR (and reason)
Jon Hallefbd9792015-03-05 16:11:36 -08001938 """
Jon Hall0468b042015-02-19 19:08:21 -08001939 import re
1940 try:
1941 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07001942 self.handle.expect( self.prompt )
Jon Hall0468b042015-02-19 19:08:21 -08001943 self.handle.sendline( "cd " + self.home )
Devin Limc20e79a2017-06-07 10:29:57 -07001944 self.handle.expect( self.prompt )
Jon Hall0468b042015-02-19 19:08:21 -08001945 self.handle.sendline( "service onos status" )
Devin Limc20e79a2017-06-07 10:29:57 -07001946 self.handle.expect( self.prompt )
Jon Hall0468b042015-02-19 19:08:21 -08001947 response = self.handle.before
1948 if re.search( "onos start/running", response ):
1949 # onos start/running, process 10457
1950 return 'RUNNING'
1951 # FIXME: Implement this case
1952 # elif re.search( pattern, response ):
1953 # return 'STARTING'
1954 elif re.search( "onos stop/", response ):
1955 # onos stop/waiting
1956 # FIXME handle this differently?: onos stop/pre-stop
1957 return 'STOPPED'
1958 # FIXME: Implement this case
1959 # elif re.search( pattern, response ):
1960 # return 'FROZEN'
1961 else:
1962 main.log.warn( self.name +
Jon Hallefbd9792015-03-05 16:11:36 -08001963 " WARNING: status received unknown response" )
Jon Hall0468b042015-02-19 19:08:21 -08001964 main.log.warn( response )
1965 return 'ERROR', "Unknown response: %s" % response
1966 except pexpect.TIMEOUT:
1967 main.log.exception( self.name + ": Timeout exception in "
1968 "setIpTables function" )
You Wang141b43b2018-06-26 16:50:18 -07001969 self.handle.send( "\x03" ) # Control-C
1970 self.handle.expect( self.prompt )
Jon Hall0468b042015-02-19 19:08:21 -08001971 return 'ERROR', "Pexpect Timeout"
1972 except pexpect.EOF:
1973 main.log.error( self.name + ": EOF exception found" )
1974 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07001975 main.cleanAndExit()
Jon Hallfebb1c72015-03-05 13:30:09 -08001976 except Exception:
1977 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07001978 main.cleanAndExit()
Jon Hall0468b042015-02-19 19:08:21 -08001979
Jeremy Ronquillo82705492017-10-18 14:19:55 -07001980 def createLinkGraphFile( self, benchIp, ONOSIpList, deviceCount ):
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00001981 '''
Jon Hall4ba53f02015-07-29 13:07:41 -07001982 Create/formats the LinkGraph.cfg file based on arguments
1983 -only creates a linear topology and connects islands
1984 -evenly distributes devices
andrew@onlab.us3b087132015-03-11 15:00:08 -07001985 -must be called by ONOSbench
1986
Jon Hall4ba53f02015-07-29 13:07:41 -07001987 ONOSIpList - list of all of the node IPs to be used
1988
1989 deviceCount - number of switches to be assigned
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00001990 '''
Jeremy Ronquillo82705492017-10-18 14:19:55 -07001991 main.log.info( "Creating link graph configuration file." )
andrew@onlab.us3b087132015-03-11 15:00:08 -07001992 linkGraphPath = self.home + "/tools/package/etc/linkGraph.cfg"
Jon Hall4ba53f02015-07-29 13:07:41 -07001993 tempFile = "/tmp/linkGraph.cfg"
andrew@onlab.us3b087132015-03-11 15:00:08 -07001994
Jeremy Ronquillo82705492017-10-18 14:19:55 -07001995 linkGraph = open( tempFile, 'w+' )
1996 linkGraph.write( "# NullLinkProvider topology description (config file).\n" )
1997 linkGraph.write( "# The NodeId is only added if the destination is another node's device.\n" )
1998 linkGraph.write( "# Bugs: Comments cannot be appended to a line to be read.\n" )
Jon Hall4ba53f02015-07-29 13:07:41 -07001999
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002000 clusterCount = len( ONOSIpList )
Jon Hall4ba53f02015-07-29 13:07:41 -07002001
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002002 if isinstance( deviceCount, int ) or isinstance( deviceCount, str ):
2003 deviceCount = int( deviceCount )
2004 switchList = [ 0 ]*( clusterCount+1 )
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00002005 baselineSwitchCount = deviceCount/clusterCount
Jon Hall4ba53f02015-07-29 13:07:41 -07002006
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002007 for node in range( 1, clusterCount + 1 ):
2008 switchList[ node ] = baselineSwitchCount
andrew@onlab.us3b087132015-03-11 15:00:08 -07002009
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002010 for node in range( 1, ( deviceCount % clusterCount )+1 ):
2011 switchList[ node ] += 1
Jon Hall4ba53f02015-07-29 13:07:41 -07002012
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002013 if isinstance( deviceCount, list ):
2014 main.log.info( "Using provided device distribution" )
2015 switchList = [ 0 ]
andrew@onlab.us3b087132015-03-11 15:00:08 -07002016 for i in deviceCount:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002017 switchList.append( int( i ) )
andrew@onlab.us3b087132015-03-11 15:00:08 -07002018
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002019 tempList = [ '0' ]
2020 tempList.extend( ONOSIpList )
andrew@onlab.us3b087132015-03-11 15:00:08 -07002021 ONOSIpList = tempList
2022
2023 myPort = 6
2024 lastSwitch = 0
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002025 for node in range( 1, clusterCount+1 ):
2026 if switchList[ node ] == 0:
andrew@onlab.us3b087132015-03-11 15:00:08 -07002027 continue
2028
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002029 linkGraph.write( "graph " + ONOSIpList[ node ] + " {\n" )
Jon Hall4ba53f02015-07-29 13:07:41 -07002030
andrew@onlab.us3b087132015-03-11 15:00:08 -07002031 if node > 1:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002032 # connect to last device on previous node
2033 line = ( "\t0:5 -> " + str( lastSwitch ) + ":6:" + lastIp + "\n" ) # ONOSIpList[node-1]
2034 linkGraph.write( line )
Jon Hall4ba53f02015-07-29 13:07:41 -07002035
2036 lastSwitch = 0
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002037 for switch in range( 0, switchList[ node ]-1 ):
andrew@onlab.us3b087132015-03-11 15:00:08 -07002038 line = ""
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002039 line = ( "\t" + str( switch ) + ":" + str( myPort ) )
andrew@onlab.us3b087132015-03-11 15:00:08 -07002040 line += " -- "
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002041 line += ( str( switch+1 ) + ":" + str( myPort-1 ) + "\n" )
2042 linkGraph.write( line )
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00002043 lastSwitch = switch+1
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002044 lastIp = ONOSIpList[ node ]
Jon Hall4ba53f02015-07-29 13:07:41 -07002045
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002046 # lastSwitch += 1
2047 if node < ( clusterCount ):
2048 # connect to first device on the next node
2049 line = ( "\t" + str( lastSwitch ) + ":6 -> 0:5:" + ONOSIpList[ node+1 ] + "\n" )
2050 linkGraph.write( line )
Jon Hall4ba53f02015-07-29 13:07:41 -07002051
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002052 linkGraph.write( "}\n" )
andrew@onlab.us3b087132015-03-11 15:00:08 -07002053 linkGraph.close()
2054
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002055 # SCP
2056 os.system( "scp " + tempFile + " " + self.user_name + "@" + benchIp + ":" + linkGraphPath )
2057 main.log.info( "linkGraph.cfg creation complete" )
andrew@onlab.us3b087132015-03-11 15:00:08 -07002058
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002059 def configNullDev( self, ONOSIpList, deviceCount, numPorts=10 ):
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00002060 '''
Jon Hall4ba53f02015-07-29 13:07:41 -07002061 ONOSIpList = list of Ip addresses of nodes switches will be devided amongst
2062 deviceCount = number of switches to distribute, or list of values to use as custom distribution
cameron@onlab.us75900962015-03-30 13:22:49 -07002063 numPorts = number of ports per device. Defaults to 10 both in this function and in ONOS. Optional arg
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00002064 '''
2065
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002066 main.log.info( "Configuring Null Device Provider" )
2067 clusterCount = len( ONOSIpList )
andrew@onlab.us3b087132015-03-11 15:00:08 -07002068
Jon Hall4ba53f02015-07-29 13:07:41 -07002069 try:
2070
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002071 if isinstance( deviceCount, int ) or isinstance( deviceCount, str ):
2072 main.log.info( "Creating device distribution" )
2073 deviceCount = int( deviceCount )
2074 switchList = [ 0 ]*( clusterCount+1 )
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00002075 baselineSwitchCount = deviceCount/clusterCount
andrew@onlab.us3b087132015-03-11 15:00:08 -07002076
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002077 for node in range( 1, clusterCount + 1 ):
2078 switchList[ node ] = baselineSwitchCount
andrew@onlab.us3b087132015-03-11 15:00:08 -07002079
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002080 for node in range( 1, ( deviceCount % clusterCount )+1 ):
2081 switchList[ node ] += 1
Jon Hall4ba53f02015-07-29 13:07:41 -07002082
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002083 if isinstance( deviceCount, list ):
2084 main.log.info( "Using provided device distribution" )
Jon Hall4ba53f02015-07-29 13:07:41 -07002085
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002086 if len( deviceCount ) == clusterCount:
2087 switchList = [ '0' ]
2088 switchList.extend( deviceCount )
Jon Hall4ba53f02015-07-29 13:07:41 -07002089
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002090 if len( deviceCount ) == ( clusterCount + 1 ):
2091 if deviceCount[ 0 ] == '0' or deviceCount[ 0 ] == 0:
cameron@onlab.us75900962015-03-30 13:22:49 -07002092 switchList = deviceCount
andrew@onlab.us3b087132015-03-11 15:00:08 -07002093
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002094 assert len( switchList ) == ( clusterCount + 1 )
Jon Hall4ba53f02015-07-29 13:07:41 -07002095
cameron@onlab.us75900962015-03-30 13:22:49 -07002096 except AssertionError:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002097 main.log.error( "Bad device/Ip list match" )
cameron@onlab.us75900962015-03-30 13:22:49 -07002098 except TypeError:
2099 main.log.exception( self.name + ": Object not as expected" )
2100 return None
Jon Hall77ba41c2015-04-06 10:25:40 -07002101 except Exception:
cameron@onlab.us75900962015-03-30 13:22:49 -07002102 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07002103 main.cleanAndExit()
cameron@onlab.us75900962015-03-30 13:22:49 -07002104
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002105 ONOSIp = [ 0 ]
2106 ONOSIp.extend( ONOSIpList )
andrew@onlab.us3b087132015-03-11 15:00:08 -07002107
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002108 devicesString = "devConfigs = "
2109 for node in range( 1, len( ONOSIp ) ):
2110 devicesString += ( ONOSIp[ node ] + ":" + str( switchList[ node ] ) )
andrew@onlab.us3b087132015-03-11 15:00:08 -07002111 if node < clusterCount:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002112 devicesString += ( "," )
Jon Hall4ba53f02015-07-29 13:07:41 -07002113
2114 try:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002115 self.handle.sendline( "onos $OC1 cfg set org.onosproject.provider.nil.device.impl.NullDeviceProvider devConfigs " + devicesString )
2116 self.handle.expect( ":~" )
2117 self.handle.sendline( "onos $OC1 cfg set org.onosproject.provider.nil.device.impl.NullDeviceProvider numPorts " + str( numPorts ) )
2118 self.handle.expect( ":~" )
andrew@onlab.us3b087132015-03-11 15:00:08 -07002119
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002120 for i in range( 10 ):
2121 self.handle.sendline( "onos $OC1 cfg get org.onosproject.provider.nil.device.impl.NullDeviceProvider" )
2122 self.handle.expect( ":~" )
cameron@onlab.us75900962015-03-30 13:22:49 -07002123 verification = self.handle.before
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002124 if ( " value=" + str( numPorts ) ) in verification and ( " value=" + devicesString ) in verification:
cameron@onlab.us75900962015-03-30 13:22:49 -07002125 break
2126 else:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002127 time.sleep( 1 )
andrew@onlab.us3b087132015-03-11 15:00:08 -07002128
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002129 assert ( "value=" + str( numPorts ) ) in verification and ( " value=" + devicesString ) in verification
Jon Hall4ba53f02015-07-29 13:07:41 -07002130
cameron@onlab.us75900962015-03-30 13:22:49 -07002131 except AssertionError:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002132 main.log.error( "Incorrect Config settings: " + verification )
Jon Hall77ba41c2015-04-06 10:25:40 -07002133 except Exception:
cameron@onlab.us75900962015-03-30 13:22:49 -07002134 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07002135 main.cleanAndExit()
cameron@onlab.us75900962015-03-30 13:22:49 -07002136
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002137 def configNullLink( self, fileName="/opt/onos/apache-karaf-3.0.3/etc/linkGraph.cfg", eventRate=0 ):
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00002138 '''
Jon Hall4ba53f02015-07-29 13:07:41 -07002139 fileName default is currently the same as the default on ONOS, specify alternate file if
cameron@onlab.us75900962015-03-30 13:22:49 -07002140 you want to use a different topology file than linkGraph.cfg
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00002141 '''
Jon Hall4ba53f02015-07-29 13:07:41 -07002142
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00002143 try:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002144 self.handle.sendline( "onos $OC1 cfg set org.onosproject.provider.nil.link.impl.NullLinkProvider eventRate " + str( eventRate ) )
2145 self.handle.expect( ":~" )
2146 self.handle.sendline( "onos $OC1 cfg set org.onosproject.provider.nil.link.impl.NullLinkProvider cfgFile " + fileName )
2147 self.handle.expect( ":~" )
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00002148
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002149 for i in range( 10 ):
2150 self.handle.sendline( "onos $OC1 cfg get org.onosproject.provider.nil.link.impl.NullLinkProvider" )
2151 self.handle.expect( ":~" )
cameron@onlab.us75900962015-03-30 13:22:49 -07002152 verification = self.handle.before
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002153 if ( " value=" + str( eventRate ) ) in verification and ( " value=" + fileName ) in verification:
cameron@onlab.us75900962015-03-30 13:22:49 -07002154 break
Jon Hall4ba53f02015-07-29 13:07:41 -07002155 else:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002156 time.sleep( 1 )
Jon Hall4ba53f02015-07-29 13:07:41 -07002157
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002158 assert ( "value=" + str( eventRate ) ) in verification and ( " value=" + fileName ) in verification
Jon Hall4ba53f02015-07-29 13:07:41 -07002159
cameron@onlab.us75900962015-03-30 13:22:49 -07002160 except pexpect.EOF:
2161 main.log.error( self.name + ": EOF exception found" )
2162 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07002163 main.cleanAndExit()
cameron@onlab.us75900962015-03-30 13:22:49 -07002164 except AssertionError:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002165 main.log.info( "Settings did not post to ONOS" )
2166 main.log.error( varification )
Jon Hall77ba41c2015-04-06 10:25:40 -07002167 except Exception:
cameron@onlab.us75900962015-03-30 13:22:49 -07002168 main.log.exception( self.name + ": Uncaught exception!" )
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002169 main.log.error( varification )
Devin Lim44075962017-08-11 10:56:37 -07002170 main.cleanAndExit()
andrew@onlab.us3b087132015-03-11 15:00:08 -07002171
kelvin-onlaba4074292015-07-09 15:19:49 -07002172 def getOnosIps( self ):
2173 """
2174 Get all onos IPs stored in
2175 """
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00002176
kelvin-onlaba4074292015-07-09 15:19:49 -07002177 return sorted( self.onosIps.values() )
cameron@onlab.us59d29d92015-05-11 14:31:54 -07002178
Chiyu Chengec63bde2016-11-17 18:11:36 -08002179 def listLog( self, nodeIp ):
2180 """
2181 Get a list of all the karaf log names
2182 """
2183 try:
2184 cmd = "onos-ssh " + nodeIp + " ls -tr /opt/onos/log"
2185 self.handle.sendline( cmd )
2186 self.handle.expect( ":~" )
2187 before = self.handle.before.splitlines()
2188 logNames = []
2189 for word in before:
2190 if 'karaf.log' in word:
2191 logNames.append( word )
2192 return logNames
2193 except pexpect.EOF:
2194 main.log.error( self.name + ": EOF exception found" )
2195 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07002196 main.cleanAndExit()
Chiyu Chengec63bde2016-11-17 18:11:36 -08002197 except pexpect.TIMEOUT:
2198 main.log.error( self.name + ": TIMEOUT exception found" )
2199 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07002200 main.cleanAndExit()
Chiyu Chengec63bde2016-11-17 18:11:36 -08002201 except Exception:
2202 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07002203 main.cleanAndExit()
Chiyu Chengec63bde2016-11-17 18:11:36 -08002204
Pratik Parab3b2ab5a2017-02-14 13:15:14 -08002205 def logReport( self, nodeIp, searchTerms, outputMode="s", startStr=None, endStr=None ):
Jon Hallb4242222016-01-25 17:07:04 -08002206 """
2207 Searches the latest ONOS log file for the given search terms and
2208 prints the total occurances of each term. Returns to combined total of
2209 all occurances.
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002210
Jon Hallb4242222016-01-25 17:07:04 -08002211 Arguments:
2212 * nodeIp - The ip of the ONOS node where the log is located
2213 * searchTerms - A string to grep for or a list of strings to grep
2214 for in the ONOS log. Will print out the number of
2215 occurances for each term.
2216 Optional Arguments:
2217 * outputMode - 's' or 'd'. If 'd' will print the last 5 lines
2218 containing each search term as well as the total
2219 number of occurances of each term. Defaults to 's',
2220 which prints the simple output of just the number
2221 of occurances for each term.
Pratik Parab3b2ab5a2017-02-14 13:15:14 -08002222 * startStr - the start string to be given to stream editor command
2223 as the start point for extraction of data
2224 * endStr - the end string to be given to stream editor command as
2225 the end point for extraction of data
Jon Hallb4242222016-01-25 17:07:04 -08002226 """
2227 try:
2228 main.log.info( " Log Report for {} ".format( nodeIp ).center( 70, '=' ) )
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002229 if isinstance( searchTerms, str ):
2230 searchTerms = [ searchTerms ]
Jon Hallb4242222016-01-25 17:07:04 -08002231 numTerms = len( searchTerms )
2232 outputMode = outputMode.lower()
cameron@onlab.us70dd8c62015-05-14 11:19:39 -07002233
Jon Hallb4242222016-01-25 17:07:04 -08002234 totalHits = 0
2235 logLines = []
2236 for termIndex in range( numTerms ):
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002237 term = searchTerms[ termIndex ]
2238 logLines.append( [ term ] )
Pratik Parab3b2ab5a2017-02-14 13:15:14 -08002239 if startStr and endStr:
2240 cmd = "onos-ssh {} \"sed -n '/{}/,/{}/p' /opt/onos/log/karaf.log | grep {}\"".format( nodeIp,
2241 startStr,
2242 endStr,
2243 term )
2244 else:
2245 cmd = "onos-ssh {} cat /opt/onos/log/karaf.log | grep {}".format( nodeIp,
2246 term )
Jon Hallb4242222016-01-25 17:07:04 -08002247 self.handle.sendline( cmd )
2248 self.handle.expect( ":~" )
2249 before = self.handle.before.splitlines()
2250 count = 0
2251 for line in before:
2252 if term in line and "grep" not in line:
2253 count += 1
2254 if before.index( line ) > ( len( before ) - 7 ):
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002255 logLines[ termIndex ].append( line )
Jon Hallb4242222016-01-25 17:07:04 -08002256 main.log.info( "{}: {}".format( term, count ) )
2257 totalHits += count
2258 if termIndex == numTerms - 1:
2259 print "\n"
2260 if outputMode != "s":
2261 outputString = ""
2262 for term in logLines:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002263 outputString = term[ 0 ] + ": \n"
Jon Hallb4242222016-01-25 17:07:04 -08002264 for line in range( 1, len( term ) ):
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002265 outputString += ( "\t" + term[ line ] + "\n" )
2266 if outputString != ( term[ 0 ] + ": \n" ):
Jon Hallb4242222016-01-25 17:07:04 -08002267 main.log.info( outputString )
2268 main.log.info( "=" * 70 )
2269 return totalHits
2270 except pexpect.EOF:
2271 main.log.error( self.name + ": EOF exception found" )
2272 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07002273 main.cleanAndExit()
Jon Hallb4242222016-01-25 17:07:04 -08002274 except pexpect.TIMEOUT:
2275 main.log.error( self.name + ": TIMEOUT exception found" )
2276 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07002277 main.cleanAndExit()
Jon Hallb4242222016-01-25 17:07:04 -08002278 except Exception:
2279 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07002280 main.cleanAndExit()
kelvin-onlab7a719bb2015-07-08 11:09:51 -07002281
2282 def copyMininetFile( self, fileName, localPath, userName, ip,
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00002283 mnPath='~/mininet/custom/', timeout = 60 ):
kelvin-onlab7a719bb2015-07-08 11:09:51 -07002284 """
2285 Description:
2286 Copy mininet topology file from dependency folder in the test folder
2287 and paste it to the mininet machine's mininet/custom folder
2288 Required:
2289 fileName - Name of the topology file to copy
2290 localPath - File path of the mininet topology file
2291 userName - User name of the mininet machine to send the file to
2292 ip - Ip address of the mininet machine
2293 Optional:
2294 mnPath - of the mininet directory to send the file to
2295 Return:
2296 Return main.TRUE if successfully copied the file otherwise
2297 return main.FALSE
2298 """
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00002299
kelvin-onlab7a719bb2015-07-08 11:09:51 -07002300 try:
2301 cmd = "scp " + localPath + fileName + " " + userName + "@" + \
2302 str( ip ) + ":" + mnPath + fileName
2303
2304 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07002305 self.handle.expect( self.prompt )
kelvin-onlab7a719bb2015-07-08 11:09:51 -07002306
2307 main.log.info( self.name + ": Execute: " + cmd )
2308
2309 self.handle.sendline( cmd )
2310
2311 i = self.handle.expect( [ 'No such file',
2312 "100%",
2313 pexpect.TIMEOUT ] )
2314
2315 if i == 0:
2316 main.log.error( self.name + ": File " + fileName +
2317 " does not exist!" )
2318 return main.FALSE
2319
2320 if i == 1:
2321 main.log.info( self.name + ": File " + fileName +
2322 " has been copied!" )
2323 self.handle.sendline( "" )
Devin Limc20e79a2017-06-07 10:29:57 -07002324 self.handle.expect( self.prompt )
kelvin-onlab7a719bb2015-07-08 11:09:51 -07002325 return main.TRUE
2326
2327 except pexpect.EOF:
2328 main.log.error( self.name + ": EOF exception found" )
2329 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07002330 main.cleanAndExit()
kelvin-onlab7a719bb2015-07-08 11:09:51 -07002331 except pexpect.TIMEOUT:
2332 main.log.error( self.name + ": TIMEOUT exception found" )
2333 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07002334 main.cleanAndExit()
cameron@onlab.us78b89652015-07-08 15:21:03 -07002335
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002336 def jvmSet( self, memory=8 ):
Jon Hall4ba53f02015-07-29 13:07:41 -07002337
cameron@onlab.us78b89652015-07-08 15:21:03 -07002338 import os
2339
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002340 homeDir = os.path.expanduser( '~' )
cameron@onlab.us78b89652015-07-08 15:21:03 -07002341 filename = "/onos/tools/package/bin/onos-service"
2342
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002343 serviceConfig = open( homeDir + filename, 'w+' )
2344 serviceConfig.write( "#!/bin/bash\n " )
2345 serviceConfig.write( "#------------------------------------- \n " )
2346 serviceConfig.write( "# Starts ONOS Apache Karaf container\n " )
2347 serviceConfig.write( "#------------------------------------- \n " )
2348 serviceConfig.write( "#export JAVA_HOME=${JAVA_HOME:-/usr/lib/jvm/java-7-openjdk-amd64/}\n " )
2349 serviceConfig.write( """export JAVA_OPTS="${JAVA_OPTS:--Xms""" + str( memory ) + "G -Xmx" + str( memory ) + """G}" \n """ )
2350 serviceConfig.write( "[ -d $ONOS_HOME ] && cd $ONOS_HOME || ONOS_HOME=$(dirname $0)/..\n" )
2351 serviceConfig.write( """${ONOS_HOME}/apache-karaf-$KARAF_VERSION/bin/karaf "$@" \n """ )
cameron@onlab.us78b89652015-07-08 15:21:03 -07002352 serviceConfig.close()
2353
Jon Hall6c44c0b2016-04-20 15:21:00 -07002354 def createDBFile( self, testData ):
Jon Hall4ba53f02015-07-29 13:07:41 -07002355
cameron@onlab.us78b89652015-07-08 15:21:03 -07002356 filename = main.TEST + "DB"
2357 DBString = ""
Jon Hall4ba53f02015-07-29 13:07:41 -07002358
cameron@onlab.us78b89652015-07-08 15:21:03 -07002359 for item in testData:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002360 if isinstance( item, string ):
cameron@onlab.us78b89652015-07-08 15:21:03 -07002361 item = "'" + item + "'"
Jon Hall6c44c0b2016-04-20 15:21:00 -07002362 if testData.index( item ) < len( testData - 1 ):
cameron@onlab.us78b89652015-07-08 15:21:03 -07002363 item += ","
Jon Hall6c44c0b2016-04-20 15:21:00 -07002364 DBString += str( item )
cameron@onlab.us78b89652015-07-08 15:21:03 -07002365
Jon Hall6c44c0b2016-04-20 15:21:00 -07002366 DBFile = open( filename, "a" )
2367 DBFile.write( DBString )
cameron@onlab.us78b89652015-07-08 15:21:03 -07002368 DBFile.close()
2369
Jon Hall6c44c0b2016-04-20 15:21:00 -07002370 def verifySummary( self, ONOSIp, *deviceCount ):
cameron@onlab.us78b89652015-07-08 15:21:03 -07002371
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002372 self.handle.sendline( "onos " + ONOSIp + " summary" )
Jon Hall6c44c0b2016-04-20 15:21:00 -07002373 self.handle.expect( ":~" )
cameron@onlab.us78b89652015-07-08 15:21:03 -07002374
2375 summaryStr = self.handle.before
2376 print "\nSummary\n==============\n" + summaryStr + "\n\n"
2377
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002378 # passed = "SCC(s)=1" in summaryStr
2379 # if deviceCount:
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00002380 # passed = "devices=" + str(deviceCount) + "," not in summaryStr
cameron@onlab.us78b89652015-07-08 15:21:03 -07002381
GlennRC772363b2015-08-25 13:05:57 -07002382 passed = False
cameron@onlab.us78b89652015-07-08 15:21:03 -07002383 if "SCC(s)=1," in summaryStr:
2384 passed = True
Jon Hall6c44c0b2016-04-20 15:21:00 -07002385 print "Summary is verifed"
Jon Hall4ba53f02015-07-29 13:07:41 -07002386 else:
Jon Hall6c44c0b2016-04-20 15:21:00 -07002387 print "Summary failed"
cameron@onlab.us78b89652015-07-08 15:21:03 -07002388
2389 if deviceCount:
2390 print" ============================="
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002391 checkStr = "devices=" + str( deviceCount[ 0 ] ) + ","
cameron@onlab.us78b89652015-07-08 15:21:03 -07002392 print "Checkstr: " + checkStr
2393 if checkStr not in summaryStr:
2394 passed = False
Jon Hall6c44c0b2016-04-20 15:21:00 -07002395 print "Device count failed"
Jon Hall4ba53f02015-07-29 13:07:41 -07002396 else:
2397 print "device count verified"
cameron@onlab.us78b89652015-07-08 15:21:03 -07002398
2399 return passed
Jon Hall6c44c0b2016-04-20 15:21:00 -07002400
Jon Hall8f6d4622016-05-23 15:27:18 -07002401 def getIpAddr( self, iface=None ):
Jon Hall6c44c0b2016-04-20 15:21:00 -07002402 """
2403 Update self.ip_address with numerical ip address. If multiple IP's are
2404 located on the device, will attempt to use self.nicAddr to choose the
2405 right one. Defaults to 127.0.0.1 if no other address is found or cannot
2406 determine the correct address.
2407
2408 ONLY WORKS WITH IPV4 ADDRESSES
2409 """
2410 try:
Jon Hall8f6d4622016-05-23 15:27:18 -07002411 LOCALHOST = "127.0.0.1"
Jon Hall6c44c0b2016-04-20 15:21:00 -07002412 ipPat = "\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}"
2413 pattern = re.compile( ipPat )
2414 match = re.search( pattern, self.ip_address )
2415 if self.nicAddr:
2416 nicPat = self.nicAddr.replace( ".", "\." ).replace( "\*", r"\d{1,3}" )
2417 nicPat = re.compile( nicPat )
2418 else:
2419 nicPat = None
2420 # IF self.ip_address is an ip address and matches
2421 # self.nicAddr: return self.ip_address
2422 if match:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002423 curIp = match.group( 0 )
Jon Hall6c44c0b2016-04-20 15:21:00 -07002424 if nicPat:
2425 nicMatch = re.search( nicPat, curIp )
2426 if nicMatch:
2427 return self.ip_address
Jon Hall8f6d4622016-05-23 15:27:18 -07002428 # ELSE: IF iface, return ip of interface
2429 cmd = "ifconfig"
2430 ifPat = re.compile( "inet addr:({})".format( ipPat ) )
2431 if iface:
2432 cmd += " " + str( iface )
2433 raw = subprocess.check_output( cmd.split() )
Jon Hall6c44c0b2016-04-20 15:21:00 -07002434 ifPat = re.compile( "inet addr:({})".format( ipPat ) )
2435 ips = re.findall( ifPat, raw )
Jon Hall8f6d4622016-05-23 15:27:18 -07002436 if iface:
2437 if ips:
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002438 ip = ips[ 0 ]
Jon Hall8f6d4622016-05-23 15:27:18 -07002439 self.ip_address = ip
2440 return ip
2441 else:
2442 main.log.error( "Error finding ip, ifconfig output:".format( raw ) )
2443 # ELSE: attempt to get address matching nicPat.
Jon Hall6c44c0b2016-04-20 15:21:00 -07002444 if nicPat:
2445 for ip in ips:
2446 curMatch = re.search( nicPat, ip )
2447 if curMatch:
2448 self.ip_address = ip
2449 return ip
Jon Hall8f6d4622016-05-23 15:27:18 -07002450 else: # If only one non-localhost ip, return that
2451 tmpList = [ ip for ip in ips if ip is not LOCALHOST ]
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002452 if len( tmpList ) == 1:
2453 curIp = tmpList[ 0 ]
Jon Hall6c44c0b2016-04-20 15:21:00 -07002454 self.ip_address = curIp
2455 return curIp
Jon Hall8f6d4622016-05-23 15:27:18 -07002456 # Either no non-localhost IPs, or more than 1
Jon Hallebccd352016-05-20 15:17:17 -07002457 main.log.warn( "getIpAddr failed to find a public IP address" )
Jon Hall8f6d4622016-05-23 15:27:18 -07002458 return LOCALHOST
Jon Halle1790952016-05-23 15:51:46 -07002459 except subprocess.CalledProcessError:
Jon Hall8f6d4622016-05-23 15:27:18 -07002460 main.log.exception( "Error executing ifconfig" )
2461 except IndexError:
2462 main.log.exception( "Error getting IP Address" )
Jon Hall6c44c0b2016-04-20 15:21:00 -07002463 except Exception:
2464 main.log.exception( "Uncaught exception" )
suibin zhang116647a2016-05-06 16:30:09 -07002465
Devin Lim461f0872017-06-05 16:49:33 -07002466 def startBasicONOS( self, nodeList, opSleep=60, onosStartupSleep=30, onosUser="sdn" ):
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00002467 '''
suibin zhang116647a2016-05-06 16:30:09 -07002468 Start onos cluster with defined nodes, but only with drivers app
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00002469 '''
suibin zhang116647a2016-05-06 16:30:09 -07002470 import time
2471
2472 self.createCellFile( self.ip_address,
2473 "temp",
2474 self.ip_address,
2475 "drivers",
Devin Lim461f0872017-06-05 16:49:33 -07002476 nodeList, onosUser )
suibin zhang116647a2016-05-06 16:30:09 -07002477
2478 main.log.info( self.name + ": Apply cell to environment" )
2479 cellResult = self.setCell( "temp" )
2480 verifyResult = self.verifyCell()
2481
2482 main.log.info( self.name + ": Creating ONOS package" )
You Wangd1bcaca2016-10-24 15:23:26 -07002483 packageResult = self.buckBuild( timeout=opSleep )
suibin zhang116647a2016-05-06 16:30:09 -07002484
You Wangc669d212017-01-25 11:09:48 -08002485 main.log.info( self.name + ": Uninstalling ONOS" )
2486 for nd in nodeList:
2487 self.onosUninstall( nodeIp=nd )
2488
suibin zhang116647a2016-05-06 16:30:09 -07002489 main.log.info( self.name + ": Installing ONOS package" )
2490 for nd in nodeList:
You Wangc669d212017-01-25 11:09:48 -08002491 self.onosInstall( node=nd )
2492
2493 main.log.info( self.name + ": Set up ONOS secure SSH" )
2494 for nd in nodeList:
2495 self.onosSecureSSH( node=nd )
suibin zhang116647a2016-05-06 16:30:09 -07002496
2497 main.log.info( self.name + ": Starting ONOS service" )
2498 time.sleep( onosStartupSleep )
2499
2500 onosStatus = True
2501 for nd in nodeList:
Jeremy Ronquillo4d5f1d02017-10-13 20:23:57 +00002502 onosStatus = onosStatus & self.isup( node = nd )
Jeremy Ronquillo82705492017-10-18 14:19:55 -07002503 # print "onosStatus is: " + str( onosStatus )
suibin zhang116647a2016-05-06 16:30:09 -07002504
2505 return main.TRUE if onosStatus else main.FALSE
2506
Devin Lim02075272017-07-10 15:33:21 -07002507 def onosNetCfg( self, controllerIp, path, fileName ):
Jeremy Songster5665f1b2016-06-20 14:38:22 -07002508 """
2509 Push a specified json file to ONOS through the onos-netcfg service
2510
2511 Required:
Devin Lim02075272017-07-10 15:33:21 -07002512 controllerIp - the Ip of the ONOS node in the cluster
Jeremy Songster5665f1b2016-06-20 14:38:22 -07002513 path - the location of the file to be sent
2514 fileName - name of the json file to be sent
2515
2516 Returns main.TRUE on successfully sending json file, and main.FALSE if
2517 there is an error.
2518 """
2519 try:
Devin Lim02075272017-07-10 15:33:21 -07002520 cmd = "onos-netcfg {0} {1}{2}".format( controllerIp, path, fileName )
2521 main.log.info( "Sending: " + cmd )
2522 main.ONOSbench.handle.sendline( cmd )
Jeremy Ronquillo3008aa32017-07-07 15:38:57 -07002523 main.ONOSbench.handle.expect( self.prompt )
Devin Lim02075272017-07-10 15:33:21 -07002524 handle = self.handle.before
Jeremy Ronquillo3008aa32017-07-07 15:38:57 -07002525 if "Error" in handle or "No such file or directory" in handle or "curl: " in handle:
2526 main.log.error( self.name + ": " + handle + self.handle.after )
Devin Lim02075272017-07-10 15:33:21 -07002527 return main.FALSE
Devin Lim752dd7b2017-06-27 14:40:03 -07002528 return main.TRUE
Jeremy Songster5665f1b2016-06-20 14:38:22 -07002529 except pexpect.EOF:
2530 main.log.error( self.name + ": EOF exception found" )
2531 main.log.error( self.name + ": " + self.handle.before )
Devin Lim44075962017-08-11 10:56:37 -07002532 main.cleanAndExit()
Jeremy Songster5665f1b2016-06-20 14:38:22 -07002533 except Exception:
2534 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -07002535 main.cleanAndExit()
Devin Lim3ebd5e72017-11-14 10:38:00 -08002536
2537 def formCluster( self, onosIPs ):
2538 """
2539 From ONOS cluster for IP addresses in onosIPs list
2540 """
2541 try:
2542 onosIPs = " ".join( onosIPs )
2543 command = "onos-form-cluster {}".format( onosIPs )
2544 main.log.info( "Sending: " + command )
2545 self.handle.sendline( "" )
2546 self.handle.expect( self.prompt )
2547 self.handle.sendline( command )
2548 self.handle.expect( self.prompt )
2549 handle = self.handle.before
2550 main.log.debug( handle )
2551 assert handle is not None, "Error in sendline"
2552 assert "Command not found:" not in handle, handle
2553 assert "Error" not in handle, handle
2554 assert "Exception:" not in handle, handle
2555 assert "curl:" not in handle, handle
2556 return main.TRUE
2557 except AssertionError:
2558 main.log.exception( "{} Error in onos-form-cluster output:".format( self.name ) )
2559 return main.FALSE
2560 except TypeError:
2561 main.log.exception( self.name + ": Object not as expected" )
2562 return main.FALSE
2563 except pexpect.EOF:
2564 main.log.error( self.name + ": EOF exception found" )
2565 main.log.error( self.name + ": " + self.handle.before )
2566 main.cleanAndExit()
2567 except Exception:
2568 main.log.exception( self.name + ": Uncaught exception!" )
2569 main.cleanAndExit()
Jon Hall7ce46ea2018-02-05 12:20:59 -08002570
2571 def backupData( self, location ):
2572 """
2573 Backs up ONOS data and logs to a given location. Returns main.FALSE
2574 if there is an error executing the command, and main.TRUE otherwise.
2575 required arguments:
2576 loaction - The file path to save the backup to
2577 """
2578 try:
2579 cmd = "/opt/onos/bin/onos-backup " + str( location )
2580 self.handle.sendline( cmd )
2581 self.handle.expect( self.prompt )
2582 handle = self.handle.before
2583 main.log.debug( handle )
2584 assert handle is not None, "Error in sendline"
2585 assert "Command not found:" not in handle, handle
2586 assert "Error" not in handle, handle
2587 assert "Exception:" not in handle, handle
2588 return main.TRUE
2589 except AssertionError:
2590 main.log.exception( "{} Error in onos-backup output:".format( self.name ) )
2591 return main.FALSE
2592 except TypeError:
2593 main.log.exception( self.name + ": Object not as expected" )
2594 return main.FALSE
2595 except pexpect.EOF:
2596 main.log.error( self.name + ": EOF exception found" )
2597 main.log.error( self.name + ": " + self.handle.before )
2598 main.cleanAndExit()
2599 except Exception:
2600 main.log.exception( self.name + ": Uncaught exception!" )
2601 main.cleanAndExit()
2602
2603 def restoreData( self, location ):
2604 """
2605 Restores ONOS data and logs from a given location. Returns main.FALSE
2606 if there is an error executing the command, and main.TRUE otherwise.
2607 required arguments:
2608 loaction - The file path of a backup file
2609 """
2610 try:
2611 cmd = "/opt/onos/bin/onos-restore " + str( location )
2612 self.handle.sendline( cmd )
2613 self.handle.expect( self.prompt )
2614 handle = self.handle.before
2615 main.log.debug( handle )
2616 assert handle is not None, "Error in sendline"
2617 assert "Command not found:" not in handle, handle
2618 assert "Error" not in handle, handle
2619 assert "Exception:" not in handle, handle
2620 return main.TRUE
2621 except AssertionError:
2622 main.log.exception( "{} Error in onos-restore output:".format( self.name ) )
2623 return main.FALSE
2624 except TypeError:
2625 main.log.exception( self.name + ": Object not as expected" )
2626 return main.FALSE
2627 except pexpect.EOF:
2628 main.log.error( self.name + ": EOF exception found" )
2629 main.log.error( self.name + ": " + self.handle.before )
2630 main.cleanAndExit()
2631 except Exception:
2632 main.log.exception( self.name + ": Uncaught exception!" )
2633 main.cleanAndExit()
You Wang5df1c6d2018-04-06 18:02:02 -07002634
You Wang5ac7db72018-07-17 11:17:52 -07002635 def onosDiagnostics( self, onosIPs, dstDir, suffix, timeout=300 ):
You Wang5df1c6d2018-04-06 18:02:02 -07002636 """
2637 Run onos-diagnostics with given ONOS instance IPs and save output to dstDir
2638 with suffix specified E.g. onos-diags-suffix.tar.gz
Jon Hall0e240372018-05-02 11:21:57 -07002639 required arguments:
You Wang5df1c6d2018-04-06 18:02:02 -07002640 onosIPs - list of ONOS IPs for collecting diags
2641 dstDir - diags file will be saved under the directory specified
2642 suffix - diags file will be named with the suffix specified
2643 returns:
2644 main.FALSE if there's an error executing the command, and main.TRUE otherwise
2645 """
2646 try:
2647 cmd = "onos-diagnostics"
2648 assert isinstance( onosIPs, list )
2649 for ip in onosIPs:
2650 cmd += " " + str( ip )
2651 self.handle.sendline( cmd )
You Wang5ac7db72018-07-17 11:17:52 -07002652 self.handle.expect( self.prompt, timeout=timeout )
You Wang5df1c6d2018-04-06 18:02:02 -07002653 handle = self.handle.before
2654 main.log.debug( handle )
2655 assert handle is not None, "Error in sendline"
2656 assert "Command not found:" not in handle, handle
2657 assert "Exception:" not in handle, handle
2658 # Rename and move diags file to dstDir from /tmp
2659 if dstDir[ -1: ] != "/":
2660 dstDir += "/"
2661 self.handle.sendline( "mv /tmp/onos-diags.tar.gz " + str( dstDir ) + "onos-diags" + str( suffix ) + ".tar.gz" )
2662 self.handle.expect( self.prompt )
2663 handle = self.handle.before
2664 main.log.debug( handle )
2665 assert handle is not None, "Error in sendline"
2666 assert "No such file or directory" not in handle, handle
2667 return main.TRUE
2668 except AssertionError:
2669 main.log.exception( "{} Error in onos-diagnostics output:".format( self.name ) )
2670 return main.FALSE
2671 except TypeError:
2672 main.log.exception( self.name + ": Object not as expected" )
2673 return main.FALSE
You Wang223faa32018-06-21 10:45:47 -07002674 except pexpect.TIMEOUT:
2675 main.log.exception( self.name + ": TIMEOUT exception found in onosDiagnostics" )
2676 main.log.error( self.name + ": " + self.handle.before )
You Wang141b43b2018-06-26 16:50:18 -07002677 self.handle.send( "\x03" ) # Control-C
2678 self.handle.expect( self.prompt )
You Wang223faa32018-06-21 10:45:47 -07002679 return main.FALSE
You Wang5df1c6d2018-04-06 18:02:02 -07002680 except pexpect.EOF:
2681 main.log.error( self.name + ": EOF exception found" )
2682 main.log.error( self.name + ": " + self.handle.before )
2683 main.cleanAndExit()
2684 except Exception:
2685 main.log.exception( self.name + ": Uncaught exception!" )
2686 main.cleanAndExit()
Jon Hall0e240372018-05-02 11:21:57 -07002687
2688 def onosPower( self, onosIP, toggle, userName=None ):
2689 """
2690 Run onos-power script to tell the cell warden to simulate a power faulure
2691 for the given container.
2692 required :
2693 onosIP - ONOS node IP
2694 toggle - either "off" or "on", used to indicate whether
2695 the node should be powered off or on
2696 returns:
2697 main.FALSE if there's an error executing the command, and main.TRUE otherwise
2698 """
2699 try:
2700 cmd = "onos-power {} {}".format( onosIP, toggle )
2701 if userName:
2702 cmd += " {}".format( userName )
2703 self.handle.sendline( cmd )
2704 self.handle.expect( self.prompt )
2705 handle = self.handle.before
2706 main.log.debug( handle )
2707 assert handle is not None, "Error in sendline"
2708 assert "Command not found:" not in handle, handle
2709 assert "Exception:" not in handle, handle
2710 assert "usage:" not in handle, handle
2711 return main.TRUE
2712 except AssertionError:
2713 main.log.exception( "{} Error in onos-power output:".format( self.name ) )
2714 return main.FALSE
2715 except TypeError:
2716 main.log.exception( self.name + ": Object not as expected" )
2717 return main.FALSE
2718 except pexpect.EOF:
2719 main.log.error( self.name + ": EOF exception found" )
2720 main.log.error( self.name + ": " + self.handle.before )
2721 main.cleanAndExit()
2722 except Exception:
2723 main.log.exception( self.name + ": Uncaught exception!" )
2724 main.cleanAndExit()
You Wangf9d95be2018-08-01 14:35:37 -07002725
2726 def atomixKill( self, nodeIp ):
2727 """
2728 Calls the command: 'atomix-kill [<node-ip>]'
2729 Kills the Atomix instance running on the specified node
2730 """
2731 try:
2732 self.handle.sendline( "" )
2733 self.handle.expect( self.prompt )
2734 self.handle.sendline( "atomix-kill " + str( nodeIp ) )
2735 i = self.handle.expect( [
2736 self.prompt,
2737 "No\sroute\sto\shost",
2738 "password:",
2739 pexpect.TIMEOUT ], timeout=60 )
2740
2741 if i == 0:
2742 main.log.info( "Atomix instance " + str( nodeIp ) + " was killed" )
2743 return main.TRUE
2744 elif i == 1:
2745 main.log.info( "No route to host" )
2746 return main.FALSE
2747 elif i == 2:
2748 main.log.info( "Passwordless login for host: " + str( nodeIp ) + " not configured" )
2749 return main.FALSE
2750 else:
2751 main.log.info( "Atomix instance was not killed" )
2752 return main.FALSE
2753
2754 except pexpect.EOF:
2755 main.log.error( self.name + ": EOF exception found" )
2756 main.log.error( self.name + ": " + self.handle.before )
2757 main.cleanAndExit()
2758 except Exception:
2759 main.log.exception( self.name + ": Uncaught exception!" )
2760 main.cleanAndExit()
2761
2762 def atomixUninstall( self, nodeIp="" ):
2763 """
2764 Calls the command: 'atomix-uninstall'
2765 Uninstalls Atomix from the designated node, stopping if needed
2766 """
2767 try:
2768 self.handle.sendline( "" )
2769 self.handle.expect( self.prompt, timeout=180 )
2770 self.handle.sendline( "atomix-uninstall " + str( nodeIp ) )
2771 self.handle.expect( self.prompt, timeout=180 )
2772 main.log.info( "Atomix " + nodeIp + " was uninstalled" )
2773 # onos-uninstall command does not return any text
2774 return main.TRUE
2775 except pexpect.TIMEOUT:
2776 main.log.exception( self.name + ": Timeout in atomixUninstall" )
2777 self.handle.send( "\x03" ) # Control-C
2778 self.handle.expect( self.prompt )
2779 return main.FALSE
2780 except pexpect.EOF:
2781 main.log.error( self.name + ": EOF exception found" )
2782 main.log.error( self.name + ": " + self.handle.before )
2783 main.cleanAndExit()
2784 except Exception:
2785 main.log.exception( self.name + ": Uncaught exception!" )
2786 main.cleanAndExit()
2787
2788 def atomixInstall( self, options="", node="" ):
2789 """
2790 Installs Atomix on the designated nodes.
2791 Returns: main.TRUE on success and main.FALSE on failure
2792 """
2793 try:
2794 if options:
2795 self.handle.sendline( "atomix-install " + options + " " + node )
2796 else:
2797 self.handle.sendline( "atomix-install " + node )
2798 self.handle.expect( "atomix-install " )
2799 i = self.handle.expect( [ "Network\sis\sunreachable",
2800 "is already installed",
2801 "saved",
2802 self.prompt,
2803 pexpect.TIMEOUT ], timeout=180 )
2804 if i == 0:
2805 # can't reach ONOS node
2806 main.log.warn( "Network is unreachable" )
2807 self.handle.expect( self.prompt )
2808 return main.FALSE
2809 elif i == 1:
2810 # same bits are already on Atomix node
2811 main.log.info( "Atomix is already installed on " + node )
2812 self.handle.expect( self.prompt )
2813 return main.TRUE
2814 elif i == 2 or i == 3:
2815 main.log.info( "Atomix was installed on " + node )
2816 self.handle.expect( self.prompt )
2817 return main.TRUE
2818 elif i == 4:
2819 # timeout
2820 main.log.info( "Installation of Atomix on " + node + " timed out" )
2821 self.handle.expect( self.prompt )
2822 main.log.warn( self.handle.before )
2823 self.handle.send( "\x03" ) # Control-C
2824 self.handle.expect( self.prompt )
2825 return main.FALSE
2826 except pexpect.EOF:
2827 main.log.error( self.name + ": EOF exception found" )
2828 main.log.error( self.name + ": " + self.handle.before )
2829 main.cleanAndExit()
2830 except Exception:
2831 main.log.exception( self.name + ": Uncaught exception!" )
2832 main.cleanAndExit()