blob: beb836aed5f606ad51fefd363886b6e68d4f431a [file] [log] [blame]
Jon Hall05b2b432014-10-08 19:53:25 -04001#!/usr/bin/env python
2'''
Jon Hall05b2b432014-10-08 19:53:25 -04003'''
Jon Hallea7818b2014-10-09 14:30:59 -04004#TODO: Document
Jon Hall05b2b432014-10-08 19:53:25 -04005
andrewonlab7735d852014-10-09 13:02:47 -04006import sys
Jon Hall05b2b432014-10-08 19:53:25 -04007import time
8import pexpect
9import re
10import traceback
andrewonlab7735d852014-10-09 13:02:47 -040011import os.path
Jon Hall05b2b432014-10-08 19:53:25 -040012sys.path.append("../")
13from drivers.common.clidriver import CLI
14
15class OnosDriver(CLI):
16
17 def __init__(self):
18 super(CLI, self).__init__()
19
20 def connect(self,**connectargs):
21 '''
22 Creates ssh handle for ONOS "bench".
23 '''
24 try:
25 for key in connectargs:
26 vars(self)[key] = connectargs[key]
27 self.home = "~/ONOS"
28 for key in self.options:
29 if key == "home":
30 self.home = self.options['home']
31 break
32
33
34 self.name = self.options['name']
Jon Hallea7818b2014-10-09 14:30:59 -040035 self.handle = super(OnosDriver,self).connect(
36 user_name = self.user_name,
37 ip_address = self.ip_address,
38 port = self.port,
39 pwd = self.pwd,
40 home = self.home)
Jon Hall05b2b432014-10-08 19:53:25 -040041
Jon Hallea7818b2014-10-09 14:30:59 -040042
43 self.handle.sendline("cd "+ self.home)
44 self.handle.expect("\$")
Jon Hall05b2b432014-10-08 19:53:25 -040045 if self.handle:
46 return self.handle
47 else :
48 main.log.info("NO ONOS HANDLE")
49 return main.FALSE
50 except pexpect.EOF:
51 main.log.error(self.name + ": EOF exception found")
52 main.log.error(self.name + ": " + self.handle.before)
53 main.cleanup()
54 main.exit()
55 except:
56 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
57 main.log.error( traceback.print_exc() )
58 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
59 main.cleanup()
60 main.exit()
61
62 def disconnect(self):
63 '''
64 Called when Test is complete to disconnect the ONOS handle.
65 '''
66 response = ''
67 try:
68 self.handle.sendline("exit")
69 self.handle.expect("closed")
70 except pexpect.EOF:
71 main.log.error(self.name + ": EOF exception found")
72 main.log.error(self.name + ": " + self.handle.before)
73 except:
74 main.log.error(self.name + ": Connection failed to the host")
75 response = main.FALSE
76 return response
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -040077
78 def onos_package(self):
79 '''
80 Produce a self-contained tar.gz file that can be deployed
81 and executed on any platform with Java 7 JRE.
82 '''
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -040083
84 try:
85 self.handle.sendline("onos-package")
Jon Hallea7818b2014-10-09 14:30:59 -040086 self.handle.expect("onos-package")
andrewonlab0748d2a2014-10-09 13:24:17 -040087 self.handle.expect("tar.gz",timeout=10)
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -040088 handle = str(self.handle.before)
89 main.log.info("onos-package command returned: "+
90 handle)
andrewonlab0748d2a2014-10-09 13:24:17 -040091 #As long as the sendline does not time out,
92 #return true. However, be careful to interpret
93 #the results of the onos-package command return
94 return main.TRUE
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -040095
andrewonlab7735d852014-10-09 13:02:47 -040096 except pexpect.EOF:
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -040097 main.log.error(self.name + ": EOF exception found")
98 main.log.error(self.name + ": " + self.handle.before)
99 except:
100 main.log.error("Failed to package ONOS")
101 main.cleanup()
102 main.exit()
103
Jon Hallde9d9aa2014-10-08 20:36:02 -0400104 def clean_install(self):
105 '''
106 Runs mvn clean install in the root of the ONOS directory.
107 This will clean all ONOS artifacts then compile each module
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400108
Jon Hallde9d9aa2014-10-08 20:36:02 -0400109 Returns: main.TRUE on success
110 On Failure, exits the test
111 '''
112 try:
Jon Hallea7818b2014-10-09 14:30:59 -0400113 main.log.info("Running 'mvn clean install' on " + str(self.name) +
114 ". This may take some time.")
115 self.handle.sendline("cd "+ self.home)
116 self.handle.expect("\$")
117
118 self.handle.sendline("\n")
119 self.handle.expect("\$")
Jon Hallde9d9aa2014-10-08 20:36:02 -0400120 self.handle.sendline("mvn clean install")
Jon Hallea7818b2014-10-09 14:30:59 -0400121 self.handle.expect("mvn clean install")
Jon Hallde9d9aa2014-10-08 20:36:02 -0400122 while 1:
123 i=self.handle.expect([
124 'There\sis\sinsufficient\smemory\sfor\sthe\sJava\s\
125 Runtime\sEnvironment\sto\scontinue',
126 'BUILD\sFAILURE',
127 'BUILD\sSUCCESS',
128 'ONOS\$',
129 pexpect.TIMEOUT],timeout=600)
Jon Hallea7818b2014-10-09 14:30:59 -0400130 #TODO: log the build time
Jon Hallde9d9aa2014-10-08 20:36:02 -0400131 if i == 0:
132 main.log.error(self.name + ":There is insufficient memory \
133 for the Java Runtime Environment to continue.")
134 #return main.FALSE
135 main.cleanup()
136 main.exit()
137 if i == 1:
138 main.log.error(self.name + ": Build failure!")
139 #return main.FALSE
140 main.cleanup()
141 main.exit()
142 elif i == 2:
143 main.log.info(self.name + ": Build success!")
144 elif i == 3:
145 main.log.info(self.name + ": Build complete")
Jon Hallea7818b2014-10-09 14:30:59 -0400146 self.handle.sendline("\n")
Jon Hallde9d9aa2014-10-08 20:36:02 -0400147 self.handle.expect("\$", timeout=60)
148 return main.TRUE
149 elif i == 4:
150 main.log.error(self.name + ": mvn clean install TIMEOUT!")
151 #return main.FALSE
152 main.cleanup()
153 main.exit()
154 else:
155 main.log.error(self.name + ": unexpected response from \
156 mvn clean install")
157 #return main.FALSE
158 main.cleanup()
159 main.exit()
160 except pexpect.EOF:
161 main.log.error(self.name + ": EOF exception found")
162 main.log.error(self.name + ": " + self.handle.before)
163 main.cleanup()
164 main.exit()
165 except:
166 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
167 main.log.error( traceback.print_exc() )
168 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
169 main.cleanup()
170 main.exit()
Jon Hallacabffd2014-10-09 12:36:53 -0400171
172 def git_pull(self, comp1=""):
173 '''
174 Assumes that "git pull" works without login
175
176 This function will perform a git pull on the ONOS instance.
177 If used as git_pull("NODE") it will do git pull + NODE. This is
178 for the purpose of pulling from other nodes if necessary.
179
180 Otherwise, this function will perform a git pull in the
181 ONOS repository. If it has any problems, it will return main.ERROR
182 If it successfully does a git_pull, it will return a 1.
183 If it has no updates, it will return a 0.
184
185 '''
186 try:
187 # main.log.info(self.name + ": Stopping ONOS")
188 #self.stop()
189 self.handle.sendline("cd " + self.home)
190 self.handle.expect("ONOS\$")
191 if comp1=="":
192 self.handle.sendline("git pull")
193 else:
194 self.handle.sendline("git pull " + comp1)
195
196 uptodate = 0
197 i=self.handle.expect(['fatal',
198 'Username\sfor\s(.*):\s',
199 '\sfile(s*) changed,\s',
200 'Already up-to-date',
201 'Aborting',
202 'You\sare\snot\scurrently\son\sa\sbranch',
203 'You\sasked\sme\sto\spull\swithout\stelling\sme\swhich\sbranch\syou',
204 'Pull\sis\snot\spossible\sbecause\syou\shave\sunmerged\sfiles',
205 pexpect.TIMEOUT],
206 timeout=300)
207 #debug
208 #main.log.report(self.name +": \n"+"git pull response: " + str(self.handle.before) + str(self.handle.after))
209 if i==0:
210 main.log.error(self.name + ": Git pull had some issue...")
211 return main.ERROR
212 elif i==1:
213 main.log.error(self.name + ": Git Pull Asking for username. ")
214 return main.ERROR
215 elif i==2:
216 main.log.info(self.name + ": Git Pull - pulling repository now")
217 self.handle.expect("ONOS\$", 120)
218 return 0
219 elif i==3:
220 main.log.info(self.name + ": Git Pull - Already up to date")
221 return 1
222 elif i==4:
223 main.log.info(self.name + ": Git Pull - Aborting... Are there conflicting git files?")
224 return main.ERROR
225 elif i==5:
226 main.log.info(self.name + ": Git Pull - You are not currently on a branch so git pull failed!")
227 return main.ERROR
228 elif i==6:
229 main.log.info(self.name + ": Git Pull - You have not configured an upstream branch to pull from. Git pull failed!")
230 return main.ERROR
231 elif i==7:
232 main.log.info(self.name + ": Git Pull - Pull is not possible because you have unmerged files.")
233 return main.ERROR
234 elif i==8:
235 main.log.error(self.name + ": Git Pull - TIMEOUT")
236 main.log.error(self.name + " Response was: " + str(self.handle.before))
237 return main.ERROR
238 else:
239 main.log.error(self.name + ": Git Pull - Unexpected response, check for pull errors")
240 return main.ERROR
241 except pexpect.EOF:
242 main.log.error(self.name + ": EOF exception found")
243 main.log.error(self.name + ": " + self.handle.before)
244 main.cleanup()
245 main.exit()
246 except:
247 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
248 main.log.error( traceback.print_exc() )
249 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
250 main.cleanup()
251 main.exit()
252
253 def git_checkout(self, branch="master"):
254 '''
255 Assumes that "git pull" works without login
256
257 This function will perform a git git checkout on the ONOS instance.
258 If used as git_checkout("branch") it will do git checkout of the "branch".
259
260 Otherwise, this function will perform a git checkout of the master
261 branch of the ONOS repository. If it has any problems, it will return
262 main.ERROR.
263 If the branch was already the specified branch, or the git checkout was
264 successful then the function will return main.TRUE.
265
266 '''
267 try:
268 # main.log.info(self.name + ": Stopping ONOS")
269 #self.stop()
270 self.handle.sendline("cd " + self.home)
271 self.handle.expect("ONOS\$")
272 if branch != 'master':
273 #self.handle.sendline('git stash')
274 #self.handle.expect('ONOS\$')
275 #print "After issuing git stash cmnd: ", self.handle.before
276 cmd = "git checkout "+branch
277 print "checkout cmd = ", cmd
278 self.handle.sendline(cmd)
279 uptodate = 0
280 i=self.handle.expect(['fatal',
281 'Username\sfor\s(.*):\s',
282 'Already\son\s\'',
283 'Switched\sto\sbranch\s\'',
284 pexpect.TIMEOUT],timeout=60)
285 else:
286 #self.handle.sendline('git stash apply')
287 #self.handle.expect('ONOS\$')
288 #print "After issuing git stash apply cmnd: ", self.handle.before
289 cmd = "git checkout "+branch
290 print "checkout cmd = ", cmd
291 self.handle.sendline(cmd)
292 uptodate = 0
293 switchedToMaster = 0
294 i=self.handle.expect(['fatal',
295 'Username\sfor\s(.*):\s',
296 'Already\son\s\'master\'',
297 'Switched\sto\sbranch\s\'master\'',
298 pexpect.TIMEOUT],timeout=60)
299
300
301 if i==0:
302 main.log.error(self.name + ": Git checkout had some issue...")
303 return main.ERROR
304 elif i==1:
305 main.log.error(self.name + ": Git checkout Asking for username!!! Bad!")
306 return main.ERROR
307 elif i==2:
308 main.log.info(self.name + ": Git Checkout %s : Already on this branch" %branch)
309 self.handle.expect("ONOS\$")
310 print "after checkout cmd = ", self.handle.before
311 switchedToMaster = 1
312 return main.TRUE
313 elif i==3:
314 main.log.info(self.name + ": Git checkout %s - Switched to this branch" %branch)
315 self.handle.expect("ONOS\$")
316 print "after checkout cmd = ", self.handle.before
317 switchedToMaster = 1
318 return main.TRUE
319 elif i==4:
320 main.log.error(self.name + ": Git Checkout- TIMEOUT")
321 main.log.error(self.name + " Response was: " + str(self.handle.before))
322 return main.ERROR
323 else:
324 main.log.error(self.name + ": Git Checkout - Unexpected response, check for pull errors")
325 return main.ERROR
326
327 except pexpect.EOF:
328 main.log.error(self.name + ": EOF exception found")
329 main.log.error(self.name + ": " + self.handle.before)
330 main.cleanup()
331 main.exit()
332 except:
333 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
334 main.log.error( traceback.print_exc() )
335 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
336 main.cleanup()
337 main.exit()
andrewonlab95ca1462014-10-09 14:04:24 -0400338
339 def set_cell(self, cellname):
340 '''
341 Calls 'cell <name>' to set the environment variables on ONOSbench
342 '''
343 try:
344 if not cellname:
345 main.log.error("Must define cellname")
346 main.cleanup()
347 main.exit()
348 else:
349 self.handle.sendline("cell "+str(cellname))
350 #Expect the cellname in the ONOS_CELL variable.
351 #Note that this variable name is subject to change
352 # and that this driver will have to change accordingly
353 self.handle.expect("ONOS_CELL="+str(cellname))
354 handle_before = self.handle.before
355 handle_after = self.handle.after
andrewonlabc03bf6c2014-10-09 14:56:18 -0400356 #Get the rest of the handle
357 self.handle.sendline("")
358 self.handle.expect("\$")
359 handle_more = self.handle.before
andrewonlab95ca1462014-10-09 14:04:24 -0400360
361 main.log.info("Cell call returned: "+handle_before+
andrewonlabc03bf6c2014-10-09 14:56:18 -0400362 handle_after + handle_more)
andrewonlab95ca1462014-10-09 14:04:24 -0400363
364 return main.TRUE
365
366 except pexpect.EOF:
367 main.log.error(self.name + ": EOF exception found")
368 main.log.error(self.name + ": " + self.handle.before)
369 main.cleanup()
370 main.exit()
371 except:
372 main.log.info(self.name+" ::::::")
373 main.log.error( traceback.print_exc())
374 main.log.info(self.name+" ::::::")
375 main.cleanup()
376 main.exit()
377
andrewonlabc03bf6c2014-10-09 14:56:18 -0400378 def verify_cell(self):
379 '''
380 Calls 'onos-verify-cell' to check for cell installation
381 '''
andrewonlab8d0d7d72014-10-09 16:33:15 -0400382 #TODO: Add meaningful expect value
383
andrewonlabc03bf6c2014-10-09 14:56:18 -0400384 try:
385 #Clean handle by sending empty and expecting $
386 self.handle.sendline("")
387 self.handle.expect("\$")
388 self.handle.sendline("onos-verify-cell")
389 self.handle.expect("\$")
390 handle_before = self.handle.before
391 handle_after = self.handle.after
392 #Get the rest of the handle
393 self.handle.sendline("")
394 self.handle.expect("\$")
395 handle_more = self.handle.before
396
397 main.log.info("Verify cell returned: "+handle_before+
398 handle_after + handle_more)
399
400 return main.TRUE
Jon Hall7993bfc2014-10-09 16:30:14 -0400401 except pexpect.EOF:
402 main.log.error(self.name + ": EOF exception found")
403 main.log.error(self.name + ": " + self.handle.before)
404 main.cleanup()
405 main.exit()
406 except:
407 main.log.info(self.name+" ::::::")
408 main.log.error( traceback.print_exc())
409 main.log.info(self.name+" ::::::")
410 main.cleanup()
411 main.exit()
412
413
414 def onos_install(self, options="-f", node = ""):
415 '''
416 Installs ONOS bits on the designated cell machine.
417 If -f option is provided, it also forces an uninstall.
418 Presently, install also includes onos-push-bits and
419 onos-config within.
420 The node option allows you to selectively only push the jar
421 files to certain onos nodes
422
423 Returns: main.TRUE on success and main.FALSE on failure
424 '''
425 try:
426 self.handle.sendline("onos-install " + options + " " + node)
427 self.handle.expect("onos-install ")
428 #NOTE: this timeout may need to change depending on the network and size of ONOS
429 i=self.handle.expect(["Network\sis\sunreachable",
430 "onos\sstart/running,\sprocess",
431 pexpect.TIMEOUT],timeout=60)
432
433
434 if i == 0:
435 main.log.warn("Network is unreachable")
436 return main.FALSE
437 elif i == 1:
438 main.log.info("ONOS was installed on the VM and started")
439 return main.TRUE
440 elif i == 2:
441 main.log.info("Installation of ONOS on the VM timed out")
442 return main.FALSE
andrewonlabc03bf6c2014-10-09 14:56:18 -0400443
444 except pexpect.EOF:
445 main.log.error(self.name + ": EOF exception found")
446 main.log.error(self.name + ": " + self.handle.before)
447 main.cleanup()
448 main.exit()
449 except:
450 main.log.info(self.name+" ::::::")
451 main.log.error( traceback.print_exc())
452 main.log.info(self.name+" ::::::")
453 main.cleanup()
454 main.exit()
andrewonlab95ca1462014-10-09 14:04:24 -0400455
andrewonlab8d0d7d72014-10-09 16:33:15 -0400456 def onos_start(self, node_ip):
457 '''
458 Calls onos command: 'onos-service [<node-ip>] start'
459 '''
460
461 try:
462 self.handle.sendline("")
463 self.handle.expect("\$")
464 self.handle.sendline("onos-service "+str(node_ip)+
465 " start")
466 i = self.handle.expect([
467 "Job\sis\salready\srunning",
468 "start/running",
469 "Unknown\sinstance",
470 pexpect.TIMEOUT],timeout=60)
471
472 if i == 0:
473 main.log.info("Service is already running")
474 return main.TRUE
475 elif i == 1:
476 main.log.info("ONOS service started")
477 return main.TRUE
478 else:
479 main.log.error("ONOS service failed to start")
480 main.cleanup()
481 main.exit()
andrewonlab8d0d7d72014-10-09 16:33:15 -0400482 except pexpect.EOF:
483 main.log.error(self.name + ": EOF exception found")
484 main.log.error(self.name + ": " + self.handle.before)
485 main.cleanup()
486 main.exit()
487 except:
488 main.log.info(self.name+" ::::::")
489 main.log.error( traceback.print_exc())
490 main.log.info(self.name+" ::::::")
491 main.cleanup()
492 main.exit()
493
andrewonlab2b30bd32014-10-09 16:48:55 -0400494 def onos_stop(self, node_ip):
495 '''
496 Calls onos command: 'onos-service [<node-ip>] stop'
497 '''
498 try:
499 self.handle.sendline("")
500 self.handle.expect("\$")
501 self.handle.sendline("onos-service "+str(node_ip)+
502 " stop")
503 i = self.handle.expect([
504 "stop/waiting",
505 "Unknown\sinstance",
506 pexpect.TIMEOUT],timeout=60)
507
508 if i == 0:
509 main.log.info("ONOS service stopped")
510 return main.TRUE
511 elif i == 1:
512 main.log.info("Unknown ONOS instance specified: "+
513 str(node_ip))
514 return main.FALSE
515 else:
516 main.log.error("ONOS service failed to stop")
517 return main.FALSE
518
519 except pexpect.EOF:
520 main.log.error(self.name + ": EOF exception found")
521 main.log.error(self.name + ": " + self.handle.before)
522 main.cleanup()
523 main.exit()
524 except:
525 main.log.info(self.name+" ::::::")
526 main.log.error( traceback.print_exc())
527 main.log.info(self.name+" ::::::")
528 main.cleanup()
529 main.exit()
530
andrewonlabc8d47972014-10-09 16:52:36 -0400531 def onos_uninstall(self):
532 '''
533 Calls the command: 'onos-uninstall'
534 '''
535 try:
536 self.handle.sendline("")
537 self.handle.expect("\$")
538 self.handle.sendline("onos-uninstall")
539 self.handle.expect("\$")
540
541 #onos-uninstall command does not return any text
542 return main.TRUE
543
544 except pexpect.EOF:
545 main.log.error(self.name + ": EOF exception found")
546 main.log.error(self.name + ": " + self.handle.before)
547 main.cleanup()
548 main.exit()
549 except:
550 main.log.info(self.name+" ::::::")
551 main.log.error( traceback.print_exc())
552 main.log.info(self.name+" ::::::")
553 main.cleanup()
554 main.exit()
andrewonlab2b30bd32014-10-09 16:48:55 -0400555
Jon Hall7993bfc2014-10-09 16:30:14 -0400556 def isup(self, node = ""):
557 '''
558 Run's onos-wait-for-start which only returns once ONOS is at run level 100(ready for use)
andrewonlab8d0d7d72014-10-09 16:33:15 -0400559
Jon Hall7993bfc2014-10-09 16:30:14 -0400560 Returns: main.TRUE if ONOS is running and main.FALSE on timeout
561 '''
562 try:
563 self.handle.sendline("onos-wait-for-start " + node )
564 self.handle.expect("onos-wait-for-start")
565 #NOTE: this timeout is arbitrary"
566 i = self.handle.expect(["\$", pexpect.TIMEOUT], timeout = 120)
567 if i == 0:
568 main.log.info(self.name + ": " + node + " is up")
569 return main.TRUE
570 elif i == 1:
571 #NOTE: since this function won't return until ONOS is ready,
572 # we will kill it on timeout
573 self.handle.sendline("\003") #Control-C
574 self.handle.expect("\$")
575 return main.FALSE
576 except pexpect.EOF:
577 main.log.error(self.name + ": EOF exception found")
578 main.log.error(self.name + ": " + self.handle.before)
579 main.cleanup()
580 main.exit()
581 except:
582 main.log.info(self.name+" ::::::")
583 main.log.error( traceback.print_exc())
584 main.log.info(self.name+" ::::::")
585 main.cleanup()
586 main.exit()