blob: 9ebfafdcefc5133f63c373ed2d47ac92b09203a8 [file] [log] [blame]
Jon Hall05b2b432014-10-08 19:53:25 -04001#!/usr/bin/env python
2'''
3TODO: Document
4'''
5
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']
andrewonlab7735d852014-10-09 13:02:47 -040035 self.handle = super(OnosDriver,self).connect(user_name = self.user_name, ip_address = self.ip_address,port = self.port, pwd = self.pwd, home = self.home)
Jon Hall05b2b432014-10-08 19:53:25 -040036
37 if self.handle:
38 return self.handle
39 else :
40 main.log.info("NO ONOS HANDLE")
41 return main.FALSE
42 except pexpect.EOF:
43 main.log.error(self.name + ": EOF exception found")
44 main.log.error(self.name + ": " + self.handle.before)
45 main.cleanup()
46 main.exit()
47 except:
48 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
49 main.log.error( traceback.print_exc() )
50 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
51 main.cleanup()
52 main.exit()
53
54 def disconnect(self):
55 '''
56 Called when Test is complete to disconnect the ONOS handle.
57 '''
58 response = ''
59 try:
60 self.handle.sendline("exit")
61 self.handle.expect("closed")
62 except pexpect.EOF:
63 main.log.error(self.name + ": EOF exception found")
64 main.log.error(self.name + ": " + self.handle.before)
65 except:
66 main.log.error(self.name + ": Connection failed to the host")
67 response = main.FALSE
68 return response
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -040069
70 def onos_package(self):
71 '''
72 Produce a self-contained tar.gz file that can be deployed
73 and executed on any platform with Java 7 JRE.
74 '''
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -040075
76 try:
77 self.handle.sendline("onos-package")
andrewonlab0748d2a2014-10-09 13:24:17 -040078 self.handle.expect("tar.gz",timeout=10)
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -040079 handle = str(self.handle.before)
80 main.log.info("onos-package command returned: "+
81 handle)
andrewonlab0748d2a2014-10-09 13:24:17 -040082 #As long as the sendline does not time out,
83 #return true. However, be careful to interpret
84 #the results of the onos-package command return
85 return main.TRUE
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -040086
andrewonlab7735d852014-10-09 13:02:47 -040087 except pexpect.EOF:
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -040088 main.log.error(self.name + ": EOF exception found")
89 main.log.error(self.name + ": " + self.handle.before)
90 except:
91 main.log.error("Failed to package ONOS")
92 main.cleanup()
93 main.exit()
94
Jon Hallde9d9aa2014-10-08 20:36:02 -040095 def clean_install(self):
96 '''
97 Runs mvn clean install in the root of the ONOS directory.
98 This will clean all ONOS artifacts then compile each module
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -040099
Jon Hallde9d9aa2014-10-08 20:36:02 -0400100 Returns: main.TRUE on success
101 On Failure, exits the test
102 '''
103 try:
104 self.handle.sendline("mvn clean install")
105 while 1:
106 i=self.handle.expect([
107 'There\sis\sinsufficient\smemory\sfor\sthe\sJava\s\
108 Runtime\sEnvironment\sto\scontinue',
109 'BUILD\sFAILURE',
110 'BUILD\sSUCCESS',
111 'ONOS\$',
112 pexpect.TIMEOUT],timeout=600)
113 if i == 0:
114 main.log.error(self.name + ":There is insufficient memory \
115 for the Java Runtime Environment to continue.")
116 #return main.FALSE
117 main.cleanup()
118 main.exit()
119 if i == 1:
120 main.log.error(self.name + ": Build failure!")
121 #return main.FALSE
122 main.cleanup()
123 main.exit()
124 elif i == 2:
125 main.log.info(self.name + ": Build success!")
126 elif i == 3:
127 main.log.info(self.name + ": Build complete")
128 self.handle.expect("\$", timeout=60)
129 return main.TRUE
130 elif i == 4:
131 main.log.error(self.name + ": mvn clean install TIMEOUT!")
132 #return main.FALSE
133 main.cleanup()
134 main.exit()
135 else:
136 main.log.error(self.name + ": unexpected response from \
137 mvn clean install")
138 #return main.FALSE
139 main.cleanup()
140 main.exit()
141 except pexpect.EOF:
142 main.log.error(self.name + ": EOF exception found")
143 main.log.error(self.name + ": " + self.handle.before)
144 main.cleanup()
145 main.exit()
146 except:
147 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
148 main.log.error( traceback.print_exc() )
149 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
150 main.cleanup()
151 main.exit()
Jon Hallacabffd2014-10-09 12:36:53 -0400152
153 def git_pull(self, comp1=""):
154 '''
155 Assumes that "git pull" works without login
156
157 This function will perform a git pull on the ONOS instance.
158 If used as git_pull("NODE") it will do git pull + NODE. This is
159 for the purpose of pulling from other nodes if necessary.
160
161 Otherwise, this function will perform a git pull in the
162 ONOS repository. If it has any problems, it will return main.ERROR
163 If it successfully does a git_pull, it will return a 1.
164 If it has no updates, it will return a 0.
165
166 '''
167 try:
168 # main.log.info(self.name + ": Stopping ONOS")
169 #self.stop()
170 self.handle.sendline("cd " + self.home)
171 self.handle.expect("ONOS\$")
172 if comp1=="":
173 self.handle.sendline("git pull")
174 else:
175 self.handle.sendline("git pull " + comp1)
176
177 uptodate = 0
178 i=self.handle.expect(['fatal',
179 'Username\sfor\s(.*):\s',
180 '\sfile(s*) changed,\s',
181 'Already up-to-date',
182 'Aborting',
183 'You\sare\snot\scurrently\son\sa\sbranch',
184 'You\sasked\sme\sto\spull\swithout\stelling\sme\swhich\sbranch\syou',
185 'Pull\sis\snot\spossible\sbecause\syou\shave\sunmerged\sfiles',
186 pexpect.TIMEOUT],
187 timeout=300)
188 #debug
189 #main.log.report(self.name +": \n"+"git pull response: " + str(self.handle.before) + str(self.handle.after))
190 if i==0:
191 main.log.error(self.name + ": Git pull had some issue...")
192 return main.ERROR
193 elif i==1:
194 main.log.error(self.name + ": Git Pull Asking for username. ")
195 return main.ERROR
196 elif i==2:
197 main.log.info(self.name + ": Git Pull - pulling repository now")
198 self.handle.expect("ONOS\$", 120)
199 return 0
200 elif i==3:
201 main.log.info(self.name + ": Git Pull - Already up to date")
202 return 1
203 elif i==4:
204 main.log.info(self.name + ": Git Pull - Aborting... Are there conflicting git files?")
205 return main.ERROR
206 elif i==5:
207 main.log.info(self.name + ": Git Pull - You are not currently on a branch so git pull failed!")
208 return main.ERROR
209 elif i==6:
210 main.log.info(self.name + ": Git Pull - You have not configured an upstream branch to pull from. Git pull failed!")
211 return main.ERROR
212 elif i==7:
213 main.log.info(self.name + ": Git Pull - Pull is not possible because you have unmerged files.")
214 return main.ERROR
215 elif i==8:
216 main.log.error(self.name + ": Git Pull - TIMEOUT")
217 main.log.error(self.name + " Response was: " + str(self.handle.before))
218 return main.ERROR
219 else:
220 main.log.error(self.name + ": Git Pull - Unexpected response, check for pull errors")
221 return main.ERROR
222 except pexpect.EOF:
223 main.log.error(self.name + ": EOF exception found")
224 main.log.error(self.name + ": " + self.handle.before)
225 main.cleanup()
226 main.exit()
227 except:
228 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
229 main.log.error( traceback.print_exc() )
230 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
231 main.cleanup()
232 main.exit()
233
234 def git_checkout(self, branch="master"):
235 '''
236 Assumes that "git pull" works without login
237
238 This function will perform a git git checkout on the ONOS instance.
239 If used as git_checkout("branch") it will do git checkout of the "branch".
240
241 Otherwise, this function will perform a git checkout of the master
242 branch of the ONOS repository. If it has any problems, it will return
243 main.ERROR.
244 If the branch was already the specified branch, or the git checkout was
245 successful then the function will return main.TRUE.
246
247 '''
248 try:
249 # main.log.info(self.name + ": Stopping ONOS")
250 #self.stop()
251 self.handle.sendline("cd " + self.home)
252 self.handle.expect("ONOS\$")
253 if branch != 'master':
254 #self.handle.sendline('git stash')
255 #self.handle.expect('ONOS\$')
256 #print "After issuing git stash cmnd: ", self.handle.before
257 cmd = "git checkout "+branch
258 print "checkout cmd = ", cmd
259 self.handle.sendline(cmd)
260 uptodate = 0
261 i=self.handle.expect(['fatal',
262 'Username\sfor\s(.*):\s',
263 'Already\son\s\'',
264 'Switched\sto\sbranch\s\'',
265 pexpect.TIMEOUT],timeout=60)
266 else:
267 #self.handle.sendline('git stash apply')
268 #self.handle.expect('ONOS\$')
269 #print "After issuing git stash apply cmnd: ", self.handle.before
270 cmd = "git checkout "+branch
271 print "checkout cmd = ", cmd
272 self.handle.sendline(cmd)
273 uptodate = 0
274 switchedToMaster = 0
275 i=self.handle.expect(['fatal',
276 'Username\sfor\s(.*):\s',
277 'Already\son\s\'master\'',
278 'Switched\sto\sbranch\s\'master\'',
279 pexpect.TIMEOUT],timeout=60)
280
281
282 if i==0:
283 main.log.error(self.name + ": Git checkout had some issue...")
284 return main.ERROR
285 elif i==1:
286 main.log.error(self.name + ": Git checkout Asking for username!!! Bad!")
287 return main.ERROR
288 elif i==2:
289 main.log.info(self.name + ": Git Checkout %s : Already on this branch" %branch)
290 self.handle.expect("ONOS\$")
291 print "after checkout cmd = ", self.handle.before
292 switchedToMaster = 1
293 return main.TRUE
294 elif i==3:
295 main.log.info(self.name + ": Git checkout %s - Switched to this branch" %branch)
296 self.handle.expect("ONOS\$")
297 print "after checkout cmd = ", self.handle.before
298 switchedToMaster = 1
299 return main.TRUE
300 elif i==4:
301 main.log.error(self.name + ": Git Checkout- TIMEOUT")
302 main.log.error(self.name + " Response was: " + str(self.handle.before))
303 return main.ERROR
304 else:
305 main.log.error(self.name + ": Git Checkout - Unexpected response, check for pull errors")
306 return main.ERROR
307
308 except pexpect.EOF:
309 main.log.error(self.name + ": EOF exception found")
310 main.log.error(self.name + ": " + self.handle.before)
311 main.cleanup()
312 main.exit()
313 except:
314 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
315 main.log.error( traceback.print_exc() )
316 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
317 main.cleanup()
318 main.exit()
andrewonlab95ca1462014-10-09 14:04:24 -0400319
320 def set_cell(self, cellname):
321 '''
322 Calls 'cell <name>' to set the environment variables on ONOSbench
323 '''
324 try:
325 if not cellname:
326 main.log.error("Must define cellname")
327 main.cleanup()
328 main.exit()
329 else:
330 self.handle.sendline("cell "+str(cellname))
331 #Expect the cellname in the ONOS_CELL variable.
332 #Note that this variable name is subject to change
333 # and that this driver will have to change accordingly
334 self.handle.expect("ONOS_CELL="+str(cellname))
335 handle_before = self.handle.before
336 handle_after = self.handle.after
andrewonlabc03bf6c2014-10-09 14:56:18 -0400337 #Get the rest of the handle
338 self.handle.sendline("")
339 self.handle.expect("\$")
340 handle_more = self.handle.before
andrewonlab95ca1462014-10-09 14:04:24 -0400341
342 main.log.info("Cell call returned: "+handle_before+
andrewonlabc03bf6c2014-10-09 14:56:18 -0400343 handle_after + handle_more)
andrewonlab95ca1462014-10-09 14:04:24 -0400344
345 return main.TRUE
346
347 except pexpect.EOF:
348 main.log.error(self.name + ": EOF exception found")
349 main.log.error(self.name + ": " + self.handle.before)
350 main.cleanup()
351 main.exit()
352 except:
353 main.log.info(self.name+" ::::::")
354 main.log.error( traceback.print_exc())
355 main.log.info(self.name+" ::::::")
356 main.cleanup()
357 main.exit()
358
andrewonlabc03bf6c2014-10-09 14:56:18 -0400359 def verify_cell(self):
360 '''
361 Calls 'onos-verify-cell' to check for cell installation
362 '''
363 try:
364 #Clean handle by sending empty and expecting $
365 self.handle.sendline("")
366 self.handle.expect("\$")
367 self.handle.sendline("onos-verify-cell")
368 self.handle.expect("\$")
369 handle_before = self.handle.before
370 handle_after = self.handle.after
371 #Get the rest of the handle
372 self.handle.sendline("")
373 self.handle.expect("\$")
374 handle_more = self.handle.before
375
376 main.log.info("Verify cell returned: "+handle_before+
377 handle_after + handle_more)
378
379 return main.TRUE
380
381 except pexpect.EOF:
382 main.log.error(self.name + ": EOF exception found")
383 main.log.error(self.name + ": " + self.handle.before)
384 main.cleanup()
385 main.exit()
386 except:
387 main.log.info(self.name+" ::::::")
388 main.log.error( traceback.print_exc())
389 main.log.info(self.name+" ::::::")
390 main.cleanup()
391 main.exit()
andrewonlab95ca1462014-10-09 14:04:24 -0400392
393
394
395
396
397
398
399
400