blob: db2303a60bb9eea4774d2c4a62dd50e2b1d3aa0d [file] [log] [blame]
Attila Fazekasa23f5002012-10-23 19:32:45 +02001# vim: tabstop=4 shiftwidth=4 softtabstop=4
2
ZhiQiang Fan39f97222013-09-20 04:49:44 +08003# Copyright 2012 OpenStack Foundation
Attila Fazekasa23f5002012-10-23 19:32:45 +02004# 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 Fazekasa23f5002012-10-23 19:32:45 +020018import re
Matthew Treinisha83a16e2012-12-07 13:44:02 -050019import time
20
Monty Taylorb2ca5ca2013-04-28 18:00:21 -070021import boto.exception
ivan-zhu1feeb382013-01-24 10:14:39 +080022from testtools import TestCase
Matthew Treinisha83a16e2012-12-07 13:44:02 -050023
Sean Dague86bd8422013-12-20 09:56:44 -050024from tempest import config
Matthew Treinishf4a9b0f2013-07-26 16:58:26 -040025from tempest.openstack.common import log as logging
Attila Fazekasa23f5002012-10-23 19:32:45 +020026
Sean Dague86bd8422013-12-20 09:56:44 -050027CONF = config.CONF
Attila Fazekasa23f5002012-10-23 19:32:45 +020028LOG = logging.getLogger(__name__)
29
Attila Fazekasa23f5002012-10-23 19:32:45 +020030
31def state_wait(lfunction, final_set=set(), valid_set=None):
Attila Fazekas3e381f72013-08-01 16:52:23 +020032 # TODO(afazekas): evaluate using ABC here
Attila Fazekasa23f5002012-10-23 19:32:45 +020033 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 Dague86bd8422013-12-20 09:56:44 -050048 if dtime > CONF.boto.build_timeout:
Attila Fazekasa23f5002012-10-23 19:32:45 +020049 raise TestCase.failureException("State change timeout exceeded!"
50 '(%ds) While waiting'
51 'for %s at "%s"' %
DennyZhangb432bac2013-09-17 16:24:12 +000052 (dtime, final_set, status))
Sean Dague86bd8422013-12-20 09:56:44 -050053 time.sleep(CONF.boto.build_interval)
Attila Fazekasa23f5002012-10-23 19:32:45 +020054 old_status = status
55 status = lfunction()
56
57
58def re_search_wait(lfunction, regexp):
Sean Daguef237ccb2013-01-04 15:19:14 -050059 """Stops waiting on success."""
Attila Fazekasa23f5002012-10-23 19:32:45 +020060 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 Dague86bd8422013-12-20 09:56:44 -050071 if dtime > CONF.boto.build_timeout:
Attila Fazekasa23f5002012-10-23 19:32:45 +020072 raise TestCase.failureException('Pattern find timeout exceeded!'
73 '(%ds) While waiting for'
74 '"%s" pattern in "%s"' %
DennyZhangb432bac2013-09-17 16:24:12 +000075 (dtime, regexp, text))
Sean Dague86bd8422013-12-20 09:56:44 -050076 time.sleep(CONF.boto.build_interval)
Attila Fazekasa23f5002012-10-23 19:32:45 +020077
78
79def wait_no_exception(lfunction, exc_class=None, exc_matcher=None):
Sean Daguef237ccb2013-01-04 15:19:14 -050080 """Stops waiting on success."""
Attila Fazekasa23f5002012-10-23 19:32:45 +020081 start_time = time.time()
82 if exc_matcher is not None:
Monty Taylorb2ca5ca2013-04-28 18:00:21 -070083 exc_class = boto.exception.BotoServerError
Attila Fazekasa23f5002012-10-23 19:32:45 +020084
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 Dague86bd8422013-12-20 09:56:44 -0500102 if dtime > CONF.boto.build_timeout:
Attila Fazekasa23f5002012-10-23 19:32:45 +0200103 raise TestCase.failureException("Wait timeout exceeded! (%ds)" %
104 dtime)
Sean Dague86bd8422013-12-20 09:56:44 -0500105 time.sleep(CONF.boto.build_interval)
Attila Fazekasa23f5002012-10-23 19:32:45 +0200106
107
Attila Fazekas3e381f72013-08-01 16:52:23 +0200108# NOTE(afazekas): EC2/boto normally raise exception instead of empty list
Attila Fazekasa23f5002012-10-23 19:32:45 +0200109def wait_exception(lfunction):
Sean Daguef237ccb2013-01-04 15:19:14 -0500110 """Returns with the exception or raises one."""
Attila Fazekasa23f5002012-10-23 19:32:45 +0200111 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 Dague86bd8422013-12-20 09:56:44 -0500120 if dtime > CONF.boto.build_timeout:
Attila Fazekasa23f5002012-10-23 19:32:45 +0200121 raise TestCase.failureException("Wait timeout exceeded! (%ds)" %
122 dtime)
Sean Dague86bd8422013-12-20 09:56:44 -0500123 time.sleep(CONF.boto.build_interval)
Attila Fazekasa23f5002012-10-23 19:32:45 +0200124
Attila Fazekas3e381f72013-08-01 16:52:23 +0200125# TODO(afazekas): consider strategy design pattern..