blob: 2b3527940fcb4e387b08e8c693c7e234bca3188b [file] [log] [blame]
Yuiko Takadab6527002015-12-07 11:49:12 +09001# 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 Kreger32737082020-04-16 16:55:28 -070015from oslo_log import log
Vladyslav Drok0cad0442016-12-14 12:48:20 +020016from tempest import config
wangxiyuandbd33dc2017-02-10 09:40:50 +080017from tempest.lib.common.utils import test_utils
Lenny Verkhovsky88625042016-03-08 17:44:00 +020018from tempest.lib import exceptions as lib_exc
Yuiko Takadab6527002015-12-07 11:49:12 +090019
Vladyslav Drok0cad0442016-12-14 12:48:20 +020020from ironic_tempest_plugin.common import utils
21
Julia Kreger32737082020-04-16 16:55:28 -070022LOG = log.getLogger(__name__)
23
Vladyslav Drok0cad0442016-12-14 12:48:20 +020024CONF = config.CONF
25
26
27def _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 Kajinami6bddaab2022-05-10 00:58:56 +090033 if (not isinstance(timeout, int)
34 or not isinstance(interval, int)
Riccardo Pittau441c5062020-03-30 15:06:28 +020035 or timeout < 0 or interval < 0):
Vladyslav Drok0cad0442016-12-14 12:48:20 +020036 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 Takadab6527002015-12-07 11:49:12 +090048
Vladyslav Drok0ac08c82016-12-13 19:51:20 +020049def wait_for_bm_node_status(client, node_id, attr, status, timeout=None,
Dmitry Tantsur4e2116d2019-08-27 17:01:58 +020050 interval=None, abort_on_error_state=False):
Yuiko Takadab6527002015-12-07 11:49:12 +090051 """Waits for a baremetal node attribute to reach given status.
52
Vladyslav Drok0ac08c82016-12-13 19:51:20 +020053 :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 Drok0cad0442016-12-14 12:48:20 +020056 :param status: desired status. Can be a list of statuses.
Vladyslav Drok0ac08c82016-12-13 19:51:20 +020057 :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 Tantsur4e2116d2019-08-27 17:01:58 +020061 :param abort_on_error_state: whether to abort waiting if the node reaches
62 an error state.
Vladyslav Drok0ac08c82016-12-13 19:51:20 +020063
64 The client should have a show_node(node_id) method to get the node.
Yuiko Takadab6527002015-12-07 11:49:12 +090065 """
Vladyslav Drok0cad0442016-12-14 12:48:20 +020066 timeout, interval = _determine_and_check_timeout_interval(
67 timeout, client.build_timeout, interval, client.build_interval)
Vladyslav Drok0ac08c82016-12-13 19:51:20 +020068
Vladyslav Drok0cad0442016-12-14 12:48:20 +020069 if not isinstance(status, list):
70 status = [status]
Yuiko Takadab6527002015-12-07 11:49:12 +090071
Vladyslav Drok0cad0442016-12-14 12:48:20 +020072 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 Tantsur4e2116d2019-08-27 17:01:58 +020076 elif (abort_on_error_state
77 and node['provision_state'].endswith(' failed')):
Julia Kreger32737082020-04-16 16:55:28 -070078 msg = ('Node %(node)s reached failure state %(state)s while '
79 'waiting for %(attr)s=%(expected)s. '
80 'Error: %(error)s' %
81 {'node': node_id, 'state': node['provision_state'],
82 'attr': attr, 'expected': status,
83 'error': node.get('last_error')})
84 LOG.debug(msg)
85 raise lib_exc.TempestException(msg)
Vladyslav Drok0cad0442016-12-14 12:48:20 +020086 return False
Yuiko Takadab6527002015-12-07 11:49:12 +090087
Vladyslav Drok0cad0442016-12-14 12:48:20 +020088 if not test_utils.call_until_true(is_attr_in_status, timeout,
89 interval):
90 message = ('Node %(node_id)s failed to reach %(attr)s=%(status)s '
91 'within the required time (%(timeout)s s).' %
92 {'node_id': node_id,
93 'attr': attr,
94 'status': status,
95 'timeout': timeout})
96 caller = test_utils.find_test_caller()
97 if caller:
98 message = '(%s) %s' % (caller, message)
Julia Kreger32737082020-04-16 16:55:28 -070099 LOG.debug(message)
Vladyslav Drok0cad0442016-12-14 12:48:20 +0200100 raise lib_exc.TimeoutException(message)
Vladyslav Drok0ac08c82016-12-13 19:51:20 +0200101
Vladyslav Drok0cad0442016-12-14 12:48:20 +0200102
103def wait_node_instance_association(client, instance_uuid, timeout=None,
104 interval=None):
105 """Waits for a node to be associated with instance_id.
106
107 :param client: an instance of tempest plugin BaremetalClient.
108 :param instance_uuid: UUID of the instance.
109 :param timeout: the timeout after which the check is considered as failed.
110 Defaults to CONF.baremetal.association_timeout.
111 :param interval: an interval between show_node calls for status check.
112 Defaults to client.build_interval.
113 """
114 timeout, interval = _determine_and_check_timeout_interval(
115 timeout, CONF.baremetal.association_timeout,
116 interval, client.build_interval)
117
118 def is_some_node_associated():
119 node = utils.get_node(client, instance_uuid=instance_uuid)
120 return node is not None
121
122 if not test_utils.call_until_true(is_some_node_associated, timeout,
123 interval):
Vladyslav Drok401fd462017-03-13 16:33:52 +0000124 msg = ('Timed out waiting to get Ironic node by instance UUID '
125 '%(instance_uuid)s within the required time (%(timeout)s s).'
126 % {'instance_uuid': instance_uuid, 'timeout': timeout})
Vladyslav Drok0cad0442016-12-14 12:48:20 +0200127 raise lib_exc.TimeoutException(msg)
Dmitry Tantsur47ff4892019-02-08 17:24:46 +0100128
129
130def wait_for_allocation(client, allocation_ident, timeout=15, interval=1,
131 expect_error=False):
132 """Wait for the allocation to become active.
133
134 :param client: an instance of tempest plugin BaremetalClient.
135 :param allocation_ident: UUID or name of the allocation.
136 :param timeout: the timeout after which the allocation is considered as
137 failed. Defaults to 15 seconds.
138 :param interval: an interval between show_allocation calls.
139 Defaults to 1 second.
140 :param expect_error: if True, return successfully even in case of an error.
141 """
142 result = [None] # a mutable object to modify in the closure
143
144 def check():
145 result[0] = client.show_allocation(allocation_ident)
146 allocation = result[0][1]
147
148 if allocation['state'] == 'error' and not expect_error:
149 raise lib_exc.TempestException(
150 "Allocation %(ident)s failed: %(error)s" %
151 {'ident': allocation_ident,
152 'error': allocation.get('last_error')})
153 else:
154 return allocation['state'] != 'allocating'
155
156 if not test_utils.call_until_true(check, timeout, interval):
157 msg = ('Timed out waiting for the allocation %s to become active' %
158 allocation_ident)
159 raise lib_exc.TimeoutException(msg)
160
161 return result[0]