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