Attila Fazekas | a23f500 | 2012-10-23 19:32:45 +0200 | [diff] [blame] | 1 | # vim: tabstop=4 shiftwidth=4 softtabstop=4 |
| 2 | |
ZhiQiang Fan | 39f9722 | 2013-09-20 04:49:44 +0800 | [diff] [blame] | 3 | # Copyright 2012 OpenStack Foundation |
Attila Fazekas | a23f500 | 2012-10-23 19:32:45 +0200 | [diff] [blame] | 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 | |
Sean Dague | 86bd842 | 2013-12-20 09:56:44 -0500 | [diff] [blame] | 24 | from tempest import 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 | |
Sean Dague | 86bd842 | 2013-12-20 09:56:44 -0500 | [diff] [blame] | 27 | CONF = config.CONF |
Attila Fazekas | a23f500 | 2012-10-23 19:32:45 +0200 | [diff] [blame] | 28 | LOG = logging.getLogger(__name__) |
| 29 | |
Attila Fazekas | a23f500 | 2012-10-23 19:32:45 +0200 | [diff] [blame] | 30 | |
| 31 | def state_wait(lfunction, final_set=set(), valid_set=None): |
Attila Fazekas | 3e381f7 | 2013-08-01 16:52:23 +0200 | [diff] [blame] | 32 | # TODO(afazekas): evaluate using ABC here |
Attila Fazekas | a23f500 | 2012-10-23 19:32:45 +0200 | [diff] [blame] | 33 | if not isinstance(final_set, set): |
| 34 | final_set = set((final_set,)) |
| 35 | if not isinstance(valid_set, set) and valid_set is not None: |
| 36 | valid_set = set((valid_set,)) |
| 37 | start_time = time.time() |
| 38 | old_status = status = lfunction() |
| 39 | while True: |
| 40 | if status != old_status: |
| 41 | LOG.info('State transition "%s" ==> "%s" %d second', old_status, |
| 42 | status, time.time() - start_time) |
| 43 | if status in final_set: |
| 44 | return status |
| 45 | if valid_set is not None and status not in valid_set: |
| 46 | return status |
| 47 | dtime = time.time() - start_time |
Sean Dague | 86bd842 | 2013-12-20 09:56:44 -0500 | [diff] [blame] | 48 | if dtime > CONF.boto.build_timeout: |
Attila Fazekas | a23f500 | 2012-10-23 19:32:45 +0200 | [diff] [blame] | 49 | raise TestCase.failureException("State change timeout exceeded!" |
| 50 | '(%ds) While waiting' |
| 51 | 'for %s at "%s"' % |
DennyZhang | b432bac | 2013-09-17 16:24:12 +0000 | [diff] [blame] | 52 | (dtime, final_set, status)) |
Sean Dague | 86bd842 | 2013-12-20 09:56:44 -0500 | [diff] [blame] | 53 | time.sleep(CONF.boto.build_interval) |
Attila Fazekas | a23f500 | 2012-10-23 19:32:45 +0200 | [diff] [blame] | 54 | old_status = status |
| 55 | status = lfunction() |
| 56 | |
| 57 | |
| 58 | def re_search_wait(lfunction, regexp): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 59 | """Stops waiting on success.""" |
Attila Fazekas | a23f500 | 2012-10-23 19:32:45 +0200 | [diff] [blame] | 60 | start_time = time.time() |
| 61 | while True: |
| 62 | text = lfunction() |
| 63 | result = re.search(regexp, text) |
| 64 | if result is not None: |
| 65 | LOG.info('Pattern "%s" found in %d second in "%s"', |
| 66 | regexp, |
| 67 | time.time() - start_time, |
| 68 | text) |
| 69 | return result |
| 70 | dtime = time.time() - start_time |
Sean Dague | 86bd842 | 2013-12-20 09:56:44 -0500 | [diff] [blame] | 71 | if dtime > CONF.boto.build_timeout: |
Attila Fazekas | a23f500 | 2012-10-23 19:32:45 +0200 | [diff] [blame] | 72 | raise TestCase.failureException('Pattern find timeout exceeded!' |
| 73 | '(%ds) While waiting for' |
| 74 | '"%s" pattern in "%s"' % |
DennyZhang | b432bac | 2013-09-17 16:24:12 +0000 | [diff] [blame] | 75 | (dtime, regexp, text)) |
Sean Dague | 86bd842 | 2013-12-20 09:56:44 -0500 | [diff] [blame] | 76 | time.sleep(CONF.boto.build_interval) |
Attila Fazekas | a23f500 | 2012-10-23 19:32:45 +0200 | [diff] [blame] | 77 | |
| 78 | |
| 79 | def wait_no_exception(lfunction, exc_class=None, exc_matcher=None): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 80 | """Stops waiting on success.""" |
Attila Fazekas | a23f500 | 2012-10-23 19:32:45 +0200 | [diff] [blame] | 81 | start_time = time.time() |
| 82 | if exc_matcher is not None: |
Monty Taylor | b2ca5ca | 2013-04-28 18:00:21 -0700 | [diff] [blame] | 83 | exc_class = boto.exception.BotoServerError |
Attila Fazekas | a23f500 | 2012-10-23 19:32:45 +0200 | [diff] [blame] | 84 | |
| 85 | if exc_class is None: |
| 86 | exc_class = BaseException |
| 87 | while True: |
| 88 | result = None |
| 89 | try: |
| 90 | result = lfunction() |
| 91 | LOG.info('No Exception in %d second', |
| 92 | time.time() - start_time) |
| 93 | return result |
| 94 | except exc_class as exc: |
| 95 | if exc_matcher is not None: |
| 96 | res = exc_matcher.match(exc) |
| 97 | if res is not None: |
| 98 | LOG.info(res) |
| 99 | raise exc |
| 100 | # Let the other exceptions propagate |
| 101 | dtime = time.time() - start_time |
Sean Dague | 86bd842 | 2013-12-20 09:56:44 -0500 | [diff] [blame] | 102 | if dtime > CONF.boto.build_timeout: |
Attila Fazekas | a23f500 | 2012-10-23 19:32:45 +0200 | [diff] [blame] | 103 | raise TestCase.failureException("Wait timeout exceeded! (%ds)" % |
| 104 | dtime) |
Sean Dague | 86bd842 | 2013-12-20 09:56:44 -0500 | [diff] [blame] | 105 | time.sleep(CONF.boto.build_interval) |
Attila Fazekas | a23f500 | 2012-10-23 19:32:45 +0200 | [diff] [blame] | 106 | |
| 107 | |
Attila Fazekas | 3e381f7 | 2013-08-01 16:52:23 +0200 | [diff] [blame] | 108 | # NOTE(afazekas): EC2/boto normally raise exception instead of empty list |
Attila Fazekas | a23f500 | 2012-10-23 19:32:45 +0200 | [diff] [blame] | 109 | def wait_exception(lfunction): |
Sean Dague | f237ccb | 2013-01-04 15:19:14 -0500 | [diff] [blame] | 110 | """Returns with the exception or raises one.""" |
Attila Fazekas | a23f500 | 2012-10-23 19:32:45 +0200 | [diff] [blame] | 111 | start_time = time.time() |
| 112 | while True: |
| 113 | try: |
| 114 | lfunction() |
| 115 | except BaseException as exc: |
| 116 | LOG.info('Exception in %d second', |
| 117 | time.time() - start_time) |
| 118 | return exc |
| 119 | dtime = time.time() - start_time |
Sean Dague | 86bd842 | 2013-12-20 09:56:44 -0500 | [diff] [blame] | 120 | if dtime > CONF.boto.build_timeout: |
Attila Fazekas | a23f500 | 2012-10-23 19:32:45 +0200 | [diff] [blame] | 121 | raise TestCase.failureException("Wait timeout exceeded! (%ds)" % |
| 122 | dtime) |
Sean Dague | 86bd842 | 2013-12-20 09:56:44 -0500 | [diff] [blame] | 123 | time.sleep(CONF.boto.build_interval) |
Attila Fazekas | a23f500 | 2012-10-23 19:32:45 +0200 | [diff] [blame] | 124 | |
Attila Fazekas | 3e381f7 | 2013-08-01 16:52:23 +0200 | [diff] [blame] | 125 | # TODO(afazekas): consider strategy design pattern.. |