blob: 83aeeabc1cfa23c64f01e97d78405fe51841289a [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
15
16import time
17
wangxiyuandbd33dc2017-02-10 09:40:50 +080018from tempest.lib.common.utils import test_utils
Lenny Verkhovsky88625042016-03-08 17:44:00 +020019from tempest.lib import exceptions as lib_exc
Yuiko Takadab6527002015-12-07 11:49:12 +090020
21
Vladyslav Drok0ac08c82016-12-13 19:51:20 +020022def wait_for_bm_node_status(client, node_id, attr, status, timeout=None,
23 interval=None):
Yuiko Takadab6527002015-12-07 11:49:12 +090024 """Waits for a baremetal node attribute to reach given status.
25
Vladyslav Drok0ac08c82016-12-13 19:51:20 +020026 :param client: an instance of tempest plugin BaremetalClient.
27 :param node_id: identifier of the node.
28 :param attr: node's API-visible attribute to check status of.
29 :param status: desired status.
30 :param timeout: the timeout after which the check is considered as failed.
31 Defaults to client.build_timeout.
32 :param interval: an interval between show_node calls for status check.
33 Defaults to client.build_interval.
34
35 The client should have a show_node(node_id) method to get the node.
Yuiko Takadab6527002015-12-07 11:49:12 +090036 """
Vladyslav Drok0ac08c82016-12-13 19:51:20 +020037 if timeout is None:
38 timeout = client.build_timeout
39 if interval is None:
40 interval = client.build_interval
41 if timeout < 0 or interval < 0:
42 raise lib_exc.InvalidConfiguration(
43 'timeout and interval should be >= 0 or None, current values are: '
44 '%(timeout)s, %(interval)s respectively.' % dict(timeout=timeout,
45 interval=interval)
46 )
47
Yuiko Takadab6527002015-12-07 11:49:12 +090048 start = int(time.time())
Vladyslav Drok0ac08c82016-12-13 19:51:20 +020049 _, node = client.show_node(node_id)
Yuiko Takadab6527002015-12-07 11:49:12 +090050
51 while node[attr] != status:
Yuiko Takadab6527002015-12-07 11:49:12 +090052 status_curr = node[attr]
53 if status_curr == status:
54 return
55
Vladyslav Drok0ac08c82016-12-13 19:51:20 +020056 if int(time.time()) - start >= timeout:
Yuiko Takadab6527002015-12-07 11:49:12 +090057 message = ('Node %(node_id)s failed to reach %(attr)s=%(status)s '
58 'within the required time (%(timeout)s s).' %
59 {'node_id': node_id,
60 'attr': attr,
61 'status': status,
62 'timeout': client.build_timeout})
63 message += ' Current state of %s: %s.' % (attr, status_curr)
wangxiyuandbd33dc2017-02-10 09:40:50 +080064 caller = test_utils.find_test_caller()
Yuiko Takadab6527002015-12-07 11:49:12 +090065 if caller:
66 message = '(%s) %s' % (caller, message)
67 raise lib_exc.TimeoutException(message)
Vladyslav Drok0ac08c82016-12-13 19:51:20 +020068
69 time.sleep(interval)
70 _, node = client.show_node(node_id)