admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
| 2 | ''' |
| 3 | Created on 20-Dec-2012 |
Jeremy Ronquillo | b27ce4c | 2017-07-17 12:41:28 -0700 | [diff] [blame] | 4 | Copyright 2012 Open Networking Foundation |
Jon Hall | 5f15fef | 2015-07-17 14:22:14 -0700 | [diff] [blame] | 5 | |
Jeremy Songster | ae01bba | 2016-07-11 15:39:17 -0700 | [diff] [blame] | 6 | Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>, |
| 7 | the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>, |
| 8 | or the System Testing Guide page at <https://wiki.onosproject.org/x/WYQg> |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 9 | |
| 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 |
| 13 | (at your option) any later version. |
| 14 | |
| 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 Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 21 | along with TestON. If not, see <http://www.gnu.org/licenses/>. |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 22 | |
| 23 | |
| 24 | ''' |
| 25 | |
| 26 | |
| 27 | """ |
| 28 | cli will provide the CLI shell for teston framework. |
| 29 | |
| 30 | A simple command-line interface for TestON. |
| 31 | |
| 32 | The TestON CLI provides a simple console which |
| 33 | makes it easy to launch the test. For example, the command run will execute the test. |
| 34 | |
| 35 | teston> run test DpctlTest |
| 36 | Several useful commands are provided. |
| 37 | """ |
| 38 | |
| 39 | from subprocess import call |
| 40 | from cmd import Cmd |
| 41 | from os import isatty |
| 42 | import sys |
| 43 | import re |
| 44 | import os |
| 45 | import time |
| 46 | import threading |
| 47 | import __builtin__ |
| 48 | import pprint |
| 49 | dump = pprint.PrettyPrinter(indent=4) |
| 50 | __builtin__.testthread = False |
| 51 | introduction = "TestON is the testing framework \nDeveloped by Paxterra Solutions (www.paxterrasolutions.com)" |
Jon Hall | 0bde9ba | 2015-03-19 11:32:57 -0700 | [diff] [blame] | 52 | __builtin__.COLORS = False |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 53 | |
Jon Hall | 1dd5a0a | 2015-07-08 10:49:26 -0700 | [diff] [blame] | 54 | path = re.sub( "/bin$", "", sys.path[0] ) |
| 55 | sys.path.insert( 1, path ) |
Jon Hall | 0bde9ba | 2015-03-19 11:32:57 -0700 | [diff] [blame] | 56 | from core.teston import * |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 57 | |
| 58 | class CLI( threading.Thread,Cmd,object ): |
| 59 | "command-line interface to execute the test." |
| 60 | |
| 61 | prompt = 'teston> ' |
| 62 | |
| 63 | def __init__( self, teston, stdin=sys.stdin ): |
| 64 | self.teston = teston |
Jon Hall | 0bde9ba | 2015-03-19 11:32:57 -0700 | [diff] [blame] | 65 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 66 | self._mainevent = threading.Event() |
| 67 | threading.Thread.__init__(self) |
| 68 | self.main_stop = False |
| 69 | self.locals = { 'test': teston } |
| 70 | self.stdin = stdin |
| 71 | Cmd.__init__( self ) |
| 72 | self.pause = False |
| 73 | self.stop = False |
| 74 | __builtin__.cli = self |
| 75 | |
| 76 | def emptyline( self ): |
| 77 | "Don't repeat last command when you hit return." |
| 78 | pass |
| 79 | |
| 80 | helpStr = ( |
| 81 | " teston help" |
| 82 | ) |
| 83 | |
| 84 | def do_help( self, line ): |
| 85 | "Describe available CLI commands." |
| 86 | Cmd.do_help( self, line ) |
| 87 | if line is '': |
| 88 | output( self.helpStr ) |
| 89 | def do_run(self,args): |
| 90 | ''' |
| 91 | 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. |
| 96 | ''' |
Jon Hall | 1306a56 | 2015-09-04 11:21:24 -0700 | [diff] [blame] | 97 | try: |
| 98 | args = args.split() |
| 99 | options = {} |
| 100 | options = self.parseArgs(args,options) |
| 101 | options = dictToObj(options) |
| 102 | if not testthread: |
| 103 | test = TestThread(options) |
| 104 | test.start() |
| 105 | while test.isAlive(): |
| 106 | test.join(1) |
| 107 | else: |
| 108 | print main.TEST+ " test execution paused, please resume that before executing to another test" |
| 109 | except KeyboardInterrupt, SystemExit: |
| 110 | print "Interrupt called, Exiting." |
| 111 | test._Thread__stop() |
| 112 | main.cleanup() |
| 113 | main.exit() |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 114 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 115 | def do_resume(self, line): |
| 116 | ''' |
| 117 | resume command will continue the execution of paused test. |
| 118 | teston>resume |
| 119 | [2013-01-07 23:03:44.640723] [PoxTest] [STEP] 1.1: Checking the host reachability using pingHost |
| 120 | 2013-01-07 23:03:44,858 - PoxTest - INFO - Expected Prompt Found |
| 121 | .... |
| 122 | ''' |
| 123 | if testthread: |
| 124 | testthread.play() |
| 125 | else : |
| 126 | print "There is no test to resume" |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 127 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 128 | def do_nextstep(self,line): |
| 129 | ''' |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 130 | nextstep will execute the next-step of the paused test and |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 131 | it will pause the test after finishing of step. |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 132 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 133 | teston> nextstep |
| 134 | Will pause the test's execution, after completion of this step..... |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 135 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 136 | teston> [2013-01-07 21:24:26.286601] [PoxTest] [STEP] 1.8: Checking the host reachability using pingHost |
| 137 | 2013-01-07 21:24:26,455 - PoxTest - INFO - Expected Prompt Found |
| 138 | ..... |
| 139 | teston> |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 140 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 141 | ''' |
| 142 | if testthread: |
| 143 | main.log.info("Executing the nextstep, Will pause test execution, after completion of the step") |
| 144 | testthread.play() |
| 145 | time.sleep(.1) |
| 146 | testthread.pause() |
| 147 | else: |
| 148 | print "There is no paused test " |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 149 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 150 | def do_dumpvar(self,line): |
| 151 | ''' |
| 152 | dumpvar will print all the test data in raw format. |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 153 | usgae : |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 154 | teston>dumpvar main |
| 155 | Here 'main' will be the test object. |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 156 | |
| 157 | teston>dumpvar params |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 158 | here 'params' will be the parameters specified in the params file. |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 159 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 160 | teston>dumpvar topology |
| 161 | here 'topology' will be topology specification of the test specified in topo file. |
| 162 | ''' |
| 163 | if testthread: |
| 164 | if line == "main": |
| 165 | dump.pprint(vars(main)) |
| 166 | else : |
| 167 | try : |
| 168 | dump.pprint(vars(main)[line]) |
Jon Hall | 1306a56 | 2015-09-04 11:21:24 -0700 | [diff] [blame] | 169 | except KeyError as e: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 170 | print e |
| 171 | else : |
| 172 | print "There is no paused test " |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 173 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 174 | def do_currentcase(self,line): |
| 175 | ''' |
| 176 | currentcase will return the current case in the test execution. |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 177 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 178 | teston>currentcase |
| 179 | Currently executing test case is: 2 |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 180 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 181 | ''' |
| 182 | if testthread: |
| 183 | print "Currently executing test case is: "+str(main.CurrentTestCaseNumber) |
| 184 | else : |
| 185 | print "There is no paused test " |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 186 | |
| 187 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 188 | def do_currentstep(self,line): |
| 189 | ''' |
| 190 | currentstep will return the current step in the test execution. |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 191 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 192 | teston>currentstep |
| 193 | Currently executing test step is: 2.3 |
| 194 | ''' |
| 195 | if testthread: |
| 196 | print "Currently executing test step is: "+str(main.CurrentTestCaseNumber)+'.'+str(main.stepCount) |
| 197 | else : |
| 198 | print "There is no paused test " |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 199 | |
| 200 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 201 | def do_stop(self,line): |
| 202 | ''' |
| 203 | Will stop the paused test, if any ! |
| 204 | ''' |
| 205 | if testthread: |
| 206 | testthread.stop() |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 207 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 208 | return 'exited by user command' |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 209 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 210 | def do_gettest(self,line): |
| 211 | ''' |
| 212 | gettest will return the test name which is under execution or recently executed. |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 213 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 214 | Test under execution: |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 215 | teston>gettest |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 216 | Currently executing Test is: PoxTest |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 217 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 218 | Test recently executed: |
| 219 | Recently executed test is: MininetTest |
| 220 | ''' |
| 221 | try : |
| 222 | if testthread : |
| 223 | print "Currently executing Test is: "+main.TEST |
| 224 | else : |
| 225 | print "Recently executed test is: "+main.TEST |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 226 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 227 | except NameError: |
| 228 | print "There is no previously executed Test" |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 229 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 230 | def do_showlog(self,line): |
| 231 | ''' |
| 232 | showlog will show the test's Log |
| 233 | teston>showlog |
| 234 | 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 |
| 235 | ..... |
| 236 | teston>showlog |
| 237 | 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 |
| 238 | ..... |
| 239 | ''' |
| 240 | try : |
| 241 | if testthread : |
| 242 | print "Currently executing Test's log is: "+main.LogFileName |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 243 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 244 | else : |
| 245 | print "Last executed test's log is : "+main.LogFileName |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 246 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 247 | logFile = main.LogFileName |
| 248 | logFileHandler = open(logFile, 'r') |
| 249 | for msg in logFileHandler.readlines() : |
| 250 | print msg, |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 251 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 252 | logFileHandler.close() |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 253 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 254 | except NameError: |
| 255 | print "There is no previously executed Test" |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 256 | |
| 257 | |
| 258 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 259 | def parseArgs(self,args,options): |
| 260 | ''' |
| 261 | This will parse the command line arguments. |
| 262 | ''' |
| 263 | options = self.initOptions(options) |
| 264 | try : |
YPZhang | 16f6e56 | 2016-07-12 15:50:31 -0700 | [diff] [blame] | 265 | index = 0 |
| 266 | while index < len( args ): |
| 267 | option = args[index] |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 268 | if index > 0 : |
YPZhang | 16f6e56 | 2016-07-12 15:50:31 -0700 | [diff] [blame] | 269 | if re.match("--params", option, flags=0): |
YPZhang | 1c89e76 | 2016-06-29 10:43:58 -0700 | [diff] [blame] | 270 | # check if there is a params |
YPZhang | 16f6e56 | 2016-07-12 15:50:31 -0700 | [diff] [blame] | 271 | options['params'].append(args[index+1]) |
| 272 | elif re.match("logdir|mail|example|testdir|testcases|onoscell", option, flags = 0): |
| 273 | options[option] = args[index+1] |
| 274 | options = self.testcasesInRange(index+1,option,args,options) |
| 275 | index += 2 |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 276 | else : |
| 277 | options['testname'] = option |
YPZhang | 16f6e56 | 2016-07-12 15:50:31 -0700 | [diff] [blame] | 278 | index += 1 |
Jon Hall | 1306a56 | 2015-09-04 11:21:24 -0700 | [diff] [blame] | 279 | except IndexError as e: |
YPZhang | 16f6e56 | 2016-07-12 15:50:31 -0700 | [diff] [blame] | 280 | print (e) |
| 281 | main.cleanup() |
| 282 | main.exit() |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 283 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 284 | return options |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 285 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 286 | def initOptions(self,options): |
| 287 | ''' |
| 288 | This will initialize the commandline options. |
| 289 | ''' |
| 290 | options['logdir'] = None |
| 291 | options['mail'] = None |
| 292 | options['example'] = None |
| 293 | options['testdir'] = None |
| 294 | options['testcases'] = None |
Hari Krishna | 03f530e | 2015-07-10 17:28:27 -0700 | [diff] [blame] | 295 | options['onoscell'] = None |
YPZhang | 1c89e76 | 2016-06-29 10:43:58 -0700 | [diff] [blame] | 296 | # init params as a empty list |
| 297 | options['params'] = [] |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 298 | return options |
| 299 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 300 | def testcasesInRange(self,index,option,args,options): |
| 301 | ''' |
| 302 | This method will handle testcases list,specified in range [1-10]. |
| 303 | ''' |
| 304 | if re.match("testcases",option,1): |
| 305 | testcases = [] |
| 306 | args[index] = re.sub("\[|\]","",args[index],0) |
| 307 | m = re.match("(\d+)\-(\d+)",args[index],flags=0) |
| 308 | if m: |
| 309 | start_case = eval(m.group(1)) |
| 310 | end_case = eval(m.group(2)) |
| 311 | if (start_case <= end_case): |
| 312 | i = start_case |
| 313 | while i <= end_case: |
| 314 | testcases.append(i) |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 315 | i= i+1 |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 316 | else : |
| 317 | print "Please specify testcases properly like 1-5" |
| 318 | else : |
| 319 | options[option] = args[index] |
| 320 | return options |
| 321 | options[option] = str(testcases) |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 322 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 323 | return options |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 324 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 325 | def cmdloop(self, intro=introduction): |
| 326 | print introduction |
| 327 | while True: |
| 328 | try: |
| 329 | super(CLI, self).cmdloop(intro="") |
| 330 | self.postloop() |
| 331 | except KeyboardInterrupt: |
Jon Hall | 1306a56 | 2015-09-04 11:21:24 -0700 | [diff] [blame] | 332 | if testthread: |
| 333 | testthread.pause() |
| 334 | else: |
| 335 | print "KeyboardInterrupt, Exiting." |
| 336 | sys.exit() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 337 | |
| 338 | def do_echo( self, line ): |
| 339 | ''' |
| 340 | Echoing of given input. |
| 341 | ''' |
| 342 | output(line) |
| 343 | |
| 344 | def do_sh( self, line ): |
| 345 | ''' |
| 346 | Run an external shell command |
| 347 | sh pwd |
| 348 | sh ifconfig etc. |
| 349 | ''' |
| 350 | call( line, shell=True ) |
| 351 | |
| 352 | |
| 353 | def do_py( self, line ): |
| 354 | ''' |
| 355 | Evaluate a Python expression. |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 356 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 357 | py main.log.info("Sample Log Information") |
| 358 | 2013-01-07 12:07:26,804 - PoxTest - INFO - Sample Log Information |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 359 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 360 | ''' |
| 361 | try: |
| 362 | exec( line ) |
Jon Hall | 1306a56 | 2015-09-04 11:21:24 -0700 | [diff] [blame] | 363 | except Exception as e: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 364 | output( str( e ) + '\n' ) |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 365 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 366 | def do_interpret(self,line): |
| 367 | ''' |
| 368 | interpret will translate the single line openspeak statement to equivalent python script. |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 369 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 370 | teston> interpret ASSERT result EQUALS main.TRUE ONPASS "Ping executed successfully" ONFAIL "Ping failed" |
| 371 | utilities.assert_equals(expect=main.TRUE,actual=result,onpass="Ping executed successfully",onfail="Ping failed") |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 372 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 373 | ''' |
| 374 | from core import openspeak |
| 375 | ospk = openspeak.OpenSpeak() |
| 376 | try : |
| 377 | translated_code = ospk.interpret(text=line) |
| 378 | print translated_code |
Jon Hall | 1306a56 | 2015-09-04 11:21:24 -0700 | [diff] [blame] | 379 | except AttributeError as e: |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 380 | print 'Dynamic params are not allowed in single statement translations' |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 381 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 382 | def do_do (self,line): |
| 383 | ''' |
| 384 | Do will translate and execute the openspeak statement for the paused test. |
| 385 | do <OpenSpeak statement> |
| 386 | ''' |
| 387 | if testthread: |
| 388 | from core import openspeak |
| 389 | ospk = openspeak.OpenSpeak() |
| 390 | try : |
| 391 | translated_code = ospk.interpret(text=line) |
| 392 | eval(translated_code) |
Jon Hall | 1306a56 | 2015-09-04 11:21:24 -0700 | [diff] [blame] | 393 | except ( AttributeError, SyntaxError ) as e: |
| 394 | print 'Dynamic params are not allowed in single statement translations:' |
| 395 | print e |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 396 | else : |
| 397 | print "Do will translate and execute the openspeak statement for the paused test.\nPlease use interpret to translate the OpenSpeak statement." |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 398 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 399 | def do_compile(self,line): |
| 400 | ''' |
| 401 | compile will translate the openspeak (.ospk) file into TestON test script (python). |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 402 | It will receive the openspeak file path as input and will generate |
| 403 | equivalent test-script file in the same directory. |
| 404 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 405 | usage: |
| 406 | ----- |
| 407 | teston>compile /home/openflow/TestON/PoxTest.ospk |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 408 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 409 | Auto-generated test-script file is /home/openflow/TestON/PoxTest.py |
| 410 | ''' |
| 411 | from core import openspeak |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 412 | openspeak = openspeak.OpenSpeak() |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 413 | openspeakfile = line |
| 414 | if os.path.exists(openspeakfile) : |
| 415 | openspeak.compiler(openspeakfile=openspeakfile,writetofile=1) |
| 416 | print "Auto-generated test-script file is "+ re.sub("ospk","py",openspeakfile,0) |
| 417 | else: |
| 418 | print 'There is no such file : '+line |
| 419 | |
| 420 | def do_exit( self, _line ): |
| 421 | "Exit" |
| 422 | if testthread: |
| 423 | testthread.stop() |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 424 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 425 | sys.exit() |
| 426 | |
| 427 | return 'exited by user command' |
| 428 | |
| 429 | def do_quit( self, line ): |
| 430 | "Exit" |
| 431 | return self.do_exit( line ) |
| 432 | |
| 433 | def do_EOF( self, line ): |
| 434 | "Exit" |
| 435 | output( '\n' ) |
| 436 | return self.do_exit( line ) |
| 437 | |
| 438 | def isatty( self ): |
| 439 | "Is our standard input a tty?" |
| 440 | return isatty( self.stdin.fileno() ) |
| 441 | |
| 442 | def do_source( self, line ): |
| 443 | ''' |
| 444 | Read shell commands from an input file and execute them sequentially. |
| 445 | cmdsource.txt : |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 446 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 447 | "pwd |
| 448 | ls " |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 449 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 450 | teston>source /home/openflow/cmdsource.txt |
| 451 | /home/openflow/TestON/bin/ |
| 452 | cli.py __init__.py |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 453 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 454 | ''' |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 455 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 456 | args = line.split() |
| 457 | if len(args) != 1: |
| 458 | error( 'usage: source <file>\n' ) |
| 459 | return |
| 460 | try: |
| 461 | self.inputFile = open( args[ 0 ] ) |
| 462 | while True: |
| 463 | line = self.inputFile.readline() |
| 464 | if len( line ) > 0: |
| 465 | call( line, shell=True ) |
| 466 | else: |
| 467 | break |
| 468 | except IOError: |
| 469 | error( 'error reading file %s\n' % args[ 0 ] ) |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 470 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 471 | def do_time( self, line ): |
| 472 | "Measure time taken for any command in TestON." |
| 473 | start = time.time() |
| 474 | self.onecmd(line) |
| 475 | elapsed = time.time() - start |
| 476 | self.stdout.write("*** Elapsed time: %0.6f secs\n" % elapsed) |
| 477 | |
| 478 | def default( self, line ): |
| 479 | """Called on an input line when the command prefix is not recognized.""" |
| 480 | first, args, line = self.parseline( line ) |
| 481 | if not args: |
| 482 | return |
| 483 | if args and len(args) > 0 and args[ -1 ] == '\n': |
| 484 | args = args[ :-1 ] |
| 485 | rest = args.split( ' ' ) |
| 486 | |
| 487 | error( '*** Unknown command: %s\n' % first ) |
| 488 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 489 | class TestThread(threading.Thread): |
| 490 | ''' |
| 491 | TestThread class will handle the test execution and will communicate with the thread in the do_run. |
| 492 | ''' |
| 493 | def __init__(self,options): |
| 494 | self._stopevent = threading.Event() |
| 495 | threading.Thread.__init__(self) |
| 496 | self.is_stop = False |
| 497 | self.options = options |
| 498 | __builtin__.testthread = self |
| 499 | |
| 500 | def run(self): |
| 501 | ''' |
| 502 | Will execute the test. |
| 503 | ''' |
| 504 | while not self.is_stop : |
| 505 | if not self._stopevent.isSet(): |
| 506 | self.test_on = TestON(self.options) |
| 507 | try : |
| 508 | if self.test_on.init_result: |
| 509 | result = self.test_on.run() |
| 510 | if not self.is_stop : |
| 511 | result = self.test_on.cleanup() |
| 512 | self.is_stop = True |
Jon Hall | 1306a56 | 2015-09-04 11:21:24 -0700 | [diff] [blame] | 513 | except KeyboardInterrupt: |
| 514 | print "Recevied Interrupt, cleaning-up the logs and drivers before exiting" |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 515 | result = self.test_on.cleanup() |
| 516 | self.is_stop = True |
| 517 | |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 518 | __builtin__.testthread = False |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 519 | |
| 520 | def pause(self): |
| 521 | ''' |
| 522 | Will pause the test. |
| 523 | ''' |
Jon Hall | 1306a56 | 2015-09-04 11:21:24 -0700 | [diff] [blame] | 524 | if not cli.pause: |
| 525 | print "Will pause the test's execution, after completion of this step.....\n\n\n\n" |
| 526 | cli.pause = True |
| 527 | self._stopevent.set() |
| 528 | elif cli.pause and self.is_stop: |
| 529 | print "KeyboardInterrupt, Exiting." |
| 530 | self.test_on.exit() |
| 531 | else: |
| 532 | print "Recevied Interrupt, cleaning-up the logs and drivers before exiting" |
| 533 | result = self.test_on.cleanup() |
| 534 | self.is_stop = True |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 535 | |
| 536 | def play(self): |
| 537 | ''' |
| 538 | Will resume the paused test. |
| 539 | ''' |
| 540 | self._stopevent.clear() |
| 541 | cli.pause = False |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 542 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 543 | def stop(self): |
| 544 | ''' |
| 545 | Will stop the test execution. |
| 546 | ''' |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 547 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 548 | print "Stopping the test" |
| 549 | self.is_stop = True |
| 550 | cli.stop = True |
| 551 | __builtin__.testthread = False |
Jon Hall | 4ba53f0 | 2015-07-29 13:07:41 -0700 | [diff] [blame] | 552 | |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 553 | def output(msg): |
| 554 | ''' |
| 555 | Simply, print the message in console |
| 556 | ''' |
| 557 | print msg |
| 558 | |
| 559 | def error(msg): |
| 560 | ''' |
| 561 | print the error message. |
| 562 | ''' |
| 563 | print msg |
| 564 | |
| 565 | def dictToObj(dictionary): |
| 566 | ''' |
| 567 | This will facilitates the converting of the dictionary to the object. |
| 568 | This method will help to send options as object format to the test. |
| 569 | ''' |
| 570 | if isinstance(dictionary, list): |
| 571 | dictionary = [dictToObj(x) for x in dictionary] |
| 572 | if not isinstance(dictionary, dict): |
| 573 | return dictionary |
| 574 | class Convert(object): |
| 575 | pass |
| 576 | obj = Convert() |
| 577 | for k in dictionary: |
| 578 | obj.__dict__[k] = dictToObj(dictionary[k]) |
| 579 | return obj |
| 580 | |
| 581 | |
| 582 | if __name__ == '__main__': |
| 583 | if len(sys.argv) > 1: |
Jon Hall | 0bde9ba | 2015-03-19 11:32:57 -0700 | [diff] [blame] | 584 | __builtin__.COLORS = True |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 585 | CLI("test").onecmd(' '.join(sys.argv[1:])) |
| 586 | else: |
Jon Hall | 0bde9ba | 2015-03-19 11:32:57 -0700 | [diff] [blame] | 587 | __builtin__.COLORS = False |
admin | bae64d8 | 2013-08-01 10:50:15 -0700 | [diff] [blame] | 588 | CLI("test").cmdloop() |