Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 1 | #!/usr/bin/env python |
Jeremy Songster | ae01bba | 2016-07-11 15:39:17 -0700 | [diff] [blame] | 2 | """ |
| 3 | Modified 2016 by ON.Lab |
| 4 | |
| 5 | Please refer questions to either the onos test mailing list at <onos-test@onosproject.org>, |
| 6 | the System Testing Plans and Results wiki page at <https://wiki.onosproject.org/x/voMg>, |
| 7 | or the System Testing Guide page at <https://wiki.onosproject.org/x/WYQg> |
| 8 | """ |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 9 | |
| 10 | import json |
| 11 | import os |
| 12 | import re |
| 13 | import subprocess |
| 14 | from docker import Client |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 15 | from docker import errors |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 16 | from drivers.common.apidriver import API |
| 17 | |
| 18 | class 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 Parab | cc40645 | 2017-02-28 15:15:25 -0800 | [diff] [blame] | 50 | 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 zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 64 | def dockerPull( self, onosRepo ="onosproject/onos", onosTag="latest" ): |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 65 | """ |
| 66 | Pulls Docker image from repository |
| 67 | """ |
| 68 | try: |
| 69 | main.log.info( self.name + |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 70 | ": Pulling Docker image " + onosRepo + ":"+ onosTag ) |
| 71 | for line in self.dockerClient.pull( repository = onosRepo, \ |
| 72 | tag = onosTag, stream = True ): |
suibin zhang | 3b067ea | 2015-11-05 13:55:21 -0800 | [diff] [blame] | 73 | print "#", |
| 74 | main.log.info(json.dumps(json.loads(line), indent =4)) |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 75 | |
| 76 | #response = json.dumps( json.load( pullResult ), indent=4 ) |
Pratik Parab | cc40645 | 2017-02-28 15:15:25 -0800 | [diff] [blame] | 77 | if re.search( "for onosproject/onos:" + onosTag, line ): |
| 78 | main.log.info( "onos docker image pulled is: " + line ) |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 79 | return main.TRUE |
| 80 | else: |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 81 | main.log.error( "Failed to download image from: " + onosRepo +":"+ onosTag ) |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 82 | main.log.error( "Error respone: " ) |
suibin zhang | 3b067ea | 2015-11-05 13:55:21 -0800 | [diff] [blame] | 83 | main.log.error( line ) |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 84 | return main.FALSE |
| 85 | except Exception: |
| 86 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 87 | main.cleanup() |
| 88 | main.exit() |
| 89 | |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 90 | def dockerCreateCT( self, onosImage="onosproject/onos:latest", onosNode="onos1" ): |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 91 | """ |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 92 | Create a Docker container with a specific image |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 93 | """ |
| 94 | try: |
| 95 | main.log.info( self.name + |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 96 | ": 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 Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 105 | else: |
| 106 | main.log.info( "Noticed warnings during create" ) |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 107 | return ( main.FALSE, null) |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 108 | except Exception: |
| 109 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 110 | main.cleanup() |
| 111 | main.exit() |
| 112 | |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 113 | def dockerStartCT( self, ctID ): |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 114 | """ |
| 115 | Start Docker container |
| 116 | """ |
| 117 | try: |
| 118 | main.log.info( self.name + |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 119 | ": 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 Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 123 | 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 zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 132 | def dockerStopCT( self, ctName ): |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 133 | """ |
| 134 | Stop docker container |
| 135 | """ |
| 136 | try: |
| 137 | main.log.info( self.name + |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 138 | ": 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 Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 142 | return main.TRUE |
| 143 | else: |
| 144 | main.log.info( "Noticed warnings during stop" ) |
| 145 | return main.FALSE |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 146 | except errors.NotFound: |
| 147 | main.log.info( ctName + " not found! Continue on tests...") |
| 148 | return main.TRUE |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 149 | except Exception: |
| 150 | main.log.exception( self.name + ": Uncaught exception!" ) |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 151 | #main.cleanup() |
| 152 | #main.exit() |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 153 | |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 154 | def dockerRestartCT( self, ctName ): |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 155 | """ |
| 156 | Restart Docker container |
| 157 | """ |
| 158 | try: |
| 159 | main.log.info( self.name + |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 160 | ": 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 Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 164 | 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 zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 173 | def dockerCheckCTName( self, ctName): |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 174 | """ |
| 175 | Check Docker conatiner status |
| 176 | """ |
| 177 | try: |
| 178 | main.log.info( self.name + |
Jon Hall | 3dbc7dc | 2015-12-01 12:11:42 -0800 | [diff] [blame] | 179 | ": Checking Docker Status for CT with 'Names' " + ctName ) |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 180 | 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 Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 184 | return main.TRUE |
| 185 | else: |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 186 | main.log.info( "Container " + ctName + " does not exist" ) |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 187 | return main.FALSE |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 188 | except errors.NotFound: |
| 189 | main.log.warn( ctName + "not found! Continue with the tests...") |
| 190 | return main.FALSE |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 191 | except Exception: |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 192 | main.log.exception( self.name + ": Uncaught exception! Continue tests..." ) |
| 193 | #main.cleanup() |
| 194 | #main.exit() |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 195 | |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 196 | def dockerRemoveCT( self, ctName ): |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 197 | """ |
| 198 | Remove Docker conatiner |
| 199 | """ |
| 200 | try: |
| 201 | main.log.info( self.name + |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 202 | ": 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 Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 206 | return main.TRUE |
| 207 | else: |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 208 | main.log.info( "Noticed warnings during Remove " + ctName) |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 209 | return main.FALSE |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 210 | 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 Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 214 | except Exception: |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 215 | main.log.exception( self.name + ": Uncaught exception! Continuing..." ) |
| 216 | #main.cleanup() |
| 217 | #main.exit() |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 218 | |
Pratik Parab | cc40645 | 2017-02-28 15:15:25 -0800 | [diff] [blame] | 219 | def dockerRemoveImage( self, imageRepoTag=None ): |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 220 | """ |
| 221 | Remove Docker image |
| 222 | """ |
suibin zhang | 3b067ea | 2015-11-05 13:55:21 -0800 | [diff] [blame] | 223 | rmResult = main.TRUE |
Jeremy Songster | ae01bba | 2016-07-11 15:39:17 -0700 | [diff] [blame] | 224 | if self.dockerClient.images() is []: |
Pratik Parab | cc40645 | 2017-02-28 15:15:25 -0800 | [diff] [blame] | 225 | main.log.info( "No docker image found" ) |
suibin zhang | 3b067ea | 2015-11-05 13:55:21 -0800 | [diff] [blame] | 226 | return rmResult |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 227 | else: |
Pratik Parab | cc40645 | 2017-02-28 15:15:25 -0800 | [diff] [blame] | 228 | 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 zhang | 3b067ea | 2015-11-05 13:55:21 -0800 | [diff] [blame] | 232 | 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 Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 250 | |
| 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 Hall | 321a457 | 2016-05-04 15:28:25 -0700 | [diff] [blame] | 267 | def onosFormCluster( self, onosIPs, cmdPath, user="karaf", passwd="karaf" ): |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 268 | """ |
| 269 | From ONOS cluster for IP addresses in onosIPs list |
| 270 | """ |
| 271 | try: |
| 272 | onosIPs = " ".join(onosIPs) |
Jon Hall | 321a457 | 2016-05-04 15:28:25 -0700 | [diff] [blame] | 273 | command = "{}/onos-form-cluster -u {} -p {} {}".format( cmdPath, |
| 274 | user, |
| 275 | passwd, |
suibin zhang | 1ab833b | 2016-05-12 12:10:11 -0700 | [diff] [blame] | 276 | onosIPs ) |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 277 | 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 Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 283 | except Exception: |
| 284 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 285 | main.cleanup() |
| 286 | main.exit() |
| 287 | |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 288 | def dockerIP( self, ctName ): |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 289 | """ |
| 290 | Fetch IP address assigned to specified node/container |
| 291 | """ |
| 292 | try: |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 293 | output = self.dockerClient.inspect_container( ctName ) |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 294 | nodeIP = output['NetworkSettings']['IPAddress'] |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 295 | main.log.info( " Docker IP " + str(nodeIP) ) |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 296 | return str(nodeIP) |
| 297 | |
| 298 | except Exception: |
| 299 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 300 | main.cleanup() |
| 301 | main.exit() |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 302 | |