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