Jude Cross | 986e3f5 | 2017-07-24 14:57:20 -0700 | [diff] [blame] | 1 | # Copyright 2017 GoDaddy |
| 2 | # Copyright 2018 Rackspace US Inc. 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 | |
| 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, |
Adam Harwell | de3e054 | 2018-05-03 18:21:06 -0700 | [diff] [blame] | 30 | check_interval, check_timeout, root_tag=None, |
| 31 | **kwargs): |
Jude Cross | 986e3f5 | 2017-07-24 14:57:20 -0700 | [diff] [blame] | 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 | :raises CommandFailed: Raised if the object goes into ERROR and ERROR was |
| 44 | not the desired status. |
| 45 | :raises TimeoutException: The object did not achieve the status or ERROR in |
| 46 | the check_timeout period. |
| 47 | :returns: The object details from the show client. |
| 48 | """ |
| 49 | start = int(time.time()) |
| 50 | LOG.info('Waiting for {name} status to update to {status}'.format( |
| 51 | name=show_client.__name__, status=status)) |
| 52 | while True: |
Adam Harwell | a795ae6 | 2018-04-23 12:11:19 -0700 | [diff] [blame] | 53 | if status == const.DELETED: |
| 54 | try: |
Adam Harwell | de3e054 | 2018-05-03 18:21:06 -0700 | [diff] [blame] | 55 | response = show_client(id, **kwargs) |
Adam Harwell | a795ae6 | 2018-04-23 12:11:19 -0700 | [diff] [blame] | 56 | except exceptions.NotFound: |
| 57 | return |
| 58 | else: |
Adam Harwell | de3e054 | 2018-05-03 18:21:06 -0700 | [diff] [blame] | 59 | response = show_client(id, **kwargs) |
Adam Harwell | a795ae6 | 2018-04-23 12:11:19 -0700 | [diff] [blame] | 60 | |
Jude Cross | 986e3f5 | 2017-07-24 14:57:20 -0700 | [diff] [blame] | 61 | if root_tag: |
| 62 | object_details = response[root_tag] |
| 63 | else: |
| 64 | object_details = response |
| 65 | |
| 66 | if object_details[status_key] == status: |
| 67 | LOG.info('{name}\'s status updated to {status}.'.format( |
| 68 | name=show_client.__name__, status=status)) |
| 69 | return object_details |
| 70 | elif object_details[status_key] == 'ERROR': |
| 71 | message = ('{name} {field} updated to an invalid state of ' |
| 72 | 'ERROR'.format(name=show_client.__name__, |
| 73 | field=status_key)) |
| 74 | caller = test_utils.find_test_caller() |
| 75 | if caller: |
| 76 | message = '({caller}) {message}'.format(caller=caller, |
| 77 | message=message) |
| 78 | raise exceptions.UnexpectedResponseCode(message) |
| 79 | elif int(time.time()) - start >= check_timeout: |
| 80 | message = ( |
| 81 | '{name} {field} failed to update to {expected_status} within ' |
| 82 | 'the required time {timeout}. Current status of {name}: ' |
| 83 | '{status}'.format( |
| 84 | name=show_client.__name__, |
| 85 | timeout=check_timeout, |
| 86 | status=object_details[status_key], |
| 87 | expected_status=status, |
| 88 | field=status_key |
| 89 | )) |
| 90 | caller = test_utils.find_test_caller() |
| 91 | if caller: |
| 92 | message = '({caller}) {message}'.format(caller=caller, |
| 93 | message=message) |
| 94 | raise exceptions.TimeoutException(message) |
| 95 | |
| 96 | time.sleep(check_interval) |
| 97 | |
| 98 | |
| 99 | def wait_for_not_found(delete_func, show_func, *args, **kwargs): |
| 100 | """Call the delete function, then wait for it to be 'NotFound' |
| 101 | |
| 102 | :param delete_func: The delete function to call. |
| 103 | :param show_func: The show function to call looking for 'NotFound'. |
| 104 | :param ID: The ID of the object to delete/show. |
| 105 | :raises TimeoutException: The object did not achieve the status or ERROR in |
| 106 | the check_timeout period. |
| 107 | :returns: None |
| 108 | """ |
| 109 | try: |
Jude Cross | fbbd2b4 | 2017-08-09 15:21:04 -0700 | [diff] [blame] | 110 | delete_func(*args, **kwargs) |
Jude Cross | 986e3f5 | 2017-07-24 14:57:20 -0700 | [diff] [blame] | 111 | except exceptions.NotFound: |
| 112 | return |
| 113 | start = int(time.time()) |
| 114 | LOG.info('Waiting for object to be NotFound') |
| 115 | while True: |
| 116 | try: |
| 117 | show_func(*args, **kwargs) |
| 118 | except exceptions.NotFound: |
| 119 | return |
| 120 | if int(time.time()) - start >= CONF.load_balancer.check_timeout: |
| 121 | message = ('{name} did not raise NotFound in {timeout} ' |
| 122 | 'seconds.'.format( |
| 123 | name=show_func.__name__, |
| 124 | timeout=CONF.load_balancer.check_timeout)) |
| 125 | raise exceptions.TimeoutException(message) |
| 126 | time.sleep(CONF.load_balancer.check_interval) |
| 127 | |
| 128 | |
| 129 | def wait_for_deleted_status_or_not_found( |
| 130 | show_client, id, status_key, check_interval, check_timeout, |
Adam Harwell | de3e054 | 2018-05-03 18:21:06 -0700 | [diff] [blame] | 131 | root_tag=None, **kwargs): |
Jude Cross | 986e3f5 | 2017-07-24 14:57:20 -0700 | [diff] [blame] | 132 | """Waits for an object to reach a DELETED status or be not found (404). |
| 133 | |
| 134 | :param show_client: The tempest service client show method. |
| 135 | Ex. cls.os_primary.servers_client.show_server |
| 136 | :param id: The id of the object to query. |
| 137 | :param status_key: The key of the status field in the response. |
| 138 | Ex. provisioning_status |
| 139 | :check_interval: How often to check the status, in seconds. |
| 140 | :check_timeout: The maximum time, in seconds, to check the status. |
| 141 | :root_tag: The root tag on the response to remove, if any. |
| 142 | :raises CommandFailed: Raised if the object goes into ERROR and ERROR was |
| 143 | not the desired status. |
| 144 | :raises TimeoutException: The object did not achieve the status or ERROR in |
| 145 | the check_timeout period. |
| 146 | :returns: None |
| 147 | """ |
| 148 | start = int(time.time()) |
| 149 | LOG.info('Waiting for {name} status to update to DELETED or be not ' |
| 150 | 'found(404)'.format(name=show_client.__name__)) |
| 151 | while True: |
| 152 | try: |
Adam Harwell | de3e054 | 2018-05-03 18:21:06 -0700 | [diff] [blame] | 153 | response = show_client(id, **kwargs) |
Jude Cross | 986e3f5 | 2017-07-24 14:57:20 -0700 | [diff] [blame] | 154 | except exceptions.NotFound: |
| 155 | return |
| 156 | |
| 157 | if root_tag: |
| 158 | object_details = response[root_tag] |
| 159 | else: |
| 160 | object_details = response |
| 161 | |
| 162 | if object_details[status_key] == const.DELETED: |
| 163 | LOG.info('{name}\'s status updated to DELETED.'.format( |
| 164 | name=show_client.__name__)) |
| 165 | return |
| 166 | elif int(time.time()) - start >= check_timeout: |
| 167 | message = ( |
| 168 | '{name} {field} failed to update to DELETED or become not ' |
| 169 | 'found (404) within the required time {timeout}. Current ' |
| 170 | 'status of {name}: {status}'.format( |
| 171 | name=show_client.__name__, |
| 172 | timeout=check_timeout, |
| 173 | status=object_details[status_key], |
| 174 | field=status_key |
| 175 | )) |
| 176 | caller = test_utils.find_test_caller() |
| 177 | if caller: |
| 178 | message = '({caller}) {message}'.format(caller=caller, |
| 179 | message=message) |
| 180 | raise exceptions.TimeoutException(message) |
| 181 | |
| 182 | time.sleep(check_interval) |