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 | |
Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 18 | from tempest import config |
| 19 | from tempest import exceptions |
Andrea Frittoli (andreaf) | db9672e | 2016-02-23 14:07:24 -0500 | [diff] [blame] | 20 | from tempest.lib.common.utils import misc as misc_utils |
| 21 | from tempest.lib import exceptions as lib_exc |
Yaroslav Lobankov | 2fea405 | 2016-04-19 15:05:57 +0300 | [diff] [blame] | 22 | from tempest.services.image.v1.json import images_client as images_v1_client |
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 | |
| 28 | # 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] | 29 | 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] | 30 | extra_timeout=0, raise_on_error=True): |
Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 31 | """Waits for a server to reach a given status.""" |
| 32 | |
| 33 | def _get_task_state(body): |
Ken'ichi Ohmichi | a58c156 | 2014-12-15 00:39:55 +0000 | [diff] [blame] | 34 | return body.get('OS-EXT-STS:task_state', None) |
Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 35 | |
| 36 | # NOTE(afazekas): UNKNOWN status possible on ERROR |
| 37 | # or in a very early stage. |
ghanshyam | 0f82525 | 2015-08-25 16:02:50 +0900 | [diff] [blame] | 38 | body = client.show_server(server_id)['server'] |
Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 39 | old_status = server_status = body['status'] |
| 40 | old_task_state = task_state = _get_task_state(body) |
| 41 | start_time = int(time.time()) |
Ken'ichi Ohmichi | 39437e2 | 2013-10-06 00:21:38 +0900 | [diff] [blame] | 42 | timeout = client.build_timeout + extra_timeout |
Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 43 | while True: |
| 44 | # NOTE(afazekas): Now the BUILD status only reached |
Shane Wang | 111f86c | 2014-02-20 16:40:22 +0800 | [diff] [blame] | 45 | # between the UNKNOWN->ACTIVE transition. |
Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 46 | # TODO(afazekas): enumerate and validate the stable status set |
| 47 | if status == 'BUILD' and server_status != 'UNKNOWN': |
| 48 | return |
| 49 | if server_status == status: |
| 50 | if ready_wait: |
| 51 | if status == 'BUILD': |
| 52 | return |
| 53 | # NOTE(afazekas): The instance is in "ready for action state" |
| 54 | # when no task in progress |
nayna-patel | fdc87a5 | 2015-08-03 10:46:33 +0000 | [diff] [blame] | 55 | # NOTE(afazekas): Converted to string because of the XML |
Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 56 | # responses |
| 57 | if str(task_state) == "None": |
| 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 |
Matt Riedemann | 128c23e | 2014-05-02 13:46:39 -0700 | [diff] [blame] | 93 | caller = misc_utils.find_test_caller() |
| 94 | if caller: |
| 95 | message = '(%s) %s' % (caller, message) |
Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 96 | raise exceptions.TimeoutException(message) |
| 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.""" |
| 103 | start_time = int(time.time()) |
| 104 | while True: |
| 105 | try: |
ghanshyam | 0f82525 | 2015-08-25 16:02:50 +0900 | [diff] [blame] | 106 | body = client.show_server(server_id)['server'] |
Ken'ichi Ohmichi | e91a0c6 | 2015-08-13 02:09:16 +0000 | [diff] [blame] | 107 | except lib_exc.NotFound: |
| 108 | return |
| 109 | |
| 110 | server_status = body['status'] |
| 111 | if server_status == 'ERROR' and not ignore_error: |
| 112 | raise exceptions.BuildErrorException(server_id=server_id) |
| 113 | |
| 114 | if int(time.time()) - start_time >= client.build_timeout: |
| 115 | raise exceptions.TimeoutException |
| 116 | |
| 117 | time.sleep(client.build_interval) |
| 118 | |
| 119 | |
Matt Riedemann | c00f326 | 2013-12-14 12:03:55 -0800 | [diff] [blame] | 120 | def wait_for_image_status(client, image_id, status): |
| 121 | """Waits for an image to reach a given status. |
| 122 | |
Ken'ichi Ohmichi | 5d41076 | 2015-05-22 01:10:03 +0000 | [diff] [blame] | 123 | 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] | 124 | The client should also have build_interval and build_timeout attributes. |
| 125 | """ |
Yaroslav Lobankov | 2fea405 | 2016-04-19 15:05:57 +0300 | [diff] [blame] | 126 | if isinstance(client, images_v1_client.ImagesClient): |
| 127 | # The 'check_image' method is used here because the show_image method |
| 128 | # returns image details plus the image itself which is very expensive. |
| 129 | # The 'check_image' method returns just image details. |
| 130 | show_image = client.check_image |
| 131 | else: |
| 132 | show_image = client.show_image |
Matt Riedemann | c00f326 | 2013-12-14 12:03:55 -0800 | [diff] [blame] | 133 | |
Yaroslav Lobankov | 2fea405 | 2016-04-19 15:05:57 +0300 | [diff] [blame] | 134 | current_status = 'An unknown status' |
| 135 | start = int(time.time()) |
| 136 | while int(time.time()) - start < client.build_timeout: |
| 137 | image = show_image(image_id) |
| 138 | # Compute image client returns response wrapped in 'image' element |
| 139 | # which is not case with Glance image client. |
ghanshyam | 1756e0b | 2015-08-18 19:19:05 +0900 | [diff] [blame] | 140 | if 'image' in image: |
| 141 | image = image['image'] |
Yaroslav Lobankov | 2fea405 | 2016-04-19 15:05:57 +0300 | [diff] [blame] | 142 | |
| 143 | current_status = image['status'] |
| 144 | if current_status == status: |
| 145 | return |
| 146 | if current_status.lower() == 'killed': |
| 147 | raise exceptions.ImageKilledException(image_id=image_id, |
| 148 | status=status) |
| 149 | if current_status.lower() == 'error': |
Matt Riedemann | c00f326 | 2013-12-14 12:03:55 -0800 | [diff] [blame] | 150 | raise exceptions.AddImageException(image_id=image_id) |
| 151 | |
Yaroslav Lobankov | 2fea405 | 2016-04-19 15:05:57 +0300 | [diff] [blame] | 152 | time.sleep(client.build_interval) |
Matt Riedemann | c00f326 | 2013-12-14 12:03:55 -0800 | [diff] [blame] | 153 | |
Yaroslav Lobankov | 2fea405 | 2016-04-19 15:05:57 +0300 | [diff] [blame] | 154 | message = ('Image %(image_id)s failed to reach %(status)s state ' |
| 155 | '(current state %(current_status)s) within the required ' |
| 156 | 'time (%(timeout)s s).' % {'image_id': image_id, |
| 157 | 'status': status, |
| 158 | 'current_status': current_status, |
| 159 | 'timeout': client.build_timeout}) |
| 160 | caller = misc_utils.find_test_caller() |
| 161 | if caller: |
| 162 | message = '(%s) %s' % (caller, message) |
| 163 | raise exceptions.TimeoutException(message) |
Adam Gandelman | 0068261 | 2014-09-02 17:10:36 -0700 | [diff] [blame] | 164 | |
| 165 | |
Ken'ichi Ohmichi | b942be6 | 2015-07-08 08:16:12 +0000 | [diff] [blame] | 166 | def wait_for_volume_status(client, volume_id, status): |
| 167 | """Waits for a Volume to reach a given status.""" |
ghanshyam | cb13176 | 2015-08-24 18:21:08 +0900 | [diff] [blame] | 168 | body = client.show_volume(volume_id)['volume'] |
Ken'ichi Ohmichi | b942be6 | 2015-07-08 08:16:12 +0000 | [diff] [blame] | 169 | volume_status = body['status'] |
| 170 | start = int(time.time()) |
| 171 | |
| 172 | while volume_status != status: |
| 173 | time.sleep(client.build_interval) |
ghanshyam | cb13176 | 2015-08-24 18:21:08 +0900 | [diff] [blame] | 174 | body = client.show_volume(volume_id)['volume'] |
Ken'ichi Ohmichi | b942be6 | 2015-07-08 08:16:12 +0000 | [diff] [blame] | 175 | volume_status = body['status'] |
| 176 | if volume_status == 'error': |
| 177 | raise exceptions.VolumeBuildErrorException(volume_id=volume_id) |
Matt Riedemann | f77e7dc | 2015-08-10 16:39:39 -0700 | [diff] [blame] | 178 | if volume_status == 'error_restoring': |
| 179 | raise exceptions.VolumeRestoreErrorException(volume_id=volume_id) |
Ken'ichi Ohmichi | b942be6 | 2015-07-08 08:16:12 +0000 | [diff] [blame] | 180 | |
| 181 | if int(time.time()) - start >= client.build_timeout: |
| 182 | message = ('Volume %s failed to reach %s status (current %s) ' |
| 183 | 'within the required time (%s s).' % |
| 184 | (volume_id, status, volume_status, |
| 185 | client.build_timeout)) |
| 186 | raise exceptions.TimeoutException(message) |
| 187 | |
| 188 | |
Gaozexu | b9c9d6e | 2015-09-10 17:08:04 +0800 | [diff] [blame] | 189 | def wait_for_snapshot_status(client, snapshot_id, status): |
| 190 | """Waits for a Snapshot to reach a given status.""" |
| 191 | body = client.show_snapshot(snapshot_id)['snapshot'] |
| 192 | snapshot_status = body['status'] |
| 193 | start = int(time.time()) |
| 194 | |
| 195 | while snapshot_status != status: |
| 196 | time.sleep(client.build_interval) |
| 197 | body = client.show_snapshot(snapshot_id)['snapshot'] |
| 198 | snapshot_status = body['status'] |
| 199 | if snapshot_status == 'error': |
| 200 | raise exceptions.SnapshotBuildErrorException( |
| 201 | snapshot_id=snapshot_id) |
| 202 | if int(time.time()) - start >= client.build_timeout: |
| 203 | message = ('Snapshot %s failed to reach %s status (current %s) ' |
| 204 | 'within the required time (%s s).' % |
| 205 | (snapshot_id, status, snapshot_status, |
| 206 | client.build_timeout)) |
| 207 | raise exceptions.TimeoutException(message) |
| 208 | |
| 209 | |
Adam Gandelman | 0068261 | 2014-09-02 17:10:36 -0700 | [diff] [blame] | 210 | def wait_for_bm_node_status(client, node_id, attr, status): |
| 211 | """Waits for a baremetal node attribute to reach given status. |
| 212 | |
| 213 | The client should have a show_node(node_uuid) method to get the node. |
| 214 | """ |
| 215 | _, node = client.show_node(node_id) |
| 216 | start = int(time.time()) |
| 217 | |
| 218 | while node[attr] != status: |
| 219 | time.sleep(client.build_interval) |
| 220 | _, node = client.show_node(node_id) |
Martin Pavlasek | 1102c3a | 2014-10-20 17:17:55 +0200 | [diff] [blame] | 221 | status_curr = node[attr] |
| 222 | if status_curr == status: |
Adam Gandelman | 0068261 | 2014-09-02 17:10:36 -0700 | [diff] [blame] | 223 | return |
| 224 | |
| 225 | if int(time.time()) - start >= client.build_timeout: |
| 226 | message = ('Node %(node_id)s failed to reach %(attr)s=%(status)s ' |
| 227 | 'within the required time (%(timeout)s s).' % |
| 228 | {'node_id': node_id, |
| 229 | 'attr': attr, |
| 230 | 'status': status, |
| 231 | 'timeout': client.build_timeout}) |
Martin Pavlasek | 1102c3a | 2014-10-20 17:17:55 +0200 | [diff] [blame] | 232 | message += ' Current state of %s: %s.' % (attr, status_curr) |
Adam Gandelman | 0068261 | 2014-09-02 17:10:36 -0700 | [diff] [blame] | 233 | caller = misc_utils.find_test_caller() |
| 234 | if caller: |
| 235 | message = '(%s) %s' % (caller, message) |
| 236 | raise exceptions.TimeoutException(message) |