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 | |
| 15 | |
| 16 | import time |
| 17 | |
Lenny Verkhovsky | 8862504 | 2016-03-08 17:44:00 +0200 | [diff] [blame] | 18 | from tempest.lib.common.utils import misc as misc_utils |
| 19 | from tempest.lib import exceptions as lib_exc |
Yuiko Takada | b652700 | 2015-12-07 11:49:12 +0900 | [diff] [blame] | 20 | |
| 21 | |
Vladyslav Drok | 0ac08c8 | 2016-12-13 19:51:20 +0200 | [diff] [blame^] | 22 | def wait_for_bm_node_status(client, node_id, attr, status, timeout=None, |
| 23 | interval=None): |
Yuiko Takada | b652700 | 2015-12-07 11:49:12 +0900 | [diff] [blame] | 24 | """Waits for a baremetal node attribute to reach given status. |
| 25 | |
Vladyslav Drok | 0ac08c8 | 2016-12-13 19:51:20 +0200 | [diff] [blame^] | 26 | :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 Takada | b652700 | 2015-12-07 11:49:12 +0900 | [diff] [blame] | 36 | """ |
Vladyslav Drok | 0ac08c8 | 2016-12-13 19:51:20 +0200 | [diff] [blame^] | 37 | 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 Takada | b652700 | 2015-12-07 11:49:12 +0900 | [diff] [blame] | 48 | start = int(time.time()) |
Vladyslav Drok | 0ac08c8 | 2016-12-13 19:51:20 +0200 | [diff] [blame^] | 49 | _, node = client.show_node(node_id) |
Yuiko Takada | b652700 | 2015-12-07 11:49:12 +0900 | [diff] [blame] | 50 | |
| 51 | while node[attr] != status: |
Yuiko Takada | b652700 | 2015-12-07 11:49:12 +0900 | [diff] [blame] | 52 | status_curr = node[attr] |
| 53 | if status_curr == status: |
| 54 | return |
| 55 | |
Vladyslav Drok | 0ac08c8 | 2016-12-13 19:51:20 +0200 | [diff] [blame^] | 56 | if int(time.time()) - start >= timeout: |
Yuiko Takada | b652700 | 2015-12-07 11:49:12 +0900 | [diff] [blame] | 57 | 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) |
| 64 | caller = misc_utils.find_test_caller() |
| 65 | if caller: |
| 66 | message = '(%s) %s' % (caller, message) |
| 67 | raise lib_exc.TimeoutException(message) |
Vladyslav Drok | 0ac08c8 | 2016-12-13 19:51:20 +0200 | [diff] [blame^] | 68 | |
| 69 | time.sleep(interval) |
| 70 | _, node = client.show_node(node_id) |