| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
import time |
| 17 |
|
| 18 |
from oslo_log import log as logging |
| 19 |
from tempest import config |
| 20 |
from tempest.lib.common.utils import test_utils |
| 21 |
from tempest.lib import exceptions |
| 22 |
|
| 23 |
from octavia_tempest_plugin.common import constants as const |
| 24 |
|
| 25 |
CONF = config.CONF |
| 26 |
LOG = logging.getLogger(__name__) |
| 27 |
|
| 28 |
|
| 29 |
def wait_for_status(show_client, id, status_key, status, |
| 30 |
check_interval, check_timeout, root_tag=None, |
| 31 |
error_ok=False, **kwargs): |
| 32 |
"""Waits for an object to reach a specific status. |
| 33 |
|
| 34 |
:param show_client: The tempest service client show method. |
| 35 |
Ex. cls.os_primary.servers_client.show_server |
| 36 |
:param id: The id of the object to query. |
| 37 |
:param status_key: The key of the status field in the response. |
| 38 |
Ex. provisioning_status |
| 39 |
:param status: The status to wait for. Ex. "ACTIVE" |
| 40 |
:check_interval: How often to check the status, in seconds. |
| 41 |
:check_timeout: The maximum time, in seconds, to check the status. |
| 42 |
:root_tag: The root tag on the response to remove, if any. |
| 43 |
:error_ok: When true, ERROR status will not raise an exception. |
| 44 |
:raises CommandFailed: Raised if the object goes into ERROR and ERROR was |
| 45 |
not the desired status. |
| 46 |
:raises TimeoutException: The object did not achieve the status or ERROR in |
| 47 |
the check_timeout period. |
| 48 |
:returns: The object details from the show client. |
| 49 |
""" |
| 50 |
start = int(time.time()) |
| 51 |
LOG.info('Waiting for %s status to update to %s', |
| 52 |
show_client.__name__, status) |
| 53 |
while True: |
| 54 |
if status == const.DELETED: |
| 55 |
try: |
| 56 |
response = show_client(id, **kwargs) |
| 57 |
except exceptions.NotFound: |
| 58 |
return |
| 59 |
else: |
| 60 |
response = show_client(id, **kwargs) |
| 61 |
|
| 62 |
if root_tag: |
| 63 |
object_details = response[root_tag] |
| 64 |
else: |
| 65 |
object_details = response |
| 66 |
|
| 67 |
if object_details[status_key] == status: |
| 68 |
LOG.info('%s\'s status updated to %s.', |
| 69 |
show_client.__name__, status) |
| 70 |
return object_details |
| 71 |
elif object_details[status_key] == 'ERROR' and not error_ok: |
| 72 |
message = ('{name} {field} updated to an invalid state of ' |
| 73 |
'ERROR'.format(name=show_client.__name__, |
| 74 |
field=status_key)) |
| 75 |
caller = test_utils.find_test_caller() |
| 76 |
if caller: |
| 77 |
message = '({caller}) {message}'.format(caller=caller, |
| 78 |
message=message) |
| 79 |
raise exceptions.UnexpectedResponseCode(message) |
| 80 |
|
| 81 |
if int(time.time()) - start >= check_timeout: |
| 82 |
message = ( |
| 83 |
'{name} {field} failed to update to {expected_status} within ' |
| 84 |
'the required time {timeout}. Current status of {name}: ' |
| 85 |
'{status}'.format( |
| 86 |
name=show_client.__name__, |
| 87 |
timeout=check_timeout, |
| 88 |
status=object_details[status_key], |
| 89 |
expected_status=status, |
| 90 |
field=status_key |
| 91 |
)) |
| 92 |
caller = test_utils.find_test_caller() |
| 93 |
if caller: |
| 94 |
message = '({caller}) {message}'.format(caller=caller, |
| 95 |
message=message) |
| 96 |
raise exceptions.TimeoutException(message) |
| 97 |
|
| 98 |
time.sleep(check_interval) |
| 99 |
|
| 100 |
|
| 101 |
def wait_for_not_found(delete_func, show_func, *args, **kwargs): |
| 102 |
"""Call the delete function, then wait for it to be 'NotFound' |
| 103 |
|
| 104 |
:param delete_func: The delete function to call. |
| 105 |
:param show_func: The show function to call looking for 'NotFound'. |
| 106 |
:param ID: The ID of the object to delete/show. |
| 107 |
:raises TimeoutException: The object did not achieve the status or ERROR in |
| 108 |
the check_timeout period. |
| 109 |
:returns: None |
| 110 |
""" |
| 111 |
try: |
| 112 |
delete_func(*args, **kwargs) |
| 113 |
except exceptions.NotFound: |
| 114 |
return |
| 115 |
start = int(time.time()) |
| 116 |
LOG.info('Waiting for object to be NotFound') |
| 117 |
while True: |
| 118 |
try: |
| 119 |
show_func(*args, **kwargs) |
| 120 |
except exceptions.NotFound: |
| 121 |
return |
| 122 |
if int(time.time()) - start >= CONF.load_balancer.check_timeout: |
| 123 |
message = ('{name} did not raise NotFound in {timeout} ' |
| 124 |
'seconds.'.format( |
| 125 |
name=show_func.__name__, |
| 126 |
timeout=CONF.load_balancer.check_timeout)) |
| 127 |
raise exceptions.TimeoutException(message) |
| 128 |
time.sleep(CONF.load_balancer.check_interval) |
| 129 |
|
| 130 |
|
| 131 |
def wait_for_deleted_status_or_not_found( |
| 132 |
show_client, id, status_key, check_interval, check_timeout, |
| 133 |
root_tag=None, **kwargs): |
| 134 |
"""Waits for an object to reach a DELETED status or be not found (404). |
| 135 |
|
| 136 |
:param show_client: The tempest service client show method. |
| 137 |
Ex. cls.os_primary.servers_client.show_server |
| 138 |
:param id: The id of the object to query. |
| 139 |
:param status_key: The key of the status field in the response. |
| 140 |
Ex. provisioning_status |
| 141 |
:check_interval: How often to check the status, in seconds. |
| 142 |
:check_timeout: The maximum time, in seconds, to check the status. |
| 143 |
:root_tag: The root tag on the response to remove, if any. |
| 144 |
:raises CommandFailed: Raised if the object goes into ERROR and ERROR was |
| 145 |
not the desired status. |
| 146 |
:raises TimeoutException: The object did not achieve the status or ERROR in |
| 147 |
the check_timeout period. |
| 148 |
:returns: None |
| 149 |
""" |
| 150 |
start = int(time.time()) |
| 151 |
LOG.info('Waiting for %s status to update to DELETED or be not found(404)', |
| 152 |
show_client.__name__) |
| 153 |
while True: |
| 154 |
try: |
| 155 |
response = show_client(id, **kwargs) |
| 156 |
except exceptions.NotFound: |
| 157 |
return |
| 158 |
|
| 159 |
if root_tag: |
| 160 |
object_details = response[root_tag] |
| 161 |
else: |
| 162 |
object_details = response |
| 163 |
|
| 164 |
if object_details[status_key] == const.DELETED: |
| 165 |
LOG.info('%s\'s status updated to DELETED.', |
| 166 |
show_client.__name__) |
| 167 |
return |
| 168 |
elif int(time.time()) - start >= check_timeout: |
| 169 |
message = ( |
| 170 |
'{name} {field} failed to update to DELETED or become not ' |
| 171 |
'found (404) within the required time {timeout}. Current ' |
| 172 |
'status of {name}: {status}'.format( |
| 173 |
name=show_client.__name__, |
| 174 |
timeout=check_timeout, |
| 175 |
status=object_details[status_key], |
| 176 |
field=status_key |
| 177 |
)) |
| 178 |
caller = test_utils.find_test_caller() |
| 179 |
if caller: |
| 180 |
message = '({caller}) {message}'.format(caller=caller, |
| 181 |
message=message) |
| 182 |
raise exceptions.TimeoutException(message) |
| 183 |
|
| 184 |
time.sleep(check_interval) |
| 185 |
|
| 186 |
|
| 187 |
def wait_for_spare_amps(list_func, check_interval, check_timeout): |
| 188 |
"""Waits for amphorae in spare pool. |
| 189 |
|
| 190 |
:param list_func: The tempest service client amphora list method. |
| 191 |
Ex. cls.os_admin.amphora_client.list_amphorae |
| 192 |
:check_interval: How often to check the status, in seconds. |
| 193 |
:check_timeout: The maximum time, in seconds, to check the status. |
| 194 |
:raises TimeoutException: No amphora available in spare pool in the |
| 195 |
check_timeout period. |
| 196 |
:returns: A list of amphorae in spare pool. |
| 197 |
""" |
| 198 |
|
| 199 |
LOG.info('Waiting for amphorae in spare pool') |
| 200 |
start = int(time.time()) |
| 201 |
while True: |
| 202 |
spare_amps = list_func( |
| 203 |
query_params='{status}={status_ready}'.format( |
| 204 |
status=const.STATUS, status_ready=const.STATUS_READY)) |
| 205 |
if len(spare_amps) >= 1: |
| 206 |
return spare_amps |
| 207 |
if int(time.time()) - start >= check_timeout: |
| 208 |
message = ("No available amphorae in spare pool within the " |
| 209 |
"required time {timeout}.".format( |
| 210 |
timeout=check_timeout)) |
| 211 |
raise exceptions.TimeoutException(message) |
| 212 |
time.sleep(check_interval) |
| 213 |
|
| 214 |
|
| 215 |
def wait_until_true(func, timeout=60, sleep=1, **kwargs): |
| 216 |
"""Wait until callable predicate is evaluated as True |
| 217 |
|
| 218 |
:param func: Callable deciding whether waiting should continue. |
| 219 |
:param timeout: Timeout in seconds how long should function wait. |
| 220 |
:param sleep: Polling interval for results in seconds. |
| 221 |
""" |
| 222 |
start = int(time.time()) |
| 223 |
while True: |
| 224 |
try: |
| 225 |
ret = func(**kwargs) |
| 226 |
if ret: |
| 227 |
return |
| 228 |
except Exception as e: |
| 229 |
LOG.error(e) |
| 230 |
|
| 231 |
if int(time.time()) - start >= timeout: |
| 232 |
message = "Timed out after {timeout} seconds waiting".format( |
| 233 |
timeout=timeout) |
| 234 |
raise exceptions.TimeoutException(message) |
| 235 |
time.sleep(sleep) |