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 | |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 50 | def dockerPull( self, onosRepo ="onosproject/onos", onosTag="latest" ): |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 51 | """ |
| 52 | Pulls Docker image from repository |
| 53 | """ |
| 54 | try: |
| 55 | main.log.info( self.name + |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 56 | ": Pulling Docker image " + onosRepo + ":"+ onosTag ) |
| 57 | for line in self.dockerClient.pull( repository = onosRepo, \ |
| 58 | tag = onosTag, stream = True ): |
suibin zhang | 3b067ea | 2015-11-05 13:55:21 -0800 | [diff] [blame] | 59 | print "#", |
| 60 | main.log.info(json.dumps(json.loads(line), indent =4)) |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 61 | |
| 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 Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 65 | return main.TRUE |
| 66 | else: |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 67 | main.log.error( "Failed to download image from: " + onosRepo +":"+ onosTag ) |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 68 | main.log.error( "Error respone: " ) |
suibin zhang | 3b067ea | 2015-11-05 13:55:21 -0800 | [diff] [blame] | 69 | main.log.error( line ) |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 70 | return main.FALSE |
| 71 | except Exception: |
| 72 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 73 | main.cleanup() |
| 74 | main.exit() |
| 75 | |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 76 | def dockerCreateCT( self, onosImage="onosproject/onos:latest", onosNode="onos1" ): |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 77 | """ |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 78 | Create a Docker container with a specific image |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 79 | """ |
| 80 | try: |
| 81 | main.log.info( self.name + |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 82 | ": 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 Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 91 | else: |
| 92 | main.log.info( "Noticed warnings during create" ) |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 93 | return ( main.FALSE, null) |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 94 | except Exception: |
| 95 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 96 | main.cleanup() |
| 97 | main.exit() |
| 98 | |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 99 | def dockerStartCT( self, ctID ): |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 100 | """ |
| 101 | Start Docker container |
| 102 | """ |
| 103 | try: |
| 104 | main.log.info( self.name + |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 105 | ": 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 Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 109 | 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 zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 118 | def dockerStopCT( self, ctName ): |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 119 | """ |
| 120 | Stop docker container |
| 121 | """ |
| 122 | try: |
| 123 | main.log.info( self.name + |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 124 | ": 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 Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 128 | return main.TRUE |
| 129 | else: |
| 130 | main.log.info( "Noticed warnings during stop" ) |
| 131 | return main.FALSE |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 132 | except errors.NotFound: |
| 133 | main.log.info( ctName + " not found! Continue on tests...") |
| 134 | return main.TRUE |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 135 | except Exception: |
| 136 | main.log.exception( self.name + ": Uncaught exception!" ) |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 137 | #main.cleanup() |
| 138 | #main.exit() |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 139 | |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 140 | def dockerRestartCT( self, ctName ): |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 141 | """ |
| 142 | Restart Docker container |
| 143 | """ |
| 144 | try: |
| 145 | main.log.info( self.name + |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 146 | ": 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 Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 150 | 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 zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 159 | def dockerCheckCTName( self, ctName): |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 160 | """ |
| 161 | Check Docker conatiner status |
| 162 | """ |
| 163 | try: |
| 164 | main.log.info( self.name + |
Jon Hall | 3dbc7dc | 2015-12-01 12:11:42 -0800 | [diff] [blame] | 165 | ": Checking Docker Status for CT with 'Names' " + ctName ) |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 166 | 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 Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 170 | return main.TRUE |
| 171 | else: |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 172 | main.log.info( "Container " + ctName + " does not exist" ) |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 173 | return main.FALSE |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 174 | except errors.NotFound: |
| 175 | main.log.warn( ctName + "not found! Continue with the tests...") |
| 176 | return main.FALSE |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 177 | except Exception: |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 178 | main.log.exception( self.name + ": Uncaught exception! Continue tests..." ) |
| 179 | #main.cleanup() |
| 180 | #main.exit() |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 181 | |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 182 | def dockerRemoveCT( self, ctName ): |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 183 | """ |
| 184 | Remove Docker conatiner |
| 185 | """ |
| 186 | try: |
| 187 | main.log.info( self.name + |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 188 | ": 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 Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 192 | return main.TRUE |
| 193 | else: |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 194 | main.log.info( "Noticed warnings during Remove " + ctName) |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 195 | return main.FALSE |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 196 | 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 Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 200 | except Exception: |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 201 | main.log.exception( self.name + ": Uncaught exception! Continuing..." ) |
| 202 | #main.cleanup() |
| 203 | #main.exit() |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 204 | |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 205 | def dockerRemoveImage( self, image="onosproject/onos:latest" ): |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 206 | """ |
| 207 | Remove Docker image |
| 208 | """ |
suibin zhang | 3b067ea | 2015-11-05 13:55:21 -0800 | [diff] [blame] | 209 | rmResult = main.TRUE |
| 210 | |
Jeremy Songster | ae01bba | 2016-07-11 15:39:17 -0700 | [diff] [blame] | 211 | if self.dockerClient.images() is []: |
suibin zhang | 5ae2533 | 2015-11-04 13:56:28 -0800 | [diff] [blame] | 212 | main.log.info( "No docker image found with RepoTags: " + image ) |
suibin zhang | 3b067ea | 2015-11-05 13:55:21 -0800 | [diff] [blame] | 213 | return rmResult |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 214 | else: |
suibin zhang | 3b067ea | 2015-11-05 13:55:21 -0800 | [diff] [blame] | 215 | 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 Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 235 | |
| 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 Hall | 321a457 | 2016-05-04 15:28:25 -0700 | [diff] [blame] | 252 | def onosFormCluster( self, onosIPs, cmdPath, user="karaf", passwd="karaf" ): |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 253 | """ |
| 254 | From ONOS cluster for IP addresses in onosIPs list |
| 255 | """ |
| 256 | try: |
| 257 | onosIPs = " ".join(onosIPs) |
Jon Hall | 321a457 | 2016-05-04 15:28:25 -0700 | [diff] [blame] | 258 | command = "{}/onos-form-cluster -u {} -p {} {}".format( cmdPath, |
| 259 | user, |
| 260 | passwd, |
suibin zhang | 1ab833b | 2016-05-12 12:10:11 -0700 | [diff] [blame] | 261 | onosIPs ) |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 262 | 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 Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 268 | except Exception: |
| 269 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 270 | main.cleanup() |
| 271 | main.exit() |
| 272 | |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 273 | def dockerIP( self, ctName ): |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 274 | """ |
| 275 | Fetch IP address assigned to specified node/container |
| 276 | """ |
| 277 | try: |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 278 | output = self.dockerClient.inspect_container( ctName ) |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 279 | nodeIP = output['NetworkSettings']['IPAddress'] |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 280 | main.log.info( " Docker IP " + str(nodeIP) ) |
Hari Krishna | 6031b7c | 2015-09-28 17:46:29 -0700 | [diff] [blame] | 281 | return str(nodeIP) |
| 282 | |
| 283 | except Exception: |
| 284 | main.log.exception( self.name + ": Uncaught exception!" ) |
| 285 | main.cleanup() |
| 286 | main.exit() |
suibin zhang | fd266fd | 2015-10-27 17:06:33 -0700 | [diff] [blame] | 287 | |