Jon Hall | 05b2b43 | 2014-10-08 19:53:25 -0400 | [diff] [blame] | 1 | #!/usr/bin/env python |
andrewonlab | e8e56fd | 2014-10-09 17:12:44 -0400 | [diff] [blame] | 2 | |
Jon Hall | 05b2b43 | 2014-10-08 19:53:25 -0400 | [diff] [blame] | 3 | ''' |
andrewonlab | e8e56fd | 2014-10-09 17:12:44 -0400 | [diff] [blame] | 4 | This driver interacts with ONOS bench, the OSGi platform |
| 5 | that configures the ONOS nodes. (aka ONOS-next) |
| 6 | |
| 7 | Please follow the coding style demonstrated by existing |
| 8 | functions and document properly. |
| 9 | |
| 10 | If you are a contributor to the driver, please |
| 11 | list your email here for future contact: |
| 12 | |
| 13 | jhall@onlab.us |
| 14 | andrew@onlab.us |
| 15 | |
| 16 | OCT 9 2014 |
| 17 | |
Jon Hall | 05b2b43 | 2014-10-08 19:53:25 -0400 | [diff] [blame] | 18 | ''' |
andrewonlab | e8e56fd | 2014-10-09 17:12:44 -0400 | [diff] [blame] | 19 | |
andrewonlab | 7735d85 | 2014-10-09 13:02:47 -0400 | [diff] [blame] | 20 | import sys |
Jon Hall | 05b2b43 | 2014-10-08 19:53:25 -0400 | [diff] [blame] | 21 | import time |
| 22 | import pexpect |
Jon Hall | 05b2b43 | 2014-10-08 19:53:25 -0400 | [diff] [blame] | 23 | import traceback |
andrewonlab | 7735d85 | 2014-10-09 13:02:47 -0400 | [diff] [blame] | 24 | import os.path |
Jon Hall | 05b2b43 | 2014-10-08 19:53:25 -0400 | [diff] [blame] | 25 | sys.path.append("../") |
| 26 | from drivers.common.clidriver import CLI |
| 27 | |
| 28 | class OnosDriver(CLI): |
| 29 | |
| 30 | def __init__(self): |
andrewonlab | 6e20c34 | 2014-10-10 18:08:48 -0400 | [diff] [blame] | 31 | ''' |
| 32 | Initialize client |
| 33 | ''' |
Jon Hall | 05b2b43 | 2014-10-08 19:53:25 -0400 | [diff] [blame] | 34 | super(CLI, self).__init__() |
| 35 | |
| 36 | def connect(self,**connectargs): |
| 37 | ''' |
| 38 | Creates ssh handle for ONOS "bench". |
| 39 | ''' |
| 40 | try: |
| 41 | for key in connectargs: |
| 42 | vars(self)[key] = connectargs[key] |
| 43 | self.home = "~/ONOS" |
| 44 | for key in self.options: |
| 45 | if key == "home": |
| 46 | self.home = self.options['home'] |
| 47 | break |
| 48 | |
| 49 | |
| 50 | self.name = self.options['name'] |
Jon Hall | ea7818b | 2014-10-09 14:30:59 -0400 | [diff] [blame] | 51 | self.handle = super(OnosDriver,self).connect( |
| 52 | user_name = self.user_name, |
| 53 | ip_address = self.ip_address, |
| 54 | port = self.port, |
| 55 | pwd = self.pwd, |
| 56 | home = self.home) |
Jon Hall | ea7818b | 2014-10-09 14:30:59 -0400 | [diff] [blame] | 57 | |
| 58 | self.handle.sendline("cd "+ self.home) |
| 59 | self.handle.expect("\$") |
Jon Hall | 05b2b43 | 2014-10-08 19:53:25 -0400 | [diff] [blame] | 60 | if self.handle: |
| 61 | return self.handle |
| 62 | else : |
| 63 | main.log.info("NO ONOS HANDLE") |
| 64 | return main.FALSE |
| 65 | except pexpect.EOF: |
| 66 | main.log.error(self.name + ": EOF exception found") |
| 67 | main.log.error(self.name + ": " + self.handle.before) |
| 68 | main.cleanup() |
| 69 | main.exit() |
| 70 | except: |
| 71 | main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::") |
| 72 | main.log.error( traceback.print_exc() ) |
| 73 | main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::") |
| 74 | main.cleanup() |
| 75 | main.exit() |
| 76 | |
| 77 | def disconnect(self): |
| 78 | ''' |
| 79 | Called when Test is complete to disconnect the ONOS handle. |
| 80 | ''' |
| 81 | response = '' |
| 82 | try: |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 83 | self.handle.sendline("") |
Jon Hall | f8d1098 | 2014-10-22 12:23:38 -0400 | [diff] [blame] | 84 | self.handle.expect("\$") |
Jon Hall | 05b2b43 | 2014-10-08 19:53:25 -0400 | [diff] [blame] | 85 | self.handle.sendline("exit") |
| 86 | self.handle.expect("closed") |
| 87 | except pexpect.EOF: |
| 88 | main.log.error(self.name + ": EOF exception found") |
| 89 | main.log.error(self.name + ": " + self.handle.before) |
| 90 | except: |
| 91 | main.log.error(self.name + ": Connection failed to the host") |
| 92 | response = main.FALSE |
| 93 | return response |
andrew@onlab.us | 9e2cd0f | 2014-10-08 20:32:32 -0400 | [diff] [blame] | 94 | |
| 95 | def onos_package(self): |
| 96 | ''' |
| 97 | Produce a self-contained tar.gz file that can be deployed |
| 98 | and executed on any platform with Java 7 JRE. |
| 99 | ''' |
andrew@onlab.us | 9e2cd0f | 2014-10-08 20:32:32 -0400 | [diff] [blame] | 100 | |
| 101 | try: |
| 102 | self.handle.sendline("onos-package") |
Jon Hall | ea7818b | 2014-10-09 14:30:59 -0400 | [diff] [blame] | 103 | self.handle.expect("onos-package") |
andrewonlab | e990790 | 2014-11-25 15:49:06 -0500 | [diff] [blame] | 104 | self.handle.expect("tar.gz",timeout=30) |
andrew@onlab.us | 9e2cd0f | 2014-10-08 20:32:32 -0400 | [diff] [blame] | 105 | handle = str(self.handle.before) |
| 106 | main.log.info("onos-package command returned: "+ |
| 107 | handle) |
andrewonlab | 0748d2a | 2014-10-09 13:24:17 -0400 | [diff] [blame] | 108 | #As long as the sendline does not time out, |
| 109 | #return true. However, be careful to interpret |
| 110 | #the results of the onos-package command return |
| 111 | return main.TRUE |
andrew@onlab.us | 9e2cd0f | 2014-10-08 20:32:32 -0400 | [diff] [blame] | 112 | |
andrewonlab | 7735d85 | 2014-10-09 13:02:47 -0400 | [diff] [blame] | 113 | except pexpect.EOF: |
andrew@onlab.us | 9e2cd0f | 2014-10-08 20:32:32 -0400 | [diff] [blame] | 114 | main.log.error(self.name + ": EOF exception found") |
| 115 | main.log.error(self.name + ": " + self.handle.before) |
| 116 | except: |
| 117 | main.log.error("Failed to package ONOS") |
| 118 | main.cleanup() |
| 119 | main.exit() |
| 120 | |
andrewonlab | 8790abb | 2014-11-06 13:51:54 -0500 | [diff] [blame] | 121 | def onos_build(self): |
| 122 | ''' |
| 123 | Use the pre defined script to build onos via mvn |
| 124 | ''' |
| 125 | |
| 126 | try: |
| 127 | self.handle.sendline("onos-build") |
| 128 | self.handle.expect("onos-build") |
| 129 | i = self.handle.expect([ |
| 130 | "BUILD SUCCESS", |
| 131 | "ERROR", |
| 132 | "BUILD FAILED"], timeout=120) |
| 133 | handle = str(self.handle.before) |
| 134 | |
| 135 | main.log.info("onos-build command returned: "+ |
| 136 | handle) |
| 137 | |
| 138 | if i == 0: |
| 139 | return main.TRUE |
| 140 | else: |
| 141 | return handle |
| 142 | |
| 143 | except pexpect.EOF: |
| 144 | main.log.error(self.name + ": EOF exception found") |
| 145 | main.log.error(self.name + ": " + self.handle.before) |
| 146 | except: |
| 147 | main.log.error("Failed to build ONOS") |
| 148 | main.cleanup() |
| 149 | main.exit() |
| 150 | |
Jon Hall | de9d9aa | 2014-10-08 20:36:02 -0400 | [diff] [blame] | 151 | def clean_install(self): |
| 152 | ''' |
| 153 | Runs mvn clean install in the root of the ONOS directory. |
| 154 | This will clean all ONOS artifacts then compile each module |
andrew@onlab.us | 9e2cd0f | 2014-10-08 20:32:32 -0400 | [diff] [blame] | 155 | |
Jon Hall | de9d9aa | 2014-10-08 20:36:02 -0400 | [diff] [blame] | 156 | Returns: main.TRUE on success |
| 157 | On Failure, exits the test |
| 158 | ''' |
| 159 | try: |
Jon Hall | ea7818b | 2014-10-09 14:30:59 -0400 | [diff] [blame] | 160 | main.log.info("Running 'mvn clean install' on " + str(self.name) + |
| 161 | ". This may take some time.") |
| 162 | self.handle.sendline("cd "+ self.home) |
| 163 | self.handle.expect("\$") |
| 164 | |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 165 | self.handle.sendline("") |
Jon Hall | ea7818b | 2014-10-09 14:30:59 -0400 | [diff] [blame] | 166 | self.handle.expect("\$") |
Jon Hall | de9d9aa | 2014-10-08 20:36:02 -0400 | [diff] [blame] | 167 | self.handle.sendline("mvn clean install") |
Jon Hall | ea7818b | 2014-10-09 14:30:59 -0400 | [diff] [blame] | 168 | self.handle.expect("mvn clean install") |
Jon Hall | de9d9aa | 2014-10-08 20:36:02 -0400 | [diff] [blame] | 169 | while 1: |
| 170 | i=self.handle.expect([ |
| 171 | 'There\sis\sinsufficient\smemory\sfor\sthe\sJava\s\ |
| 172 | Runtime\sEnvironment\sto\scontinue', |
| 173 | 'BUILD\sFAILURE', |
| 174 | 'BUILD\sSUCCESS', |
| 175 | 'ONOS\$', |
| 176 | pexpect.TIMEOUT],timeout=600) |
| 177 | if i == 0: |
| 178 | main.log.error(self.name + ":There is insufficient memory \ |
| 179 | for the Java Runtime Environment to continue.") |
| 180 | #return main.FALSE |
| 181 | main.cleanup() |
| 182 | main.exit() |
| 183 | if i == 1: |
| 184 | main.log.error(self.name + ": Build failure!") |
| 185 | #return main.FALSE |
| 186 | main.cleanup() |
| 187 | main.exit() |
| 188 | elif i == 2: |
| 189 | main.log.info(self.name + ": Build success!") |
| 190 | elif i == 3: |
| 191 | main.log.info(self.name + ": Build complete") |
Jon Hall | f8ef52c | 2014-10-09 19:37:33 -0400 | [diff] [blame] | 192 | #Print the build time |
| 193 | for line in self.handle.before.splitlines(): |
| 194 | if "Total time:" in line: |
| 195 | main.log.info(line) |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 196 | self.handle.sendline("") |
Jon Hall | de9d9aa | 2014-10-08 20:36:02 -0400 | [diff] [blame] | 197 | self.handle.expect("\$", timeout=60) |
| 198 | return main.TRUE |
| 199 | elif i == 4: |
| 200 | main.log.error(self.name + ": mvn clean install TIMEOUT!") |
| 201 | #return main.FALSE |
| 202 | main.cleanup() |
| 203 | main.exit() |
| 204 | else: |
| 205 | main.log.error(self.name + ": unexpected response from \ |
| 206 | mvn clean install") |
| 207 | #return main.FALSE |
| 208 | main.cleanup() |
| 209 | main.exit() |
| 210 | except pexpect.EOF: |
| 211 | main.log.error(self.name + ": EOF exception found") |
| 212 | main.log.error(self.name + ": " + self.handle.before) |
| 213 | main.cleanup() |
| 214 | main.exit() |
| 215 | except: |
| 216 | main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::") |
| 217 | main.log.error( traceback.print_exc() ) |
| 218 | main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::") |
| 219 | main.cleanup() |
| 220 | main.exit() |
Jon Hall | acabffd | 2014-10-09 12:36:53 -0400 | [diff] [blame] | 221 | |
| 222 | def git_pull(self, comp1=""): |
| 223 | ''' |
| 224 | Assumes that "git pull" works without login |
Jon Hall | 47a93fb | 2015-01-06 16:46:06 -0800 | [diff] [blame] | 225 | |
Jon Hall | acabffd | 2014-10-09 12:36:53 -0400 | [diff] [blame] | 226 | This function will perform a git pull on the ONOS instance. |
| 227 | If used as git_pull("NODE") it will do git pull + NODE. This is |
| 228 | for the purpose of pulling from other nodes if necessary. |
| 229 | |
Jon Hall | 47a93fb | 2015-01-06 16:46:06 -0800 | [diff] [blame] | 230 | Otherwise, this function will perform a git pull in the |
Jon Hall | acabffd | 2014-10-09 12:36:53 -0400 | [diff] [blame] | 231 | ONOS repository. If it has any problems, it will return main.ERROR |
Shreya Shah | ee15f6c | 2014-10-28 18:12:30 -0400 | [diff] [blame] | 232 | If it successfully does a git_pull, it will return a 1 (main.TRUE) |
| 233 | If it has no updates, it will return 3. |
Jon Hall | acabffd | 2014-10-09 12:36:53 -0400 | [diff] [blame] | 234 | |
| 235 | ''' |
| 236 | try: |
| 237 | # main.log.info(self.name + ": Stopping ONOS") |
| 238 | #self.stop() |
| 239 | self.handle.sendline("cd " + self.home) |
| 240 | self.handle.expect("ONOS\$") |
| 241 | if comp1=="": |
| 242 | self.handle.sendline("git pull") |
| 243 | else: |
| 244 | self.handle.sendline("git pull " + comp1) |
Jon Hall | 47a93fb | 2015-01-06 16:46:06 -0800 | [diff] [blame] | 245 | |
Jon Hall | acabffd | 2014-10-09 12:36:53 -0400 | [diff] [blame] | 246 | i=self.handle.expect(['fatal', |
| 247 | 'Username\sfor\s(.*):\s', |
| 248 | '\sfile(s*) changed,\s', |
| 249 | 'Already up-to-date', |
| 250 | 'Aborting', |
Jon Hall | 47a93fb | 2015-01-06 16:46:06 -0800 | [diff] [blame] | 251 | 'You\sare\snot\scurrently\son\sa\sbranch', |
Jon Hall | acabffd | 2014-10-09 12:36:53 -0400 | [diff] [blame] | 252 | 'You\sasked\sme\sto\spull\swithout\stelling\sme\swhich\sbranch\syou', |
| 253 | 'Pull\sis\snot\spossible\sbecause\syou\shave\sunmerged\sfiles', |
| 254 | pexpect.TIMEOUT], |
| 255 | timeout=300) |
| 256 | #debug |
Jon Hall | 81e29af | 2014-11-04 20:41:23 -0500 | [diff] [blame] | 257 | #main.log.report(self.name +": DEBUG: \n"+"git pull response: " + str(self.handle.before) + str(self.handle.after)) |
Jon Hall | acabffd | 2014-10-09 12:36:53 -0400 | [diff] [blame] | 258 | if i==0: |
| 259 | main.log.error(self.name + ": Git pull had some issue...") |
| 260 | return main.ERROR |
| 261 | elif i==1: |
| 262 | main.log.error(self.name + ": Git Pull Asking for username. ") |
| 263 | return main.ERROR |
| 264 | elif i==2: |
| 265 | main.log.info(self.name + ": Git Pull - pulling repository now") |
| 266 | self.handle.expect("ONOS\$", 120) |
Shreya Shah | ee15f6c | 2014-10-28 18:12:30 -0400 | [diff] [blame] | 267 | return main.TRUE # So that only when git pull is done, we do mvn clean compile |
Jon Hall | acabffd | 2014-10-09 12:36:53 -0400 | [diff] [blame] | 268 | elif i==3: |
| 269 | main.log.info(self.name + ": Git Pull - Already up to date") |
Jon Hall | 47a93fb | 2015-01-06 16:46:06 -0800 | [diff] [blame] | 270 | return i |
Jon Hall | acabffd | 2014-10-09 12:36:53 -0400 | [diff] [blame] | 271 | elif i==4: |
| 272 | main.log.info(self.name + ": Git Pull - Aborting... Are there conflicting git files?") |
| 273 | return main.ERROR |
| 274 | elif i==5: |
| 275 | main.log.info(self.name + ": Git Pull - You are not currently on a branch so git pull failed!") |
| 276 | return main.ERROR |
| 277 | elif i==6: |
| 278 | main.log.info(self.name + ": Git Pull - You have not configured an upstream branch to pull from. Git pull failed!") |
| 279 | return main.ERROR |
| 280 | elif i==7: |
| 281 | main.log.info(self.name + ": Git Pull - Pull is not possible because you have unmerged files.") |
| 282 | return main.ERROR |
| 283 | elif i==8: |
| 284 | main.log.error(self.name + ": Git Pull - TIMEOUT") |
| 285 | main.log.error(self.name + " Response was: " + str(self.handle.before)) |
| 286 | return main.ERROR |
| 287 | else: |
| 288 | main.log.error(self.name + ": Git Pull - Unexpected response, check for pull errors") |
| 289 | return main.ERROR |
| 290 | except pexpect.EOF: |
| 291 | main.log.error(self.name + ": EOF exception found") |
| 292 | main.log.error(self.name + ": " + self.handle.before) |
| 293 | main.cleanup() |
| 294 | main.exit() |
| 295 | except: |
| 296 | main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::") |
| 297 | main.log.error( traceback.print_exc() ) |
| 298 | main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::") |
| 299 | main.cleanup() |
| 300 | main.exit() |
| 301 | |
| 302 | def git_checkout(self, branch="master"): |
| 303 | ''' |
| 304 | Assumes that "git pull" works without login |
| 305 | |
| 306 | This function will perform a git git checkout on the ONOS instance. |
| 307 | If used as git_checkout("branch") it will do git checkout of the "branch". |
| 308 | |
| 309 | Otherwise, this function will perform a git checkout of the master |
| 310 | branch of the ONOS repository. If it has any problems, it will return |
| 311 | main.ERROR. |
| 312 | If the branch was already the specified branch, or the git checkout was |
| 313 | successful then the function will return main.TRUE. |
| 314 | |
| 315 | ''' |
| 316 | try: |
Jon Hall | acabffd | 2014-10-09 12:36:53 -0400 | [diff] [blame] | 317 | self.handle.sendline("cd " + self.home) |
| 318 | self.handle.expect("ONOS\$") |
Jon Hall | 81e29af | 2014-11-04 20:41:23 -0500 | [diff] [blame] | 319 | main.log.info(self.name + ": Checking out git branch: " + branch + "...") |
| 320 | cmd = "git checkout "+branch |
| 321 | self.handle.sendline(cmd) |
| 322 | self.handle.expect(cmd) |
| 323 | i=self.handle.expect(['fatal', |
| 324 | 'Username\sfor\s(.*):\s', |
| 325 | 'Already\son\s\'', |
| 326 | 'Switched\sto\sbranch\s\'' + str(branch), |
| 327 | pexpect.TIMEOUT, |
| 328 | 'error: Your local changes to the following files would be overwritten by checkout:', |
| 329 | 'error: you need to resolve your current index first'],timeout=60) |
Jon Hall | acabffd | 2014-10-09 12:36:53 -0400 | [diff] [blame] | 330 | |
| 331 | if i==0: |
| 332 | main.log.error(self.name + ": Git checkout had some issue...") |
Jon Hall | 81e29af | 2014-11-04 20:41:23 -0500 | [diff] [blame] | 333 | main.log.error(self.name + ": " + self.handle.before) |
Jon Hall | acabffd | 2014-10-09 12:36:53 -0400 | [diff] [blame] | 334 | return main.ERROR |
| 335 | elif i==1: |
Jon Hall | 81e29af | 2014-11-04 20:41:23 -0500 | [diff] [blame] | 336 | main.log.error(self.name + ": Git checkout asking for username."\ |
| 337 | +" Please configure your local git repository to be able "\ |
| 338 | +"to access your remote repository passwordlessly") |
Jon Hall | acabffd | 2014-10-09 12:36:53 -0400 | [diff] [blame] | 339 | return main.ERROR |
| 340 | elif i==2: |
| 341 | main.log.info(self.name + ": Git Checkout %s : Already on this branch" %branch) |
| 342 | self.handle.expect("ONOS\$") |
Jon Hall | 81e29af | 2014-11-04 20:41:23 -0500 | [diff] [blame] | 343 | #main.log.info("DEBUG: after checkout cmd = "+ self.handle.before) |
Jon Hall | acabffd | 2014-10-09 12:36:53 -0400 | [diff] [blame] | 344 | return main.TRUE |
| 345 | elif i==3: |
| 346 | main.log.info(self.name + ": Git checkout %s - Switched to this branch" %branch) |
| 347 | self.handle.expect("ONOS\$") |
Jon Hall | 81e29af | 2014-11-04 20:41:23 -0500 | [diff] [blame] | 348 | #main.log.info("DEBUG: after checkout cmd = "+ self.handle.before) |
Jon Hall | acabffd | 2014-10-09 12:36:53 -0400 | [diff] [blame] | 349 | return main.TRUE |
| 350 | elif i==4: |
| 351 | main.log.error(self.name + ": Git Checkout- TIMEOUT") |
| 352 | main.log.error(self.name + " Response was: " + str(self.handle.before)) |
| 353 | return main.ERROR |
Jon Hall | 81e29af | 2014-11-04 20:41:23 -0500 | [diff] [blame] | 354 | elif i==5: |
| 355 | self.handle.expect("Aborting") |
| 356 | main.log.error(self.name + ": Git checkout error: \n" + \ |
| 357 | "Your local changes to the following files would be overwritten by checkout:" + \ |
| 358 | str(self.handle.before)) |
| 359 | self.handle.expect("ONOS\$") |
| 360 | return main.ERROR |
| 361 | elif i==6: |
| 362 | main.log.error(self.name + ": Git checkout error: \n" + \ |
| 363 | "You need to resolve your current index first:" + \ |
| 364 | str(self.handle.before)) |
| 365 | self.handle.expect("ONOS\$") |
| 366 | return main.ERROR |
Jon Hall | acabffd | 2014-10-09 12:36:53 -0400 | [diff] [blame] | 367 | else: |
| 368 | main.log.error(self.name + ": Git Checkout - Unexpected response, check for pull errors") |
Jon Hall | 81e29af | 2014-11-04 20:41:23 -0500 | [diff] [blame] | 369 | main.log.error(self.name + ": " + self.handle.before) |
Jon Hall | acabffd | 2014-10-09 12:36:53 -0400 | [diff] [blame] | 370 | return main.ERROR |
| 371 | |
| 372 | except pexpect.EOF: |
| 373 | main.log.error(self.name + ": EOF exception found") |
| 374 | main.log.error(self.name + ": " + self.handle.before) |
| 375 | main.cleanup() |
| 376 | main.exit() |
| 377 | except: |
| 378 | main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::") |
| 379 | main.log.error( traceback.print_exc() ) |
| 380 | main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::") |
| 381 | main.cleanup() |
| 382 | main.exit() |
andrewonlab | 95ca146 | 2014-10-09 14:04:24 -0400 | [diff] [blame] | 383 | |
Jon Hall | fd19120 | 2014-11-07 18:36:09 -0500 | [diff] [blame] | 384 | def get_version(self, report=False): |
Jon Hall | 45ec092 | 2014-10-10 19:33:49 -0400 | [diff] [blame] | 385 | ''' |
| 386 | Writes the COMMIT number to the report to be parsed by Jenkins data collecter. |
| 387 | ''' |
| 388 | try: |
| 389 | self.handle.sendline("export TERM=xterm-256color") |
| 390 | self.handle.expect("xterm-256color") |
| 391 | self.handle.expect("\$") |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 392 | self.handle.sendline("") |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 393 | self.handle.expect("\$") |
| 394 | self.handle.sendline("cd " + self.home + "; git log -1 --pretty=fuller --decorate=short | grep -A 6 \"commit\" --color=never") |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 395 | #NOTE: for some reason there are backspaces inserted in this phrase when run from Jenkins on some tests |
Jon Hall | ffb386d | 2014-11-21 13:43:38 -0800 | [diff] [blame] | 396 | self.handle.expect("never") |
Jon Hall | 45ec092 | 2014-10-10 19:33:49 -0400 | [diff] [blame] | 397 | self.handle.expect("\$") |
| 398 | response=(self.name +": \n"+ str(self.handle.before + self.handle.after)) |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 399 | self.handle.sendline("cd " + self.home) |
| 400 | self.handle.expect("\$") |
Jon Hall | 45ec092 | 2014-10-10 19:33:49 -0400 | [diff] [blame] | 401 | lines=response.splitlines() |
| 402 | for line in lines: |
Jon Hall | fd19120 | 2014-11-07 18:36:09 -0500 | [diff] [blame] | 403 | print line |
| 404 | if report: |
| 405 | for line in lines[2:-1]: |
andrewonlab | ac5810a | 2014-11-18 14:33:03 -0500 | [diff] [blame] | 406 | #Bracket replacement is for Wiki-compliant |
| 407 | #formatting. '<' or '>' are interpreted |
| 408 | #as xml specific tags that cause errors |
| 409 | line = line.replace("<","[") |
| 410 | line = line.replace(">","]") |
Jon Hall | b1290e8 | 2014-11-18 16:17:48 -0500 | [diff] [blame] | 411 | main.log.report("\t" + line) |
Jon Hall | fd19120 | 2014-11-07 18:36:09 -0500 | [diff] [blame] | 412 | return lines[2] |
Jon Hall | 45ec092 | 2014-10-10 19:33:49 -0400 | [diff] [blame] | 413 | except pexpect.EOF: |
| 414 | main.log.error(self.name + ": EOF exception found") |
| 415 | main.log.error(self.name + ": " + self.handle.before) |
| 416 | main.cleanup() |
| 417 | main.exit() |
Jon Hall | 368769f | 2014-11-19 15:43:35 -0800 | [diff] [blame] | 418 | except pexpect.TIMEOUT: |
| 419 | main.log.error(self.name + ": TIMEOUT exception found") |
| 420 | main.log.error(self.name + ": " + self.handle.before) |
| 421 | main.cleanup() |
| 422 | main.exit() |
Jon Hall | 45ec092 | 2014-10-10 19:33:49 -0400 | [diff] [blame] | 423 | except: |
| 424 | main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::") |
| 425 | main.log.error( traceback.print_exc() ) |
| 426 | main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::") |
| 427 | main.cleanup() |
| 428 | main.exit() |
| 429 | |
andrewonlab | ba44bcf | 2014-10-16 16:54:41 -0400 | [diff] [blame] | 430 | def create_cell_file(self, bench_ip, file_name, mn_ip_addrs, |
andrewonlab | 3f0a4af | 2014-10-17 12:25:14 -0400 | [diff] [blame] | 431 | extra_feature_string, *onos_ip_addrs): |
andrewonlab | 9428209 | 2014-10-10 13:00:11 -0400 | [diff] [blame] | 432 | ''' |
| 433 | Creates a cell file based on arguments |
| 434 | Required: |
| 435 | * Bench IP address (bench_ip) |
| 436 | - Needed to copy the cell file over |
| 437 | * File name of the cell file (file_name) |
| 438 | * Mininet IP address (mn_ip_addrs) |
| 439 | - Note that only 1 ip address is |
| 440 | supported currently |
| 441 | * ONOS IP addresses (onos_ip_addrs) |
| 442 | - Must be passed in as last arguments |
| 443 | |
| 444 | NOTE: Assumes cells are located at: |
| 445 | ~/<self.home>/tools/test/cells/ |
| 446 | ''' |
| 447 | |
| 448 | #Variable initialization |
| 449 | cell_directory = self.home + "/tools/test/cells/" |
| 450 | #We want to create the cell file in the dependencies directory |
| 451 | #of TestON first, then copy over to ONOS bench |
| 452 | temp_directory = "/tmp/" |
| 453 | #Create the cell file in the directory for writing (w+) |
| 454 | cell_file = open(temp_directory+file_name , 'w+') |
andrewonlab | d494049 | 2014-10-24 12:21:27 -0400 | [diff] [blame] | 455 | |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 456 | #Feature string is hardcoded environment variables |
| 457 | #That you may wish to use by default on startup. |
| 458 | #Note that you may not want certain features listed |
| 459 | #on here. |
andrewonlab | ba44bcf | 2014-10-16 16:54:41 -0400 | [diff] [blame] | 460 | core_feature_string = "export ONOS_FEATURES=webconsole,onos-api,"+\ |
andrewonlab | aedc833 | 2014-12-04 12:43:03 -0500 | [diff] [blame] | 461 | "onos-cli,onos-openflow,"+extra_feature_string |
andrewonlab | 9428209 | 2014-10-10 13:00:11 -0400 | [diff] [blame] | 462 | mn_string = "export OCN=" |
| 463 | onos_string = "export OC" |
| 464 | temp_count = 1 |
| 465 | |
| 466 | #Create ONOS_NIC ip address prefix |
| 467 | temp_onos_ip = onos_ip_addrs[0] |
| 468 | temp_list = [] |
| 469 | temp_list = temp_onos_ip.split(".") |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 470 | #Omit last element of list to format for NIC |
andrewonlab | 9428209 | 2014-10-10 13:00:11 -0400 | [diff] [blame] | 471 | temp_list = temp_list[:-1] |
| 472 | #Structure the nic string ip |
andrewonlab | d494049 | 2014-10-24 12:21:27 -0400 | [diff] [blame] | 473 | nic_addr = ".".join(temp_list) + ".*" |
andrewonlab | 9428209 | 2014-10-10 13:00:11 -0400 | [diff] [blame] | 474 | onos_nic_string = "export ONOS_NIC="+nic_addr |
| 475 | |
| 476 | try: |
| 477 | #Start writing to file |
andrewonlab | d494049 | 2014-10-24 12:21:27 -0400 | [diff] [blame] | 478 | cell_file.write(onos_nic_string + "\n") |
andrewonlab | 9428209 | 2014-10-10 13:00:11 -0400 | [diff] [blame] | 479 | |
| 480 | for arg in onos_ip_addrs: |
| 481 | #For each argument in onos_ip_addrs, write to file |
| 482 | #Output should look like the following: |
andrewonlab | d494049 | 2014-10-24 12:21:27 -0400 | [diff] [blame] | 483 | # export OC1="10.128.20.11" |
| 484 | # export OC2="10.128.20.12" |
andrewonlab | 9428209 | 2014-10-10 13:00:11 -0400 | [diff] [blame] | 485 | cell_file.write(onos_string + str(temp_count) + |
andrewonlab | d494049 | 2014-10-24 12:21:27 -0400 | [diff] [blame] | 486 | "=" + "\"" + arg + "\"" + "\n" ) |
andrewonlab | 9428209 | 2014-10-10 13:00:11 -0400 | [diff] [blame] | 487 | temp_count = temp_count + 1 |
| 488 | |
andrewonlab | d494049 | 2014-10-24 12:21:27 -0400 | [diff] [blame] | 489 | cell_file.write(mn_string +"\""+ mn_ip_addrs +"\""+ "\n") |
| 490 | cell_file.write(core_feature_string + "\n") |
andrewonlab | 9428209 | 2014-10-10 13:00:11 -0400 | [diff] [blame] | 491 | cell_file.close() |
| 492 | |
| 493 | #We use os.system to send the command to TestON cluster |
| 494 | #to account for the case in which TestON is not located |
| 495 | #on the same cluster as the ONOS bench |
| 496 | #Note that even if TestON is located on the same cluster |
| 497 | #as ONOS bench, you must setup passwordless ssh |
andrewonlab | 7e4d2d3 | 2014-10-15 13:23:21 -0400 | [diff] [blame] | 498 | #between TestON and ONOS bench in order to automate the test. |
andrewonlab | 9428209 | 2014-10-10 13:00:11 -0400 | [diff] [blame] | 499 | os.system("scp "+temp_directory+file_name+ |
| 500 | " admin@"+bench_ip+":"+cell_directory) |
| 501 | |
andrewonlab | 2a6c934 | 2014-10-16 13:40:15 -0400 | [diff] [blame] | 502 | return main.TRUE |
| 503 | |
andrewonlab | 9428209 | 2014-10-10 13:00:11 -0400 | [diff] [blame] | 504 | except pexpect.EOF: |
| 505 | main.log.error(self.name + ": EOF exception found") |
| 506 | main.log.error(self.name + ": " + self.handle.before) |
| 507 | main.cleanup() |
| 508 | main.exit() |
| 509 | except: |
| 510 | main.log.info(self.name + ":::::::::") |
| 511 | main.log.error( traceback.print_exc() ) |
| 512 | main.log.info(":::::::") |
| 513 | main.cleanup() |
| 514 | main.exit() |
| 515 | |
andrewonlab | 95ca146 | 2014-10-09 14:04:24 -0400 | [diff] [blame] | 516 | def set_cell(self, cellname): |
| 517 | ''' |
| 518 | Calls 'cell <name>' to set the environment variables on ONOSbench |
| 519 | ''' |
| 520 | try: |
| 521 | if not cellname: |
| 522 | main.log.error("Must define cellname") |
| 523 | main.cleanup() |
| 524 | main.exit() |
| 525 | else: |
| 526 | self.handle.sendline("cell "+str(cellname)) |
| 527 | #Expect the cellname in the ONOS_CELL variable. |
| 528 | #Note that this variable name is subject to change |
| 529 | # and that this driver will have to change accordingly |
| 530 | self.handle.expect("ONOS_CELL="+str(cellname)) |
| 531 | handle_before = self.handle.before |
| 532 | handle_after = self.handle.after |
andrewonlab | c03bf6c | 2014-10-09 14:56:18 -0400 | [diff] [blame] | 533 | #Get the rest of the handle |
| 534 | self.handle.sendline("") |
| 535 | self.handle.expect("\$") |
| 536 | handle_more = self.handle.before |
andrewonlab | 95ca146 | 2014-10-09 14:04:24 -0400 | [diff] [blame] | 537 | |
| 538 | main.log.info("Cell call returned: "+handle_before+ |
andrewonlab | c03bf6c | 2014-10-09 14:56:18 -0400 | [diff] [blame] | 539 | handle_after + handle_more) |
andrewonlab | 95ca146 | 2014-10-09 14:04:24 -0400 | [diff] [blame] | 540 | |
| 541 | return main.TRUE |
| 542 | |
| 543 | except pexpect.EOF: |
| 544 | main.log.error(self.name + ": EOF exception found") |
| 545 | main.log.error(self.name + ": " + self.handle.before) |
| 546 | main.cleanup() |
| 547 | main.exit() |
| 548 | except: |
| 549 | main.log.info(self.name+" ::::::") |
| 550 | main.log.error( traceback.print_exc()) |
| 551 | main.log.info(self.name+" ::::::") |
| 552 | main.cleanup() |
| 553 | main.exit() |
| 554 | |
andrewonlab | c03bf6c | 2014-10-09 14:56:18 -0400 | [diff] [blame] | 555 | def verify_cell(self): |
| 556 | ''' |
| 557 | Calls 'onos-verify-cell' to check for cell installation |
| 558 | ''' |
andrewonlab | 8d0d7d7 | 2014-10-09 16:33:15 -0400 | [diff] [blame] | 559 | #TODO: Add meaningful expect value |
| 560 | |
andrewonlab | c03bf6c | 2014-10-09 14:56:18 -0400 | [diff] [blame] | 561 | try: |
| 562 | #Clean handle by sending empty and expecting $ |
| 563 | self.handle.sendline("") |
| 564 | self.handle.expect("\$") |
| 565 | self.handle.sendline("onos-verify-cell") |
| 566 | self.handle.expect("\$") |
| 567 | handle_before = self.handle.before |
| 568 | handle_after = self.handle.after |
| 569 | #Get the rest of the handle |
| 570 | self.handle.sendline("") |
| 571 | self.handle.expect("\$") |
| 572 | handle_more = self.handle.before |
| 573 | |
| 574 | main.log.info("Verify cell returned: "+handle_before+ |
| 575 | handle_after + handle_more) |
| 576 | |
| 577 | return main.TRUE |
Jon Hall | 7993bfc | 2014-10-09 16:30:14 -0400 | [diff] [blame] | 578 | except pexpect.EOF: |
| 579 | main.log.error(self.name + ": EOF exception found") |
| 580 | main.log.error(self.name + ": " + self.handle.before) |
| 581 | main.cleanup() |
| 582 | main.exit() |
| 583 | except: |
| 584 | main.log.info(self.name+" ::::::") |
| 585 | main.log.error( traceback.print_exc()) |
| 586 | main.log.info(self.name+" ::::::") |
| 587 | main.cleanup() |
| 588 | main.exit() |
| 589 | |
andrewonlab | 05e362f | 2014-10-10 00:40:57 -0400 | [diff] [blame] | 590 | def onos_cli(self, ONOS_ip, cmdstr): |
| 591 | ''' |
| 592 | Uses 'onos' command to send various ONOS CLI arguments. |
| 593 | Required: |
| 594 | * ONOS_ip: specify the ip of the cell machine |
andrewonlab | 9428209 | 2014-10-10 13:00:11 -0400 | [diff] [blame] | 595 | * cmdstr: specify the command string to send |
andrewonlab | 6e20c34 | 2014-10-10 18:08:48 -0400 | [diff] [blame] | 596 | |
| 597 | This function is intended to expose the entire karaf |
| 598 | CLI commands for ONOS. Try to use this function first |
| 599 | before attempting to write a ONOS CLI specific driver |
| 600 | function. |
| 601 | You can see a list of available 'cmdstr' arguments |
| 602 | by starting onos, and typing in 'onos' to enter the |
| 603 | onos> CLI. Then, type 'help' to see the list of |
| 604 | available commands. |
andrewonlab | 05e362f | 2014-10-10 00:40:57 -0400 | [diff] [blame] | 605 | ''' |
| 606 | try: |
| 607 | if not ONOS_ip: |
| 608 | main.log.error("You must specify the IP address") |
| 609 | return main.FALSE |
| 610 | if not cmdstr: |
| 611 | main.log.error("You must specify the command string") |
| 612 | return main.FALSE |
| 613 | |
| 614 | cmdstr = str(cmdstr) |
| 615 | self.handle.sendline("") |
| 616 | self.handle.expect("\$") |
| 617 | |
| 618 | self.handle.sendline("onos -w " + ONOS_ip + " " + cmdstr) |
| 619 | self.handle.expect("\$") |
| 620 | |
Shreya Shah | a73aaad | 2014-10-27 18:03:09 -0400 | [diff] [blame] | 621 | handle_before = self.handle.before |
| 622 | print "handle_before = ", self.handle.before |
Jon Hall | 47a93fb | 2015-01-06 16:46:06 -0800 | [diff] [blame] | 623 | #handle_after = str(self.handle.after) |
| 624 | |
Shreya Shah | a73aaad | 2014-10-27 18:03:09 -0400 | [diff] [blame] | 625 | #self.handle.sendline("") |
| 626 | #self.handle.expect("\$") |
| 627 | #handle_more = str(self.handle.before) |
andrewonlab | 05e362f | 2014-10-10 00:40:57 -0400 | [diff] [blame] | 628 | |
| 629 | main.log.info("Command sent successfully") |
| 630 | |
andrewonlab | 9428209 | 2014-10-10 13:00:11 -0400 | [diff] [blame] | 631 | #Obtain return handle that consists of result from |
| 632 | #the onos command. The string may need to be |
| 633 | #configured further. |
Shreya Shah | a73aaad | 2014-10-27 18:03:09 -0400 | [diff] [blame] | 634 | #return_string = handle_before + handle_after |
| 635 | return_string = handle_before |
| 636 | print "return_string = ", return_string |
andrewonlab | 05e362f | 2014-10-10 00:40:57 -0400 | [diff] [blame] | 637 | return return_string |
| 638 | |
| 639 | except pexpect.EOF: |
| 640 | main.log.error(self.name + ": EOF exception found") |
| 641 | main.log.error(self.name + ": " + self.handle.before) |
| 642 | main.cleanup() |
| 643 | main.exit() |
| 644 | except: |
| 645 | main.log.info(self.name+" ::::::") |
| 646 | main.log.error( traceback.print_exc()) |
| 647 | main.log.info(self.name+" ::::::") |
| 648 | main.cleanup() |
| 649 | main.exit() |
Jon Hall | 7993bfc | 2014-10-09 16:30:14 -0400 | [diff] [blame] | 650 | |
| 651 | def onos_install(self, options="-f", node = ""): |
| 652 | ''' |
| 653 | Installs ONOS bits on the designated cell machine. |
| 654 | If -f option is provided, it also forces an uninstall. |
| 655 | Presently, install also includes onos-push-bits and |
| 656 | onos-config within. |
| 657 | The node option allows you to selectively only push the jar |
| 658 | files to certain onos nodes |
| 659 | |
| 660 | Returns: main.TRUE on success and main.FALSE on failure |
| 661 | ''' |
| 662 | try: |
andrewonlab | 114768a | 2014-11-14 12:44:44 -0500 | [diff] [blame] | 663 | if options: |
| 664 | self.handle.sendline("onos-install " + options + " " + node) |
| 665 | else: |
| 666 | self.handle.sendline("onos-install "+node) |
Jon Hall | 7993bfc | 2014-10-09 16:30:14 -0400 | [diff] [blame] | 667 | self.handle.expect("onos-install ") |
| 668 | #NOTE: this timeout may need to change depending on the network and size of ONOS |
| 669 | i=self.handle.expect(["Network\sis\sunreachable", |
| 670 | "onos\sstart/running,\sprocess", |
andrewonlab | d9a73a7 | 2014-11-14 17:28:21 -0500 | [diff] [blame] | 671 | "ONOS\sis\salready\sinstalled", |
Jon Hall | 7993bfc | 2014-10-09 16:30:14 -0400 | [diff] [blame] | 672 | pexpect.TIMEOUT],timeout=60) |
| 673 | |
Jon Hall | 7993bfc | 2014-10-09 16:30:14 -0400 | [diff] [blame] | 674 | if i == 0: |
| 675 | main.log.warn("Network is unreachable") |
| 676 | return main.FALSE |
| 677 | elif i == 1: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 678 | main.log.info("ONOS was installed on " + node + " and started") |
Jon Hall | 7993bfc | 2014-10-09 16:30:14 -0400 | [diff] [blame] | 679 | return main.TRUE |
andrewonlab | d9a73a7 | 2014-11-14 17:28:21 -0500 | [diff] [blame] | 680 | elif i == 2: |
| 681 | main.log.info("ONOS is already installed on "+node) |
| 682 | return main.TRUE |
| 683 | elif i == 3: |
Jon Hall | 42db6dc | 2014-10-24 19:03:48 -0400 | [diff] [blame] | 684 | main.log.info("Installation of ONOS on " + node + " timed out") |
Jon Hall | 7993bfc | 2014-10-09 16:30:14 -0400 | [diff] [blame] | 685 | return main.FALSE |
andrewonlab | c03bf6c | 2014-10-09 14:56:18 -0400 | [diff] [blame] | 686 | |
andrewonlab | d9a73a7 | 2014-11-14 17:28:21 -0500 | [diff] [blame] | 687 | |
andrewonlab | c03bf6c | 2014-10-09 14:56:18 -0400 | [diff] [blame] | 688 | except pexpect.EOF: |
| 689 | main.log.error(self.name + ": EOF exception found") |
| 690 | main.log.error(self.name + ": " + self.handle.before) |
| 691 | main.cleanup() |
| 692 | main.exit() |
| 693 | except: |
| 694 | main.log.info(self.name+" ::::::") |
| 695 | main.log.error( traceback.print_exc()) |
| 696 | main.log.info(self.name+" ::::::") |
| 697 | main.cleanup() |
| 698 | main.exit() |
andrewonlab | 95ca146 | 2014-10-09 14:04:24 -0400 | [diff] [blame] | 699 | |
andrewonlab | 8d0d7d7 | 2014-10-09 16:33:15 -0400 | [diff] [blame] | 700 | def onos_start(self, node_ip): |
| 701 | ''' |
| 702 | Calls onos command: 'onos-service [<node-ip>] start' |
andrewonlab | e8e56fd | 2014-10-09 17:12:44 -0400 | [diff] [blame] | 703 | This command is a remote management of the ONOS upstart daemon |
andrewonlab | 8d0d7d7 | 2014-10-09 16:33:15 -0400 | [diff] [blame] | 704 | ''' |
| 705 | |
| 706 | try: |
| 707 | self.handle.sendline("") |
| 708 | self.handle.expect("\$") |
| 709 | self.handle.sendline("onos-service "+str(node_ip)+ |
| 710 | " start") |
| 711 | i = self.handle.expect([ |
| 712 | "Job\sis\salready\srunning", |
| 713 | "start/running", |
| 714 | "Unknown\sinstance", |
Shreya Shah | d01153d | 2014-10-23 15:08:56 -0400 | [diff] [blame] | 715 | pexpect.TIMEOUT],timeout=120) |
andrewonlab | 8d0d7d7 | 2014-10-09 16:33:15 -0400 | [diff] [blame] | 716 | |
| 717 | if i == 0: |
| 718 | main.log.info("Service is already running") |
| 719 | return main.TRUE |
| 720 | elif i == 1: |
| 721 | main.log.info("ONOS service started") |
| 722 | return main.TRUE |
| 723 | else: |
| 724 | main.log.error("ONOS service failed to start") |
| 725 | main.cleanup() |
| 726 | main.exit() |
andrewonlab | 8d0d7d7 | 2014-10-09 16:33:15 -0400 | [diff] [blame] | 727 | except pexpect.EOF: |
| 728 | main.log.error(self.name + ": EOF exception found") |
| 729 | main.log.error(self.name + ": " + self.handle.before) |
| 730 | main.cleanup() |
| 731 | main.exit() |
| 732 | except: |
| 733 | main.log.info(self.name+" ::::::") |
| 734 | main.log.error( traceback.print_exc()) |
| 735 | main.log.info(self.name+" ::::::") |
| 736 | main.cleanup() |
| 737 | main.exit() |
| 738 | |
andrewonlab | 2b30bd3 | 2014-10-09 16:48:55 -0400 | [diff] [blame] | 739 | def onos_stop(self, node_ip): |
| 740 | ''' |
| 741 | Calls onos command: 'onos-service [<node-ip>] stop' |
andrewonlab | e8e56fd | 2014-10-09 17:12:44 -0400 | [diff] [blame] | 742 | This command is a remote management of the ONOS upstart daemon |
andrewonlab | 2b30bd3 | 2014-10-09 16:48:55 -0400 | [diff] [blame] | 743 | ''' |
| 744 | try: |
| 745 | self.handle.sendline("") |
| 746 | self.handle.expect("\$") |
| 747 | self.handle.sendline("onos-service "+str(node_ip)+ |
| 748 | " stop") |
| 749 | i = self.handle.expect([ |
| 750 | "stop/waiting", |
| 751 | "Unknown\sinstance", |
| 752 | pexpect.TIMEOUT],timeout=60) |
| 753 | |
| 754 | if i == 0: |
| 755 | main.log.info("ONOS service stopped") |
| 756 | return main.TRUE |
| 757 | elif i == 1: |
| 758 | main.log.info("Unknown ONOS instance specified: "+ |
| 759 | str(node_ip)) |
| 760 | return main.FALSE |
| 761 | else: |
| 762 | main.log.error("ONOS service failed to stop") |
| 763 | return main.FALSE |
| 764 | |
| 765 | except pexpect.EOF: |
| 766 | main.log.error(self.name + ": EOF exception found") |
| 767 | main.log.error(self.name + ": " + self.handle.before) |
| 768 | main.cleanup() |
| 769 | main.exit() |
| 770 | except: |
| 771 | main.log.info(self.name+" ::::::") |
| 772 | main.log.error( traceback.print_exc()) |
| 773 | main.log.info(self.name+" ::::::") |
| 774 | main.cleanup() |
| 775 | main.exit() |
andrewonlab | 99d3f7f | 2014-11-13 17:45:08 -0500 | [diff] [blame] | 776 | |
andrewonlab | a6f7d88 | 2014-11-13 17:32:24 -0500 | [diff] [blame] | 777 | def onos_uninstall(self, node_ip=""): |
andrewonlab | c8d4797 | 2014-10-09 16:52:36 -0400 | [diff] [blame] | 778 | ''' |
| 779 | Calls the command: 'onos-uninstall' |
andrewonlab | e8e56fd | 2014-10-09 17:12:44 -0400 | [diff] [blame] | 780 | Uninstalls ONOS from the designated cell machine, stopping |
| 781 | if needed |
andrewonlab | c8d4797 | 2014-10-09 16:52:36 -0400 | [diff] [blame] | 782 | ''' |
| 783 | try: |
| 784 | self.handle.sendline("") |
| 785 | self.handle.expect("\$") |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 786 | self.handle.sendline( "onos-uninstall "+str(node_ip) ) |
andrewonlab | c8d4797 | 2014-10-09 16:52:36 -0400 | [diff] [blame] | 787 | self.handle.expect("\$") |
| 788 | |
andrewonlab | 9dfd208 | 2014-11-13 17:44:03 -0500 | [diff] [blame] | 789 | main.log.info("ONOS "+node_ip+" was uninstalled") |
| 790 | |
andrewonlab | c8d4797 | 2014-10-09 16:52:36 -0400 | [diff] [blame] | 791 | #onos-uninstall command does not return any text |
| 792 | return main.TRUE |
| 793 | |
| 794 | except pexpect.EOF: |
| 795 | main.log.error(self.name + ": EOF exception found") |
| 796 | main.log.error(self.name + ": " + self.handle.before) |
| 797 | main.cleanup() |
| 798 | main.exit() |
| 799 | except: |
| 800 | main.log.info(self.name+" ::::::") |
| 801 | main.log.error( traceback.print_exc()) |
| 802 | main.log.info(self.name+" ::::::") |
| 803 | main.cleanup() |
| 804 | main.exit() |
andrewonlab | 2b30bd3 | 2014-10-09 16:48:55 -0400 | [diff] [blame] | 805 | |
andrewonlab | aedc833 | 2014-12-04 12:43:03 -0500 | [diff] [blame] | 806 | def onos_die(self, node_ip): |
| 807 | ''' |
| 808 | Issues the command 'onos-die <node-ip>' |
| 809 | This command calls onos-kill and also stops the node |
| 810 | ''' |
| 811 | try: |
| 812 | self.handle.sendline("") |
| 813 | self.handle.expect("\$") |
| 814 | cmd_str = "onos-kill "+str(node_ip) |
| 815 | self.handle.sendline(cmd_str) |
| 816 | i = self.handle.expect([ |
| 817 | "Killing\sONOS", |
| 818 | "ONOS\sprocess\sis\snot\srunning", |
| 819 | pexpect.TIMEOUT], timeout=20) |
| 820 | if i == 0: |
| 821 | main.log.info("ONOS instance "+str(node_ip)+ |
| 822 | " was killed and stopped") |
| 823 | return main.TRUE |
| 824 | elif i == 1: |
| 825 | main.log.info("ONOS process was not running") |
| 826 | return main.FALSE |
| 827 | except pexpect.EOF: |
| 828 | main.log.error(self.name + ": EOF exception found") |
| 829 | main.log.error(self.name + ": " + self.handle.before) |
| 830 | main.cleanup() |
| 831 | main.exit() |
| 832 | except: |
| 833 | main.log.info(self.name+" ::::::") |
| 834 | main.log.error( traceback.print_exc()) |
| 835 | main.log.info(self.name+" ::::::") |
| 836 | main.cleanup() |
| 837 | main.exit() |
| 838 | |
| 839 | |
andrewonlab | e8e56fd | 2014-10-09 17:12:44 -0400 | [diff] [blame] | 840 | def onos_kill(self, node_ip): |
| 841 | ''' |
| 842 | Calls the command: 'onos-kill [<node-ip>]' |
| 843 | "Remotely, and unceremoniously kills the ONOS instance running on |
| 844 | the specified cell machine" - Tom V |
| 845 | ''' |
| 846 | |
| 847 | try: |
| 848 | self.handle.sendline("") |
| 849 | self.handle.expect("\$") |
| 850 | self.handle.sendline("onos-kill " + str(node_ip)) |
| 851 | i = self.handle.expect([ |
| 852 | "\$", |
| 853 | "No\sroute\sto\shost", |
| 854 | "password:", |
| 855 | pexpect.TIMEOUT], timeout=20) |
| 856 | |
| 857 | if i == 0: |
| 858 | main.log.info("ONOS instance "+str(node_ip)+" was killed") |
| 859 | return main.TRUE |
| 860 | elif i == 1: |
| 861 | main.log.info("No route to host") |
| 862 | return main.FALSE |
| 863 | elif i == 2: |
| 864 | main.log.info("Passwordless login for host: "+str(node_ip)+ |
| 865 | " not configured") |
| 866 | return main.FALSE |
| 867 | else: |
| 868 | main.log.info("ONOS instasnce was not killed") |
| 869 | return main.FALSE |
| 870 | |
| 871 | except pexpect.EOF: |
| 872 | main.log.error(self.name + ": EOF exception found") |
| 873 | main.log.error(self.name + ": " + self.handle.before) |
| 874 | main.cleanup() |
| 875 | main.exit() |
| 876 | except: |
| 877 | main.log.info(self.name+" ::::::") |
| 878 | main.log.error( traceback.print_exc()) |
| 879 | main.log.info(self.name+" ::::::") |
| 880 | main.cleanup() |
| 881 | main.exit() |
| 882 | |
andrewonlab | 19fbdca | 2014-11-14 12:55:59 -0500 | [diff] [blame] | 883 | def onos_remove_raft_logs(self): |
| 884 | ''' |
| 885 | Removes Raft / Copy cat files from ONOS to ensure |
Jon Hall | fcc8862 | 2014-11-25 13:09:54 -0500 | [diff] [blame] | 886 | a cleaner environment. |
| 887 | |
andrewonlab | 19fbdca | 2014-11-14 12:55:59 -0500 | [diff] [blame] | 888 | Description: |
Jon Hall | fcc8862 | 2014-11-25 13:09:54 -0500 | [diff] [blame] | 889 | Stops all ONOS defined in the cell, |
andrewonlab | 19fbdca | 2014-11-14 12:55:59 -0500 | [diff] [blame] | 890 | wipes the raft / copycat log files |
| 891 | ''' |
| 892 | try: |
| 893 | self.handle.sendline("") |
| 894 | self.handle.expect("\$") |
| 895 | self.handle.sendline("onos-remove-raft-logs") |
Jon Hall | fcc8862 | 2014-11-25 13:09:54 -0500 | [diff] [blame] | 896 | #Sometimes this command hangs |
andrewonlab | eb7b107 | 2014-11-25 15:50:07 -0500 | [diff] [blame] | 897 | i = self.handle.expect(["\$", pexpect.TIMEOUT], |
| 898 | timeout=120) |
Jon Hall | fcc8862 | 2014-11-25 13:09:54 -0500 | [diff] [blame] | 899 | if i == 1: |
andrewonlab | eb7b107 | 2014-11-25 15:50:07 -0500 | [diff] [blame] | 900 | i = self.handle.expect(["\$", pexpect.TIMEOUT], |
| 901 | timeout=120) |
Jon Hall | fcc8862 | 2014-11-25 13:09:54 -0500 | [diff] [blame] | 902 | if i == 1: |
| 903 | return main.FALSE |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 904 | self.handle.sendline("") |
| 905 | self.handle.expect("\$") |
andrewonlab | 19fbdca | 2014-11-14 12:55:59 -0500 | [diff] [blame] | 906 | return main.TRUE |
| 907 | |
| 908 | except pexpect.EOF: |
| 909 | main.log.error(self.name + ": EOF exception found") |
| 910 | main.log.error(self.name + ": " + self.handle.before) |
| 911 | main.cleanup() |
| 912 | main.exit() |
| 913 | except: |
| 914 | main.log.info(self.name+" ::::::") |
| 915 | main.log.error( traceback.print_exc()) |
| 916 | main.log.info(self.name+" ::::::") |
| 917 | main.cleanup() |
| 918 | main.exit() |
Jon Hall | fcc8862 | 2014-11-25 13:09:54 -0500 | [diff] [blame] | 919 | |
andrewonlab | 9428209 | 2014-10-10 13:00:11 -0400 | [diff] [blame] | 920 | def onos_start_network(self, mntopo): |
| 921 | ''' |
| 922 | Calls the command 'onos-start-network [<mininet-topo>] |
| 923 | "remotely starts the specified topology on the cell's |
| 924 | mininet machine against all controllers configured in the |
| 925 | cell." |
| 926 | * Specify mininet topology file name for mntopo |
| 927 | * Topo files should be placed at: |
| 928 | ~/<your-onos-directory>/tools/test/topos |
| 929 | |
| 930 | NOTE: This function will take you to the mininet prompt |
| 931 | ''' |
| 932 | try: |
| 933 | if not mntopo: |
| 934 | main.log.error("You must specify a topo file to execute") |
| 935 | return main.FALSE |
| 936 | |
| 937 | mntopo = str(mntopo) |
| 938 | self.handle.sendline("") |
| 939 | self.handle.expect("\$") |
| 940 | |
| 941 | self.handle.sendline("onos-start-network " + mntopo) |
| 942 | self.handle.expect("mininet>") |
| 943 | main.log.info("Network started, entered mininet prompt") |
| 944 | |
| 945 | #TODO: Think about whether return is necessary or not |
| 946 | |
| 947 | except pexpect.EOF: |
| 948 | main.log.error(self.name + ": EOF exception found") |
| 949 | main.log.error(self.name + ": " + self.handle.before) |
| 950 | main.cleanup() |
| 951 | main.exit() |
| 952 | except: |
| 953 | main.log.info(self.name+" ::::::") |
| 954 | main.log.error( traceback.print_exc()) |
| 955 | main.log.info(self.name+" ::::::") |
| 956 | main.cleanup() |
| 957 | main.exit() |
| 958 | |
| 959 | |
Jon Hall | 7993bfc | 2014-10-09 16:30:14 -0400 | [diff] [blame] | 960 | def isup(self, node = ""): |
| 961 | ''' |
Jon Hall | 77f53ce | 2014-10-13 18:02:06 -0400 | [diff] [blame] | 962 | Run's onos-wait-for-start which only returns once ONOS is at run |
| 963 | level 100(ready for use) |
andrewonlab | 8d0d7d7 | 2014-10-09 16:33:15 -0400 | [diff] [blame] | 964 | |
Jon Hall | 7993bfc | 2014-10-09 16:30:14 -0400 | [diff] [blame] | 965 | Returns: main.TRUE if ONOS is running and main.FALSE on timeout |
| 966 | ''' |
| 967 | try: |
| 968 | self.handle.sendline("onos-wait-for-start " + node ) |
| 969 | self.handle.expect("onos-wait-for-start") |
| 970 | #NOTE: this timeout is arbitrary" |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 971 | i = self.handle.expect(["\$", pexpect.TIMEOUT], timeout = 120) |
Jon Hall | 7993bfc | 2014-10-09 16:30:14 -0400 | [diff] [blame] | 972 | if i == 0: |
| 973 | main.log.info(self.name + ": " + node + " is up") |
| 974 | return main.TRUE |
| 975 | elif i == 1: |
| 976 | #NOTE: since this function won't return until ONOS is ready, |
| 977 | # we will kill it on timeout |
Jon Hall | 73cf9cc | 2014-11-20 22:28:38 -0800 | [diff] [blame] | 978 | main.log.error("ONOS has not started yet") |
| 979 | self.handle.send("\x03") #Control-C |
Jon Hall | 7993bfc | 2014-10-09 16:30:14 -0400 | [diff] [blame] | 980 | self.handle.expect("\$") |
| 981 | return main.FALSE |
| 982 | except pexpect.EOF: |
| 983 | main.log.error(self.name + ": EOF exception found") |
| 984 | main.log.error(self.name + ": " + self.handle.before) |
| 985 | main.cleanup() |
| 986 | main.exit() |
| 987 | except: |
| 988 | main.log.info(self.name+" ::::::") |
| 989 | main.log.error( traceback.print_exc()) |
| 990 | main.log.info(self.name+" ::::::") |
| 991 | main.cleanup() |
| 992 | main.exit() |
andrewonlab | 05e362f | 2014-10-10 00:40:57 -0400 | [diff] [blame] | 993 | |
andrewonlab | b66dfa1 | 2014-12-02 15:51:10 -0500 | [diff] [blame] | 994 | def push_test_intents_shell(self, dpid_src, dpid_dst, num_intents, |
andrewonlab | aedc833 | 2014-12-04 12:43:03 -0500 | [diff] [blame] | 995 | dir_file, onos_ip, num_mult="", app_id="", report=True, |
| 996 | options=""): |
andrewonlab | b66dfa1 | 2014-12-02 15:51:10 -0500 | [diff] [blame] | 997 | ''' |
| 998 | Description: |
| 999 | Use the linux prompt to push test intents to |
| 1000 | better parallelize the results than the CLI |
| 1001 | Required: |
| 1002 | * dpid_src: specify source dpid |
| 1003 | * dpid_dst: specify destination dpid |
| 1004 | * num_intents: specify number of intents to push |
| 1005 | * dir_file: specify directory and file name to save |
| 1006 | results |
| 1007 | * onos_ip: specify the IP of ONOS to install on |
| 1008 | NOTE: |
| 1009 | You must invoke this command at linux shell prompt |
| 1010 | ''' |
| 1011 | try: |
| 1012 | #Create the string to sendline |
andrewonlab | aedc833 | 2014-12-04 12:43:03 -0500 | [diff] [blame] | 1013 | if options: |
| 1014 | base_cmd = "onos "+str(onos_ip)+" push-test-intents "+\ |
| 1015 | options+" " |
| 1016 | else: |
| 1017 | base_cmd = "onos "+str(onos_ip)+" push-test-intents " |
| 1018 | |
andrewonlab | b66dfa1 | 2014-12-02 15:51:10 -0500 | [diff] [blame] | 1019 | add_dpid = base_cmd + str(dpid_src) + " " + str(dpid_dst) |
| 1020 | if not num_mult: |
| 1021 | add_intents = add_dpid + " " + str(num_intents) |
| 1022 | elif num_mult: |
| 1023 | add_intents = add_dpid + " " + str(num_intents) + " " +\ |
| 1024 | str(num_mult) |
| 1025 | if app_id: |
| 1026 | add_app = add_intents + " " + str(app_id) |
| 1027 | else: |
| 1028 | add_app = add_intents |
| 1029 | |
andrewonlab | aedc833 | 2014-12-04 12:43:03 -0500 | [diff] [blame] | 1030 | if report: |
| 1031 | send_cmd = add_app + " > " + str(dir_file) + " &" |
| 1032 | else: |
| 1033 | send_cmd = add_app + " &" |
andrewonlab | b66dfa1 | 2014-12-02 15:51:10 -0500 | [diff] [blame] | 1034 | main.log.info("Send cmd: "+send_cmd) |
| 1035 | |
| 1036 | self.handle.sendline(send_cmd) |
| 1037 | |
| 1038 | except pexpect.EOF: |
| 1039 | main.log.error(self.name + ": EOF exception found") |
| 1040 | main.log.error(self.name + ": " + self.handle.before) |
| 1041 | main.cleanup() |
| 1042 | main.exit() |
| 1043 | except: |
| 1044 | main.log.info(self.name+" ::::::") |
| 1045 | main.log.error( traceback.print_exc()) |
| 1046 | main.log.info(self.name+" ::::::") |
| 1047 | main.cleanup() |
| 1048 | main.exit() |
andrewonlab | 05e362f | 2014-10-10 00:40:57 -0400 | [diff] [blame] | 1049 | |
Shreya Shah | a73aaad | 2014-10-27 18:03:09 -0400 | [diff] [blame] | 1050 | def get_topology(self,topology_output): |
Jon Hall | 77f53ce | 2014-10-13 18:02:06 -0400 | [diff] [blame] | 1051 | ''' |
| 1052 | parses the onos:topology output |
| 1053 | Returns: a topology dict populated by the key values found in |
| 1054 | the cli command. |
| 1055 | ''' |
| 1056 | |
| 1057 | try: |
| 1058 | #call the cli to get the topology summary |
Shreya Shah | a73aaad | 2014-10-27 18:03:09 -0400 | [diff] [blame] | 1059 | #cmdstr = "onos:topology" |
| 1060 | #cli_result = self.onos_cli(ip, cmdstr) |
| 1061 | #print "cli_result = ", cli_result |
Jon Hall | 77f53ce | 2014-10-13 18:02:06 -0400 | [diff] [blame] | 1062 | |
| 1063 | #Parse the output |
| 1064 | topology = {} |
| 1065 | #for line in cli_result.split("\n"): |
Shreya Shah | a73aaad | 2014-10-27 18:03:09 -0400 | [diff] [blame] | 1066 | for line in topology_output.splitlines(): |
Jon Hall | 77f53ce | 2014-10-13 18:02:06 -0400 | [diff] [blame] | 1067 | if not line.startswith("time="): |
| 1068 | continue |
| 1069 | #else |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1070 | #print line |
Jon Hall | 77f53ce | 2014-10-13 18:02:06 -0400 | [diff] [blame] | 1071 | for var in line.split(","): |
Shreya Shah | a73aaad | 2014-10-27 18:03:09 -0400 | [diff] [blame] | 1072 | #print "'"+var+"'" |
| 1073 | #print "'"+var.strip()+"'" |
Jon Hall | 77f53ce | 2014-10-13 18:02:06 -0400 | [diff] [blame] | 1074 | key, value = var.strip().split("=") |
| 1075 | topology[key] = value |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1076 | #print "topology = ", topology |
Jon Hall | 47a93fb | 2015-01-06 16:46:06 -0800 | [diff] [blame] | 1077 | #devices = topology.get('devices', False) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1078 | #print "devices = ", devices |
Jon Hall | 47a93fb | 2015-01-06 16:46:06 -0800 | [diff] [blame] | 1079 | #links = topology.get('links', False) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1080 | #print "links = ", links |
Jon Hall | 47a93fb | 2015-01-06 16:46:06 -0800 | [diff] [blame] | 1081 | #SCCs = topology.get('SCC(s)', False) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1082 | #print "SCCs = ", SCCs |
Jon Hall | 47a93fb | 2015-01-06 16:46:06 -0800 | [diff] [blame] | 1083 | #paths = topology.get('paths', False) |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1084 | #print "paths = ", paths |
Jon Hall | 77f53ce | 2014-10-13 18:02:06 -0400 | [diff] [blame] | 1085 | |
| 1086 | return topology |
| 1087 | except pexpect.EOF: |
| 1088 | main.log.error(self.name + ": EOF exception found") |
| 1089 | main.log.error(self.name + ": " + self.handle.before) |
| 1090 | main.cleanup() |
| 1091 | main.exit() |
| 1092 | except: |
| 1093 | main.log.info(self.name+" ::::::") |
| 1094 | main.log.error( traceback.print_exc()) |
| 1095 | main.log.info(self.name+" ::::::") |
| 1096 | main.cleanup() |
| 1097 | main.exit() |
Shreya Shah | a73aaad | 2014-10-27 18:03:09 -0400 | [diff] [blame] | 1098 | |
| 1099 | |
Jon Hall | 77f53ce | 2014-10-13 18:02:06 -0400 | [diff] [blame] | 1100 | |
Shreya Shah | a73aaad | 2014-10-27 18:03:09 -0400 | [diff] [blame] | 1101 | def check_status(self, topology_result, numoswitch, numolink, log_level="info"): |
Jon Hall | 77f53ce | 2014-10-13 18:02:06 -0400 | [diff] [blame] | 1102 | ''' |
| 1103 | Checks the number of swithes & links that ONOS sees against the |
| 1104 | supplied values. By default this will report to main.log, but the |
| 1105 | log level can be specifid. |
| 1106 | |
| 1107 | Params: ip = ip used for the onos cli |
| 1108 | numoswitch = expected number of switches |
| 1109 | numlink = expected number of links |
| 1110 | log_level = level to log to. Currently accepts 'info', 'warn' and 'report' |
| 1111 | |
| 1112 | |
| 1113 | log_level can |
| 1114 | |
| 1115 | Returns: main.TRUE if the number of switchs and links are correct, |
| 1116 | main.FALSE if the numer of switches and links is incorrect, |
| 1117 | and main.ERROR otherwise |
| 1118 | ''' |
| 1119 | |
| 1120 | try: |
Shreya Shah | a73aaad | 2014-10-27 18:03:09 -0400 | [diff] [blame] | 1121 | topology = self.get_topology(topology_result) |
Jon Hall | 77f53ce | 2014-10-13 18:02:06 -0400 | [diff] [blame] | 1122 | if topology == {}: |
| 1123 | return main.ERROR |
| 1124 | output = "" |
| 1125 | #Is the number of switches is what we expected |
| 1126 | devices = topology.get('devices',False) |
| 1127 | links = topology.get('links',False) |
| 1128 | if devices == False or links == False: |
| 1129 | return main.ERROR |
| 1130 | switch_check = ( int(devices) == int(numoswitch) ) |
| 1131 | #Is the number of links is what we expected |
| 1132 | link_check = ( int(links) == int(numolink) ) |
| 1133 | if (switch_check and link_check): |
| 1134 | #We expected the correct numbers |
| 1135 | output = output + "The number of links and switches match "\ |
| 1136 | + "what was expected" |
| 1137 | result = main.TRUE |
| 1138 | else: |
| 1139 | output = output + \ |
| 1140 | "The number of links and switches does not match what was expected" |
| 1141 | result = main.FALSE |
| 1142 | output = output + "\n ONOS sees %i devices (%i expected) and %i links (%i expected)"\ |
| 1143 | % ( int(devices), int(numoswitch), int(links), int(numolink) ) |
| 1144 | if log_level == "report": |
| 1145 | main.log.report(output) |
| 1146 | elif log_level == "warn": |
| 1147 | main.log.warn(output) |
| 1148 | else: |
| 1149 | main.log.info(output) |
| 1150 | return result |
| 1151 | except pexpect.EOF: |
| 1152 | main.log.error(self.name + ": EOF exception found") |
| 1153 | main.log.error(self.name + ": " + self.handle.before) |
| 1154 | main.cleanup() |
| 1155 | main.exit() |
| 1156 | except: |
| 1157 | main.log.info(self.name+" ::::::") |
| 1158 | main.log.error( traceback.print_exc()) |
| 1159 | main.log.info(self.name+" ::::::") |
| 1160 | main.cleanup() |
| 1161 | main.exit() |
andrewonlab | ba44bcf | 2014-10-16 16:54:41 -0400 | [diff] [blame] | 1162 | |
andrewonlab | 970399c | 2014-11-07 13:09:32 -0500 | [diff] [blame] | 1163 | def tshark_pcap(self, interface, dir_file): |
| 1164 | ''' |
| 1165 | Capture all packet activity and store in specified |
| 1166 | directory/file |
| 1167 | |
| 1168 | Required: |
| 1169 | * interface: interface to capture |
| 1170 | * dir: directory/filename to store pcap |
| 1171 | ''' |
| 1172 | self.handle.sendline("") |
| 1173 | self.handle.expect("\$") |
| 1174 | |
| 1175 | self.handle.sendline("tshark -i "+str(interface)+ |
andrewonlab | 5d7a8f3 | 2014-11-10 13:07:56 -0500 | [diff] [blame] | 1176 | " -t e -w "+str(dir_file)+ " &") |
| 1177 | self.handle.sendline("\r") |
andrewonlab | 970399c | 2014-11-07 13:09:32 -0500 | [diff] [blame] | 1178 | self.handle.expect("Capturing on") |
andrewonlab | 5d7a8f3 | 2014-11-10 13:07:56 -0500 | [diff] [blame] | 1179 | self.handle.sendline("\r") |
| 1180 | self.handle.expect("\$") |
andrewonlab | 970399c | 2014-11-07 13:09:32 -0500 | [diff] [blame] | 1181 | |
| 1182 | main.log.info("Tshark started capturing files on "+ |
| 1183 | str(interface)+ " and saving to directory: "+ |
andrewonlab | 5d7a8f3 | 2014-11-10 13:07:56 -0500 | [diff] [blame] | 1184 | str(dir_file)) |
Shreya Shah | a73aaad | 2014-10-27 18:03:09 -0400 | [diff] [blame] | 1185 | |
shahshreya | 4d79aab | 2014-11-07 15:20:41 -0800 | [diff] [blame] | 1186 | |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 1187 | def run_onos_topo_cfg(self, instance_name, json_file): |
shahshreya | 4d79aab | 2014-11-07 15:20:41 -0800 | [diff] [blame] | 1188 | ''' |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 1189 | On ONOS bench, run this command: ./~/ONOS/tools/test/bin/onos-topo-cfg $OC1 filename |
shahshreya | 4d79aab | 2014-11-07 15:20:41 -0800 | [diff] [blame] | 1190 | which starts the rest and copies the json file to the onos instance |
| 1191 | ''' |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 1192 | try: |
| 1193 | self.handle.sendline("") |
| 1194 | self.handle.expect("\$") |
| 1195 | self.handle.sendline("cd ~/ONOS/tools/test/bin") |
| 1196 | self.handle.expect("/bin$") |
| 1197 | cmd = "./onos-topo-cfg " +instance_name +" " +json_file |
| 1198 | print "cmd = ", cmd |
| 1199 | self.handle.sendline(cmd) |
shahshreya | d0c8043 | 2014-12-04 16:56:05 -0800 | [diff] [blame] | 1200 | self.handle.expect("\$") |
shahshreya | e6c7cf4 | 2014-11-26 16:39:01 -0800 | [diff] [blame] | 1201 | self.handle.sendline("cd ~") |
| 1202 | self.handle.expect("\$") |
| 1203 | return main.TRUE |
| 1204 | except: |
| 1205 | return main.FALSE |
| 1206 | |
andrewonlab | ba44bcf | 2014-10-16 16:54:41 -0400 | [diff] [blame] | 1207 | def tshark_grep(self, grep, directory, interface='eth0'): |
| 1208 | ''' |
| 1209 | Required: |
| 1210 | * grep string |
| 1211 | * directory to store results |
| 1212 | Optional: |
| 1213 | * interface - default: eth0 |
| 1214 | Description: |
| 1215 | Uses tshark command to grep specific group of packets |
| 1216 | and stores the results to specified directory. |
| 1217 | The timestamp is hardcoded to be in epoch |
| 1218 | ''' |
| 1219 | self.handle.sendline("") |
| 1220 | self.handle.expect("\$") |
andrewonlab | efd7f3d | 2014-10-21 16:02:31 -0400 | [diff] [blame] | 1221 | self.handle.sendline("") |
andrewonlab | ba44bcf | 2014-10-16 16:54:41 -0400 | [diff] [blame] | 1222 | self.handle.sendline("tshark -i "+str(interface)+ |
andrewonlab | d494049 | 2014-10-24 12:21:27 -0400 | [diff] [blame] | 1223 | " -t e | grep --line-buffered \""+str(grep)+"\" >"+directory+" &") |
andrewonlab | ba44bcf | 2014-10-16 16:54:41 -0400 | [diff] [blame] | 1224 | self.handle.sendline("\r") |
| 1225 | self.handle.expect("Capturing on") |
| 1226 | self.handle.sendline("\r") |
| 1227 | self.handle.expect("\$") |
| 1228 | |
| 1229 | def tshark_stop(self): |
| 1230 | ''' |
| 1231 | Removes wireshark files from /tmp and kills all tshark processes |
| 1232 | ''' |
andrewonlab | 5ba0d9e | 2014-10-24 13:32:23 -0400 | [diff] [blame] | 1233 | #Remove all pcap from previous captures |
| 1234 | self.execute(cmd="sudo rm /tmp/wireshark*") |
andrewonlab | ba44bcf | 2014-10-16 16:54:41 -0400 | [diff] [blame] | 1235 | self.handle.sendline("") |
| 1236 | self.handle.sendline("sudo kill -9 `ps -ef | grep \"tshark -i\" |"+ |
| 1237 | " grep -v grep | awk '{print $2}'`") |
| 1238 | self.handle.sendline("") |
| 1239 | main.log.info("Tshark stopped") |
| 1240 | |
andrewonlab | 0c38a4a | 2014-10-28 18:35:35 -0400 | [diff] [blame] | 1241 | def ptpd(self, args): |
| 1242 | ''' |
| 1243 | Initiate ptp with user-specified args. |
| 1244 | Required: |
| 1245 | * args: specify string of args after command |
| 1246 | 'sudo ptpd' |
| 1247 | ''' |
| 1248 | try: |
| 1249 | self.handle.sendline("sudo ptpd "+str(args)) |
andrewonlab | 978dff6 | 2014-10-29 16:53:35 -0400 | [diff] [blame] | 1250 | i = self.handle.expect([ |
andrewonlab | 0c38a4a | 2014-10-28 18:35:35 -0400 | [diff] [blame] | 1251 | "Multiple", |
| 1252 | "Error", |
| 1253 | "\$"]) |
| 1254 | self.handle.expect("\$") |
andrewonlab | ba44bcf | 2014-10-16 16:54:41 -0400 | [diff] [blame] | 1255 | |
andrewonlab | 0c38a4a | 2014-10-28 18:35:35 -0400 | [diff] [blame] | 1256 | if i == 0: |
| 1257 | handle = self.handle.before |
| 1258 | main.log.info("ptpd returned an error: "+ |
| 1259 | str(handle)) |
| 1260 | return handle |
| 1261 | elif i == 1: |
| 1262 | handle = self.handle.before |
| 1263 | main.log.error("ptpd returned an error: "+ |
| 1264 | str(handle)) |
| 1265 | return handle |
| 1266 | else: |
| 1267 | return main.TRUE |
| 1268 | |
| 1269 | except pexpect.EOF: |
| 1270 | main.log.error(self.name + ": EOF exception found") |
| 1271 | main.log.error(self.name + ": " + self.handle.before) |
| 1272 | main.cleanup() |
| 1273 | main.exit() |
| 1274 | except: |
| 1275 | main.log.info(self.name+" ::::::") |
| 1276 | main.log.error( traceback.print_exc()) |
| 1277 | main.log.info(self.name+" ::::::") |
| 1278 | main.cleanup() |
| 1279 | main.exit() |
andrewonlab | ba44bcf | 2014-10-16 16:54:41 -0400 | [diff] [blame] | 1280 | |
andrewonlab | 5d7a8f3 | 2014-11-10 13:07:56 -0500 | [diff] [blame] | 1281 | def cp_logs_to_dir(self, log_to_copy, |
| 1282 | dest_dir, copy_file_name=""): |
| 1283 | ''' |
| 1284 | Copies logs to a desired directory. |
| 1285 | Current implementation of ONOS deletes its karaf |
| 1286 | logs on every iteration. For debugging purposes, |
| 1287 | you may want to use this function to capture |
| 1288 | certain karaf logs. (or any other logs if needed) |
| 1289 | Localtime will be attached to the filename |
| 1290 | |
| 1291 | Required: |
| 1292 | * log_to_copy: specify directory and log name to |
| 1293 | copy. |
| 1294 | ex) /opt/onos/log/karaf.log.1 |
| 1295 | For copying multiple files, leave copy_file_name |
| 1296 | empty and only specify dest_dir - |
| 1297 | ex) /opt/onos/log/karaf* |
| 1298 | * dest_dir: specify directory to copy to. |
| 1299 | ex) /tmp/ |
| 1300 | Optional: |
| 1301 | * copy_file_name: If you want to rename the log |
| 1302 | file, specify copy_file_name. This will not work |
| 1303 | with multiple file copying |
| 1304 | ''' |
| 1305 | try: |
| 1306 | localtime = time.strftime('%x %X') |
| 1307 | localtime = localtime.replace("/","") |
| 1308 | localtime = localtime.replace(" ","_") |
| 1309 | localtime = localtime.replace(":","") |
| 1310 | if dest_dir[-1:] != "/": |
| 1311 | dest_dir += "/" |
| 1312 | |
| 1313 | if copy_file_name: |
| 1314 | self.handle.sendline("cp "+str(log_to_copy)+ |
| 1315 | " "+str(dest_dir)+str(copy_file_name)+ |
| 1316 | localtime) |
| 1317 | self.handle.expect("cp") |
| 1318 | self.handle.expect("\$") |
| 1319 | else: |
| 1320 | self.handle.sendline("cp "+str(log_to_copy)+ |
| 1321 | " "+str(dest_dir)) |
| 1322 | self.handle.expect("cp") |
| 1323 | self.handle.expect("\$") |
| 1324 | |
| 1325 | return self.handle.before |
| 1326 | |
| 1327 | except pexpect.EOF: |
| 1328 | main.log.error("Copying files failed") |
| 1329 | main.log.error(self.name + ": EOF exception found") |
| 1330 | main.log.error(self.name + ": " + self.handle.before) |
| 1331 | except: |
| 1332 | main.log.error("Copying files failed") |
| 1333 | main.log.info(self.name+" ::::::") |
| 1334 | main.log.error( traceback.print_exc()) |
| 1335 | main.log.info(self.name+" ::::::") |
| 1336 | |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1337 | def check_logs(self, onos_ip): |
| 1338 | ''' |
| 1339 | runs onos-check-logs on the given onos node |
| 1340 | returns the response |
| 1341 | ''' |
| 1342 | try: |
| 1343 | cmd = "onos-check-logs " + str(onos_ip) |
| 1344 | self.handle.sendline(cmd) |
| 1345 | self.handle.expect(cmd) |
| 1346 | self.handle.expect("\$") |
| 1347 | response = self.handle.before |
| 1348 | return response |
| 1349 | except pexpect.EOF: |
| 1350 | main.log.error("Lost ssh connection") |
| 1351 | main.log.error(self.name + ": EOF exception found") |
| 1352 | main.log.error(self.name + ": " + self.handle.before) |
| 1353 | except: |
| 1354 | main.log.error("Some error in check_logs:") |
| 1355 | main.log.info(self.name+" ::::::") |
| 1356 | main.log.error( traceback.print_exc()) |
| 1357 | main.log.info(self.name+" ::::::") |
| 1358 | |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 1359 | def onos_status(self, node=""): |
| 1360 | ''' |
| 1361 | Calls onos command: 'onos-service [<node-ip>] status' |
| 1362 | ''' |
Jon Hall | 94fd047 | 2014-12-08 11:52:42 -0800 | [diff] [blame] | 1363 | |
Hari Krishna | a43d4e9 | 2014-12-19 13:22:40 -0800 | [diff] [blame] | 1364 | try: |
| 1365 | self.handle.sendline("") |
| 1366 | self.handle.expect("\$") |
| 1367 | self.handle.sendline("onos-service "+str(node)+ |
| 1368 | " status") |
| 1369 | i = self.handle.expect([ |
| 1370 | "start/running", |
| 1371 | "stop/waiting", |
| 1372 | pexpect.TIMEOUT],timeout=120) |
| 1373 | |
| 1374 | if i == 0: |
| 1375 | main.log.info("ONOS is running") |
| 1376 | return main.TRUE |
| 1377 | elif i == 1: |
| 1378 | main.log.info("ONOS is stopped") |
| 1379 | return main.FALSE |
| 1380 | else: |
| 1381 | main.log.error("ONOS service failed to check the status") |
| 1382 | main.cleanup() |
| 1383 | main.exit() |
| 1384 | except pexpect.EOF: |
| 1385 | main.log.error(self.name + ": EOF exception found") |
| 1386 | main.log.error(self.name + ": " + self.handle.before) |
| 1387 | main.cleanup() |
| 1388 | main.exit() |
| 1389 | except: |
| 1390 | main.log.info(self.name+" ::::::") |
| 1391 | main.log.error( traceback.print_exc()) |
| 1392 | main.log.info(self.name+" ::::::") |
| 1393 | main.cleanup() |
| 1394 | main.exit() |