Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 1 | # vim: tabstop=4 shiftwidth=4 softtabstop=4 |
| 2 | |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | # not use this file except in compliance with the License. You may obtain |
| 5 | # a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | # License for the specific language governing permissions and limitations |
| 13 | # under the License. |
| 14 | |
| 15 | |
| 16 | import time |
| 17 | |
| 18 | from tempest import config |
| 19 | from tempest import exceptions |
| 20 | from tempest.openstack.common import log as logging |
| 21 | |
| 22 | CONFIG = config.TempestConfig() |
| 23 | LOG = logging.getLogger(__name__) |
| 24 | |
| 25 | |
| 26 | # 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] | 27 | def wait_for_server_status(client, server_id, status, ready_wait=True, |
| 28 | extra_timeout=0): |
Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 29 | """Waits for a server to reach a given status.""" |
| 30 | |
| 31 | def _get_task_state(body): |
| 32 | task_state = body.get('OS-EXT-STS:task_state', None) |
| 33 | return task_state |
| 34 | |
| 35 | # NOTE(afazekas): UNKNOWN status possible on ERROR |
| 36 | # or in a very early stage. |
| 37 | resp, body = client.get_server(server_id) |
| 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 |
| 44 | # between the UNKOWN->ACTIVE transition. |
| 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 |
| 58 | time.sleep(CONFIG.compute.ready_wait) |
| 59 | return |
| 60 | else: |
| 61 | return |
| 62 | |
| 63 | time.sleep(client.build_interval) |
| 64 | resp, body = client.get_server(server_id) |
| 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) |
| 72 | if server_status == 'ERROR': |
| 73 | raise exceptions.BuildErrorException(server_id=server_id) |
| 74 | |
Ken'ichi Ohmichi | 39437e2 | 2013-10-06 00:21:38 +0900 | [diff] [blame] | 75 | timed_out = int(time.time()) - start_time >= timeout |
Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 76 | |
| 77 | if timed_out: |
| 78 | message = ('Server %s failed to reach %s status within the ' |
| 79 | 'required time (%s s).' % |
Ken'ichi Ohmichi | 39437e2 | 2013-10-06 00:21:38 +0900 | [diff] [blame] | 80 | (server_id, status, timeout)) |
Attila Fazekas | 0abbc95 | 2013-07-01 19:19:42 +0200 | [diff] [blame] | 81 | message += ' Current status: %s.' % server_status |
| 82 | raise exceptions.TimeoutException(message) |
| 83 | old_status = server_status |
| 84 | old_task_state = task_state |