Yuiko Takada | b652700 | 2015-12-07 11:49:12 +0900 | [diff] [blame] | 1 | # All Rights Reserved. |
| 2 | # |
| 3 | # Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 4 | # not use this file except in compliance with the License. You may obtain |
| 5 | # a copy of the License at |
| 6 | # |
| 7 | # http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | # |
| 9 | # Unless required by applicable law or agreed to in writing, software |
| 10 | # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 11 | # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 12 | # License for the specific language governing permissions and limitations |
| 13 | # under the License. |
| 14 | |
Julia Kreger | 3273708 | 2020-04-16 16:55:28 -0700 | [diff] [blame] | 15 | from oslo_log import log |
Vladyslav Drok | 0cad044 | 2016-12-14 12:48:20 +0200 | [diff] [blame] | 16 | from tempest import config |
wangxiyuan | dbd33dc | 2017-02-10 09:40:50 +0800 | [diff] [blame] | 17 | from tempest.lib.common.utils import test_utils |
Lenny Verkhovsky | 8862504 | 2016-03-08 17:44:00 +0200 | [diff] [blame] | 18 | from tempest.lib import exceptions as lib_exc |
Yuiko Takada | b652700 | 2015-12-07 11:49:12 +0900 | [diff] [blame] | 19 | |
Vladyslav Drok | 0cad044 | 2016-12-14 12:48:20 +0200 | [diff] [blame] | 20 | from ironic_tempest_plugin.common import utils |
| 21 | |
Julia Kreger | 3273708 | 2020-04-16 16:55:28 -0700 | [diff] [blame] | 22 | LOG = log.getLogger(__name__) |
| 23 | |
Vladyslav Drok | 0cad044 | 2016-12-14 12:48:20 +0200 | [diff] [blame] | 24 | CONF = config.CONF |
| 25 | |
| 26 | |
| 27 | def _determine_and_check_timeout_interval(timeout, default_timeout, |
| 28 | interval, default_interval): |
| 29 | if timeout is None: |
| 30 | timeout = default_timeout |
| 31 | if interval is None: |
| 32 | interval = default_interval |
Takashi Kajinami | 6bddaab | 2022-05-10 00:58:56 +0900 | [diff] [blame] | 33 | if (not isinstance(timeout, int) |
| 34 | or not isinstance(interval, int) |
Riccardo Pittau | 441c506 | 2020-03-30 15:06:28 +0200 | [diff] [blame] | 35 | or timeout < 0 or interval < 0): |
Vladyslav Drok | 0cad044 | 2016-12-14 12:48:20 +0200 | [diff] [blame] | 36 | raise AssertionError( |
| 37 | 'timeout and interval should be >= 0 or None, current values are: ' |
| 38 | '%(timeout)s, %(interval)s respectively. If timeout and/or ' |
| 39 | 'interval are None, the default_timeout and default_interval are ' |
| 40 | 'used, and they should be integers >= 0, current values are: ' |
| 41 | '%(default_timeout)s, %(default_interval)s respectively.' % dict( |
| 42 | timeout=timeout, interval=interval, |
| 43 | default_timeout=default_timeout, |
| 44 | default_interval=default_interval) |
| 45 | ) |
| 46 | return timeout, interval |
| 47 | |
Yuiko Takada | b652700 | 2015-12-07 11:49:12 +0900 | [diff] [blame] | 48 | |
Vladyslav Drok | 0ac08c8 | 2016-12-13 19:51:20 +0200 | [diff] [blame] | 49 | def wait_for_bm_node_status(client, node_id, attr, status, timeout=None, |
Dmitry Tantsur | 4e2116d | 2019-08-27 17:01:58 +0200 | [diff] [blame] | 50 | interval=None, abort_on_error_state=False): |
Yuiko Takada | b652700 | 2015-12-07 11:49:12 +0900 | [diff] [blame] | 51 | """Waits for a baremetal node attribute to reach given status. |
| 52 | |
Vladyslav Drok | 0ac08c8 | 2016-12-13 19:51:20 +0200 | [diff] [blame] | 53 | :param client: an instance of tempest plugin BaremetalClient. |
| 54 | :param node_id: identifier of the node. |
| 55 | :param attr: node's API-visible attribute to check status of. |
Vladyslav Drok | 0cad044 | 2016-12-14 12:48:20 +0200 | [diff] [blame] | 56 | :param status: desired status. Can be a list of statuses. |
Vladyslav Drok | 0ac08c8 | 2016-12-13 19:51:20 +0200 | [diff] [blame] | 57 | :param timeout: the timeout after which the check is considered as failed. |
| 58 | Defaults to client.build_timeout. |
| 59 | :param interval: an interval between show_node calls for status check. |
| 60 | Defaults to client.build_interval. |
Dmitry Tantsur | 4e2116d | 2019-08-27 17:01:58 +0200 | [diff] [blame] | 61 | :param abort_on_error_state: whether to abort waiting if the node reaches |
| 62 | an error state. |
Vladyslav Drok | 0ac08c8 | 2016-12-13 19:51:20 +0200 | [diff] [blame] | 63 | |
| 64 | The client should have a show_node(node_id) method to get the node. |
Yuiko Takada | b652700 | 2015-12-07 11:49:12 +0900 | [diff] [blame] | 65 | """ |
Vladyslav Drok | 0cad044 | 2016-12-14 12:48:20 +0200 | [diff] [blame] | 66 | timeout, interval = _determine_and_check_timeout_interval( |
| 67 | timeout, client.build_timeout, interval, client.build_interval) |
Vladyslav Drok | 0ac08c8 | 2016-12-13 19:51:20 +0200 | [diff] [blame] | 68 | |
Vladyslav Drok | 0cad044 | 2016-12-14 12:48:20 +0200 | [diff] [blame] | 69 | if not isinstance(status, list): |
| 70 | status = [status] |
Yuiko Takada | b652700 | 2015-12-07 11:49:12 +0900 | [diff] [blame] | 71 | |
Vladyslav Drok | 0cad044 | 2016-12-14 12:48:20 +0200 | [diff] [blame] | 72 | def is_attr_in_status(): |
| 73 | node = utils.get_node(client, node_id=node_id) |
| 74 | if node[attr] in status: |
| 75 | return True |
Dmitry Tantsur | 4e2116d | 2019-08-27 17:01:58 +0200 | [diff] [blame] | 76 | elif (abort_on_error_state |
Julia Kreger | 982d177 | 2022-03-30 16:40:44 -0700 | [diff] [blame] | 77 | and (node['provision_state'].endswith(' failed') |
| 78 | or node['provision_state'] == 'error')): |
Julia Kreger | 3273708 | 2020-04-16 16:55:28 -0700 | [diff] [blame] | 79 | msg = ('Node %(node)s reached failure state %(state)s while ' |
| 80 | 'waiting for %(attr)s=%(expected)s. ' |
| 81 | 'Error: %(error)s' % |
| 82 | {'node': node_id, 'state': node['provision_state'], |
| 83 | 'attr': attr, 'expected': status, |
| 84 | 'error': node.get('last_error')}) |
| 85 | LOG.debug(msg) |
| 86 | raise lib_exc.TempestException(msg) |
Vladyslav Drok | 0cad044 | 2016-12-14 12:48:20 +0200 | [diff] [blame] | 87 | return False |
Yuiko Takada | b652700 | 2015-12-07 11:49:12 +0900 | [diff] [blame] | 88 | |
Vladyslav Drok | 0cad044 | 2016-12-14 12:48:20 +0200 | [diff] [blame] | 89 | if not test_utils.call_until_true(is_attr_in_status, timeout, |
| 90 | interval): |
| 91 | message = ('Node %(node_id)s failed to reach %(attr)s=%(status)s ' |
| 92 | 'within the required time (%(timeout)s s).' % |
| 93 | {'node_id': node_id, |
| 94 | 'attr': attr, |
| 95 | 'status': status, |
| 96 | 'timeout': timeout}) |
| 97 | caller = test_utils.find_test_caller() |
| 98 | if caller: |
| 99 | message = '(%s) %s' % (caller, message) |
Julia Kreger | 3273708 | 2020-04-16 16:55:28 -0700 | [diff] [blame] | 100 | LOG.debug(message) |
Vladyslav Drok | 0cad044 | 2016-12-14 12:48:20 +0200 | [diff] [blame] | 101 | raise lib_exc.TimeoutException(message) |
Vladyslav Drok | 0ac08c8 | 2016-12-13 19:51:20 +0200 | [diff] [blame] | 102 | |
Vladyslav Drok | 0cad044 | 2016-12-14 12:48:20 +0200 | [diff] [blame] | 103 | |
| 104 | def wait_node_instance_association(client, instance_uuid, timeout=None, |
| 105 | interval=None): |
| 106 | """Waits for a node to be associated with instance_id. |
| 107 | |
| 108 | :param client: an instance of tempest plugin BaremetalClient. |
| 109 | :param instance_uuid: UUID of the instance. |
| 110 | :param timeout: the timeout after which the check is considered as failed. |
| 111 | Defaults to CONF.baremetal.association_timeout. |
| 112 | :param interval: an interval between show_node calls for status check. |
| 113 | Defaults to client.build_interval. |
| 114 | """ |
| 115 | timeout, interval = _determine_and_check_timeout_interval( |
| 116 | timeout, CONF.baremetal.association_timeout, |
| 117 | interval, client.build_interval) |
| 118 | |
| 119 | def is_some_node_associated(): |
| 120 | node = utils.get_node(client, instance_uuid=instance_uuid) |
| 121 | return node is not None |
| 122 | |
| 123 | if not test_utils.call_until_true(is_some_node_associated, timeout, |
| 124 | interval): |
Vladyslav Drok | 401fd46 | 2017-03-13 16:33:52 +0000 | [diff] [blame] | 125 | msg = ('Timed out waiting to get Ironic node by instance UUID ' |
| 126 | '%(instance_uuid)s within the required time (%(timeout)s s).' |
| 127 | % {'instance_uuid': instance_uuid, 'timeout': timeout}) |
Vladyslav Drok | 0cad044 | 2016-12-14 12:48:20 +0200 | [diff] [blame] | 128 | raise lib_exc.TimeoutException(msg) |
Dmitry Tantsur | 47ff489 | 2019-02-08 17:24:46 +0100 | [diff] [blame] | 129 | |
| 130 | |
| 131 | def wait_for_allocation(client, allocation_ident, timeout=15, interval=1, |
| 132 | expect_error=False): |
| 133 | """Wait for the allocation to become active. |
| 134 | |
| 135 | :param client: an instance of tempest plugin BaremetalClient. |
| 136 | :param allocation_ident: UUID or name of the allocation. |
| 137 | :param timeout: the timeout after which the allocation is considered as |
| 138 | failed. Defaults to 15 seconds. |
| 139 | :param interval: an interval between show_allocation calls. |
| 140 | Defaults to 1 second. |
| 141 | :param expect_error: if True, return successfully even in case of an error. |
| 142 | """ |
| 143 | result = [None] # a mutable object to modify in the closure |
| 144 | |
| 145 | def check(): |
| 146 | result[0] = client.show_allocation(allocation_ident) |
| 147 | allocation = result[0][1] |
| 148 | |
| 149 | if allocation['state'] == 'error' and not expect_error: |
| 150 | raise lib_exc.TempestException( |
| 151 | "Allocation %(ident)s failed: %(error)s" % |
| 152 | {'ident': allocation_ident, |
| 153 | 'error': allocation.get('last_error')}) |
| 154 | else: |
| 155 | return allocation['state'] != 'allocating' |
| 156 | |
| 157 | if not test_utils.call_until_true(check, timeout, interval): |
| 158 | msg = ('Timed out waiting for the allocation %s to become active' % |
| 159 | allocation_ident) |
| 160 | raise lib_exc.TimeoutException(msg) |
| 161 | |
| 162 | return result[0] |
Julia Kreger | 982d177 | 2022-03-30 16:40:44 -0700 | [diff] [blame] | 163 | |
| 164 | |
| 165 | def wait_node_value_in_field(client, node_id, field, value, |
| 166 | raise_if_insufficent_access=True, |
| 167 | timeout=None, interval=None): |
| 168 | """Waits for a node to have a field value appear. |
| 169 | |
| 170 | :param client: an instance of tempest plugin BaremetalClient. |
| 171 | :param node_id: the UUID of the node |
| 172 | :param field: the field in the node object to examine |
| 173 | :param value: the value/key with-in the field to look for. |
| 174 | :param timeout: the timeout after which the check is considered as failed. |
| 175 | :param interval: an interval between show_node calls for status check. |
| 176 | """ |
| 177 | |
| 178 | def is_field_updated(): |
| 179 | node = utils.get_node(client, node_id=node_id) |
| 180 | field_value = node[field] |
| 181 | if raise_if_insufficent_access and '** Redacted' in field_value: |
| 182 | msg = ('Unable to see contents of redacted field ' |
| 183 | 'indicating insufficent access to execute this test.') |
| 184 | raise lib_exc.InsufficientAPIAccess(msg) |
| 185 | return value in field_value |
| 186 | |
| 187 | if not test_utils.call_until_true(is_field_updated, timeout, |
| 188 | interval): |
| 189 | msg = ('Timed out waiting to get Ironic node by node_id ' |
| 190 | '%(node_id)s within the required time (%(timeout)s s). ' |
| 191 | 'Field value %(value) did not appear in field %(field)s.' |
| 192 | % {'node_id': node_id, 'timeout': timeout, |
| 193 | 'field': field, 'value': value}) |
| 194 | raise lib_exc.TimeoutException(msg) |