blob: 1d19488d324065556b251a377e5826fcf43aa70c [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
27 super( API, self ).__init__()
28
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 = []
57 for imageDict in imageList:
58 if imageDict[ 'RepoTags' ] is not None:
59 imageListToSend.append( imageDict['RepoTags'][0].encode('UTF8').split(':')[1] )
60 return imageListToSend
61 except Exception as e:
62 main.log.exception( e )
63
suibin zhangfd266fd2015-10-27 17:06:33 -070064 def dockerPull( self, onosRepo ="onosproject/onos", onosTag="latest" ):
Hari Krishna6031b7c2015-09-28 17:46:29 -070065 """
66 Pulls Docker image from repository
67 """
68 try:
69 main.log.info( self.name +
suibin zhangfd266fd2015-10-27 17:06:33 -070070 ": Pulling Docker image " + onosRepo + ":"+ onosTag )
71 for line in self.dockerClient.pull( repository = onosRepo, \
72 tag = onosTag, stream = True ):
suibin zhang3b067ea2015-11-05 13:55:21 -080073 print "#",
74 main.log.info(json.dumps(json.loads(line), indent =4))
suibin zhangfd266fd2015-10-27 17:06:33 -070075
76 #response = json.dumps( json.load( pullResult ), indent=4 )
Pratik Parabcc406452017-02-28 15:15:25 -080077 if re.search( "for onosproject/onos:" + onosTag, line ):
78 main.log.info( "onos docker image pulled is: " + line )
Hari Krishna6031b7c2015-09-28 17:46:29 -070079 return main.TRUE
80 else:
suibin zhangfd266fd2015-10-27 17:06:33 -070081 main.log.error( "Failed to download image from: " + onosRepo +":"+ onosTag )
Hari Krishna6031b7c2015-09-28 17:46:29 -070082 main.log.error( "Error respone: " )
suibin zhang3b067ea2015-11-05 13:55:21 -080083 main.log.error( line )
Hari Krishna6031b7c2015-09-28 17:46:29 -070084 return main.FALSE
85 except Exception:
86 main.log.exception( self.name + ": Uncaught exception!" )
87 main.cleanup()
88 main.exit()
89
suibin zhangfd266fd2015-10-27 17:06:33 -070090 def dockerCreateCT( self, onosImage="onosproject/onos:latest", onosNode="onos1" ):
Hari Krishna6031b7c2015-09-28 17:46:29 -070091 """
suibin zhangfd266fd2015-10-27 17:06:33 -070092 Create a Docker container with a specific image
Hari Krishna6031b7c2015-09-28 17:46:29 -070093 """
94 try:
95 main.log.info( self.name +
suibin zhangfd266fd2015-10-27 17:06:33 -070096 ": Creating Docker container for node: " + onosNode )
97 response = self.dockerClient.create_container( image=onosImage, \
98 tty=True, name=onosNode, detach=True )
99 #print response
100 #print response.get("Id")
101 #print response.get("Warnings")
102 if( str( response.get("Warnings") ) == 'None' ):
103 main.log.info( "Created container for node: " + onosNode + "; container id is: " + response.get("Id") )
104 return ( main.TRUE, response.get("Id") )
Hari Krishna6031b7c2015-09-28 17:46:29 -0700105 else:
106 main.log.info( "Noticed warnings during create" )
suibin zhangfd266fd2015-10-27 17:06:33 -0700107 return ( main.FALSE, null)
Hari Krishna6031b7c2015-09-28 17:46:29 -0700108 except Exception:
109 main.log.exception( self.name + ": Uncaught exception!" )
110 main.cleanup()
111 main.exit()
112
suibin zhangfd266fd2015-10-27 17:06:33 -0700113 def dockerStartCT( self, ctID ):
Hari Krishna6031b7c2015-09-28 17:46:29 -0700114 """
115 Start Docker container
116 """
117 try:
118 main.log.info( self.name +
suibin zhangfd266fd2015-10-27 17:06:33 -0700119 ": Starting Docker conatiner Id " + ctID )
120 response = self.dockerClient.start( container = ctID )
121 if response is None:
122 main.log.info( "Started container for Id: " + ctID )
Hari Krishna6031b7c2015-09-28 17:46:29 -0700123 return main.TRUE
124 else:
125 main.log.info( "Noticed warnings during start" )
126 return main.FALSE
127 except Exception:
128 main.log.exception( self.name + ": Uncaught exception!" )
129 main.cleanup()
130 main.exit()
131
suibin zhangfd266fd2015-10-27 17:06:33 -0700132 def dockerStopCT( self, ctName ):
Hari Krishna6031b7c2015-09-28 17:46:29 -0700133 """
134 Stop docker container
135 """
136 try:
137 main.log.info( self.name +
suibin zhangfd266fd2015-10-27 17:06:33 -0700138 ": Stopping Docker conatiner for node " + ctName )
139 response = self.dockerClient.stop( ctName )
140 if response is None:
141 main.log.info( "Stopped container for node: " + ctName )
Hari Krishna6031b7c2015-09-28 17:46:29 -0700142 return main.TRUE
143 else:
144 main.log.info( "Noticed warnings during stop" )
145 return main.FALSE
suibin zhangfd266fd2015-10-27 17:06:33 -0700146 except errors.NotFound:
147 main.log.info( ctName + " not found! Continue on tests...")
148 return main.TRUE
Hari Krishna6031b7c2015-09-28 17:46:29 -0700149 except Exception:
150 main.log.exception( self.name + ": Uncaught exception!" )
suibin zhangfd266fd2015-10-27 17:06:33 -0700151 #main.cleanup()
152 #main.exit()
Hari Krishna6031b7c2015-09-28 17:46:29 -0700153
suibin zhangfd266fd2015-10-27 17:06:33 -0700154 def dockerRestartCT( self, ctName ):
Hari Krishna6031b7c2015-09-28 17:46:29 -0700155 """
156 Restart Docker container
157 """
158 try:
159 main.log.info( self.name +
suibin zhangfd266fd2015-10-27 17:06:33 -0700160 ": Restarting Docker conatiner for node " + ctName )
161 response = self.dockerClient.restart( ctName )
162 if response is None:
163 main.log.info( "Restarted container for node: " + ctName )
Hari Krishna6031b7c2015-09-28 17:46:29 -0700164 return main.TRUE
165 else:
166 main.log.info( "Noticed warnings during Restart" )
167 return main.FALSE
168 except Exception:
169 main.log.exception( self.name + ": Uncaught exception!" )
170 main.cleanup()
171 main.exit()
172
suibin zhangfd266fd2015-10-27 17:06:33 -0700173 def dockerCheckCTName( self, ctName):
Hari Krishna6031b7c2015-09-28 17:46:29 -0700174 """
175 Check Docker conatiner status
176 """
177 try:
178 main.log.info( self.name +
Jon Hall3dbc7dc2015-12-01 12:11:42 -0800179 ": Checking Docker Status for CT with 'Names' " + ctName )
suibin zhangfd266fd2015-10-27 17:06:33 -0700180 namelist = [response["Names"] for response in self.dockerClient.containers(all=True) if not []]
181 main.log.info("Name list is: " + str(namelist) )
182 if( [ctName] in namelist):
183 main.log.info( "Container " + ctName + " exists" )
Hari Krishna6031b7c2015-09-28 17:46:29 -0700184 return main.TRUE
185 else:
suibin zhangfd266fd2015-10-27 17:06:33 -0700186 main.log.info( "Container " + ctName + " does not exist" )
Hari Krishna6031b7c2015-09-28 17:46:29 -0700187 return main.FALSE
suibin zhangfd266fd2015-10-27 17:06:33 -0700188 except errors.NotFound:
189 main.log.warn( ctName + "not found! Continue with the tests...")
190 return main.FALSE
Hari Krishna6031b7c2015-09-28 17:46:29 -0700191 except Exception:
suibin zhangfd266fd2015-10-27 17:06:33 -0700192 main.log.exception( self.name + ": Uncaught exception! Continue tests..." )
193 #main.cleanup()
194 #main.exit()
Hari Krishna6031b7c2015-09-28 17:46:29 -0700195
suibin zhangfd266fd2015-10-27 17:06:33 -0700196 def dockerRemoveCT( self, ctName ):
Hari Krishna6031b7c2015-09-28 17:46:29 -0700197 """
198 Remove Docker conatiner
199 """
200 try:
201 main.log.info( self.name +
suibin zhangfd266fd2015-10-27 17:06:33 -0700202 ": Removing Docker container for node " + ctName )
203 response = self.dockerClient.remove_container( ctName, force=True )
204 if response is None:
205 main.log.info( "Removed container for node: " + ctName )
Hari Krishna6031b7c2015-09-28 17:46:29 -0700206 return main.TRUE
207 else:
suibin zhangfd266fd2015-10-27 17:06:33 -0700208 main.log.info( "Noticed warnings during Remove " + ctName)
Hari Krishna6031b7c2015-09-28 17:46:29 -0700209 return main.FALSE
suibin zhangfd266fd2015-10-27 17:06:33 -0700210 main.log.exception(self.name + ": not found, continuing...")
211 except errors.NotFound:
212 main.log.warn( ctName + "not found! Continue with the tests...")
213 return main.TRUE
Hari Krishna6031b7c2015-09-28 17:46:29 -0700214 except Exception:
suibin zhangfd266fd2015-10-27 17:06:33 -0700215 main.log.exception( self.name + ": Uncaught exception! Continuing..." )
216 #main.cleanup()
217 #main.exit()
Hari Krishna6031b7c2015-09-28 17:46:29 -0700218
Pratik Parabcc406452017-02-28 15:15:25 -0800219 def dockerRemoveImage( self, imageRepoTag=None ):
Hari Krishna6031b7c2015-09-28 17:46:29 -0700220 """
221 Remove Docker image
222 """
suibin zhang3b067ea2015-11-05 13:55:21 -0800223 rmResult = main.TRUE
Jeremy Songsterae01bba2016-07-11 15:39:17 -0700224 if self.dockerClient.images() is []:
Pratik Parabcc406452017-02-28 15:15:25 -0800225 main.log.info( "No docker image found" )
suibin zhang3b067ea2015-11-05 13:55:21 -0800226 return rmResult
suibin zhangfd266fd2015-10-27 17:06:33 -0700227 else:
Pratik Parabcc406452017-02-28 15:15:25 -0800228 imageList = [ image["Id"] for image in self.dockerClient.images()
229 if image["RepoTags"] is None
230 or imageRepoTag in image["RepoTags"] ]
231 for id in imageList:
suibin zhang3b067ea2015-11-05 13:55:21 -0800232 try:
233 main.log.info( self.name + ": Removing Docker image " + id )
234 response = self.dockerClient.remove_image(id, force = True)
235 if response is None:
236 main.log.info( "Removed Docker image: " + id )
237 rmResult = rmResult and main.TRUE
238 else:
239 main.log.info( "Noticed warnings during Remove " + id )
240 rmResult = rmResult and main.FALSE
241 except errors.NotFound:
242 main.log.warn( image + "not found! Continue with the tests...")
243 rmResult = rmResult and main.TRUE
244 except Exception:
245 main.log.exception( self.name + ": Uncaught exception! Continuing..." )
246 rmResult = rmResult and main.FALSE
247 #main.cleanup()
248 #main.exit()
249 return rmResult
Hari Krishna6031b7c2015-09-28 17:46:29 -0700250
251 def fetchLatestClusterFile( self, branch="master" ):
252 """
253 Fetch onos-form-cluster file from a particular branch
254 """
255 try:
256 command = "wget -N https://raw.githubusercontent.com/opennetworkinglab/\
257 onos/" + branch + "/tools/package/bin/onos-form-cluster"
258 subprocess.call( command ) # output checks are missing for now
259 command = "chmod u+x " + "onos-form-cluster"
260 subprocess.call( command )
261 return main.TRUE
262 except Exception:
263 main.log.exception( self.name + ": Uncaught exception!" )
264 main.cleanup()
265 main.exit()
266
Jon Hall321a4572016-05-04 15:28:25 -0700267 def onosFormCluster( self, onosIPs, cmdPath, user="karaf", passwd="karaf" ):
Hari Krishna6031b7c2015-09-28 17:46:29 -0700268 """
269 From ONOS cluster for IP addresses in onosIPs list
270 """
271 try:
272 onosIPs = " ".join(onosIPs)
Jon Hall321a4572016-05-04 15:28:25 -0700273 command = "{}/onos-form-cluster -u {} -p {} {}".format( cmdPath,
274 user,
275 passwd,
suibin zhang1ab833b2016-05-12 12:10:11 -0700276 onosIPs )
suibin zhangfd266fd2015-10-27 17:06:33 -0700277 result = subprocess.call( command, shell=True )
278 if result == 0:
279 return main.TRUE
280 else:
281 main.log.info("Something is not right in forming cluster>")
282 return main.FALSE
Hari Krishna6031b7c2015-09-28 17:46:29 -0700283 except Exception:
284 main.log.exception( self.name + ": Uncaught exception!" )
285 main.cleanup()
286 main.exit()
287
suibin zhangfd266fd2015-10-27 17:06:33 -0700288 def dockerIP( self, ctName ):
Hari Krishna6031b7c2015-09-28 17:46:29 -0700289 """
290 Fetch IP address assigned to specified node/container
291 """
292 try:
suibin zhangfd266fd2015-10-27 17:06:33 -0700293 output = self.dockerClient.inspect_container( ctName )
Hari Krishna6031b7c2015-09-28 17:46:29 -0700294 nodeIP = output['NetworkSettings']['IPAddress']
suibin zhangfd266fd2015-10-27 17:06:33 -0700295 main.log.info( " Docker IP " + str(nodeIP) )
Hari Krishna6031b7c2015-09-28 17:46:29 -0700296 return str(nodeIP)
297
298 except Exception:
299 main.log.exception( self.name + ": Uncaught exception!" )
300 main.cleanup()
301 main.exit()
suibin zhangfd266fd2015-10-27 17:06:33 -0700302