blob: 5a0b3d5dc504aa6bd6f0289c68dc552983a09c03 [file] [log] [blame]
adminbae64d82013-08-01 10:50:15 -07001#!/usr/bin/env python
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -07002"""
adminbae64d82013-08-01 10:50:15 -07003Created on 20-Dec-2012
Jeremy Ronquillob27ce4c2017-07-17 12:41:28 -07004Copyright 2012 Open Networking Foundation
Jon Hall5f15fef2015-07-17 14:22:14 -07005
Jeremy Songsterae01bba2016-07-11 15:39:17 -07006Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>,
7the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>,
8or the System Testing Guide page at <https://wiki.onosproject.org/x/WYQg>
adminbae64d82013-08-01 10:50:15 -07009
10 TestON is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License as published by
12 the Free Software Foundation, either version 2 of the License, or
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -070013 ( at your option ) any later version.
adminbae64d82013-08-01 10:50:15 -070014
15 TestON is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License for more details.
19
20 You should have received a copy of the GNU General Public License
Jon Hall4ba53f02015-07-29 13:07:41 -070021 along with TestON. If not, see <http://www.gnu.org/licenses/>.
adminbae64d82013-08-01 10:50:15 -070022
23
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -070024"""
adminbae64d82013-08-01 10:50:15 -070025"""
26cli will provide the CLI shell for teston framework.
27
28A simple command-line interface for TestON.
29
30The TestON CLI provides a simple console which
31makes it easy to launch the test. For example, the command run will execute the test.
32
33teston> run test DpctlTest
34Several useful commands are provided.
35"""
adminbae64d82013-08-01 10:50:15 -070036from subprocess import call
37from cmd import Cmd
38from os import isatty
39import sys
40import re
41import os
42import time
43import threading
44import __builtin__
45import pprint
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -070046dump = pprint.PrettyPrinter( indent=4 )
adminbae64d82013-08-01 10:50:15 -070047__builtin__.testthread = False
48introduction = "TestON is the testing framework \nDeveloped by Paxterra Solutions (www.paxterrasolutions.com)"
Jon Hall0bde9ba2015-03-19 11:32:57 -070049__builtin__.COLORS = False
adminbae64d82013-08-01 10:50:15 -070050
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -070051path = re.sub( "/bin$", "", sys.path[ 0 ] )
Jon Hall1dd5a0a2015-07-08 10:49:26 -070052sys.path.insert( 1, path )
Jon Hall0bde9ba2015-03-19 11:32:57 -070053from core.teston import *
adminbae64d82013-08-01 10:50:15 -070054
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -070055
56class CLI( threading.Thread, Cmd, object ):
57
adminbae64d82013-08-01 10:50:15 -070058 "command-line interface to execute the test."
59
60 prompt = 'teston> '
61
62 def __init__( self, teston, stdin=sys.stdin ):
63 self.teston = teston
Jon Hall0bde9ba2015-03-19 11:32:57 -070064
adminbae64d82013-08-01 10:50:15 -070065 self._mainevent = threading.Event()
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -070066 threading.Thread.__init__( self )
adminbae64d82013-08-01 10:50:15 -070067 self.main_stop = False
68 self.locals = { 'test': teston }
69 self.stdin = stdin
70 Cmd.__init__( self )
71 self.pause = False
72 self.stop = False
73 __builtin__.cli = self
74
75 def emptyline( self ):
76 "Don't repeat last command when you hit return."
77 pass
78
79 helpStr = (
80 " teston help"
81 )
82
83 def do_help( self, line ):
84 "Describe available CLI commands."
85 Cmd.do_help( self, line )
86 if line is '':
87 output( self.helpStr )
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -070088
89 def do_run( self, args ):
90 """
adminbae64d82013-08-01 10:50:15 -070091 run command will execute the test with following optional command line arguments
92 logdir <directory to store logs in>
93 testcases <list of testcases separated by comma or range of testcases separated by hypen>
94 mail <mail-id or list of mail-ids seperated by comma>
95 example 1, to execute the examples specified in the ~/examples diretory.
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -070096 """
Jon Hall1306a562015-09-04 11:21:24 -070097 try:
98 args = args.split()
99 options = {}
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700100 options = self.parseArgs( args, options )
101 options = dictToObj( options )
Jon Hall1306a562015-09-04 11:21:24 -0700102 if not testthread:
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700103 test = TestThread( options )
Jon Hall1306a562015-09-04 11:21:24 -0700104 test.start()
105 while test.isAlive():
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700106 test.join( 1 )
Jon Hall1306a562015-09-04 11:21:24 -0700107 else:
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700108 print main.TEST + " test execution paused, please resume that before executing to another test"
Jon Hall1306a562015-09-04 11:21:24 -0700109 except KeyboardInterrupt, SystemExit:
110 print "Interrupt called, Exiting."
111 test._Thread__stop()
112 main.cleanup()
113 main.exit()
Jon Hall4ba53f02015-07-29 13:07:41 -0700114
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700115 def do_resume( self, line ):
116 """
adminbae64d82013-08-01 10:50:15 -0700117 resume command will continue the execution of paused test.
118 teston>resume
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700119 [ 2013-01-07 23:03:44.640723 ] [ PoxTest ] [ STEP ] 1.1: Checking the host reachability using pingHost
adminbae64d82013-08-01 10:50:15 -0700120 2013-01-07 23:03:44,858 - PoxTest - INFO - Expected Prompt Found
121 ....
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700122 """
adminbae64d82013-08-01 10:50:15 -0700123 if testthread:
124 testthread.play()
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700125 else:
adminbae64d82013-08-01 10:50:15 -0700126 print "There is no test to resume"
Jon Hall4ba53f02015-07-29 13:07:41 -0700127
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700128 def do_nextstep( self, line ):
129 """
Jon Hall4ba53f02015-07-29 13:07:41 -0700130 nextstep will execute the next-step of the paused test and
adminbae64d82013-08-01 10:50:15 -0700131 it will pause the test after finishing of step.
Jon Hall4ba53f02015-07-29 13:07:41 -0700132
adminbae64d82013-08-01 10:50:15 -0700133 teston> nextstep
134 Will pause the test's execution, after completion of this step.....
Jon Hall4ba53f02015-07-29 13:07:41 -0700135
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700136 teston> [ 2013-01-07 21:24:26.286601 ] [ PoxTest ] [ STEP ] 1.8: Checking the host reachability using pingHost
adminbae64d82013-08-01 10:50:15 -0700137 2013-01-07 21:24:26,455 - PoxTest - INFO - Expected Prompt Found
138 .....
139 teston>
Jon Hall4ba53f02015-07-29 13:07:41 -0700140
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700141 """
adminbae64d82013-08-01 10:50:15 -0700142 if testthread:
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700143 main.log.info( "Executing the nextstep, Will pause test execution, after completion of the step" )
adminbae64d82013-08-01 10:50:15 -0700144 testthread.play()
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700145 time.sleep( .1 )
adminbae64d82013-08-01 10:50:15 -0700146 testthread.pause()
147 else:
148 print "There is no paused test "
Jon Hall4ba53f02015-07-29 13:07:41 -0700149
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700150 def do_dumpvar( self, line ):
151 """
adminbae64d82013-08-01 10:50:15 -0700152 dumpvar will print all the test data in raw format.
Jon Hall4ba53f02015-07-29 13:07:41 -0700153 usgae :
adminbae64d82013-08-01 10:50:15 -0700154 teston>dumpvar main
155 Here 'main' will be the test object.
Jon Hall4ba53f02015-07-29 13:07:41 -0700156
157 teston>dumpvar params
adminbae64d82013-08-01 10:50:15 -0700158 here 'params' will be the parameters specified in the params file.
Jon Hall4ba53f02015-07-29 13:07:41 -0700159
adminbae64d82013-08-01 10:50:15 -0700160 teston>dumpvar topology
161 here 'topology' will be topology specification of the test specified in topo file.
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700162 """
adminbae64d82013-08-01 10:50:15 -0700163 if testthread:
164 if line == "main":
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700165 dump.pprint( vars( main ) )
166 else:
167 try:
168 dump.pprint( vars( main )[ line ] )
Jon Hall1306a562015-09-04 11:21:24 -0700169 except KeyError as e:
adminbae64d82013-08-01 10:50:15 -0700170 print e
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700171 else:
adminbae64d82013-08-01 10:50:15 -0700172 print "There is no paused test "
Jon Hall4ba53f02015-07-29 13:07:41 -0700173
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700174 def do_currentcase( self, line ):
175 """
adminbae64d82013-08-01 10:50:15 -0700176 currentcase will return the current case in the test execution.
Jon Hall4ba53f02015-07-29 13:07:41 -0700177
adminbae64d82013-08-01 10:50:15 -0700178 teston>currentcase
179 Currently executing test case is: 2
Jon Hall4ba53f02015-07-29 13:07:41 -0700180
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700181 """
adminbae64d82013-08-01 10:50:15 -0700182 if testthread:
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700183 print "Currently executing test case is: " + str( main.CurrentTestCaseNumber )
184 else:
adminbae64d82013-08-01 10:50:15 -0700185 print "There is no paused test "
Jon Hall4ba53f02015-07-29 13:07:41 -0700186
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700187 def do_currentstep( self, line ):
188 """
adminbae64d82013-08-01 10:50:15 -0700189 currentstep will return the current step in the test execution.
Jon Hall4ba53f02015-07-29 13:07:41 -0700190
adminbae64d82013-08-01 10:50:15 -0700191 teston>currentstep
192 Currently executing test step is: 2.3
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700193 """
adminbae64d82013-08-01 10:50:15 -0700194 if testthread:
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700195 print "Currently executing test step is: " + str( main.CurrentTestCaseNumber ) + '.' + str( main.stepCount )
196 else:
adminbae64d82013-08-01 10:50:15 -0700197 print "There is no paused test "
Jon Hall4ba53f02015-07-29 13:07:41 -0700198
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700199 def do_stop( self, line ):
200 """
adminbae64d82013-08-01 10:50:15 -0700201 Will stop the paused test, if any !
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700202 """
adminbae64d82013-08-01 10:50:15 -0700203 if testthread:
204 testthread.stop()
Jon Hall4ba53f02015-07-29 13:07:41 -0700205
adminbae64d82013-08-01 10:50:15 -0700206 return 'exited by user command'
Jon Hall4ba53f02015-07-29 13:07:41 -0700207
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700208 def do_gettest( self, line ):
209 """
adminbae64d82013-08-01 10:50:15 -0700210 gettest will return the test name which is under execution or recently executed.
Jon Hall4ba53f02015-07-29 13:07:41 -0700211
adminbae64d82013-08-01 10:50:15 -0700212 Test under execution:
Jon Hall4ba53f02015-07-29 13:07:41 -0700213 teston>gettest
adminbae64d82013-08-01 10:50:15 -0700214 Currently executing Test is: PoxTest
Jon Hall4ba53f02015-07-29 13:07:41 -0700215
adminbae64d82013-08-01 10:50:15 -0700216 Test recently executed:
217 Recently executed test is: MininetTest
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700218 """
219 try:
220 if testthread:
221 print "Currently executing Test is: " + main.TEST
222 else:
223 print "Recently executed test is: " + main.TEST
Jon Hall4ba53f02015-07-29 13:07:41 -0700224
adminbae64d82013-08-01 10:50:15 -0700225 except NameError:
226 print "There is no previously executed Test"
Jon Hall4ba53f02015-07-29 13:07:41 -0700227
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700228 def do_showlog( self, line ):
229 """
adminbae64d82013-08-01 10:50:15 -0700230 showlog will show the test's Log
231 teston>showlog
232 Last executed test's log is : //home/openflow/TestON/logs/PoxTest_07_Jan_2013_21_42_11/PoxTest_07_Jan_2013_21_42_11.log
233 .....
234 teston>showlog
235 Currently executing Test's log is: /home/openflow/TestON/logs/PoxTest_07_Jan_2013_21_46_58/PoxTest_07_Jan_2013_21_46_58.log
236 .....
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700237 """
238 try:
239 if testthread:
240 print "Currently executing Test's log is: " + main.LogFileName
Jon Hall4ba53f02015-07-29 13:07:41 -0700241
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700242 else:
243 print "Last executed test's log is : " + main.LogFileName
Jon Hall4ba53f02015-07-29 13:07:41 -0700244
adminbae64d82013-08-01 10:50:15 -0700245 logFile = main.LogFileName
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700246 logFileHandler = open( logFile, 'r' )
247 for msg in logFileHandler.readlines():
adminbae64d82013-08-01 10:50:15 -0700248 print msg,
Jon Hall4ba53f02015-07-29 13:07:41 -0700249
adminbae64d82013-08-01 10:50:15 -0700250 logFileHandler.close()
Jon Hall4ba53f02015-07-29 13:07:41 -0700251
adminbae64d82013-08-01 10:50:15 -0700252 except NameError:
253 print "There is no previously executed Test"
Jon Hall4ba53f02015-07-29 13:07:41 -0700254
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700255 def parseArgs( self, args, options ):
256 """
adminbae64d82013-08-01 10:50:15 -0700257 This will parse the command line arguments.
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700258 """
259 options = self.initOptions( options )
260 try:
YPZhang16f6e562016-07-12 15:50:31 -0700261 index = 0
262 while index < len( args ):
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700263 option = args[ index ]
264 if index > 0:
265 if re.match( "--params", option, flags=0 ):
YPZhang1c89e762016-06-29 10:43:58 -0700266 # check if there is a params
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700267 options[ 'params' ].append( args[ index + 1 ] )
268 elif re.match( "logdir|mail|example|testdir|testcases|onoscell", option, flags=0 ):
269 options[ option ] = args[ index + 1 ]
270 options = self.testcasesInRange( index + 1, option, args, options )
YPZhang16f6e562016-07-12 15:50:31 -0700271 index += 2
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700272 else:
273 options[ 'testname' ] = option
YPZhang16f6e562016-07-12 15:50:31 -0700274 index += 1
Jon Hall1306a562015-09-04 11:21:24 -0700275 except IndexError as e:
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700276 print ( e )
YPZhang16f6e562016-07-12 15:50:31 -0700277 main.cleanup()
278 main.exit()
Jon Hall4ba53f02015-07-29 13:07:41 -0700279
adminbae64d82013-08-01 10:50:15 -0700280 return options
Jon Hall4ba53f02015-07-29 13:07:41 -0700281
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700282 def initOptions( self, options ):
283 """
adminbae64d82013-08-01 10:50:15 -0700284 This will initialize the commandline options.
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700285 """
286 options[ 'logdir' ] = None
287 options[ 'mail' ] = None
288 options[ 'example' ] = None
289 options[ 'testdir' ] = None
290 options[ 'testcases' ] = None
291 options[ 'onoscell' ] = None
YPZhang1c89e762016-06-29 10:43:58 -0700292 # init params as a empty list
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700293 options[ 'params' ] = []
Jon Hall4ba53f02015-07-29 13:07:41 -0700294 return options
295
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700296 def testcasesInRange( self, index, option, args, options ):
297 """
298 This method will handle testcases list,specified in range [ 1-10 ].
299 """
300 if re.match( "testcases", option, 1 ):
adminbae64d82013-08-01 10:50:15 -0700301 testcases = []
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700302 args[ index ] = re.sub( "\[|\]", "", args[ index ], 0 )
303 m = re.match( "(\d+)\-(\d+)", args[ index ], flags=0 )
adminbae64d82013-08-01 10:50:15 -0700304 if m:
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700305 start_case = eval( m.group( 1 ) )
306 end_case = eval( m.group( 2 ) )
307 if ( start_case <= end_case ):
adminbae64d82013-08-01 10:50:15 -0700308 i = start_case
309 while i <= end_case:
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700310 testcases.append( i )
311 i = i + 1
312 else:
adminbae64d82013-08-01 10:50:15 -0700313 print "Please specify testcases properly like 1-5"
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700314 else:
315 options[ option ] = args[ index ]
adminbae64d82013-08-01 10:50:15 -0700316 return options
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700317 options[ option ] = str( testcases )
Jon Hall4ba53f02015-07-29 13:07:41 -0700318
adminbae64d82013-08-01 10:50:15 -0700319 return options
Jon Hall4ba53f02015-07-29 13:07:41 -0700320
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700321 def cmdloop( self, intro=introduction ):
adminbae64d82013-08-01 10:50:15 -0700322 print introduction
323 while True:
324 try:
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700325 super( CLI, self ).cmdloop( intro="" )
adminbae64d82013-08-01 10:50:15 -0700326 self.postloop()
327 except KeyboardInterrupt:
Jon Hall1306a562015-09-04 11:21:24 -0700328 if testthread:
329 testthread.pause()
330 else:
331 print "KeyboardInterrupt, Exiting."
332 sys.exit()
adminbae64d82013-08-01 10:50:15 -0700333
334 def do_echo( self, line ):
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700335 """
adminbae64d82013-08-01 10:50:15 -0700336 Echoing of given input.
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700337 """
338 output( line )
adminbae64d82013-08-01 10:50:15 -0700339
340 def do_sh( self, line ):
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700341 """
adminbae64d82013-08-01 10:50:15 -0700342 Run an external shell command
343 sh pwd
344 sh ifconfig etc.
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700345 """
adminbae64d82013-08-01 10:50:15 -0700346 call( line, shell=True )
347
adminbae64d82013-08-01 10:50:15 -0700348 def do_py( self, line ):
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700349 """
adminbae64d82013-08-01 10:50:15 -0700350 Evaluate a Python expression.
Jon Hall4ba53f02015-07-29 13:07:41 -0700351
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700352 py main.log.info( "Sample Log Information" )
adminbae64d82013-08-01 10:50:15 -0700353 2013-01-07 12:07:26,804 - PoxTest - INFO - Sample Log Information
Jon Hall4ba53f02015-07-29 13:07:41 -0700354
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700355 """
adminbae64d82013-08-01 10:50:15 -0700356 try:
357 exec( line )
Jon Hall1306a562015-09-04 11:21:24 -0700358 except Exception as e:
adminbae64d82013-08-01 10:50:15 -0700359 output( str( e ) + '\n' )
Jon Hall4ba53f02015-07-29 13:07:41 -0700360
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700361 def do_interpret( self, line ):
362 """
adminbae64d82013-08-01 10:50:15 -0700363 interpret will translate the single line openspeak statement to equivalent python script.
Jon Hall4ba53f02015-07-29 13:07:41 -0700364
adminbae64d82013-08-01 10:50:15 -0700365 teston> interpret ASSERT result EQUALS main.TRUE ONPASS "Ping executed successfully" ONFAIL "Ping failed"
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700366 utilities.assert_equals( expect=main.TRUE,actual=result,onpass="Ping executed successfully",onfail="Ping failed" )
Jon Hall4ba53f02015-07-29 13:07:41 -0700367
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700368 """
adminbae64d82013-08-01 10:50:15 -0700369 from core import openspeak
370 ospk = openspeak.OpenSpeak()
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700371 try:
372 translated_code = ospk.interpret( text=line )
adminbae64d82013-08-01 10:50:15 -0700373 print translated_code
Jon Hall1306a562015-09-04 11:21:24 -0700374 except AttributeError as e:
adminbae64d82013-08-01 10:50:15 -0700375 print 'Dynamic params are not allowed in single statement translations'
Jon Hall4ba53f02015-07-29 13:07:41 -0700376
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700377 def do_do( self, line ):
378 """
adminbae64d82013-08-01 10:50:15 -0700379 Do will translate and execute the openspeak statement for the paused test.
380 do <OpenSpeak statement>
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700381 """
adminbae64d82013-08-01 10:50:15 -0700382 if testthread:
383 from core import openspeak
384 ospk = openspeak.OpenSpeak()
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700385 try:
386 translated_code = ospk.interpret( text=line )
387 eval( translated_code )
Jon Hall1306a562015-09-04 11:21:24 -0700388 except ( AttributeError, SyntaxError ) as e:
389 print 'Dynamic params are not allowed in single statement translations:'
390 print e
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700391 else:
adminbae64d82013-08-01 10:50:15 -0700392 print "Do will translate and execute the openspeak statement for the paused test.\nPlease use interpret to translate the OpenSpeak statement."
Jon Hall4ba53f02015-07-29 13:07:41 -0700393
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700394 def do_compile( self, line ):
395 """
396 compile will translate the openspeak ( .ospk ) file into TestON test script ( python ).
Jon Hall4ba53f02015-07-29 13:07:41 -0700397 It will receive the openspeak file path as input and will generate
398 equivalent test-script file in the same directory.
399
adminbae64d82013-08-01 10:50:15 -0700400 usage:
401 -----
402 teston>compile /home/openflow/TestON/PoxTest.ospk
Jon Hall4ba53f02015-07-29 13:07:41 -0700403
adminbae64d82013-08-01 10:50:15 -0700404 Auto-generated test-script file is /home/openflow/TestON/PoxTest.py
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700405 """
adminbae64d82013-08-01 10:50:15 -0700406 from core import openspeak
Jon Hall4ba53f02015-07-29 13:07:41 -0700407 openspeak = openspeak.OpenSpeak()
adminbae64d82013-08-01 10:50:15 -0700408 openspeakfile = line
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700409 if os.path.exists( openspeakfile ):
410 openspeak.compiler( openspeakfile=openspeakfile, writetofile=1 )
411 print "Auto-generated test-script file is " + re.sub( "ospk", "py", openspeakfile, 0 )
adminbae64d82013-08-01 10:50:15 -0700412 else:
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700413 print 'There is no such file : ' + line
adminbae64d82013-08-01 10:50:15 -0700414
415 def do_exit( self, _line ):
416 "Exit"
417 if testthread:
418 testthread.stop()
Jon Hall4ba53f02015-07-29 13:07:41 -0700419
adminbae64d82013-08-01 10:50:15 -0700420 sys.exit()
421
422 return 'exited by user command'
423
424 def do_quit( self, line ):
425 "Exit"
426 return self.do_exit( line )
427
428 def do_EOF( self, line ):
429 "Exit"
430 output( '\n' )
431 return self.do_exit( line )
432
433 def isatty( self ):
434 "Is our standard input a tty?"
435 return isatty( self.stdin.fileno() )
436
437 def do_source( self, line ):
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700438 """
adminbae64d82013-08-01 10:50:15 -0700439 Read shell commands from an input file and execute them sequentially.
440 cmdsource.txt :
Jon Hall4ba53f02015-07-29 13:07:41 -0700441
adminbae64d82013-08-01 10:50:15 -0700442 "pwd
443 ls "
Jon Hall4ba53f02015-07-29 13:07:41 -0700444
adminbae64d82013-08-01 10:50:15 -0700445 teston>source /home/openflow/cmdsource.txt
446 /home/openflow/TestON/bin/
447 cli.py __init__.py
Jon Hall4ba53f02015-07-29 13:07:41 -0700448
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700449 """
adminbae64d82013-08-01 10:50:15 -0700450 args = line.split()
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700451 if len( args ) != 1:
adminbae64d82013-08-01 10:50:15 -0700452 error( 'usage: source <file>\n' )
453 return
454 try:
455 self.inputFile = open( args[ 0 ] )
456 while True:
457 line = self.inputFile.readline()
458 if len( line ) > 0:
459 call( line, shell=True )
460 else:
461 break
462 except IOError:
463 error( 'error reading file %s\n' % args[ 0 ] )
Jon Hall4ba53f02015-07-29 13:07:41 -0700464
adminbae64d82013-08-01 10:50:15 -0700465 def do_time( self, line ):
466 "Measure time taken for any command in TestON."
467 start = time.time()
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700468 self.onecmd( line )
adminbae64d82013-08-01 10:50:15 -0700469 elapsed = time.time() - start
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700470 self.stdout.write( "*** Elapsed time: %0.6f secs\n" % elapsed )
adminbae64d82013-08-01 10:50:15 -0700471
472 def default( self, line ):
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700473 "Called on an input line when the command prefix is not recognized."
adminbae64d82013-08-01 10:50:15 -0700474 first, args, line = self.parseline( line )
475 if not args:
476 return
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700477 if args and len( args ) > 0 and args[ -1 ] == '\n':
adminbae64d82013-08-01 10:50:15 -0700478 args = args[ :-1 ]
479 rest = args.split( ' ' )
480
481 error( '*** Unknown command: %s\n' % first )
482
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700483
484class TestThread( threading.Thread ):
485
486 """
adminbae64d82013-08-01 10:50:15 -0700487 TestThread class will handle the test execution and will communicate with the thread in the do_run.
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700488 """
489 def __init__( self, options ):
adminbae64d82013-08-01 10:50:15 -0700490 self._stopevent = threading.Event()
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700491 threading.Thread.__init__( self )
adminbae64d82013-08-01 10:50:15 -0700492 self.is_stop = False
493 self.options = options
494 __builtin__.testthread = self
495
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700496 def run( self ):
497 """
adminbae64d82013-08-01 10:50:15 -0700498 Will execute the test.
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700499 """
500 while not self.is_stop:
adminbae64d82013-08-01 10:50:15 -0700501 if not self._stopevent.isSet():
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700502 self.test_on = TestON( self.options )
503 try:
adminbae64d82013-08-01 10:50:15 -0700504 if self.test_on.init_result:
505 result = self.test_on.run()
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700506 if not self.is_stop:
adminbae64d82013-08-01 10:50:15 -0700507 result = self.test_on.cleanup()
508 self.is_stop = True
Jon Hall1306a562015-09-04 11:21:24 -0700509 except KeyboardInterrupt:
510 print "Recevied Interrupt, cleaning-up the logs and drivers before exiting"
adminbae64d82013-08-01 10:50:15 -0700511 result = self.test_on.cleanup()
512 self.is_stop = True
513
Jon Hall4ba53f02015-07-29 13:07:41 -0700514 __builtin__.testthread = False
adminbae64d82013-08-01 10:50:15 -0700515
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700516 def pause( self ):
517 """
adminbae64d82013-08-01 10:50:15 -0700518 Will pause the test.
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700519 """
Jon Hall1306a562015-09-04 11:21:24 -0700520 if not cli.pause:
521 print "Will pause the test's execution, after completion of this step.....\n\n\n\n"
522 cli.pause = True
523 self._stopevent.set()
524 elif cli.pause and self.is_stop:
525 print "KeyboardInterrupt, Exiting."
526 self.test_on.exit()
527 else:
528 print "Recevied Interrupt, cleaning-up the logs and drivers before exiting"
529 result = self.test_on.cleanup()
530 self.is_stop = True
adminbae64d82013-08-01 10:50:15 -0700531
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700532 def play( self ):
533 """
adminbae64d82013-08-01 10:50:15 -0700534 Will resume the paused test.
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700535 """
adminbae64d82013-08-01 10:50:15 -0700536 self._stopevent.clear()
537 cli.pause = False
Jon Hall4ba53f02015-07-29 13:07:41 -0700538
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700539 def stop( self ):
540 """
adminbae64d82013-08-01 10:50:15 -0700541 Will stop the test execution.
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700542 """
adminbae64d82013-08-01 10:50:15 -0700543 print "Stopping the test"
544 self.is_stop = True
545 cli.stop = True
546 __builtin__.testthread = False
Jon Hall4ba53f02015-07-29 13:07:41 -0700547
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700548
549def output( msg ):
550 """
adminbae64d82013-08-01 10:50:15 -0700551 Simply, print the message in console
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700552 """
adminbae64d82013-08-01 10:50:15 -0700553 print msg
554
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700555
556def error( msg ):
557 """
adminbae64d82013-08-01 10:50:15 -0700558 print the error message.
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700559 """
adminbae64d82013-08-01 10:50:15 -0700560 print msg
561
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700562
563def dictToObj( dictionary ):
564 """
adminbae64d82013-08-01 10:50:15 -0700565 This will facilitates the converting of the dictionary to the object.
566 This method will help to send options as object format to the test.
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700567 """
568 if isinstance( dictionary, list ):
569 dictionary = [ dictToObj( x ) for x in dictionary ]
570 if not isinstance( dictionary, dict ):
adminbae64d82013-08-01 10:50:15 -0700571 return dictionary
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700572
573 class Convert( object ):
adminbae64d82013-08-01 10:50:15 -0700574 pass
575 obj = Convert()
576 for k in dictionary:
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700577 obj.__dict__[ k ] = dictToObj( dictionary[ k ] )
adminbae64d82013-08-01 10:50:15 -0700578 return obj
579
580
581if __name__ == '__main__':
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700582 if len( sys.argv ) > 1:
Jon Hall0bde9ba2015-03-19 11:32:57 -0700583 __builtin__.COLORS = True
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700584 CLI( "test" ).onecmd( ' '.join( sys.argv[ 1: ] ) )
adminbae64d82013-08-01 10:50:15 -0700585 else:
Jon Hall0bde9ba2015-03-19 11:32:57 -0700586 __builtin__.COLORS = False
Jeremy Ronquillo23fb2162017-09-15 14:59:57 -0700587 CLI( "test" ).cmdloop()