Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 1 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 2 | # not use this file except in compliance with the License. You may obtain |
| 3 | # a copy of the License at |
| 4 | # |
| 5 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 6 | # |
| 7 | # Unless required by applicable law or agreed to in writing, software |
| 8 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 9 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 10 | # License for the specific language governing permissions and limitations |
| 11 | # under the License. |
| 12 | |
lkuchlan | 52d7b0d | 2016-11-07 20:53:19 +0200 | [diff] [blame] | 13 | import re |
Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 14 | import time |
| 15 | |
Doug Hellmann | 583ce2c | 2015-03-11 14:55:46 +0000 | [diff] [blame] | 16 | from oslo_log import log as logging |
Matthew Treinish | 01472ff | 2015-02-20 17:26:52 -0500 | [diff] [blame] | 17 | |
Ken'ichi Ohmichi | 01151e8 | 2016-06-10 11:19:52 -0700 | [diff] [blame] | 18 | from tempest.common import image as common_image |
Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 19 | from tempest import config |
| 20 | from tempest import exceptions |
zhufl | 88c89b5 | 2016-07-01 18:09:05 +0800 | [diff] [blame] | 21 | from tempest.lib.common.utils import test_utils |
Andrea Frittoli (andreaf) | db9672e | 2016-02-23 14:07:24 -0500 | [diff] [blame] | 22 | from tempest.lib import exceptions as lib_exc |
Ken'ichi Ohmichi | 4172101 | 2016-06-22 10:41:26 -0700 | [diff] [blame] | 23 | from tempest.lib.services.image.v1 import images_client as images_v1_client |
Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 24 | |
Sean Dague | 86bd842 | 2013-12-20 09:56:44 -0500 | [diff] [blame] | 25 | CONF = config.CONF |
Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 26 | LOG = logging.getLogger(__name__) |
| 27 | |
| 28 | |
Matt Riedemann | f748c11 | 2017-02-03 11:33:11 -0500 | [diff] [blame] | 29 | def _get_task_state(body): |
| 30 | return body.get('OS-EXT-STS:task_state', None) |
| 31 | |
| 32 | |
Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 33 | # NOTE(afazekas): This function needs to know a token and a subject. |
Ken'ichi Ohmichi | 39437e2 | 2013-10-06 00:21:38 +0900 | [diff] [blame] | 34 | def wait_for_server_status(client, server_id, status, ready_wait=True, |
Zhi Kun Liu | e540176 | 2013-09-11 20:45:48 +0800 | [diff] [blame] | 35 | extra_timeout=0, raise_on_error=True): |
Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 36 | """Waits for a server to reach a given status.""" |
| 37 | |
Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 38 | # NOTE(afazekas): UNKNOWN status possible on ERROR |
| 39 | # or in a very early stage. |
ghanshyam | 0f82525 | 2015-08-25 16:02:50 +0900 | [diff] [blame] | 40 | body = client.show_server(server_id)['server'] |
Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 41 | old_status = server_status = body['status'] |
| 42 | old_task_state = task_state = _get_task_state(body) |
| 43 | start_time = int(time.time()) |
Ken'ichi Ohmichi | 39437e2 | 2013-10-06 00:21:38 +0900 | [diff] [blame] | 44 | timeout = client.build_timeout + extra_timeout |
Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 45 | while True: |
| 46 | # NOTE(afazekas): Now the BUILD status only reached |
Shane Wang | 111f86c | 2014-02-20 16:40:22 +0800 | [diff] [blame] | 47 | # between the UNKNOWN->ACTIVE transition. |
Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 48 | # TODO(afazekas): enumerate and validate the stable status set |
| 49 | if status == 'BUILD' and server_status != 'UNKNOWN': |
| 50 | return |
| 51 | if server_status == status: |
| 52 | if ready_wait: |
| 53 | if status == 'BUILD': |
| 54 | return |
| 55 | # NOTE(afazekas): The instance is in "ready for action state" |
| 56 | # when no task in progress |
Ken'ichi Ohmichi | 534a05e | 2016-08-03 14:45:15 -0700 | [diff] [blame] | 57 | if task_state is None: |
Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 58 | # without state api extension 3 sec usually enough |
Sean Dague | 86bd842 | 2013-12-20 09:56:44 -0500 | [diff] [blame] | 59 | time.sleep(CONF.compute.ready_wait) |
Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 60 | return |
| 61 | else: |
| 62 | return |
| 63 | |
| 64 | time.sleep(client.build_interval) |
ghanshyam | 0f82525 | 2015-08-25 16:02:50 +0900 | [diff] [blame] | 65 | body = client.show_server(server_id)['server'] |
Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 66 | server_status = body['status'] |
| 67 | task_state = _get_task_state(body) |
| 68 | if (server_status != old_status) or (task_state != old_task_state): |
| 69 | LOG.info('State transition "%s" ==> "%s" after %d second wait', |
| 70 | '/'.join((old_status, str(old_task_state))), |
| 71 | '/'.join((server_status, str(task_state))), |
| 72 | time.time() - start_time) |
Zhi Kun Liu | e540176 | 2013-09-11 20:45:48 +0800 | [diff] [blame] | 73 | if (server_status == 'ERROR') and raise_on_error: |
Attila Fazekas | 0462a7f | 2014-06-20 07:38:06 +0200 | [diff] [blame] | 74 | if 'fault' in body: |
| 75 | raise exceptions.BuildErrorException(body['fault'], |
Matthew Treinish | 1d14c54 | 2014-06-17 20:25:40 -0400 | [diff] [blame] | 76 | server_id=server_id) |
Attila Fazekas | 0462a7f | 2014-06-20 07:38:06 +0200 | [diff] [blame] | 77 | else: |
| 78 | raise exceptions.BuildErrorException(server_id=server_id) |
Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 79 | |
Ken'ichi Ohmichi | 39437e2 | 2013-10-06 00:21:38 +0900 | [diff] [blame] | 80 | timed_out = int(time.time()) - start_time >= timeout |
Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 81 | |
| 82 | if timed_out: |
Matt Riedemann | 629fa7c | 2013-12-11 18:20:56 -0800 | [diff] [blame] | 83 | expected_task_state = 'None' if ready_wait else 'n/a' |
| 84 | message = ('Server %(server_id)s failed to reach %(status)s ' |
| 85 | 'status and task state "%(expected_task_state)s" ' |
| 86 | 'within the required time (%(timeout)s s).' % |
| 87 | {'server_id': server_id, |
| 88 | 'status': status, |
| 89 | 'expected_task_state': expected_task_state, |
| 90 | 'timeout': timeout}) |
Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 91 | message += ' Current status: %s.' % server_status |
Matt Riedemann | 629fa7c | 2013-12-11 18:20:56 -0800 | [diff] [blame] | 92 | message += ' Current task state: %s.' % task_state |
zhufl | 88c89b5 | 2016-07-01 18:09:05 +0800 | [diff] [blame] | 93 | caller = test_utils.find_test_caller() |
Matt Riedemann | 128c23e | 2014-05-02 13:46:39 -0700 | [diff] [blame] | 94 | if caller: |
| 95 | message = '(%s) %s' % (caller, message) |
guo yunxian | ebb15f2 | 2016-11-01 21:03:35 +0800 | [diff] [blame] | 96 | raise lib_exc.TimeoutException(message) |
Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 97 | old_status = server_status |
| 98 | old_task_state = task_state |
Matt Riedemann | c00f326 | 2013-12-14 12:03:55 -0800 | [diff] [blame] | 99 | |
| 100 | |
Ken'ichi Ohmichi | e91a0c6 | 2015-08-13 02:09:16 +0000 | [diff] [blame] | 101 | def wait_for_server_termination(client, server_id, ignore_error=False): |
| 102 | """Waits for server to reach termination.""" |
Matt Riedemann | f748c11 | 2017-02-03 11:33:11 -0500 | [diff] [blame] | 103 | try: |
| 104 | body = client.show_server(server_id)['server'] |
| 105 | except lib_exc.NotFound: |
| 106 | return |
zhufl | f5cff8b | 2019-04-28 15:26:32 +0800 | [diff] [blame] | 107 | old_status = body['status'] |
| 108 | old_task_state = _get_task_state(body) |
Ken'ichi Ohmichi | e91a0c6 | 2015-08-13 02:09:16 +0000 | [diff] [blame] | 109 | start_time = int(time.time()) |
| 110 | while True: |
Matt Riedemann | f748c11 | 2017-02-03 11:33:11 -0500 | [diff] [blame] | 111 | time.sleep(client.build_interval) |
Ken'ichi Ohmichi | e91a0c6 | 2015-08-13 02:09:16 +0000 | [diff] [blame] | 112 | try: |
ghanshyam | 0f82525 | 2015-08-25 16:02:50 +0900 | [diff] [blame] | 113 | body = client.show_server(server_id)['server'] |
Ken'ichi Ohmichi | e91a0c6 | 2015-08-13 02:09:16 +0000 | [diff] [blame] | 114 | except lib_exc.NotFound: |
| 115 | return |
Ken'ichi Ohmichi | e91a0c6 | 2015-08-13 02:09:16 +0000 | [diff] [blame] | 116 | server_status = body['status'] |
Matt Riedemann | f748c11 | 2017-02-03 11:33:11 -0500 | [diff] [blame] | 117 | task_state = _get_task_state(body) |
| 118 | if (server_status != old_status) or (task_state != old_task_state): |
| 119 | LOG.info('State transition "%s" ==> "%s" after %d second wait', |
| 120 | '/'.join((old_status, str(old_task_state))), |
| 121 | '/'.join((server_status, str(task_state))), |
| 122 | time.time() - start_time) |
Ken'ichi Ohmichi | e91a0c6 | 2015-08-13 02:09:16 +0000 | [diff] [blame] | 123 | if server_status == 'ERROR' and not ignore_error: |
zhufl | 0ded98f | 2019-06-13 11:44:24 +0800 | [diff] [blame] | 124 | raise lib_exc.DeleteErrorException( |
| 125 | "Server %s failed to delete and is in ERROR status" % |
| 126 | server_id) |
ericxiett | 5f65bf5 | 2020-09-25 08:42:26 +0000 | [diff] [blame] | 127 | |
ericxiett | d0ef93e | 2019-12-16 08:55:05 +0000 | [diff] [blame] | 128 | if server_status == 'SOFT_DELETED': |
| 129 | # Soft-deleted instances need to be forcibly deleted to |
| 130 | # prevent some test cases from failing. |
| 131 | LOG.debug("Automatically force-deleting soft-deleted server %s", |
| 132 | server_id) |
ericxiett | 5f65bf5 | 2020-09-25 08:42:26 +0000 | [diff] [blame] | 133 | try: |
| 134 | client.force_delete_server(server_id) |
| 135 | except lib_exc.NotFound: |
| 136 | # The instance may have been deleted so ignore |
| 137 | # NotFound exception |
| 138 | return |
Ken'ichi Ohmichi | e91a0c6 | 2015-08-13 02:09:16 +0000 | [diff] [blame] | 139 | |
| 140 | if int(time.time()) - start_time >= client.build_timeout: |
guo yunxian | ebb15f2 | 2016-11-01 21:03:35 +0800 | [diff] [blame] | 141 | raise lib_exc.TimeoutException |
Matt Riedemann | f748c11 | 2017-02-03 11:33:11 -0500 | [diff] [blame] | 142 | old_status = server_status |
| 143 | old_task_state = task_state |
Ken'ichi Ohmichi | e91a0c6 | 2015-08-13 02:09:16 +0000 | [diff] [blame] | 144 | |
| 145 | |
Matt Riedemann | c00f326 | 2013-12-14 12:03:55 -0800 | [diff] [blame] | 146 | def wait_for_image_status(client, image_id, status): |
| 147 | """Waits for an image to reach a given status. |
| 148 | |
Ken'ichi Ohmichi | 5d41076 | 2015-05-22 01:10:03 +0000 | [diff] [blame] | 149 | The client should have a show_image(image_id) method to get the image. |
Matt Riedemann | c00f326 | 2013-12-14 12:03:55 -0800 | [diff] [blame] | 150 | The client should also have build_interval and build_timeout attributes. |
| 151 | """ |
Yaroslav Lobankov | 2fea405 | 2016-04-19 15:05:57 +0300 | [diff] [blame] | 152 | if isinstance(client, images_v1_client.ImagesClient): |
| 153 | # The 'check_image' method is used here because the show_image method |
| 154 | # returns image details plus the image itself which is very expensive. |
| 155 | # The 'check_image' method returns just image details. |
Ken'ichi Ohmichi | 01151e8 | 2016-06-10 11:19:52 -0700 | [diff] [blame] | 156 | def _show_image_v1(image_id): |
| 157 | resp = client.check_image(image_id) |
| 158 | return common_image.get_image_meta_from_headers(resp) |
| 159 | |
| 160 | show_image = _show_image_v1 |
Yaroslav Lobankov | 2fea405 | 2016-04-19 15:05:57 +0300 | [diff] [blame] | 161 | else: |
| 162 | show_image = client.show_image |
Matt Riedemann | c00f326 | 2013-12-14 12:03:55 -0800 | [diff] [blame] | 163 | |
Yaroslav Lobankov | 2fea405 | 2016-04-19 15:05:57 +0300 | [diff] [blame] | 164 | current_status = 'An unknown status' |
| 165 | start = int(time.time()) |
| 166 | while int(time.time()) - start < client.build_timeout: |
| 167 | image = show_image(image_id) |
| 168 | # Compute image client returns response wrapped in 'image' element |
Xiangfei Zhu | a8fd20a | 2016-07-26 21:28:12 -0700 | [diff] [blame] | 169 | # which is not the case with Glance image client. |
ghanshyam | 1756e0b | 2015-08-18 19:19:05 +0900 | [diff] [blame] | 170 | if 'image' in image: |
| 171 | image = image['image'] |
Yaroslav Lobankov | 2fea405 | 2016-04-19 15:05:57 +0300 | [diff] [blame] | 172 | |
| 173 | current_status = image['status'] |
| 174 | if current_status == status: |
| 175 | return |
| 176 | if current_status.lower() == 'killed': |
| 177 | raise exceptions.ImageKilledException(image_id=image_id, |
| 178 | status=status) |
| 179 | if current_status.lower() == 'error': |
Matt Riedemann | c00f326 | 2013-12-14 12:03:55 -0800 | [diff] [blame] | 180 | raise exceptions.AddImageException(image_id=image_id) |
| 181 | |
Yaroslav Lobankov | 2fea405 | 2016-04-19 15:05:57 +0300 | [diff] [blame] | 182 | time.sleep(client.build_interval) |
Matt Riedemann | c00f326 | 2013-12-14 12:03:55 -0800 | [diff] [blame] | 183 | |
Yaroslav Lobankov | 2fea405 | 2016-04-19 15:05:57 +0300 | [diff] [blame] | 184 | message = ('Image %(image_id)s failed to reach %(status)s state ' |
| 185 | '(current state %(current_status)s) within the required ' |
| 186 | 'time (%(timeout)s s).' % {'image_id': image_id, |
| 187 | 'status': status, |
| 188 | 'current_status': current_status, |
| 189 | 'timeout': client.build_timeout}) |
zhufl | 88c89b5 | 2016-07-01 18:09:05 +0800 | [diff] [blame] | 190 | caller = test_utils.find_test_caller() |
Yaroslav Lobankov | 2fea405 | 2016-04-19 15:05:57 +0300 | [diff] [blame] | 191 | if caller: |
| 192 | message = '(%s) %s' % (caller, message) |
guo yunxian | ebb15f2 | 2016-11-01 21:03:35 +0800 | [diff] [blame] | 193 | raise lib_exc.TimeoutException(message) |
Adam Gandelman | 0068261 | 2014-09-02 17:10:36 -0700 | [diff] [blame] | 194 | |
| 195 | |
Ghanshyam Mann | b15b58e | 2021-04-29 19:45:29 -0500 | [diff] [blame] | 196 | def wait_for_image_tasks_status(client, image_id, status): |
| 197 | """Waits for an image tasks to reach a given status.""" |
| 198 | pending_tasks = [] |
| 199 | start = int(time.time()) |
| 200 | while int(time.time()) - start < client.build_timeout: |
| 201 | tasks = client.show_image_tasks(image_id)['tasks'] |
| 202 | |
| 203 | pending_tasks = [task for task in tasks if task['status'] != status] |
| 204 | if not pending_tasks: |
| 205 | return tasks |
| 206 | time.sleep(client.build_interval) |
| 207 | |
| 208 | message = ('Image %(image_id)s tasks: %(pending_tasks)s ' |
| 209 | 'failed to reach %(status)s state within the required ' |
| 210 | 'time (%(timeout)s s).' % {'image_id': image_id, |
| 211 | 'pending_tasks': pending_tasks, |
| 212 | 'status': status, |
| 213 | 'timeout': client.build_timeout}) |
| 214 | caller = test_utils.find_test_caller() |
| 215 | if caller: |
| 216 | message = '(%s) %s' % (caller, message) |
| 217 | raise lib_exc.TimeoutException(message) |
| 218 | |
| 219 | |
Dan Smith | ef8e054 | 2021-02-05 13:05:45 -0800 | [diff] [blame] | 220 | def wait_for_image_imported_to_stores(client, image_id, stores=None): |
PranaliD | 491d63e | 2020-08-18 13:29:21 +0000 | [diff] [blame] | 221 | """Waits for an image to be imported to all requested stores. |
| 222 | |
Dan Smith | ef8e054 | 2021-02-05 13:05:45 -0800 | [diff] [blame] | 223 | Short circuits to fail if the serer reports failure of any store. |
| 224 | If stores is None, just wait for status==active. |
| 225 | |
PranaliD | 491d63e | 2020-08-18 13:29:21 +0000 | [diff] [blame] | 226 | The client should also have build_interval and build_timeout attributes. |
| 227 | """ |
| 228 | |
Dan Smith | ef8e054 | 2021-02-05 13:05:45 -0800 | [diff] [blame] | 229 | exc_cls = lib_exc.TimeoutException |
PranaliD | 491d63e | 2020-08-18 13:29:21 +0000 | [diff] [blame] | 230 | start = int(time.time()) |
| 231 | while int(time.time()) - start < client.build_timeout: |
| 232 | image = client.show_image(image_id) |
Dan Smith | ef8e054 | 2021-02-05 13:05:45 -0800 | [diff] [blame] | 233 | if image['status'] == 'active' and (stores is None or |
| 234 | image['stores'] == stores): |
PranaliD | 491d63e | 2020-08-18 13:29:21 +0000 | [diff] [blame] | 235 | return |
Dan Smith | ef8e054 | 2021-02-05 13:05:45 -0800 | [diff] [blame] | 236 | if image.get('os_glance_failed_import'): |
| 237 | exc_cls = lib_exc.OtherRestClientException |
| 238 | break |
PranaliD | 491d63e | 2020-08-18 13:29:21 +0000 | [diff] [blame] | 239 | |
| 240 | time.sleep(client.build_interval) |
| 241 | |
zhufl | 414ffba | 2020-11-19 16:57:06 +0800 | [diff] [blame] | 242 | message = ('Image %s failed to import on stores: %s' % |
Dan Smith | ef8e054 | 2021-02-05 13:05:45 -0800 | [diff] [blame] | 243 | (image_id, str(image.get('os_glance_failed_import')))) |
PranaliD | 491d63e | 2020-08-18 13:29:21 +0000 | [diff] [blame] | 244 | caller = test_utils.find_test_caller() |
| 245 | if caller: |
| 246 | message = '(%s) %s' % (caller, message) |
Dan Smith | ef8e054 | 2021-02-05 13:05:45 -0800 | [diff] [blame] | 247 | raise exc_cls(message) |
PranaliD | 491d63e | 2020-08-18 13:29:21 +0000 | [diff] [blame] | 248 | |
| 249 | |
Ghanshyam Mann | 4346a82 | 2020-07-29 13:45:04 -0500 | [diff] [blame] | 250 | def wait_for_image_copied_to_stores(client, image_id): |
| 251 | """Waits for an image to be copied on all requested stores. |
| 252 | |
| 253 | The client should also have build_interval and build_timeout attributes. |
| 254 | This return the list of stores where copy is failed. |
| 255 | """ |
| 256 | |
| 257 | start = int(time.time()) |
| 258 | store_left = [] |
| 259 | while int(time.time()) - start < client.build_timeout: |
| 260 | image = client.show_image(image_id) |
| 261 | store_left = image.get('os_glance_importing_to_stores') |
| 262 | # NOTE(danms): If os_glance_importing_to_stores is None, then |
| 263 | # we've raced with the startup of the task and should continue |
| 264 | # to wait. |
| 265 | if store_left is not None and not store_left: |
| 266 | return image['os_glance_failed_import'] |
| 267 | if image['status'].lower() == 'killed': |
| 268 | raise exceptions.ImageKilledException(image_id=image_id, |
| 269 | status=image['status']) |
| 270 | |
| 271 | time.sleep(client.build_interval) |
| 272 | |
zhufl | 414ffba | 2020-11-19 16:57:06 +0800 | [diff] [blame] | 273 | message = ('Image %s failed to finish the copy operation ' |
| 274 | 'on stores: %s' % (image_id, str(store_left))) |
Ghanshyam Mann | 4346a82 | 2020-07-29 13:45:04 -0500 | [diff] [blame] | 275 | caller = test_utils.find_test_caller() |
| 276 | if caller: |
| 277 | message = '(%s) %s' % (caller, message) |
| 278 | raise lib_exc.TimeoutException(message) |
| 279 | |
| 280 | |
Matt Riedemann | b36186b | 2017-12-04 17:54:08 +0000 | [diff] [blame] | 281 | def wait_for_volume_resource_status(client, resource_id, status): |
| 282 | """Waits for a volume resource to reach a given status. |
lkuchlan | 52d7b0d | 2016-11-07 20:53:19 +0200 | [diff] [blame] | 283 | |
| 284 | This function is a common function for volume, snapshot and backup |
| 285 | resources. The function extracts the name of the desired resource from |
| 286 | the client class name of the resource. |
| 287 | """ |
xing-yang | 41ed715 | 2017-05-03 06:52:56 -0400 | [diff] [blame] | 288 | resource_name = re.findall( |
| 289 | r'(volume|group-snapshot|snapshot|backup|group)', |
| 290 | client.resource_type)[-1].replace('-', '_') |
lkuchlan | 52d7b0d | 2016-11-07 20:53:19 +0200 | [diff] [blame] | 291 | show_resource = getattr(client, 'show_' + resource_name) |
| 292 | resource_status = show_resource(resource_id)[resource_name]['status'] |
Ken'ichi Ohmichi | b942be6 | 2015-07-08 08:16:12 +0000 | [diff] [blame] | 293 | start = int(time.time()) |
| 294 | |
Matt Riedemann | b36186b | 2017-12-04 17:54:08 +0000 | [diff] [blame] | 295 | while resource_status != status: |
Ken'ichi Ohmichi | b942be6 | 2015-07-08 08:16:12 +0000 | [diff] [blame] | 296 | time.sleep(client.build_interval) |
lkuchlan | 52d7b0d | 2016-11-07 20:53:19 +0200 | [diff] [blame] | 297 | resource_status = show_resource(resource_id)[ |
| 298 | '{}'.format(resource_name)]['status'] |
Matt Riedemann | b36186b | 2017-12-04 17:54:08 +0000 | [diff] [blame] | 299 | if resource_status == 'error' and resource_status != status: |
lkuchlan | 52d7b0d | 2016-11-07 20:53:19 +0200 | [diff] [blame] | 300 | raise exceptions.VolumeResourceBuildErrorException( |
| 301 | resource_name=resource_name, resource_id=resource_id) |
| 302 | if resource_name == 'volume' and resource_status == 'error_restoring': |
| 303 | raise exceptions.VolumeRestoreErrorException(volume_id=resource_id) |
zhufl | 0ea2c01 | 2019-06-03 15:37:13 +0800 | [diff] [blame] | 304 | if resource_status == 'error_extending' and resource_status != status: |
| 305 | raise exceptions.VolumeExtendErrorException(volume_id=resource_id) |
Ken'ichi Ohmichi | b942be6 | 2015-07-08 08:16:12 +0000 | [diff] [blame] | 306 | |
| 307 | if int(time.time()) - start >= client.build_timeout: |
lkuchlan | 52d7b0d | 2016-11-07 20:53:19 +0200 | [diff] [blame] | 308 | message = ('%s %s failed to reach %s status (current %s) ' |
Ken'ichi Ohmichi | b942be6 | 2015-07-08 08:16:12 +0000 | [diff] [blame] | 309 | 'within the required time (%s s).' % |
Matt Riedemann | b36186b | 2017-12-04 17:54:08 +0000 | [diff] [blame] | 310 | (resource_name, resource_id, status, resource_status, |
Ken'ichi Ohmichi | b942be6 | 2015-07-08 08:16:12 +0000 | [diff] [blame] | 311 | client.build_timeout)) |
guo yunxian | ebb15f2 | 2016-11-01 21:03:35 +0800 | [diff] [blame] | 312 | raise lib_exc.TimeoutException(message) |
zhufl | 019ad73 | 2017-08-28 13:51:22 +0800 | [diff] [blame] | 313 | LOG.info('%s %s reached %s after waiting for %f seconds', |
Matt Riedemann | b36186b | 2017-12-04 17:54:08 +0000 | [diff] [blame] | 314 | resource_name, resource_id, status, time.time() - start) |
Ken'ichi Ohmichi | b942be6 | 2015-07-08 08:16:12 +0000 | [diff] [blame] | 315 | |
| 316 | |
Peter Penchev | 5c243a9 | 2020-09-06 02:26:03 +0300 | [diff] [blame] | 317 | def wait_for_volume_attachment_create(client, volume_id, server_id): |
| 318 | """Waits for a volume attachment to be created at a given volume.""" |
| 319 | start = int(time.time()) |
| 320 | while True: |
| 321 | attachments = client.show_volume(volume_id)['volume']['attachments'] |
| 322 | found = [a for a in attachments if a['server_id'] == server_id] |
| 323 | if found: |
| 324 | LOG.info('Attachment %s created for volume %s to server %s after ' |
| 325 | 'waiting for %f seconds', found[0]['attachment_id'], |
| 326 | volume_id, server_id, time.time() - start) |
| 327 | return found[0] |
| 328 | time.sleep(client.build_interval) |
| 329 | if int(time.time()) - start >= client.build_timeout: |
| 330 | message = ('Failed to attach volume %s to server %s ' |
| 331 | 'within the required time (%s s).' % |
| 332 | (volume_id, server_id, client.build_timeout)) |
| 333 | raise lib_exc.TimeoutException(message) |
| 334 | |
| 335 | |
Lee Yarwood | c1b2a4a | 2020-01-08 17:02:49 +0000 | [diff] [blame] | 336 | def wait_for_volume_attachment_remove(client, volume_id, attachment_id): |
| 337 | """Waits for a volume attachment to be removed from a given volume.""" |
| 338 | start = int(time.time()) |
| 339 | attachments = client.show_volume(volume_id)['volume']['attachments'] |
| 340 | while any(attachment_id == a['attachment_id'] for a in attachments): |
| 341 | time.sleep(client.build_interval) |
| 342 | if int(time.time()) - start >= client.build_timeout: |
zhufl | 747300b | 2020-04-14 14:23:47 +0800 | [diff] [blame] | 343 | message = ('Failed to remove attachment %s from volume %s ' |
Lee Yarwood | c1b2a4a | 2020-01-08 17:02:49 +0000 | [diff] [blame] | 344 | 'within the required time (%s s).' % |
| 345 | (attachment_id, volume_id, client.build_timeout)) |
| 346 | raise lib_exc.TimeoutException(message) |
| 347 | attachments = client.show_volume(volume_id)['volume']['attachments'] |
| 348 | LOG.info('Attachment %s removed from volume %s after waiting for %f ' |
| 349 | 'seconds', attachment_id, volume_id, time.time() - start) |
| 350 | |
| 351 | |
Balazs Gibizer | 2e515fe | 2020-12-07 15:10:11 +0100 | [diff] [blame] | 352 | def wait_for_volume_attachment_remove_from_server( |
| 353 | client, server_id, volume_id): |
| 354 | """Waits for a volume to be removed from a given server. |
| 355 | |
| 356 | This waiter checks the compute API if the volume attachment is removed. |
| 357 | """ |
| 358 | start = int(time.time()) |
| 359 | volumes = client.list_volume_attachments(server_id)['volumeAttachments'] |
| 360 | |
| 361 | while any(volume for volume in volumes if volume['volumeId'] == volume_id): |
| 362 | time.sleep(client.build_interval) |
| 363 | |
| 364 | timed_out = int(time.time()) - start >= client.build_timeout |
| 365 | if timed_out: |
| 366 | message = ('Volume %s failed to detach from server %s within ' |
| 367 | 'the required time (%s s) from the compute API ' |
| 368 | 'perspective' % |
| 369 | (volume_id, server_id, client.build_timeout)) |
| 370 | raise lib_exc.TimeoutException(message) |
| 371 | |
| 372 | volumes = client.list_volume_attachments(server_id)[ |
| 373 | 'volumeAttachments'] |
| 374 | |
| 375 | return volumes |
| 376 | |
| 377 | |
Lee Yarwood | e559740 | 2019-02-15 20:17:00 +0000 | [diff] [blame] | 378 | def wait_for_volume_migration(client, volume_id, new_host): |
| 379 | """Waits for a Volume to move to a new host.""" |
| 380 | body = client.show_volume(volume_id)['volume'] |
| 381 | host = body['os-vol-host-attr:host'] |
| 382 | migration_status = body['migration_status'] |
| 383 | start = int(time.time()) |
| 384 | |
| 385 | # new_host is hostname@backend while current_host is hostname@backend#type |
| 386 | while migration_status != 'success' or new_host not in host: |
| 387 | time.sleep(client.build_interval) |
| 388 | body = client.show_volume(volume_id)['volume'] |
| 389 | host = body['os-vol-host-attr:host'] |
| 390 | migration_status = body['migration_status'] |
| 391 | |
| 392 | if migration_status == 'error': |
| 393 | message = ('volume %s failed to migrate.' % (volume_id)) |
| 394 | raise lib_exc.TempestException(message) |
| 395 | |
| 396 | if int(time.time()) - start >= client.build_timeout: |
| 397 | message = ('Volume %s failed to migrate to %s (current %s) ' |
| 398 | 'within the required time (%s s).' % |
| 399 | (volume_id, new_host, host, client.build_timeout)) |
| 400 | raise lib_exc.TimeoutException(message) |
| 401 | |
| 402 | |
scottda | 61f68ac | 2016-06-07 12:07:55 -0600 | [diff] [blame] | 403 | def wait_for_volume_retype(client, volume_id, new_volume_type): |
| 404 | """Waits for a Volume to have a new volume type.""" |
| 405 | body = client.show_volume(volume_id)['volume'] |
| 406 | current_volume_type = body['volume_type'] |
| 407 | start = int(time.time()) |
| 408 | |
| 409 | while current_volume_type != new_volume_type: |
| 410 | time.sleep(client.build_interval) |
| 411 | body = client.show_volume(volume_id)['volume'] |
| 412 | current_volume_type = body['volume_type'] |
| 413 | |
| 414 | if int(time.time()) - start >= client.build_timeout: |
| 415 | message = ('Volume %s failed to reach %s volume type (current %s) ' |
| 416 | 'within the required time (%s s).' % |
| 417 | (volume_id, new_volume_type, current_volume_type, |
| 418 | client.build_timeout)) |
Matt Riedemann | 74eb3b5 | 2017-02-14 11:34:30 -0500 | [diff] [blame] | 419 | raise lib_exc.TimeoutException(message) |
scottda | 61f68ac | 2016-06-07 12:07:55 -0600 | [diff] [blame] | 420 | |
| 421 | |
lkuchlan | ec1ba4f | 2016-09-06 10:28:18 +0300 | [diff] [blame] | 422 | def wait_for_qos_operations(client, qos_id, operation, args=None): |
| 423 | """Waits for a qos operations to be completed. |
| 424 | |
| 425 | NOTE : operation value is required for wait_for_qos_operations() |
| 426 | operation = 'qos-key' / 'disassociate' / 'disassociate-all' |
| 427 | args = keys[] when operation = 'qos-key' |
| 428 | args = volume-type-id disassociated when operation = 'disassociate' |
| 429 | args = None when operation = 'disassociate-all' |
| 430 | """ |
| 431 | start_time = int(time.time()) |
| 432 | while True: |
| 433 | if operation == 'qos-key-unset': |
| 434 | body = client.show_qos(qos_id)['qos_specs'] |
| 435 | if not any(key in body['specs'] for key in args): |
| 436 | return |
| 437 | elif operation == 'disassociate': |
| 438 | body = client.show_association_qos(qos_id)['qos_associations'] |
| 439 | if not any(args in body[i]['id'] for i in range(0, len(body))): |
| 440 | return |
| 441 | elif operation == 'disassociate-all': |
| 442 | body = client.show_association_qos(qos_id)['qos_associations'] |
| 443 | if not body: |
| 444 | return |
| 445 | else: |
| 446 | msg = (" operation value is either not defined or incorrect.") |
| 447 | raise lib_exc.UnprocessableEntity(msg) |
| 448 | |
| 449 | if int(time.time()) - start_time >= client.build_timeout: |
guo yunxian | ebb15f2 | 2016-11-01 21:03:35 +0800 | [diff] [blame] | 450 | raise lib_exc.TimeoutException |
lkuchlan | ec1ba4f | 2016-09-06 10:28:18 +0300 | [diff] [blame] | 451 | time.sleep(client.build_interval) |
zhufl | 7b63833 | 2016-11-14 10:23:30 +0800 | [diff] [blame] | 452 | |
| 453 | |
Matt Riedemann | 2ea48f0 | 2017-04-03 11:36:19 -0400 | [diff] [blame] | 454 | def wait_for_interface_status(client, server_id, port_id, status): |
zhufl | 7b63833 | 2016-11-14 10:23:30 +0800 | [diff] [blame] | 455 | """Waits for an interface to reach a given status.""" |
Matt Riedemann | 2ea48f0 | 2017-04-03 11:36:19 -0400 | [diff] [blame] | 456 | body = (client.show_interface(server_id, port_id) |
zhufl | 7b63833 | 2016-11-14 10:23:30 +0800 | [diff] [blame] | 457 | ['interfaceAttachment']) |
| 458 | interface_status = body['port_state'] |
| 459 | start = int(time.time()) |
| 460 | |
| 461 | while(interface_status != status): |
| 462 | time.sleep(client.build_interval) |
Matt Riedemann | 2ea48f0 | 2017-04-03 11:36:19 -0400 | [diff] [blame] | 463 | body = (client.show_interface(server_id, port_id) |
zhufl | 7b63833 | 2016-11-14 10:23:30 +0800 | [diff] [blame] | 464 | ['interfaceAttachment']) |
| 465 | interface_status = body['port_state'] |
| 466 | |
| 467 | timed_out = int(time.time()) - start >= client.build_timeout |
| 468 | |
| 469 | if interface_status != status and timed_out: |
| 470 | message = ('Interface %s failed to reach %s status ' |
| 471 | '(current %s) within the required time (%s s).' % |
| 472 | (port_id, status, interface_status, |
| 473 | client.build_timeout)) |
| 474 | raise lib_exc.TimeoutException(message) |
| 475 | |
| 476 | return body |
Artom Lifshitz | 3306d42 | 2018-03-22 12:20:54 -0400 | [diff] [blame] | 477 | |
| 478 | |
| 479 | def wait_for_interface_detach(client, server_id, port_id): |
| 480 | """Waits for an interface to be detached from a server.""" |
| 481 | body = client.list_interfaces(server_id)['interfaceAttachments'] |
| 482 | ports = [iface['port_id'] for iface in body] |
| 483 | start = int(time.time()) |
| 484 | |
| 485 | while port_id in ports: |
| 486 | time.sleep(client.build_interval) |
| 487 | body = client.list_interfaces(server_id)['interfaceAttachments'] |
| 488 | ports = [iface['port_id'] for iface in body] |
| 489 | if port_id not in ports: |
| 490 | return body |
| 491 | |
| 492 | timed_out = int(time.time()) - start >= client.build_timeout |
| 493 | if timed_out: |
| 494 | message = ('Interface %s failed to detach from server %s within ' |
| 495 | 'the required time (%s s)' % (port_id, server_id, |
| 496 | client.build_timeout)) |
| 497 | raise lib_exc.TimeoutException(message) |
Slawek Kaplonski | e3405ba | 2020-11-09 17:24:13 +0100 | [diff] [blame] | 498 | |
| 499 | |
| 500 | def wait_for_guest_os_boot(client, server_id): |
| 501 | start_time = int(time.time()) |
| 502 | while True: |
| 503 | console_output = client.get_console_output(server_id)['output'] |
| 504 | for line in console_output.split('\n'): |
| 505 | if 'login:' in line.lower(): |
| 506 | return |
| 507 | if int(time.time()) - start_time >= client.build_timeout: |
| 508 | LOG.info("Guest OS on server %s probably isn't ready or its " |
| 509 | "console log can't be parsed properly. If guest OS " |
| 510 | "isn't ready, that may cause problems with SSH to " |
| 511 | "the server.", |
| 512 | server_id) |
| 513 | return |
| 514 | time.sleep(client.build_interval) |