blob: 0518188734c8c1d524c3370c5ccd92b9033e6b67 [file] [log] [blame]
Jon Hall05b2b432014-10-08 19:53:25 -04001#!/usr/bin/env python
2'''
3TODO: Document
4'''
5
6
7import time
8import pexpect
9import re
10import traceback
11sys.path.append("../")
12from drivers.common.clidriver import CLI
13
14class OnosDriver(CLI):
15
16 def __init__(self):
17 super(CLI, self).__init__()
18
19 def connect(self,**connectargs):
20 '''
21 Creates ssh handle for ONOS "bench".
22 '''
23 try:
24 for key in connectargs:
25 vars(self)[key] = connectargs[key]
26 self.home = "~/ONOS"
27 for key in self.options:
28 if key == "home":
29 self.home = self.options['home']
30 break
31
32
33 self.name = self.options['name']
34 self.handle = super(OnosCliDriver,self).connect(user_name = self.user_name, ip_address = self.ip_address,port = self.port, pwd = self.pwd, home = self.home)
35
36 if self.handle:
37 return self.handle
38 else :
39 main.log.info("NO ONOS HANDLE")
40 return main.FALSE
41 except pexpect.EOF:
42 main.log.error(self.name + ": EOF exception found")
43 main.log.error(self.name + ": " + self.handle.before)
44 main.cleanup()
45 main.exit()
46 except:
47 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
48 main.log.error( traceback.print_exc() )
49 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
50 main.cleanup()
51 main.exit()
52
53 def disconnect(self):
54 '''
55 Called when Test is complete to disconnect the ONOS handle.
56 '''
57 response = ''
58 try:
59 self.handle.sendline("exit")
60 self.handle.expect("closed")
61 except pexpect.EOF:
62 main.log.error(self.name + ": EOF exception found")
63 main.log.error(self.name + ": " + self.handle.before)
64 except:
65 main.log.error(self.name + ": Connection failed to the host")
66 response = main.FALSE
67 return response
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -040068
69 def onos_package(self):
70 '''
71 Produce a self-contained tar.gz file that can be deployed
72 and executed on any platform with Java 7 JRE.
73 '''
74 import os.path
75
76 try:
77 self.handle.sendline("onos-package")
78 self.handle.expect("\$")
79 handle = str(self.handle.before)
80 main.log.info("onos-package command returned: "+
81 handle)
82
83 #Create list out of the handle by partitioning
84 #spaces.
85 #NOTE: The last element of the list at the time
86 # of writing this function is the filepath
87 #save this filepath for comparison later on
88 temp_list = handle.split(" ")
89 file_path = handle[-1:]
90
91 #If last string contains the filepath, return
92 # as success.
93 if "/tmp" in file_path:
94 return main.TRUE
95 else:
96 return main.FALSE
97
98 except:
99 main.log.error(self.name + ": EOF exception found")
100 main.log.error(self.name + ": " + self.handle.before)
101 except:
102 main.log.error("Failed to package ONOS")
103 main.cleanup()
104 main.exit()
105
Jon Hallde9d9aa2014-10-08 20:36:02 -0400106 def clean_install(self):
107 '''
108 Runs mvn clean install in the root of the ONOS directory.
109 This will clean all ONOS artifacts then compile each module
andrew@onlab.us9e2cd0f2014-10-08 20:32:32 -0400110
Jon Hallde9d9aa2014-10-08 20:36:02 -0400111 Returns: main.TRUE on success
112 On Failure, exits the test
113 '''
114 try:
115 self.handle.sendline("mvn clean install")
116 while 1:
117 i=self.handle.expect([
118 'There\sis\sinsufficient\smemory\sfor\sthe\sJava\s\
119 Runtime\sEnvironment\sto\scontinue',
120 'BUILD\sFAILURE',
121 'BUILD\sSUCCESS',
122 'ONOS\$',
123 pexpect.TIMEOUT],timeout=600)
124 if i == 0:
125 main.log.error(self.name + ":There is insufficient memory \
126 for the Java Runtime Environment to continue.")
127 #return main.FALSE
128 main.cleanup()
129 main.exit()
130 if i == 1:
131 main.log.error(self.name + ": Build failure!")
132 #return main.FALSE
133 main.cleanup()
134 main.exit()
135 elif i == 2:
136 main.log.info(self.name + ": Build success!")
137 elif i == 3:
138 main.log.info(self.name + ": Build complete")
139 self.handle.expect("\$", timeout=60)
140 return main.TRUE
141 elif i == 4:
142 main.log.error(self.name + ": mvn clean install TIMEOUT!")
143 #return main.FALSE
144 main.cleanup()
145 main.exit()
146 else:
147 main.log.error(self.name + ": unexpected response from \
148 mvn clean install")
149 #return main.FALSE
150 main.cleanup()
151 main.exit()
152 except pexpect.EOF:
153 main.log.error(self.name + ": EOF exception found")
154 main.log.error(self.name + ": " + self.handle.before)
155 main.cleanup()
156 main.exit()
157 except:
158 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
159 main.log.error( traceback.print_exc() )
160 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
161 main.cleanup()
162 main.exit()
Jon Hallacabffd2014-10-09 12:36:53 -0400163
164 def git_pull(self, comp1=""):
165 '''
166 Assumes that "git pull" works without login
167
168 This function will perform a git pull on the ONOS instance.
169 If used as git_pull("NODE") it will do git pull + NODE. This is
170 for the purpose of pulling from other nodes if necessary.
171
172 Otherwise, this function will perform a git pull in the
173 ONOS repository. If it has any problems, it will return main.ERROR
174 If it successfully does a git_pull, it will return a 1.
175 If it has no updates, it will return a 0.
176
177 '''
178 try:
179 # main.log.info(self.name + ": Stopping ONOS")
180 #self.stop()
181 self.handle.sendline("cd " + self.home)
182 self.handle.expect("ONOS\$")
183 if comp1=="":
184 self.handle.sendline("git pull")
185 else:
186 self.handle.sendline("git pull " + comp1)
187
188 uptodate = 0
189 i=self.handle.expect(['fatal',
190 'Username\sfor\s(.*):\s',
191 '\sfile(s*) changed,\s',
192 'Already up-to-date',
193 'Aborting',
194 'You\sare\snot\scurrently\son\sa\sbranch',
195 'You\sasked\sme\sto\spull\swithout\stelling\sme\swhich\sbranch\syou',
196 'Pull\sis\snot\spossible\sbecause\syou\shave\sunmerged\sfiles',
197 pexpect.TIMEOUT],
198 timeout=300)
199 #debug
200 #main.log.report(self.name +": \n"+"git pull response: " + str(self.handle.before) + str(self.handle.after))
201 if i==0:
202 main.log.error(self.name + ": Git pull had some issue...")
203 return main.ERROR
204 elif i==1:
205 main.log.error(self.name + ": Git Pull Asking for username. ")
206 return main.ERROR
207 elif i==2:
208 main.log.info(self.name + ": Git Pull - pulling repository now")
209 self.handle.expect("ONOS\$", 120)
210 return 0
211 elif i==3:
212 main.log.info(self.name + ": Git Pull - Already up to date")
213 return 1
214 elif i==4:
215 main.log.info(self.name + ": Git Pull - Aborting... Are there conflicting git files?")
216 return main.ERROR
217 elif i==5:
218 main.log.info(self.name + ": Git Pull - You are not currently on a branch so git pull failed!")
219 return main.ERROR
220 elif i==6:
221 main.log.info(self.name + ": Git Pull - You have not configured an upstream branch to pull from. Git pull failed!")
222 return main.ERROR
223 elif i==7:
224 main.log.info(self.name + ": Git Pull - Pull is not possible because you have unmerged files.")
225 return main.ERROR
226 elif i==8:
227 main.log.error(self.name + ": Git Pull - TIMEOUT")
228 main.log.error(self.name + " Response was: " + str(self.handle.before))
229 return main.ERROR
230 else:
231 main.log.error(self.name + ": Git Pull - Unexpected response, check for pull errors")
232 return main.ERROR
233 except pexpect.EOF:
234 main.log.error(self.name + ": EOF exception found")
235 main.log.error(self.name + ": " + self.handle.before)
236 main.cleanup()
237 main.exit()
238 except:
239 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
240 main.log.error( traceback.print_exc() )
241 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
242 main.cleanup()
243 main.exit()
244
245 def git_checkout(self, branch="master"):
246 '''
247 Assumes that "git pull" works without login
248
249 This function will perform a git git checkout on the ONOS instance.
250 If used as git_checkout("branch") it will do git checkout of the "branch".
251
252 Otherwise, this function will perform a git checkout of the master
253 branch of the ONOS repository. If it has any problems, it will return
254 main.ERROR.
255 If the branch was already the specified branch, or the git checkout was
256 successful then the function will return main.TRUE.
257
258 '''
259 try:
260 # main.log.info(self.name + ": Stopping ONOS")
261 #self.stop()
262 self.handle.sendline("cd " + self.home)
263 self.handle.expect("ONOS\$")
264 if branch != 'master':
265 #self.handle.sendline('git stash')
266 #self.handle.expect('ONOS\$')
267 #print "After issuing git stash cmnd: ", self.handle.before
268 cmd = "git checkout "+branch
269 print "checkout cmd = ", cmd
270 self.handle.sendline(cmd)
271 uptodate = 0
272 i=self.handle.expect(['fatal',
273 'Username\sfor\s(.*):\s',
274 'Already\son\s\'',
275 'Switched\sto\sbranch\s\'',
276 pexpect.TIMEOUT],timeout=60)
277 else:
278 #self.handle.sendline('git stash apply')
279 #self.handle.expect('ONOS\$')
280 #print "After issuing git stash apply cmnd: ", self.handle.before
281 cmd = "git checkout "+branch
282 print "checkout cmd = ", cmd
283 self.handle.sendline(cmd)
284 uptodate = 0
285 switchedToMaster = 0
286 i=self.handle.expect(['fatal',
287 'Username\sfor\s(.*):\s',
288 'Already\son\s\'master\'',
289 'Switched\sto\sbranch\s\'master\'',
290 pexpect.TIMEOUT],timeout=60)
291
292
293 if i==0:
294 main.log.error(self.name + ": Git checkout had some issue...")
295 return main.ERROR
296 elif i==1:
297 main.log.error(self.name + ": Git checkout Asking for username!!! Bad!")
298 return main.ERROR
299 elif i==2:
300 main.log.info(self.name + ": Git Checkout %s : Already on this branch" %branch)
301 self.handle.expect("ONOS\$")
302 print "after checkout cmd = ", self.handle.before
303 switchedToMaster = 1
304 return main.TRUE
305 elif i==3:
306 main.log.info(self.name + ": Git checkout %s - Switched to this branch" %branch)
307 self.handle.expect("ONOS\$")
308 print "after checkout cmd = ", self.handle.before
309 switchedToMaster = 1
310 return main.TRUE
311 elif i==4:
312 main.log.error(self.name + ": Git Checkout- TIMEOUT")
313 main.log.error(self.name + " Response was: " + str(self.handle.before))
314 return main.ERROR
315 else:
316 main.log.error(self.name + ": Git Checkout - Unexpected response, check for pull errors")
317 return main.ERROR
318
319 except pexpect.EOF:
320 main.log.error(self.name + ": EOF exception found")
321 main.log.error(self.name + ": " + self.handle.before)
322 main.cleanup()
323 main.exit()
324 except:
325 main.log.info(self.name + ":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
326 main.log.error( traceback.print_exc() )
327 main.log.info(":::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::")
328 main.cleanup()
329 main.exit()