blob: 10743da252606ade54dca2e22e16c8b5c2a91275 [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
suibin zhangfd266fd2015-10-27 17:06:33 -070050 def dockerPull( self, onosRepo ="onosproject/onos", onosTag="latest" ):
Hari Krishna6031b7c2015-09-28 17:46:29 -070051 """
52 Pulls Docker image from repository
53 """
54 try:
55 main.log.info( self.name +
suibin zhangfd266fd2015-10-27 17:06:33 -070056 ": Pulling Docker image " + onosRepo + ":"+ onosTag )
57 for line in self.dockerClient.pull( repository = onosRepo, \
58 tag = onosTag, stream = True ):
suibin zhang3b067ea2015-11-05 13:55:21 -080059 print "#",
60 main.log.info(json.dumps(json.loads(line), indent =4))
suibin zhangfd266fd2015-10-27 17:06:33 -070061
62 #response = json.dumps( json.load( pullResult ), indent=4 )
63 if re.search( "for onosproject/onos:latest", line ):
64 main.log.info( "latest onos docker image pulled is: " + line )
Hari Krishna6031b7c2015-09-28 17:46:29 -070065 return main.TRUE
66 else:
suibin zhangfd266fd2015-10-27 17:06:33 -070067 main.log.error( "Failed to download image from: " + onosRepo +":"+ onosTag )
Hari Krishna6031b7c2015-09-28 17:46:29 -070068 main.log.error( "Error respone: " )
suibin zhang3b067ea2015-11-05 13:55:21 -080069 main.log.error( line )
Hari Krishna6031b7c2015-09-28 17:46:29 -070070 return main.FALSE
71 except Exception:
72 main.log.exception( self.name + ": Uncaught exception!" )
73 main.cleanup()
74 main.exit()
75
suibin zhangfd266fd2015-10-27 17:06:33 -070076 def dockerCreateCT( self, onosImage="onosproject/onos:latest", onosNode="onos1" ):
Hari Krishna6031b7c2015-09-28 17:46:29 -070077 """
suibin zhangfd266fd2015-10-27 17:06:33 -070078 Create a Docker container with a specific image
Hari Krishna6031b7c2015-09-28 17:46:29 -070079 """
80 try:
81 main.log.info( self.name +
suibin zhangfd266fd2015-10-27 17:06:33 -070082 ": Creating Docker container for node: " + onosNode )
83 response = self.dockerClient.create_container( image=onosImage, \
84 tty=True, name=onosNode, detach=True )
85 #print response
86 #print response.get("Id")
87 #print response.get("Warnings")
88 if( str( response.get("Warnings") ) == 'None' ):
89 main.log.info( "Created container for node: " + onosNode + "; container id is: " + response.get("Id") )
90 return ( main.TRUE, response.get("Id") )
Hari Krishna6031b7c2015-09-28 17:46:29 -070091 else:
92 main.log.info( "Noticed warnings during create" )
suibin zhangfd266fd2015-10-27 17:06:33 -070093 return ( main.FALSE, null)
Hari Krishna6031b7c2015-09-28 17:46:29 -070094 except Exception:
95 main.log.exception( self.name + ": Uncaught exception!" )
96 main.cleanup()
97 main.exit()
98
suibin zhangfd266fd2015-10-27 17:06:33 -070099 def dockerStartCT( self, ctID ):
Hari Krishna6031b7c2015-09-28 17:46:29 -0700100 """
101 Start Docker container
102 """
103 try:
104 main.log.info( self.name +
suibin zhangfd266fd2015-10-27 17:06:33 -0700105 ": Starting Docker conatiner Id " + ctID )
106 response = self.dockerClient.start( container = ctID )
107 if response is None:
108 main.log.info( "Started container for Id: " + ctID )
Hari Krishna6031b7c2015-09-28 17:46:29 -0700109 return main.TRUE
110 else:
111 main.log.info( "Noticed warnings during start" )
112 return main.FALSE
113 except Exception:
114 main.log.exception( self.name + ": Uncaught exception!" )
115 main.cleanup()
116 main.exit()
117
suibin zhangfd266fd2015-10-27 17:06:33 -0700118 def dockerStopCT( self, ctName ):
Hari Krishna6031b7c2015-09-28 17:46:29 -0700119 """
120 Stop docker container
121 """
122 try:
123 main.log.info( self.name +
suibin zhangfd266fd2015-10-27 17:06:33 -0700124 ": Stopping Docker conatiner for node " + ctName )
125 response = self.dockerClient.stop( ctName )
126 if response is None:
127 main.log.info( "Stopped container for node: " + ctName )
Hari Krishna6031b7c2015-09-28 17:46:29 -0700128 return main.TRUE
129 else:
130 main.log.info( "Noticed warnings during stop" )
131 return main.FALSE
suibin zhangfd266fd2015-10-27 17:06:33 -0700132 except errors.NotFound:
133 main.log.info( ctName + " not found! Continue on tests...")
134 return main.TRUE
Hari Krishna6031b7c2015-09-28 17:46:29 -0700135 except Exception:
136 main.log.exception( self.name + ": Uncaught exception!" )
suibin zhangfd266fd2015-10-27 17:06:33 -0700137 #main.cleanup()
138 #main.exit()
Hari Krishna6031b7c2015-09-28 17:46:29 -0700139
suibin zhangfd266fd2015-10-27 17:06:33 -0700140 def dockerRestartCT( self, ctName ):
Hari Krishna6031b7c2015-09-28 17:46:29 -0700141 """
142 Restart Docker container
143 """
144 try:
145 main.log.info( self.name +
suibin zhangfd266fd2015-10-27 17:06:33 -0700146 ": Restarting Docker conatiner for node " + ctName )
147 response = self.dockerClient.restart( ctName )
148 if response is None:
149 main.log.info( "Restarted container for node: " + ctName )
Hari Krishna6031b7c2015-09-28 17:46:29 -0700150 return main.TRUE
151 else:
152 main.log.info( "Noticed warnings during Restart" )
153 return main.FALSE
154 except Exception:
155 main.log.exception( self.name + ": Uncaught exception!" )
156 main.cleanup()
157 main.exit()
158
suibin zhangfd266fd2015-10-27 17:06:33 -0700159 def dockerCheckCTName( self, ctName):
Hari Krishna6031b7c2015-09-28 17:46:29 -0700160 """
161 Check Docker conatiner status
162 """
163 try:
164 main.log.info( self.name +
Jon Hall3dbc7dc2015-12-01 12:11:42 -0800165 ": Checking Docker Status for CT with 'Names' " + ctName )
suibin zhangfd266fd2015-10-27 17:06:33 -0700166 namelist = [response["Names"] for response in self.dockerClient.containers(all=True) if not []]
167 main.log.info("Name list is: " + str(namelist) )
168 if( [ctName] in namelist):
169 main.log.info( "Container " + ctName + " exists" )
Hari Krishna6031b7c2015-09-28 17:46:29 -0700170 return main.TRUE
171 else:
suibin zhangfd266fd2015-10-27 17:06:33 -0700172 main.log.info( "Container " + ctName + " does not exist" )
Hari Krishna6031b7c2015-09-28 17:46:29 -0700173 return main.FALSE
suibin zhangfd266fd2015-10-27 17:06:33 -0700174 except errors.NotFound:
175 main.log.warn( ctName + "not found! Continue with the tests...")
176 return main.FALSE
Hari Krishna6031b7c2015-09-28 17:46:29 -0700177 except Exception:
suibin zhangfd266fd2015-10-27 17:06:33 -0700178 main.log.exception( self.name + ": Uncaught exception! Continue tests..." )
179 #main.cleanup()
180 #main.exit()
Hari Krishna6031b7c2015-09-28 17:46:29 -0700181
suibin zhangfd266fd2015-10-27 17:06:33 -0700182 def dockerRemoveCT( self, ctName ):
Hari Krishna6031b7c2015-09-28 17:46:29 -0700183 """
184 Remove Docker conatiner
185 """
186 try:
187 main.log.info( self.name +
suibin zhangfd266fd2015-10-27 17:06:33 -0700188 ": Removing Docker container for node " + ctName )
189 response = self.dockerClient.remove_container( ctName, force=True )
190 if response is None:
191 main.log.info( "Removed container for node: " + ctName )
Hari Krishna6031b7c2015-09-28 17:46:29 -0700192 return main.TRUE
193 else:
suibin zhangfd266fd2015-10-27 17:06:33 -0700194 main.log.info( "Noticed warnings during Remove " + ctName)
Hari Krishna6031b7c2015-09-28 17:46:29 -0700195 return main.FALSE
suibin zhangfd266fd2015-10-27 17:06:33 -0700196 main.log.exception(self.name + ": not found, continuing...")
197 except errors.NotFound:
198 main.log.warn( ctName + "not found! Continue with the tests...")
199 return main.TRUE
Hari Krishna6031b7c2015-09-28 17:46:29 -0700200 except Exception:
suibin zhangfd266fd2015-10-27 17:06:33 -0700201 main.log.exception( self.name + ": Uncaught exception! Continuing..." )
202 #main.cleanup()
203 #main.exit()
Hari Krishna6031b7c2015-09-28 17:46:29 -0700204
suibin zhangfd266fd2015-10-27 17:06:33 -0700205 def dockerRemoveImage( self, image="onosproject/onos:latest" ):
Hari Krishna6031b7c2015-09-28 17:46:29 -0700206 """
207 Remove Docker image
208 """
suibin zhang3b067ea2015-11-05 13:55:21 -0800209 rmResult = main.TRUE
210
Jeremy Songsterae01bba2016-07-11 15:39:17 -0700211 if self.dockerClient.images() is []:
suibin zhang5ae25332015-11-04 13:56:28 -0800212 main.log.info( "No docker image found with RepoTags: " + image )
suibin zhang3b067ea2015-11-05 13:55:21 -0800213 return rmResult
suibin zhangfd266fd2015-10-27 17:06:33 -0700214 else:
suibin zhang3b067ea2015-11-05 13:55:21 -0800215 list = [ image["Id"] for image in self.dockerClient.images() if image["RepoTags"][0] == '<none>:<none>' ]
216 for id in list:
217 try:
218 main.log.info( self.name + ": Removing Docker image " + id )
219 response = self.dockerClient.remove_image(id, force = True)
220 if response is None:
221 main.log.info( "Removed Docker image: " + id )
222 rmResult = rmResult and main.TRUE
223 else:
224 main.log.info( "Noticed warnings during Remove " + id )
225 rmResult = rmResult and main.FALSE
226 except errors.NotFound:
227 main.log.warn( image + "not found! Continue with the tests...")
228 rmResult = rmResult and main.TRUE
229 except Exception:
230 main.log.exception( self.name + ": Uncaught exception! Continuing..." )
231 rmResult = rmResult and main.FALSE
232 #main.cleanup()
233 #main.exit()
234 return rmResult
Hari Krishna6031b7c2015-09-28 17:46:29 -0700235
236 def fetchLatestClusterFile( self, branch="master" ):
237 """
238 Fetch onos-form-cluster file from a particular branch
239 """
240 try:
241 command = "wget -N https://raw.githubusercontent.com/opennetworkinglab/\
242 onos/" + branch + "/tools/package/bin/onos-form-cluster"
243 subprocess.call( command ) # output checks are missing for now
244 command = "chmod u+x " + "onos-form-cluster"
245 subprocess.call( command )
246 return main.TRUE
247 except Exception:
248 main.log.exception( self.name + ": Uncaught exception!" )
249 main.cleanup()
250 main.exit()
251
Jon Hall321a4572016-05-04 15:28:25 -0700252 def onosFormCluster( self, onosIPs, cmdPath, user="karaf", passwd="karaf" ):
Hari Krishna6031b7c2015-09-28 17:46:29 -0700253 """
254 From ONOS cluster for IP addresses in onosIPs list
255 """
256 try:
257 onosIPs = " ".join(onosIPs)
Jon Hall321a4572016-05-04 15:28:25 -0700258 command = "{}/onos-form-cluster -u {} -p {} {}".format( cmdPath,
259 user,
260 passwd,
suibin zhang1ab833b2016-05-12 12:10:11 -0700261 onosIPs )
suibin zhangfd266fd2015-10-27 17:06:33 -0700262 result = subprocess.call( command, shell=True )
263 if result == 0:
264 return main.TRUE
265 else:
266 main.log.info("Something is not right in forming cluster>")
267 return main.FALSE
Hari Krishna6031b7c2015-09-28 17:46:29 -0700268 except Exception:
269 main.log.exception( self.name + ": Uncaught exception!" )
270 main.cleanup()
271 main.exit()
272
suibin zhangfd266fd2015-10-27 17:06:33 -0700273 def dockerIP( self, ctName ):
Hari Krishna6031b7c2015-09-28 17:46:29 -0700274 """
275 Fetch IP address assigned to specified node/container
276 """
277 try:
suibin zhangfd266fd2015-10-27 17:06:33 -0700278 output = self.dockerClient.inspect_container( ctName )
Hari Krishna6031b7c2015-09-28 17:46:29 -0700279 nodeIP = output['NetworkSettings']['IPAddress']
suibin zhangfd266fd2015-10-27 17:06:33 -0700280 main.log.info( " Docker IP " + str(nodeIP) )
Hari Krishna6031b7c2015-09-28 17:46:29 -0700281 return str(nodeIP)
282
283 except Exception:
284 main.log.exception( self.name + ": Uncaught exception!" )
285 main.cleanup()
286 main.exit()
suibin zhangfd266fd2015-10-27 17:06:33 -0700287