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