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