blob: 8771ed75a85e344ae1e9466bfd4e7c943966fbf5 [file] [log] [blame]
ZhiQiang Fan39f97222013-09-20 04:49:44 +08001# Copyright 2012 OpenStack Foundation
Attila Fazekasa23f5002012-10-23 19:32:45 +02002# All Rights Reserved.
3#
4# Licensed under the Apache License, Version 2.0 (the "License"); you may
5# not use this file except in compliance with the License. You may obtain
6# a copy of the License at
7#
8# http://www.apache.org/licenses/LICENSE-2.0
9#
10# Unless required by applicable law or agreed to in writing, software
11# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13# License for the specific language governing permissions and limitations
14# under the License.
15
Attila Fazekasa23f5002012-10-23 19:32:45 +020016import re
Matthew Treinisha83a16e2012-12-07 13:44:02 -050017import time
18
Monty Taylorb2ca5ca2013-04-28 18:00:21 -070019import boto.exception
Doug Hellmann583ce2c2015-03-11 14:55:46 +000020from oslo_log import log as logging
Masayuki Igawa224a8272014-02-17 15:07:43 +090021import testtools
Matthew Treinisha83a16e2012-12-07 13:44:02 -050022
Sean Dague86bd8422013-12-20 09:56:44 -050023from tempest import config
Attila Fazekasa23f5002012-10-23 19:32:45 +020024
Sean Dague86bd8422013-12-20 09:56:44 -050025CONF = config.CONF
Attila Fazekasa23f5002012-10-23 19:32:45 +020026LOG = logging.getLogger(__name__)
27
Attila Fazekasa23f5002012-10-23 19:32:45 +020028
29def state_wait(lfunction, final_set=set(), valid_set=None):
Attila Fazekas3e381f72013-08-01 16:52:23 +020030 # TODO(afazekas): evaluate using ABC here
Attila Fazekasa23f5002012-10-23 19:32:45 +020031 if not isinstance(final_set, set):
32 final_set = set((final_set,))
33 if not isinstance(valid_set, set) and valid_set is not None:
34 valid_set = set((valid_set,))
35 start_time = time.time()
36 old_status = status = lfunction()
37 while True:
38 if status != old_status:
39 LOG.info('State transition "%s" ==> "%s" %d second', old_status,
40 status, time.time() - start_time)
41 if status in final_set:
42 return status
43 if valid_set is not None and status not in valid_set:
44 return status
45 dtime = time.time() - start_time
Sean Dague86bd8422013-12-20 09:56:44 -050046 if dtime > CONF.boto.build_timeout:
Masayuki Igawa224a8272014-02-17 15:07:43 +090047 raise testtools.TestCase\
48 .failureException("State change timeout exceeded!"
49 '(%ds) While waiting'
50 'for %s at "%s"' %
51 (dtime, final_set, status))
Sean Dague86bd8422013-12-20 09:56:44 -050052 time.sleep(CONF.boto.build_interval)
Attila Fazekasa23f5002012-10-23 19:32:45 +020053 old_status = status
54 status = lfunction()
55
56
57def re_search_wait(lfunction, regexp):
Sean Daguef237ccb2013-01-04 15:19:14 -050058 """Stops waiting on success."""
Attila Fazekasa23f5002012-10-23 19:32:45 +020059 start_time = time.time()
60 while True:
61 text = lfunction()
62 result = re.search(regexp, text)
63 if result is not None:
64 LOG.info('Pattern "%s" found in %d second in "%s"',
65 regexp,
66 time.time() - start_time,
67 text)
68 return result
69 dtime = time.time() - start_time
Sean Dague86bd8422013-12-20 09:56:44 -050070 if dtime > CONF.boto.build_timeout:
Masayuki Igawa224a8272014-02-17 15:07:43 +090071 raise testtools.TestCase\
72 .failureException('Pattern find timeout exceeded!'
73 '(%ds) While waiting for'
74 '"%s" pattern in "%s"' %
75 (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:
Masayuki Igawa224a8272014-02-17 15:07:43 +0900103 raise testtools.TestCase\
104 .failureException("Wait timeout exceeded! (%ds)" % 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:
Masayuki Igawa224a8272014-02-17 15:07:43 +0900121 raise testtools.TestCase\
122 .failureException("Wait timeout exceeded! (%ds)" % 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..