blob: 6d9650c132a1b1352bf2afd0a4244cd39f921746 [file] [log] [blame]
Hari Krishna6031b7c2015-09-28 17:46:29 -07001#!/usr/bin/env python
Jeremy Songsterae01bba2016-07-11 15:39:17 -07002"""
3Modified 2016 by ON.Lab
4
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>
8"""
Hari Krishna6031b7c2015-09-28 17:46:29 -07009
10import json
11import os
12import re
13import subprocess
14from docker import Client
suibin zhangfd266fd2015-10-27 17:06:33 -070015from docker import errors
Hari Krishna6031b7c2015-09-28 17:46:29 -070016from drivers.common.apidriver import API
17
18class DockerApiDriver( API ):
19
20 def __init__( self ):
21 """
22 Initialize client
23 """
24 self.name = None
25 self.home = None
26 self.handle = None
Devin Limdc78e202017-06-09 18:30:07 -070027 super( DockerApiDriver, self ).__init__()
Hari Krishna6031b7c2015-09-28 17:46:29 -070028
29 def connect( self, **connectargs ):
30 """
31 Create Client handle to connnect to Docker server
32 """
33 try:
34 for key in connectargs:
35 vars( self )[ key ] = connectargs[ key ]
36 self.name = self.options[ 'name' ]
37 for key in self.options:
38 if key == "home":
39 self.home = self.options[ 'home' ]
40 break
41 if self.home is None or self.home == "":
42 self.home = "/var/tmp"
43
44 self.handle = super( DockerApiDriver, self ).connect()
45 self.dockerClient = Client(base_url='unix://var/run/docker.sock')
46 return self.handle
47 except Exception as e:
48 main.log.exception( e )
49
Pratik Parabcc406452017-02-28 15:15:25 -080050 def getListOfImages( self, repo="onosproject/onos" ):
51 """
52 Get the list of image tags
53 """
54 try:
55 imageList = list( self.dockerClient.images( name=repo ) )
56 imageListToSend = []
Pratik Parab50d53d42017-03-24 14:28:22 -070057 duplicateTagDetected = 0
Pratik Parabcc406452017-02-28 15:15:25 -080058 for imageDict in imageList:
59 if imageDict[ 'RepoTags' ] is not None:
Pratik Parab50d53d42017-03-24 14:28:22 -070060 if len( imageDict[ 'RepoTags' ] ) > 1:
61 duplicateTagDetected = 1
Pratik Parabcc406452017-02-28 15:15:25 -080062 imageListToSend.append( imageDict['RepoTags'][0].encode('UTF8').split(':')[1] )
Pratik Parab50d53d42017-03-24 14:28:22 -070063 return imageListToSend, duplicateTagDetected
Pratik Parabcc406452017-02-28 15:15:25 -080064 except Exception as e:
65 main.log.exception( e )
66
suibin zhangfd266fd2015-10-27 17:06:33 -070067 def dockerPull( self, onosRepo ="onosproject/onos", onosTag="latest" ):
Hari Krishna6031b7c2015-09-28 17:46:29 -070068 """
69 Pulls Docker image from repository
70 """
71 try:
72 main.log.info( self.name +
suibin zhangfd266fd2015-10-27 17:06:33 -070073 ": Pulling Docker image " + onosRepo + ":"+ onosTag )
74 for line in self.dockerClient.pull( repository = onosRepo, \
75 tag = onosTag, stream = True ):
suibin zhang3b067ea2015-11-05 13:55:21 -080076 print "#",
77 main.log.info(json.dumps(json.loads(line), indent =4))
suibin zhangfd266fd2015-10-27 17:06:33 -070078
79 #response = json.dumps( json.load( pullResult ), indent=4 )
Pratik Parabcc406452017-02-28 15:15:25 -080080 if re.search( "for onosproject/onos:" + onosTag, line ):
81 main.log.info( "onos docker image pulled is: " + line )
Hari Krishna6031b7c2015-09-28 17:46:29 -070082 return main.TRUE
83 else:
suibin zhangfd266fd2015-10-27 17:06:33 -070084 main.log.error( "Failed to download image from: " + onosRepo +":"+ onosTag )
Hari Krishna6031b7c2015-09-28 17:46:29 -070085 main.log.error( "Error respone: " )
suibin zhang3b067ea2015-11-05 13:55:21 -080086 main.log.error( line )
Hari Krishna6031b7c2015-09-28 17:46:29 -070087 return main.FALSE
88 except Exception:
89 main.log.exception( self.name + ": Uncaught exception!" )
90 main.cleanup()
91 main.exit()
92
suibin zhangfd266fd2015-10-27 17:06:33 -070093 def dockerCreateCT( self, onosImage="onosproject/onos:latest", onosNode="onos1" ):
Hari Krishna6031b7c2015-09-28 17:46:29 -070094 """
suibin zhangfd266fd2015-10-27 17:06:33 -070095 Create a Docker container with a specific image
Hari Krishna6031b7c2015-09-28 17:46:29 -070096 """
97 try:
98 main.log.info( self.name +
suibin zhangfd266fd2015-10-27 17:06:33 -070099 ": Creating Docker container for node: " + onosNode )
100 response = self.dockerClient.create_container( image=onosImage, \
101 tty=True, name=onosNode, detach=True )
102 #print response
103 #print response.get("Id")
104 #print response.get("Warnings")
105 if( str( response.get("Warnings") ) == 'None' ):
106 main.log.info( "Created container for node: " + onosNode + "; container id is: " + response.get("Id") )
107 return ( main.TRUE, response.get("Id") )
Hari Krishna6031b7c2015-09-28 17:46:29 -0700108 else:
109 main.log.info( "Noticed warnings during create" )
suibin zhangfd266fd2015-10-27 17:06:33 -0700110 return ( main.FALSE, null)
Hari Krishna6031b7c2015-09-28 17:46:29 -0700111 except Exception:
112 main.log.exception( self.name + ": Uncaught exception!" )
113 main.cleanup()
114 main.exit()
115
suibin zhangfd266fd2015-10-27 17:06:33 -0700116 def dockerStartCT( self, ctID ):
Hari Krishna6031b7c2015-09-28 17:46:29 -0700117 """
118 Start Docker container
119 """
120 try:
121 main.log.info( self.name +
suibin zhangfd266fd2015-10-27 17:06:33 -0700122 ": Starting Docker conatiner Id " + ctID )
123 response = self.dockerClient.start( container = ctID )
124 if response is None:
125 main.log.info( "Started container for Id: " + ctID )
Hari Krishna6031b7c2015-09-28 17:46:29 -0700126 return main.TRUE
127 else:
128 main.log.info( "Noticed warnings during start" )
129 return main.FALSE
130 except Exception:
131 main.log.exception( self.name + ": Uncaught exception!" )
132 main.cleanup()
133 main.exit()
134
suibin zhangfd266fd2015-10-27 17:06:33 -0700135 def dockerStopCT( self, ctName ):
Hari Krishna6031b7c2015-09-28 17:46:29 -0700136 """
137 Stop docker container
138 """
139 try:
140 main.log.info( self.name +
suibin zhangfd266fd2015-10-27 17:06:33 -0700141 ": Stopping Docker conatiner for node " + ctName )
142 response = self.dockerClient.stop( ctName )
143 if response is None:
144 main.log.info( "Stopped container for node: " + ctName )
Hari Krishna6031b7c2015-09-28 17:46:29 -0700145 return main.TRUE
146 else:
147 main.log.info( "Noticed warnings during stop" )
148 return main.FALSE
suibin zhangfd266fd2015-10-27 17:06:33 -0700149 except errors.NotFound:
150 main.log.info( ctName + " not found! Continue on tests...")
151 return main.TRUE
Hari Krishna6031b7c2015-09-28 17:46:29 -0700152 except Exception:
153 main.log.exception( self.name + ": Uncaught exception!" )
suibin zhangfd266fd2015-10-27 17:06:33 -0700154 #main.cleanup()
155 #main.exit()
Hari Krishna6031b7c2015-09-28 17:46:29 -0700156
suibin zhangfd266fd2015-10-27 17:06:33 -0700157 def dockerRestartCT( self, ctName ):
Hari Krishna6031b7c2015-09-28 17:46:29 -0700158 """
159 Restart Docker container
160 """
161 try:
162 main.log.info( self.name +
suibin zhangfd266fd2015-10-27 17:06:33 -0700163 ": Restarting Docker conatiner for node " + ctName )
164 response = self.dockerClient.restart( ctName )
165 if response is None:
166 main.log.info( "Restarted container for node: " + ctName )
Hari Krishna6031b7c2015-09-28 17:46:29 -0700167 return main.TRUE
168 else:
169 main.log.info( "Noticed warnings during Restart" )
170 return main.FALSE
171 except Exception:
172 main.log.exception( self.name + ": Uncaught exception!" )
173 main.cleanup()
174 main.exit()
175
suibin zhangfd266fd2015-10-27 17:06:33 -0700176 def dockerCheckCTName( self, ctName):
Hari Krishna6031b7c2015-09-28 17:46:29 -0700177 """
178 Check Docker conatiner status
179 """
180 try:
181 main.log.info( self.name +
Jon Hall3dbc7dc2015-12-01 12:11:42 -0800182 ": Checking Docker Status for CT with 'Names' " + ctName )
suibin zhangfd266fd2015-10-27 17:06:33 -0700183 namelist = [response["Names"] for response in self.dockerClient.containers(all=True) if not []]
184 main.log.info("Name list is: " + str(namelist) )
185 if( [ctName] in namelist):
186 main.log.info( "Container " + ctName + " exists" )
Hari Krishna6031b7c2015-09-28 17:46:29 -0700187 return main.TRUE
188 else:
suibin zhangfd266fd2015-10-27 17:06:33 -0700189 main.log.info( "Container " + ctName + " does not exist" )
Hari Krishna6031b7c2015-09-28 17:46:29 -0700190 return main.FALSE
suibin zhangfd266fd2015-10-27 17:06:33 -0700191 except errors.NotFound:
192 main.log.warn( ctName + "not found! Continue with the tests...")
193 return main.FALSE
Hari Krishna6031b7c2015-09-28 17:46:29 -0700194 except Exception:
suibin zhangfd266fd2015-10-27 17:06:33 -0700195 main.log.exception( self.name + ": Uncaught exception! Continue tests..." )
196 #main.cleanup()
197 #main.exit()
Hari Krishna6031b7c2015-09-28 17:46:29 -0700198
suibin zhangfd266fd2015-10-27 17:06:33 -0700199 def dockerRemoveCT( self, ctName ):
Hari Krishna6031b7c2015-09-28 17:46:29 -0700200 """
201 Remove Docker conatiner
202 """
203 try:
204 main.log.info( self.name +
suibin zhangfd266fd2015-10-27 17:06:33 -0700205 ": Removing Docker container for node " + ctName )
206 response = self.dockerClient.remove_container( ctName, force=True )
207 if response is None:
208 main.log.info( "Removed container for node: " + ctName )
Hari Krishna6031b7c2015-09-28 17:46:29 -0700209 return main.TRUE
210 else:
suibin zhangfd266fd2015-10-27 17:06:33 -0700211 main.log.info( "Noticed warnings during Remove " + ctName)
Hari Krishna6031b7c2015-09-28 17:46:29 -0700212 return main.FALSE
suibin zhangfd266fd2015-10-27 17:06:33 -0700213 main.log.exception(self.name + ": not found, continuing...")
214 except errors.NotFound:
215 main.log.warn( ctName + "not found! Continue with the tests...")
216 return main.TRUE
Hari Krishna6031b7c2015-09-28 17:46:29 -0700217 except Exception:
suibin zhangfd266fd2015-10-27 17:06:33 -0700218 main.log.exception( self.name + ": Uncaught exception! Continuing..." )
219 #main.cleanup()
220 #main.exit()
Hari Krishna6031b7c2015-09-28 17:46:29 -0700221
Pratik Parabcc406452017-02-28 15:15:25 -0800222 def dockerRemoveImage( self, imageRepoTag=None ):
Hari Krishna6031b7c2015-09-28 17:46:29 -0700223 """
224 Remove Docker image
225 """
suibin zhang3b067ea2015-11-05 13:55:21 -0800226 rmResult = main.TRUE
Jeremy Songsterae01bba2016-07-11 15:39:17 -0700227 if self.dockerClient.images() is []:
Pratik Parabcc406452017-02-28 15:15:25 -0800228 main.log.info( "No docker image found" )
suibin zhang3b067ea2015-11-05 13:55:21 -0800229 return rmResult
suibin zhangfd266fd2015-10-27 17:06:33 -0700230 else:
Pratik Parabcc406452017-02-28 15:15:25 -0800231 imageList = [ image["Id"] for image in self.dockerClient.images()
232 if image["RepoTags"] is None
233 or imageRepoTag in image["RepoTags"] ]
234 for id in imageList:
suibin zhang3b067ea2015-11-05 13:55:21 -0800235 try:
236 main.log.info( self.name + ": Removing Docker image " + id )
237 response = self.dockerClient.remove_image(id, force = True)
238 if response is None:
239 main.log.info( "Removed Docker image: " + id )
240 rmResult = rmResult and main.TRUE
241 else:
242 main.log.info( "Noticed warnings during Remove " + id )
243 rmResult = rmResult and main.FALSE
244 except errors.NotFound:
245 main.log.warn( image + "not found! Continue with the tests...")
246 rmResult = rmResult and main.TRUE
247 except Exception:
248 main.log.exception( self.name + ": Uncaught exception! Continuing..." )
249 rmResult = rmResult and main.FALSE
250 #main.cleanup()
251 #main.exit()
252 return rmResult
Hari Krishna6031b7c2015-09-28 17:46:29 -0700253
254 def fetchLatestClusterFile( self, branch="master" ):
255 """
256 Fetch onos-form-cluster file from a particular branch
257 """
258 try:
259 command = "wget -N https://raw.githubusercontent.com/opennetworkinglab/\
260 onos/" + branch + "/tools/package/bin/onos-form-cluster"
261 subprocess.call( command ) # output checks are missing for now
262 command = "chmod u+x " + "onos-form-cluster"
263 subprocess.call( command )
264 return main.TRUE
265 except Exception:
266 main.log.exception( self.name + ": Uncaught exception!" )
267 main.cleanup()
268 main.exit()
269
Jon Hall321a4572016-05-04 15:28:25 -0700270 def onosFormCluster( self, onosIPs, cmdPath, user="karaf", passwd="karaf" ):
Hari Krishna6031b7c2015-09-28 17:46:29 -0700271 """
272 From ONOS cluster for IP addresses in onosIPs list
273 """
274 try:
275 onosIPs = " ".join(onosIPs)
Jon Hall321a4572016-05-04 15:28:25 -0700276 command = "{}/onos-form-cluster -u {} -p {} {}".format( cmdPath,
277 user,
278 passwd,
suibin zhang1ab833b2016-05-12 12:10:11 -0700279 onosIPs )
suibin zhangfd266fd2015-10-27 17:06:33 -0700280 result = subprocess.call( command, shell=True )
281 if result == 0:
282 return main.TRUE
283 else:
284 main.log.info("Something is not right in forming cluster>")
285 return main.FALSE
Hari Krishna6031b7c2015-09-28 17:46:29 -0700286 except Exception:
287 main.log.exception( self.name + ": Uncaught exception!" )
288 main.cleanup()
289 main.exit()
290
suibin zhangfd266fd2015-10-27 17:06:33 -0700291 def dockerIP( self, ctName ):
Hari Krishna6031b7c2015-09-28 17:46:29 -0700292 """
293 Fetch IP address assigned to specified node/container
294 """
295 try:
suibin zhangfd266fd2015-10-27 17:06:33 -0700296 output = self.dockerClient.inspect_container( ctName )
Hari Krishna6031b7c2015-09-28 17:46:29 -0700297 nodeIP = output['NetworkSettings']['IPAddress']
suibin zhangfd266fd2015-10-27 17:06:33 -0700298 main.log.info( " Docker IP " + str(nodeIP) )
Hari Krishna6031b7c2015-09-28 17:46:29 -0700299 return str(nodeIP)
300
301 except Exception:
302 main.log.exception( self.name + ": Uncaught exception!" )
303 main.cleanup()
304 main.exit()
suibin zhangfd266fd2015-10-27 17:06:33 -0700305