Kurt Taylor | 6a6f5be | 2013-04-02 18:53:47 -0400 | [diff] [blame] | 1 | # Copyright 2013 IBM Corp. |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 2 | # All Rights Reserved. |
| 3 | # |
| 4 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 5 | # not use this file except in compliance with the License. You may obtain |
| 6 | # a copy of the License at |
| 7 | # |
| 8 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | # |
| 10 | # Unless required by applicable law or agreed to in writing, software |
| 11 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | # License for the specific language governing permissions and limitations |
| 14 | # under the License. |
| 15 | |
| 16 | import copy |
Attila Fazekas | 5c06881 | 2013-02-14 15:29:44 +0100 | [diff] [blame] | 17 | import errno |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 18 | import os |
Attila Fazekas | e72b7cd | 2013-03-26 18:34:21 +0100 | [diff] [blame] | 19 | import time |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 20 | |
Doug Hellmann | 583ce2c | 2015-03-11 14:55:46 +0000 | [diff] [blame] | 21 | from oslo_log import log as logging |
Matthew Treinish | 2190551 | 2015-07-13 10:33:35 -0400 | [diff] [blame] | 22 | from oslo_serialization import jsonutils as json |
Matthew Treinish | 7142668 | 2015-04-23 11:19:38 -0400 | [diff] [blame] | 23 | import six |
Matthew Treinish | 8912814 | 2015-04-23 10:44:30 -0400 | [diff] [blame] | 24 | from six.moves.urllib import parse as urllib |
Matthew Treinish | 01472ff | 2015-02-20 17:26:52 -0500 | [diff] [blame] | 25 | from tempest_lib.common.utils import misc as misc_utils |
Masayuki Igawa | bfa0760 | 2015-01-20 18:47:17 +0900 | [diff] [blame] | 26 | from tempest_lib import exceptions as lib_exc |
| 27 | |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 28 | from tempest.common import glance_http |
Ken'ichi Ohmichi | 0e83665 | 2015-01-08 04:38:56 +0000 | [diff] [blame] | 29 | from tempest.common import service_client |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 30 | from tempest import exceptions |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 31 | |
Attila Fazekas | e72b7cd | 2013-03-26 18:34:21 +0100 | [diff] [blame] | 32 | LOG = logging.getLogger(__name__) |
| 33 | |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 34 | |
Ken'ichi Ohmichi | a628707 | 2015-07-02 02:43:15 +0000 | [diff] [blame] | 35 | class ImageClient(service_client.ServiceClient): |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 36 | |
Masayuki Igawa | bc7e189 | 2015-03-03 11:46:48 +0900 | [diff] [blame] | 37 | def __init__(self, auth_provider, catalog_type, region, endpoint_type=None, |
| 38 | build_interval=None, build_timeout=None, |
| 39 | disable_ssl_certificate_validation=None, |
David Kranz | 85b660b | 2015-03-23 10:26:52 -0400 | [diff] [blame] | 40 | ca_certs=None, trace_requests=None): |
Ken'ichi Ohmichi | a628707 | 2015-07-02 02:43:15 +0000 | [diff] [blame] | 41 | super(ImageClient, self).__init__( |
Ken'ichi Ohmichi | 0690ea4 | 2015-01-02 07:03:51 +0000 | [diff] [blame] | 42 | auth_provider, |
Masayuki Igawa | bc7e189 | 2015-03-03 11:46:48 +0900 | [diff] [blame] | 43 | catalog_type, |
| 44 | region, |
| 45 | endpoint_type=endpoint_type, |
| 46 | build_interval=build_interval, |
| 47 | build_timeout=build_timeout, |
| 48 | disable_ssl_certificate_validation=( |
| 49 | disable_ssl_certificate_validation), |
| 50 | ca_certs=ca_certs, |
David Kranz | 85b660b | 2015-03-23 10:26:52 -0400 | [diff] [blame] | 51 | trace_requests=trace_requests) |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 52 | self._http = None |
Masayuki Igawa | bc7e189 | 2015-03-03 11:46:48 +0900 | [diff] [blame] | 53 | self.dscv = disable_ssl_certificate_validation |
| 54 | self.ca_certs = ca_certs |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 55 | |
| 56 | def _image_meta_from_headers(self, headers): |
| 57 | meta = {'properties': {}} |
Matthew Treinish | 7142668 | 2015-04-23 11:19:38 -0400 | [diff] [blame] | 58 | for key, value in six.iteritems(headers): |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 59 | if key.startswith('x-image-meta-property-'): |
| 60 | _key = key[22:] |
| 61 | meta['properties'][_key] = value |
| 62 | elif key.startswith('x-image-meta-'): |
| 63 | _key = key[13:] |
| 64 | meta[_key] = value |
| 65 | |
| 66 | for key in ['is_public', 'protected', 'deleted']: |
| 67 | if key in meta: |
| 68 | meta[key] = meta[key].strip().lower() in ('t', 'true', 'yes', |
| 69 | '1') |
| 70 | for key in ['size', 'min_ram', 'min_disk']: |
| 71 | if key in meta: |
| 72 | try: |
| 73 | meta[key] = int(meta[key]) |
| 74 | except ValueError: |
| 75 | pass |
| 76 | return meta |
| 77 | |
| 78 | def _image_meta_to_headers(self, fields): |
| 79 | headers = {} |
| 80 | fields_copy = copy.deepcopy(fields) |
Attila Fazekas | e72b7cd | 2013-03-26 18:34:21 +0100 | [diff] [blame] | 81 | copy_from = fields_copy.pop('copy_from', None) |
| 82 | if copy_from is not None: |
| 83 | headers['x-glance-api-copy-from'] = copy_from |
Matthew Treinish | 7142668 | 2015-04-23 11:19:38 -0400 | [diff] [blame] | 84 | for key, value in six.iteritems(fields_copy.pop('properties', {})): |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 85 | headers['x-image-meta-property-%s' % key] = str(value) |
Matthew Treinish | 7142668 | 2015-04-23 11:19:38 -0400 | [diff] [blame] | 86 | for key, value in six.iteritems(fields_copy.pop('api', {})): |
Attila Fazekas | e72b7cd | 2013-03-26 18:34:21 +0100 | [diff] [blame] | 87 | headers['x-glance-api-property-%s' % key] = str(value) |
Matthew Treinish | 7142668 | 2015-04-23 11:19:38 -0400 | [diff] [blame] | 88 | for key, value in six.iteritems(fields_copy): |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 89 | headers['x-image-meta-%s' % key] = str(value) |
| 90 | return headers |
| 91 | |
| 92 | def _get_file_size(self, obj): |
| 93 | """Analyze file-like object and attempt to determine its size. |
| 94 | |
| 95 | :param obj: file-like object, typically redirected from stdin. |
| 96 | :retval The file's size or None if it cannot be determined. |
| 97 | """ |
| 98 | # For large images, we need to supply the size of the |
| 99 | # image file. See LP Bugs #827660 and #845788. |
| 100 | if hasattr(obj, 'seek') and hasattr(obj, 'tell'): |
| 101 | try: |
| 102 | obj.seek(0, os.SEEK_END) |
| 103 | obj_size = obj.tell() |
| 104 | obj.seek(0) |
| 105 | return obj_size |
Dirk Mueller | 1db5db2 | 2013-06-23 20:21:32 +0200 | [diff] [blame] | 106 | except IOError as e: |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 107 | if e.errno == errno.ESPIPE: |
| 108 | # Illegal seek. This means the user is trying |
| 109 | # to pipe image data to the client, e.g. |
| 110 | # echo testdata | bin/glance add blah..., or |
| 111 | # that stdin is empty, or that a file-like |
| 112 | # object which doesn't support 'seek/tell' has |
| 113 | # been supplied. |
| 114 | return None |
| 115 | else: |
| 116 | raise |
| 117 | else: |
| 118 | # Cannot determine size of input image |
| 119 | return None |
| 120 | |
| 121 | def _get_http(self): |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 122 | return glance_http.HTTPClient(auth_provider=self.auth_provider, |
| 123 | filters=self.filters, |
Masayuki Igawa | bc7e189 | 2015-03-03 11:46:48 +0900 | [diff] [blame] | 124 | insecure=self.dscv, |
| 125 | ca_certs=self.ca_certs) |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 126 | |
| 127 | def _create_with_data(self, headers, data): |
| 128 | resp, body_iter = self.http.raw_request('POST', '/v1/images', |
| 129 | headers=headers, body=data) |
| 130 | self._error_checker('POST', '/v1/images', headers, data, resp, |
| 131 | body_iter) |
| 132 | body = json.loads(''.join([c for c in body_iter])) |
Ken'ichi Ohmichi | a6ac242 | 2015-01-13 01:09:39 +0000 | [diff] [blame] | 133 | return service_client.ResponseBody(resp, body['image']) |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 134 | |
| 135 | def _update_with_data(self, image_id, headers, data): |
| 136 | url = '/v1/images/%s' % image_id |
| 137 | resp, body_iter = self.http.raw_request('PUT', url, headers=headers, |
| 138 | body=data) |
| 139 | self._error_checker('PUT', url, headers, data, |
| 140 | resp, body_iter) |
| 141 | body = json.loads(''.join([c for c in body_iter])) |
Ken'ichi Ohmichi | a6ac242 | 2015-01-13 01:09:39 +0000 | [diff] [blame] | 142 | return service_client.ResponseBody(resp, body['image']) |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 143 | |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 144 | @property |
| 145 | def http(self): |
| 146 | if self._http is None: |
Masayuki Igawa | bc7e189 | 2015-03-03 11:46:48 +0900 | [diff] [blame] | 147 | self._http = self._get_http() |
Andrea Frittoli | 8bbdb16 | 2014-01-06 11:06:13 +0000 | [diff] [blame] | 148 | return self._http |
| 149 | |
Matthew Treinish | ce3ef92 | 2013-03-11 14:02:46 -0400 | [diff] [blame] | 150 | def create_image(self, name, container_format, disk_format, **kwargs): |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 151 | params = { |
| 152 | "name": name, |
| 153 | "container_format": container_format, |
| 154 | "disk_format": disk_format, |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 155 | } |
Matthew Treinish | ce3ef92 | 2013-03-11 14:02:46 -0400 | [diff] [blame] | 156 | |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 157 | headers = {} |
| 158 | |
hi2suresh | 75a2030 | 2013-04-09 09:17:06 +0000 | [diff] [blame] | 159 | for option in ['is_public', 'location', 'properties', |
| 160 | 'copy_from', 'min_ram']: |
Matthew Treinish | ce3ef92 | 2013-03-11 14:02:46 -0400 | [diff] [blame] | 161 | if option in kwargs: |
| 162 | params[option] = kwargs.get(option) |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 163 | |
| 164 | headers.update(self._image_meta_to_headers(params)) |
| 165 | |
Matthew Treinish | ce3ef92 | 2013-03-11 14:02:46 -0400 | [diff] [blame] | 166 | if 'data' in kwargs: |
| 167 | return self._create_with_data(headers, kwargs.get('data')) |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 168 | |
Matthew Treinish | ce3ef92 | 2013-03-11 14:02:46 -0400 | [diff] [blame] | 169 | resp, body = self.post('v1/images', None, headers) |
David Kranz | 9c3b3b6 | 2014-06-19 16:05:53 -0400 | [diff] [blame] | 170 | self.expected_success(201, resp.status) |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 171 | body = json.loads(body) |
Ken'ichi Ohmichi | a6ac242 | 2015-01-13 01:09:39 +0000 | [diff] [blame] | 172 | return service_client.ResponseBody(resp, body['image']) |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 173 | |
| 174 | def update_image(self, image_id, name=None, container_format=None, |
ivan-zhu | d1bbe5d | 2013-12-29 18:32:46 +0800 | [diff] [blame] | 175 | data=None, properties=None): |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 176 | params = {} |
| 177 | headers = {} |
| 178 | if name is not None: |
| 179 | params['name'] = name |
| 180 | |
| 181 | if container_format is not None: |
| 182 | params['container_format'] = container_format |
| 183 | |
ivan-zhu | d1bbe5d | 2013-12-29 18:32:46 +0800 | [diff] [blame] | 184 | if properties is not None: |
| 185 | params['properties'] = properties |
| 186 | |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 187 | headers.update(self._image_meta_to_headers(params)) |
| 188 | |
| 189 | if data is not None: |
| 190 | return self._update_with_data(image_id, headers, data) |
| 191 | |
| 192 | url = 'v1/images/%s' % image_id |
| 193 | resp, body = self.put(url, data, headers) |
David Kranz | 9c3b3b6 | 2014-06-19 16:05:53 -0400 | [diff] [blame] | 194 | self.expected_success(200, resp.status) |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 195 | body = json.loads(body) |
Ken'ichi Ohmichi | a6ac242 | 2015-01-13 01:09:39 +0000 | [diff] [blame] | 196 | return service_client.ResponseBody(resp, body['image']) |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 197 | |
| 198 | def delete_image(self, image_id): |
| 199 | url = 'v1/images/%s' % image_id |
David Kranz | 9c3b3b6 | 2014-06-19 16:05:53 -0400 | [diff] [blame] | 200 | resp, body = self.delete(url) |
| 201 | self.expected_success(200, resp.status) |
Ken'ichi Ohmichi | a6ac242 | 2015-01-13 01:09:39 +0000 | [diff] [blame] | 202 | return service_client.ResponseBody(resp, body) |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 203 | |
Ken'ichi Ohmichi | bcad2a2 | 2015-05-22 09:37:06 +0000 | [diff] [blame] | 204 | def list_images(self, detail=False, properties=dict(), |
| 205 | changes_since=None, **kwargs): |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 206 | url = 'v1/images' |
| 207 | |
Ken'ichi Ohmichi | bcad2a2 | 2015-05-22 09:37:06 +0000 | [diff] [blame] | 208 | if detail: |
| 209 | url += '/detail' |
Attila Fazekas | 11795b5 | 2013-02-24 15:49:08 +0100 | [diff] [blame] | 210 | |
ivan-zhu | ccc8946 | 2013-10-31 16:53:12 +0800 | [diff] [blame] | 211 | params = {} |
| 212 | for key, value in properties.items(): |
| 213 | params['property-%s' % key] = value |
| 214 | |
| 215 | kwargs.update(params) |
| 216 | |
ivan-zhu | d1bbe5d | 2013-12-29 18:32:46 +0800 | [diff] [blame] | 217 | if changes_since is not None: |
| 218 | kwargs['changes-since'] = changes_since |
| 219 | |
Attila Fazekas | 11795b5 | 2013-02-24 15:49:08 +0100 | [diff] [blame] | 220 | if len(kwargs) > 0: |
| 221 | url += '?%s' % urllib.urlencode(kwargs) |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 222 | |
| 223 | resp, body = self.get(url) |
David Kranz | 9c3b3b6 | 2014-06-19 16:05:53 -0400 | [diff] [blame] | 224 | self.expected_success(200, resp.status) |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 225 | body = json.loads(body) |
Ken'ichi Ohmichi | a6ac242 | 2015-01-13 01:09:39 +0000 | [diff] [blame] | 226 | return service_client.ResponseBodyList(resp, body['images']) |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 227 | |
Attila Fazekas | e72b7cd | 2013-03-26 18:34:21 +0100 | [diff] [blame] | 228 | def get_image_meta(self, image_id): |
| 229 | url = 'v1/images/%s' % image_id |
| 230 | resp, __ = self.head(url) |
David Kranz | 9c3b3b6 | 2014-06-19 16:05:53 -0400 | [diff] [blame] | 231 | self.expected_success(200, resp.status) |
Attila Fazekas | e72b7cd | 2013-03-26 18:34:21 +0100 | [diff] [blame] | 232 | body = self._image_meta_from_headers(resp) |
Ken'ichi Ohmichi | a6ac242 | 2015-01-13 01:09:39 +0000 | [diff] [blame] | 233 | return service_client.ResponseBody(resp, body) |
Attila Fazekas | e72b7cd | 2013-03-26 18:34:21 +0100 | [diff] [blame] | 234 | |
Ken'ichi Ohmichi | 5d41076 | 2015-05-22 01:10:03 +0000 | [diff] [blame] | 235 | def show_image(self, image_id): |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 236 | url = 'v1/images/%s' % image_id |
Attila Fazekas | e72b7cd | 2013-03-26 18:34:21 +0100 | [diff] [blame] | 237 | resp, body = self.get(url) |
David Kranz | 9c3b3b6 | 2014-06-19 16:05:53 -0400 | [diff] [blame] | 238 | self.expected_success(200, resp.status) |
David Kranz | d7e97b4 | 2015-02-16 09:37:31 -0500 | [diff] [blame] | 239 | return service_client.ResponseBodyData(resp, body) |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 240 | |
| 241 | def is_resource_deleted(self, id): |
| 242 | try: |
Attila Fazekas | 9fa2947 | 2014-08-18 09:48:00 +0200 | [diff] [blame] | 243 | self.get_image_meta(id) |
Masayuki Igawa | bfa0760 | 2015-01-20 18:47:17 +0900 | [diff] [blame] | 244 | except lib_exc.NotFound: |
Matthew Treinish | 72ea442 | 2013-02-07 14:42:49 -0500 | [diff] [blame] | 245 | return True |
| 246 | return False |
Matthew Treinish | fa23cf8 | 2013-03-06 14:23:02 -0500 | [diff] [blame] | 247 | |
Matt Riedemann | d2b9651 | 2014-10-13 10:18:16 -0700 | [diff] [blame] | 248 | @property |
| 249 | def resource_type(self): |
| 250 | """Returns the primary type of resource this client works with.""" |
| 251 | return 'image_meta' |
| 252 | |
Ken'ichi Ohmichi | f0df53c | 2015-05-22 01:16:50 +0000 | [diff] [blame] | 253 | def list_image_members(self, image_id): |
Matthew Treinish | fa23cf8 | 2013-03-06 14:23:02 -0500 | [diff] [blame] | 254 | url = 'v1/images/%s/members' % image_id |
| 255 | resp, body = self.get(url) |
David Kranz | 9c3b3b6 | 2014-06-19 16:05:53 -0400 | [diff] [blame] | 256 | self.expected_success(200, resp.status) |
Matthew Treinish | fa23cf8 | 2013-03-06 14:23:02 -0500 | [diff] [blame] | 257 | body = json.loads(body) |
Ken'ichi Ohmichi | a6ac242 | 2015-01-13 01:09:39 +0000 | [diff] [blame] | 258 | return service_client.ResponseBody(resp, body) |
Matthew Treinish | fa23cf8 | 2013-03-06 14:23:02 -0500 | [diff] [blame] | 259 | |
Ken'ichi Ohmichi | bcad2a2 | 2015-05-22 09:37:06 +0000 | [diff] [blame] | 260 | def list_shared_images(self, tenant_id): |
| 261 | """List shared images with the specified tenant""" |
| 262 | url = 'v1/shared-images/%s' % tenant_id |
Matthew Treinish | fa23cf8 | 2013-03-06 14:23:02 -0500 | [diff] [blame] | 263 | resp, body = self.get(url) |
David Kranz | 9c3b3b6 | 2014-06-19 16:05:53 -0400 | [diff] [blame] | 264 | self.expected_success(200, resp.status) |
Matthew Treinish | fa23cf8 | 2013-03-06 14:23:02 -0500 | [diff] [blame] | 265 | body = json.loads(body) |
Ken'ichi Ohmichi | a6ac242 | 2015-01-13 01:09:39 +0000 | [diff] [blame] | 266 | return service_client.ResponseBody(resp, body) |
Matthew Treinish | fa23cf8 | 2013-03-06 14:23:02 -0500 | [diff] [blame] | 267 | |
| 268 | def add_member(self, member_id, image_id, can_share=False): |
| 269 | url = 'v1/images/%s/members/%s' % (image_id, member_id) |
| 270 | body = None |
| 271 | if can_share: |
| 272 | body = json.dumps({'member': {'can_share': True}}) |
Valeriy Ponomaryov | 88686d8 | 2014-02-16 12:24:51 +0200 | [diff] [blame] | 273 | resp, __ = self.put(url, body) |
David Kranz | 9c3b3b6 | 2014-06-19 16:05:53 -0400 | [diff] [blame] | 274 | self.expected_success(204, resp.status) |
Ken'ichi Ohmichi | a6ac242 | 2015-01-13 01:09:39 +0000 | [diff] [blame] | 275 | return service_client.ResponseBody(resp) |
Matthew Treinish | fa23cf8 | 2013-03-06 14:23:02 -0500 | [diff] [blame] | 276 | |
| 277 | def delete_member(self, member_id, image_id): |
| 278 | url = 'v1/images/%s/members/%s' % (image_id, member_id) |
| 279 | resp, __ = self.delete(url) |
David Kranz | 9c3b3b6 | 2014-06-19 16:05:53 -0400 | [diff] [blame] | 280 | self.expected_success(204, resp.status) |
Ken'ichi Ohmichi | a6ac242 | 2015-01-13 01:09:39 +0000 | [diff] [blame] | 281 | return service_client.ResponseBody(resp) |
Matthew Treinish | fa23cf8 | 2013-03-06 14:23:02 -0500 | [diff] [blame] | 282 | |
Attila Fazekas | a8b5fe7 | 2013-08-01 16:59:06 +0200 | [diff] [blame] | 283 | # NOTE(afazekas): just for the wait function |
Attila Fazekas | e72b7cd | 2013-03-26 18:34:21 +0100 | [diff] [blame] | 284 | def _get_image_status(self, image_id): |
David Kranz | 34f1878 | 2015-01-06 13:43:55 -0500 | [diff] [blame] | 285 | meta = self.get_image_meta(image_id) |
Attila Fazekas | e72b7cd | 2013-03-26 18:34:21 +0100 | [diff] [blame] | 286 | status = meta['status'] |
| 287 | return status |
| 288 | |
Attila Fazekas | a8b5fe7 | 2013-08-01 16:59:06 +0200 | [diff] [blame] | 289 | # NOTE(afazkas): Wait reinvented again. It is not in the correct layer |
Attila Fazekas | e72b7cd | 2013-03-26 18:34:21 +0100 | [diff] [blame] | 290 | def wait_for_image_status(self, image_id, status): |
| 291 | """Waits for a Image to reach a given status.""" |
| 292 | start_time = time.time() |
| 293 | old_value = value = self._get_image_status(image_id) |
| 294 | while True: |
| 295 | dtime = time.time() - start_time |
| 296 | time.sleep(self.build_interval) |
| 297 | if value != old_value: |
| 298 | LOG.info('Value transition from "%s" to "%s"' |
| 299 | 'in %d second(s).', old_value, |
| 300 | value, dtime) |
Attila Fazekas | 195f1d4 | 2013-10-24 10:33:16 +0200 | [diff] [blame] | 301 | if value == status: |
Attila Fazekas | e72b7cd | 2013-03-26 18:34:21 +0100 | [diff] [blame] | 302 | return value |
| 303 | |
Attila Fazekas | 195f1d4 | 2013-10-24 10:33:16 +0200 | [diff] [blame] | 304 | if value == 'killed': |
| 305 | raise exceptions.ImageKilledException(image_id=image_id, |
Attila Fazekas | 9dfb907 | 2013-11-13 16:45:36 +0100 | [diff] [blame] | 306 | status=status) |
Attila Fazekas | e72b7cd | 2013-03-26 18:34:21 +0100 | [diff] [blame] | 307 | if dtime > self.build_timeout: |
| 308 | message = ('Time Limit Exceeded! (%ds)' |
| 309 | 'while waiting for %s, ' |
| 310 | 'but we got %s.' % |
| 311 | (self.build_timeout, status, value)) |
Matt Riedemann | 7cec346 | 2014-06-25 12:48:43 -0700 | [diff] [blame] | 312 | caller = misc_utils.find_test_caller() |
| 313 | if caller: |
| 314 | message = '(%s) %s' % (caller, message) |
Attila Fazekas | e72b7cd | 2013-03-26 18:34:21 +0100 | [diff] [blame] | 315 | raise exceptions.TimeoutException(message) |
| 316 | time.sleep(self.build_interval) |
| 317 | old_value = value |
| 318 | value = self._get_image_status(image_id) |