blob: 3092bcdd6d78d96d856f833fc83c1bd84a70bdc6 [file] [log] [blame]
Hari Krishna6031b7c2015-09-28 17:46:29 -07001#!/usr/bin/env python
Jeremy Songsterae01bba2016-07-11 15:39:17 -07002"""
Jeremy Ronquillob27ce4c2017-07-17 12:41:28 -07003Copyright 2016 Open Networking Foundation (ONF)
Jeremy Songsterae01bba2016-07-11 15:39:17 -07004
5Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>,
6the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>,
7or the System Testing Guide page at <https://wiki.onosproject.org/x/WYQg>
Jeremy Ronquillob27ce4c2017-07-17 12:41:28 -07008
9 TestON is free software: you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation, either version 2 of the License, or
12 (at your option) any later version.
13
14 TestON is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with TestON. If not, see <http://www.gnu.org/licenses/>.
Jeremy Songsterae01bba2016-07-11 15:39:17 -070021"""
Hari Krishna6031b7c2015-09-28 17:46:29 -070022
23import json
24import os
25import re
26import subprocess
27from docker import Client
suibin zhangfd266fd2015-10-27 17:06:33 -070028from docker import errors
Hari Krishna6031b7c2015-09-28 17:46:29 -070029from drivers.common.apidriver import API
30
31class DockerApiDriver( API ):
32
33 def __init__( self ):
34 """
35 Initialize client
36 """
37 self.name = None
38 self.home = None
39 self.handle = None
Devin Limdc78e202017-06-09 18:30:07 -070040 super( DockerApiDriver, self ).__init__()
Hari Krishna6031b7c2015-09-28 17:46:29 -070041
42 def connect( self, **connectargs ):
43 """
44 Create Client handle to connnect to Docker server
45 """
46 try:
47 for key in connectargs:
48 vars( self )[ key ] = connectargs[ key ]
49 self.name = self.options[ 'name' ]
50 for key in self.options:
51 if key == "home":
52 self.home = self.options[ 'home' ]
53 break
54 if self.home is None or self.home == "":
55 self.home = "/var/tmp"
56
57 self.handle = super( DockerApiDriver, self ).connect()
58 self.dockerClient = Client(base_url='unix://var/run/docker.sock')
59 return self.handle
60 except Exception as e:
61 main.log.exception( e )
62
Pratik Parabcc406452017-02-28 15:15:25 -080063 def getListOfImages( self, repo="onosproject/onos" ):
64 """
65 Get the list of image tags
66 """
67 try:
68 imageList = list( self.dockerClient.images( name=repo ) )
69 imageListToSend = []
Pratik Parab50d53d42017-03-24 14:28:22 -070070 duplicateTagDetected = 0
Pratik Parabcc406452017-02-28 15:15:25 -080071 for imageDict in imageList:
72 if imageDict[ 'RepoTags' ] is not None:
Pratik Parab50d53d42017-03-24 14:28:22 -070073 if len( imageDict[ 'RepoTags' ] ) > 1:
74 duplicateTagDetected = 1
Pratik Parabcc406452017-02-28 15:15:25 -080075 imageListToSend.append( imageDict['RepoTags'][0].encode('UTF8').split(':')[1] )
Pratik Parab50d53d42017-03-24 14:28:22 -070076 return imageListToSend, duplicateTagDetected
Pratik Parabcc406452017-02-28 15:15:25 -080077 except Exception as e:
78 main.log.exception( e )
79
suibin zhangfd266fd2015-10-27 17:06:33 -070080 def dockerPull( self, onosRepo ="onosproject/onos", onosTag="latest" ):
Hari Krishna6031b7c2015-09-28 17:46:29 -070081 """
82 Pulls Docker image from repository
83 """
84 try:
85 main.log.info( self.name +
suibin zhangfd266fd2015-10-27 17:06:33 -070086 ": Pulling Docker image " + onosRepo + ":"+ onosTag )
87 for line in self.dockerClient.pull( repository = onosRepo, \
88 tag = onosTag, stream = True ):
suibin zhang3b067ea2015-11-05 13:55:21 -080089 print "#",
90 main.log.info(json.dumps(json.loads(line), indent =4))
suibin zhangfd266fd2015-10-27 17:06:33 -070091
92 #response = json.dumps( json.load( pullResult ), indent=4 )
Pratik Parabcc406452017-02-28 15:15:25 -080093 if re.search( "for onosproject/onos:" + onosTag, line ):
94 main.log.info( "onos docker image pulled is: " + line )
Hari Krishna6031b7c2015-09-28 17:46:29 -070095 return main.TRUE
96 else:
suibin zhangfd266fd2015-10-27 17:06:33 -070097 main.log.error( "Failed to download image from: " + onosRepo +":"+ onosTag )
Hari Krishna6031b7c2015-09-28 17:46:29 -070098 main.log.error( "Error respone: " )
suibin zhang3b067ea2015-11-05 13:55:21 -080099 main.log.error( line )
Hari Krishna6031b7c2015-09-28 17:46:29 -0700100 return main.FALSE
101 except Exception:
102 main.log.exception( self.name + ": Uncaught exception!" )
103 main.cleanup()
104 main.exit()
105
suibin zhangfd266fd2015-10-27 17:06:33 -0700106 def dockerCreateCT( self, onosImage="onosproject/onos:latest", onosNode="onos1" ):
Hari Krishna6031b7c2015-09-28 17:46:29 -0700107 """
suibin zhangfd266fd2015-10-27 17:06:33 -0700108 Create a Docker container with a specific image
Hari Krishna6031b7c2015-09-28 17:46:29 -0700109 """
110 try:
111 main.log.info( self.name +
suibin zhangfd266fd2015-10-27 17:06:33 -0700112 ": Creating Docker container for node: " + onosNode )
113 response = self.dockerClient.create_container( image=onosImage, \
114 tty=True, name=onosNode, detach=True )
115 #print response
116 #print response.get("Id")
117 #print response.get("Warnings")
118 if( str( response.get("Warnings") ) == 'None' ):
119 main.log.info( "Created container for node: " + onosNode + "; container id is: " + response.get("Id") )
120 return ( main.TRUE, response.get("Id") )
Hari Krishna6031b7c2015-09-28 17:46:29 -0700121 else:
122 main.log.info( "Noticed warnings during create" )
suibin zhangfd266fd2015-10-27 17:06:33 -0700123 return ( main.FALSE, null)
Hari Krishna6031b7c2015-09-28 17:46:29 -0700124 except Exception:
125 main.log.exception( self.name + ": Uncaught exception!" )
126 main.cleanup()
127 main.exit()
128
suibin zhangfd266fd2015-10-27 17:06:33 -0700129 def dockerStartCT( self, ctID ):
Hari Krishna6031b7c2015-09-28 17:46:29 -0700130 """
131 Start Docker container
132 """
133 try:
134 main.log.info( self.name +
suibin zhangfd266fd2015-10-27 17:06:33 -0700135 ": Starting Docker conatiner Id " + ctID )
136 response = self.dockerClient.start( container = ctID )
137 if response is None:
138 main.log.info( "Started container for Id: " + ctID )
Hari Krishna6031b7c2015-09-28 17:46:29 -0700139 return main.TRUE
140 else:
141 main.log.info( "Noticed warnings during start" )
142 return main.FALSE
143 except Exception:
144 main.log.exception( self.name + ": Uncaught exception!" )
145 main.cleanup()
146 main.exit()
147
suibin zhangfd266fd2015-10-27 17:06:33 -0700148 def dockerStopCT( self, ctName ):
Hari Krishna6031b7c2015-09-28 17:46:29 -0700149 """
150 Stop docker container
151 """
152 try:
153 main.log.info( self.name +
suibin zhangfd266fd2015-10-27 17:06:33 -0700154 ": Stopping Docker conatiner for node " + ctName )
155 response = self.dockerClient.stop( ctName )
156 if response is None:
157 main.log.info( "Stopped container for node: " + ctName )
Hari Krishna6031b7c2015-09-28 17:46:29 -0700158 return main.TRUE
159 else:
160 main.log.info( "Noticed warnings during stop" )
161 return main.FALSE
suibin zhangfd266fd2015-10-27 17:06:33 -0700162 except errors.NotFound:
163 main.log.info( ctName + " not found! Continue on tests...")
164 return main.TRUE
Hari Krishna6031b7c2015-09-28 17:46:29 -0700165 except Exception:
166 main.log.exception( self.name + ": Uncaught exception!" )
suibin zhangfd266fd2015-10-27 17:06:33 -0700167 #main.cleanup()
168 #main.exit()
Hari Krishna6031b7c2015-09-28 17:46:29 -0700169
suibin zhangfd266fd2015-10-27 17:06:33 -0700170 def dockerRestartCT( self, ctName ):
Hari Krishna6031b7c2015-09-28 17:46:29 -0700171 """
172 Restart Docker container
173 """
174 try:
175 main.log.info( self.name +
suibin zhangfd266fd2015-10-27 17:06:33 -0700176 ": Restarting Docker conatiner for node " + ctName )
177 response = self.dockerClient.restart( ctName )
178 if response is None:
179 main.log.info( "Restarted container for node: " + ctName )
Hari Krishna6031b7c2015-09-28 17:46:29 -0700180 return main.TRUE
181 else:
182 main.log.info( "Noticed warnings during Restart" )
183 return main.FALSE
184 except Exception:
185 main.log.exception( self.name + ": Uncaught exception!" )
186 main.cleanup()
187 main.exit()
188
suibin zhangfd266fd2015-10-27 17:06:33 -0700189 def dockerCheckCTName( self, ctName):
Hari Krishna6031b7c2015-09-28 17:46:29 -0700190 """
191 Check Docker conatiner status
192 """
193 try:
194 main.log.info( self.name +
Jon Hall3dbc7dc2015-12-01 12:11:42 -0800195 ": Checking Docker Status for CT with 'Names' " + ctName )
suibin zhangfd266fd2015-10-27 17:06:33 -0700196 namelist = [response["Names"] for response in self.dockerClient.containers(all=True) if not []]
197 main.log.info("Name list is: " + str(namelist) )
198 if( [ctName] in namelist):
199 main.log.info( "Container " + ctName + " exists" )
Hari Krishna6031b7c2015-09-28 17:46:29 -0700200 return main.TRUE
201 else:
suibin zhangfd266fd2015-10-27 17:06:33 -0700202 main.log.info( "Container " + ctName + " does not exist" )
Hari Krishna6031b7c2015-09-28 17:46:29 -0700203 return main.FALSE
suibin zhangfd266fd2015-10-27 17:06:33 -0700204 except errors.NotFound:
205 main.log.warn( ctName + "not found! Continue with the tests...")
206 return main.FALSE
Hari Krishna6031b7c2015-09-28 17:46:29 -0700207 except Exception:
suibin zhangfd266fd2015-10-27 17:06:33 -0700208 main.log.exception( self.name + ": Uncaught exception! Continue tests..." )
209 #main.cleanup()
210 #main.exit()
Hari Krishna6031b7c2015-09-28 17:46:29 -0700211
suibin zhangfd266fd2015-10-27 17:06:33 -0700212 def dockerRemoveCT( self, ctName ):
Hari Krishna6031b7c2015-09-28 17:46:29 -0700213 """
214 Remove Docker conatiner
215 """
216 try:
217 main.log.info( self.name +
suibin zhangfd266fd2015-10-27 17:06:33 -0700218 ": Removing Docker container for node " + ctName )
219 response = self.dockerClient.remove_container( ctName, force=True )
220 if response is None:
221 main.log.info( "Removed container for node: " + ctName )
Hari Krishna6031b7c2015-09-28 17:46:29 -0700222 return main.TRUE
223 else:
suibin zhangfd266fd2015-10-27 17:06:33 -0700224 main.log.info( "Noticed warnings during Remove " + ctName)
Hari Krishna6031b7c2015-09-28 17:46:29 -0700225 return main.FALSE
suibin zhangfd266fd2015-10-27 17:06:33 -0700226 main.log.exception(self.name + ": not found, continuing...")
227 except errors.NotFound:
228 main.log.warn( ctName + "not found! Continue with the tests...")
229 return main.TRUE
Hari Krishna6031b7c2015-09-28 17:46:29 -0700230 except Exception:
suibin zhangfd266fd2015-10-27 17:06:33 -0700231 main.log.exception( self.name + ": Uncaught exception! Continuing..." )
232 #main.cleanup()
233 #main.exit()
Hari Krishna6031b7c2015-09-28 17:46:29 -0700234
Pratik Parabcc406452017-02-28 15:15:25 -0800235 def dockerRemoveImage( self, imageRepoTag=None ):
Hari Krishna6031b7c2015-09-28 17:46:29 -0700236 """
237 Remove Docker image
238 """
suibin zhang3b067ea2015-11-05 13:55:21 -0800239 rmResult = main.TRUE
Jeremy Songsterae01bba2016-07-11 15:39:17 -0700240 if self.dockerClient.images() is []:
Pratik Parabcc406452017-02-28 15:15:25 -0800241 main.log.info( "No docker image found" )
suibin zhang3b067ea2015-11-05 13:55:21 -0800242 return rmResult
suibin zhangfd266fd2015-10-27 17:06:33 -0700243 else:
Pratik Parabcc406452017-02-28 15:15:25 -0800244 imageList = [ image["Id"] for image in self.dockerClient.images()
245 if image["RepoTags"] is None
246 or imageRepoTag in image["RepoTags"] ]
247 for id in imageList:
suibin zhang3b067ea2015-11-05 13:55:21 -0800248 try:
249 main.log.info( self.name + ": Removing Docker image " + id )
250 response = self.dockerClient.remove_image(id, force = True)
251 if response is None:
252 main.log.info( "Removed Docker image: " + id )
253 rmResult = rmResult and main.TRUE
254 else:
255 main.log.info( "Noticed warnings during Remove " + id )
256 rmResult = rmResult and main.FALSE
257 except errors.NotFound:
258 main.log.warn( image + "not found! Continue with the tests...")
259 rmResult = rmResult and main.TRUE
260 except Exception:
261 main.log.exception( self.name + ": Uncaught exception! Continuing..." )
262 rmResult = rmResult and main.FALSE
263 #main.cleanup()
264 #main.exit()
265 return rmResult
Hari Krishna6031b7c2015-09-28 17:46:29 -0700266
267 def fetchLatestClusterFile( self, branch="master" ):
268 """
269 Fetch onos-form-cluster file from a particular branch
270 """
271 try:
272 command = "wget -N https://raw.githubusercontent.com/opennetworkinglab/\
273 onos/" + branch + "/tools/package/bin/onos-form-cluster"
274 subprocess.call( command ) # output checks are missing for now
275 command = "chmod u+x " + "onos-form-cluster"
276 subprocess.call( command )
277 return main.TRUE
278 except Exception:
279 main.log.exception( self.name + ": Uncaught exception!" )
280 main.cleanup()
281 main.exit()
282
Jon Hall321a4572016-05-04 15:28:25 -0700283 def onosFormCluster( self, onosIPs, cmdPath, user="karaf", passwd="karaf" ):
Hari Krishna6031b7c2015-09-28 17:46:29 -0700284 """
285 From ONOS cluster for IP addresses in onosIPs list
286 """
287 try:
288 onosIPs = " ".join(onosIPs)
Jon Hall321a4572016-05-04 15:28:25 -0700289 command = "{}/onos-form-cluster -u {} -p {} {}".format( cmdPath,
290 user,
291 passwd,
suibin zhang1ab833b2016-05-12 12:10:11 -0700292 onosIPs )
suibin zhangfd266fd2015-10-27 17:06:33 -0700293 result = subprocess.call( command, shell=True )
294 if result == 0:
295 return main.TRUE
296 else:
297 main.log.info("Something is not right in forming cluster>")
298 return main.FALSE
Hari Krishna6031b7c2015-09-28 17:46:29 -0700299 except Exception:
300 main.log.exception( self.name + ": Uncaught exception!" )
301 main.cleanup()
302 main.exit()
303
suibin zhangfd266fd2015-10-27 17:06:33 -0700304 def dockerIP( self, ctName ):
Hari Krishna6031b7c2015-09-28 17:46:29 -0700305 """
306 Fetch IP address assigned to specified node/container
307 """
308 try:
suibin zhangfd266fd2015-10-27 17:06:33 -0700309 output = self.dockerClient.inspect_container( ctName )
Hari Krishna6031b7c2015-09-28 17:46:29 -0700310 nodeIP = output['NetworkSettings']['IPAddress']
suibin zhangfd266fd2015-10-27 17:06:33 -0700311 main.log.info( " Docker IP " + str(nodeIP) )
Hari Krishna6031b7c2015-09-28 17:46:29 -0700312 return str(nodeIP)
313
314 except Exception:
315 main.log.exception( self.name + ": Uncaught exception!" )
316 main.cleanup()
317 main.exit()
suibin zhangfd266fd2015-10-27 17:06:33 -0700318