blob: e2625cf4ed2ce43abc24fa9c3146287f31c5d15c [file] [log] [blame]
Jon Hall05b2b432014-10-08 19:53:25 -04001#!/usr/bin/env python
andrewonlabe8e56fd2014-10-09 17:12:44 -04002
Jon Hall05b2b432014-10-08 19:53:25 -04003'''
andrewonlabe8e56fd2014-10-09 17:12:44 -04004This driver interacts with ONOS bench, the OSGi platform
5that configures the ONOS nodes. (aka ONOS-next)
6
7Please follow the coding style demonstrated by existing
8functions and document properly.
9
10If you are a contributor to the driver, please
11list your email here for future contact:
12
13jhall@onlab.us
14andrew@onlab.us
15
16OCT 9 2014
17
Jon Hall05b2b432014-10-08 19:53:25 -040018'''
andrewonlabe8e56fd2014-10-09 17:12:44 -040019
andrewonlab7735d852014-10-09 13:02:47 -040020import sys
Jon Hall05b2b432014-10-08 19:53:25 -040021import time
22import pexpect
23import re
24import traceback
andrewonlab7735d852014-10-09 13:02:47 -040025import os.path
andrewonlab6e20c342014-10-10 18:08:48 -040026import pydoc
Jon Hall05b2b432014-10-08 19:53:25 -040027sys.path.append("../")
28from drivers.common.clidriver import CLI
29
30class OnosDriver(CLI):
31
32 def __init__(self):
andrewonlab6e20c342014-10-10 18:08:48 -040033 '''
34 Initialize client
35 '''
Jon Hall05b2b432014-10-08 19:53:25 -040036 super(CLI, self).__init__()
37
38 def connect(self,**connectargs):
39 '''
40 Creates ssh handle for ONOS "bench".
41 '''
42 try:
43 for key in connectargs:
44 vars(self)[key] = connectargs[key]
45 self.home = "~/ONOS"
46 for key in self.options:
47 if key == "home":
48 self.home = self.options['home']
49 break
50
51
52 self.name = self.options['name']
Jon Hallea7818b2014-10-09 14:30:59 -040053 self.handle = super(OnosDriver,self).connect(
54 user_name = self.user_name,
55 ip_address = self.ip_address,
56 port = self.port,
57 pwd = self.pwd,
58 home = self.home)
Jon Hallea7818b2014-10-09 14:30:59 -040059
60 self.handle.sendline("cd "+ self.home)
61 self.handle.expect("\$")
Jon Hall05b2b432014-10-08 19:53:25 -040062 if self.handle:
63 return self.handle
64 else :
65 main.log.info("NO ONOS HANDLE")
66 return main.FALSE
67 except pexpect.EOF:
68 main.log.error(self.name + ": EOF exception found")
69 main.log.error(self.name + ": " + self.handle.before)
70 main.cleanup()
71 main.exit()
72 except:
73 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
74 main.log.error( traceback.print_exc() )
75 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
76 main.cleanup()
77 main.exit()
78
79 def disconnect(self):
80 '''
81 Called when Test is complete to disconnect the ONOS handle.
82 '''
83 response = ''
84 try:
Jon Hallffb386d2014-11-21 13:43:38 -080085 self.handle.sendline("")
Jon Hallf8d10982014-10-22 12:23:38 -040086 self.handle.expect("\$")
Jon Hall05b2b432014-10-08 19:53:25 -040087 self.handle.sendline("exit")
88 self.handle.expect("closed")
89 except pexpect.EOF:
90 main.log.error(self.name + ": EOF exception found")
91 main.log.error(self.name + ": " + self.handle.before)
92 except:
93 main.log.error(self.name + ": Connection failed to the host")
94 response = main.FALSE
95 return response
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -040096
97 def onos_package(self):
98 '''
99 Produce a self-contained tar.gz file that can be deployed
100 and executed on any platform with Java 7 JRE.
101 '''
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400102
103 try:
104 self.handle.sendline("onos-package")
Jon Hallea7818b2014-10-09 14:30:59 -0400105 self.handle.expect("onos-package")
andrewonlabe9907902014-11-25 15:49:06 -0500106 self.handle.expect("tar.gz",timeout=30)
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400107 handle = str(self.handle.before)
108 main.log.info("onos-package command returned: "+
109 handle)
andrewonlab0748d2a2014-10-09 13:24:17 -0400110 #As long as the sendline does not time out,
111 #return true. However, be careful to interpret
112 #the results of the onos-package command return
113 return main.TRUE
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400114
andrewonlab7735d852014-10-09 13:02:47 -0400115 except pexpect.EOF:
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400116 main.log.error(self.name + ": EOF exception found")
117 main.log.error(self.name + ": " + self.handle.before)
118 except:
119 main.log.error("Failed to package ONOS")
120 main.cleanup()
121 main.exit()
122
andrewonlab8790abb2014-11-06 13:51:54 -0500123 def onos_build(self):
124 '''
125 Use the pre defined script to build onos via mvn
126 '''
127
128 try:
129 self.handle.sendline("onos-build")
130 self.handle.expect("onos-build")
131 i = self.handle.expect([
132 "BUILD SUCCESS",
133 "ERROR",
134 "BUILD FAILED"], timeout=120)
135 handle = str(self.handle.before)
136
137 main.log.info("onos-build command returned: "+
138 handle)
139
140 if i == 0:
141 return main.TRUE
142 else:
143 return handle
144
145 except pexpect.EOF:
146 main.log.error(self.name + ": EOF exception found")
147 main.log.error(self.name + ": " + self.handle.before)
148 except:
149 main.log.error("Failed to build ONOS")
150 main.cleanup()
151 main.exit()
152
Jon Hallde9d9aa2014-10-08 20:36:02 -0400153 def clean_install(self):
154 '''
155 Runs mvn clean install in the root of the ONOS directory.
156 This will clean all ONOS artifacts then compile each module
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400157
Jon Hallde9d9aa2014-10-08 20:36:02 -0400158 Returns: main.TRUE on success
159 On Failure, exits the test
160 '''
161 try:
Jon Hallea7818b2014-10-09 14:30:59 -0400162 main.log.info("Running 'mvn clean install' on " + str(self.name) +
163 ". This may take some time.")
164 self.handle.sendline("cd "+ self.home)
165 self.handle.expect("\$")
166
Jon Hallffb386d2014-11-21 13:43:38 -0800167 self.handle.sendline("")
Jon Hallea7818b2014-10-09 14:30:59 -0400168 self.handle.expect("\$")
Jon Hallde9d9aa2014-10-08 20:36:02 -0400169 self.handle.sendline("mvn clean install")
Jon Hallea7818b2014-10-09 14:30:59 -0400170 self.handle.expect("mvn clean install")
Jon Hallde9d9aa2014-10-08 20:36:02 -0400171 while 1:
172 i=self.handle.expect([
173 'There\sis\sinsufficient\smemory\sfor\sthe\sJava\s\
174 Runtime\sEnvironment\sto\scontinue',
175 'BUILD\sFAILURE',
176 'BUILD\sSUCCESS',
177 'ONOS\$',
178 pexpect.TIMEOUT],timeout=600)
179 if i == 0:
180 main.log.error(self.name + ":There is insufficient memory \
181 for the Java Runtime Environment to continue.")
182 #return main.FALSE
183 main.cleanup()
184 main.exit()
185 if i == 1:
186 main.log.error(self.name + ": Build failure!")
187 #return main.FALSE
188 main.cleanup()
189 main.exit()
190 elif i == 2:
191 main.log.info(self.name + ": Build success!")
192 elif i == 3:
193 main.log.info(self.name + ": Build complete")
Jon Hallf8ef52c2014-10-09 19:37:33 -0400194 #Print the build time
195 for line in self.handle.before.splitlines():
196 if "Total time:" in line:
197 main.log.info(line)
Jon Hallffb386d2014-11-21 13:43:38 -0800198 self.handle.sendline("")
Jon Hallde9d9aa2014-10-08 20:36:02 -0400199 self.handle.expect("\$", timeout=60)
200 return main.TRUE
201 elif i == 4:
202 main.log.error(self.name + ": mvn clean install TIMEOUT!")
203 #return main.FALSE
204 main.cleanup()
205 main.exit()
206 else:
207 main.log.error(self.name + ": unexpected response from \
208 mvn clean install")
209 #return main.FALSE
210 main.cleanup()
211 main.exit()
212 except pexpect.EOF:
213 main.log.error(self.name + ": EOF exception found")
214 main.log.error(self.name + ": " + self.handle.before)
215 main.cleanup()
216 main.exit()
217 except:
218 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
219 main.log.error( traceback.print_exc() )
220 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
221 main.cleanup()
222 main.exit()
Jon Hallacabffd2014-10-09 12:36:53 -0400223
224 def git_pull(self, comp1=""):
225 '''
226 Assumes that "git pull" works without login
227
228 This function will perform a git pull on the ONOS instance.
229 If used as git_pull("NODE") it will do git pull + NODE. This is
230 for the purpose of pulling from other nodes if necessary.
231
232 Otherwise, this function will perform a git pull in the
233 ONOS repository. If it has any problems, it will return main.ERROR
Shreya Shahee15f6c2014-10-28 18:12:30 -0400234 If it successfully does a git_pull, it will return a 1 (main.TRUE)
235 If it has no updates, it will return 3.
Jon Hallacabffd2014-10-09 12:36:53 -0400236
237 '''
238 try:
239 # main.log.info(self.name + ": Stopping ONOS")
240 #self.stop()
241 self.handle.sendline("cd " + self.home)
242 self.handle.expect("ONOS\$")
243 if comp1=="":
244 self.handle.sendline("git pull")
245 else:
246 self.handle.sendline("git pull " + comp1)
247
248 uptodate = 0
249 i=self.handle.expect(['fatal',
250 'Username\sfor\s(.*):\s',
251 '\sfile(s*) changed,\s',
252 'Already up-to-date',
253 'Aborting',
254 'You\sare\snot\scurrently\son\sa\sbranch',
255 'You\sasked\sme\sto\spull\swithout\stelling\sme\swhich\sbranch\syou',
256 'Pull\sis\snot\spossible\sbecause\syou\shave\sunmerged\sfiles',
257 pexpect.TIMEOUT],
258 timeout=300)
259 #debug
Jon Hall81e29af2014-11-04 20:41:23 -0500260 #main.log.report(self.name +": DEBUG: \n"+"git pull response: " + str(self.handle.before) + str(self.handle.after))
Jon Hallacabffd2014-10-09 12:36:53 -0400261 if i==0:
262 main.log.error(self.name + ": Git pull had some issue...")
263 return main.ERROR
264 elif i==1:
265 main.log.error(self.name + ": Git Pull Asking for username. ")
266 return main.ERROR
267 elif i==2:
268 main.log.info(self.name + ": Git Pull - pulling repository now")
269 self.handle.expect("ONOS\$", 120)
Shreya Shahee15f6c2014-10-28 18:12:30 -0400270 return main.TRUE # So that only when git pull is done, we do mvn clean compile
Jon Hallacabffd2014-10-09 12:36:53 -0400271 elif i==3:
272 main.log.info(self.name + ": Git Pull - Already up to date")
Shreya Shahee15f6c2014-10-28 18:12:30 -0400273 return i
Jon Hallacabffd2014-10-09 12:36:53 -0400274 elif i==4:
275 main.log.info(self.name + ": Git Pull - Aborting... Are there conflicting git files?")
276 return main.ERROR
277 elif i==5:
278 main.log.info(self.name + ": Git Pull - You are not currently on a branch so git pull failed!")
279 return main.ERROR
280 elif i==6:
281 main.log.info(self.name + ": Git Pull - You have not configured an upstream branch to pull from. Git pull failed!")
282 return main.ERROR
283 elif i==7:
284 main.log.info(self.name + ": Git Pull - Pull is not possible because you have unmerged files.")
285 return main.ERROR
286 elif i==8:
287 main.log.error(self.name + ": Git Pull - TIMEOUT")
288 main.log.error(self.name + " Response was: " + str(self.handle.before))
289 return main.ERROR
290 else:
291 main.log.error(self.name + ": Git Pull - Unexpected response, check for pull errors")
292 return main.ERROR
293 except pexpect.EOF:
294 main.log.error(self.name + ": EOF exception found")
295 main.log.error(self.name + ": " + self.handle.before)
296 main.cleanup()
297 main.exit()
298 except:
299 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
300 main.log.error( traceback.print_exc() )
301 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
302 main.cleanup()
303 main.exit()
304
305 def git_checkout(self, branch="master"):
306 '''
307 Assumes that "git pull" works without login
308
309 This function will perform a git git checkout on the ONOS instance.
310 If used as git_checkout("branch") it will do git checkout of the "branch".
311
312 Otherwise, this function will perform a git checkout of the master
313 branch of the ONOS repository. If it has any problems, it will return
314 main.ERROR.
315 If the branch was already the specified branch, or the git checkout was
316 successful then the function will return main.TRUE.
317
318 '''
319 try:
Jon Hallacabffd2014-10-09 12:36:53 -0400320 self.handle.sendline("cd " + self.home)
321 self.handle.expect("ONOS\$")
Jon Hall81e29af2014-11-04 20:41:23 -0500322 main.log.info(self.name + ": Checking out git branch: " + branch + "...")
323 cmd = "git checkout "+branch
324 self.handle.sendline(cmd)
325 self.handle.expect(cmd)
326 i=self.handle.expect(['fatal',
327 'Username\sfor\s(.*):\s',
328 'Already\son\s\'',
329 'Switched\sto\sbranch\s\'' + str(branch),
330 pexpect.TIMEOUT,
331 'error: Your local changes to the following files would be overwritten by checkout:',
332 'error: you need to resolve your current index first'],timeout=60)
Jon Hallacabffd2014-10-09 12:36:53 -0400333
334 if i==0:
335 main.log.error(self.name + ": Git checkout had some issue...")
Jon Hall81e29af2014-11-04 20:41:23 -0500336 main.log.error(self.name + ": " + self.handle.before)
Jon Hallacabffd2014-10-09 12:36:53 -0400337 return main.ERROR
338 elif i==1:
Jon Hall81e29af2014-11-04 20:41:23 -0500339 main.log.error(self.name + ": Git checkout asking for username."\
340 +" Please configure your local git repository to be able "\
341 +"to access your remote repository passwordlessly")
Jon Hallacabffd2014-10-09 12:36:53 -0400342 return main.ERROR
343 elif i==2:
344 main.log.info(self.name + ": Git Checkout %s : Already on this branch" %branch)
345 self.handle.expect("ONOS\$")
Jon Hall81e29af2014-11-04 20:41:23 -0500346 #main.log.info("DEBUG: after checkout cmd = "+ self.handle.before)
Jon Hallacabffd2014-10-09 12:36:53 -0400347 return main.TRUE
348 elif i==3:
349 main.log.info(self.name + ": Git checkout %s - Switched to this branch" %branch)
350 self.handle.expect("ONOS\$")
Jon Hall81e29af2014-11-04 20:41:23 -0500351 #main.log.info("DEBUG: after checkout cmd = "+ self.handle.before)
Jon Hallacabffd2014-10-09 12:36:53 -0400352 return main.TRUE
353 elif i==4:
354 main.log.error(self.name + ": Git Checkout- TIMEOUT")
355 main.log.error(self.name + " Response was: " + str(self.handle.before))
356 return main.ERROR
Jon Hall81e29af2014-11-04 20:41:23 -0500357 elif i==5:
358 self.handle.expect("Aborting")
359 main.log.error(self.name + ": Git checkout error: \n" + \
360 "Your local changes to the following files would be overwritten by checkout:" + \
361 str(self.handle.before))
362 self.handle.expect("ONOS\$")
363 return main.ERROR
364 elif i==6:
365 main.log.error(self.name + ": Git checkout error: \n" + \
366 "You need to resolve your current index first:" + \
367 str(self.handle.before))
368 self.handle.expect("ONOS\$")
369 return main.ERROR
Jon Hallacabffd2014-10-09 12:36:53 -0400370 else:
371 main.log.error(self.name + ": Git Checkout - Unexpected response, check for pull errors")
Jon Hall81e29af2014-11-04 20:41:23 -0500372 main.log.error(self.name + ": " + self.handle.before)
Jon Hallacabffd2014-10-09 12:36:53 -0400373 return main.ERROR
374
375 except pexpect.EOF:
376 main.log.error(self.name + ": EOF exception found")
377 main.log.error(self.name + ": " + self.handle.before)
378 main.cleanup()
379 main.exit()
380 except:
381 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
382 main.log.error( traceback.print_exc() )
383 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
384 main.cleanup()
385 main.exit()
andrewonlab95ca1462014-10-09 14:04:24 -0400386
Jon Hallfd191202014-11-07 18:36:09 -0500387 def get_version(self, report=False):
Jon Hall45ec0922014-10-10 19:33:49 -0400388 '''
389 Writes the COMMIT number to the report to be parsed by Jenkins data collecter.
390 '''
391 try:
392 self.handle.sendline("export TERM=xterm-256color")
393 self.handle.expect("xterm-256color")
394 self.handle.expect("\$")
Jon Hallffb386d2014-11-21 13:43:38 -0800395 self.handle.sendline("")
Jon Hall42db6dc2014-10-24 19:03:48 -0400396 self.handle.expect("\$")
397 self.handle.sendline("cd " + self.home + "; git log -1 --pretty=fuller --decorate=short | grep -A 6 \"commit\" --color=never")
Jon Hall73cf9cc2014-11-20 22:28:38 -0800398 #NOTE: for some reason there are backspaces inserted in this phrase when run from Jenkins on some tests
Jon Hallffb386d2014-11-21 13:43:38 -0800399 self.handle.expect("never")
Jon Hall45ec0922014-10-10 19:33:49 -0400400 self.handle.expect("\$")
401 response=(self.name +": \n"+ str(self.handle.before + self.handle.after))
Jon Hall42db6dc2014-10-24 19:03:48 -0400402 self.handle.sendline("cd " + self.home)
403 self.handle.expect("\$")
Jon Hall45ec0922014-10-10 19:33:49 -0400404 lines=response.splitlines()
405 for line in lines:
Jon Hallfd191202014-11-07 18:36:09 -0500406 print line
407 if report:
408 for line in lines[2:-1]:
andrewonlabac5810a2014-11-18 14:33:03 -0500409 #Bracket replacement is for Wiki-compliant
410 #formatting. '<' or '>' are interpreted
411 #as xml specific tags that cause errors
412 line = line.replace("<","[")
413 line = line.replace(">","]")
Jon Hallb1290e82014-11-18 16:17:48 -0500414 main.log.report("\t" + line)
Jon Hallfd191202014-11-07 18:36:09 -0500415 return lines[2]
Jon Hall45ec0922014-10-10 19:33:49 -0400416 except pexpect.EOF:
417 main.log.error(self.name + ": EOF exception found")
418 main.log.error(self.name + ": " + self.handle.before)
419 main.cleanup()
420 main.exit()
Jon Hall368769f2014-11-19 15:43:35 -0800421 except pexpect.TIMEOUT:
422 main.log.error(self.name + ": TIMEOUT exception found")
423 main.log.error(self.name + ": " + self.handle.before)
424 main.cleanup()
425 main.exit()
Jon Hall45ec0922014-10-10 19:33:49 -0400426 except:
427 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
428 main.log.error( traceback.print_exc() )
429 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
430 main.cleanup()
431 main.exit()
432
andrewonlabba44bcf2014-10-16 16:54:41 -0400433 def create_cell_file(self, bench_ip, file_name, mn_ip_addrs,
andrewonlab3f0a4af2014-10-17 12:25:14 -0400434 extra_feature_string, *onos_ip_addrs):
andrewonlab94282092014-10-10 13:00:11 -0400435 '''
436 Creates a cell file based on arguments
437 Required:
438 * Bench IP address (bench_ip)
439 - Needed to copy the cell file over
440 * File name of the cell file (file_name)
441 * Mininet IP address (mn_ip_addrs)
442 - Note that only 1 ip address is
443 supported currently
444 * ONOS IP addresses (onos_ip_addrs)
445 - Must be passed in as last arguments
446
447 NOTE: Assumes cells are located at:
448 ~/<self.home>/tools/test/cells/
449 '''
450
451 #Variable initialization
452 cell_directory = self.home + "/tools/test/cells/"
453 #We want to create the cell file in the dependencies directory
454 #of TestON first, then copy over to ONOS bench
455 temp_directory = "/tmp/"
456 #Create the cell file in the directory for writing (w+)
457 cell_file = open(temp_directory+file_name , 'w+')
andrewonlabd4940492014-10-24 12:21:27 -0400458
459 comment = ""
460 comment_string = "#"+ comment
andrewonlab7e4d2d32014-10-15 13:23:21 -0400461 #Feature string is hardcoded environment variables
462 #That you may wish to use by default on startup.
463 #Note that you may not want certain features listed
464 #on here.
andrewonlabba44bcf2014-10-16 16:54:41 -0400465 core_feature_string = "export ONOS_FEATURES=webconsole,onos-api,"+\
466 "onos-cli,onos-openflow,onos-app-mobility,onos-app-tvue,"+\
467 "onos-app-proxyarp,"+extra_feature_string
andrewonlab94282092014-10-10 13:00:11 -0400468 mn_string = "export OCN="
469 onos_string = "export OC"
470 temp_count = 1
471
472 #Create ONOS_NIC ip address prefix
473 temp_onos_ip = onos_ip_addrs[0]
474 temp_list = []
475 temp_list = temp_onos_ip.split(".")
andrewonlab7e4d2d32014-10-15 13:23:21 -0400476 #Omit last element of list to format for NIC
andrewonlab94282092014-10-10 13:00:11 -0400477 temp_list = temp_list[:-1]
478 #Structure the nic string ip
andrewonlabd4940492014-10-24 12:21:27 -0400479 nic_addr = ".".join(temp_list) + ".*"
andrewonlab94282092014-10-10 13:00:11 -0400480 onos_nic_string = "export ONOS_NIC="+nic_addr
481
482 try:
483 #Start writing to file
andrewonlabd4940492014-10-24 12:21:27 -0400484 cell_file.write(onos_nic_string + "\n")
andrewonlab94282092014-10-10 13:00:11 -0400485
486 for arg in onos_ip_addrs:
487 #For each argument in onos_ip_addrs, write to file
488 #Output should look like the following:
andrewonlabd4940492014-10-24 12:21:27 -0400489 # export OC1="10.128.20.11"
490 # export OC2="10.128.20.12"
andrewonlab94282092014-10-10 13:00:11 -0400491 cell_file.write(onos_string + str(temp_count) +
andrewonlabd4940492014-10-24 12:21:27 -0400492 "=" + "\"" + arg + "\"" + "\n" )
andrewonlab94282092014-10-10 13:00:11 -0400493 temp_count = temp_count + 1
494
andrewonlabd4940492014-10-24 12:21:27 -0400495 cell_file.write(mn_string +"\""+ mn_ip_addrs +"\""+ "\n")
496 cell_file.write(core_feature_string + "\n")
andrewonlab94282092014-10-10 13:00:11 -0400497 cell_file.close()
498
499 #We use os.system to send the command to TestON cluster
500 #to account for the case in which TestON is not located
501 #on the same cluster as the ONOS bench
502 #Note that even if TestON is located on the same cluster
503 #as ONOS bench, you must setup passwordless ssh
andrewonlab7e4d2d32014-10-15 13:23:21 -0400504 #between TestON and ONOS bench in order to automate the test.
andrewonlab94282092014-10-10 13:00:11 -0400505 os.system("scp "+temp_directory+file_name+
506 " admin@"+bench_ip+":"+cell_directory)
507
andrewonlab2a6c9342014-10-16 13:40:15 -0400508 return main.TRUE
509
andrewonlab94282092014-10-10 13:00:11 -0400510 except pexpect.EOF:
511 main.log.error(self.name + ": EOF exception found")
512 main.log.error(self.name + ": " + self.handle.before)
513 main.cleanup()
514 main.exit()
515 except:
516 main.log.info(self.name + ":::::::::")
517 main.log.error( traceback.print_exc() )
518 main.log.info(":::::::")
519 main.cleanup()
520 main.exit()
521
andrewonlab95ca1462014-10-09 14:04:24 -0400522 def set_cell(self, cellname):
523 '''
524 Calls 'cell <name>' to set the environment variables on ONOSbench
525 '''
526 try:
527 if not cellname:
528 main.log.error("Must define cellname")
529 main.cleanup()
530 main.exit()
531 else:
532 self.handle.sendline("cell "+str(cellname))
533 #Expect the cellname in the ONOS_CELL variable.
534 #Note that this variable name is subject to change
535 # and that this driver will have to change accordingly
536 self.handle.expect("ONOS_CELL="+str(cellname))
537 handle_before = self.handle.before
538 handle_after = self.handle.after
andrewonlabc03bf6c2014-10-09 14:56:18 -0400539 #Get the rest of the handle
540 self.handle.sendline("")
541 self.handle.expect("\$")
542 handle_more = self.handle.before
andrewonlab95ca1462014-10-09 14:04:24 -0400543
544 main.log.info("Cell call returned: "+handle_before+
andrewonlabc03bf6c2014-10-09 14:56:18 -0400545 handle_after + handle_more)
andrewonlab95ca1462014-10-09 14:04:24 -0400546
547 return main.TRUE
548
549 except pexpect.EOF:
550 main.log.error(self.name + ": EOF exception found")
551 main.log.error(self.name + ": " + self.handle.before)
552 main.cleanup()
553 main.exit()
554 except:
555 main.log.info(self.name+" ::::::")
556 main.log.error( traceback.print_exc())
557 main.log.info(self.name+" ::::::")
558 main.cleanup()
559 main.exit()
560
andrewonlabc03bf6c2014-10-09 14:56:18 -0400561 def verify_cell(self):
562 '''
563 Calls 'onos-verify-cell' to check for cell installation
564 '''
andrewonlab8d0d7d72014-10-09 16:33:15 -0400565 #TODO: Add meaningful expect value
566
andrewonlabc03bf6c2014-10-09 14:56:18 -0400567 try:
568 #Clean handle by sending empty and expecting $
569 self.handle.sendline("")
570 self.handle.expect("\$")
571 self.handle.sendline("onos-verify-cell")
572 self.handle.expect("\$")
573 handle_before = self.handle.before
574 handle_after = self.handle.after
575 #Get the rest of the handle
576 self.handle.sendline("")
577 self.handle.expect("\$")
578 handle_more = self.handle.before
579
580 main.log.info("Verify cell returned: "+handle_before+
581 handle_after + handle_more)
582
583 return main.TRUE
Jon Hall7993bfc2014-10-09 16:30:14 -0400584 except pexpect.EOF:
585 main.log.error(self.name + ": EOF exception found")
586 main.log.error(self.name + ": " + self.handle.before)
587 main.cleanup()
588 main.exit()
589 except:
590 main.log.info(self.name+" ::::::")
591 main.log.error( traceback.print_exc())
592 main.log.info(self.name+" ::::::")
593 main.cleanup()
594 main.exit()
595
andrewonlab05e362f2014-10-10 00:40:57 -0400596 def onos_cli(self, ONOS_ip, cmdstr):
597 '''
598 Uses 'onos' command to send various ONOS CLI arguments.
599 Required:
600 * ONOS_ip: specify the ip of the cell machine
andrewonlab94282092014-10-10 13:00:11 -0400601 * cmdstr: specify the command string to send
andrewonlab6e20c342014-10-10 18:08:48 -0400602
603 This function is intended to expose the entire karaf
604 CLI commands for ONOS. Try to use this function first
605 before attempting to write a ONOS CLI specific driver
606 function.
607 You can see a list of available 'cmdstr' arguments
608 by starting onos, and typing in 'onos' to enter the
609 onos> CLI. Then, type 'help' to see the list of
610 available commands.
andrewonlab05e362f2014-10-10 00:40:57 -0400611 '''
612 try:
613 if not ONOS_ip:
614 main.log.error("You must specify the IP address")
615 return main.FALSE
616 if not cmdstr:
617 main.log.error("You must specify the command string")
618 return main.FALSE
619
620 cmdstr = str(cmdstr)
621 self.handle.sendline("")
622 self.handle.expect("\$")
623
624 self.handle.sendline("onos -w " + ONOS_ip + " " + cmdstr)
625 self.handle.expect("\$")
626
Shreya Shaha73aaad2014-10-27 18:03:09 -0400627 handle_before = self.handle.before
628 print "handle_before = ", self.handle.before
andrewonlab05e362f2014-10-10 00:40:57 -0400629 handle_after = str(self.handle.after)
630
Shreya Shaha73aaad2014-10-27 18:03:09 -0400631 #self.handle.sendline("")
632 #self.handle.expect("\$")
633 #handle_more = str(self.handle.before)
andrewonlab05e362f2014-10-10 00:40:57 -0400634
635 main.log.info("Command sent successfully")
636
andrewonlab94282092014-10-10 13:00:11 -0400637 #Obtain return handle that consists of result from
638 #the onos command. The string may need to be
639 #configured further.
Shreya Shaha73aaad2014-10-27 18:03:09 -0400640 #return_string = handle_before + handle_after
641 return_string = handle_before
642 print "return_string = ", return_string
andrewonlab05e362f2014-10-10 00:40:57 -0400643 return return_string
644
645 except pexpect.EOF:
646 main.log.error(self.name + ": EOF exception found")
647 main.log.error(self.name + ": " + self.handle.before)
648 main.cleanup()
649 main.exit()
650 except:
651 main.log.info(self.name+" ::::::")
652 main.log.error( traceback.print_exc())
653 main.log.info(self.name+" ::::::")
654 main.cleanup()
655 main.exit()
Jon Hall7993bfc2014-10-09 16:30:14 -0400656
657 def onos_install(self, options="-f", node = ""):
658 '''
659 Installs ONOS bits on the designated cell machine.
660 If -f option is provided, it also forces an uninstall.
661 Presently, install also includes onos-push-bits and
662 onos-config within.
663 The node option allows you to selectively only push the jar
664 files to certain onos nodes
665
666 Returns: main.TRUE on success and main.FALSE on failure
667 '''
668 try:
andrewonlab114768a2014-11-14 12:44:44 -0500669 if options:
670 self.handle.sendline("onos-install " + options + " " + node)
671 else:
672 self.handle.sendline("onos-install "+node)
Jon Hall7993bfc2014-10-09 16:30:14 -0400673 self.handle.expect("onos-install ")
674 #NOTE: this timeout may need to change depending on the network and size of ONOS
675 i=self.handle.expect(["Network\sis\sunreachable",
676 "onos\sstart/running,\sprocess",
andrewonlabd9a73a72014-11-14 17:28:21 -0500677 "ONOS\sis\salready\sinstalled",
Jon Hall7993bfc2014-10-09 16:30:14 -0400678 pexpect.TIMEOUT],timeout=60)
679
Jon Hall7993bfc2014-10-09 16:30:14 -0400680 if i == 0:
681 main.log.warn("Network is unreachable")
682 return main.FALSE
683 elif i == 1:
Jon Hall42db6dc2014-10-24 19:03:48 -0400684 main.log.info("ONOS was installed on " + node + " and started")
Jon Hall7993bfc2014-10-09 16:30:14 -0400685 return main.TRUE
andrewonlabd9a73a72014-11-14 17:28:21 -0500686 elif i == 2:
687 main.log.info("ONOS is already installed on "+node)
688 return main.TRUE
689 elif i == 3:
Jon Hall42db6dc2014-10-24 19:03:48 -0400690 main.log.info("Installation of ONOS on " + node + " timed out")
Jon Hall7993bfc2014-10-09 16:30:14 -0400691 return main.FALSE
andrewonlabc03bf6c2014-10-09 14:56:18 -0400692
andrewonlabd9a73a72014-11-14 17:28:21 -0500693
andrewonlabc03bf6c2014-10-09 14:56:18 -0400694 except pexpect.EOF:
695 main.log.error(self.name + ": EOF exception found")
696 main.log.error(self.name + ": " + self.handle.before)
697 main.cleanup()
698 main.exit()
699 except:
700 main.log.info(self.name+" ::::::")
701 main.log.error( traceback.print_exc())
702 main.log.info(self.name+" ::::::")
703 main.cleanup()
704 main.exit()
andrewonlab95ca1462014-10-09 14:04:24 -0400705
andrewonlab8d0d7d72014-10-09 16:33:15 -0400706 def onos_start(self, node_ip):
707 '''
708 Calls onos command: 'onos-service [<node-ip>] start'
andrewonlabe8e56fd2014-10-09 17:12:44 -0400709 This command is a remote management of the ONOS upstart daemon
andrewonlab8d0d7d72014-10-09 16:33:15 -0400710 '''
711
712 try:
713 self.handle.sendline("")
714 self.handle.expect("\$")
715 self.handle.sendline("onos-service "+str(node_ip)+
716 " start")
717 i = self.handle.expect([
718 "Job\sis\salready\srunning",
719 "start/running",
720 "Unknown\sinstance",
Shreya Shahd01153d2014-10-23 15:08:56 -0400721 pexpect.TIMEOUT],timeout=120)
andrewonlab8d0d7d72014-10-09 16:33:15 -0400722
723 if i == 0:
724 main.log.info("Service is already running")
725 return main.TRUE
726 elif i == 1:
727 main.log.info("ONOS service started")
728 return main.TRUE
729 else:
730 main.log.error("ONOS service failed to start")
731 main.cleanup()
732 main.exit()
andrewonlab8d0d7d72014-10-09 16:33:15 -0400733 except pexpect.EOF:
734 main.log.error(self.name + ": EOF exception found")
735 main.log.error(self.name + ": " + self.handle.before)
736 main.cleanup()
737 main.exit()
738 except:
739 main.log.info(self.name+" ::::::")
740 main.log.error( traceback.print_exc())
741 main.log.info(self.name+" ::::::")
742 main.cleanup()
743 main.exit()
744
andrewonlab2b30bd32014-10-09 16:48:55 -0400745 def onos_stop(self, node_ip):
746 '''
747 Calls onos command: 'onos-service [<node-ip>] stop'
andrewonlabe8e56fd2014-10-09 17:12:44 -0400748 This command is a remote management of the ONOS upstart daemon
andrewonlab2b30bd32014-10-09 16:48:55 -0400749 '''
750 try:
751 self.handle.sendline("")
752 self.handle.expect("\$")
753 self.handle.sendline("onos-service "+str(node_ip)+
754 " stop")
755 i = self.handle.expect([
756 "stop/waiting",
757 "Unknown\sinstance",
758 pexpect.TIMEOUT],timeout=60)
759
760 if i == 0:
761 main.log.info("ONOS service stopped")
762 return main.TRUE
763 elif i == 1:
764 main.log.info("Unknown ONOS instance specified: "+
765 str(node_ip))
766 return main.FALSE
767 else:
768 main.log.error("ONOS service failed to stop")
769 return main.FALSE
770
771 except pexpect.EOF:
772 main.log.error(self.name + ": EOF exception found")
773 main.log.error(self.name + ": " + self.handle.before)
774 main.cleanup()
775 main.exit()
776 except:
777 main.log.info(self.name+" ::::::")
778 main.log.error( traceback.print_exc())
779 main.log.info(self.name+" ::::::")
780 main.cleanup()
781 main.exit()
andrewonlab99d3f7f2014-11-13 17:45:08 -0500782
andrewonlaba6f7d882014-11-13 17:32:24 -0500783 def onos_uninstall(self, node_ip=""):
andrewonlabc8d47972014-10-09 16:52:36 -0400784 '''
785 Calls the command: 'onos-uninstall'
andrewonlabe8e56fd2014-10-09 17:12:44 -0400786 Uninstalls ONOS from the designated cell machine, stopping
787 if needed
andrewonlabc8d47972014-10-09 16:52:36 -0400788 '''
789 try:
790 self.handle.sendline("")
791 self.handle.expect("\$")
Jon Hall73cf9cc2014-11-20 22:28:38 -0800792 self.handle.sendline( "onos-uninstall "+str(node_ip) )
andrewonlabc8d47972014-10-09 16:52:36 -0400793 self.handle.expect("\$")
794
andrewonlab9dfd2082014-11-13 17:44:03 -0500795 main.log.info("ONOS "+node_ip+" was uninstalled")
796
andrewonlabc8d47972014-10-09 16:52:36 -0400797 #onos-uninstall command does not return any text
798 return main.TRUE
799
800 except pexpect.EOF:
801 main.log.error(self.name + ": EOF exception found")
802 main.log.error(self.name + ": " + self.handle.before)
803 main.cleanup()
804 main.exit()
805 except:
806 main.log.info(self.name+" ::::::")
807 main.log.error( traceback.print_exc())
808 main.log.info(self.name+" ::::::")
809 main.cleanup()
810 main.exit()
andrewonlab2b30bd32014-10-09 16:48:55 -0400811
andrewonlabe8e56fd2014-10-09 17:12:44 -0400812 def onos_kill(self, node_ip):
813 '''
814 Calls the command: 'onos-kill [<node-ip>]'
815 "Remotely, and unceremoniously kills the ONOS instance running on
816 the specified cell machine" - Tom V
817 '''
818
819 try:
820 self.handle.sendline("")
821 self.handle.expect("\$")
822 self.handle.sendline("onos-kill " + str(node_ip))
823 i = self.handle.expect([
824 "\$",
825 "No\sroute\sto\shost",
826 "password:",
827 pexpect.TIMEOUT], timeout=20)
828
829 if i == 0:
830 main.log.info("ONOS instance "+str(node_ip)+" was killed")
831 return main.TRUE
832 elif i == 1:
833 main.log.info("No route to host")
834 return main.FALSE
835 elif i == 2:
836 main.log.info("Passwordless login for host: "+str(node_ip)+
837 " not configured")
838 return main.FALSE
839 else:
840 main.log.info("ONOS instasnce was not killed")
841 return main.FALSE
842
843 except pexpect.EOF:
844 main.log.error(self.name + ": EOF exception found")
845 main.log.error(self.name + ": " + self.handle.before)
846 main.cleanup()
847 main.exit()
848 except:
849 main.log.info(self.name+" ::::::")
850 main.log.error( traceback.print_exc())
851 main.log.info(self.name+" ::::::")
852 main.cleanup()
853 main.exit()
854
andrewonlab19fbdca2014-11-14 12:55:59 -0500855 def onos_remove_raft_logs(self):
856 '''
857 Removes Raft / Copy cat files from ONOS to ensure
Jon Hallfcc88622014-11-25 13:09:54 -0500858 a cleaner environment.
859
andrewonlab19fbdca2014-11-14 12:55:59 -0500860 Description:
Jon Hallfcc88622014-11-25 13:09:54 -0500861 Stops all ONOS defined in the cell,
andrewonlab19fbdca2014-11-14 12:55:59 -0500862 wipes the raft / copycat log files
863 '''
864 try:
865 self.handle.sendline("")
866 self.handle.expect("\$")
867 self.handle.sendline("onos-remove-raft-logs")
Jon Hallfcc88622014-11-25 13:09:54 -0500868 #Sometimes this command hangs
andrewonlabeb7b1072014-11-25 15:50:07 -0500869 i = self.handle.expect(["\$", pexpect.TIMEOUT],
870 timeout=120)
Jon Hallfcc88622014-11-25 13:09:54 -0500871 if i == 1:
andrewonlabeb7b1072014-11-25 15:50:07 -0500872 i = self.handle.expect(["\$", pexpect.TIMEOUT],
873 timeout=120)
Jon Hallfcc88622014-11-25 13:09:54 -0500874 if i == 1:
875 return main.FALSE
andrewonlab19fbdca2014-11-14 12:55:59 -0500876 return main.TRUE
877
878 except pexpect.EOF:
879 main.log.error(self.name + ": EOF exception found")
880 main.log.error(self.name + ": " + self.handle.before)
881 main.cleanup()
882 main.exit()
883 except:
884 main.log.info(self.name+" ::::::")
885 main.log.error( traceback.print_exc())
886 main.log.info(self.name+" ::::::")
887 main.cleanup()
888 main.exit()
Jon Hallfcc88622014-11-25 13:09:54 -0500889
andrewonlab94282092014-10-10 13:00:11 -0400890 def onos_start_network(self, mntopo):
891 '''
892 Calls the command 'onos-start-network [<mininet-topo>]
893 "remotely starts the specified topology on the cell's
894 mininet machine against all controllers configured in the
895 cell."
896 * Specify mininet topology file name for mntopo
897 * Topo files should be placed at:
898 ~/<your-onos-directory>/tools/test/topos
899
900 NOTE: This function will take you to the mininet prompt
901 '''
902 try:
903 if not mntopo:
904 main.log.error("You must specify a topo file to execute")
905 return main.FALSE
906
907 mntopo = str(mntopo)
908 self.handle.sendline("")
909 self.handle.expect("\$")
910
911 self.handle.sendline("onos-start-network " + mntopo)
912 self.handle.expect("mininet>")
913 main.log.info("Network started, entered mininet prompt")
914
915 #TODO: Think about whether return is necessary or not
916
917 except pexpect.EOF:
918 main.log.error(self.name + ": EOF exception found")
919 main.log.error(self.name + ": " + self.handle.before)
920 main.cleanup()
921 main.exit()
922 except:
923 main.log.info(self.name+" ::::::")
924 main.log.error( traceback.print_exc())
925 main.log.info(self.name+" ::::::")
926 main.cleanup()
927 main.exit()
928
929
Jon Hall7993bfc2014-10-09 16:30:14 -0400930 def isup(self, node = ""):
931 '''
Jon Hall77f53ce2014-10-13 18:02:06 -0400932 Run's onos-wait-for-start which only returns once ONOS is at run
933 level 100(ready for use)
andrewonlab8d0d7d72014-10-09 16:33:15 -0400934
Jon Hall7993bfc2014-10-09 16:30:14 -0400935 Returns: main.TRUE if ONOS is running and main.FALSE on timeout
936 '''
937 try:
938 self.handle.sendline("onos-wait-for-start " + node )
939 self.handle.expect("onos-wait-for-start")
940 #NOTE: this timeout is arbitrary"
Jon Hall73cf9cc2014-11-20 22:28:38 -0800941 i = self.handle.expect(["\$", pexpect.TIMEOUT], timeout = 120)
Jon Hall7993bfc2014-10-09 16:30:14 -0400942 if i == 0:
943 main.log.info(self.name + ": " + node + " is up")
944 return main.TRUE
945 elif i == 1:
946 #NOTE: since this function won't return until ONOS is ready,
947 # we will kill it on timeout
Jon Hall73cf9cc2014-11-20 22:28:38 -0800948 main.log.error("ONOS has not started yet")
949 self.handle.send("\x03") #Control-C
Jon Hall7993bfc2014-10-09 16:30:14 -0400950 self.handle.expect("\$")
951 return main.FALSE
952 except pexpect.EOF:
953 main.log.error(self.name + ": EOF exception found")
954 main.log.error(self.name + ": " + self.handle.before)
955 main.cleanup()
956 main.exit()
957 except:
958 main.log.info(self.name+" ::::::")
959 main.log.error( traceback.print_exc())
960 main.log.info(self.name+" ::::::")
961 main.cleanup()
962 main.exit()
andrewonlab05e362f2014-10-10 00:40:57 -0400963
964
Shreya Shaha73aaad2014-10-27 18:03:09 -0400965 def get_topology(self,topology_output):
Jon Hall77f53ce2014-10-13 18:02:06 -0400966 '''
967 parses the onos:topology output
968 Returns: a topology dict populated by the key values found in
969 the cli command.
970 '''
971
972 try:
973 #call the cli to get the topology summary
Shreya Shaha73aaad2014-10-27 18:03:09 -0400974 #cmdstr = "onos:topology"
975 #cli_result = self.onos_cli(ip, cmdstr)
976 #print "cli_result = ", cli_result
Jon Hall77f53ce2014-10-13 18:02:06 -0400977
978 #Parse the output
979 topology = {}
980 #for line in cli_result.split("\n"):
Shreya Shaha73aaad2014-10-27 18:03:09 -0400981 for line in topology_output.splitlines():
Jon Hall77f53ce2014-10-13 18:02:06 -0400982 if not line.startswith("time="):
983 continue
984 #else
985 print line
986 for var in line.split(","):
Shreya Shaha73aaad2014-10-27 18:03:09 -0400987 #print "'"+var+"'"
988 #print "'"+var.strip()+"'"
Jon Hall77f53ce2014-10-13 18:02:06 -0400989 key, value = var.strip().split("=")
990 topology[key] = value
Shreya Shaha73aaad2014-10-27 18:03:09 -0400991 print "topology = ", topology
Jon Hall77f53ce2014-10-13 18:02:06 -0400992 devices = topology.get('devices', False)
Shreya Shaha73aaad2014-10-27 18:03:09 -0400993 print "devices = ", devices
Jon Hall77f53ce2014-10-13 18:02:06 -0400994 links = topology.get('links', False)
Shreya Shaha73aaad2014-10-27 18:03:09 -0400995 print "links = ", links
Jon Hall77f53ce2014-10-13 18:02:06 -0400996 clusters = topology.get('clusters', False)
Shreya Shaha73aaad2014-10-27 18:03:09 -0400997 print "clusters = ", clusters
Jon Hall77f53ce2014-10-13 18:02:06 -0400998 paths = topology.get('paths', False)
Shreya Shaha73aaad2014-10-27 18:03:09 -0400999 print "paths = ", paths
Jon Hall77f53ce2014-10-13 18:02:06 -04001000
1001 return topology
1002 except pexpect.EOF:
1003 main.log.error(self.name + ": EOF exception found")
1004 main.log.error(self.name + ": " + self.handle.before)
1005 main.cleanup()
1006 main.exit()
1007 except:
1008 main.log.info(self.name+" ::::::")
1009 main.log.error( traceback.print_exc())
1010 main.log.info(self.name+" ::::::")
1011 main.cleanup()
1012 main.exit()
Shreya Shaha73aaad2014-10-27 18:03:09 -04001013
1014
Jon Hall77f53ce2014-10-13 18:02:06 -04001015
Shreya Shaha73aaad2014-10-27 18:03:09 -04001016 def check_status(self, topology_result, numoswitch, numolink, log_level="info"):
Jon Hall77f53ce2014-10-13 18:02:06 -04001017 '''
1018 Checks the number of swithes & links that ONOS sees against the
1019 supplied values. By default this will report to main.log, but the
1020 log level can be specifid.
1021
1022 Params: ip = ip used for the onos cli
1023 numoswitch = expected number of switches
1024 numlink = expected number of links
1025 log_level = level to log to. Currently accepts 'info', 'warn' and 'report'
1026
1027
1028 log_level can
1029
1030 Returns: main.TRUE if the number of switchs and links are correct,
1031 main.FALSE if the numer of switches and links is incorrect,
1032 and main.ERROR otherwise
1033 '''
1034
1035 try:
Shreya Shaha73aaad2014-10-27 18:03:09 -04001036 topology = self.get_topology(topology_result)
Jon Hall77f53ce2014-10-13 18:02:06 -04001037 if topology == {}:
1038 return main.ERROR
1039 output = ""
1040 #Is the number of switches is what we expected
1041 devices = topology.get('devices',False)
1042 links = topology.get('links',False)
1043 if devices == False or links == False:
1044 return main.ERROR
1045 switch_check = ( int(devices) == int(numoswitch) )
1046 #Is the number of links is what we expected
1047 link_check = ( int(links) == int(numolink) )
1048 if (switch_check and link_check):
1049 #We expected the correct numbers
1050 output = output + "The number of links and switches match "\
1051 + "what was expected"
1052 result = main.TRUE
1053 else:
1054 output = output + \
1055 "The number of links and switches does not match what was expected"
1056 result = main.FALSE
1057 output = output + "\n ONOS sees %i devices (%i expected) and %i links (%i expected)"\
1058 % ( int(devices), int(numoswitch), int(links), int(numolink) )
1059 if log_level == "report":
1060 main.log.report(output)
1061 elif log_level == "warn":
1062 main.log.warn(output)
1063 else:
1064 main.log.info(output)
1065 return result
1066 except pexpect.EOF:
1067 main.log.error(self.name + ": EOF exception found")
1068 main.log.error(self.name + ": " + self.handle.before)
1069 main.cleanup()
1070 main.exit()
1071 except:
1072 main.log.info(self.name+" ::::::")
1073 main.log.error( traceback.print_exc())
1074 main.log.info(self.name+" ::::::")
1075 main.cleanup()
1076 main.exit()
andrewonlabba44bcf2014-10-16 16:54:41 -04001077
andrewonlab970399c2014-11-07 13:09:32 -05001078 def tshark_pcap(self, interface, dir_file):
1079 '''
1080 Capture all packet activity and store in specified
1081 directory/file
1082
1083 Required:
1084 * interface: interface to capture
1085 * dir: directory/filename to store pcap
1086 '''
1087 self.handle.sendline("")
1088 self.handle.expect("\$")
1089
1090 self.handle.sendline("tshark -i "+str(interface)+
andrewonlab5d7a8f32014-11-10 13:07:56 -05001091 " -t e -w "+str(dir_file)+ " &")
1092 self.handle.sendline("\r")
andrewonlab970399c2014-11-07 13:09:32 -05001093 self.handle.expect("Capturing on")
andrewonlab5d7a8f32014-11-10 13:07:56 -05001094 self.handle.sendline("\r")
1095 self.handle.expect("\$")
andrewonlab970399c2014-11-07 13:09:32 -05001096
1097 main.log.info("Tshark started capturing files on "+
1098 str(interface)+ " and saving to directory: "+
andrewonlab5d7a8f32014-11-10 13:07:56 -05001099 str(dir_file))
Shreya Shaha73aaad2014-10-27 18:03:09 -04001100
shahshreya4d79aab2014-11-07 15:20:41 -08001101
1102 def run_onos_topo_cfg(self):
1103 '''
1104 On ONOS bench, run this command: ./~/ONOS/tools/test/bin/onos-topo-cfg
1105 which starts the rest and copies the json file to the onos instance
1106 '''
1107 self.handle.sendline("")
1108 self.handle.expect("\$")
1109 self.handle.sendline("cd ~/ONOS/tools/test/bin")
1110 self.handle.expect("/bin$")
1111 self.handle.sendline("./onos-topo-cfg")
shahshreyadf73d762014-11-11 13:58:56 -08001112 self.handle.expect("{}")
shahshreya4d79aab2014-11-07 15:20:41 -08001113 self.handle.sendline("cd ~")
1114 self.handle.expect("\$")
1115
1116
andrewonlabba44bcf2014-10-16 16:54:41 -04001117 def tshark_grep(self, grep, directory, interface='eth0'):
1118 '''
1119 Required:
1120 * grep string
1121 * directory to store results
1122 Optional:
1123 * interface - default: eth0
1124 Description:
1125 Uses tshark command to grep specific group of packets
1126 and stores the results to specified directory.
1127 The timestamp is hardcoded to be in epoch
1128 '''
1129 self.handle.sendline("")
1130 self.handle.expect("\$")
andrewonlabefd7f3d2014-10-21 16:02:31 -04001131 self.handle.sendline("")
andrewonlabba44bcf2014-10-16 16:54:41 -04001132 self.handle.sendline("tshark -i "+str(interface)+
andrewonlabd4940492014-10-24 12:21:27 -04001133 " -t e | grep --line-buffered \""+str(grep)+"\" >"+directory+" &")
andrewonlabba44bcf2014-10-16 16:54:41 -04001134 self.handle.sendline("\r")
1135 self.handle.expect("Capturing on")
1136 self.handle.sendline("\r")
1137 self.handle.expect("\$")
1138
1139 def tshark_stop(self):
1140 '''
1141 Removes wireshark files from /tmp and kills all tshark processes
1142 '''
andrewonlab5ba0d9e2014-10-24 13:32:23 -04001143 #Remove all pcap from previous captures
1144 self.execute(cmd="sudo rm /tmp/wireshark*")
andrewonlabba44bcf2014-10-16 16:54:41 -04001145 self.handle.sendline("")
1146 self.handle.sendline("sudo kill -9 `ps -ef | grep \"tshark -i\" |"+
1147 " grep -v grep | awk '{print $2}'`")
1148 self.handle.sendline("")
1149 main.log.info("Tshark stopped")
1150
andrewonlab0c38a4a2014-10-28 18:35:35 -04001151 def ptpd(self, args):
1152 '''
1153 Initiate ptp with user-specified args.
1154 Required:
1155 * args: specify string of args after command
1156 'sudo ptpd'
1157 '''
1158 try:
1159 self.handle.sendline("sudo ptpd "+str(args))
andrewonlab978dff62014-10-29 16:53:35 -04001160 i = self.handle.expect([
andrewonlab0c38a4a2014-10-28 18:35:35 -04001161 "Multiple",
1162 "Error",
1163 "\$"])
1164 self.handle.expect("\$")
andrewonlabba44bcf2014-10-16 16:54:41 -04001165
andrewonlab0c38a4a2014-10-28 18:35:35 -04001166 if i == 0:
1167 handle = self.handle.before
1168 main.log.info("ptpd returned an error: "+
1169 str(handle))
1170 return handle
1171 elif i == 1:
1172 handle = self.handle.before
1173 main.log.error("ptpd returned an error: "+
1174 str(handle))
1175 return handle
1176 else:
1177 return main.TRUE
1178
1179 except pexpect.EOF:
1180 main.log.error(self.name + ": EOF exception found")
1181 main.log.error(self.name + ": " + self.handle.before)
1182 main.cleanup()
1183 main.exit()
1184 except:
1185 main.log.info(self.name+" ::::::")
1186 main.log.error( traceback.print_exc())
1187 main.log.info(self.name+" ::::::")
1188 main.cleanup()
1189 main.exit()
andrewonlabba44bcf2014-10-16 16:54:41 -04001190
andrewonlab5d7a8f32014-11-10 13:07:56 -05001191 def cp_logs_to_dir(self, log_to_copy,
1192 dest_dir, copy_file_name=""):
1193 '''
1194 Copies logs to a desired directory.
1195 Current implementation of ONOS deletes its karaf
1196 logs on every iteration. For debugging purposes,
1197 you may want to use this function to capture
1198 certain karaf logs. (or any other logs if needed)
1199 Localtime will be attached to the filename
1200
1201 Required:
1202 * log_to_copy: specify directory and log name to
1203 copy.
1204 ex) /opt/onos/log/karaf.log.1
1205 For copying multiple files, leave copy_file_name
1206 empty and only specify dest_dir -
1207 ex) /opt/onos/log/karaf*
1208 * dest_dir: specify directory to copy to.
1209 ex) /tmp/
1210 Optional:
1211 * copy_file_name: If you want to rename the log
1212 file, specify copy_file_name. This will not work
1213 with multiple file copying
1214 '''
1215 try:
1216 localtime = time.strftime('%x %X')
1217 localtime = localtime.replace("/","")
1218 localtime = localtime.replace(" ","_")
1219 localtime = localtime.replace(":","")
1220 if dest_dir[-1:] != "/":
1221 dest_dir += "/"
1222
1223 if copy_file_name:
1224 self.handle.sendline("cp "+str(log_to_copy)+
1225 " "+str(dest_dir)+str(copy_file_name)+
1226 localtime)
1227 self.handle.expect("cp")
1228 self.handle.expect("\$")
1229 else:
1230 self.handle.sendline("cp "+str(log_to_copy)+
1231 " "+str(dest_dir))
1232 self.handle.expect("cp")
1233 self.handle.expect("\$")
1234
1235 return self.handle.before
1236
1237 except pexpect.EOF:
1238 main.log.error("Copying files failed")
1239 main.log.error(self.name + ": EOF exception found")
1240 main.log.error(self.name + ": " + self.handle.before)
1241 except:
1242 main.log.error("Copying files failed")
1243 main.log.info(self.name+" ::::::")
1244 main.log.error( traceback.print_exc())
1245 main.log.info(self.name+" ::::::")
1246