blob: 1507deb13b8c49a4cd761449f4474acd73d78cf0 [file] [log] [blame]
Attila Fazekasa23f5002012-10-23 19:32:45 +02001# 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 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
24import tempest.config
Matthew Treinishf4a9b0f2013-07-26 16:58:26 -040025from tempest.openstack.common import log as logging
Attila Fazekasa23f5002012-10-23 19:32:45 +020026
27LOG = logging.getLogger(__name__)
28
29_boto_config = tempest.config.TempestConfig().boto
30
31default_timeout = _boto_config.build_timeout
32
33default_check_interval = _boto_config.build_interval
34
35
36def state_wait(lfunction, final_set=set(), valid_set=None):
Attila Fazekas3e381f72013-08-01 16:52:23 +020037 # TODO(afazekas): evaluate using ABC here
Attila Fazekasa23f5002012-10-23 19:32:45 +020038 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
64def re_search_wait(lfunction, regexp):
Sean Daguef237ccb2013-01-04 15:19:14 -050065 """Stops waiting on success."""
Attila Fazekasa23f5002012-10-23 19:32:45 +020066 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
86def wait_no_exception(lfunction, exc_class=None, exc_matcher=None):
Sean Daguef237ccb2013-01-04 15:19:14 -050087 """Stops waiting on success."""
Attila Fazekasa23f5002012-10-23 19:32:45 +020088 start_time = time.time()
89 if exc_matcher is not None:
Monty Taylorb2ca5ca2013-04-28 18:00:21 -070090 exc_class = boto.exception.BotoServerError
Attila Fazekasa23f5002012-10-23 19:32:45 +020091
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 Fazekas3e381f72013-08-01 16:52:23 +0200115# NOTE(afazekas): EC2/boto normally raise exception instead of empty list
Attila Fazekasa23f5002012-10-23 19:32:45 +0200116def wait_exception(lfunction):
Sean Daguef237ccb2013-01-04 15:19:14 -0500117 """Returns with the exception or raises one."""
Attila Fazekasa23f5002012-10-23 19:32:45 +0200118 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 Fazekas3e381f72013-08-01 16:52:23 +0200132# TODO(afazekas): consider strategy design pattern..