blob: 08e09411daf93b3081a4f0a5d7c0709549f0aa6d [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
Vladyslav Drok0cad0442016-12-14 12:48:20 +020015import six
16from 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
22CONF = config.CONF
23
24
25def _determine_and_check_timeout_interval(timeout, default_timeout,
26 interval, default_interval):
27 if timeout is None:
28 timeout = default_timeout
29 if interval is None:
30 interval = default_interval
31 if (not isinstance(timeout, six.integer_types) or
32 not isinstance(interval, six.integer_types) or
33 timeout < 0 or interval < 0):
34 raise AssertionError(
35 'timeout and interval should be >= 0 or None, current values are: '
36 '%(timeout)s, %(interval)s respectively. If timeout and/or '
37 'interval are None, the default_timeout and default_interval are '
38 'used, and they should be integers >= 0, current values are: '
39 '%(default_timeout)s, %(default_interval)s respectively.' % dict(
40 timeout=timeout, interval=interval,
41 default_timeout=default_timeout,
42 default_interval=default_interval)
43 )
44 return timeout, interval
45
Yuiko Takadab6527002015-12-07 11:49:12 +090046
Vladyslav Drok0ac08c82016-12-13 19:51:20 +020047def wait_for_bm_node_status(client, node_id, attr, status, timeout=None,
48 interval=None):
Yuiko Takadab6527002015-12-07 11:49:12 +090049 """Waits for a baremetal node attribute to reach given status.
50
Vladyslav Drok0ac08c82016-12-13 19:51:20 +020051 :param client: an instance of tempest plugin BaremetalClient.
52 :param node_id: identifier of the node.
53 :param attr: node's API-visible attribute to check status of.
Vladyslav Drok0cad0442016-12-14 12:48:20 +020054 :param status: desired status. Can be a list of statuses.
Vladyslav Drok0ac08c82016-12-13 19:51:20 +020055 :param timeout: the timeout after which the check is considered as failed.
56 Defaults to client.build_timeout.
57 :param interval: an interval between show_node calls for status check.
58 Defaults to client.build_interval.
59
60 The client should have a show_node(node_id) method to get the node.
Yuiko Takadab6527002015-12-07 11:49:12 +090061 """
Vladyslav Drok0cad0442016-12-14 12:48:20 +020062 timeout, interval = _determine_and_check_timeout_interval(
63 timeout, client.build_timeout, interval, client.build_interval)
Vladyslav Drok0ac08c82016-12-13 19:51:20 +020064
Vladyslav Drok0cad0442016-12-14 12:48:20 +020065 if not isinstance(status, list):
66 status = [status]
Yuiko Takadab6527002015-12-07 11:49:12 +090067
Vladyslav Drok0cad0442016-12-14 12:48:20 +020068 def is_attr_in_status():
69 node = utils.get_node(client, node_id=node_id)
70 if node[attr] in status:
71 return True
72 return False
Yuiko Takadab6527002015-12-07 11:49:12 +090073
Vladyslav Drok0cad0442016-12-14 12:48:20 +020074 if not test_utils.call_until_true(is_attr_in_status, timeout,
75 interval):
76 message = ('Node %(node_id)s failed to reach %(attr)s=%(status)s '
77 'within the required time (%(timeout)s s).' %
78 {'node_id': node_id,
79 'attr': attr,
80 'status': status,
81 'timeout': timeout})
82 caller = test_utils.find_test_caller()
83 if caller:
84 message = '(%s) %s' % (caller, message)
85 raise lib_exc.TimeoutException(message)
Vladyslav Drok0ac08c82016-12-13 19:51:20 +020086
Vladyslav Drok0cad0442016-12-14 12:48:20 +020087
88def wait_node_instance_association(client, instance_uuid, timeout=None,
89 interval=None):
90 """Waits for a node to be associated with instance_id.
91
92 :param client: an instance of tempest plugin BaremetalClient.
93 :param instance_uuid: UUID of the instance.
94 :param timeout: the timeout after which the check is considered as failed.
95 Defaults to CONF.baremetal.association_timeout.
96 :param interval: an interval between show_node calls for status check.
97 Defaults to client.build_interval.
98 """
99 timeout, interval = _determine_and_check_timeout_interval(
100 timeout, CONF.baremetal.association_timeout,
101 interval, client.build_interval)
102
103 def is_some_node_associated():
104 node = utils.get_node(client, instance_uuid=instance_uuid)
105 return node is not None
106
107 if not test_utils.call_until_true(is_some_node_associated, timeout,
108 interval):
Vladyslav Drok401fd462017-03-13 16:33:52 +0000109 msg = ('Timed out waiting to get Ironic node by instance UUID '
110 '%(instance_uuid)s within the required time (%(timeout)s s).'
111 % {'instance_uuid': instance_uuid, 'timeout': timeout})
Vladyslav Drok0cad0442016-12-14 12:48:20 +0200112 raise lib_exc.TimeoutException(msg)
Dmitry Tantsur47ff4892019-02-08 17:24:46 +0100113
114
115def wait_for_allocation(client, allocation_ident, timeout=15, interval=1,
116 expect_error=False):
117 """Wait for the allocation to become active.
118
119 :param client: an instance of tempest plugin BaremetalClient.
120 :param allocation_ident: UUID or name of the allocation.
121 :param timeout: the timeout after which the allocation is considered as
122 failed. Defaults to 15 seconds.
123 :param interval: an interval between show_allocation calls.
124 Defaults to 1 second.
125 :param expect_error: if True, return successfully even in case of an error.
126 """
127 result = [None] # a mutable object to modify in the closure
128
129 def check():
130 result[0] = client.show_allocation(allocation_ident)
131 allocation = result[0][1]
132
133 if allocation['state'] == 'error' and not expect_error:
134 raise lib_exc.TempestException(
135 "Allocation %(ident)s failed: %(error)s" %
136 {'ident': allocation_ident,
137 'error': allocation.get('last_error')})
138 else:
139 return allocation['state'] != 'allocating'
140
141 if not test_utils.call_until_true(check, timeout, interval):
142 msg = ('Timed out waiting for the allocation %s to become active' %
143 allocation_ident)
144 raise lib_exc.TimeoutException(msg)
145
146 return result[0]