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 | from tempest_lib.common.utils import misc as misc_utils |
Ken'ichi Ohmichi | e91a0c6 | 2015-08-13 02:09:16 +0000 | [diff] [blame] | 18 | from tempest_lib import exceptions as lib_exc |
Matthew Treinish | 01472ff | 2015-02-20 17:26:52 -0500 | [diff] [blame] | 19 | |
Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 20 | from tempest import config |
| 21 | from tempest import exceptions |
Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 22 | |
Sean Dague | 86bd842 | 2013-12-20 09:56:44 -0500 | [diff] [blame] | 23 | CONF = config.CONF |
Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 24 | LOG = logging.getLogger(__name__) |
| 25 | |
| 26 | |
| 27 | # 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] | 28 | 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] | 29 | extra_timeout=0, raise_on_error=True): |
Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 30 | """Waits for a server to reach a given status.""" |
| 31 | |
| 32 | def _get_task_state(body): |
Ken'ichi Ohmichi | a58c156 | 2014-12-15 00:39:55 +0000 | [diff] [blame] | 33 | return body.get('OS-EXT-STS:task_state', None) |
Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 34 | |
| 35 | # NOTE(afazekas): UNKNOWN status possible on ERROR |
| 36 | # or in a very early stage. |
Ken'ichi Ohmichi | 7680024 | 2015-07-03 05:12:31 +0000 | [diff] [blame] | 37 | body = client.show_server(server_id) |
Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 38 | old_status = server_status = body['status'] |
| 39 | old_task_state = task_state = _get_task_state(body) |
| 40 | start_time = int(time.time()) |
Ken'ichi Ohmichi | 39437e2 | 2013-10-06 00:21:38 +0900 | [diff] [blame] | 41 | timeout = client.build_timeout + extra_timeout |
Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 42 | while True: |
| 43 | # NOTE(afazekas): Now the BUILD status only reached |
Shane Wang | 111f86c | 2014-02-20 16:40:22 +0800 | [diff] [blame] | 44 | # between the UNKNOWN->ACTIVE transition. |
Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 45 | # TODO(afazekas): enumerate and validate the stable status set |
| 46 | if status == 'BUILD' and server_status != 'UNKNOWN': |
| 47 | return |
| 48 | if server_status == status: |
| 49 | if ready_wait: |
| 50 | if status == 'BUILD': |
| 51 | return |
| 52 | # NOTE(afazekas): The instance is in "ready for action state" |
| 53 | # when no task in progress |
| 54 | # NOTE(afazekas): Converted to string bacuse of the XML |
| 55 | # responses |
| 56 | if str(task_state) == "None": |
| 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) |
Ken'ichi Ohmichi | 7680024 | 2015-07-03 05:12:31 +0000 | [diff] [blame] | 64 | body = client.show_server(server_id) |
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 |
Matt Riedemann | 128c23e | 2014-05-02 13:46:39 -0700 | [diff] [blame] | 92 | caller = misc_utils.find_test_caller() |
| 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: |
| 105 | body = client.show_server(server_id) |
| 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 | """ |
Ken'ichi Ohmichi | 5d41076 | 2015-05-22 01:10:03 +0000 | [diff] [blame] | 125 | image = client.show_image(image_id) |
Matt Riedemann | c00f326 | 2013-12-14 12:03:55 -0800 | [diff] [blame] | 126 | start = int(time.time()) |
| 127 | |
| 128 | while image['status'] != status: |
| 129 | time.sleep(client.build_interval) |
Ken'ichi Ohmichi | 5d41076 | 2015-05-22 01:10:03 +0000 | [diff] [blame] | 130 | image = client.show_image(image_id) |
Martin Pavlasek | 1102c3a | 2014-10-20 17:17:55 +0200 | [diff] [blame] | 131 | status_curr = image['status'] |
| 132 | if status_curr == 'ERROR': |
Matt Riedemann | c00f326 | 2013-12-14 12:03:55 -0800 | [diff] [blame] | 133 | raise exceptions.AddImageException(image_id=image_id) |
| 134 | |
| 135 | # check the status again to avoid a false negative where we hit |
| 136 | # the timeout at the same time that the image reached the expected |
| 137 | # status |
Martin Pavlasek | 1102c3a | 2014-10-20 17:17:55 +0200 | [diff] [blame] | 138 | if status_curr == status: |
Matt Riedemann | c00f326 | 2013-12-14 12:03:55 -0800 | [diff] [blame] | 139 | return |
| 140 | |
| 141 | if int(time.time()) - start >= client.build_timeout: |
Martin Pavlasek | 1102c3a | 2014-10-20 17:17:55 +0200 | [diff] [blame] | 142 | message = ('Image %(image_id)s failed to reach %(status)s state' |
| 143 | '(current state %(status_curr)s) ' |
| 144 | 'within the required time (%(timeout)s s).' % |
Matt Riedemann | c00f326 | 2013-12-14 12:03:55 -0800 | [diff] [blame] | 145 | {'image_id': image_id, |
| 146 | 'status': status, |
Martin Pavlasek | 1102c3a | 2014-10-20 17:17:55 +0200 | [diff] [blame] | 147 | 'status_curr': status_curr, |
Matt Riedemann | c00f326 | 2013-12-14 12:03:55 -0800 | [diff] [blame] | 148 | 'timeout': client.build_timeout}) |
Matt Riedemann | 128c23e | 2014-05-02 13:46:39 -0700 | [diff] [blame] | 149 | caller = misc_utils.find_test_caller() |
| 150 | if caller: |
| 151 | message = '(%s) %s' % (caller, message) |
Matt Riedemann | c00f326 | 2013-12-14 12:03:55 -0800 | [diff] [blame] | 152 | raise exceptions.TimeoutException(message) |
Adam Gandelman | 0068261 | 2014-09-02 17:10:36 -0700 | [diff] [blame] | 153 | |
| 154 | |
Ken'ichi Ohmichi | b942be6 | 2015-07-08 08:16:12 +0000 | [diff] [blame] | 155 | def wait_for_volume_status(client, volume_id, status): |
| 156 | """Waits for a Volume to reach a given status.""" |
| 157 | body = client.show_volume(volume_id) |
| 158 | volume_status = body['status'] |
| 159 | start = int(time.time()) |
| 160 | |
| 161 | while volume_status != status: |
| 162 | time.sleep(client.build_interval) |
| 163 | body = client.show_volume(volume_id) |
| 164 | volume_status = body['status'] |
| 165 | if volume_status == 'error': |
| 166 | raise exceptions.VolumeBuildErrorException(volume_id=volume_id) |
Matt Riedemann | f77e7dc | 2015-08-10 16:39:39 -0700 | [diff] [blame] | 167 | if volume_status == 'error_restoring': |
| 168 | raise exceptions.VolumeRestoreErrorException(volume_id=volume_id) |
Ken'ichi Ohmichi | b942be6 | 2015-07-08 08:16:12 +0000 | [diff] [blame] | 169 | |
| 170 | if int(time.time()) - start >= client.build_timeout: |
| 171 | message = ('Volume %s failed to reach %s status (current %s) ' |
| 172 | 'within the required time (%s s).' % |
| 173 | (volume_id, status, volume_status, |
| 174 | client.build_timeout)) |
| 175 | raise exceptions.TimeoutException(message) |
| 176 | |
| 177 | |
Adam Gandelman | 0068261 | 2014-09-02 17:10:36 -0700 | [diff] [blame] | 178 | def wait_for_bm_node_status(client, node_id, attr, status): |
| 179 | """Waits for a baremetal node attribute to reach given status. |
| 180 | |
| 181 | The client should have a show_node(node_uuid) method to get the node. |
| 182 | """ |
| 183 | _, node = client.show_node(node_id) |
| 184 | start = int(time.time()) |
| 185 | |
| 186 | while node[attr] != status: |
| 187 | time.sleep(client.build_interval) |
| 188 | _, node = client.show_node(node_id) |
Martin Pavlasek | 1102c3a | 2014-10-20 17:17:55 +0200 | [diff] [blame] | 189 | status_curr = node[attr] |
| 190 | if status_curr == status: |
Adam Gandelman | 0068261 | 2014-09-02 17:10:36 -0700 | [diff] [blame] | 191 | return |
| 192 | |
| 193 | if int(time.time()) - start >= client.build_timeout: |
| 194 | message = ('Node %(node_id)s failed to reach %(attr)s=%(status)s ' |
| 195 | 'within the required time (%(timeout)s s).' % |
| 196 | {'node_id': node_id, |
| 197 | 'attr': attr, |
| 198 | 'status': status, |
| 199 | 'timeout': client.build_timeout}) |
Martin Pavlasek | 1102c3a | 2014-10-20 17:17:55 +0200 | [diff] [blame] | 200 | message += ' Current state of %s: %s.' % (attr, status_curr) |
Adam Gandelman | 0068261 | 2014-09-02 17:10:36 -0700 | [diff] [blame] | 201 | caller = misc_utils.find_test_caller() |
| 202 | if caller: |
| 203 | message = '(%s) %s' % (caller, message) |
| 204 | raise exceptions.TimeoutException(message) |