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 | |
Matt Riedemann | 128c23e | 2014-05-02 13:46:39 -0700 | [diff] [blame] | 16 | from tempest.common.utils import misc as misc_utils |
Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 17 | from tempest import config |
| 18 | from tempest import exceptions |
| 19 | from tempest.openstack.common import log as logging |
| 20 | |
Sean Dague | 86bd842 | 2013-12-20 09:56:44 -0500 | [diff] [blame] | 21 | CONF = config.CONF |
Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 22 | LOG = logging.getLogger(__name__) |
| 23 | |
| 24 | |
| 25 | # 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] | 26 | 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] | 27 | extra_timeout=0, raise_on_error=True): |
Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 28 | """Waits for a server to reach a given status.""" |
| 29 | |
| 30 | def _get_task_state(body): |
ivan-zhu | 2ca89b3 | 2013-08-07 22:37:32 +0800 | [diff] [blame] | 31 | if client.service == CONF.compute.catalog_v3_type: |
| 32 | task_state = body.get("os-extended-status:task_state", None) |
| 33 | else: |
| 34 | task_state = body.get('OS-EXT-STS:task_state', None) |
Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 35 | return task_state |
| 36 | |
| 37 | # NOTE(afazekas): UNKNOWN status possible on ERROR |
| 38 | # or in a very early stage. |
| 39 | resp, body = client.get_server(server_id) |
| 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 |
| 56 | # NOTE(afazekas): Converted to string bacuse of the XML |
| 57 | # responses |
| 58 | if str(task_state) == "None": |
| 59 | # without state api extension 3 sec usually enough |
Sean Dague | 86bd842 | 2013-12-20 09:56:44 -0500 | [diff] [blame] | 60 | time.sleep(CONF.compute.ready_wait) |
Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 61 | return |
| 62 | else: |
| 63 | return |
| 64 | |
| 65 | time.sleep(client.build_interval) |
| 66 | resp, body = client.get_server(server_id) |
| 67 | server_status = body['status'] |
| 68 | task_state = _get_task_state(body) |
| 69 | if (server_status != old_status) or (task_state != old_task_state): |
| 70 | LOG.info('State transition "%s" ==> "%s" after %d second wait', |
| 71 | '/'.join((old_status, str(old_task_state))), |
| 72 | '/'.join((server_status, str(task_state))), |
| 73 | time.time() - start_time) |
Zhi Kun Liu | e540176 | 2013-09-11 20:45:48 +0800 | [diff] [blame] | 74 | if (server_status == 'ERROR') and raise_on_error: |
Attila Fazekas | 0462a7f | 2014-06-20 07:38:06 +0200 | [diff] [blame] | 75 | if 'fault' in body: |
| 76 | raise exceptions.BuildErrorException(body['fault'], |
| 77 | server_id=server_id) |
| 78 | else: |
| 79 | raise exceptions.BuildErrorException(server_id=server_id) |
Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 80 | |
Ken'ichi Ohmichi | 39437e2 | 2013-10-06 00:21:38 +0900 | [diff] [blame] | 81 | timed_out = int(time.time()) - start_time >= timeout |
Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 82 | |
| 83 | if timed_out: |
Matt Riedemann | 629fa7c | 2013-12-11 18:20:56 -0800 | [diff] [blame] | 84 | expected_task_state = 'None' if ready_wait else 'n/a' |
| 85 | message = ('Server %(server_id)s failed to reach %(status)s ' |
| 86 | 'status and task state "%(expected_task_state)s" ' |
| 87 | 'within the required time (%(timeout)s s).' % |
| 88 | {'server_id': server_id, |
| 89 | 'status': status, |
| 90 | 'expected_task_state': expected_task_state, |
| 91 | 'timeout': timeout}) |
Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 92 | message += ' Current status: %s.' % server_status |
Matt Riedemann | 629fa7c | 2013-12-11 18:20:56 -0800 | [diff] [blame] | 93 | message += ' Current task state: %s.' % task_state |
Matt Riedemann | 128c23e | 2014-05-02 13:46:39 -0700 | [diff] [blame] | 94 | caller = misc_utils.find_test_caller() |
| 95 | if caller: |
| 96 | message = '(%s) %s' % (caller, message) |
Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 97 | raise exceptions.TimeoutException(message) |
| 98 | old_status = server_status |
| 99 | old_task_state = task_state |
Matt Riedemann | c00f326 | 2013-12-14 12:03:55 -0800 | [diff] [blame] | 100 | |
| 101 | |
| 102 | def wait_for_image_status(client, image_id, status): |
| 103 | """Waits for an image to reach a given status. |
| 104 | |
| 105 | The client should have a get_image(image_id) method to get the image. |
| 106 | The client should also have build_interval and build_timeout attributes. |
| 107 | """ |
| 108 | resp, image = client.get_image(image_id) |
| 109 | start = int(time.time()) |
| 110 | |
| 111 | while image['status'] != status: |
| 112 | time.sleep(client.build_interval) |
| 113 | resp, image = client.get_image(image_id) |
| 114 | if image['status'] == 'ERROR': |
| 115 | raise exceptions.AddImageException(image_id=image_id) |
| 116 | |
| 117 | # check the status again to avoid a false negative where we hit |
| 118 | # the timeout at the same time that the image reached the expected |
| 119 | # status |
| 120 | if image['status'] == status: |
| 121 | return |
| 122 | |
| 123 | if int(time.time()) - start >= client.build_timeout: |
| 124 | message = ('Image %(image_id)s failed to reach %(status)s ' |
| 125 | 'status within the required time (%(timeout)s s).' % |
| 126 | {'image_id': image_id, |
| 127 | 'status': status, |
| 128 | 'timeout': client.build_timeout}) |
| 129 | message += ' Current status: %s.' % image['status'] |
Matt Riedemann | 128c23e | 2014-05-02 13:46:39 -0700 | [diff] [blame] | 130 | caller = misc_utils.find_test_caller() |
| 131 | if caller: |
| 132 | message = '(%s) %s' % (caller, message) |
Matt Riedemann | c00f326 | 2013-12-14 12:03:55 -0800 | [diff] [blame] | 133 | raise exceptions.TimeoutException(message) |