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 | |
| 13 | |
| 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 | |
| 29 | # 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] | 30 | 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] | 31 | extra_timeout=0, raise_on_error=True): |
Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 32 | """Waits for a server to reach a given status.""" |
| 33 | |
| 34 | def _get_task_state(body): |
Ken'ichi Ohmichi | a58c156 | 2014-12-15 00:39:55 +0000 | [diff] [blame] | 35 | return body.get('OS-EXT-STS:task_state', None) |
Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 36 | |
| 37 | # NOTE(afazekas): UNKNOWN status possible on ERROR |
| 38 | # or in a very early stage. |
ghanshyam | 0f82525 | 2015-08-25 16:02:50 +0900 | [diff] [blame] | 39 | body = client.show_server(server_id)['server'] |
Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 40 | old_status = server_status = body['status'] |
| 41 | old_task_state = task_state = _get_task_state(body) |
| 42 | start_time = int(time.time()) |
Ken'ichi Ohmichi | 39437e2 | 2013-10-06 00:21:38 +0900 | [diff] [blame] | 43 | timeout = client.build_timeout + extra_timeout |
Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 44 | while True: |
| 45 | # NOTE(afazekas): Now the BUILD status only reached |
Shane Wang | 111f86c | 2014-02-20 16:40:22 +0800 | [diff] [blame] | 46 | # between the UNKNOWN->ACTIVE transition. |
Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 47 | # TODO(afazekas): enumerate and validate the stable status set |
| 48 | if status == 'BUILD' and server_status != 'UNKNOWN': |
| 49 | return |
| 50 | if server_status == status: |
| 51 | if ready_wait: |
| 52 | if status == 'BUILD': |
| 53 | return |
| 54 | # NOTE(afazekas): The instance is in "ready for action state" |
| 55 | # when no task in progress |
Ken'ichi Ohmichi | 534a05e | 2016-08-03 14:45:15 -0700 | [diff] [blame] | 56 | if task_state is None: |
Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 57 | # without state api extension 3 sec usually enough |
Sean Dague | 86bd842 | 2013-12-20 09:56:44 -0500 | [diff] [blame] | 58 | time.sleep(CONF.compute.ready_wait) |
Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 59 | return |
| 60 | else: |
| 61 | return |
| 62 | |
| 63 | time.sleep(client.build_interval) |
ghanshyam | 0f82525 | 2015-08-25 16:02:50 +0900 | [diff] [blame] | 64 | body = client.show_server(server_id)['server'] |
Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 65 | server_status = body['status'] |
| 66 | task_state = _get_task_state(body) |
| 67 | if (server_status != old_status) or (task_state != old_task_state): |
| 68 | LOG.info('State transition "%s" ==> "%s" after %d second wait', |
| 69 | '/'.join((old_status, str(old_task_state))), |
| 70 | '/'.join((server_status, str(task_state))), |
| 71 | time.time() - start_time) |
Zhi Kun Liu | e540176 | 2013-09-11 20:45:48 +0800 | [diff] [blame] | 72 | if (server_status == 'ERROR') and raise_on_error: |
Attila Fazekas | 0462a7f | 2014-06-20 07:38:06 +0200 | [diff] [blame] | 73 | if 'fault' in body: |
| 74 | raise exceptions.BuildErrorException(body['fault'], |
Matthew Treinish | 1d14c54 | 2014-06-17 20:25:40 -0400 | [diff] [blame] | 75 | server_id=server_id) |
Attila Fazekas | 0462a7f | 2014-06-20 07:38:06 +0200 | [diff] [blame] | 76 | else: |
| 77 | raise exceptions.BuildErrorException(server_id=server_id) |
Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 78 | |
Ken'ichi Ohmichi | 39437e2 | 2013-10-06 00:21:38 +0900 | [diff] [blame] | 79 | timed_out = int(time.time()) - start_time >= timeout |
Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 80 | |
| 81 | if timed_out: |
Matt Riedemann | 629fa7c | 2013-12-11 18:20:56 -0800 | [diff] [blame] | 82 | expected_task_state = 'None' if ready_wait else 'n/a' |
| 83 | message = ('Server %(server_id)s failed to reach %(status)s ' |
| 84 | 'status and task state "%(expected_task_state)s" ' |
| 85 | 'within the required time (%(timeout)s s).' % |
| 86 | {'server_id': server_id, |
| 87 | 'status': status, |
| 88 | 'expected_task_state': expected_task_state, |
| 89 | 'timeout': timeout}) |
Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 90 | message += ' Current status: %s.' % server_status |
Matt Riedemann | 629fa7c | 2013-12-11 18:20:56 -0800 | [diff] [blame] | 91 | message += ' Current task state: %s.' % task_state |
zhufl | 88c89b5 | 2016-07-01 18:09:05 +0800 | [diff] [blame] | 92 | caller = test_utils.find_test_caller() |
Matt Riedemann | 128c23e | 2014-05-02 13:46:39 -0700 | [diff] [blame] | 93 | if caller: |
| 94 | message = '(%s) %s' % (caller, message) |
Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 95 | raise exceptions.TimeoutException(message) |
| 96 | old_status = server_status |
| 97 | old_task_state = task_state |
Matt Riedemann | c00f326 | 2013-12-14 12:03:55 -0800 | [diff] [blame] | 98 | |
| 99 | |
Ken'ichi Ohmichi | e91a0c6 | 2015-08-13 02:09:16 +0000 | [diff] [blame] | 100 | def wait_for_server_termination(client, server_id, ignore_error=False): |
| 101 | """Waits for server to reach termination.""" |
| 102 | start_time = int(time.time()) |
| 103 | while True: |
| 104 | try: |
ghanshyam | 0f82525 | 2015-08-25 16:02:50 +0900 | [diff] [blame] | 105 | body = client.show_server(server_id)['server'] |
Ken'ichi Ohmichi | e91a0c6 | 2015-08-13 02:09:16 +0000 | [diff] [blame] | 106 | except lib_exc.NotFound: |
| 107 | return |
| 108 | |
| 109 | server_status = body['status'] |
| 110 | if server_status == 'ERROR' and not ignore_error: |
| 111 | raise exceptions.BuildErrorException(server_id=server_id) |
| 112 | |
| 113 | if int(time.time()) - start_time >= client.build_timeout: |
| 114 | raise exceptions.TimeoutException |
| 115 | |
| 116 | time.sleep(client.build_interval) |
| 117 | |
| 118 | |
Matt Riedemann | c00f326 | 2013-12-14 12:03:55 -0800 | [diff] [blame] | 119 | def wait_for_image_status(client, image_id, status): |
| 120 | """Waits for an image to reach a given status. |
| 121 | |
Ken'ichi Ohmichi | 5d41076 | 2015-05-22 01:10:03 +0000 | [diff] [blame] | 122 | 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] | 123 | The client should also have build_interval and build_timeout attributes. |
| 124 | """ |
Yaroslav Lobankov | 2fea405 | 2016-04-19 15:05:57 +0300 | [diff] [blame] | 125 | if isinstance(client, images_v1_client.ImagesClient): |
| 126 | # The 'check_image' method is used here because the show_image method |
| 127 | # returns image details plus the image itself which is very expensive. |
| 128 | # The 'check_image' method returns just image details. |
Ken'ichi Ohmichi | 01151e8 | 2016-06-10 11:19:52 -0700 | [diff] [blame] | 129 | def _show_image_v1(image_id): |
| 130 | resp = client.check_image(image_id) |
| 131 | return common_image.get_image_meta_from_headers(resp) |
| 132 | |
| 133 | show_image = _show_image_v1 |
Yaroslav Lobankov | 2fea405 | 2016-04-19 15:05:57 +0300 | [diff] [blame] | 134 | else: |
| 135 | show_image = client.show_image |
Matt Riedemann | c00f326 | 2013-12-14 12:03:55 -0800 | [diff] [blame] | 136 | |
Yaroslav Lobankov | 2fea405 | 2016-04-19 15:05:57 +0300 | [diff] [blame] | 137 | current_status = 'An unknown status' |
| 138 | start = int(time.time()) |
| 139 | while int(time.time()) - start < client.build_timeout: |
| 140 | image = show_image(image_id) |
| 141 | # Compute image client returns response wrapped in 'image' element |
Xiangfei Zhu | a8fd20a | 2016-07-26 21:28:12 -0700 | [diff] [blame] | 142 | # which is not the case with Glance image client. |
ghanshyam | 1756e0b | 2015-08-18 19:19:05 +0900 | [diff] [blame] | 143 | if 'image' in image: |
| 144 | image = image['image'] |
Yaroslav Lobankov | 2fea405 | 2016-04-19 15:05:57 +0300 | [diff] [blame] | 145 | |
| 146 | current_status = image['status'] |
| 147 | if current_status == status: |
| 148 | return |
| 149 | if current_status.lower() == 'killed': |
| 150 | raise exceptions.ImageKilledException(image_id=image_id, |
| 151 | status=status) |
| 152 | if current_status.lower() == 'error': |
Matt Riedemann | c00f326 | 2013-12-14 12:03:55 -0800 | [diff] [blame] | 153 | raise exceptions.AddImageException(image_id=image_id) |
| 154 | |
Yaroslav Lobankov | 2fea405 | 2016-04-19 15:05:57 +0300 | [diff] [blame] | 155 | time.sleep(client.build_interval) |
Matt Riedemann | c00f326 | 2013-12-14 12:03:55 -0800 | [diff] [blame] | 156 | |
Yaroslav Lobankov | 2fea405 | 2016-04-19 15:05:57 +0300 | [diff] [blame] | 157 | message = ('Image %(image_id)s failed to reach %(status)s state ' |
| 158 | '(current state %(current_status)s) within the required ' |
| 159 | 'time (%(timeout)s s).' % {'image_id': image_id, |
| 160 | 'status': status, |
| 161 | 'current_status': current_status, |
| 162 | 'timeout': client.build_timeout}) |
zhufl | 88c89b5 | 2016-07-01 18:09:05 +0800 | [diff] [blame] | 163 | caller = test_utils.find_test_caller() |
Yaroslav Lobankov | 2fea405 | 2016-04-19 15:05:57 +0300 | [diff] [blame] | 164 | if caller: |
| 165 | message = '(%s) %s' % (caller, message) |
| 166 | raise exceptions.TimeoutException(message) |
Adam Gandelman | 0068261 | 2014-09-02 17:10:36 -0700 | [diff] [blame] | 167 | |
| 168 | |
Ken'ichi Ohmichi | b942be6 | 2015-07-08 08:16:12 +0000 | [diff] [blame] | 169 | def wait_for_volume_status(client, volume_id, status): |
| 170 | """Waits for a Volume to reach a given status.""" |
ghanshyam | cb13176 | 2015-08-24 18:21:08 +0900 | [diff] [blame] | 171 | body = client.show_volume(volume_id)['volume'] |
Ken'ichi Ohmichi | b942be6 | 2015-07-08 08:16:12 +0000 | [diff] [blame] | 172 | volume_status = body['status'] |
| 173 | start = int(time.time()) |
| 174 | |
| 175 | while volume_status != status: |
| 176 | time.sleep(client.build_interval) |
ghanshyam | cb13176 | 2015-08-24 18:21:08 +0900 | [diff] [blame] | 177 | body = client.show_volume(volume_id)['volume'] |
Ken'ichi Ohmichi | b942be6 | 2015-07-08 08:16:12 +0000 | [diff] [blame] | 178 | volume_status = body['status'] |
| 179 | if volume_status == 'error': |
| 180 | raise exceptions.VolumeBuildErrorException(volume_id=volume_id) |
Matt Riedemann | f77e7dc | 2015-08-10 16:39:39 -0700 | [diff] [blame] | 181 | if volume_status == 'error_restoring': |
| 182 | raise exceptions.VolumeRestoreErrorException(volume_id=volume_id) |
Ken'ichi Ohmichi | b942be6 | 2015-07-08 08:16:12 +0000 | [diff] [blame] | 183 | |
| 184 | if int(time.time()) - start >= client.build_timeout: |
| 185 | message = ('Volume %s failed to reach %s status (current %s) ' |
| 186 | 'within the required time (%s s).' % |
| 187 | (volume_id, status, volume_status, |
| 188 | client.build_timeout)) |
| 189 | raise exceptions.TimeoutException(message) |
| 190 | |
| 191 | |
Gaozexu | b9c9d6e | 2015-09-10 17:08:04 +0800 | [diff] [blame] | 192 | def wait_for_snapshot_status(client, snapshot_id, status): |
| 193 | """Waits for a Snapshot to reach a given status.""" |
| 194 | body = client.show_snapshot(snapshot_id)['snapshot'] |
| 195 | snapshot_status = body['status'] |
| 196 | start = int(time.time()) |
| 197 | |
| 198 | while snapshot_status != status: |
| 199 | time.sleep(client.build_interval) |
| 200 | body = client.show_snapshot(snapshot_id)['snapshot'] |
| 201 | snapshot_status = body['status'] |
| 202 | if snapshot_status == 'error': |
| 203 | raise exceptions.SnapshotBuildErrorException( |
| 204 | snapshot_id=snapshot_id) |
| 205 | if int(time.time()) - start >= client.build_timeout: |
| 206 | message = ('Snapshot %s failed to reach %s status (current %s) ' |
| 207 | 'within the required time (%s s).' % |
| 208 | (snapshot_id, status, snapshot_status, |
| 209 | client.build_timeout)) |
| 210 | raise exceptions.TimeoutException(message) |
| 211 | |
| 212 | |
Adam Gandelman | 0068261 | 2014-09-02 17:10:36 -0700 | [diff] [blame] | 213 | def wait_for_bm_node_status(client, node_id, attr, status): |
| 214 | """Waits for a baremetal node attribute to reach given status. |
| 215 | |
| 216 | The client should have a show_node(node_uuid) method to get the node. |
| 217 | """ |
| 218 | _, node = client.show_node(node_id) |
| 219 | start = int(time.time()) |
| 220 | |
| 221 | while node[attr] != status: |
| 222 | time.sleep(client.build_interval) |
| 223 | _, node = client.show_node(node_id) |
Martin Pavlasek | 1102c3a | 2014-10-20 17:17:55 +0200 | [diff] [blame] | 224 | status_curr = node[attr] |
| 225 | if status_curr == status: |
Adam Gandelman | 0068261 | 2014-09-02 17:10:36 -0700 | [diff] [blame] | 226 | return |
| 227 | |
| 228 | if int(time.time()) - start >= client.build_timeout: |
| 229 | message = ('Node %(node_id)s failed to reach %(attr)s=%(status)s ' |
| 230 | 'within the required time (%(timeout)s s).' % |
| 231 | {'node_id': node_id, |
| 232 | 'attr': attr, |
| 233 | 'status': status, |
| 234 | 'timeout': client.build_timeout}) |
Martin Pavlasek | 1102c3a | 2014-10-20 17:17:55 +0200 | [diff] [blame] | 235 | message += ' Current state of %s: %s.' % (attr, status_curr) |
zhufl | 88c89b5 | 2016-07-01 18:09:05 +0800 | [diff] [blame] | 236 | caller = test_utils.find_test_caller() |
Adam Gandelman | 0068261 | 2014-09-02 17:10:36 -0700 | [diff] [blame] | 237 | if caller: |
| 238 | message = '(%s) %s' % (caller, message) |
| 239 | raise exceptions.TimeoutException(message) |