blob: 3fac61022d5115a476165075ab986cbf69c3f7e1 [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!" )
Devin Lim44075962017-08-11 10:56:37 -0700103 main.cleanAndExit()
Hari Krishna6031b7c2015-09-28 17:46:29 -0700104
suibin zhangfd266fd2015-10-27 17:06:33 -0700105 def dockerCreateCT( self, onosImage="onosproject/onos:latest", onosNode="onos1" ):
Hari Krishna6031b7c2015-09-28 17:46:29 -0700106 """
suibin zhangfd266fd2015-10-27 17:06:33 -0700107 Create a Docker container with a specific image
Hari Krishna6031b7c2015-09-28 17:46:29 -0700108 """
109 try:
110 main.log.info( self.name +
suibin zhangfd266fd2015-10-27 17:06:33 -0700111 ": Creating Docker container for node: " + onosNode )
112 response = self.dockerClient.create_container( image=onosImage, \
113 tty=True, name=onosNode, detach=True )
114 #print response
115 #print response.get("Id")
116 #print response.get("Warnings")
117 if( str( response.get("Warnings") ) == 'None' ):
118 main.log.info( "Created container for node: " + onosNode + "; container id is: " + response.get("Id") )
119 return ( main.TRUE, response.get("Id") )
Hari Krishna6031b7c2015-09-28 17:46:29 -0700120 else:
121 main.log.info( "Noticed warnings during create" )
suibin zhangfd266fd2015-10-27 17:06:33 -0700122 return ( main.FALSE, null)
Hari Krishna6031b7c2015-09-28 17:46:29 -0700123 except Exception:
124 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700125 main.cleanAndExit()
Hari Krishna6031b7c2015-09-28 17:46:29 -0700126
suibin zhangfd266fd2015-10-27 17:06:33 -0700127 def dockerStartCT( self, ctID ):
Hari Krishna6031b7c2015-09-28 17:46:29 -0700128 """
129 Start Docker container
130 """
131 try:
132 main.log.info( self.name +
suibin zhangfd266fd2015-10-27 17:06:33 -0700133 ": Starting Docker conatiner Id " + ctID )
134 response = self.dockerClient.start( container = ctID )
135 if response is None:
136 main.log.info( "Started container for Id: " + ctID )
Hari Krishna6031b7c2015-09-28 17:46:29 -0700137 return main.TRUE
138 else:
139 main.log.info( "Noticed warnings during start" )
140 return main.FALSE
141 except Exception:
142 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700143 main.cleanAndExit()
Hari Krishna6031b7c2015-09-28 17:46:29 -0700144
suibin zhangfd266fd2015-10-27 17:06:33 -0700145 def dockerStopCT( self, ctName ):
Hari Krishna6031b7c2015-09-28 17:46:29 -0700146 """
147 Stop docker container
148 """
149 try:
150 main.log.info( self.name +
suibin zhangfd266fd2015-10-27 17:06:33 -0700151 ": Stopping Docker conatiner for node " + ctName )
152 response = self.dockerClient.stop( ctName )
153 if response is None:
154 main.log.info( "Stopped container for node: " + ctName )
Hari Krishna6031b7c2015-09-28 17:46:29 -0700155 return main.TRUE
156 else:
157 main.log.info( "Noticed warnings during stop" )
158 return main.FALSE
suibin zhangfd266fd2015-10-27 17:06:33 -0700159 except errors.NotFound:
160 main.log.info( ctName + " not found! Continue on tests...")
161 return main.TRUE
Hari Krishna6031b7c2015-09-28 17:46:29 -0700162 except Exception:
163 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700164 #main.cleanAndExit()
Hari Krishna6031b7c2015-09-28 17:46:29 -0700165
suibin zhangfd266fd2015-10-27 17:06:33 -0700166 def dockerRestartCT( self, ctName ):
Hari Krishna6031b7c2015-09-28 17:46:29 -0700167 """
168 Restart Docker container
169 """
170 try:
171 main.log.info( self.name +
suibin zhangfd266fd2015-10-27 17:06:33 -0700172 ": Restarting Docker conatiner for node " + ctName )
173 response = self.dockerClient.restart( ctName )
174 if response is None:
175 main.log.info( "Restarted container for node: " + ctName )
Hari Krishna6031b7c2015-09-28 17:46:29 -0700176 return main.TRUE
177 else:
178 main.log.info( "Noticed warnings during Restart" )
179 return main.FALSE
180 except Exception:
181 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700182 main.cleanAndExit()
Hari Krishna6031b7c2015-09-28 17:46:29 -0700183
suibin zhangfd266fd2015-10-27 17:06:33 -0700184 def dockerCheckCTName( self, ctName):
Hari Krishna6031b7c2015-09-28 17:46:29 -0700185 """
186 Check Docker conatiner status
187 """
188 try:
189 main.log.info( self.name +
Jon Hall3dbc7dc2015-12-01 12:11:42 -0800190 ": Checking Docker Status for CT with 'Names' " + ctName )
suibin zhangfd266fd2015-10-27 17:06:33 -0700191 namelist = [response["Names"] for response in self.dockerClient.containers(all=True) if not []]
192 main.log.info("Name list is: " + str(namelist) )
193 if( [ctName] in namelist):
194 main.log.info( "Container " + ctName + " exists" )
Hari Krishna6031b7c2015-09-28 17:46:29 -0700195 return main.TRUE
196 else:
suibin zhangfd266fd2015-10-27 17:06:33 -0700197 main.log.info( "Container " + ctName + " does not exist" )
Hari Krishna6031b7c2015-09-28 17:46:29 -0700198 return main.FALSE
suibin zhangfd266fd2015-10-27 17:06:33 -0700199 except errors.NotFound:
200 main.log.warn( ctName + "not found! Continue with the tests...")
201 return main.FALSE
Hari Krishna6031b7c2015-09-28 17:46:29 -0700202 except Exception:
suibin zhangfd266fd2015-10-27 17:06:33 -0700203 main.log.exception( self.name + ": Uncaught exception! Continue tests..." )
Devin Lim44075962017-08-11 10:56:37 -0700204 #main.cleanAndExit()
Hari Krishna6031b7c2015-09-28 17:46:29 -0700205
suibin zhangfd266fd2015-10-27 17:06:33 -0700206 def dockerRemoveCT( self, ctName ):
Hari Krishna6031b7c2015-09-28 17:46:29 -0700207 """
208 Remove Docker conatiner
209 """
210 try:
211 main.log.info( self.name +
suibin zhangfd266fd2015-10-27 17:06:33 -0700212 ": Removing Docker container for node " + ctName )
213 response = self.dockerClient.remove_container( ctName, force=True )
214 if response is None:
215 main.log.info( "Removed container for node: " + ctName )
Hari Krishna6031b7c2015-09-28 17:46:29 -0700216 return main.TRUE
217 else:
suibin zhangfd266fd2015-10-27 17:06:33 -0700218 main.log.info( "Noticed warnings during Remove " + ctName)
Hari Krishna6031b7c2015-09-28 17:46:29 -0700219 return main.FALSE
suibin zhangfd266fd2015-10-27 17:06:33 -0700220 main.log.exception(self.name + ": not found, continuing...")
221 except errors.NotFound:
222 main.log.warn( ctName + "not found! Continue with the tests...")
223 return main.TRUE
Hari Krishna6031b7c2015-09-28 17:46:29 -0700224 except Exception:
suibin zhangfd266fd2015-10-27 17:06:33 -0700225 main.log.exception( self.name + ": Uncaught exception! Continuing..." )
Devin Lim44075962017-08-11 10:56:37 -0700226 #main.cleanAndExit()
Hari Krishna6031b7c2015-09-28 17:46:29 -0700227
Pratik Parabcc406452017-02-28 15:15:25 -0800228 def dockerRemoveImage( self, imageRepoTag=None ):
Hari Krishna6031b7c2015-09-28 17:46:29 -0700229 """
230 Remove Docker image
231 """
suibin zhang3b067ea2015-11-05 13:55:21 -0800232 rmResult = main.TRUE
Jeremy Songsterae01bba2016-07-11 15:39:17 -0700233 if self.dockerClient.images() is []:
Pratik Parabcc406452017-02-28 15:15:25 -0800234 main.log.info( "No docker image found" )
suibin zhang3b067ea2015-11-05 13:55:21 -0800235 return rmResult
suibin zhangfd266fd2015-10-27 17:06:33 -0700236 else:
Pratik Parabcc406452017-02-28 15:15:25 -0800237 imageList = [ image["Id"] for image in self.dockerClient.images()
238 if image["RepoTags"] is None
239 or imageRepoTag in image["RepoTags"] ]
240 for id in imageList:
suibin zhang3b067ea2015-11-05 13:55:21 -0800241 try:
242 main.log.info( self.name + ": Removing Docker image " + id )
243 response = self.dockerClient.remove_image(id, force = True)
244 if response is None:
245 main.log.info( "Removed Docker image: " + id )
246 rmResult = rmResult and main.TRUE
247 else:
248 main.log.info( "Noticed warnings during Remove " + id )
249 rmResult = rmResult and main.FALSE
250 except errors.NotFound:
251 main.log.warn( image + "not found! Continue with the tests...")
252 rmResult = rmResult and main.TRUE
253 except Exception:
254 main.log.exception( self.name + ": Uncaught exception! Continuing..." )
255 rmResult = rmResult and main.FALSE
Devin Lim44075962017-08-11 10:56:37 -0700256 #main.cleanAndExit()
suibin zhang3b067ea2015-11-05 13:55:21 -0800257 return rmResult
Hari Krishna6031b7c2015-09-28 17:46:29 -0700258
259 def fetchLatestClusterFile( self, branch="master" ):
260 """
261 Fetch onos-form-cluster file from a particular branch
262 """
263 try:
264 command = "wget -N https://raw.githubusercontent.com/opennetworkinglab/\
265 onos/" + branch + "/tools/package/bin/onos-form-cluster"
266 subprocess.call( command ) # output checks are missing for now
267 command = "chmod u+x " + "onos-form-cluster"
268 subprocess.call( command )
269 return main.TRUE
270 except Exception:
271 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700272 main.cleanAndExit()
Hari Krishna6031b7c2015-09-28 17:46:29 -0700273
Jon Hall321a4572016-05-04 15:28:25 -0700274 def onosFormCluster( self, onosIPs, cmdPath, user="karaf", passwd="karaf" ):
Hari Krishna6031b7c2015-09-28 17:46:29 -0700275 """
276 From ONOS cluster for IP addresses in onosIPs list
277 """
278 try:
279 onosIPs = " ".join(onosIPs)
Jon Hall321a4572016-05-04 15:28:25 -0700280 command = "{}/onos-form-cluster -u {} -p {} {}".format( cmdPath,
281 user,
282 passwd,
suibin zhang1ab833b2016-05-12 12:10:11 -0700283 onosIPs )
suibin zhangfd266fd2015-10-27 17:06:33 -0700284 result = subprocess.call( command, shell=True )
285 if result == 0:
286 return main.TRUE
287 else:
288 main.log.info("Something is not right in forming cluster>")
289 return main.FALSE
Hari Krishna6031b7c2015-09-28 17:46:29 -0700290 except Exception:
291 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700292 main.cleanAndExit()
Hari Krishna6031b7c2015-09-28 17:46:29 -0700293
suibin zhangfd266fd2015-10-27 17:06:33 -0700294 def dockerIP( self, ctName ):
Hari Krishna6031b7c2015-09-28 17:46:29 -0700295 """
296 Fetch IP address assigned to specified node/container
297 """
298 try:
suibin zhangfd266fd2015-10-27 17:06:33 -0700299 output = self.dockerClient.inspect_container( ctName )
Hari Krishna6031b7c2015-09-28 17:46:29 -0700300 nodeIP = output['NetworkSettings']['IPAddress']
suibin zhangfd266fd2015-10-27 17:06:33 -0700301 main.log.info( " Docker IP " + str(nodeIP) )
Hari Krishna6031b7c2015-09-28 17:46:29 -0700302 return str(nodeIP)
303
304 except Exception:
305 main.log.exception( self.name + ": Uncaught exception!" )
Devin Lim44075962017-08-11 10:56:37 -0700306 main.cleanAndExit()
suibin zhangfd266fd2015-10-27 17:06:33 -0700307