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 | |
Lee Yarwood | 0b4bc3d | 2021-11-11 17:45:25 +0000 | [diff] [blame] | 13 | import os |
lkuchlan | 52d7b0d | 2016-11-07 20:53:19 +0200 | [diff] [blame] | 14 | import re |
Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 15 | import time |
| 16 | |
Doug Hellmann | 583ce2c | 2015-03-11 14:55:46 +0000 | [diff] [blame] | 17 | from oslo_log import log as logging |
Matthew Treinish | 01472ff | 2015-02-20 17:26:52 -0500 | [diff] [blame] | 18 | |
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 |
Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 23 | |
Sean Dague | 86bd842 | 2013-12-20 09:56:44 -0500 | [diff] [blame] | 24 | CONF = config.CONF |
Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 25 | LOG = logging.getLogger(__name__) |
| 26 | |
| 27 | |
Matt Riedemann | f748c11 | 2017-02-03 11:33:11 -0500 | [diff] [blame] | 28 | def _get_task_state(body): |
| 29 | return body.get('OS-EXT-STS:task_state', None) |
| 30 | |
| 31 | |
Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 32 | # 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] | 33 | def wait_for_server_status(client, server_id, status, ready_wait=True, |
Artom Lifshitz | da48e4e | 2021-11-22 15:59:15 -0500 | [diff] [blame] | 34 | extra_timeout=0, raise_on_error=True, |
| 35 | request_id=None): |
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': |
Marian Krcmarik | ca5ddb4 | 2022-08-29 17:01:01 +0200 | [diff] [blame] | 50 | return body |
Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 51 | if server_status == status: |
| 52 | if ready_wait: |
| 53 | if status == 'BUILD': |
Marian Krcmarik | ca5ddb4 | 2022-08-29 17:01:01 +0200 | [diff] [blame] | 54 | return body |
Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 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) |
Marian Krcmarik | ca5ddb4 | 2022-08-29 17:01:01 +0200 | [diff] [blame] | 60 | return body |
Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 61 | else: |
Marian Krcmarik | ca5ddb4 | 2022-08-29 17:01:01 +0200 | [diff] [blame] | 62 | return body |
Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 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: |
Artom Lifshitz | da48e4e | 2021-11-22 15:59:15 -0500 | [diff] [blame] | 74 | details = '' |
Attila Fazekas | 0462a7f | 2014-06-20 07:38:06 +0200 | [diff] [blame] | 75 | if 'fault' in body: |
Artom Lifshitz | da48e4e | 2021-11-22 15:59:15 -0500 | [diff] [blame] | 76 | details += 'Fault: %s.' % body['fault'] |
| 77 | if request_id: |
Martin Kopec | 3f844b2 | 2023-05-24 17:00:25 +0200 | [diff] [blame] | 78 | details += ' Request ID of server operation performed before' |
| 79 | details += ' checking the server status %s.' % request_id |
Artom Lifshitz | da48e4e | 2021-11-22 15:59:15 -0500 | [diff] [blame] | 80 | raise exceptions.BuildErrorException(details, server_id=server_id) |
Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 81 | |
Ken'ichi Ohmichi | 39437e2 | 2013-10-06 00:21:38 +0900 | [diff] [blame] | 82 | timed_out = int(time.time()) - start_time >= timeout |
Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 83 | |
| 84 | if timed_out: |
Matt Riedemann | 629fa7c | 2013-12-11 18:20:56 -0800 | [diff] [blame] | 85 | expected_task_state = 'None' if ready_wait else 'n/a' |
| 86 | message = ('Server %(server_id)s failed to reach %(status)s ' |
| 87 | 'status and task state "%(expected_task_state)s" ' |
| 88 | 'within the required time (%(timeout)s s).' % |
| 89 | {'server_id': server_id, |
| 90 | 'status': status, |
| 91 | 'expected_task_state': expected_task_state, |
| 92 | 'timeout': timeout}) |
Artom Lifshitz | da48e4e | 2021-11-22 15:59:15 -0500 | [diff] [blame] | 93 | if request_id: |
Martin Kopec | 3f844b2 | 2023-05-24 17:00:25 +0200 | [diff] [blame] | 94 | message += ' Request ID of server operation performed before' |
| 95 | message += ' checking the server status %s.' % request_id |
Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 96 | message += ' Current status: %s.' % server_status |
Matt Riedemann | 629fa7c | 2013-12-11 18:20:56 -0800 | [diff] [blame] | 97 | message += ' Current task state: %s.' % task_state |
zhufl | 88c89b5 | 2016-07-01 18:09:05 +0800 | [diff] [blame] | 98 | caller = test_utils.find_test_caller() |
Matt Riedemann | 128c23e | 2014-05-02 13:46:39 -0700 | [diff] [blame] | 99 | if caller: |
| 100 | message = '(%s) %s' % (caller, message) |
guo yunxian | ebb15f2 | 2016-11-01 21:03:35 +0800 | [diff] [blame] | 101 | raise lib_exc.TimeoutException(message) |
Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 102 | old_status = server_status |
| 103 | old_task_state = task_state |
Matt Riedemann | c00f326 | 2013-12-14 12:03:55 -0800 | [diff] [blame] | 104 | |
| 105 | |
Ken'ichi Ohmichi | e91a0c6 | 2015-08-13 02:09:16 +0000 | [diff] [blame] | 106 | def wait_for_server_termination(client, server_id, ignore_error=False): |
| 107 | """Waits for server to reach termination.""" |
Matt Riedemann | f748c11 | 2017-02-03 11:33:11 -0500 | [diff] [blame] | 108 | try: |
| 109 | body = client.show_server(server_id)['server'] |
| 110 | except lib_exc.NotFound: |
| 111 | return |
zhufl | f5cff8b | 2019-04-28 15:26:32 +0800 | [diff] [blame] | 112 | old_status = body['status'] |
| 113 | old_task_state = _get_task_state(body) |
Ken'ichi Ohmichi | e91a0c6 | 2015-08-13 02:09:16 +0000 | [diff] [blame] | 114 | start_time = int(time.time()) |
| 115 | while True: |
Matt Riedemann | f748c11 | 2017-02-03 11:33:11 -0500 | [diff] [blame] | 116 | time.sleep(client.build_interval) |
Ken'ichi Ohmichi | e91a0c6 | 2015-08-13 02:09:16 +0000 | [diff] [blame] | 117 | try: |
ghanshyam | 0f82525 | 2015-08-25 16:02:50 +0900 | [diff] [blame] | 118 | body = client.show_server(server_id)['server'] |
Ken'ichi Ohmichi | e91a0c6 | 2015-08-13 02:09:16 +0000 | [diff] [blame] | 119 | except lib_exc.NotFound: |
| 120 | return |
Ken'ichi Ohmichi | e91a0c6 | 2015-08-13 02:09:16 +0000 | [diff] [blame] | 121 | server_status = body['status'] |
Matt Riedemann | f748c11 | 2017-02-03 11:33:11 -0500 | [diff] [blame] | 122 | task_state = _get_task_state(body) |
| 123 | if (server_status != old_status) or (task_state != old_task_state): |
| 124 | LOG.info('State transition "%s" ==> "%s" after %d second wait', |
| 125 | '/'.join((old_status, str(old_task_state))), |
| 126 | '/'.join((server_status, str(task_state))), |
| 127 | time.time() - start_time) |
Ken'ichi Ohmichi | e91a0c6 | 2015-08-13 02:09:16 +0000 | [diff] [blame] | 128 | if server_status == 'ERROR' and not ignore_error: |
zhufl | 0ded98f | 2019-06-13 11:44:24 +0800 | [diff] [blame] | 129 | raise lib_exc.DeleteErrorException( |
| 130 | "Server %s failed to delete and is in ERROR status" % |
| 131 | server_id) |
ericxiett | 5f65bf5 | 2020-09-25 08:42:26 +0000 | [diff] [blame] | 132 | |
ericxiett | d0ef93e | 2019-12-16 08:55:05 +0000 | [diff] [blame] | 133 | if server_status == 'SOFT_DELETED': |
| 134 | # Soft-deleted instances need to be forcibly deleted to |
| 135 | # prevent some test cases from failing. |
| 136 | LOG.debug("Automatically force-deleting soft-deleted server %s", |
| 137 | server_id) |
ericxiett | 5f65bf5 | 2020-09-25 08:42:26 +0000 | [diff] [blame] | 138 | try: |
| 139 | client.force_delete_server(server_id) |
| 140 | except lib_exc.NotFound: |
| 141 | # The instance may have been deleted so ignore |
| 142 | # NotFound exception |
| 143 | return |
Ken'ichi Ohmichi | e91a0c6 | 2015-08-13 02:09:16 +0000 | [diff] [blame] | 144 | |
| 145 | if int(time.time()) - start_time >= client.build_timeout: |
guo yunxian | ebb15f2 | 2016-11-01 21:03:35 +0800 | [diff] [blame] | 146 | raise lib_exc.TimeoutException |
Matt Riedemann | f748c11 | 2017-02-03 11:33:11 -0500 | [diff] [blame] | 147 | old_status = server_status |
| 148 | old_task_state = task_state |
Ken'ichi Ohmichi | e91a0c6 | 2015-08-13 02:09:16 +0000 | [diff] [blame] | 149 | |
| 150 | |
Matt Riedemann | c00f326 | 2013-12-14 12:03:55 -0800 | [diff] [blame] | 151 | def wait_for_image_status(client, image_id, status): |
| 152 | """Waits for an image to reach a given status. |
| 153 | |
Ken'ichi Ohmichi | 5d41076 | 2015-05-22 01:10:03 +0000 | [diff] [blame] | 154 | 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] | 155 | The client should also have build_interval and build_timeout attributes. |
| 156 | """ |
Ghanshyam Mann | 3562cd0 | 2023-08-05 17:22:03 -0700 | [diff] [blame] | 157 | show_image = client.show_image |
Matt Riedemann | c00f326 | 2013-12-14 12:03:55 -0800 | [diff] [blame] | 158 | |
Yaroslav Lobankov | 2fea405 | 2016-04-19 15:05:57 +0300 | [diff] [blame] | 159 | current_status = 'An unknown status' |
| 160 | start = int(time.time()) |
| 161 | while int(time.time()) - start < client.build_timeout: |
| 162 | image = show_image(image_id) |
| 163 | # Compute image client returns response wrapped in 'image' element |
Xiangfei Zhu | a8fd20a | 2016-07-26 21:28:12 -0700 | [diff] [blame] | 164 | # which is not the case with Glance image client. |
ghanshyam | 1756e0b | 2015-08-18 19:19:05 +0900 | [diff] [blame] | 165 | if 'image' in image: |
| 166 | image = image['image'] |
Yaroslav Lobankov | 2fea405 | 2016-04-19 15:05:57 +0300 | [diff] [blame] | 167 | |
| 168 | current_status = image['status'] |
| 169 | if current_status == status: |
| 170 | return |
| 171 | if current_status.lower() == 'killed': |
| 172 | raise exceptions.ImageKilledException(image_id=image_id, |
| 173 | status=status) |
| 174 | if current_status.lower() == 'error': |
Matt Riedemann | c00f326 | 2013-12-14 12:03:55 -0800 | [diff] [blame] | 175 | raise exceptions.AddImageException(image_id=image_id) |
| 176 | |
Yaroslav Lobankov | 2fea405 | 2016-04-19 15:05:57 +0300 | [diff] [blame] | 177 | time.sleep(client.build_interval) |
Matt Riedemann | c00f326 | 2013-12-14 12:03:55 -0800 | [diff] [blame] | 178 | |
Yaroslav Lobankov | 2fea405 | 2016-04-19 15:05:57 +0300 | [diff] [blame] | 179 | message = ('Image %(image_id)s failed to reach %(status)s state ' |
| 180 | '(current state %(current_status)s) within the required ' |
| 181 | 'time (%(timeout)s s).' % {'image_id': image_id, |
| 182 | 'status': status, |
| 183 | 'current_status': current_status, |
| 184 | 'timeout': client.build_timeout}) |
zhufl | 88c89b5 | 2016-07-01 18:09:05 +0800 | [diff] [blame] | 185 | caller = test_utils.find_test_caller() |
Yaroslav Lobankov | 2fea405 | 2016-04-19 15:05:57 +0300 | [diff] [blame] | 186 | if caller: |
| 187 | message = '(%s) %s' % (caller, message) |
guo yunxian | ebb15f2 | 2016-11-01 21:03:35 +0800 | [diff] [blame] | 188 | raise lib_exc.TimeoutException(message) |
Adam Gandelman | 0068261 | 2014-09-02 17:10:36 -0700 | [diff] [blame] | 189 | |
| 190 | |
Ghanshyam Mann | b15b58e | 2021-04-29 19:45:29 -0500 | [diff] [blame] | 191 | def wait_for_image_tasks_status(client, image_id, status): |
| 192 | """Waits for an image tasks to reach a given status.""" |
| 193 | pending_tasks = [] |
| 194 | start = int(time.time()) |
| 195 | while int(time.time()) - start < client.build_timeout: |
| 196 | tasks = client.show_image_tasks(image_id)['tasks'] |
| 197 | |
| 198 | pending_tasks = [task for task in tasks if task['status'] != status] |
| 199 | if not pending_tasks: |
| 200 | return tasks |
| 201 | time.sleep(client.build_interval) |
| 202 | |
| 203 | message = ('Image %(image_id)s tasks: %(pending_tasks)s ' |
| 204 | 'failed to reach %(status)s state within the required ' |
| 205 | 'time (%(timeout)s s).' % {'image_id': image_id, |
| 206 | 'pending_tasks': pending_tasks, |
| 207 | 'status': status, |
| 208 | 'timeout': client.build_timeout}) |
| 209 | caller = test_utils.find_test_caller() |
| 210 | if caller: |
| 211 | message = '(%s) %s' % (caller, message) |
| 212 | raise lib_exc.TimeoutException(message) |
| 213 | |
| 214 | |
msava | 88660d4 | 2023-07-18 11:27:04 +0300 | [diff] [blame] | 215 | def wait_for_tasks_status(client, task_id, status): |
| 216 | start = int(time.time()) |
| 217 | while int(time.time()) - start < client.build_timeout: |
| 218 | task = client.show_tasks(task_id) |
| 219 | if task['status'] == status: |
| 220 | return task |
| 221 | time.sleep(client.build_interval) |
| 222 | message = ('Task %(task_id)s tasks: ' |
| 223 | 'failed to reach %(status)s state within the required ' |
| 224 | 'time (%(timeout)s s).' % {'task_id': task_id, |
| 225 | 'status': status, |
| 226 | 'timeout': client.build_timeout}) |
| 227 | caller = test_utils.find_test_caller() |
| 228 | if caller: |
| 229 | message = '(%s) %s' % (caller, message) |
| 230 | raise lib_exc.TimeoutException(message) |
| 231 | |
| 232 | |
Dan Smith | ef8e054 | 2021-02-05 13:05:45 -0800 | [diff] [blame] | 233 | def wait_for_image_imported_to_stores(client, image_id, stores=None): |
PranaliD | 491d63e | 2020-08-18 13:29:21 +0000 | [diff] [blame] | 234 | """Waits for an image to be imported to all requested stores. |
| 235 | |
Dan Smith | ef8e054 | 2021-02-05 13:05:45 -0800 | [diff] [blame] | 236 | Short circuits to fail if the serer reports failure of any store. |
| 237 | If stores is None, just wait for status==active. |
| 238 | |
PranaliD | 491d63e | 2020-08-18 13:29:21 +0000 | [diff] [blame] | 239 | The client should also have build_interval and build_timeout attributes. |
| 240 | """ |
| 241 | |
Dan Smith | ef8e054 | 2021-02-05 13:05:45 -0800 | [diff] [blame] | 242 | exc_cls = lib_exc.TimeoutException |
PranaliD | 491d63e | 2020-08-18 13:29:21 +0000 | [diff] [blame] | 243 | start = int(time.time()) |
Dan Smith | 466f706 | 2022-10-10 15:25:39 -0700 | [diff] [blame] | 244 | |
| 245 | # NOTE(danms): Don't wait for stores that are read-only as those |
| 246 | # will never complete |
| 247 | try: |
| 248 | store_info = client.info_stores()['stores'] |
| 249 | stores = ','.join(sorted([ |
| 250 | store['id'] for store in store_info |
| 251 | if store.get('read-only') != 'true' and |
| 252 | (not stores or store['id'] in stores.split(','))])) |
| 253 | except lib_exc.NotFound: |
| 254 | # If multi-store is not enabled, then we can not resolve which |
| 255 | # ones are read-only, and stores must have been passed as None |
| 256 | # anyway for us to succeed. If not, then we should raise right |
| 257 | # now and avoid waiting since we will never see the stores |
| 258 | # appear. |
| 259 | if stores is not None: |
| 260 | raise lib_exc.TimeoutException( |
| 261 | 'Image service has no store support; ' |
| 262 | 'cowardly refusing to wait for them.') |
| 263 | |
PranaliD | 491d63e | 2020-08-18 13:29:21 +0000 | [diff] [blame] | 264 | while int(time.time()) - start < client.build_timeout: |
| 265 | image = client.show_image(image_id) |
Dan Smith | ef8e054 | 2021-02-05 13:05:45 -0800 | [diff] [blame] | 266 | if image['status'] == 'active' and (stores is None or |
| 267 | image['stores'] == stores): |
PranaliD | 491d63e | 2020-08-18 13:29:21 +0000 | [diff] [blame] | 268 | return |
Dan Smith | ef8e054 | 2021-02-05 13:05:45 -0800 | [diff] [blame] | 269 | if image.get('os_glance_failed_import'): |
| 270 | exc_cls = lib_exc.OtherRestClientException |
| 271 | break |
PranaliD | 491d63e | 2020-08-18 13:29:21 +0000 | [diff] [blame] | 272 | |
| 273 | time.sleep(client.build_interval) |
| 274 | |
zhufl | 414ffba | 2020-11-19 16:57:06 +0800 | [diff] [blame] | 275 | message = ('Image %s failed to import on stores: %s' % |
Dan Smith | ef8e054 | 2021-02-05 13:05:45 -0800 | [diff] [blame] | 276 | (image_id, str(image.get('os_glance_failed_import')))) |
PranaliD | 491d63e | 2020-08-18 13:29:21 +0000 | [diff] [blame] | 277 | caller = test_utils.find_test_caller() |
| 278 | if caller: |
| 279 | message = '(%s) %s' % (caller, message) |
Dan Smith | ef8e054 | 2021-02-05 13:05:45 -0800 | [diff] [blame] | 280 | raise exc_cls(message) |
PranaliD | 491d63e | 2020-08-18 13:29:21 +0000 | [diff] [blame] | 281 | |
| 282 | |
Ghanshyam Mann | 4346a82 | 2020-07-29 13:45:04 -0500 | [diff] [blame] | 283 | def wait_for_image_copied_to_stores(client, image_id): |
| 284 | """Waits for an image to be copied on all requested stores. |
| 285 | |
| 286 | The client should also have build_interval and build_timeout attributes. |
| 287 | This return the list of stores where copy is failed. |
| 288 | """ |
| 289 | |
| 290 | start = int(time.time()) |
| 291 | store_left = [] |
| 292 | while int(time.time()) - start < client.build_timeout: |
| 293 | image = client.show_image(image_id) |
| 294 | store_left = image.get('os_glance_importing_to_stores') |
| 295 | # NOTE(danms): If os_glance_importing_to_stores is None, then |
| 296 | # we've raced with the startup of the task and should continue |
| 297 | # to wait. |
| 298 | if store_left is not None and not store_left: |
| 299 | return image['os_glance_failed_import'] |
| 300 | if image['status'].lower() == 'killed': |
| 301 | raise exceptions.ImageKilledException(image_id=image_id, |
| 302 | status=image['status']) |
| 303 | |
| 304 | time.sleep(client.build_interval) |
| 305 | |
zhufl | 414ffba | 2020-11-19 16:57:06 +0800 | [diff] [blame] | 306 | message = ('Image %s failed to finish the copy operation ' |
| 307 | 'on stores: %s' % (image_id, str(store_left))) |
Ghanshyam Mann | 4346a82 | 2020-07-29 13:45:04 -0500 | [diff] [blame] | 308 | caller = test_utils.find_test_caller() |
| 309 | if caller: |
| 310 | message = '(%s) %s' % (caller, message) |
| 311 | raise lib_exc.TimeoutException(message) |
| 312 | |
| 313 | |
Maxim Sava | bd9cbd3 | 2023-10-17 13:13:33 +0300 | [diff] [blame^] | 314 | def wait_for_image_deleted_from_store(client, image, available_stores, |
| 315 | image_store_deleted): |
| 316 | """Waits for an image to be deleted from specific store. |
| 317 | |
| 318 | API will not allow deletion of the last location for an image. |
| 319 | This return image if image deleted from store. |
| 320 | """ |
| 321 | |
| 322 | # Check if image have last store location |
| 323 | if len(available_stores) == 1: |
| 324 | exc_cls = lib_exc.OtherRestClientException |
| 325 | message = ('Delete from last store location not allowed' |
| 326 | % (image, image_store_deleted)) |
| 327 | raise exc_cls(message) |
| 328 | start = int(time.time()) |
| 329 | while int(time.time()) - start < client.build_timeout: |
| 330 | image = client.show_image(image['id']) |
| 331 | image_stores = image['stores'].split(",") |
| 332 | if image_store_deleted not in image_stores: |
| 333 | return |
| 334 | time.sleep(client.build_interval) |
| 335 | message = ('Failed to delete %s from requested store location: %s ' |
| 336 | 'within the required time: (%s s)' % |
| 337 | (image, image_store_deleted, client.build_timeout)) |
| 338 | caller = test_utils.find_test_caller() |
| 339 | if caller: |
| 340 | message = '(%s) %s' % (caller, message) |
| 341 | raise exc_cls(message) |
| 342 | |
| 343 | |
Dan Smith | aeacd8c | 2023-02-21 13:34:20 -0800 | [diff] [blame] | 344 | def wait_for_volume_resource_status(client, resource_id, status, |
| 345 | server_id=None, servers_client=None): |
Matt Riedemann | b36186b | 2017-12-04 17:54:08 +0000 | [diff] [blame] | 346 | """Waits for a volume resource to reach a given status. |
lkuchlan | 52d7b0d | 2016-11-07 20:53:19 +0200 | [diff] [blame] | 347 | |
| 348 | This function is a common function for volume, snapshot and backup |
| 349 | resources. The function extracts the name of the desired resource from |
| 350 | the client class name of the resource. |
Dan Smith | aeacd8c | 2023-02-21 13:34:20 -0800 | [diff] [blame] | 351 | |
| 352 | If server_id and servers_client are provided, dump the console for that |
| 353 | server on failure. |
lkuchlan | 52d7b0d | 2016-11-07 20:53:19 +0200 | [diff] [blame] | 354 | """ |
xing-yang | 41ed715 | 2017-05-03 06:52:56 -0400 | [diff] [blame] | 355 | resource_name = re.findall( |
| 356 | r'(volume|group-snapshot|snapshot|backup|group)', |
| 357 | client.resource_type)[-1].replace('-', '_') |
lkuchlan | 52d7b0d | 2016-11-07 20:53:19 +0200 | [diff] [blame] | 358 | show_resource = getattr(client, 'show_' + resource_name) |
| 359 | resource_status = show_resource(resource_id)[resource_name]['status'] |
Ken'ichi Ohmichi | b942be6 | 2015-07-08 08:16:12 +0000 | [diff] [blame] | 360 | start = int(time.time()) |
| 361 | |
Matt Riedemann | b36186b | 2017-12-04 17:54:08 +0000 | [diff] [blame] | 362 | while resource_status != status: |
Ken'ichi Ohmichi | b942be6 | 2015-07-08 08:16:12 +0000 | [diff] [blame] | 363 | time.sleep(client.build_interval) |
lkuchlan | 52d7b0d | 2016-11-07 20:53:19 +0200 | [diff] [blame] | 364 | resource_status = show_resource(resource_id)[ |
| 365 | '{}'.format(resource_name)]['status'] |
Matt Riedemann | b36186b | 2017-12-04 17:54:08 +0000 | [diff] [blame] | 366 | if resource_status == 'error' and resource_status != status: |
lkuchlan | 52d7b0d | 2016-11-07 20:53:19 +0200 | [diff] [blame] | 367 | raise exceptions.VolumeResourceBuildErrorException( |
| 368 | resource_name=resource_name, resource_id=resource_id) |
| 369 | if resource_name == 'volume' and resource_status == 'error_restoring': |
| 370 | raise exceptions.VolumeRestoreErrorException(volume_id=resource_id) |
zhufl | 0ea2c01 | 2019-06-03 15:37:13 +0800 | [diff] [blame] | 371 | if resource_status == 'error_extending' and resource_status != status: |
| 372 | raise exceptions.VolumeExtendErrorException(volume_id=resource_id) |
Ken'ichi Ohmichi | b942be6 | 2015-07-08 08:16:12 +0000 | [diff] [blame] | 373 | |
| 374 | if int(time.time()) - start >= client.build_timeout: |
Dan Smith | aeacd8c | 2023-02-21 13:34:20 -0800 | [diff] [blame] | 375 | if server_id and servers_client: |
| 376 | console_output = servers_client.get_console_output( |
| 377 | server_id)['output'] |
| 378 | LOG.debug('Console output for %s\nbody=\n%s', |
| 379 | server_id, console_output) |
lkuchlan | 52d7b0d | 2016-11-07 20:53:19 +0200 | [diff] [blame] | 380 | message = ('%s %s failed to reach %s status (current %s) ' |
Ken'ichi Ohmichi | b942be6 | 2015-07-08 08:16:12 +0000 | [diff] [blame] | 381 | 'within the required time (%s s).' % |
Matt Riedemann | b36186b | 2017-12-04 17:54:08 +0000 | [diff] [blame] | 382 | (resource_name, resource_id, status, resource_status, |
Ken'ichi Ohmichi | b942be6 | 2015-07-08 08:16:12 +0000 | [diff] [blame] | 383 | client.build_timeout)) |
guo yunxian | ebb15f2 | 2016-11-01 21:03:35 +0800 | [diff] [blame] | 384 | raise lib_exc.TimeoutException(message) |
zhufl | 019ad73 | 2017-08-28 13:51:22 +0800 | [diff] [blame] | 385 | LOG.info('%s %s reached %s after waiting for %f seconds', |
Matt Riedemann | b36186b | 2017-12-04 17:54:08 +0000 | [diff] [blame] | 386 | resource_name, resource_id, status, time.time() - start) |
Ken'ichi Ohmichi | b942be6 | 2015-07-08 08:16:12 +0000 | [diff] [blame] | 387 | |
| 388 | |
Peter Penchev | 5c243a9 | 2020-09-06 02:26:03 +0300 | [diff] [blame] | 389 | def wait_for_volume_attachment_create(client, volume_id, server_id): |
| 390 | """Waits for a volume attachment to be created at a given volume.""" |
| 391 | start = int(time.time()) |
| 392 | while True: |
| 393 | attachments = client.show_volume(volume_id)['volume']['attachments'] |
| 394 | found = [a for a in attachments if a['server_id'] == server_id] |
| 395 | if found: |
| 396 | LOG.info('Attachment %s created for volume %s to server %s after ' |
| 397 | 'waiting for %f seconds', found[0]['attachment_id'], |
| 398 | volume_id, server_id, time.time() - start) |
| 399 | return found[0] |
| 400 | time.sleep(client.build_interval) |
| 401 | if int(time.time()) - start >= client.build_timeout: |
| 402 | message = ('Failed to attach volume %s to server %s ' |
| 403 | 'within the required time (%s s).' % |
| 404 | (volume_id, server_id, client.build_timeout)) |
| 405 | raise lib_exc.TimeoutException(message) |
| 406 | |
| 407 | |
Lee Yarwood | c1b2a4a | 2020-01-08 17:02:49 +0000 | [diff] [blame] | 408 | def wait_for_volume_attachment_remove(client, volume_id, attachment_id): |
| 409 | """Waits for a volume attachment to be removed from a given volume.""" |
| 410 | start = int(time.time()) |
| 411 | attachments = client.show_volume(volume_id)['volume']['attachments'] |
| 412 | while any(attachment_id == a['attachment_id'] for a in attachments): |
| 413 | time.sleep(client.build_interval) |
| 414 | if int(time.time()) - start >= client.build_timeout: |
zhufl | 747300b | 2020-04-14 14:23:47 +0800 | [diff] [blame] | 415 | message = ('Failed to remove attachment %s from volume %s ' |
Lee Yarwood | c1b2a4a | 2020-01-08 17:02:49 +0000 | [diff] [blame] | 416 | 'within the required time (%s s).' % |
| 417 | (attachment_id, volume_id, client.build_timeout)) |
| 418 | raise lib_exc.TimeoutException(message) |
| 419 | attachments = client.show_volume(volume_id)['volume']['attachments'] |
| 420 | LOG.info('Attachment %s removed from volume %s after waiting for %f ' |
| 421 | 'seconds', attachment_id, volume_id, time.time() - start) |
| 422 | |
| 423 | |
Balazs Gibizer | 2e515fe | 2020-12-07 15:10:11 +0100 | [diff] [blame] | 424 | def wait_for_volume_attachment_remove_from_server( |
| 425 | client, server_id, volume_id): |
| 426 | """Waits for a volume to be removed from a given server. |
| 427 | |
| 428 | This waiter checks the compute API if the volume attachment is removed. |
| 429 | """ |
| 430 | start = int(time.time()) |
Lee Yarwood | 1bd6059 | 2021-06-04 10:18:35 +0100 | [diff] [blame] | 431 | |
| 432 | try: |
| 433 | volumes = client.list_volume_attachments( |
| 434 | server_id)['volumeAttachments'] |
| 435 | except lib_exc.NotFound: |
| 436 | # Ignore 404s on detach in case the server is deleted or the volume |
| 437 | # is already detached. |
| 438 | return |
Balazs Gibizer | 2e515fe | 2020-12-07 15:10:11 +0100 | [diff] [blame] | 439 | |
| 440 | while any(volume for volume in volumes if volume['volumeId'] == volume_id): |
| 441 | time.sleep(client.build_interval) |
| 442 | |
| 443 | timed_out = int(time.time()) - start >= client.build_timeout |
| 444 | if timed_out: |
Lee Yarwood | 1bd6059 | 2021-06-04 10:18:35 +0100 | [diff] [blame] | 445 | console_output = client.get_console_output(server_id)['output'] |
| 446 | LOG.debug('Console output for %s\nbody=\n%s', |
| 447 | server_id, console_output) |
Balazs Gibizer | 2e515fe | 2020-12-07 15:10:11 +0100 | [diff] [blame] | 448 | message = ('Volume %s failed to detach from server %s within ' |
| 449 | 'the required time (%s s) from the compute API ' |
| 450 | 'perspective' % |
| 451 | (volume_id, server_id, client.build_timeout)) |
| 452 | raise lib_exc.TimeoutException(message) |
Lee Yarwood | 1bd6059 | 2021-06-04 10:18:35 +0100 | [diff] [blame] | 453 | try: |
| 454 | volumes = client.list_volume_attachments( |
| 455 | server_id)['volumeAttachments'] |
| 456 | except lib_exc.NotFound: |
| 457 | # Ignore 404s on detach in case the server is deleted or the volume |
| 458 | # is already detached. |
| 459 | return |
| 460 | return |
Balazs Gibizer | 2e515fe | 2020-12-07 15:10:11 +0100 | [diff] [blame] | 461 | |
| 462 | |
Lee Yarwood | e559740 | 2019-02-15 20:17:00 +0000 | [diff] [blame] | 463 | def wait_for_volume_migration(client, volume_id, new_host): |
| 464 | """Waits for a Volume to move to a new host.""" |
| 465 | body = client.show_volume(volume_id)['volume'] |
| 466 | host = body['os-vol-host-attr:host'] |
| 467 | migration_status = body['migration_status'] |
| 468 | start = int(time.time()) |
| 469 | |
| 470 | # new_host is hostname@backend while current_host is hostname@backend#type |
| 471 | while migration_status != 'success' or new_host not in host: |
| 472 | time.sleep(client.build_interval) |
| 473 | body = client.show_volume(volume_id)['volume'] |
| 474 | host = body['os-vol-host-attr:host'] |
| 475 | migration_status = body['migration_status'] |
| 476 | |
| 477 | if migration_status == 'error': |
| 478 | message = ('volume %s failed to migrate.' % (volume_id)) |
| 479 | raise lib_exc.TempestException(message) |
| 480 | |
| 481 | if int(time.time()) - start >= client.build_timeout: |
| 482 | message = ('Volume %s failed to migrate to %s (current %s) ' |
| 483 | 'within the required time (%s s).' % |
| 484 | (volume_id, new_host, host, client.build_timeout)) |
| 485 | raise lib_exc.TimeoutException(message) |
| 486 | |
| 487 | |
scottda | 61f68ac | 2016-06-07 12:07:55 -0600 | [diff] [blame] | 488 | def wait_for_volume_retype(client, volume_id, new_volume_type): |
| 489 | """Waits for a Volume to have a new volume type.""" |
| 490 | body = client.show_volume(volume_id)['volume'] |
| 491 | current_volume_type = body['volume_type'] |
| 492 | start = int(time.time()) |
| 493 | |
| 494 | while current_volume_type != new_volume_type: |
| 495 | time.sleep(client.build_interval) |
| 496 | body = client.show_volume(volume_id)['volume'] |
| 497 | current_volume_type = body['volume_type'] |
| 498 | |
| 499 | if int(time.time()) - start >= client.build_timeout: |
| 500 | message = ('Volume %s failed to reach %s volume type (current %s) ' |
| 501 | 'within the required time (%s s).' % |
| 502 | (volume_id, new_volume_type, current_volume_type, |
| 503 | client.build_timeout)) |
Matt Riedemann | 74eb3b5 | 2017-02-14 11:34:30 -0500 | [diff] [blame] | 504 | raise lib_exc.TimeoutException(message) |
scottda | 61f68ac | 2016-06-07 12:07:55 -0600 | [diff] [blame] | 505 | |
| 506 | |
lkuchlan | ec1ba4f | 2016-09-06 10:28:18 +0300 | [diff] [blame] | 507 | def wait_for_qos_operations(client, qos_id, operation, args=None): |
| 508 | """Waits for a qos operations to be completed. |
| 509 | |
| 510 | NOTE : operation value is required for wait_for_qos_operations() |
| 511 | operation = 'qos-key' / 'disassociate' / 'disassociate-all' |
| 512 | args = keys[] when operation = 'qos-key' |
| 513 | args = volume-type-id disassociated when operation = 'disassociate' |
| 514 | args = None when operation = 'disassociate-all' |
| 515 | """ |
| 516 | start_time = int(time.time()) |
| 517 | while True: |
| 518 | if operation == 'qos-key-unset': |
| 519 | body = client.show_qos(qos_id)['qos_specs'] |
| 520 | if not any(key in body['specs'] for key in args): |
| 521 | return |
| 522 | elif operation == 'disassociate': |
| 523 | body = client.show_association_qos(qos_id)['qos_associations'] |
| 524 | if not any(args in body[i]['id'] for i in range(0, len(body))): |
| 525 | return |
| 526 | elif operation == 'disassociate-all': |
| 527 | body = client.show_association_qos(qos_id)['qos_associations'] |
| 528 | if not body: |
| 529 | return |
| 530 | else: |
| 531 | msg = (" operation value is either not defined or incorrect.") |
| 532 | raise lib_exc.UnprocessableEntity(msg) |
| 533 | |
| 534 | if int(time.time()) - start_time >= client.build_timeout: |
guo yunxian | ebb15f2 | 2016-11-01 21:03:35 +0800 | [diff] [blame] | 535 | raise lib_exc.TimeoutException |
lkuchlan | ec1ba4f | 2016-09-06 10:28:18 +0300 | [diff] [blame] | 536 | time.sleep(client.build_interval) |
zhufl | 7b63833 | 2016-11-14 10:23:30 +0800 | [diff] [blame] | 537 | |
| 538 | |
Matt Riedemann | 2ea48f0 | 2017-04-03 11:36:19 -0400 | [diff] [blame] | 539 | def wait_for_interface_status(client, server_id, port_id, status): |
zhufl | 7b63833 | 2016-11-14 10:23:30 +0800 | [diff] [blame] | 540 | """Waits for an interface to reach a given status.""" |
Matt Riedemann | 2ea48f0 | 2017-04-03 11:36:19 -0400 | [diff] [blame] | 541 | body = (client.show_interface(server_id, port_id) |
zhufl | 7b63833 | 2016-11-14 10:23:30 +0800 | [diff] [blame] | 542 | ['interfaceAttachment']) |
| 543 | interface_status = body['port_state'] |
| 544 | start = int(time.time()) |
| 545 | |
| 546 | while(interface_status != status): |
| 547 | time.sleep(client.build_interval) |
Matt Riedemann | 2ea48f0 | 2017-04-03 11:36:19 -0400 | [diff] [blame] | 548 | body = (client.show_interface(server_id, port_id) |
zhufl | 7b63833 | 2016-11-14 10:23:30 +0800 | [diff] [blame] | 549 | ['interfaceAttachment']) |
| 550 | interface_status = body['port_state'] |
| 551 | |
| 552 | timed_out = int(time.time()) - start >= client.build_timeout |
| 553 | |
| 554 | if interface_status != status and timed_out: |
| 555 | message = ('Interface %s failed to reach %s status ' |
| 556 | '(current %s) within the required time (%s s).' % |
| 557 | (port_id, status, interface_status, |
| 558 | client.build_timeout)) |
| 559 | raise lib_exc.TimeoutException(message) |
| 560 | |
| 561 | return body |
Artom Lifshitz | 3306d42 | 2018-03-22 12:20:54 -0400 | [diff] [blame] | 562 | |
| 563 | |
Balazs Gibizer | 5541458 | 2021-10-05 11:22:30 +0200 | [diff] [blame] | 564 | def wait_for_interface_detach(client, server_id, port_id, detach_request_id): |
Artom Lifshitz | 3306d42 | 2018-03-22 12:20:54 -0400 | [diff] [blame] | 565 | """Waits for an interface to be detached from a server.""" |
Balazs Gibizer | 5541458 | 2021-10-05 11:22:30 +0200 | [diff] [blame] | 566 | def _get_detach_event_results(): |
| 567 | # NOTE(gibi): The obvious choice for this waiter would be to wait |
| 568 | # until the interface disappears from the client.list_interfaces() |
| 569 | # response. However that response is based on the binding status of the |
| 570 | # port in Neutron. Nova deallocates the port resources _after the port |
| 571 | # was unbound in Neutron. This can cause that the naive waiter would |
| 572 | # return before the port is fully deallocated. Wait instead of the |
| 573 | # os-instance-action to succeed as that is recorded after both the |
| 574 | # port is fully deallocated. |
| 575 | events = client.show_instance_action( |
| 576 | server_id, detach_request_id)['instanceAction'].get('events', []) |
| 577 | return [ |
| 578 | event['result'] for event in events |
| 579 | if event['event'] == 'compute_detach_interface' |
| 580 | ] |
| 581 | |
| 582 | detach_event_results = _get_detach_event_results() |
| 583 | |
Artom Lifshitz | 3306d42 | 2018-03-22 12:20:54 -0400 | [diff] [blame] | 584 | start = int(time.time()) |
| 585 | |
Balazs Gibizer | 5541458 | 2021-10-05 11:22:30 +0200 | [diff] [blame] | 586 | while "Success" not in detach_event_results: |
Artom Lifshitz | 3306d42 | 2018-03-22 12:20:54 -0400 | [diff] [blame] | 587 | time.sleep(client.build_interval) |
Balazs Gibizer | 5541458 | 2021-10-05 11:22:30 +0200 | [diff] [blame] | 588 | detach_event_results = _get_detach_event_results() |
| 589 | if "Success" in detach_event_results: |
| 590 | return client.show_instance_action( |
| 591 | server_id, detach_request_id)['instanceAction'] |
Artom Lifshitz | 3306d42 | 2018-03-22 12:20:54 -0400 | [diff] [blame] | 592 | |
| 593 | timed_out = int(time.time()) - start >= client.build_timeout |
| 594 | if timed_out: |
| 595 | message = ('Interface %s failed to detach from server %s within ' |
| 596 | 'the required time (%s s)' % (port_id, server_id, |
| 597 | client.build_timeout)) |
| 598 | raise lib_exc.TimeoutException(message) |
Slawek Kaplonski | e3405ba | 2020-11-09 17:24:13 +0100 | [diff] [blame] | 599 | |
| 600 | |
Artom Lifshitz | 8a959ea | 2021-09-27 12:09:12 -0400 | [diff] [blame] | 601 | def wait_for_server_floating_ip(servers_client, server, floating_ip, |
| 602 | wait_for_disassociate=False): |
| 603 | """Wait for floating IP association or disassociation. |
| 604 | |
| 605 | :param servers_client: The servers client to use when querying the server's |
| 606 | floating IPs. |
| 607 | :param server: The server JSON dict on which to wait. |
| 608 | :param floating_ip: The floating IP JSON dict on which to wait. |
| 609 | :param wait_for_disassociate: Boolean indiating whether to wait for |
| 610 | disassociation instead of association. |
| 611 | """ |
| 612 | |
| 613 | def _get_floating_ip_in_server_addresses(floating_ip, server): |
| 614 | for addresses in server['addresses'].values(): |
| 615 | for address in addresses: |
| 616 | if ( |
| 617 | address['OS-EXT-IPS:type'] == 'floating' and |
| 618 | address['addr'] == floating_ip['floating_ip_address'] |
| 619 | ): |
| 620 | return address |
| 621 | return None |
| 622 | |
| 623 | start_time = int(time.time()) |
| 624 | while True: |
| 625 | server = servers_client.show_server(server['id'])['server'] |
| 626 | address = _get_floating_ip_in_server_addresses(floating_ip, server) |
| 627 | if address is None and wait_for_disassociate: |
| 628 | return None |
| 629 | if not wait_for_disassociate and address: |
| 630 | return address |
| 631 | |
| 632 | if int(time.time()) - start_time >= servers_client.build_timeout: |
| 633 | if wait_for_disassociate: |
| 634 | msg = ('Floating ip %s failed to disassociate from server %s ' |
| 635 | 'in time.' % (floating_ip, server['id'])) |
| 636 | else: |
| 637 | msg = ('Floating ip %s failed to associate with server %s ' |
| 638 | 'in time.' % (floating_ip, server['id'])) |
| 639 | raise lib_exc.TimeoutException(msg) |
| 640 | time.sleep(servers_client.build_interval) |
Lee Yarwood | 0b4bc3d | 2021-11-11 17:45:25 +0000 | [diff] [blame] | 641 | |
| 642 | |
| 643 | def wait_for_ping(server_ip, timeout=30, interval=1): |
| 644 | """Waits for an address to become pingable""" |
| 645 | start_time = int(time.time()) |
| 646 | while int(time.time()) - start_time < timeout: |
| 647 | response = os.system("ping -c 1 " + server_ip) |
| 648 | if response == 0: |
| 649 | return |
| 650 | time.sleep(interval) |
| 651 | raise lib_exc.TimeoutException() |
| 652 | |
| 653 | |
Eliad Cohen | bec2d4d | 2022-09-14 17:52:59 +0000 | [diff] [blame] | 654 | def wait_for_port_status(client, port_id, status): |
| 655 | """Wait for a port reach a certain status : ["BUILD" | "DOWN" | "ACTIVE"] |
| 656 | :param client: The network client to use when querying the port's |
| 657 | status |
| 658 | :param status: A string to compare the current port status-to. |
| 659 | :param port_id: The uuid of the port we would like queried for status. |
| 660 | """ |
| 661 | start_time = time.time() |
| 662 | while (time.time() - start_time <= client.build_timeout): |
| 663 | result = client.show_port(port_id) |
| 664 | if result['port']['status'].lower() == status.lower(): |
| 665 | return result |
| 666 | time.sleep(client.build_interval) |
| 667 | raise lib_exc.TimeoutException |
| 668 | |
| 669 | |
Lee Yarwood | 0b4bc3d | 2021-11-11 17:45:25 +0000 | [diff] [blame] | 670 | def wait_for_ssh(ssh_client, timeout=30): |
| 671 | """Waits for SSH connection to become usable""" |
| 672 | start_time = int(time.time()) |
| 673 | while int(time.time()) - start_time < timeout: |
| 674 | try: |
| 675 | ssh_client.validate_authentication() |
| 676 | return |
| 677 | except lib_exc.SSHTimeout: |
| 678 | pass |
| 679 | raise lib_exc.TimeoutException() |
Abhishek Kekane | 0188f46 | 2022-01-18 06:47:28 +0000 | [diff] [blame] | 680 | |
| 681 | |
| 682 | def wait_for_caching(client, cache_client, image_id): |
| 683 | """Waits until image is cached""" |
| 684 | start = int(time.time()) |
| 685 | while int(time.time()) - start < client.build_timeout: |
| 686 | caching = cache_client.list_cache() |
| 687 | output = [image['image_id'] for image in caching['cached_images']] |
| 688 | if output and image_id in output: |
| 689 | return caching |
| 690 | |
| 691 | time.sleep(client.build_interval) |
| 692 | |
| 693 | message = ('Image %s failed to cache in time.' % image_id) |
| 694 | caller = test_utils.find_test_caller() |
| 695 | if caller: |
| 696 | message = '(%s) %s' % (caller, message) |
| 697 | raise lib_exc.TimeoutException(message) |
Bas de Bruijne | c9f9a03 | 2022-07-19 11:16:02 -0300 | [diff] [blame] | 698 | |
| 699 | |
| 700 | def wait_for_object_create(object_client, container_name, object_name, |
| 701 | interval=1): |
| 702 | """Waits for created object to become available""" |
| 703 | start_time = time.time() |
| 704 | while time.time() - start_time < object_client.build_timeout: |
| 705 | try: |
| 706 | return object_client.get_object(container_name, object_name) |
| 707 | except lib_exc.NotFound: |
| 708 | time.sleep(interval) |
| 709 | message = ('Object %s failed to create within the required time (%s s).' % |
| 710 | (object_name, object_client.build_timeout)) |
| 711 | raise lib_exc.TimeoutException(message) |