blob: 914ce45b306ac02bec10e4170f34df15c4839096 [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
401
402 except pexpect.EOF:
403 main.log.error(self.name + ": EOF exception found")
404 main.log.error(self.name + ": " + self.handle.before)
405 main.cleanup()
406 main.exit()
407 except:
408 main.log.info(self.name+" ::::::")
409 main.log.error( traceback.print_exc())
410 main.log.info(self.name+" ::::::")
411 main.cleanup()
412 main.exit()
andrewonlab95ca1462014-10-09 14:04:24 -0400413
andrewonlab8d0d7d72014-10-09 16:33:15 -0400414 def onos_start(self, node_ip):
415 '''
416 Calls onos command: 'onos-service [<node-ip>] start'
417 '''
418
419 try:
420 self.handle.sendline("")
421 self.handle.expect("\$")
422 self.handle.sendline("onos-service "+str(node_ip)+
423 " start")
424 i = self.handle.expect([
425 "Job\sis\salready\srunning",
426 "start/running",
427 "Unknown\sinstance",
428 pexpect.TIMEOUT],timeout=60)
429
430 if i == 0:
431 main.log.info("Service is already running")
432 return main.TRUE
433 elif i == 1:
434 main.log.info("ONOS service started")
435 return main.TRUE
436 else:
437 main.log.error("ONOS service failed to start")
438 main.cleanup()
439 main.exit()
440
441 except pexpect.EOF:
442 main.log.error(self.name + ": EOF exception found")
443 main.log.error(self.name + ": " + self.handle.before)
444 main.cleanup()
445 main.exit()
446 except:
447 main.log.info(self.name+" ::::::")
448 main.log.error( traceback.print_exc())
449 main.log.info(self.name+" ::::::")
450 main.cleanup()
451 main.exit()
452
453
454
455
456