Attila Fazekas | a23f500 | 2012-10-23 19:32:45 +0200 | [diff] [blame] | 1 | # vim: tabstop=4 shiftwidth=4 softtabstop=4 |
| 2 | |
| 3 | # Copyright 2012 OpenStack, LLC |
| 4 | # All Rights Reserved. |
| 5 | # |
| 6 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 7 | # not use this file except in compliance with the License. You may obtain |
| 8 | # a copy of the License at |
| 9 | # |
| 10 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | # |
| 12 | # Unless required by applicable law or agreed to in writing, software |
| 13 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 14 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 15 | # License for the specific language governing permissions and limitations |
| 16 | # under the License. |
| 17 | |
Attila Fazekas | a23f500 | 2012-10-23 19:32:45 +0200 | [diff] [blame] | 18 | import re |
Matthew Treinish | a83a16e | 2012-12-07 13:44:02 -0500 | [diff] [blame] | 19 | import time |
| 20 | |
Monty Taylor | b2ca5ca | 2013-04-28 18:00:21 -0700 | [diff] [blame] | 21 | import boto.exception |
ivan-zhu | 1feeb38 | 2013-01-24 10:14:39 +0800 | [diff] [blame] | 22 | from testtools import TestCase |
Matthew Treinish | a83a16e | 2012-12-07 13:44:02 -0500 | [diff] [blame] | 23 | |
| 24 | import tempest.config |
Matthew Treinish | f4a9b0f | 2013-07-26 16:58:26 -0400 | [diff] [blame] | 25 | from tempest.openstack.common import log as logging |
Attila Fazekas | a23f500 | 2012-10-23 19:32:45 +0200 | [diff] [blame] | 26 | |
| 27 | LOG = logging.getLogger(__name__) |
| 28 | |
| 29 | _boto_config = tempest.config.TempestConfig().boto |
| 30 | |
| 31 | default_timeout = _boto_config.build_timeout |
| 32 | |
| 33 | default_check_interval = _boto_config.build_interval |
| 34 | |
| 35 | |
| 36 | def state_wait(lfunction, final_set=set(), valid_set=None): |
Attila Fazekas | 3e381f7 | 2013-08-01 16:52:23 +0200 | [diff] [blame] | 37 | # TODO(afazekas): evaluate using ABC here |
Attila Fazekas | a23f500 | 2012-10-23 19:32:45 +0200 | [diff] [blame] | 38 | if not isinstance(final_set, set): |
| 39 | final_set = set((final_set,)) |
| 40 | if not isinstance(valid_set, set) and valid_set is not None: |
| 41 | valid_set = set((valid_set,)) |
| 42 | start_time = time.time() |
| 43 | old_status = status = lfunction() |
| 44 | while True: |
| 45 | if status != old_status: |
| 46 | LOG.info('State transition "%s" ==> "%s" %d second', old_status, |
| 47 | status, time.time() - start_time) |
| 48 | if status in final_set: |
| 49 | return status |
| 50 | if valid_set is not None and status not in valid_set: |
| 51 | return status |
| 52 | dtime = time.time() - start_time |
| 53 | if dtime > default_timeout: |
| 54 | raise TestCase.failureException("State change timeout exceeded!" |
| 55 | '(%ds) While waiting' |
| 56 | 'for %s at "%s"' % |
| 57 | (dtime, |
| 58 | final_set, status)) |
| 59 | time.sleep(default_check_interval) |
| 60 | old_status = status |
| 61 | status = lfunction() |
| 62 | |
| 63 | |
| 64 | def re_search_wait(lfunction, regexp): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 65 | """Stops waiting on success.""" |
Attila Fazekas | a23f500 | 2012-10-23 19:32:45 +0200 | [diff] [blame] | 66 | start_time = time.time() |
| 67 | while True: |
| 68 | text = lfunction() |
| 69 | result = re.search(regexp, text) |
| 70 | if result is not None: |
| 71 | LOG.info('Pattern "%s" found in %d second in "%s"', |
| 72 | regexp, |
| 73 | time.time() - start_time, |
| 74 | text) |
| 75 | return result |
| 76 | dtime = time.time() - start_time |
| 77 | if dtime > default_timeout: |
| 78 | raise TestCase.failureException('Pattern find timeout exceeded!' |
| 79 | '(%ds) While waiting for' |
| 80 | '"%s" pattern in "%s"' % |
| 81 | (dtime, |
| 82 | regexp, text)) |
| 83 | time.sleep(default_check_interval) |
| 84 | |
| 85 | |
| 86 | def wait_no_exception(lfunction, exc_class=None, exc_matcher=None): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 87 | """Stops waiting on success.""" |
Attila Fazekas | a23f500 | 2012-10-23 19:32:45 +0200 | [diff] [blame] | 88 | start_time = time.time() |
| 89 | if exc_matcher is not None: |
Monty Taylor | b2ca5ca | 2013-04-28 18:00:21 -0700 | [diff] [blame] | 90 | exc_class = boto.exception.BotoServerError |
Attila Fazekas | a23f500 | 2012-10-23 19:32:45 +0200 | [diff] [blame] | 91 | |
| 92 | if exc_class is None: |
| 93 | exc_class = BaseException |
| 94 | while True: |
| 95 | result = None |
| 96 | try: |
| 97 | result = lfunction() |
| 98 | LOG.info('No Exception in %d second', |
| 99 | time.time() - start_time) |
| 100 | return result |
| 101 | except exc_class as exc: |
| 102 | if exc_matcher is not None: |
| 103 | res = exc_matcher.match(exc) |
| 104 | if res is not None: |
| 105 | LOG.info(res) |
| 106 | raise exc |
| 107 | # Let the other exceptions propagate |
| 108 | dtime = time.time() - start_time |
| 109 | if dtime > default_timeout: |
| 110 | raise TestCase.failureException("Wait timeout exceeded! (%ds)" % |
| 111 | dtime) |
| 112 | time.sleep(default_check_interval) |
| 113 | |
| 114 | |
Attila Fazekas | 3e381f7 | 2013-08-01 16:52:23 +0200 | [diff] [blame] | 115 | # NOTE(afazekas): EC2/boto normally raise exception instead of empty list |
Attila Fazekas | a23f500 | 2012-10-23 19:32:45 +0200 | [diff] [blame] | 116 | def wait_exception(lfunction): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 117 | """Returns with the exception or raises one.""" |
Attila Fazekas | a23f500 | 2012-10-23 19:32:45 +0200 | [diff] [blame] | 118 | start_time = time.time() |
| 119 | while True: |
| 120 | try: |
| 121 | lfunction() |
| 122 | except BaseException as exc: |
| 123 | LOG.info('Exception in %d second', |
| 124 | time.time() - start_time) |
| 125 | return exc |
| 126 | dtime = time.time() - start_time |
| 127 | if dtime > default_timeout: |
| 128 | raise TestCase.failureException("Wait timeout exceeded! (%ds)" % |
| 129 | dtime) |
| 130 | time.sleep(default_check_interval) |
| 131 | |
Attila Fazekas | 3e381f7 | 2013-08-01 16:52:23 +0200 | [diff] [blame] | 132 | # TODO(afazekas): consider strategy design pattern.. |